Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
authorRyan C. Gordon <icculus@icculus.org>
Sat, 22 Feb 2014 00:55:28 -0500
changeset 82301c185bacf915
parent 8229 437c4e5961e0
child 8231 ad3edb1923f2
Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.

Besides being a little more simple to use than the earlier IOKit HID API, and
less likely to be deprecated, it also has the added benefit of working with
the Sony DualShock 4 controller in Bluetooth mode out of the box, whereas
the previous API has a bug that makes it report bad data for the
controller.

Cleaned up several other things in this code, having gone over every line of
it. The remaining deprecated calls are also gone.
src/joystick/darwin/SDL_sysjoystick.c
src/joystick/darwin/SDL_sysjoystick_c.h
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Fri Feb 21 08:37:57 2014 -0300
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Sat Feb 22 00:55:28 2014 -0500
     1.3 @@ -22,22 +22,7 @@
     1.4  
     1.5  #ifdef SDL_JOYSTICK_IOKIT
     1.6  
     1.7 -/* SDL joystick driver for Darwin / Mac OS X, based on the IOKit HID API */
     1.8 -/* Written 2001 by Max Horn */
     1.9 -
    1.10 -#include <unistd.h>
    1.11 -#include <ctype.h>
    1.12 -#include <sysexits.h>
    1.13 -#include <mach/mach.h>
    1.14 -#include <mach/mach_error.h>
    1.15 -#include <IOKit/IOKitLib.h>
    1.16 -#include <IOKit/IOCFPlugIn.h>
    1.17 -#include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
    1.18  #include <IOKit/hid/IOHIDLib.h>
    1.19 -#include <IOKit/hid/IOHIDKeys.h>
    1.20 -#include <CoreFoundation/CoreFoundation.h>
    1.21 -#include <Carbon/Carbon.h>      /* for NewPtrClear, DisposePtr */
    1.22 -#include <IOKit/IOMessage.h>
    1.23  
    1.24  /* For force feedback testing. */
    1.25  #include <ForceFeedback/ForceFeedback.h>
    1.26 @@ -53,75 +38,99 @@
    1.27  #include "../../events/SDL_events_c.h"
    1.28  #endif
    1.29  
    1.30 +/* The base object of the HID Manager API */
    1.31 +static IOHIDManagerRef hidman = NULL;
    1.32  
    1.33  /* Linked list of all available devices */
    1.34  static recDevice *gpDeviceList = NULL;
    1.35 -/* OSX reference to the notification object that tells us about device insertion/removal */
    1.36 -IONotificationPortRef notificationPort = 0;
    1.37 -/* if 1 then a device was added since the last update call */
    1.38 +
    1.39 +/* if SDL_TRUE then a device was added since the last update call */
    1.40  static SDL_bool s_bDeviceAdded = SDL_FALSE;
    1.41  static SDL_bool s_bDeviceRemoved = SDL_FALSE;
    1.42  
    1.43  /* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
    1.44  static int s_joystick_instance_id = -1;
    1.45  
    1.46 +
    1.47  static void
    1.48 -HIDReportErrorNum(char *strError, long numError)
    1.49 +FreeElementList(recElement *pElement)
    1.50  {
    1.51 -    SDL_SetError(strError);
    1.52 +    while (pElement) {
    1.53 +        recElement *pElementNext = pElement->pNext;
    1.54 +        SDL_free(pElement);
    1.55 +        pElement = pElementNext;
    1.56 +    }
    1.57  }
    1.58  
    1.59 -static void HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
    1.60 -                                     recDevice * pDevice);
    1.61 +static recDevice *
    1.62 +FreeDevice(recDevice *removeDevice)
    1.63 +{
    1.64 +    recDevice *pDeviceNext = NULL;
    1.65 +    if (removeDevice) {
    1.66 +        /* save next device prior to disposing of this device */
    1.67 +        pDeviceNext = removeDevice->pNext;
    1.68  
    1.69 -/* returns current value for element, polling element
    1.70 - * will return 0 on error conditions which should be accounted for by application
    1.71 - */
    1.72 +        if ( gpDeviceList == removeDevice ) {
    1.73 +            gpDeviceList = pDeviceNext;
    1.74 +        } else {
    1.75 +            recDevice *device = gpDeviceList;
    1.76 +            while (device->pNext != removeDevice) {
    1.77 +                device = device->pNext;
    1.78 +            }
    1.79 +            device->pNext = pDeviceNext;
    1.80 +        }
    1.81 +        removeDevice->pNext = NULL;
    1.82 +
    1.83 +        /* free element lists */
    1.84 +        FreeElementList(removeDevice->firstAxis);
    1.85 +        FreeElementList(removeDevice->firstButton);
    1.86 +        FreeElementList(removeDevice->firstHat);
    1.87 +
    1.88 +        SDL_free(removeDevice);
    1.89 +    }
    1.90 +    return pDeviceNext;
    1.91 +}
    1.92  
    1.93  static SInt32
    1.94 -HIDGetElementValue(recDevice * pDevice, recElement * pElement)
    1.95 +GetHIDElementState(recDevice *pDevice, recElement *pElement)
    1.96  {
    1.97 -    IOReturn result = kIOReturnSuccess;
    1.98 -    IOHIDEventStruct hidEvent;
    1.99 -    hidEvent.value = 0;
   1.100 +    SInt32 value = 0;
   1.101  
   1.102 -    if (NULL != pDevice && NULL != pElement && NULL != pDevice->interface) {
   1.103 -        result =
   1.104 -            (*(pDevice->interface))->getElementValue(pDevice->interface,
   1.105 -                                                     pElement->cookie,
   1.106 -                                                     &hidEvent);
   1.107 -        if (kIOReturnSuccess == result) {
   1.108 +    if (pDevice && pElement) {
   1.109 +        IOHIDValueRef valueRef;
   1.110 +        if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) {
   1.111 +            value = (SInt32) IOHIDValueGetIntegerValue(valueRef);
   1.112 +
   1.113              /* record min and max for auto calibration */
   1.114 -            if (hidEvent.value < pElement->minReport)
   1.115 -                pElement->minReport = hidEvent.value;
   1.116 -            if (hidEvent.value > pElement->maxReport)
   1.117 -                pElement->maxReport = hidEvent.value;
   1.118 +            if (value < pElement->minReport) {
   1.119 +                pElement->minReport = value;
   1.120 +            }
   1.121 +            if (value > pElement->maxReport) {
   1.122 +                pElement->maxReport = value;
   1.123 +            }
   1.124          }
   1.125      }
   1.126  
   1.127 -    /* auto user scale */
   1.128 -    return hidEvent.value;
   1.129 +    return value;
   1.130  }
   1.131  
   1.132  static SInt32
   1.133 -HIDScaledCalibratedValue(recDevice * pDevice, recElement * pElement,
   1.134 -                         long min, long max)
   1.135 +GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max)
   1.136  {
   1.137 -    float deviceScale = max - min;
   1.138 -    float readScale = pElement->maxReport - pElement->minReport;
   1.139 -    SInt32 value = HIDGetElementValue(pDevice, pElement);
   1.140 -    if (readScale == 0)
   1.141 +    const float deviceScale = max - min;
   1.142 +    const float readScale = pElement->maxReport - pElement->minReport;
   1.143 +    const SInt32 value = GetHIDElementState(pDevice, pElement);
   1.144 +    if (readScale == 0) {
   1.145          return value;           /* no scaling at all */
   1.146 -    else
   1.147 -        return ((value - pElement->minReport) * deviceScale / readScale) +
   1.148 -            min;
   1.149 +    }
   1.150 +    return ((value - pElement->minReport) * deviceScale / readScale) + min;
   1.151  }
   1.152  
   1.153  
   1.154  static void
   1.155 -HIDRemovalCallback(void *target, IOReturn result, void *refcon, void *sender)
   1.156 +JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
   1.157  {
   1.158 -    recDevice *device = (recDevice *) refcon;
   1.159 +    recDevice *device = (recDevice *) ctx;
   1.160      device->removed = 1;
   1.161  #if SDL_HAPTIC_IOKIT
   1.162      MacHaptic_MaybeRemoveDevice(device->ffservice);
   1.163 @@ -130,567 +139,240 @@
   1.164  }
   1.165  
   1.166  
   1.167 -/* Called by the io port notifier on removal of this device
   1.168 - */
   1.169 -void JoystickDeviceWasRemovedCallback( void * refcon, io_service_t service, natural_t messageType, void * messageArgument )
   1.170 +static void AddHIDElement(const void *value, void *parameter);
   1.171 +
   1.172 +/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */
   1.173 +static void
   1.174 +AddHIDElements(CFArrayRef array, recDevice *pDevice)
   1.175  {
   1.176 -    if( messageType == kIOMessageServiceIsTerminated && refcon )
   1.177 -    {
   1.178 -        recDevice *device = (recDevice *) refcon;
   1.179 -        device->removed = 1;
   1.180 -#if SDL_HAPTIC_IOKIT
   1.181 -        MacHaptic_MaybeRemoveDevice(device->ffservice);
   1.182 -#endif
   1.183 -        s_bDeviceRemoved = SDL_TRUE;
   1.184 -    }
   1.185 +    const CFRange range = { 0, CFArrayGetCount(array) };
   1.186 +    CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
   1.187  }
   1.188  
   1.189 +/* See if we care about this HID element, and if so, note it in our recDevice. */
   1.190 +static void
   1.191 +AddHIDElement(const void *value, void *parameter)
   1.192 +{
   1.193 +    recDevice *pDevice = (recDevice *) parameter;
   1.194 +    IOHIDElementRef refElement = (IOHIDElementRef) value;
   1.195 +    const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
   1.196  
   1.197 -/* Create and open an interface to device, required prior to extracting values or building queues.
   1.198 - * Note: application now owns the device and must close and release it prior to exiting
   1.199 - */
   1.200 +    if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
   1.201 +        const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
   1.202 +        const uint32_t usage = IOHIDElementGetUsage(refElement);
   1.203 +        recElement *element = NULL;
   1.204 +        recElement **headElement = NULL;
   1.205  
   1.206 -static IOReturn
   1.207 -HIDCreateOpenDeviceInterface(io_object_t hidDevice, recDevice * pDevice)
   1.208 -{
   1.209 -    IOReturn result = kIOReturnSuccess;
   1.210 -    HRESULT plugInResult = S_OK;
   1.211 -    SInt32 score = 0;
   1.212 -    IOCFPlugInInterface **ppPlugInInterface = NULL;
   1.213 +        /* look at types of interest */
   1.214 +        switch (IOHIDElementGetType(refElement)) {
   1.215 +            case kIOHIDElementTypeInput_Misc:
   1.216 +            case kIOHIDElementTypeInput_Button:
   1.217 +            case kIOHIDElementTypeInput_Axis: {
   1.218 +                switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
   1.219 +                    case kHIDPage_GenericDesktop:
   1.220 +                        switch (usage) {
   1.221 +                            case kHIDUsage_GD_X:
   1.222 +                            case kHIDUsage_GD_Y:
   1.223 +                            case kHIDUsage_GD_Z:
   1.224 +                            case kHIDUsage_GD_Rx:
   1.225 +                            case kHIDUsage_GD_Ry:
   1.226 +                            case kHIDUsage_GD_Rz:
   1.227 +                            case kHIDUsage_GD_Slider:
   1.228 +                            case kHIDUsage_GD_Dial:
   1.229 +                            case kHIDUsage_GD_Wheel:
   1.230 +                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
   1.231 +                                if (element) {
   1.232 +                                    pDevice->axes++;
   1.233 +                                    headElement = &(pDevice->firstAxis);
   1.234 +                                }
   1.235 +                                break;
   1.236  
   1.237 -    if (NULL == pDevice->interface) {
   1.238 -        result =
   1.239 -            IOCreatePlugInInterfaceForService(hidDevice,
   1.240 -                                              kIOHIDDeviceUserClientTypeID,
   1.241 -                                              kIOCFPlugInInterfaceID,
   1.242 -                                              &ppPlugInInterface, &score);
   1.243 -        if (kIOReturnSuccess == result) {
   1.244 -            /* Call a method of the intermediate plug-in to create the device interface */
   1.245 -            plugInResult =
   1.246 -                (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
   1.247 -                                                     CFUUIDGetUUIDBytes
   1.248 -                                                     (kIOHIDDeviceInterfaceID),
   1.249 -                                                     (void *)
   1.250 -                                                     &(pDevice->interface));
   1.251 -            if (S_OK != plugInResult)
   1.252 -                HIDReportErrorNum
   1.253 -                    ("Couldn't query HID class device interface from plugInInterface",
   1.254 -                     plugInResult);
   1.255 -            (*ppPlugInInterface)->Release(ppPlugInInterface);
   1.256 -        } else
   1.257 -            HIDReportErrorNum
   1.258 -                ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.",
   1.259 -                 result);
   1.260 -    }
   1.261 -    if (NULL != pDevice->interface) {
   1.262 -        result = (*(pDevice->interface))->open(pDevice->interface, 0);
   1.263 -        if (kIOReturnSuccess != result)
   1.264 -            HIDReportErrorNum
   1.265 -                ("Failed to open pDevice->interface via open.", result);
   1.266 -        else
   1.267 -        {
   1.268 -            pDevice->portIterator = 0;
   1.269 +                            case kHIDUsage_GD_Hatswitch:
   1.270 +                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
   1.271 +                                if (element) {
   1.272 +                                    pDevice->hats++;
   1.273 +                                    headElement = &(pDevice->firstHat);
   1.274 +                                }
   1.275 +                                break;
   1.276 +                        }
   1.277 +                        break;
   1.278  
   1.279 -            /* It's okay if this fails, we have another detection method below */
   1.280 -            (*(pDevice->interface))->setRemovalCallback(pDevice->interface,
   1.281 -                                                        HIDRemovalCallback,
   1.282 -                                                        pDevice, pDevice);
   1.283 +                    case kHIDPage_Simulation:
   1.284 +                        switch (usage) {
   1.285 +                            case kHIDUsage_Sim_Rudder:
   1.286 +                            case kHIDUsage_Sim_Throttle:
   1.287 +                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
   1.288 +                                if (element) {
   1.289 +                                    pDevice->axes++;
   1.290 +                                    headElement = &(pDevice->firstAxis);
   1.291 +                                }
   1.292 +                                break;
   1.293  
   1.294 -            /* now connect notification for new devices */
   1.295 -            pDevice->notificationPort = IONotificationPortCreate(kIOMasterPortDefault);
   1.296 +                            default:
   1.297 +                                break;
   1.298 +                        }
   1.299 +                        break;
   1.300  
   1.301 -            CFRunLoopAddSource(CFRunLoopGetCurrent(),
   1.302 -                               IONotificationPortGetRunLoopSource(pDevice->notificationPort),
   1.303 -                               kCFRunLoopDefaultMode);
   1.304 +                    case kHIDPage_Button:
   1.305 +                        element = (recElement *) SDL_calloc(1, sizeof (recElement));
   1.306 +                        if (element) {
   1.307 +                            pDevice->buttons++;
   1.308 +                            headElement = &(pDevice->firstButton);
   1.309 +                        }
   1.310 +                        break;
   1.311  
   1.312 -            /* Register for notifications when a serial port is added to the system */
   1.313 -            result = IOServiceAddInterestNotification(pDevice->notificationPort,
   1.314 -                                                      hidDevice,
   1.315 -                                                      kIOGeneralInterest,
   1.316 -                                                      JoystickDeviceWasRemovedCallback,
   1.317 -                                                      pDevice,
   1.318 -                                                      &pDevice->portIterator);
   1.319 -            if (kIOReturnSuccess != result) {
   1.320 -                HIDReportErrorNum
   1.321 -                    ("Failed to register for removal callback.", result);
   1.322 +                    default:
   1.323 +                        break;
   1.324 +                }
   1.325              }
   1.326 +            break;
   1.327 +
   1.328 +            #if 0  /* !!! FIXME: this causes everything to get added twice on a DualShock 4. */
   1.329 +            case kIOHIDElementTypeCollection: {
   1.330 +                CFArrayRef array = IOHIDElementGetChildren(refElement);
   1.331 +                if (array) {
   1.332 +                    AddHIDElements(array, pDevice);
   1.333 +                }
   1.334 +            }
   1.335 +            break;
   1.336 +            #endif
   1.337 +
   1.338 +            default:
   1.339 +                break;
   1.340          }
   1.341  
   1.342 -    }
   1.343 -    return result;
   1.344 -}
   1.345 -
   1.346 -/* Closes and releases interface to device, should be done prior to exiting application
   1.347 - * Note: will have no affect if device or interface do not exist
   1.348 - * application will "own" the device if interface is not closed
   1.349 - * (device may have to be plug and re-plugged in different location to get it working again without a restart)
   1.350 - */
   1.351 -
   1.352 -static IOReturn
   1.353 -HIDCloseReleaseInterface(recDevice * pDevice)
   1.354 -{
   1.355 -    IOReturn result = kIOReturnSuccess;
   1.356 -
   1.357 -    if ((NULL != pDevice) && (NULL != pDevice->interface)) {
   1.358 -        /* close the interface */
   1.359 -        result = (*(pDevice->interface))->close(pDevice->interface);
   1.360 -        if (kIOReturnNotOpen == result) {
   1.361 -            /* do nothing as device was not opened, thus can't be closed */
   1.362 -        } else if (kIOReturnSuccess != result)
   1.363 -            HIDReportErrorNum("Failed to close IOHIDDeviceInterface.",
   1.364 -                              result);
   1.365 -        /* release the interface */
   1.366 -        result = (*(pDevice->interface))->Release(pDevice->interface);
   1.367 -        if (kIOReturnSuccess != result)
   1.368 -            HIDReportErrorNum("Failed to release IOHIDDeviceInterface.",
   1.369 -                              result);
   1.370 -        pDevice->interface = NULL;
   1.371 -
   1.372 -        if ( pDevice->portIterator )
   1.373 -        {
   1.374 -            IOObjectRelease( pDevice->portIterator );
   1.375 -            pDevice->portIterator = 0;
   1.376 -        }
   1.377 -    }
   1.378 -    return result;
   1.379 -}
   1.380 -
   1.381 -/* extracts actual specific element information from each element CF dictionary entry */
   1.382 -
   1.383 -static void
   1.384 -HIDGetElementInfo(CFTypeRef refElement, recElement * pElement)
   1.385 -{
   1.386 -    long number;
   1.387 -    CFTypeRef refType;
   1.388 -
   1.389 -    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementCookieKey));
   1.390 -    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.391 -        pElement->cookie = (IOHIDElementCookie) number;
   1.392 -    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMinKey));
   1.393 -    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.394 -        pElement->minReport = pElement->min = number;
   1.395 -    pElement->maxReport = pElement->min;
   1.396 -    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMaxKey));
   1.397 -    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.398 -        pElement->maxReport = pElement->max = number;
   1.399 -/*
   1.400 -    TODO: maybe should handle the following stuff somehow?
   1.401 -
   1.402 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMinKey));
   1.403 -    if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.404 -        pElement->scaledMin = number;
   1.405 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMaxKey));
   1.406 -    if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.407 -        pElement->scaledMax = number;
   1.408 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementSizeKey));
   1.409 -    if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.410 -        pElement->size = number;
   1.411 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsRelativeKey));
   1.412 -    if (refType)
   1.413 -        pElement->relative = CFBooleanGetValue (refType);
   1.414 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsWrappingKey));
   1.415 -    if (refType)
   1.416 -        pElement->wrapping = CFBooleanGetValue (refType);
   1.417 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsNonLinearKey));
   1.418 -    if (refType)
   1.419 -        pElement->nonLinear = CFBooleanGetValue (refType);
   1.420 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferedStateKey));
   1.421 -    if (refType)
   1.422 -        pElement->preferredState = CFBooleanGetValue (refType);
   1.423 -    refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasNullStateKey));
   1.424 -    if (refType)
   1.425 -        pElement->nullState = CFBooleanGetValue (refType);
   1.426 -*/
   1.427 -}
   1.428 -
   1.429 -/* examines CF dictionary value in device element hierarchy to determine if it is element of interest or a collection of more elements
   1.430 - * if element of interest allocate storage, add to list and retrieve element specific info
   1.431 - * if collection then pass on to deconstruction collection into additional individual elements
   1.432 - */
   1.433 -
   1.434 -static void
   1.435 -HIDAddElement(CFTypeRef refElement, recDevice * pDevice)
   1.436 -{
   1.437 -    recElement *element = NULL;
   1.438 -    recElement **headElement = NULL;
   1.439 -    long elementType, usagePage, usage;
   1.440 -    CFTypeRef refElementType =
   1.441 -        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
   1.442 -    CFTypeRef refUsagePage =
   1.443 -        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
   1.444 -    CFTypeRef refUsage =
   1.445 -        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
   1.446 -
   1.447 -
   1.448 -    if ((refElementType)
   1.449 -        &&
   1.450 -        (CFNumberGetValue(refElementType, kCFNumberLongType, &elementType))) {
   1.451 -        /* look at types of interest */
   1.452 -        if ((elementType == kIOHIDElementTypeInput_Misc)
   1.453 -            || (elementType == kIOHIDElementTypeInput_Button)
   1.454 -            || (elementType == kIOHIDElementTypeInput_Axis)) {
   1.455 -            if (refUsagePage
   1.456 -                && CFNumberGetValue(refUsagePage, kCFNumberLongType,
   1.457 -                                    &usagePage) && refUsage
   1.458 -                && CFNumberGetValue(refUsage, kCFNumberLongType, &usage)) {
   1.459 -                switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
   1.460 -                case kHIDPage_GenericDesktop:
   1.461 -                    {
   1.462 -                        switch (usage) {        /* look at usage to determine function */
   1.463 -                        case kHIDUsage_GD_X:
   1.464 -                        case kHIDUsage_GD_Y:
   1.465 -                        case kHIDUsage_GD_Z:
   1.466 -                        case kHIDUsage_GD_Rx:
   1.467 -                        case kHIDUsage_GD_Ry:
   1.468 -                        case kHIDUsage_GD_Rz:
   1.469 -                        case kHIDUsage_GD_Slider:
   1.470 -                        case kHIDUsage_GD_Dial:
   1.471 -                        case kHIDUsage_GD_Wheel:
   1.472 -                            element = (recElement *)
   1.473 -                                NewPtrClear(sizeof(recElement));
   1.474 -                            if (element) {
   1.475 -                                pDevice->axes++;
   1.476 -                                headElement = &(pDevice->firstAxis);
   1.477 -                            }
   1.478 -                            break;
   1.479 -                        case kHIDUsage_GD_Hatswitch:
   1.480 -                            element = (recElement *)
   1.481 -                                NewPtrClear(sizeof(recElement));
   1.482 -                            if (element) {
   1.483 -                                pDevice->hats++;
   1.484 -                                headElement = &(pDevice->firstHat);
   1.485 -                            }
   1.486 -                            break;
   1.487 -                        }
   1.488 -                    }
   1.489 -                    break;
   1.490 -                case kHIDPage_Simulation:
   1.491 -                    switch (usage) {
   1.492 -                        case kHIDUsage_Sim_Rudder:
   1.493 -                        case kHIDUsage_Sim_Throttle:
   1.494 -                            element = (recElement *)
   1.495 -                                NewPtrClear(sizeof(recElement));
   1.496 -                            if (element) {
   1.497 -                                pDevice->axes++;
   1.498 -                                headElement = &(pDevice->firstAxis);
   1.499 -                            }
   1.500 -                            break;
   1.501 -
   1.502 -                        default:
   1.503 -                            break;
   1.504 -                    }
   1.505 -                    break;
   1.506 -                case kHIDPage_Button:
   1.507 -                    element = (recElement *)
   1.508 -                        NewPtrClear(sizeof(recElement));
   1.509 -                    if (element) {
   1.510 -                        pDevice->buttons++;
   1.511 -                        headElement = &(pDevice->firstButton);
   1.512 -                    }
   1.513 -                    break;
   1.514 -                default:
   1.515 -                    break;
   1.516 -                }
   1.517 +        if (element && headElement) {       /* add to list */
   1.518 +            recElement *elementPrevious = NULL;
   1.519 +            recElement *elementCurrent = *headElement;
   1.520 +            while (elementCurrent && usage >= elementCurrent->usage) {
   1.521 +                elementPrevious = elementCurrent;
   1.522 +                elementCurrent = elementCurrent->pNext;
   1.523              }
   1.524 -        } else if (kIOHIDElementTypeCollection == elementType)
   1.525 -            HIDGetCollectionElements((CFMutableDictionaryRef) refElement,
   1.526 -                                     pDevice);
   1.527 -    }
   1.528 -
   1.529 -    if (element && headElement) {       /* add to list */
   1.530 -        recElement *elementPrevious = NULL;
   1.531 -        recElement *elementCurrent = *headElement;
   1.532 -        while (elementCurrent && usage >= elementCurrent->usage) {
   1.533 -            elementPrevious = elementCurrent;
   1.534 -            elementCurrent = elementCurrent->pNext;
   1.535 -        }
   1.536 -        if (elementPrevious) {
   1.537 -            elementPrevious->pNext = element;
   1.538 -        } else {
   1.539 -            *headElement = element;
   1.540 -        }
   1.541 -        element->usagePage = usagePage;
   1.542 -        element->usage = usage;
   1.543 -        element->pNext = elementCurrent;
   1.544 -        HIDGetElementInfo(refElement, element);
   1.545 -        pDevice->elements++;
   1.546 -    }
   1.547 -}
   1.548 -
   1.549 -/* collects information from each array member in device element list (each array member = element) */
   1.550 -
   1.551 -static void
   1.552 -HIDGetElementsCFArrayHandler(const void *value, void *parameter)
   1.553 -{
   1.554 -    if (CFGetTypeID(value) == CFDictionaryGetTypeID())
   1.555 -        HIDAddElement((CFTypeRef) value, (recDevice *) parameter);
   1.556 -}
   1.557 -
   1.558 -/* handles retrieval of element information from arrays of elements in device IO registry information */
   1.559 -
   1.560 -static void
   1.561 -HIDGetElements(CFTypeRef refElementCurrent, recDevice * pDevice)
   1.562 -{
   1.563 -    CFTypeID type = CFGetTypeID(refElementCurrent);
   1.564 -    if (type == CFArrayGetTypeID()) {   /* if element is an array */
   1.565 -        CFRange range = { 0, CFArrayGetCount(refElementCurrent) };
   1.566 -        /* CountElementsCFArrayHandler called for each array member */
   1.567 -        CFArrayApplyFunction(refElementCurrent, range,
   1.568 -                             HIDGetElementsCFArrayHandler, pDevice);
   1.569 -    }
   1.570 -}
   1.571 -
   1.572 -/* handles extracting element information from element collection CF types
   1.573 - * used from top level element decoding and hierarchy deconstruction to flatten device element list
   1.574 - */
   1.575 -
   1.576 -static void
   1.577 -HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
   1.578 -                         recDevice * pDevice)
   1.579 -{
   1.580 -    CFTypeRef refElementTop =
   1.581 -        CFDictionaryGetValue(deviceProperties, CFSTR(kIOHIDElementKey));
   1.582 -    if (refElementTop)
   1.583 -        HIDGetElements(refElementTop, pDevice);
   1.584 -}
   1.585 -
   1.586 -/* use top level element usage page and usage to discern device usage page and usage setting appropriate vlaues in device record */
   1.587 -
   1.588 -static void
   1.589 -HIDTopLevelElementHandler(const void *value, void *parameter)
   1.590 -{
   1.591 -    CFTypeRef refCF = 0;
   1.592 -    if (CFGetTypeID(value) != CFDictionaryGetTypeID())
   1.593 -        return;
   1.594 -    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsagePageKey));
   1.595 -    if (!CFNumberGetValue
   1.596 -        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
   1.597 -        SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.598 -    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsageKey));
   1.599 -    if (!CFNumberGetValue
   1.600 -        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
   1.601 -        SDL_SetError("CFNumberGetValue error retrieving pDevice->usage.");
   1.602 -}
   1.603 -
   1.604 -/* extracts device info from CF dictionary records in IO registry */
   1.605 -
   1.606 -static void
   1.607 -HIDGetDeviceInfo(io_object_t hidDevice, CFMutableDictionaryRef hidProperties,
   1.608 -                 recDevice * pDevice)
   1.609 -{
   1.610 -    CFMutableDictionaryRef usbProperties = 0;
   1.611 -    io_registry_entry_t parent1, parent2;
   1.612 -
   1.613 -    /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
   1.614 -     * get dictionary for USB properties: step up two levels and get CF dictionary for USB properties
   1.615 -     */
   1.616 -    if ((KERN_SUCCESS == IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent1))
   1.617 -        && (KERN_SUCCESS == IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
   1.618 -        && (KERN_SUCCESS == IORegistryEntryCreateCFProperties(parent2, &usbProperties, kCFAllocatorDefault, kNilOptions))) {
   1.619 -        if (usbProperties) {
   1.620 -            CFTypeRef refCF = 0;
   1.621 -            /* get device info
   1.622 -             * try hid dictionary first, if fail then go to usb dictionary
   1.623 -             */
   1.624 -
   1.625 -            /* get product name */
   1.626 -            refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
   1.627 -            if (!refCF) {
   1.628 -                refCF = CFDictionaryGetValue(usbProperties, CFSTR("USB Product Name"));
   1.629 -            }
   1.630 -            if (refCF) {
   1.631 -                if (!CFStringGetCString(refCF, pDevice->product, 256, CFStringGetSystemEncoding())) {
   1.632 -                    SDL_SetError("CFStringGetCString error retrieving pDevice->product.");
   1.633 -                }
   1.634 +            if (elementPrevious) {
   1.635 +                elementPrevious->pNext = element;
   1.636 +            } else {
   1.637 +                *headElement = element;
   1.638              }
   1.639  
   1.640 -            /* get usage page and usage */
   1.641 -            refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
   1.642 -            if (refCF) {
   1.643 -                if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage)) {
   1.644 -                    SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.645 -                }
   1.646 +            element->elementRef = refElement;
   1.647 +            element->usagePage = usagePage;
   1.648 +            element->usage = usage;
   1.649 +            element->pNext = elementCurrent;
   1.650  
   1.651 -                refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
   1.652 -                if (refCF) {
   1.653 -                    if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage)) {
   1.654 -                        SDL_SetError("CFNumberGetValue error retrieving pDevice->usage.");
   1.655 -                    }
   1.656 -                }
   1.657 -            }
   1.658 +            element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
   1.659 +            element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
   1.660  
   1.661 -            refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDVendorIDKey));
   1.662 -            if (refCF) {
   1.663 -                if (!CFNumberGetValue(refCF, kCFNumberLongType, &pDevice->guid.data[0])) {
   1.664 -                    SDL_SetError("CFNumberGetValue error retrieving pDevice->guid[0]");
   1.665 -                }
   1.666 -            }
   1.667 -
   1.668 -            refCF = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductIDKey));
   1.669 -            if (refCF) {
   1.670 -                if (!CFNumberGetValue(refCF, kCFNumberLongType, &pDevice->guid.data[8])) {
   1.671 -                    SDL_SetError("CFNumberGetValue error retrieving pDevice->guid[8]");
   1.672 -                }
   1.673 -            }
   1.674 -
   1.675 -            /* Check to make sure we have a vendor and product ID
   1.676 -               If we don't, use the same algorithm as the Linux code for Bluetooth devices */
   1.677 -            {
   1.678 -                Uint32 *guid32 = (Uint32*)pDevice->guid.data;
   1.679 -                if (!guid32[0] && !guid32[1]) {
   1.680 -                    const Uint16 BUS_BLUETOOTH = 0x05;
   1.681 -                    Uint16 *guid16 = (Uint16 *)guid32;
   1.682 -                    *guid16++ = BUS_BLUETOOTH;
   1.683 -                    *guid16++ = 0;
   1.684 -                    SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
   1.685 -                }
   1.686 -            }
   1.687 -
   1.688 -            /* If we don't have a vendor and product ID this is probably a Bluetooth device */
   1.689 -
   1.690 -            if (NULL == refCF) {    /* get top level element HID usage page or usage */
   1.691 -                /* use top level element instead */
   1.692 -                CFTypeRef refCFTopElement = 0;
   1.693 -                refCFTopElement = CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDElementKey));
   1.694 -                {
   1.695 -                    /* refCFTopElement points to an array of element dictionaries */
   1.696 -                    CFRange range = { 0, CFArrayGetCount(refCFTopElement) };
   1.697 -                    CFArrayApplyFunction(refCFTopElement, range, HIDTopLevelElementHandler, pDevice);
   1.698 -                }
   1.699 -            }
   1.700 -
   1.701 -            CFRelease(usbProperties);
   1.702 -        } else {
   1.703 -            SDL_SetError("IORegistryEntryCreateCFProperties failed to create usbProperties.");
   1.704 -        }
   1.705 -
   1.706 -        if (kIOReturnSuccess != IOObjectRelease(parent2)) {
   1.707 -            SDL_SetError("IOObjectRelease error with parent2");
   1.708 -        }
   1.709 -        if (kIOReturnSuccess != IOObjectRelease(parent1)) {
   1.710 -            SDL_SetError("IOObjectRelease error with parent1");
   1.711 +            pDevice->elements++;
   1.712          }
   1.713      }
   1.714  }
   1.715  
   1.716 +static SDL_bool
   1.717 +GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
   1.718 +{
   1.719 +    Uint32 *guid32 = NULL;
   1.720 +    CFTypeRef refCF = NULL;
   1.721 +    CFArrayRef array = NULL;
   1.722  
   1.723 -static recDevice *
   1.724 -HIDBuildDevice(io_object_t hidDevice)
   1.725 -{
   1.726 -    recDevice *pDevice = (recDevice *) NewPtrClear(sizeof(recDevice));
   1.727 -    if (pDevice) {
   1.728 -        /* get dictionary for HID properties */
   1.729 -        CFMutableDictionaryRef hidProperties = 0;
   1.730 -        kern_return_t result =
   1.731 -            IORegistryEntryCreateCFProperties(hidDevice, &hidProperties,
   1.732 -                                              kCFAllocatorDefault,
   1.733 -                                              kNilOptions);
   1.734 -        if ((result == KERN_SUCCESS) && hidProperties) {
   1.735 -            /* create device interface */
   1.736 -            result = HIDCreateOpenDeviceInterface(hidDevice, pDevice);
   1.737 -            if (kIOReturnSuccess == result) {
   1.738 -                HIDGetDeviceInfo(hidDevice, hidProperties, pDevice);    /* hidDevice used to find parents in registry tree */
   1.739 -                HIDGetCollectionElements(hidProperties, pDevice);
   1.740 -            } else {
   1.741 -                DisposePtr((Ptr) pDevice);
   1.742 -                pDevice = NULL;
   1.743 -            }
   1.744 -            CFRelease(hidProperties);
   1.745 -        } else {
   1.746 -            DisposePtr((Ptr) pDevice);
   1.747 -            pDevice = NULL;
   1.748 -        }
   1.749 +    /* get usage page and usage */
   1.750 +    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey));
   1.751 +    if (refCF) {
   1.752 +        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage);
   1.753      }
   1.754 -    return pDevice;
   1.755 +    if (pDevice->usagePage != kHIDPage_GenericDesktop) {
   1.756 +        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
   1.757 +    }
   1.758 +
   1.759 +    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
   1.760 +    if (refCF) {
   1.761 +        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
   1.762 +    }
   1.763 +
   1.764 +    if ((pDevice->usage != kHIDUsage_GD_Joystick &&
   1.765 +         pDevice->usage != kHIDUsage_GD_GamePad &&
   1.766 +         pDevice->usage != kHIDUsage_GD_MultiAxisController)) {
   1.767 +        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
   1.768 +    }
   1.769 +
   1.770 +    pDevice->deviceRef = hidDevice;
   1.771 +
   1.772 +    /* get device name */
   1.773 +    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
   1.774 +    if (!refCF) {
   1.775 +        /* Maybe we can't get "AwesomeJoystick2000", but we can get "Logitech"? */
   1.776 +        refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
   1.777 +    }
   1.778 +    if ((!refCF) || (!CFStringGetCString(refCF, pDevice->product, sizeof (pDevice->product), kCFStringEncodingUTF8))) {
   1.779 +        SDL_strlcpy(pDevice->product, "Unidentified joystick", sizeof (pDevice->product));
   1.780 +    }
   1.781 +
   1.782 +    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
   1.783 +    if (refCF) {
   1.784 +        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[0]);
   1.785 +    }
   1.786 +
   1.787 +    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
   1.788 +    if (refCF) {
   1.789 +        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[8]);
   1.790 +    }
   1.791 +
   1.792 +    /* Check to make sure we have a vendor and product ID
   1.793 +       If we don't, use the same algorithm as the Linux code for Bluetooth devices */
   1.794 +    guid32 = (Uint32*)pDevice->guid.data;
   1.795 +    if (!guid32[0] && !guid32[1]) {
   1.796 +        /* If we don't have a vendor and product ID this is probably a Bluetooth device */
   1.797 +        const Uint16 BUS_BLUETOOTH = 0x05;
   1.798 +        Uint16 *guid16 = (Uint16 *)guid32;
   1.799 +        *guid16++ = BUS_BLUETOOTH;
   1.800 +        *guid16++ = 0;
   1.801 +        SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
   1.802 +    }
   1.803 +
   1.804 +    array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone);
   1.805 +    if (array) {
   1.806 +        AddHIDElements(array, pDevice);
   1.807 +        CFRelease(array);
   1.808 +    }
   1.809 +
   1.810 +    return SDL_TRUE;
   1.811  }
   1.812  
   1.813 -/* disposes of the element list associated with a device and the memory associated with the list
   1.814 - */
   1.815  
   1.816  static void
   1.817 -HIDDisposeElementList(recElement ** elementList)
   1.818 +JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
   1.819  {
   1.820 -    recElement *pElement = *elementList;
   1.821 -    while (pElement) {
   1.822 -        recElement *pElementNext = pElement->pNext;
   1.823 -        DisposePtr((Ptr) pElement);
   1.824 -        pElement = pElementNext;
   1.825 +    if (res != kIOReturnSuccess) {
   1.826 +        return;
   1.827      }
   1.828 -    *elementList = NULL;
   1.829 -}
   1.830  
   1.831 -/* disposes of a single device, closing and releaseing interface, freeing memory fro device and elements, setting device pointer to NULL
   1.832 - * all your device no longer belong to us... (i.e., you do not 'own' the device anymore)
   1.833 - */
   1.834 +    recDevice *device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
   1.835  
   1.836 -static recDevice *
   1.837 -HIDDisposeDevice(recDevice ** ppDevice)
   1.838 -{
   1.839 -    kern_return_t result = KERN_SUCCESS;
   1.840 -    recDevice *pDeviceNext = NULL;
   1.841 -    if (*ppDevice) {
   1.842 -        /* save next device prior to disposing of this device */
   1.843 -        pDeviceNext = (*ppDevice)->pNext;
   1.844 +    if (!device) {
   1.845 +        SDL_OutOfMemory();
   1.846 +        return;
   1.847 +    }
   1.848  
   1.849 -        /* free posible io_service_t */
   1.850 -        if ((*ppDevice)->ffservice) {
   1.851 -            IOObjectRelease((*ppDevice)->ffservice);
   1.852 -            (*ppDevice)->ffservice = 0;
   1.853 -        }
   1.854 +    if (!GetDeviceInfo(ioHIDDeviceObject, device)) {
   1.855 +        SDL_free(device);
   1.856 +        return;   /* not a device we care about, probably. */
   1.857 +    }
   1.858  
   1.859 -        /* free element lists */
   1.860 -        HIDDisposeElementList(&(*ppDevice)->firstAxis);
   1.861 -        HIDDisposeElementList(&(*ppDevice)->firstButton);
   1.862 -        HIDDisposeElementList(&(*ppDevice)->firstHat);
   1.863 -
   1.864 -        result = HIDCloseReleaseInterface(*ppDevice);   /* function sanity checks interface value (now application does not own device) */
   1.865 -        if (kIOReturnSuccess != result)
   1.866 -            HIDReportErrorNum
   1.867 -                ("HIDCloseReleaseInterface failed when trying to dipose device.",
   1.868 -                 result);
   1.869 -        DisposePtr((Ptr) * ppDevice);
   1.870 -        *ppDevice = NULL;
   1.871 -    }
   1.872 -    return pDeviceNext;
   1.873 -}
   1.874 -
   1.875 -
   1.876 -/* Given an io_object_t from OSX adds a joystick device to our list if appropriate
   1.877 - */
   1.878 -int
   1.879 -AddDeviceHelper( io_object_t ioHIDDeviceObject )
   1.880 -{
   1.881 -    recDevice *device;
   1.882 -
   1.883 -    /* build a device record */
   1.884 -    device = HIDBuildDevice(ioHIDDeviceObject);
   1.885 -    if (!device)
   1.886 -        return 0;
   1.887 -
   1.888 -    /* Filter device list to non-keyboard/mouse stuff */
   1.889 -    if ((device->usagePage != kHIDPage_GenericDesktop) ||
   1.890 -        ((device->usage != kHIDUsage_GD_Joystick &&
   1.891 -          device->usage != kHIDUsage_GD_GamePad &&
   1.892 -          device->usage != kHIDUsage_GD_MultiAxisController))) {
   1.893 -
   1.894 -        /* release memory for the device */
   1.895 -        HIDDisposeDevice(&device);
   1.896 -        DisposePtr((Ptr) device);
   1.897 -        return 0;
   1.898 -    }
   1.899 +    /* Get notified when this device is disconnected. */
   1.900 +    IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device);
   1.901 +    IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
   1.902  
   1.903      /* Allocate an instance ID for this device */
   1.904      device->instance_id = ++s_joystick_instance_id;
   1.905  
   1.906 -    /* We have to do some storage of the io_service_t for
   1.907 -     * SDL_HapticOpenFromJoystick */
   1.908 -    if (FFIsForceFeedback(ioHIDDeviceObject) == FF_OK) {
   1.909 -        device->ffservice = ioHIDDeviceObject;
   1.910 +    /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
   1.911 +    if (IOHIDDeviceGetService != NULL) {  /* weak reference: available in 10.6 and later. */
   1.912 +        const io_service_t ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
   1.913 +        if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
   1.914 +            device->ffservice = ioservice;
   1.915  #if SDL_HAPTIC_IOKIT
   1.916 -        MacHaptic_MaybeAddDevice(ioHIDDeviceObject);
   1.917 +            MacHaptic_MaybeAddDevice(ioservice);
   1.918  #endif
   1.919 -    } else {
   1.920 -        device->ffservice = 0;
   1.921 +        }
   1.922      }
   1.923  
   1.924      device->send_open_event = 1;
   1.925 @@ -712,25 +394,93 @@
   1.926          }
   1.927          curdevice->pNext = device;
   1.928      }
   1.929 -
   1.930 -    return 1;
   1.931  }
   1.932  
   1.933 +static SDL_bool
   1.934 +ConfigHIDManager(CFArrayRef matchingArray)
   1.935 +{
   1.936 +    CFRunLoopRef runloop = CFRunLoopGetCurrent();
   1.937  
   1.938 -/* Called by our IO port notifier on the master port when a HID device is inserted, we iterate
   1.939 - *  and check for new joysticks
   1.940 - */
   1.941 -void JoystickDeviceWasAddedCallback( void *refcon, io_iterator_t iterator )
   1.942 +    /* Run in a custom RunLoop mode just while initializing,
   1.943 +       so we can detect sticks without messing with everything else. */
   1.944 +    CFStringRef tempRunLoopMode = CFSTR("SDLJoystickInit");
   1.945 +
   1.946 +    if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
   1.947 +        return SDL_FALSE;
   1.948 +    }
   1.949 +
   1.950 +    IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL);
   1.951 +    IOHIDManagerScheduleWithRunLoop(hidman, runloop, tempRunLoopMode);
   1.952 +    IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray);
   1.953 +
   1.954 +    while (CFRunLoopRunInMode(tempRunLoopMode,0,TRUE)==kCFRunLoopRunHandledSource) {
   1.955 +        /* no-op. Callback fires once per existing device. */
   1.956 +    }
   1.957 +
   1.958 +    /* Put this in the normal RunLoop mode now, for future hotplug events. */
   1.959 +    IOHIDManagerUnscheduleFromRunLoop(hidman, runloop, tempRunLoopMode);
   1.960 +    IOHIDManagerScheduleWithRunLoop(hidman, runloop, kCFRunLoopDefaultMode);
   1.961 +
   1.962 +    return SDL_TRUE;  /* good to go. */
   1.963 +}
   1.964 +
   1.965 +
   1.966 +static CFDictionaryRef
   1.967 +CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay)
   1.968  {
   1.969 -    io_object_t ioHIDDeviceObject = 0;
   1.970 +    CFDictionaryRef retval = NULL;
   1.971 +    CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
   1.972 +    CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
   1.973 +    const void *keys[2] = { (void *) CFSTR(kIOHIDDeviceUsagePageKey), (void *) CFSTR(kIOHIDDeviceUsageKey) };
   1.974 +    const void *vals[2] = { (void *) pageNumRef, (void *) usageNumRef };
   1.975  
   1.976 -    while ( ( ioHIDDeviceObject = IOIteratorNext(iterator) ) )
   1.977 -    {
   1.978 -        if ( ioHIDDeviceObject )
   1.979 -        {
   1.980 -            AddDeviceHelper( ioHIDDeviceObject );
   1.981 +    if (pageNumRef && usageNumRef) {
   1.982 +        retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
   1.983 +    }
   1.984 +
   1.985 +    if (pageNumRef) {
   1.986 +        CFRelease(pageNumRef);
   1.987 +    }
   1.988 +    if (usageNumRef) {
   1.989 +        CFRelease(pageNumRef);
   1.990 +    }
   1.991 +
   1.992 +    if (!retval) {
   1.993 +        *okay = 0;
   1.994 +    }
   1.995 +
   1.996 +    return retval;
   1.997 +}
   1.998 +
   1.999 +static SDL_bool
  1.1000 +CreateHIDManager(void)
  1.1001 +{
  1.1002 +    SDL_bool retval = SDL_FALSE;
  1.1003 +    int okay = 1;
  1.1004 +    const void *vals[] = {
  1.1005 +        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
  1.1006 +        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
  1.1007 +        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
  1.1008 +    };
  1.1009 +    const size_t numElements = SDL_arraysize(vals);
  1.1010 +    CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL;
  1.1011 +    size_t i;
  1.1012 +
  1.1013 +    for (i = 0; i < numElements; i++) {
  1.1014 +        if (vals[i]) {
  1.1015 +            CFRelease((CFTypeRef) vals[i]);
  1.1016          }
  1.1017      }
  1.1018 +
  1.1019 +    if (array) {
  1.1020 +        hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
  1.1021 +        if (hidman != NULL) {
  1.1022 +            retval = ConfigHIDManager(array);
  1.1023 +        }
  1.1024 +        CFRelease(array);
  1.1025 +    }
  1.1026 +
  1.1027 +    return retval;
  1.1028  }
  1.1029  
  1.1030  
  1.1031 @@ -742,82 +492,14 @@
  1.1032  int
  1.1033  SDL_SYS_JoystickInit(void)
  1.1034  {
  1.1035 -    IOReturn result = kIOReturnSuccess;
  1.1036 -    mach_port_t masterPort = 0;
  1.1037 -    io_iterator_t hidObjectIterator = 0;
  1.1038 -    CFMutableDictionaryRef hidMatchDictionary = NULL;
  1.1039 -    io_object_t ioHIDDeviceObject = 0;
  1.1040 -    io_iterator_t portIterator = 0;
  1.1041 -
  1.1042      if (gpDeviceList) {
  1.1043          return SDL_SetError("Joystick: Device list already inited.");
  1.1044      }
  1.1045  
  1.1046 -    result = IOMasterPort(bootstrap_port, &masterPort);
  1.1047 -    if (kIOReturnSuccess != result) {
  1.1048 -        return SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
  1.1049 +    if (!CreateHIDManager()) {
  1.1050 +        return SDL_SetError("Joystick: Couldn't initialize HID Manager");
  1.1051      }
  1.1052  
  1.1053 -    /* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
  1.1054 -    hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
  1.1055 -    if (hidMatchDictionary) {
  1.1056 -        /* Add key for device type (joystick, in this case) to refine the matching dictionary. */
  1.1057 -
  1.1058 -        /* NOTE: we now perform this filtering later
  1.1059 -           UInt32 usagePage = kHIDPage_GenericDesktop;
  1.1060 -           UInt32 usage = kHIDUsage_GD_Joystick;
  1.1061 -           CFNumberRef refUsage = NULL, refUsagePage = NULL;
  1.1062 -
  1.1063 -           refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
  1.1064 -           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
  1.1065 -           refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
  1.1066 -           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
  1.1067 -         */
  1.1068 -    } else {
  1.1069 -        return SDL_SetError
  1.1070 -            ("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
  1.1071 -    }
  1.1072 -
  1.1073 -    /* Now search I/O Registry for matching devices. */
  1.1074 -    result =
  1.1075 -        IOServiceGetMatchingServices(masterPort, hidMatchDictionary,
  1.1076 -                                     &hidObjectIterator);
  1.1077 -    /* Check for errors */
  1.1078 -    if (kIOReturnSuccess != result) {
  1.1079 -        return SDL_SetError("Joystick: Couldn't create a HID object iterator.");
  1.1080 -    }
  1.1081 -    if (!hidObjectIterator) {   /* there are no joysticks */
  1.1082 -        gpDeviceList = NULL;
  1.1083 -        return 0;
  1.1084 -    }
  1.1085 -    /* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
  1.1086 -
  1.1087 -    /* build flat linked list of devices from device iterator */
  1.1088 -
  1.1089 -    gpDeviceList = NULL;
  1.1090 -
  1.1091 -    while ((ioHIDDeviceObject = IOIteratorNext(hidObjectIterator))) {
  1.1092 -        AddDeviceHelper( ioHIDDeviceObject );
  1.1093 -    }
  1.1094 -    result = IOObjectRelease(hidObjectIterator);        /* release the iterator */
  1.1095 -
  1.1096 -    /* now connect notification for new devices */
  1.1097 -    notificationPort = IONotificationPortCreate(masterPort);
  1.1098 -    hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
  1.1099 -
  1.1100 -    CFRunLoopAddSource(CFRunLoopGetCurrent(),
  1.1101 -                       IONotificationPortGetRunLoopSource(notificationPort),
  1.1102 -                       kCFRunLoopDefaultMode);
  1.1103 -
  1.1104 -    /* Register for notifications when a serial port is added to the system */
  1.1105 -    result = IOServiceAddMatchingNotification(notificationPort,
  1.1106 -                                                            kIOFirstMatchNotification,
  1.1107 -                                                            hidMatchDictionary,
  1.1108 -                                                            JoystickDeviceWasAddedCallback,
  1.1109 -                                                            NULL,
  1.1110 -                                                            &portIterator);
  1.1111 -    while (IOIteratorNext(portIterator)) {}; /* Run out the iterator or notifications won't start (you can also use it to iterate the available devices). */
  1.1112 -
  1.1113      return SDL_SYS_NumJoysticks();
  1.1114  }
  1.1115  
  1.1116 @@ -828,10 +510,10 @@
  1.1117      recDevice *device = gpDeviceList;
  1.1118      int nJoySticks = 0;
  1.1119  
  1.1120 -    while ( device )
  1.1121 -    {
  1.1122 -        if ( !device->removed )
  1.1123 +    while (device) {
  1.1124 +        if (!device->removed) {
  1.1125              nJoySticks++;
  1.1126 +        }
  1.1127          device = device->pNext;
  1.1128      }
  1.1129  
  1.1130 @@ -843,18 +525,16 @@
  1.1131  void
  1.1132  SDL_SYS_JoystickDetect()
  1.1133  {
  1.1134 -    if ( s_bDeviceAdded || s_bDeviceRemoved )
  1.1135 -    {
  1.1136 +    if (s_bDeviceAdded || s_bDeviceRemoved) {
  1.1137          recDevice *device = gpDeviceList;
  1.1138          s_bDeviceAdded = SDL_FALSE;
  1.1139          s_bDeviceRemoved = SDL_FALSE;
  1.1140          int device_index = 0;
  1.1141          /* send notifications */
  1.1142 -        while ( device )
  1.1143 -        {
  1.1144 -            if ( device->send_open_event )
  1.1145 -            {
  1.1146 +        while (device) {
  1.1147 +            if (device->send_open_event) {
  1.1148                  device->send_open_event = 0;
  1.1149 +/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
  1.1150  #if !SDL_EVENTS_DISABLED
  1.1151                  SDL_Event event;
  1.1152                  event.type = SDL_JOYDEVICEADDED;
  1.1153 @@ -870,43 +550,25 @@
  1.1154  
  1.1155              }
  1.1156  
  1.1157 -            if ( device->removed )
  1.1158 -            {
  1.1159 -                recDevice *removeDevice = device;
  1.1160 -                if ( gpDeviceList == removeDevice )
  1.1161 -                {
  1.1162 -                    device = device->pNext;
  1.1163 -                    gpDeviceList = device;
  1.1164 -                }
  1.1165 -                else
  1.1166 -                {
  1.1167 -                    device = gpDeviceList;
  1.1168 -                    while ( device->pNext != removeDevice )
  1.1169 -                    {
  1.1170 -                        device = device->pNext;
  1.1171 -                    }
  1.1172 +            if (device->removed) {
  1.1173 +                const int instance_id = device->instance_id;
  1.1174 +                device = FreeDevice(device);
  1.1175  
  1.1176 -                    device->pNext = removeDevice->pNext;
  1.1177 -                }
  1.1178 -
  1.1179 +/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
  1.1180  #if !SDL_EVENTS_DISABLED
  1.1181                  SDL_Event event;
  1.1182                  event.type = SDL_JOYDEVICEREMOVED;
  1.1183  
  1.1184                  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1.1185 -                    event.jdevice.which = removeDevice->instance_id;
  1.1186 +                    event.jdevice.which = instance_id;
  1.1187                      if ((SDL_EventOK == NULL)
  1.1188                          || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
  1.1189                          SDL_PushEvent(&event);
  1.1190                      }
  1.1191                  }
  1.1192 -
  1.1193 -                DisposePtr((Ptr) removeDevice);
  1.1194  #endif /* !SDL_EVENTS_DISABLED */
  1.1195  
  1.1196 -            }
  1.1197 -            else
  1.1198 -            {
  1.1199 +            } else {
  1.1200                  device = device->pNext;
  1.1201                  device_index++;
  1.1202              }
  1.1203 @@ -926,8 +588,9 @@
  1.1204  {
  1.1205      recDevice *device = gpDeviceList;
  1.1206  
  1.1207 -    for (; device_index > 0; device_index--)
  1.1208 +    while (device_index-- > 0) {
  1.1209          device = device->pNext;
  1.1210 +    }
  1.1211  
  1.1212      return device->product;
  1.1213  }
  1.1214 @@ -940,8 +603,9 @@
  1.1215      recDevice *device = gpDeviceList;
  1.1216      int index;
  1.1217  
  1.1218 -    for (index = device_index; index > 0; index--)
  1.1219 +    for (index = device_index; index > 0; index--) {
  1.1220          device = device->pNext;
  1.1221 +    }
  1.1222  
  1.1223      return device->instance_id;
  1.1224  }
  1.1225 @@ -957,8 +621,9 @@
  1.1226      recDevice *device = gpDeviceList;
  1.1227      int index;
  1.1228  
  1.1229 -    for (index = device_index; index > 0; index--)
  1.1230 +    for (index = device_index; index > 0; index--) {
  1.1231          device = device->pNext;
  1.1232 +    }
  1.1233  
  1.1234      joystick->instance_id = device->instance_id;
  1.1235      joystick->hwdata = device;
  1.1236 @@ -979,11 +644,10 @@
  1.1237  {
  1.1238      recDevice *device = gpDeviceList;
  1.1239  
  1.1240 -    while ( device )
  1.1241 -    {
  1.1242 -        if ( joystick->instance_id == device->instance_id )
  1.1243 +    while (device) {
  1.1244 +        if (joystick->instance_id == device->instance_id) {
  1.1245              return SDL_TRUE;
  1.1246 -
  1.1247 +        }
  1.1248          device = device->pNext;
  1.1249      }
  1.1250  
  1.1251 @@ -1003,53 +667,24 @@
  1.1252      SInt32 value, range;
  1.1253      int i;
  1.1254  
  1.1255 -    if ( !device )
  1.1256 +    if (!device) {
  1.1257          return;
  1.1258 +    }
  1.1259  
  1.1260      if (device->removed) {      /* device was unplugged; ignore it. */
  1.1261 -        recDevice *devicelist = gpDeviceList;
  1.1262          joystick->closed = 1;
  1.1263          joystick->uncentered = 1;
  1.1264 -
  1.1265 -        if ( devicelist == device )
  1.1266 -        {
  1.1267 -            gpDeviceList = device->pNext;
  1.1268 -        }
  1.1269 -        else
  1.1270 -        {
  1.1271 -            while ( devicelist->pNext != device )
  1.1272 -            {
  1.1273 -                devicelist = devicelist->pNext;
  1.1274 -            }
  1.1275 -
  1.1276 -            devicelist->pNext = device->pNext;
  1.1277 -        }
  1.1278 -
  1.1279 -        DisposePtr((Ptr) device);
  1.1280          joystick->hwdata = NULL;
  1.1281 -
  1.1282 -#if !SDL_EVENTS_DISABLED
  1.1283 -        SDL_Event event;
  1.1284 -        event.type = SDL_JOYDEVICEREMOVED;
  1.1285 -
  1.1286 -        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
  1.1287 -            event.jdevice.which = joystick->instance_id;
  1.1288 -            if ((SDL_EventOK == NULL)
  1.1289 -                || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
  1.1290 -                SDL_PushEvent(&event);
  1.1291 -            }
  1.1292 -        }
  1.1293 -#endif /* !SDL_EVENTS_DISABLED */
  1.1294 -
  1.1295          return;
  1.1296      }
  1.1297  
  1.1298      element = device->firstAxis;
  1.1299      i = 0;
  1.1300      while (element) {
  1.1301 -        value = HIDScaledCalibratedValue(device, element, -32768, 32767);
  1.1302 -        if (value != joystick->axes[i])
  1.1303 +        value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
  1.1304 +        if (value != joystick->axes[i]) {
  1.1305              SDL_PrivateJoystickAxis(joystick, i, value);
  1.1306 +        }
  1.1307          element = element->pNext;
  1.1308          ++i;
  1.1309      }
  1.1310 @@ -1057,11 +692,13 @@
  1.1311      element = device->firstButton;
  1.1312      i = 0;
  1.1313      while (element) {
  1.1314 -        value = HIDGetElementValue(device, element);
  1.1315 -        if (value > 1)          /* handle pressure-sensitive buttons */
  1.1316 +        value = GetHIDElementState(device, element);
  1.1317 +        if (value > 1) {          /* handle pressure-sensitive buttons */
  1.1318              value = 1;
  1.1319 -        if (value != joystick->buttons[i])
  1.1320 +        }
  1.1321 +        if (value != joystick->buttons[i]) {
  1.1322              SDL_PrivateJoystickButton(joystick, i, value);
  1.1323 +        }
  1.1324          element = element->pNext;
  1.1325          ++i;
  1.1326      }
  1.1327 @@ -1072,11 +709,12 @@
  1.1328          Uint8 pos = 0;
  1.1329  
  1.1330          range = (element->max - element->min + 1);
  1.1331 -        value = HIDGetElementValue(device, element) - element->min;
  1.1332 -        if (range == 4)         /* 4 position hatswitch - scale up value */
  1.1333 +        value = GetHIDElementState(device, element) - element->min;
  1.1334 +        if (range == 4) {         /* 4 position hatswitch - scale up value */
  1.1335              value *= 2;
  1.1336 -        else if (range != 8)    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
  1.1337 +        } else if (range != 8) {    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
  1.1338              value = -1;
  1.1339 +        }
  1.1340          switch (value) {
  1.1341          case 0:
  1.1342              pos = SDL_HAT_UP;
  1.1343 @@ -1110,13 +748,14 @@
  1.1344              pos = SDL_HAT_CENTERED;
  1.1345              break;
  1.1346          }
  1.1347 -        if (pos != joystick->hats[i])
  1.1348 +
  1.1349 +        if (pos != joystick->hats[i]) {
  1.1350              SDL_PrivateJoystickHat(joystick, i, pos);
  1.1351 +        }
  1.1352 +
  1.1353          element = element->pNext;
  1.1354          ++i;
  1.1355      }
  1.1356 -
  1.1357 -    return;
  1.1358  }
  1.1359  
  1.1360  /* Function to close a joystick after use */
  1.1361 @@ -1130,14 +769,17 @@
  1.1362  void
  1.1363  SDL_SYS_JoystickQuit(void)
  1.1364  {
  1.1365 -    while (NULL != gpDeviceList)
  1.1366 -        gpDeviceList = HIDDisposeDevice(&gpDeviceList);
  1.1367 +    while (FreeDevice(gpDeviceList)) {
  1.1368 +        /* spin */
  1.1369 +    }
  1.1370  
  1.1371 -    if ( notificationPort )
  1.1372 -    {
  1.1373 -        IONotificationPortDestroy( notificationPort );
  1.1374 -        notificationPort = 0;
  1.1375 +    if (hidman) {
  1.1376 +        IOHIDManagerClose(hidman, kIOHIDOptionsTypeNone);
  1.1377 +        CFRelease(hidman);
  1.1378 +        hidman = NULL;
  1.1379      }
  1.1380 +
  1.1381 +    s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
  1.1382  }
  1.1383  
  1.1384  
  1.1385 @@ -1146,8 +788,9 @@
  1.1386      recDevice *device = gpDeviceList;
  1.1387      int index;
  1.1388  
  1.1389 -    for (index = device_index; index > 0; index--)
  1.1390 +    for (index = device_index; index > 0; index--) {
  1.1391          device = device->pNext;
  1.1392 +    }
  1.1393  
  1.1394      return device->guid;
  1.1395  }
     2.1 --- a/src/joystick/darwin/SDL_sysjoystick_c.h	Fri Feb 21 08:37:57 2014 -0300
     2.2 +++ b/src/joystick/darwin/SDL_sysjoystick_c.h	Sat Feb 22 00:55:28 2014 -0500
     2.3 @@ -22,34 +22,18 @@
     2.4  
     2.5  #ifndef SDL_JOYSTICK_IOKIT_H
     2.6  
     2.7 -
     2.8  #include <IOKit/hid/IOHIDLib.h>
     2.9 -#include <IOKit/hid/IOHIDKeys.h>
    2.10 -#include <IOKit/IOKitLib.h>
    2.11 -
    2.12  
    2.13  struct recElement
    2.14  {
    2.15 -    IOHIDElementCookie cookie;  /* unique value which identifies element, will NOT change */
    2.16 -    long usagePage, usage;      /* HID usage */
    2.17 -    long min;                   /* reported min value possible */
    2.18 -    long max;                   /* reported max value possible */
    2.19 -#if 0
    2.20 -    /* TODO: maybe should handle the following stuff somehow? */
    2.21 -
    2.22 -    long scaledMin;             /* reported scaled min value possible */
    2.23 -    long scaledMax;             /* reported scaled max value possible */
    2.24 -    long size;                  /* size in bits of data return from element */
    2.25 -    Boolean relative;           /* are reports relative to last report (deltas) */
    2.26 -    Boolean wrapping;           /* does element wrap around (one value higher than max is min) */
    2.27 -    Boolean nonLinear;          /* are the values reported non-linear relative to element movement */
    2.28 -    Boolean preferredState;     /* does element have a preferred state (such as a button) */
    2.29 -    Boolean nullState;          /* does element have null state */
    2.30 -#endif                          /* 0 */
    2.31 +    IOHIDElementRef elementRef;
    2.32 +    uint32_t usagePage, usage;      /* HID usage */
    2.33 +    SInt32 min;                   /* reported min value possible */
    2.34 +    SInt32 max;                   /* reported max value possible */
    2.35  
    2.36      /* runtime variables used for auto-calibration */
    2.37 -    long minReport;             /* min returned value */
    2.38 -    long maxReport;             /* max returned value */
    2.39 +    SInt32 minReport;             /* min returned value */
    2.40 +    SInt32 maxReport;             /* max returned value */
    2.41  
    2.42      struct recElement *pNext;   /* next element in list */
    2.43  };
    2.44 @@ -57,19 +41,17 @@
    2.45  
    2.46  struct joystick_hwdata
    2.47  {
    2.48 +    IOHIDDeviceRef deviceRef;   /* HIDManager device handle */
    2.49      io_service_t ffservice;     /* Interface for force feedback, 0 = no ff */
    2.50 -    IOHIDDeviceInterface **interface;   /* interface to device, NULL = no interface */
    2.51 -    IONotificationPortRef notificationPort; /* port to be notified on joystick removal */
    2.52 -    io_iterator_t portIterator; /* iterator for removal callback */
    2.53  
    2.54      char product[256];          /* name of product */
    2.55 -    long usage;                 /* usage page from IOUSBHID Parser.h which defines general usage */
    2.56 -    long usagePage;             /* usage within above page from IOUSBHID Parser.h which defines specific usage */
    2.57 +    uint32_t usage;                 /* usage page from IOUSBHID Parser.h which defines general usage */
    2.58 +    uint32_t usagePage;             /* usage within above page from IOUSBHID Parser.h which defines specific usage */
    2.59  
    2.60 -    long axes;                  /* number of axis (calculated, not reported by device) */
    2.61 -    long buttons;               /* number of buttons (calculated, not reported by device) */
    2.62 -    long hats;                  /* number of hat switches (calculated, not reported by device) */
    2.63 -    long elements;              /* number of total elements (should be total of above) (calculated, not reported by device) */
    2.64 +    int axes;                  /* number of axis (calculated, not reported by device) */
    2.65 +    int buttons;               /* number of buttons (calculated, not reported by device) */
    2.66 +    int hats;                  /* number of hat switches (calculated, not reported by device) */
    2.67 +    int elements;              /* number of total elements (should be total of above) (calculated, not reported by device) */
    2.68  
    2.69      recElement *firstAxis;
    2.70      recElement *firstButton;