joypad_osx.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /*************************************************************************/
  2. /* joypad_osx.cpp */
  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. #include "joypad_osx.h"
  31. #include <machine/endian.h>
  32. #define GODOT_JOY_LOOP_RUN_MODE CFSTR("GodotJoypad")
  33. static JoypadOSX *self = NULL;
  34. joypad::joypad() {
  35. device_ref = NULL;
  36. ff_device = NULL;
  37. ff_axes = NULL;
  38. ff_directions = NULL;
  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 = NULL;
  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 = NULL;
  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 result, void *sender) {
  187. int id = (intptr_t)ctx;
  188. self->_device_removed(id);
  189. }
  190. static void joypad_added_callback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject) {
  191. self->_device_added(res, ioHIDDeviceObject);
  192. }
  193. static bool is_joypad(IOHIDDeviceRef p_device_ref) {
  194. int usage_page = 0;
  195. int usage = 0;
  196. CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsagePageKey));
  197. if (refCF) {
  198. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage_page);
  199. }
  200. if (usage_page != kHIDPage_GenericDesktop) {
  201. return false;
  202. }
  203. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDPrimaryUsageKey));
  204. if (refCF) {
  205. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &usage);
  206. }
  207. if ((usage != kHIDUsage_GD_Joystick &&
  208. usage != kHIDUsage_GD_GamePad &&
  209. usage != kHIDUsage_GD_MultiAxisController)) {
  210. return false;
  211. }
  212. return true;
  213. }
  214. void JoypadOSX::_device_added(IOReturn p_res, IOHIDDeviceRef p_device) {
  215. if (p_res != kIOReturnSuccess || have_device(p_device)) {
  216. return;
  217. }
  218. joypad new_joypad;
  219. if (is_joypad(p_device)) {
  220. configure_joypad(p_device, &new_joypad);
  221. #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
  222. if (IOHIDDeviceGetService != NULL) {
  223. #endif
  224. const io_service_t ioservice = IOHIDDeviceGetService(p_device);
  225. if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK) && new_joypad.config_force_feedback(ioservice)) {
  226. new_joypad.ffservice = ioservice;
  227. }
  228. #if MAC_OS_X_VERSION_MIN_REQUIRED < 1060
  229. }
  230. #endif
  231. device_list.push_back(new_joypad);
  232. }
  233. IOHIDDeviceRegisterRemovalCallback(p_device, joypad_removed_callback, (void *)(intptr_t)new_joypad.id);
  234. IOHIDDeviceScheduleWithRunLoop(p_device, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  235. }
  236. void JoypadOSX::_device_removed(int p_id) {
  237. int device = get_joy_index(p_id);
  238. ERR_FAIL_COND(device == -1);
  239. input->joy_connection_changed(p_id, false, "");
  240. device_list.write[device].free();
  241. device_list.remove(device);
  242. }
  243. static String _hex_str(uint8_t p_byte) {
  244. static const char *dict = "0123456789abcdef";
  245. char ret[3];
  246. ret[2] = 0;
  247. ret[0] = dict[p_byte >> 4];
  248. ret[1] = dict[p_byte & 0xF];
  249. return ret;
  250. }
  251. bool JoypadOSX::configure_joypad(IOHIDDeviceRef p_device_ref, joypad *p_joy) {
  252. p_joy->device_ref = p_device_ref;
  253. /* get device name */
  254. String name;
  255. char c_name[256];
  256. CFTypeRef refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductKey));
  257. if (!refCF) {
  258. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDManufacturerKey));
  259. }
  260. if ((!refCF) || (!CFStringGetCString((CFStringRef)refCF, c_name, sizeof(c_name), kCFStringEncodingUTF8))) {
  261. name = "Unidentified Joypad";
  262. }
  263. name = c_name;
  264. int id = input->get_unused_joy_id();
  265. ERR_FAIL_COND_V(id == -1, false);
  266. p_joy->id = id;
  267. int vendor = 0;
  268. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDVendorIDKey));
  269. if (refCF) {
  270. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &vendor);
  271. }
  272. int product_id = 0;
  273. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDProductIDKey));
  274. if (refCF) {
  275. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &product_id);
  276. }
  277. if (vendor && product_id) {
  278. char uid[128];
  279. sprintf(uid, "%04x%08x%04x%08x", OSSwapHostToBigInt32(vendor), 0, OSSwapHostToBigInt32(product_id), 0);
  280. input->joy_connection_changed(id, true, name, uid);
  281. } else {
  282. //bluetooth device
  283. String guid = "05000000";
  284. for (int i = 0; i < 12; i++) {
  285. if (i < name.size())
  286. guid += _hex_str(name[i]);
  287. else
  288. guid += "00";
  289. }
  290. input->joy_connection_changed(id, true, name, guid);
  291. }
  292. CFArrayRef array = IOHIDDeviceCopyMatchingElements(p_device_ref, NULL, kIOHIDOptionsTypeNone);
  293. if (array) {
  294. p_joy->add_hid_elements(array);
  295. CFRelease(array);
  296. }
  297. return true;
  298. }
  299. #define FF_ERR() \
  300. { \
  301. if (ret != FF_OK) { \
  302. FFReleaseDevice(ff_device); \
  303. return false; \
  304. } \
  305. }
  306. bool joypad::config_force_feedback(io_service_t p_service) {
  307. HRESULT ret = FFCreateDevice(p_service, &ff_device);
  308. ERR_FAIL_COND_V(ret != FF_OK, false);
  309. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_RESET);
  310. FF_ERR();
  311. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_SETACTUATORSON);
  312. FF_ERR();
  313. if (check_ff_features()) {
  314. ret = FFDeviceCreateEffect(ff_device, kFFEffectType_ConstantForce_ID, &ff_effect, &ff_object);
  315. FF_ERR();
  316. return true;
  317. }
  318. FFReleaseDevice(ff_device);
  319. return false;
  320. }
  321. #undef FF_ERR
  322. #define TEST_FF(ff) (features.supportedEffects & (ff))
  323. bool joypad::check_ff_features() {
  324. FFCAPABILITIES features;
  325. HRESULT ret = FFDeviceGetForceFeedbackCapabilities(ff_device, &features);
  326. if (ret == FF_OK && (features.supportedEffects & FFCAP_ET_CONSTANTFORCE)) {
  327. uint32_t val;
  328. ret = FFDeviceGetForceFeedbackProperty(ff_device, FFPROP_FFGAIN, &val, sizeof(val));
  329. if (ret != FF_OK) return false;
  330. int num_axes = features.numFfAxes;
  331. ff_axes = (DWORD *)memalloc(sizeof(DWORD) * num_axes);
  332. ff_directions = (LONG *)memalloc(sizeof(LONG) * num_axes);
  333. for (int i = 0; i < num_axes; i++) {
  334. ff_axes[i] = features.ffAxes[i];
  335. ff_directions[i] = 0;
  336. }
  337. ff_effect.cAxes = num_axes;
  338. ff_effect.rgdwAxes = ff_axes;
  339. ff_effect.rglDirection = ff_directions;
  340. return true;
  341. }
  342. return false;
  343. }
  344. static int process_hat_value(int p_min, int p_max, int p_value) {
  345. int range = (p_max - p_min + 1);
  346. int value = p_value - p_min;
  347. int hat_value = InputDefault::HAT_MASK_CENTER;
  348. if (range == 4) {
  349. value *= 2;
  350. }
  351. switch (value) {
  352. case 0:
  353. hat_value = InputDefault::HAT_MASK_UP;
  354. break;
  355. case 1:
  356. hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT;
  357. break;
  358. case 2:
  359. hat_value = InputDefault::HAT_MASK_RIGHT;
  360. break;
  361. case 3:
  362. hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_RIGHT;
  363. break;
  364. case 4:
  365. hat_value = InputDefault::HAT_MASK_DOWN;
  366. break;
  367. case 5:
  368. hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT;
  369. break;
  370. case 6:
  371. hat_value = InputDefault::HAT_MASK_LEFT;
  372. break;
  373. case 7:
  374. hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_LEFT;
  375. break;
  376. default:
  377. hat_value = InputDefault::HAT_MASK_CENTER;
  378. break;
  379. }
  380. return hat_value;
  381. }
  382. void JoypadOSX::poll_joypads() const {
  383. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  384. /* no-op. Pending callbacks will fire. */
  385. }
  386. }
  387. static const InputDefault::JoyAxis axis_correct(int p_value, int p_min, int p_max) {
  388. InputDefault::JoyAxis jx;
  389. if (p_min < 0) {
  390. jx.min = -1;
  391. if (p_value < 0) {
  392. jx.value = (float)-p_value / p_min;
  393. } else
  394. jx.value = (float)p_value / p_max;
  395. }
  396. if (p_min == 0) {
  397. jx.min = 0;
  398. jx.value = 0.0f + (float)p_value / p_max;
  399. }
  400. return jx;
  401. }
  402. void JoypadOSX::process_joypads() {
  403. poll_joypads();
  404. for (int i = 0; i < device_list.size(); i++) {
  405. joypad &joy = device_list.write[i];
  406. for (int j = 0; j < joy.axis_elements.size(); j++) {
  407. rec_element &elem = joy.axis_elements.write[j];
  408. int value = joy.get_hid_element_state(&elem);
  409. input->joy_axis(joy.id, j, axis_correct(value, elem.min, elem.max));
  410. }
  411. for (int j = 0; j < joy.button_elements.size(); j++) {
  412. int value = joy.get_hid_element_state(&joy.button_elements.write[j]);
  413. input->joy_button(joy.id, j, (value >= 1));
  414. }
  415. for (int j = 0; j < joy.hat_elements.size(); j++) {
  416. rec_element &elem = joy.hat_elements.write[j];
  417. int value = joy.get_hid_element_state(&elem);
  418. int hat_value = process_hat_value(elem.min, elem.max, value);
  419. input->joy_hat(joy.id, hat_value);
  420. }
  421. if (joy.ffservice) {
  422. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  423. if (timestamp > joy.ff_timestamp) {
  424. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  425. float duration = input->get_joy_vibration_duration(joy.id);
  426. if (strength.x == 0 && strength.y == 0) {
  427. joypad_vibration_stop(joy.id, timestamp);
  428. } else {
  429. float gain = MAX(strength.x, strength.y);
  430. joypad_vibration_start(joy.id, gain, duration, timestamp);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. void JoypadOSX::joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp) {
  437. joypad *joy = &device_list.write[get_joy_index(p_id)];
  438. joy->ff_timestamp = p_timestamp;
  439. joy->ff_effect.dwDuration = p_duration * FF_SECONDS;
  440. joy->ff_effect.dwGain = p_magnitude * FF_FFNOMINALMAX;
  441. FFEffectSetParameters(joy->ff_object, &joy->ff_effect, FFEP_DURATION | FFEP_GAIN);
  442. FFEffectStart(joy->ff_object, 1, 0);
  443. }
  444. void JoypadOSX::joypad_vibration_stop(int p_id, uint64_t p_timestamp) {
  445. joypad *joy = &device_list.write[get_joy_index(p_id)];
  446. joy->ff_timestamp = p_timestamp;
  447. FFEffectStop(joy->ff_object);
  448. }
  449. int JoypadOSX::get_joy_index(int p_id) const {
  450. for (int i = 0; i < device_list.size(); i++) {
  451. if (device_list[i].id == p_id) return i;
  452. }
  453. return -1;
  454. }
  455. bool JoypadOSX::have_device(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 true;
  459. }
  460. }
  461. return false;
  462. }
  463. static CFDictionaryRef create_match_dictionary(const UInt32 page, const UInt32 usage, int *okay) {
  464. CFDictionaryRef retval = NULL;
  465. CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
  466. CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
  467. const void *keys[2] = { (void *)CFSTR(kIOHIDDeviceUsagePageKey), (void *)CFSTR(kIOHIDDeviceUsageKey) };
  468. const void *vals[2] = { (void *)pageNumRef, (void *)usageNumRef };
  469. if (pageNumRef && usageNumRef) {
  470. retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  471. }
  472. if (pageNumRef) {
  473. CFRelease(pageNumRef);
  474. }
  475. if (usageNumRef) {
  476. CFRelease(usageNumRef);
  477. }
  478. if (!retval) {
  479. *okay = 0;
  480. }
  481. return retval;
  482. }
  483. void JoypadOSX::config_hid_manager(CFArrayRef p_matching_array) const {
  484. CFRunLoopRef runloop = CFRunLoopGetCurrent();
  485. IOReturn ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
  486. ERR_FAIL_COND(ret != kIOReturnSuccess);
  487. IOHIDManagerSetDeviceMatchingMultiple(hid_manager, p_matching_array);
  488. IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, joypad_added_callback, NULL);
  489. IOHIDManagerScheduleWithRunLoop(hid_manager, runloop, GODOT_JOY_LOOP_RUN_MODE);
  490. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  491. /* no-op. Callback fires once per existing device. */
  492. }
  493. }
  494. JoypadOSX::JoypadOSX() {
  495. self = this;
  496. input = (InputDefault *)Input::get_singleton();
  497. int okay = 1;
  498. const void *vals[] = {
  499. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
  500. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
  501. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
  502. };
  503. const size_t n_elements = sizeof(vals) / sizeof(vals[0]);
  504. CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, n_elements, &kCFTypeArrayCallBacks) : NULL;
  505. for (int i = 0; i < n_elements; i++) {
  506. if (vals[i]) {
  507. CFRelease((CFTypeRef)vals[i]);
  508. }
  509. }
  510. if (array) {
  511. hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  512. if (hid_manager != NULL) {
  513. config_hid_manager(array);
  514. }
  515. CFRelease(array);
  516. }
  517. }
  518. JoypadOSX::~JoypadOSX() {
  519. for (int i = 0; i < device_list.size(); i++) {
  520. device_list.write[i].free();
  521. }
  522. IOHIDManagerUnscheduleFromRunLoop(hid_manager, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  523. IOHIDManagerClose(hid_manager, kIOHIDOptionsTypeNone);
  524. CFRelease(hid_manager);
  525. hid_manager = NULL;
  526. }