joypad_osx.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*************************************************************************/
  2. /* joypad_osx.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef JOYPADOSX_H
  31. #define JOYPADOSX_H
  32. #ifdef MACOS_10_0_4
  33. #include <IOKit/hidsystem/IOHIDUsageTables.h>
  34. #else
  35. #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
  36. #endif
  37. #include <ForceFeedback/ForceFeedback.h>
  38. #include <ForceFeedback/ForceFeedbackConstants.h>
  39. #include <IOKit/hid/IOHIDLib.h>
  40. #include "main/input_default.h"
  41. struct rec_element {
  42. IOHIDElementRef ref;
  43. IOHIDElementCookie cookie;
  44. uint32_t usage;
  45. int min;
  46. int max;
  47. struct Comparator {
  48. bool operator()(const rec_element p_a, const rec_element p_b) const { return p_a.usage < p_b.usage; }
  49. };
  50. };
  51. struct joypad {
  52. IOHIDDeviceRef device_ref;
  53. Vector<rec_element> axis_elements;
  54. Vector<rec_element> button_elements;
  55. Vector<rec_element> hat_elements;
  56. int id;
  57. io_service_t ffservice; /* Interface for force feedback, 0 = no ff */
  58. FFCONSTANTFORCE ff_constant_force;
  59. FFDeviceObjectReference ff_device;
  60. FFEffectObjectReference ff_object;
  61. uint64_t ff_timestamp;
  62. LONG *ff_directions;
  63. FFEFFECT ff_effect;
  64. DWORD *ff_axes;
  65. void add_hid_elements(CFArrayRef p_array);
  66. void add_hid_element(IOHIDElementRef p_element);
  67. bool has_element(IOHIDElementCookie p_cookie, Vector<rec_element> *p_list) const;
  68. bool config_force_feedback(io_service_t p_service);
  69. bool check_ff_features();
  70. int get_hid_element_state(rec_element *p_element) const;
  71. void free();
  72. joypad();
  73. };
  74. class JoypadOSX {
  75. enum {
  76. JOYPADS_MAX = 16,
  77. };
  78. private:
  79. InputDefault *input;
  80. IOHIDManagerRef hid_manager;
  81. Vector<joypad> device_list;
  82. bool have_device(IOHIDDeviceRef p_device) const;
  83. bool configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy);
  84. int get_joy_index(int p_id) const;
  85. int get_joy_ref(IOHIDDeviceRef p_device) const;
  86. void poll_joypads() const;
  87. void setup_joypad_objects();
  88. void config_hid_manager(CFArrayRef p_matching_array) const;
  89. void joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp);
  90. void joypad_vibration_stop(int p_id, uint64_t p_timestamp);
  91. public:
  92. void process_joypads();
  93. void _device_added(IOReturn p_res, IOHIDDeviceRef p_device);
  94. void _device_removed(IOReturn p_res, IOHIDDeviceRef p_device);
  95. JoypadOSX();
  96. ~JoypadOSX();
  97. };
  98. #endif // JOYPADOSX_H