SDL_sysjoystick.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #ifdef SDL_JOYSTICK_IOKIT
  20. #include <IOKit/hid/IOHIDLib.h>
  21. /* For force feedback testing. */
  22. #include <ForceFeedback/ForceFeedback.h>
  23. #include <ForceFeedback/ForceFeedbackConstants.h>
  24. #include "SDL_joystick.h"
  25. #include "../SDL_sysjoystick.h"
  26. #include "../SDL_joystick_c.h"
  27. #include "SDL_sysjoystick_c.h"
  28. #include "SDL_events.h"
  29. #include "../../haptic/darwin/SDL_syshaptic_c.h" /* For haptic hot plugging */
  30. #if !SDL_EVENTS_DISABLED
  31. #include "../../events/SDL_events_c.h"
  32. #endif
  33. /* The base object of the HID Manager API */
  34. static IOHIDManagerRef hidman = NULL;
  35. /* Linked list of all available devices */
  36. static recDevice *gpDeviceList = NULL;
  37. /* if SDL_TRUE then a device was added since the last update call */
  38. static SDL_bool s_bDeviceAdded = SDL_FALSE;
  39. static SDL_bool s_bDeviceRemoved = SDL_FALSE;
  40. /* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
  41. static int s_joystick_instance_id = -1;
  42. static void
  43. FreeElementList(recElement *pElement)
  44. {
  45. while (pElement) {
  46. recElement *pElementNext = pElement->pNext;
  47. SDL_free(pElement);
  48. pElement = pElementNext;
  49. }
  50. }
  51. static recDevice *
  52. FreeDevice(recDevice *removeDevice)
  53. {
  54. recDevice *pDeviceNext = NULL;
  55. if (removeDevice) {
  56. /* save next device prior to disposing of this device */
  57. pDeviceNext = removeDevice->pNext;
  58. if ( gpDeviceList == removeDevice ) {
  59. gpDeviceList = pDeviceNext;
  60. } else {
  61. recDevice *device = gpDeviceList;
  62. while (device->pNext != removeDevice) {
  63. device = device->pNext;
  64. }
  65. device->pNext = pDeviceNext;
  66. }
  67. removeDevice->pNext = NULL;
  68. /* free element lists */
  69. FreeElementList(removeDevice->firstAxis);
  70. FreeElementList(removeDevice->firstButton);
  71. FreeElementList(removeDevice->firstHat);
  72. SDL_free(removeDevice);
  73. }
  74. return pDeviceNext;
  75. }
  76. static SInt32
  77. GetHIDElementState(recDevice *pDevice, recElement *pElement)
  78. {
  79. SInt32 value = 0;
  80. if (pDevice && pElement) {
  81. IOHIDValueRef valueRef;
  82. if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) {
  83. value = (SInt32) IOHIDValueGetIntegerValue(valueRef);
  84. /* record min and max for auto calibration */
  85. if (value < pElement->minReport) {
  86. pElement->minReport = value;
  87. }
  88. if (value > pElement->maxReport) {
  89. pElement->maxReport = value;
  90. }
  91. }
  92. }
  93. return value;
  94. }
  95. static SInt32
  96. GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max)
  97. {
  98. const float deviceScale = max - min;
  99. const float readScale = pElement->maxReport - pElement->minReport;
  100. const SInt32 value = GetHIDElementState(pDevice, pElement);
  101. if (readScale == 0) {
  102. return value; /* no scaling at all */
  103. }
  104. return ((value - pElement->minReport) * deviceScale / readScale) + min;
  105. }
  106. static void
  107. JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
  108. {
  109. recDevice *device = (recDevice *) ctx;
  110. device->removed = 1;
  111. #if SDL_HAPTIC_IOKIT
  112. MacHaptic_MaybeRemoveDevice(device->ffservice);
  113. #endif
  114. s_bDeviceRemoved = SDL_TRUE;
  115. }
  116. static void AddHIDElement(const void *value, void *parameter);
  117. /* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */
  118. static void
  119. AddHIDElements(CFArrayRef array, recDevice *pDevice)
  120. {
  121. const CFRange range = { 0, CFArrayGetCount(array) };
  122. CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
  123. }
  124. static SDL_bool
  125. ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) {
  126. while (listitem) {
  127. if (listitem->cookie == cookie) {
  128. return SDL_TRUE;
  129. }
  130. listitem = listitem->pNext;
  131. }
  132. return SDL_FALSE;
  133. }
  134. /* See if we care about this HID element, and if so, note it in our recDevice. */
  135. static void
  136. AddHIDElement(const void *value, void *parameter)
  137. {
  138. recDevice *pDevice = (recDevice *) parameter;
  139. IOHIDElementRef refElement = (IOHIDElementRef) value;
  140. const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
  141. if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
  142. const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
  143. const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
  144. const uint32_t usage = IOHIDElementGetUsage(refElement);
  145. recElement *element = NULL;
  146. recElement **headElement = NULL;
  147. /* look at types of interest */
  148. switch (IOHIDElementGetType(refElement)) {
  149. case kIOHIDElementTypeInput_Misc:
  150. case kIOHIDElementTypeInput_Button:
  151. case kIOHIDElementTypeInput_Axis: {
  152. switch (usagePage) { /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
  153. case kHIDPage_GenericDesktop:
  154. switch (usage) {
  155. case kHIDUsage_GD_X:
  156. case kHIDUsage_GD_Y:
  157. case kHIDUsage_GD_Z:
  158. case kHIDUsage_GD_Rx:
  159. case kHIDUsage_GD_Ry:
  160. case kHIDUsage_GD_Rz:
  161. case kHIDUsage_GD_Slider:
  162. case kHIDUsage_GD_Dial:
  163. case kHIDUsage_GD_Wheel:
  164. if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
  165. element = (recElement *) SDL_calloc(1, sizeof (recElement));
  166. if (element) {
  167. pDevice->axes++;
  168. headElement = &(pDevice->firstAxis);
  169. }
  170. }
  171. break;
  172. case kHIDUsage_GD_Hatswitch:
  173. if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) {
  174. element = (recElement *) SDL_calloc(1, sizeof (recElement));
  175. if (element) {
  176. pDevice->hats++;
  177. headElement = &(pDevice->firstHat);
  178. }
  179. }
  180. break;
  181. }
  182. break;
  183. case kHIDPage_Simulation:
  184. switch (usage) {
  185. case kHIDUsage_Sim_Rudder:
  186. case kHIDUsage_Sim_Throttle:
  187. if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
  188. element = (recElement *) SDL_calloc(1, sizeof (recElement));
  189. if (element) {
  190. pDevice->axes++;
  191. headElement = &(pDevice->firstAxis);
  192. }
  193. }
  194. break;
  195. default:
  196. break;
  197. }
  198. break;
  199. case kHIDPage_Button:
  200. if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
  201. element = (recElement *) SDL_calloc(1, sizeof (recElement));
  202. if (element) {
  203. pDevice->buttons++;
  204. headElement = &(pDevice->firstButton);
  205. }
  206. }
  207. break;
  208. default:
  209. break;
  210. }
  211. }
  212. break;
  213. case kIOHIDElementTypeCollection: {
  214. CFArrayRef array = IOHIDElementGetChildren(refElement);
  215. if (array) {
  216. AddHIDElements(array, pDevice);
  217. }
  218. }
  219. break;
  220. default:
  221. break;
  222. }
  223. if (element && headElement) { /* add to list */
  224. recElement *elementPrevious = NULL;
  225. recElement *elementCurrent = *headElement;
  226. while (elementCurrent && usage >= elementCurrent->usage) {
  227. elementPrevious = elementCurrent;
  228. elementCurrent = elementCurrent->pNext;
  229. }
  230. if (elementPrevious) {
  231. elementPrevious->pNext = element;
  232. } else {
  233. *headElement = element;
  234. }
  235. element->elementRef = refElement;
  236. element->usagePage = usagePage;
  237. element->usage = usage;
  238. element->pNext = elementCurrent;
  239. element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
  240. element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
  241. element->cookie = IOHIDElementGetCookie(refElement);
  242. pDevice->elements++;
  243. }
  244. }
  245. }
  246. static SDL_bool
  247. GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
  248. {
  249. Uint32 *guid32 = NULL;
  250. CFTypeRef refCF = NULL;
  251. CFArrayRef array = NULL;
  252. /* get usage page and usage */
  253. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey));
  254. if (refCF) {
  255. CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage);
  256. }
  257. if (pDevice->usagePage != kHIDPage_GenericDesktop) {
  258. return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
  259. }
  260. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
  261. if (refCF) {
  262. CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
  263. }
  264. if ((pDevice->usage != kHIDUsage_GD_Joystick &&
  265. pDevice->usage != kHIDUsage_GD_GamePad &&
  266. pDevice->usage != kHIDUsage_GD_MultiAxisController)) {
  267. return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
  268. }
  269. pDevice->deviceRef = hidDevice;
  270. /* get device name */
  271. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
  272. if (!refCF) {
  273. /* Maybe we can't get "AwesomeJoystick2000", but we can get "Logitech"? */
  274. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
  275. }
  276. if ((!refCF) || (!CFStringGetCString(refCF, pDevice->product, sizeof (pDevice->product), kCFStringEncodingUTF8))) {
  277. SDL_strlcpy(pDevice->product, "Unidentified joystick", sizeof (pDevice->product));
  278. }
  279. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
  280. if (refCF) {
  281. CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[0]);
  282. }
  283. refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
  284. if (refCF) {
  285. CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[8]);
  286. }
  287. /* Check to make sure we have a vendor and product ID
  288. If we don't, use the same algorithm as the Linux code for Bluetooth devices */
  289. guid32 = (Uint32*)pDevice->guid.data;
  290. if (!guid32[0] && !guid32[1]) {
  291. /* If we don't have a vendor and product ID this is probably a Bluetooth device */
  292. const Uint16 BUS_BLUETOOTH = 0x05;
  293. Uint16 *guid16 = (Uint16 *)guid32;
  294. *guid16++ = BUS_BLUETOOTH;
  295. *guid16++ = 0;
  296. SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
  297. }
  298. array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone);
  299. if (array) {
  300. AddHIDElements(array, pDevice);
  301. CFRelease(array);
  302. }
  303. return SDL_TRUE;
  304. }
  305. static void
  306. JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
  307. {
  308. if (res != kIOReturnSuccess) {
  309. return;
  310. }
  311. recDevice *device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
  312. if (!device) {
  313. SDL_OutOfMemory();
  314. return;
  315. }
  316. if (!GetDeviceInfo(ioHIDDeviceObject, device)) {
  317. SDL_free(device);
  318. return; /* not a device we care about, probably. */
  319. }
  320. /* Get notified when this device is disconnected. */
  321. IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device);
  322. IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
  323. /* Allocate an instance ID for this device */
  324. device->instance_id = ++s_joystick_instance_id;
  325. /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
  326. if (IOHIDDeviceGetService != NULL) { /* weak reference: available in 10.6 and later. */
  327. const io_service_t ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
  328. if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
  329. device->ffservice = ioservice;
  330. #if SDL_HAPTIC_IOKIT
  331. MacHaptic_MaybeAddDevice(ioservice);
  332. #endif
  333. }
  334. }
  335. device->send_open_event = 1;
  336. s_bDeviceAdded = SDL_TRUE;
  337. /* Add device to the end of the list */
  338. if ( !gpDeviceList )
  339. {
  340. gpDeviceList = device;
  341. }
  342. else
  343. {
  344. recDevice *curdevice;
  345. curdevice = gpDeviceList;
  346. while ( curdevice->pNext )
  347. {
  348. curdevice = curdevice->pNext;
  349. }
  350. curdevice->pNext = device;
  351. }
  352. }
  353. static SDL_bool
  354. ConfigHIDManager(CFArrayRef matchingArray)
  355. {
  356. CFRunLoopRef runloop = CFRunLoopGetCurrent();
  357. /* Run in a custom RunLoop mode just while initializing,
  358. so we can detect sticks without messing with everything else. */
  359. CFStringRef tempRunLoopMode = CFSTR("SDLJoystickInit");
  360. if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
  361. return SDL_FALSE;
  362. }
  363. IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL);
  364. IOHIDManagerScheduleWithRunLoop(hidman, runloop, tempRunLoopMode);
  365. IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray);
  366. while (CFRunLoopRunInMode(tempRunLoopMode,0,TRUE)==kCFRunLoopRunHandledSource) {
  367. /* no-op. Callback fires once per existing device. */
  368. }
  369. /* Put this in the normal RunLoop mode now, for future hotplug events. */
  370. IOHIDManagerUnscheduleFromRunLoop(hidman, runloop, tempRunLoopMode);
  371. IOHIDManagerScheduleWithRunLoop(hidman, runloop, kCFRunLoopDefaultMode);
  372. return SDL_TRUE; /* good to go. */
  373. }
  374. static CFDictionaryRef
  375. CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay)
  376. {
  377. CFDictionaryRef retval = NULL;
  378. CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
  379. CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
  380. const void *keys[2] = { (void *) CFSTR(kIOHIDDeviceUsagePageKey), (void *) CFSTR(kIOHIDDeviceUsageKey) };
  381. const void *vals[2] = { (void *) pageNumRef, (void *) usageNumRef };
  382. if (pageNumRef && usageNumRef) {
  383. retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
  384. }
  385. if (pageNumRef) {
  386. CFRelease(pageNumRef);
  387. }
  388. if (usageNumRef) {
  389. CFRelease(pageNumRef);
  390. }
  391. if (!retval) {
  392. *okay = 0;
  393. }
  394. return retval;
  395. }
  396. static SDL_bool
  397. CreateHIDManager(void)
  398. {
  399. SDL_bool retval = SDL_FALSE;
  400. int okay = 1;
  401. const void *vals[] = {
  402. (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
  403. (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
  404. (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
  405. };
  406. const size_t numElements = SDL_arraysize(vals);
  407. CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL;
  408. size_t i;
  409. for (i = 0; i < numElements; i++) {
  410. if (vals[i]) {
  411. CFRelease((CFTypeRef) vals[i]);
  412. }
  413. }
  414. if (array) {
  415. hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  416. if (hidman != NULL) {
  417. retval = ConfigHIDManager(array);
  418. }
  419. CFRelease(array);
  420. }
  421. return retval;
  422. }
  423. /* Function to scan the system for joysticks.
  424. * Joystick 0 should be the system default joystick.
  425. * This function should return the number of available joysticks, or -1
  426. * on an unrecoverable fatal error.
  427. */
  428. int
  429. SDL_SYS_JoystickInit(void)
  430. {
  431. if (gpDeviceList) {
  432. return SDL_SetError("Joystick: Device list already inited.");
  433. }
  434. if (!CreateHIDManager()) {
  435. return SDL_SetError("Joystick: Couldn't initialize HID Manager");
  436. }
  437. return SDL_SYS_NumJoysticks();
  438. }
  439. /* Function to return the number of joystick devices plugged in right now */
  440. int
  441. SDL_SYS_NumJoysticks()
  442. {
  443. recDevice *device = gpDeviceList;
  444. int nJoySticks = 0;
  445. while (device) {
  446. if (!device->removed) {
  447. nJoySticks++;
  448. }
  449. device = device->pNext;
  450. }
  451. return nJoySticks;
  452. }
  453. /* Function to cause any queued joystick insertions to be processed
  454. */
  455. void
  456. SDL_SYS_JoystickDetect()
  457. {
  458. if (s_bDeviceAdded || s_bDeviceRemoved) {
  459. recDevice *device = gpDeviceList;
  460. s_bDeviceAdded = SDL_FALSE;
  461. s_bDeviceRemoved = SDL_FALSE;
  462. int device_index = 0;
  463. /* send notifications */
  464. while (device) {
  465. if (device->send_open_event) {
  466. device->send_open_event = 0;
  467. /* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
  468. #if !SDL_EVENTS_DISABLED
  469. SDL_Event event;
  470. event.type = SDL_JOYDEVICEADDED;
  471. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  472. event.jdevice.which = device_index;
  473. if ((SDL_EventOK == NULL)
  474. || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
  475. SDL_PushEvent(&event);
  476. }
  477. }
  478. #endif /* !SDL_EVENTS_DISABLED */
  479. }
  480. if (device->removed) {
  481. const int instance_id = device->instance_id;
  482. device = FreeDevice(device);
  483. /* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
  484. #if !SDL_EVENTS_DISABLED
  485. SDL_Event event;
  486. event.type = SDL_JOYDEVICEREMOVED;
  487. if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  488. event.jdevice.which = instance_id;
  489. if ((SDL_EventOK == NULL)
  490. || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
  491. SDL_PushEvent(&event);
  492. }
  493. }
  494. #endif /* !SDL_EVENTS_DISABLED */
  495. } else {
  496. device = device->pNext;
  497. device_index++;
  498. }
  499. }
  500. }
  501. }
  502. SDL_bool
  503. SDL_SYS_JoystickNeedsPolling()
  504. {
  505. return s_bDeviceAdded || s_bDeviceRemoved;
  506. }
  507. /* Function to get the device-dependent name of a joystick */
  508. const char *
  509. SDL_SYS_JoystickNameForDeviceIndex(int device_index)
  510. {
  511. recDevice *device = gpDeviceList;
  512. while (device_index-- > 0) {
  513. device = device->pNext;
  514. }
  515. return device->product;
  516. }
  517. /* Function to return the instance id of the joystick at device_index
  518. */
  519. SDL_JoystickID
  520. SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
  521. {
  522. recDevice *device = gpDeviceList;
  523. int index;
  524. for (index = device_index; index > 0; index--) {
  525. device = device->pNext;
  526. }
  527. return device->instance_id;
  528. }
  529. /* Function to open a joystick for use.
  530. * The joystick to open is specified by the index field of the joystick.
  531. * This should fill the nbuttons and naxes fields of the joystick structure.
  532. * It returns 0, or -1 if there is an error.
  533. */
  534. int
  535. SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
  536. {
  537. recDevice *device = gpDeviceList;
  538. int index;
  539. for (index = device_index; index > 0; index--) {
  540. device = device->pNext;
  541. }
  542. joystick->instance_id = device->instance_id;
  543. joystick->hwdata = device;
  544. joystick->name = device->product;
  545. joystick->naxes = device->axes;
  546. joystick->nhats = device->hats;
  547. joystick->nballs = 0;
  548. joystick->nbuttons = device->buttons;
  549. return 0;
  550. }
  551. /* Function to query if the joystick is currently attached
  552. * It returns 1 if attached, 0 otherwise.
  553. */
  554. SDL_bool
  555. SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
  556. {
  557. recDevice *device = gpDeviceList;
  558. while (device) {
  559. if (joystick->instance_id == device->instance_id) {
  560. return SDL_TRUE;
  561. }
  562. device = device->pNext;
  563. }
  564. return SDL_FALSE;
  565. }
  566. /* Function to update the state of a joystick - called as a device poll.
  567. * This function shouldn't update the joystick structure directly,
  568. * but instead should call SDL_PrivateJoystick*() to deliver events
  569. * and update joystick device state.
  570. */
  571. void
  572. SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  573. {
  574. recDevice *device = joystick->hwdata;
  575. recElement *element;
  576. SInt32 value, range;
  577. int i;
  578. if (!device) {
  579. return;
  580. }
  581. if (device->removed) { /* device was unplugged; ignore it. */
  582. joystick->closed = 1;
  583. joystick->uncentered = 1;
  584. joystick->hwdata = NULL;
  585. return;
  586. }
  587. element = device->firstAxis;
  588. i = 0;
  589. while (element) {
  590. value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
  591. if (value != joystick->axes[i]) {
  592. SDL_PrivateJoystickAxis(joystick, i, value);
  593. }
  594. element = element->pNext;
  595. ++i;
  596. }
  597. element = device->firstButton;
  598. i = 0;
  599. while (element) {
  600. value = GetHIDElementState(device, element);
  601. if (value > 1) { /* handle pressure-sensitive buttons */
  602. value = 1;
  603. }
  604. if (value != joystick->buttons[i]) {
  605. SDL_PrivateJoystickButton(joystick, i, value);
  606. }
  607. element = element->pNext;
  608. ++i;
  609. }
  610. element = device->firstHat;
  611. i = 0;
  612. while (element) {
  613. Uint8 pos = 0;
  614. range = (element->max - element->min + 1);
  615. value = GetHIDElementState(device, element) - element->min;
  616. if (range == 4) { /* 4 position hatswitch - scale up value */
  617. value *= 2;
  618. } else if (range != 8) { /* Neither a 4 nor 8 positions - fall back to default position (centered) */
  619. value = -1;
  620. }
  621. switch (value) {
  622. case 0:
  623. pos = SDL_HAT_UP;
  624. break;
  625. case 1:
  626. pos = SDL_HAT_RIGHTUP;
  627. break;
  628. case 2:
  629. pos = SDL_HAT_RIGHT;
  630. break;
  631. case 3:
  632. pos = SDL_HAT_RIGHTDOWN;
  633. break;
  634. case 4:
  635. pos = SDL_HAT_DOWN;
  636. break;
  637. case 5:
  638. pos = SDL_HAT_LEFTDOWN;
  639. break;
  640. case 6:
  641. pos = SDL_HAT_LEFT;
  642. break;
  643. case 7:
  644. pos = SDL_HAT_LEFTUP;
  645. break;
  646. default:
  647. /* Every other value is mapped to center. We do that because some
  648. * joysticks use 8 and some 15 for this value, and apparently
  649. * there are even more variants out there - so we try to be generous.
  650. */
  651. pos = SDL_HAT_CENTERED;
  652. break;
  653. }
  654. if (pos != joystick->hats[i]) {
  655. SDL_PrivateJoystickHat(joystick, i, pos);
  656. }
  657. element = element->pNext;
  658. ++i;
  659. }
  660. }
  661. /* Function to close a joystick after use */
  662. void
  663. SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  664. {
  665. joystick->closed = 1;
  666. }
  667. /* Function to perform any system-specific joystick related cleanup */
  668. void
  669. SDL_SYS_JoystickQuit(void)
  670. {
  671. while (FreeDevice(gpDeviceList)) {
  672. /* spin */
  673. }
  674. if (hidman) {
  675. IOHIDManagerClose(hidman, kIOHIDOptionsTypeNone);
  676. CFRelease(hidman);
  677. hidman = NULL;
  678. }
  679. s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
  680. }
  681. SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
  682. {
  683. recDevice *device = gpDeviceList;
  684. int index;
  685. for (index = device_index; index > 0; index--) {
  686. device = device->pNext;
  687. }
  688. return device->guid;
  689. }
  690. SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
  691. {
  692. return joystick->hwdata->guid;
  693. }
  694. #endif /* SDL_JOYSTICK_IOKIT */
  695. /* vi: set ts=4 sw=4 expandtab: */