2
0

joypad_osx.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*************************************************************************/
  2. /* joypad_osx.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 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 != NULL) {
  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. int version = 0;
  276. refCF = IOHIDDeviceGetProperty(p_device_ref, CFSTR(kIOHIDVersionNumberKey));
  277. if (refCF) {
  278. CFNumberGetValue((CFNumberRef)refCF, kCFNumberSInt32Type, &version);
  279. }
  280. if (vendor && product_id) {
  281. char uid[128];
  282. sprintf(uid, "%08x%08x%08x%08x", OSSwapHostToBigInt32(3), OSSwapHostToBigInt32(vendor), OSSwapHostToBigInt32(product_id), OSSwapHostToBigInt32(version));
  283. input->joy_connection_changed(id, true, name, uid);
  284. } else {
  285. //bluetooth device
  286. String guid = "05000000";
  287. for (int i = 0; i < 12; i++) {
  288. if (i < name.size())
  289. guid += _hex_str(name[i]);
  290. else
  291. guid += "00";
  292. }
  293. input->joy_connection_changed(id, true, name, guid);
  294. }
  295. CFArrayRef array = IOHIDDeviceCopyMatchingElements(p_device_ref, NULL, kIOHIDOptionsTypeNone);
  296. if (array) {
  297. p_joy->add_hid_elements(array);
  298. CFRelease(array);
  299. }
  300. return true;
  301. }
  302. #define FF_ERR() \
  303. { \
  304. if (ret != FF_OK) { \
  305. FFReleaseDevice(ff_device); \
  306. return false; \
  307. } \
  308. }
  309. bool joypad::config_force_feedback(io_service_t p_service) {
  310. HRESULT ret = FFCreateDevice(p_service, &ff_device);
  311. ERR_FAIL_COND_V(ret != FF_OK, false);
  312. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_RESET);
  313. FF_ERR();
  314. ret = FFDeviceSendForceFeedbackCommand(ff_device, FFSFFC_SETACTUATORSON);
  315. FF_ERR();
  316. if (check_ff_features()) {
  317. ret = FFDeviceCreateEffect(ff_device, kFFEffectType_ConstantForce_ID, &ff_effect, &ff_object);
  318. FF_ERR();
  319. return true;
  320. }
  321. FFReleaseDevice(ff_device);
  322. return false;
  323. }
  324. #undef FF_ERR
  325. #define TEST_FF(ff) (features.supportedEffects & (ff))
  326. bool joypad::check_ff_features() {
  327. FFCAPABILITIES features;
  328. HRESULT ret = FFDeviceGetForceFeedbackCapabilities(ff_device, &features);
  329. if (ret == FF_OK && (features.supportedEffects & FFCAP_ET_CONSTANTFORCE)) {
  330. uint32_t val;
  331. ret = FFDeviceGetForceFeedbackProperty(ff_device, FFPROP_FFGAIN, &val, sizeof(val));
  332. if (ret != FF_OK) return false;
  333. int num_axes = features.numFfAxes;
  334. ff_axes = (DWORD *)memalloc(sizeof(DWORD) * num_axes);
  335. ff_directions = (LONG *)memalloc(sizeof(LONG) * num_axes);
  336. for (int i = 0; i < num_axes; i++) {
  337. ff_axes[i] = features.ffAxes[i];
  338. ff_directions[i] = 0;
  339. }
  340. ff_effect.cAxes = num_axes;
  341. ff_effect.rgdwAxes = ff_axes;
  342. ff_effect.rglDirection = ff_directions;
  343. return true;
  344. }
  345. return false;
  346. }
  347. static int process_hat_value(int p_min, int p_max, int p_value) {
  348. int range = (p_max - p_min + 1);
  349. int value = p_value - p_min;
  350. int hat_value = InputDefault::HAT_MASK_CENTER;
  351. if (range == 4) {
  352. value *= 2;
  353. }
  354. switch (value) {
  355. case 0:
  356. hat_value = InputDefault::HAT_MASK_UP;
  357. break;
  358. case 1:
  359. hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_RIGHT;
  360. break;
  361. case 2:
  362. hat_value = InputDefault::HAT_MASK_RIGHT;
  363. break;
  364. case 3:
  365. hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_RIGHT;
  366. break;
  367. case 4:
  368. hat_value = InputDefault::HAT_MASK_DOWN;
  369. break;
  370. case 5:
  371. hat_value = InputDefault::HAT_MASK_DOWN | InputDefault::HAT_MASK_LEFT;
  372. break;
  373. case 6:
  374. hat_value = InputDefault::HAT_MASK_LEFT;
  375. break;
  376. case 7:
  377. hat_value = InputDefault::HAT_MASK_UP | InputDefault::HAT_MASK_LEFT;
  378. break;
  379. default:
  380. hat_value = InputDefault::HAT_MASK_CENTER;
  381. break;
  382. }
  383. return hat_value;
  384. }
  385. void JoypadOSX::poll_joypads() const {
  386. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  387. /* no-op. Pending callbacks will fire. */
  388. }
  389. }
  390. static const InputDefault::JoyAxis axis_correct(int p_value, int p_min, int p_max) {
  391. InputDefault::JoyAxis jx;
  392. if (p_min < 0) {
  393. jx.min = -1;
  394. if (p_value < 0) {
  395. jx.value = (float)-p_value / p_min;
  396. } else
  397. jx.value = (float)p_value / p_max;
  398. }
  399. if (p_min == 0) {
  400. jx.min = 0;
  401. jx.value = 0.0f + (float)p_value / p_max;
  402. }
  403. return jx;
  404. }
  405. void JoypadOSX::process_joypads() {
  406. poll_joypads();
  407. for (int i = 0; i < device_list.size(); i++) {
  408. joypad &joy = device_list.write[i];
  409. for (int j = 0; j < joy.axis_elements.size(); j++) {
  410. rec_element &elem = joy.axis_elements.write[j];
  411. int value = joy.get_hid_element_state(&elem);
  412. input->joy_axis(joy.id, j, axis_correct(value, elem.min, elem.max));
  413. }
  414. for (int j = 0; j < joy.button_elements.size(); j++) {
  415. int value = joy.get_hid_element_state(&joy.button_elements.write[j]);
  416. input->joy_button(joy.id, j, (value >= 1));
  417. }
  418. for (int j = 0; j < joy.hat_elements.size(); j++) {
  419. rec_element &elem = joy.hat_elements.write[j];
  420. int value = joy.get_hid_element_state(&elem);
  421. int hat_value = process_hat_value(elem.min, elem.max, value);
  422. input->joy_hat(joy.id, hat_value);
  423. }
  424. if (joy.ffservice) {
  425. uint64_t timestamp = input->get_joy_vibration_timestamp(joy.id);
  426. if (timestamp > joy.ff_timestamp) {
  427. Vector2 strength = input->get_joy_vibration_strength(joy.id);
  428. float duration = input->get_joy_vibration_duration(joy.id);
  429. if (strength.x == 0 && strength.y == 0) {
  430. joypad_vibration_stop(joy.id, timestamp);
  431. } else {
  432. float gain = MAX(strength.x, strength.y);
  433. joypad_vibration_start(joy.id, gain, duration, timestamp);
  434. }
  435. }
  436. }
  437. }
  438. }
  439. void JoypadOSX::joypad_vibration_start(int p_id, float p_magnitude, float p_duration, uint64_t p_timestamp) {
  440. joypad *joy = &device_list.write[get_joy_index(p_id)];
  441. joy->ff_timestamp = p_timestamp;
  442. joy->ff_effect.dwDuration = p_duration * FF_SECONDS;
  443. joy->ff_effect.dwGain = p_magnitude * FF_FFNOMINALMAX;
  444. FFEffectSetParameters(joy->ff_object, &joy->ff_effect, FFEP_DURATION | FFEP_GAIN);
  445. FFEffectStart(joy->ff_object, 1, 0);
  446. }
  447. void JoypadOSX::joypad_vibration_stop(int p_id, uint64_t p_timestamp) {
  448. joypad *joy = &device_list.write[get_joy_index(p_id)];
  449. joy->ff_timestamp = p_timestamp;
  450. FFEffectStop(joy->ff_object);
  451. }
  452. int JoypadOSX::get_joy_index(int p_id) const {
  453. for (int i = 0; i < device_list.size(); i++) {
  454. if (device_list[i].id == p_id) return i;
  455. }
  456. return -1;
  457. }
  458. int JoypadOSX::get_joy_ref(IOHIDDeviceRef p_device) const {
  459. for (int i = 0; i < device_list.size(); i++) {
  460. if (device_list[i].device_ref == p_device) return i;
  461. }
  462. return -1;
  463. }
  464. bool JoypadOSX::have_device(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 true;
  468. }
  469. }
  470. return false;
  471. }
  472. static CFDictionaryRef create_match_dictionary(const UInt32 page, const UInt32 usage, int *okay) {
  473. CFDictionaryRef retval = NULL;
  474. CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
  475. CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
  476. const void *keys[2] = { (void *)CFSTR(kIOHIDDeviceUsagePageKey), (void *)CFSTR(kIOHIDDeviceUsageKey) };
  477. const void *vals[2] = { (void *)pageNumRef, (void *)usageNumRef };
  478. if (pageNumRef && usageNumRef) {
  479. retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  480. }
  481. if (pageNumRef) {
  482. CFRelease(pageNumRef);
  483. }
  484. if (usageNumRef) {
  485. CFRelease(usageNumRef);
  486. }
  487. if (!retval) {
  488. *okay = 0;
  489. }
  490. return retval;
  491. }
  492. void JoypadOSX::config_hid_manager(CFArrayRef p_matching_array) const {
  493. CFRunLoopRef runloop = CFRunLoopGetCurrent();
  494. IOReturn ret = IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
  495. ERR_FAIL_COND(ret != kIOReturnSuccess);
  496. IOHIDManagerSetDeviceMatchingMultiple(hid_manager, p_matching_array);
  497. IOHIDManagerRegisterDeviceMatchingCallback(hid_manager, joypad_added_callback, NULL);
  498. IOHIDManagerRegisterDeviceRemovalCallback(hid_manager, joypad_removed_callback, NULL);
  499. IOHIDManagerScheduleWithRunLoop(hid_manager, runloop, GODOT_JOY_LOOP_RUN_MODE);
  500. while (CFRunLoopRunInMode(GODOT_JOY_LOOP_RUN_MODE, 0, TRUE) == kCFRunLoopRunHandledSource) {
  501. /* no-op. Callback fires once per existing device. */
  502. }
  503. }
  504. JoypadOSX::JoypadOSX() {
  505. self = this;
  506. input = (InputDefault *)Input::get_singleton();
  507. int okay = 1;
  508. const void *vals[] = {
  509. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
  510. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
  511. (void *)create_match_dictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
  512. };
  513. const size_t n_elements = sizeof(vals) / sizeof(vals[0]);
  514. CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, n_elements, &kCFTypeArrayCallBacks) : NULL;
  515. for (size_t i = 0; i < n_elements; i++) {
  516. if (vals[i]) {
  517. CFRelease((CFTypeRef)vals[i]);
  518. }
  519. }
  520. if (array) {
  521. hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  522. if (hid_manager != NULL) {
  523. config_hid_manager(array);
  524. }
  525. CFRelease(array);
  526. }
  527. }
  528. JoypadOSX::~JoypadOSX() {
  529. for (int i = 0; i < device_list.size(); i++) {
  530. device_list.write[i].free();
  531. }
  532. IOHIDManagerUnscheduleFromRunLoop(hid_manager, CFRunLoopGetCurrent(), GODOT_JOY_LOOP_RUN_MODE);
  533. IOHIDManagerClose(hid_manager, kIOHIDOptionsTypeNone);
  534. CFRelease(hid_manager);
  535. hid_manager = NULL;
  536. }