joypad_osx.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**************************************************************************/
  2. /* joypad_osx.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 JOYPAD_OSX_H
  31. #define JOYPAD_OSX_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 = 0;
  57. bool offset_hat = false;
  58. io_service_t ffservice = 0; /* Interface for force feedback, 0 = no ff */
  59. FFCONSTANTFORCE ff_constant_force;
  60. FFDeviceObjectReference ff_device = nullptr;
  61. FFEffectObjectReference ff_object = nullptr;
  62. uint64_t ff_timestamp = 0;
  63. LONG *ff_directions = nullptr;
  64. FFEFFECT ff_effect;
  65. DWORD *ff_axes = nullptr;
  66. void add_hid_elements(CFArrayRef p_array);
  67. void add_hid_element(IOHIDElementRef p_element);
  68. bool has_element(IOHIDElementCookie p_cookie, Vector<rec_element> *p_list) const;
  69. bool config_force_feedback(io_service_t p_service);
  70. bool check_ff_features();
  71. int get_hid_element_state(rec_element *p_element) const;
  72. void free();
  73. joypad();
  74. };
  75. class JoypadOSX {
  76. enum {
  77. JOYPADS_MAX = 16,
  78. };
  79. private:
  80. InputDefault *input;
  81. IOHIDManagerRef hid_manager;
  82. Vector<joypad> device_list;
  83. bool have_device(IOHIDDeviceRef p_device) const;
  84. bool configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy);
  85. int get_joy_index(int p_id) const;
  86. int get_joy_ref(IOHIDDeviceRef p_device) const;
  87. void poll_joypads() const;
  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 // JOYPAD_OSX_H