joypad_macos.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**************************************************************************/
  2. /* joypad_macos.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_MACOS_H
  31. #define JOYPAD_MACOS_H
  32. #include "core/input/input.h"
  33. #import <ForceFeedback/ForceFeedback.h>
  34. #import <ForceFeedback/ForceFeedbackConstants.h>
  35. #import <IOKit/hid/IOHIDLib.h>
  36. #import <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
  37. struct rec_element {
  38. IOHIDElementRef ref;
  39. IOHIDElementCookie cookie;
  40. uint32_t usage = 0;
  41. int min = 0;
  42. int max = 0;
  43. struct Comparator {
  44. bool operator()(const rec_element p_a, const rec_element p_b) const { return p_a.usage < p_b.usage; }
  45. };
  46. };
  47. struct joypad {
  48. IOHIDDeviceRef device_ref = nullptr;
  49. Vector<rec_element> axis_elements;
  50. Vector<rec_element> button_elements;
  51. Vector<rec_element> hat_elements;
  52. int id = 0;
  53. bool offset_hat = false;
  54. io_service_t ffservice = 0; // Interface for force feedback, 0 = no ff.
  55. FFCONSTANTFORCE ff_constant_force;
  56. FFDeviceObjectReference ff_device = nullptr;
  57. FFEffectObjectReference ff_object = nullptr;
  58. uint64_t ff_timestamp = 0;
  59. LONG *ff_directions = nullptr;
  60. FFEFFECT ff_effect;
  61. DWORD *ff_axes = nullptr;
  62. void add_hid_elements(CFArrayRef p_array);
  63. void add_hid_element(IOHIDElementRef p_element);
  64. bool has_element(IOHIDElementCookie p_cookie, Vector<rec_element> *p_list) const;
  65. bool config_force_feedback(io_service_t p_service);
  66. bool check_ff_features();
  67. int get_hid_element_state(rec_element *p_element) const;
  68. void free();
  69. joypad();
  70. };
  71. class JoypadMacOS {
  72. enum {
  73. JOYPADS_MAX = 16,
  74. };
  75. private:
  76. Input *input = nullptr;
  77. IOHIDManagerRef hid_manager;
  78. Vector<joypad> device_list;
  79. bool have_device(IOHIDDeviceRef p_device) const;
  80. bool configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy);
  81. int get_joy_index(int p_id) const;
  82. int get_joy_ref(IOHIDDeviceRef p_device) const;
  83. void poll_joypads() const;
  84. void config_hid_manager(CFArrayRef p_matching_array) const;
  85. void joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp);
  86. void joypad_vibration_stop(int p_id, uint64_t p_timestamp);
  87. public:
  88. void process_joypads();
  89. void _device_added(IOReturn p_res, IOHIDDeviceRef p_device);
  90. void _device_removed(IOReturn p_res, IOHIDDeviceRef p_device);
  91. JoypadMacOS(Input *in);
  92. ~JoypadMacOS();
  93. };
  94. #endif // JOYPAD_MACOS_H