src/joystick/darwin/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2008 15:10:03 +0000
changeset 2735 204be4fc2726
parent 2713 0906692aa6a4
child 2776 caf3d5b2153f
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

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