joypad_osx.cpp 19 KB

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