joypad_osx.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*************************************************************************/
  2. /* joypad_osx.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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. #include "joypad_osx.h"
  31. #include <machine/endian.h>
  32. #define GODOT_JOY_LOOP_RUN_MODE CFSTR("GodotJoypad")
  33. static JoypadOSX *self = nullptr;
  34. joypad::joypad() {
  35. device_ref = nullptr;
  36. ff_device = nullptr;
  37. ff_axes = nullptr;
  38. ff_directions = nullptr;
  39. ffservice = 0;
  40. ff_timestamp = 0;
  41. id = 0;
  42. ff_constant_force.lMagnitude = 10000;
  43. ff_effect.dwDuration = 0;
  44. ff_effect.dwSamplePeriod = 0;
  45. ff_effect.dwGain = 10000;
  46. ff_effect.dwFlags = FFEFF_OBJECTOFFSETS;
  47. ff_effect.dwTriggerButton = FFEB_NOTRIGGER;
  48. ff_effect.dwStartDelay = 0;
  49. ff_effect.dwTriggerRepeatInterval = 0;
  50. ff_effect.lpEnvelope = nullptr;
  51. ff_effect.cbTypeSpecificParams = sizeof(FFCONSTANTFORCE);
  52. ff_effect.lpvTypeSpecificParams = &ff_constant_force;
  53. ff_effect.dwSize = sizeof(ff_effect);
  54. }
  55. void joypad::free() {
  56. if (device_ref) {
  57. IOHIDDeviceUnscheduleFromRunLoop(device_ref, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  58. }
  59. if (ff_device) {
  60. FFDeviceReleaseEffect(ff_device, ff_object);
  61. FFReleaseDevice(ff_device);
  62. memfree(ff_axes);
  63. memfree(ff_directions);
  64. }
  65. }
  66. bool joypad::has_element(IOHIDElementCookie p_cookie, Vector<rec_element> *p_list) const {
  67. for (int i = 0; i < p_list->size(); i++) {
  68. if (p_cookie == p_list->get(i).cookie) {
  69. return true;
  70. }
  71. }
  72. return false;
  73. }
  74. int joypad::get_hid_element_state(rec_element *p_element) const {
  75. int value = 0;
  76. if (p_element && p_element->ref) {
  77. IOHIDValueRef valueRef;
  78. if (IOHIDDeviceGetValue(device_ref, p_element->ref, &valueRef) == kIOReturnSuccess) {
  79. value = (SInt32)IOHIDValueGetIntegerValue(valueRef);
  80. /* record min and max for auto calibration */
  81. if (value < p_element->min) {
  82. p_element->min = value;
  83. }
  84. if (value > p_element->max) {
  85. p_element->max = value;
  86. }
  87. }
  88. }
  89. return value;
  90. }
  91. void joypad::add_hid_element(IOHIDElementRef p_element) {
  92. const CFTypeID elementTypeID = p_element ? CFGetTypeID(p_element) : 0;
  93. if (p_element && (elementTypeID == IOHIDElementGetTypeID())) {
  94. const IOHIDElementCookie cookie = IOHIDElementGetCookie(p_element);
  95. const uint32_t usagePage = IOHIDElementGetUsagePage(p_element);
  96. const uint32_t usage = IOHIDElementGetUsage(p_element);
  97. Vector<rec_element> *list = nullptr;
  98. switch (IOHIDElementGetType(p_element)) {
  99. case kIOHIDElementTypeInput_Misc:
  100. case kIOHIDElementTypeInput_Button:
  101. case kIOHIDElementTypeInput_Axis: {
  102. switch (usagePage) {
  103. case kHIDPage_GenericDesktop:
  104. switch (usage) {
  105. case kHIDUsage_GD_X:
  106. case kHIDUsage_GD_Y:
  107. case kHIDUsage_GD_Z:
  108. case kHIDUsage_GD_Rx:
  109. case kHIDUsage_GD_Ry:
  110. case kHIDUsage_GD_Rz:
  111. case kHIDUsage_GD_Slider:
  112. case kHIDUsage_GD_Dial:
  113. case kHIDUsage_GD_Wheel:
  114. if (!has_element(cookie, &axis_elements)) {
  115. list = &axis_elements;
  116. }
  117. break;
  118. case kHIDUsage_GD_Hatswitch:
  119. if (!has_element(cookie, &hat_elements)) {
  120. list = &hat_elements;
  121. }
  122. break;
  123. case kHIDUsage_GD_DPadUp:
  124. case kHIDUsage_GD_DPadDown:
  125. case kHIDUsage_GD_DPadRight:
  126. case kHIDUsage_GD_DPadLeft:
  127. case kHIDUsage_GD_Start:
  128. case kHIDUsage_GD_Select:
  129. if (!has_element(cookie, &button_elements)) {
  130. list = &button_elements;
  131. }
  132. break;
  133. }
  134. break;
  135. case kHIDPage_Simulation:
  136. switch (usage) {
  137. case kHIDUsage_Sim_Rudder:
  138. case kHIDUsage_Sim_Throttle:
  139. if (!has_element(cookie, &axis_elements)) {
  140. list = &axis_elements;
  141. }
  142. break;
  143. default:
  144. break;
  145. }
  146. break;
  147. case kHIDPage_Button:
  148. case kHIDPage_Consumer:
  149. if (!has_element(cookie, &button_elements)) {
  150. list = &button_elements;
  151. }
  152. break;
  153. default:
  154. break;
  155. }
  156. } break;
  157. case kIOHIDElementTypeCollection: {
  158. CFArrayRef array = IOHIDElementGetChildren(p_element);
  159. if (array) {
  160. add_hid_elements(array);
  161. }
  162. } break;
  163. default:
  164. break;
  165. }
  166. if (list) { /* add to list */
  167. rec_element element;
  168. element.ref = p_element;
  169. element.usage = usage;
  170. element.min = (SInt32)IOHIDElementGetLogicalMin(p_element);
  171. element.max = (SInt32)IOHIDElementGetLogicalMax(p_element);
  172. element.cookie = IOHIDElementGetCookie(p_element);
  173. list->push_back(element);
  174. list->sort_custom<rec_element::Comparator>();
  175. }
  176. }
  177. }
  178. static void hid_element_added(const void *p_value, void *p_parameter) {
  179. joypad *joy = (joypad *)p_parameter;
  180. joy->add_hid_element((IOHIDElementRef)p_value);
  181. }
  182. void joypad::add_hid_elements(CFArrayRef p_array) {
  183. CFRange range = { 0, CFArrayGetCount(p_array) };
  184. CFArrayApplyFunction(p_array, range, hid_element_added, this);
  185. }
  186. static void joypad_removed_callback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) {
  187. self->_device_removed(res, ioHIDDeviceObject);
  188. }
  189. static void joypad_added_callback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) {
  190. self->_device_added(res, ioHIDDeviceObject);
  191. }
  192. static bool is_joypad(IOHIDDeviceRef p_device_ref) {
  193. int usage_page = 0;
  194. int usage = 0;
  195. CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsagePageKey));
  196. if (refCF) {
  197. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage_page);
  198. }
  199. if (usage_page != kHIDPage_GenericDesktop) {
  200. return false;
  201. }
  202. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsageKey));
  203. if (refCF) {
  204. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage);
  205. }
  206. if ((usage != kHIDUsage_GD_Joystick &&
  207. usage != kHIDUsage_GD_GamePad &&
  208. usage != kHIDUsage_GD_MultiAxisController)) {
  209. return false;
  210. }
  211. return true;
  212. }
  213. void JoypadOSX::_device_added(IOReturn p_res, IOHIDDeviceRef p_device) {
  214. if (p_res != kIOReturnSuccess || have_device(p_device)) {
  215. return;
  216. }
  217. joypad new_joypad;
  218. if (is_joypad(p_device)) {
  219. configure_joypad(p_device, &new_joypad);
  220. #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
  221. if (IOHIDDeviceGetService != nullptr) {
  222. #endif
  223. const io_service_t ioservice = IOHIDDeviceGetService(p_device);
  224. if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK) && new_joypad.config_force_feedback(ioservice)) {
  225. new_joypad.ffservice = ioservice;
  226. }
  227. #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
  228. }
  229. #endif
  230. device_list.push_back(new_joypad);
  231. }
  232. IOHIDDeviceScheduleWithRunLoop(p_device, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  233. }
  234. void JoypadOSX::_device_removed(IOReturn p_res, IOHIDDeviceRef p_device) {
  235. int device = get_joy_ref(p_device);
  236. ERR_FAIL_COND(device == -1);
  237. input->joy_connection_changed(device_list[device].id, false, "");
  238. device_list.write[device].free();
  239. device_list.remove(device);
  240. }
  241. static String _hex_str(uint8_t p_byte) {
  242. static const char *dict = "0123456789abcdef";
  243. char ret[3];
  244. ret[2] = 0;
  245. ret[0] = dict[p_byte >> 4];
  246. ret[1] = dict[p_byte & 0xF];
  247. return ret;
  248. }
  249. bool JoypadOSX::configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy) {
  250. p_joy->device_ref = p_device_ref;
  251. /* get device name */
  252. String name;
  253. char c_name[256];
  254. CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductKey));
  255. if (!refCF) {
  256. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDManufacturerKey));
  257. }
  258. if ((!refCF) || (!CFStringGetCString((CFStringRef)refCF, c_name, sizeof(c_name), kCFStringEncodingUTF8))) {
  259. name = "Unidentified Joypad";
  260. }
  261. name = c_name;
  262. int id = input->get_unused_joy_id();
  263. ERR_FAIL_COND_V(id == -1, false);
  264. p_joy->id = id;
  265. int vendor = 0;
  266. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDVendorIDKey));
  267. if (refCF) {
  268. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &vendor);
  269. }
  270. int product_id = 0;
  271. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductIDKey));
  272. if (refCF) {
  273. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &product_id);
  274. }
  275. if (vendor && product_id) {
  276. char uid[128];
  277. sprintf(uid, "%04x%08x%04x%08x", OSSwapHostToBigInt32(vendor), 0, OSSwapHostToBigInt32(product_id), 0);
  278. input->joy_connection_changed(id, true, name, uid);
  279. } else {
  280. //bluetooth device
  281. String guid = "05000000";
  282. for (int i = 0; i < 12; i++) {
  283. if (i < name.size())
  284. guid += _hex_str(name[i]);
  285. else
  286. guid += "00";
  287. }
  288. input->joy_connection_changed(id, true, name, guid);
  289. }
  290. CFArrayRef array = IOHIDDeviceCopyMatchingElements(p_device_ref, nullptr, kIOHIDOptionsTypeNone);
  291. if (array) {
  292. p_joy->add_hid_elements(array);
  293. CFRelease(array);
  294. }
  295. return true;
  296. }
  297. #define FF_ERR() \
  298. { \
  299. if (ret != FF_OK) { \
  300. FFReleaseDevice(ff_device); \
  301. return false; \
  302. } \
  303. }
  304. bool joypad::config_force_feedback(io_service_t p_service) {
  305. HRESULT ret = FFCreateDevice(p_service, &ff_device);
  306. ERR_FAIL_COND_V(ret != FF_OK, false);
  307. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_RESET);
  308. FF_ERR();
  309. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_SETACTUATORSON);
  310. FF_ERR();
  311. if (check_ff_features()) {
  312. ret = FFDeviceCreateEffect(ff_device, kFFEffectType_ConstantForce_ID, &ff_effect, &ff_object);
  313. FF_ERR();
  314. return true;
  315. }
  316. FFReleaseDevice(ff_device);
  317. return false;
  318. }
  319. #undef FF_ERR
  320. #define TEST_FF(ff) (features.supportedEffects & (ff))
  321. bool joypad::check_ff_features() {
  322. FFCAPABILITIES features;
  323. HRESULT ret = FFDeviceGetForceFeedbackCapabilities(ff_device, &features);
  324. if (ret == FF_OK && (features.supportedEffects & FFCAP_ET_CONSTANTFORCE)) {
  325. uint32_t val;
  326. ret = FFDeviceGetForceFeedbackProperty(ff_device, FFPROP_FFGAIN, &val, sizeof(val));
  327. if (ret != FF_OK)
  328. return false;
  329. int num_axes = features.numFfAxes;
  330. ff_axes = (DWORD *)memalloc(sizeof(DWORD) * num_axes);
  331. ff_directions = (LONG *)memalloc(sizeof(LONG) * num_axes);
  332. for (int i = 0; i < num_axes; i++) {
  333. ff_axes[i] = features.ffAxes[i];
  334. ff_directions[i] = 0;
  335. }
  336. ff_effect.cAxes = num_axes;
  337. ff_effect.rgdwAxes = ff_axes;
  338. ff_effect.rglDirection = ff_directions;
  339. return true;
  340. }
  341. return false;
  342. }
  343. static int process_hat_value(int p_min, int p_max, int p_value) {
  344. int range = (p_max - p_min + 1);
  345. int value = p_value - p_min;
  346. int hat_value = Input::HAT_MASK_CENTER;
  347. if (range == 4) {
  348. value *= 2;
  349. }
  350. switch (value) {
  351. case 0:
  352. hat_value = Input::HAT_MASK_UP;
  353. break;
  354. case 1:
  355. hat_value = Input::HAT_MASK_UP | Input::HAT_MASK_RIGHT;
  356. break;
  357. case 2:
  358. hat_value = Input::HAT_MASK_RIGHT;
  359. break;
  360. case 3:
  361. hat_value = Input::HAT_MASK_DOWN | Input::HAT_MASK_RIGHT;
  362. break;
  363. case 4:
  364. hat_value = Input::HAT_MASK_DOWN;
  365. break;
  366. case 5:
  367. hat_value = Input::HAT_MASK_DOWN | Input::HAT_MASK_LEFT;
  368. break;
  369. case 6:
  370. hat_value = Input::HAT_MASK_LEFT;
  371. break;
  372. case 7:
  373. hat_value = Input::HAT_MASK_UP | Input::HAT_MASK_LEFT;
  374. break;
  375. default:
  376. hat_value = Input::HAT_MASK_CENTER;
  377. break;
  378. }
  379. return hat_value;
  380. }
  381. void JoypadOSX::poll_joypads() const {
  382. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  383. /* no-op. Pending callbacks will fire. */
  384. }
  385. }
  386. static const Input::JoyAxis axis_correct(int p_value, int p_min, int p_max) {
  387. Input::JoyAxis jx;
  388. if (p_min < 0) {
  389. jx.min = -1;
  390. if (p_value < 0) {
  391. jx.value = (float)-p_value / p_min;
  392. } else
  393. jx.value = (float)p_value / p_max;
  394. }
  395. if (p_min == 0) {
  396. jx.min = 0;
  397. jx.value = 0.0f + (float)p_value / p_max;
  398. }
  399. return jx;
  400. }
  401. void JoypadOSX::process_joypads() {
  402. poll_joypads();
  403. for (int i = 0; i < device_list.size(); i++) {
  404. joypad &joy = device_list.write[i];
  405. for (int j = 0; j < joy.axis_elements.size(); j++) {
  406. rec_element &elem = joy.axis_elements.write[j];
  407. int value = joy.get_hid_element_state(&elem);
  408. input->joy_axis(joy.id, j, axis_correct(value, elem.min, elem.max));
  409. }
  410. for (int j = 0; j < joy.button_elements.size(); j++) {
  411. int value = joy.get_hid_element_state(&joy.button_elements.write[j]);
  412. input->joy_button(joy.id, j, (value >= 1));
  413. }
  414. for (int j = 0; j < joy.hat_elements.size(); j++) {
  415. rec_element &elem = joy.hat_elements.write[j];
  416. int value = joy.get_hid_element_state(&elem);
  417. int hat_value = process_hat_value(elem.min, elem.max, value);
  418. input->joy_hat(joy.id, hat_value);
  419. }
  420. if (joy.ffservice) {
  421. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  422. if (timestamp > joy.ff_timestamp) {
  423. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  424. float duration = input->get_joy_vibration_duration(joy.id);
  425. if (strength.x == 0 && strength.y == 0) {
  426. joypad_vibration_stop(joy.id, timestamp);
  427. } else {
  428. float gain = MAX(strength.x, strength.y);
  429. joypad_vibration_start(joy.id, gain, duration, timestamp);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. void JoypadOSX::joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp) {
  436. joypad *joy = &device_list.write[get_joy_index(p_id)];
  437. joy->ff_timestamp = p_timestamp;
  438. joy->ff_effect.dwDuration = p_duration * FF_SECONDS;
  439. joy->ff_effect.dwGain = p_magnitude * FF_FFNOMINALMAX;
  440. FFEffectSetParameters(joy->ff_object, &joy->ff_effect, FFEP_DURATION | FFEP_GAIN);
  441. FFEffectStart(joy->ff_object, 1, 0);
  442. }
  443. void JoypadOSX::joypad_vibration_stop(int p_id, uint64_t p_timestamp) {
  444. joypad *joy = &device_list.write[get_joy_index(p_id)];
  445. joy->ff_timestamp = p_timestamp;
  446. FFEffectStop(joy->ff_object);
  447. }
  448. int JoypadOSX::get_joy_index(int p_id) const {
  449. for (int i = 0; i < device_list.size(); i++) {
  450. if (device_list[i].id == p_id)
  451. return i;
  452. }
  453. return -1;
  454. }
  455. int JoypadOSX::get_joy_ref(IOHIDDeviceRef p_device) const {
  456. for (int i = 0; i < device_list.size(); i++) {
  457. if (device_list[i].device_ref == p_device)
  458. return i;
  459. }
  460. return -1;
  461. }
  462. bool JoypadOSX::have_device(IOHIDDeviceRef p_device) const {
  463. for (int i = 0; i < device_list.size(); i++) {
  464. if (device_list[i].device_ref == p_device) {
  465. return true;
  466. }
  467. }
  468. return false;
  469. }
  470. static CFDictionaryRef create_match_dictionary(const UInt32 page, const UInt32 usage, int *okay) {
  471. CFDictionaryRef retval = nullptr;
  472. CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
  473. CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
  474. const void *keys[2] = { (void *)CFSTR(kIOHIDDeviceUsagePageKey), (void *)CFSTR(kIOHIDDeviceUsageKey) };
  475. const void *vals[2] = { (void *)pageNumRef, (void *)usageNumRef };
  476. if (pageNumRef && usageNumRef) {
  477. retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  478. }
  479. if (pageNumRef) {
  480. CFRelease(pageNumRef);
  481. }
  482. if (usageNumRef) {
  483. CFRelease(usageNumRef);
  484. }
  485. if (!retval) {
  486. *okay = 0;
  487. }
  488. return retval;
  489. }
  490. void JoypadOSX::config_hid_manager(CFArrayRef p_matching_array) const {
  491. CFRunLoopRef runloop = CFRunLoopGetCurrent();
  492. IOReturn ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
  493. ERR_FAIL_COND(ret != kIOReturnSuccess);
  494. IOHIDManagerSetDeviceMatchingMultiple(hid_manager, p_matching_array);
  495. IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, joypad_added_callback, nullptr);
  496. IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, joypad_removed_callback, nullptr);
  497. IOHIDManagerScheduleWithRunLoop(hid_manager, runloop, GODOT_JOY_LOOP_RUN_MODE);
  498. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  499. /* no-op. Callback fires once per existing device. */
  500. }
  501. }
  502. JoypadOSX::JoypadOSX(Input *in) {
  503. self = this;
  504. input = in;
  505. int okay = 1;
  506. const void *vals[] = {
  507. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
  508. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
  509. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
  510. };
  511. const size_t n_elements = sizeof(vals) / sizeof(vals[0]);
  512. CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, n_elements, &kCFTypeArrayCallBacks) : nullptr;
  513. for (size_t i = 0; i < n_elements; i++) {
  514. if (vals[i]) {
  515. CFRelease((CFTypeRef)vals[i]);
  516. }
  517. }
  518. if (array) {
  519. hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  520. if (hid_manager != nullptr) {
  521. config_hid_manager(array);
  522. }
  523. CFRelease(array);
  524. }
  525. }
  526. JoypadOSX::~JoypadOSX() {
  527. for (int i = 0; i < device_list.size(); i++) {
  528. device_list.write[i].free();
  529. }
  530. IOHIDManagerUnscheduleFromRunLoop(hid_manager, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  531. IOHIDManagerClose(hid_manager, kIOHIDOptionsTypeNone);
  532. CFRelease(hid_manager);
  533. hid_manager = nullptr;
  534. }