pcsxr-1.9.92
[pcsx_rearmed.git] / macosx / plugins / DFInput / SDL / src / joystick / darwin / SDL_sysjoystick.c
1 /*
2     SDL - Simple DirectMedia Layer
3     Copyright (C) 1997-2010 Sam Lantinga
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19     Sam Lantinga
20     slouken@libsdl.org
21 */
22 #include "SDL_config.h"
23
24 #ifdef SDL_JOYSTICK_IOKIT
25
26 /* SDL joystick driver for Darwin / Mac OS X, based on the IOKit HID API */
27 /* Written 2001 by Max Horn */
28
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <sysexits.h>
32 #include <mach/mach.h>
33 #include <mach/mach_error.h>
34 #include <IOKit/IOKitLib.h>
35 #include <IOKit/IOCFPlugIn.h>
36 #ifdef MACOS_10_0_4
37 #include <IOKit/hidsystem/IOHIDUsageTables.h>
38 #else
39 /* The header was moved here in Mac OS X 10.1 */
40 #include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
41 #endif
42 #include <IOKit/hid/IOHIDLib.h>
43 #include <IOKit/hid/IOHIDKeys.h>
44 #include <CoreFoundation/CoreFoundation.h>
45 #include <Carbon/Carbon.h>      /* for NewPtrClear, DisposePtr */
46
47 /* For force feedback testing. */
48 #include <ForceFeedback/ForceFeedback.h>
49 #include <ForceFeedback/ForceFeedbackConstants.h>
50
51 #include "SDL_joystick.h"
52 #include "../SDL_sysjoystick.h"
53 #include "../SDL_joystick_c.h"
54 #include "SDL_sysjoystick_c.h"
55
56
57 /* Linked list of all available devices */
58 static recDevice *gpDeviceList = NULL;
59
60
61 static void
62 HIDReportErrorNum(char *strError, long numError)
63 {
64     SDL_SetError(strError);
65 }
66
67 static void HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
68                                      recDevice * pDevice);
69
70 /* returns current value for element, polling element
71  * will return 0 on error conditions which should be accounted for by application
72  */
73
74 static SInt32
75 HIDGetElementValue(recDevice * pDevice, recElement * pElement)
76 {
77     IOReturn result = kIOReturnSuccess;
78     IOHIDEventStruct hidEvent;
79     hidEvent.value = 0;
80
81     if (NULL != pDevice && NULL != pElement && NULL != pDevice->interface) {
82         result =
83             (*(pDevice->interface))->getElementValue(pDevice->interface,
84                                                      pElement->cookie,
85                                                      &hidEvent);
86         if (kIOReturnSuccess == result) {
87             /* record min and max for auto calibration */
88             if (hidEvent.value < pElement->minReport)
89                 pElement->minReport = hidEvent.value;
90             if (hidEvent.value > pElement->maxReport)
91                 pElement->maxReport = hidEvent.value;
92         }
93     }
94
95     /* auto user scale */
96     return hidEvent.value;
97 }
98
99 static SInt32
100 HIDScaledCalibratedValue(recDevice * pDevice, recElement * pElement,
101                          long min, long max)
102 {
103     float deviceScale = max - min;
104     float readScale = pElement->maxReport - pElement->minReport;
105     SInt32 value = HIDGetElementValue(pDevice, pElement);
106     if (readScale == 0)
107         return value;           /* no scaling at all */
108     else
109         return ((value - pElement->minReport) * deviceScale / readScale) +
110             min;
111 }
112
113
114 static void
115 HIDRemovalCallback(void *target, IOReturn result, void *refcon, void *sender)
116 {
117     recDevice *device = (recDevice *) refcon;
118     device->removed = 1;
119     device->uncentered = 1;
120 }
121
122
123
124 /* Create and open an interface to device, required prior to extracting values or building queues.
125  * Note: appliction now owns the device and must close and release it prior to exiting
126  */
127
128 static IOReturn
129 HIDCreateOpenDeviceInterface(io_object_t hidDevice, recDevice * pDevice)
130 {
131     IOReturn result = kIOReturnSuccess;
132     HRESULT plugInResult = S_OK;
133     SInt32 score = 0;
134     IOCFPlugInInterface **ppPlugInInterface = NULL;
135
136     if (NULL == pDevice->interface) {
137         result =
138             IOCreatePlugInInterfaceForService(hidDevice,
139                                               kIOHIDDeviceUserClientTypeID,
140                                               kIOCFPlugInInterfaceID,
141                                               &ppPlugInInterface, &score);
142         if (kIOReturnSuccess == result) {
143             /* Call a method of the intermediate plug-in to create the device interface */
144             plugInResult =
145                 (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
146                                                      CFUUIDGetUUIDBytes
147                                                      (kIOHIDDeviceInterfaceID),
148                                                      (void *)
149                                                      &(pDevice->interface));
150             if (S_OK != plugInResult)
151                 HIDReportErrorNum
152                     ("CouldnĂ•t query HID class device interface from plugInInterface",
153                      plugInResult);
154             (*ppPlugInInterface)->Release(ppPlugInInterface);
155         } else
156             HIDReportErrorNum
157                 ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.",
158                  result);
159     }
160     if (NULL != pDevice->interface) {
161         result = (*(pDevice->interface))->open(pDevice->interface, 0);
162         if (kIOReturnSuccess != result)
163             HIDReportErrorNum
164                 ("Failed to open pDevice->interface via open.", result);
165         else
166             (*(pDevice->interface))->setRemovalCallback(pDevice->interface,
167                                                         HIDRemovalCallback,
168                                                         pDevice, pDevice);
169
170     }
171     return result;
172 }
173
174 /* Closes and releases interface to device, should be done prior to exting application
175  * Note: will have no affect if device or interface do not exist
176  * application will "own" the device if interface is not closed
177  * (device may have to be plug and re-plugged in different location to get it working again without a restart)
178  */
179
180 static IOReturn
181 HIDCloseReleaseInterface(recDevice * pDevice)
182 {
183     IOReturn result = kIOReturnSuccess;
184
185     if ((NULL != pDevice) && (NULL != pDevice->interface)) {
186         /* close the interface */
187         result = (*(pDevice->interface))->close(pDevice->interface);
188         if (kIOReturnNotOpen == result) {
189             /* do nothing as device was not opened, thus can't be closed */
190         } else if (kIOReturnSuccess != result)
191             HIDReportErrorNum("Failed to close IOHIDDeviceInterface.",
192                               result);
193         /* release the interface */
194         result = (*(pDevice->interface))->Release(pDevice->interface);
195         if (kIOReturnSuccess != result)
196             HIDReportErrorNum("Failed to release IOHIDDeviceInterface.",
197                               result);
198         pDevice->interface = NULL;
199     }
200     return result;
201 }
202
203 /* extracts actual specific element information from each element CF dictionary entry */
204
205 static void
206 HIDGetElementInfo(CFTypeRef refElement, recElement * pElement)
207 {
208     long number;
209     CFTypeRef refType;
210
211     refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementCookieKey));
212     if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
213         pElement->cookie = (IOHIDElementCookie) number;
214     refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMinKey));
215     if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
216         pElement->minReport = pElement->min = number;
217     pElement->maxReport = pElement->min;
218     refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMaxKey));
219     if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
220         pElement->maxReport = pElement->max = number;
221 /*
222         TODO: maybe should handle the following stuff somehow?
223
224         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMinKey));
225         if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
226                 pElement->scaledMin = number;
227         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMaxKey));
228         if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
229                 pElement->scaledMax = number;
230         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementSizeKey));
231         if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
232                 pElement->size = number;
233         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsRelativeKey));
234         if (refType)
235                 pElement->relative = CFBooleanGetValue (refType);
236         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsWrappingKey));
237         if (refType)
238                 pElement->wrapping = CFBooleanGetValue (refType);
239         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsNonLinearKey));
240         if (refType)
241                 pElement->nonLinear = CFBooleanGetValue (refType);
242         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferedStateKey));
243         if (refType)
244                 pElement->preferredState = CFBooleanGetValue (refType);
245         refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasNullStateKey));
246         if (refType)
247                 pElement->nullState = CFBooleanGetValue (refType);
248 */
249 }
250
251 /* examines CF dictionary vlaue in device element hierarchy to determine if it is element of interest or a collection of more elements
252  * if element of interest allocate storage, add to list and retrieve element specific info
253  * if collection then pass on to deconstruction collection into additional individual elements
254  */
255
256 static void
257 HIDAddElement(CFTypeRef refElement, recDevice * pDevice)
258 {
259     recElement *element = NULL;
260     recElement **headElement = NULL;
261     long elementType, usagePage, usage;
262     CFTypeRef refElementType =
263         CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
264     CFTypeRef refUsagePage =
265         CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
266     CFTypeRef refUsage =
267         CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
268
269
270     if ((refElementType)
271         &&
272         (CFNumberGetValue(refElementType, kCFNumberLongType, &elementType))) {
273         /* look at types of interest */
274         if ((elementType == kIOHIDElementTypeInput_Misc)
275             || (elementType == kIOHIDElementTypeInput_Button)
276             || (elementType == kIOHIDElementTypeInput_Axis)) {
277             if (refUsagePage
278                 && CFNumberGetValue(refUsagePage, kCFNumberLongType,
279                                     &usagePage) && refUsage
280                 && CFNumberGetValue(refUsage, kCFNumberLongType, &usage)) {
281                 switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
282                 case kHIDPage_GenericDesktop:
283                     {
284                         switch (usage) {        /* look at usage to determine function */
285                         case kHIDUsage_GD_X:
286                         case kHIDUsage_GD_Y:
287                         case kHIDUsage_GD_Z:
288                         case kHIDUsage_GD_Rx:
289                         case kHIDUsage_GD_Ry:
290                         case kHIDUsage_GD_Rz:
291                         case kHIDUsage_GD_Slider:
292                         case kHIDUsage_GD_Dial:
293                         case kHIDUsage_GD_Wheel:
294                             element = (recElement *)
295                                 NewPtrClear(sizeof(recElement));
296                             if (element) {
297                                 pDevice->axes++;
298                                 headElement = &(pDevice->firstAxis);
299                             }
300                             break;
301                         case kHIDUsage_GD_Hatswitch:
302                             element = (recElement *)
303                                 NewPtrClear(sizeof(recElement));
304                             if (element) {
305                                 pDevice->hats++;
306                                 headElement = &(pDevice->firstHat);
307                             }
308                             break;
309                         }
310                     }
311                     break;
312                 case kHIDPage_Button:
313                     element = (recElement *)
314                         NewPtrClear(sizeof(recElement));
315                     if (element) {
316                         pDevice->buttons++;
317                         headElement = &(pDevice->firstButton);
318                     }
319                     break;
320                 default:
321                     break;
322                 }
323             }
324         } else if (kIOHIDElementTypeCollection == elementType)
325             HIDGetCollectionElements((CFMutableDictionaryRef) refElement,
326                                      pDevice);
327     }
328
329     if (element && headElement) {       /* add to list */
330         recElement *elementPrevious = NULL;
331         recElement *elementCurrent = *headElement;
332         while (elementCurrent && usage >= elementCurrent->usage) {
333             elementPrevious = elementCurrent;
334             elementCurrent = elementCurrent->pNext;
335         }
336         if (elementPrevious) {
337             elementPrevious->pNext = element;
338         } else {
339             *headElement = element;
340         }
341         element->usagePage = usagePage;
342         element->usage = usage;
343         element->pNext = elementCurrent;
344         HIDGetElementInfo(refElement, element);
345         pDevice->elements++;
346     }
347 }
348
349 /* collects information from each array member in device element list (each array memeber = element) */
350
351 static void
352 HIDGetElementsCFArrayHandler(const void *value, void *parameter)
353 {
354     if (CFGetTypeID(value) == CFDictionaryGetTypeID())
355         HIDAddElement((CFTypeRef) value, (recDevice *) parameter);
356 }
357
358 /* handles retrieval of element information from arrays of elements in device IO registry information */
359
360 static void
361 HIDGetElements(CFTypeRef refElementCurrent, recDevice * pDevice)
362 {
363     CFTypeID type = CFGetTypeID(refElementCurrent);
364     if (type == CFArrayGetTypeID()) {   /* if element is an array */
365         CFRange range = { 0, CFArrayGetCount(refElementCurrent) };
366         /* CountElementsCFArrayHandler called for each array member */
367         CFArrayApplyFunction(refElementCurrent, range,
368                              HIDGetElementsCFArrayHandler, pDevice);
369     }
370 }
371
372 /* handles extracting element information from element collection CF types
373  * used from top level element decoding and hierarchy deconstruction to flatten device element list
374  */
375
376 static void
377 HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
378                          recDevice * pDevice)
379 {
380     CFTypeRef refElementTop =
381         CFDictionaryGetValue(deviceProperties, CFSTR(kIOHIDElementKey));
382     if (refElementTop)
383         HIDGetElements(refElementTop, pDevice);
384 }
385
386 /* use top level element usage page and usage to discern device usage page and usage setting appropriate vlaues in device record */
387
388 static void
389 HIDTopLevelElementHandler(const void *value, void *parameter)
390 {
391     CFTypeRef refCF = 0;
392     if (CFGetTypeID(value) != CFDictionaryGetTypeID())
393         return;
394     refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsagePageKey));
395     if (!CFNumberGetValue
396         (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
397         SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage.");
398     refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsageKey));
399     if (!CFNumberGetValue
400         (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
401         SDL_SetError("CFNumberGetValue error retrieving pDevice->usage.");
402 }
403
404 /* extracts device info from CF dictionary records in IO registry */
405
406 static void
407 HIDGetDeviceInfo(io_object_t hidDevice, CFMutableDictionaryRef hidProperties,
408                  recDevice * pDevice)
409 {
410     CFMutableDictionaryRef usbProperties = 0;
411     io_registry_entry_t parent1, parent2;
412
413     /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
414      * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
415      */
416     if ((KERN_SUCCESS ==
417          IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent1))
418         && (KERN_SUCCESS ==
419             IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
420         && (KERN_SUCCESS ==
421             IORegistryEntryCreateCFProperties(parent2, &usbProperties,
422                                               kCFAllocatorDefault,
423                                               kNilOptions))) {
424         if (usbProperties) {
425             CFTypeRef refCF = 0;
426             /* get device info
427              * try hid dictionary first, if fail then go to usb dictionary
428              */
429
430
431             /* get product name */
432             refCF =
433                 CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
434             if (!refCF)
435                 refCF =
436                     CFDictionaryGetValue(usbProperties,
437                                          CFSTR("USB Product Name"));
438             if (refCF) {
439                 if (!CFStringGetCString
440                     (refCF, pDevice->product, 256,
441                      CFStringGetSystemEncoding()))
442                     SDL_SetError
443                         ("CFStringGetCString error retrieving pDevice->product.");
444             }
445
446             /* get usage page and usage */
447             refCF =
448                 CFDictionaryGetValue(hidProperties,
449                                      CFSTR(kIOHIDPrimaryUsagePageKey));
450             if (refCF) {
451                 if (!CFNumberGetValue
452                     (refCF, kCFNumberLongType, &pDevice->usagePage))
453                     SDL_SetError
454                         ("CFNumberGetValue error retrieving pDevice->usagePage.");
455                 refCF =
456                     CFDictionaryGetValue(hidProperties,
457                                          CFSTR(kIOHIDPrimaryUsageKey));
458                 if (refCF)
459                     if (!CFNumberGetValue
460                         (refCF, kCFNumberLongType, &pDevice->usage))
461                         SDL_SetError
462                             ("CFNumberGetValue error retrieving pDevice->usage.");
463             }
464
465             if (NULL == refCF) {        /* get top level element HID usage page or usage */
466                 /* use top level element instead */
467                 CFTypeRef refCFTopElement = 0;
468                 refCFTopElement =
469                     CFDictionaryGetValue(hidProperties,
470                                          CFSTR(kIOHIDElementKey));
471                 {
472                     /* refCFTopElement points to an array of element dictionaries */
473                     CFRange range = { 0, CFArrayGetCount(refCFTopElement) };
474                     CFArrayApplyFunction(refCFTopElement, range,
475                                          HIDTopLevelElementHandler, pDevice);
476                 }
477             }
478
479             CFRelease(usbProperties);
480         } else
481             SDL_SetError
482                 ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
483
484         if (kIOReturnSuccess != IOObjectRelease(parent2))
485             SDL_SetError("IOObjectRelease error with parent2.");
486         if (kIOReturnSuccess != IOObjectRelease(parent1))
487             SDL_SetError("IOObjectRelease error with parent1.");
488     }
489 }
490
491
492 static recDevice *
493 HIDBuildDevice(io_object_t hidDevice)
494 {
495     recDevice *pDevice = (recDevice *) NewPtrClear(sizeof(recDevice));
496     if (pDevice) {
497         /* get dictionary for HID properties */
498         CFMutableDictionaryRef hidProperties = 0;
499         kern_return_t result =
500             IORegistryEntryCreateCFProperties(hidDevice, &hidProperties,
501                                               kCFAllocatorDefault,
502                                               kNilOptions);
503         if ((result == KERN_SUCCESS) && hidProperties) {
504             /* create device interface */
505             result = HIDCreateOpenDeviceInterface(hidDevice, pDevice);
506             if (kIOReturnSuccess == result) {
507                 HIDGetDeviceInfo(hidDevice, hidProperties, pDevice);    /* hidDevice used to find parents in registry tree */
508                 HIDGetCollectionElements(hidProperties, pDevice);
509             } else {
510                 DisposePtr((Ptr) pDevice);
511                 pDevice = NULL;
512             }
513             CFRelease(hidProperties);
514         } else {
515             DisposePtr((Ptr) pDevice);
516             pDevice = NULL;
517         }
518     }
519     return pDevice;
520 }
521
522 /* disposes of the element list associated with a device and the memory associated with the list
523  */
524
525 static void
526 HIDDisposeElementList(recElement ** elementList)
527 {
528     recElement *pElement = *elementList;
529     while (pElement) {
530         recElement *pElementNext = pElement->pNext;
531         DisposePtr((Ptr) pElement);
532         pElement = pElementNext;
533     }
534     *elementList = NULL;
535 }
536
537 /* disposes of a single device, closing and releaseing interface, freeing memory fro device and elements, setting device pointer to NULL
538  * all your device no longer belong to us... (i.e., you do not 'own' the device anymore)
539  */
540
541 static recDevice *
542 HIDDisposeDevice(recDevice ** ppDevice)
543 {
544     kern_return_t result = KERN_SUCCESS;
545     recDevice *pDeviceNext = NULL;
546     if (*ppDevice) {
547         /* save next device prior to disposing of this device */
548         pDeviceNext = (*ppDevice)->pNext;
549
550         /* free posible io_service_t */
551         if ((*ppDevice)->ffservice) {
552             IOObjectRelease((*ppDevice)->ffservice);
553             (*ppDevice)->ffservice = 0;
554         }
555
556         /* free element lists */
557         HIDDisposeElementList(&(*ppDevice)->firstAxis);
558         HIDDisposeElementList(&(*ppDevice)->firstButton);
559         HIDDisposeElementList(&(*ppDevice)->firstHat);
560
561         result = HIDCloseReleaseInterface(*ppDevice);   /* function sanity checks interface value (now application does not own device) */
562         if (kIOReturnSuccess != result)
563             HIDReportErrorNum
564                 ("HIDCloseReleaseInterface failed when trying to dipose device.",
565                  result);
566         DisposePtr((Ptr) * ppDevice);
567         *ppDevice = NULL;
568     }
569     return pDeviceNext;
570 }
571
572
573 /* Function to scan the system for joysticks.
574  * Joystick 0 should be the system default joystick.
575  * This function should return the number of available joysticks, or -1
576  * on an unrecoverable fatal error.
577  */
578 int
579 SDL_SYS_JoystickInit(void)
580 {
581     IOReturn result = kIOReturnSuccess;
582     mach_port_t masterPort = 0;
583     io_iterator_t hidObjectIterator = 0;
584     CFMutableDictionaryRef hidMatchDictionary = NULL;
585     recDevice *device, *lastDevice;
586     io_object_t ioHIDDeviceObject = 0;
587
588     SDL_numjoysticks = 0;
589
590     if (gpDeviceList) {
591         SDL_SetError("Joystick: Device list already inited.");
592         return -1;
593     }
594
595     result = IOMasterPort(bootstrap_port, &masterPort);
596     if (kIOReturnSuccess != result) {
597         SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
598         return -1;
599     }
600
601     /* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
602     hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
603     if (hidMatchDictionary) {
604         /* Add key for device type (joystick, in this case) to refine the matching dictionary. */
605
606         /* NOTE: we now perform this filtering later
607            UInt32 usagePage = kHIDPage_GenericDesktop;
608            UInt32 usage = kHIDUsage_GD_Joystick;
609            CFNumberRef refUsage = NULL, refUsagePage = NULL;
610
611            refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
612            CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
613            refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
614            CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
615          */
616     } else {
617         SDL_SetError
618             ("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
619         return -1;
620     }
621
622     /*/ Now search I/O Registry for matching devices. */
623     result =
624         IOServiceGetMatchingServices(masterPort, hidMatchDictionary,
625                                      &hidObjectIterator);
626     /* Check for errors */
627     if (kIOReturnSuccess != result) {
628         SDL_SetError("Joystick: Couldn't create a HID object iterator.");
629         return -1;
630     }
631     if (!hidObjectIterator) {   /* there are no joysticks */
632         gpDeviceList = NULL;
633         SDL_numjoysticks = 0;
634         return 0;
635     }
636     /* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
637
638     /* build flat linked list of devices from device iterator */
639
640     gpDeviceList = lastDevice = NULL;
641
642     while ((ioHIDDeviceObject = IOIteratorNext(hidObjectIterator))) {
643         /* build a device record */
644         device = HIDBuildDevice(ioHIDDeviceObject);
645         if (!device)
646             continue;
647
648         /* Filter device list to non-keyboard/mouse stuff */
649         if ((device->usagePage != kHIDPage_GenericDesktop) ||
650             ((device->usage != kHIDUsage_GD_Joystick &&
651               device->usage != kHIDUsage_GD_GamePad &&
652               device->usage != kHIDUsage_GD_MultiAxisController))) {
653
654             /* release memory for the device */
655             HIDDisposeDevice(&device);
656             DisposePtr((Ptr) device);
657             continue;
658         }
659
660         /* We have to do some storage of the io_service_t for
661          * SDL_HapticOpenFromJoystick */
662         if (FFIsForceFeedback(ioHIDDeviceObject) == FF_OK) {
663             device->ffservice = ioHIDDeviceObject;
664         } else {
665             device->ffservice = 0;
666         }
667
668         /* Add device to the end of the list */
669         if (lastDevice)
670             lastDevice->pNext = device;
671         else
672             gpDeviceList = device;
673         lastDevice = device;
674     }
675     result = IOObjectRelease(hidObjectIterator);        /* release the iterator */
676
677     /* Count the total number of devices we found */
678     device = gpDeviceList;
679     while (device) {
680         SDL_numjoysticks++;
681         device = device->pNext;
682     }
683
684     return SDL_numjoysticks;
685 }
686
687 /* Function to get the device-dependent name of a joystick */
688 const char *
689 SDL_SYS_JoystickName(int index)
690 {
691     recDevice *device = gpDeviceList;
692
693     for (; index > 0; index--)
694         device = device->pNext;
695
696     return device->product;
697 }
698
699 /* Function to open a joystick for use.
700  * The joystick to open is specified by the index field of the joystick.
701  * This should fill the nbuttons and naxes fields of the joystick structure.
702  * It returns 0, or -1 if there is an error.
703  */
704 int
705 SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
706 {
707     recDevice *device = gpDeviceList;
708     int index;
709
710     for (index = joystick->index; index > 0; index--)
711         device = device->pNext;
712
713     joystick->hwdata = device;
714     joystick->name = device->product;
715
716     joystick->naxes = device->axes;
717     joystick->nhats = device->hats;
718     joystick->nballs = 0;
719     joystick->nbuttons = device->buttons;
720
721     return 0;
722 }
723
724 /* Function to update the state of a joystick - called as a device poll.
725  * This function shouldn't update the joystick structure directly,
726  * but instead should call SDL_PrivateJoystick*() to deliver events
727  * and update joystick device state.
728  */
729 void
730 SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
731 {
732     recDevice *device = joystick->hwdata;
733     recElement *element;
734     SInt32 value, range;
735     int i;
736
737     if (device->removed) {      /* device was unplugged; ignore it. */
738         if (device->uncentered) {
739             device->uncentered = 0;
740
741             /* Tell the app that everything is centered/unpressed... */
742             for (i = 0; i < device->axes; i++)
743                 SDL_PrivateJoystickAxis(joystick, i, 0);
744
745             for (i = 0; i < device->buttons; i++)
746                 SDL_PrivateJoystickButton(joystick, i, 0);
747
748             for (i = 0; i < device->hats; i++)
749                 SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
750         }
751
752         return;
753     }
754
755     element = device->firstAxis;
756     i = 0;
757     while (element) {
758         value = HIDScaledCalibratedValue(device, element, -32768, 32767);
759         if (value != joystick->axes[i])
760             SDL_PrivateJoystickAxis(joystick, i, value);
761         element = element->pNext;
762         ++i;
763     }
764
765     element = device->firstButton;
766     i = 0;
767     while (element) {
768         value = HIDGetElementValue(device, element);
769         if (value > 1)          /* handle pressure-sensitive buttons */
770             value = 1;
771         if (value != joystick->buttons[i])
772             SDL_PrivateJoystickButton(joystick, i, value);
773         element = element->pNext;
774         ++i;
775     }
776
777     element = device->firstHat;
778     i = 0;
779     while (element) {
780         Uint8 pos = 0;
781
782         range = (element->max - element->min + 1);
783         value = HIDGetElementValue(device, element) - element->min;
784         if (range == 4)         /* 4 position hatswitch - scale up value */
785             value *= 2;
786         else if (range != 8)    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
787             value = -1;
788         switch (value) {
789         case 0:
790             pos = SDL_HAT_UP;
791             break;
792         case 1:
793             pos = SDL_HAT_RIGHTUP;
794             break;
795         case 2:
796             pos = SDL_HAT_RIGHT;
797             break;
798         case 3:
799             pos = SDL_HAT_RIGHTDOWN;
800             break;
801         case 4:
802             pos = SDL_HAT_DOWN;
803             break;
804         case 5:
805             pos = SDL_HAT_LEFTDOWN;
806             break;
807         case 6:
808             pos = SDL_HAT_LEFT;
809             break;
810         case 7:
811             pos = SDL_HAT_LEFTUP;
812             break;
813         default:
814             /* Every other value is mapped to center. We do that because some
815              * joysticks use 8 and some 15 for this value, and apparently
816              * there are even more variants out there - so we try to be generous.
817              */
818             pos = SDL_HAT_CENTERED;
819             break;
820         }
821         if (pos != joystick->hats[i])
822             SDL_PrivateJoystickHat(joystick, i, pos);
823         element = element->pNext;
824         ++i;
825     }
826
827     return;
828 }
829
830 /* Function to close a joystick after use */
831 void
832 SDL_SYS_JoystickClose(SDL_Joystick * joystick)
833 {
834     /* Should we do anything here? */
835     return;
836 }
837
838 /* Function to perform any system-specific joystick related cleanup */
839 void
840 SDL_SYS_JoystickQuit(void)
841 {
842     while (NULL != gpDeviceList)
843         gpDeviceList = HIDDisposeDevice(&gpDeviceList);
844 }
845
846 #endif /* SDL_JOYSTICK_IOKIT */
847 /* vi: set ts=4 sw=4 expandtab: */