src/joystick/darwin/SDL_sysjoystick.c
changeset 1895 c121d94672cb
parent 1635 92947e3a18db
child 2097 b0048df1701a
     1.1 --- a/src/joystick/darwin/SDL_sysjoystick.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/joystick/darwin/SDL_sysjoystick.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -42,7 +42,7 @@
     1.4  #include <IOKit/hid/IOHIDLib.h>
     1.5  #include <IOKit/hid/IOHIDKeys.h>
     1.6  #include <CoreFoundation/CoreFoundation.h>
     1.7 -#include <Carbon/Carbon.h> /* for NewPtrClear, DisposePtr */
     1.8 +#include <Carbon/Carbon.h>      /* for NewPtrClear, DisposePtr */
     1.9  
    1.10  #include "SDL_joystick.h"
    1.11  #include "../SDL_sysjoystick.h"
    1.12 @@ -50,51 +50,51 @@
    1.13  
    1.14  struct recElement
    1.15  {
    1.16 -	IOHIDElementCookie cookie;				/* unique value which identifies element, will NOT change */
    1.17 -	long min;								/* reported min value possible */
    1.18 -	long max;								/* reported max value possible */
    1.19 +    IOHIDElementCookie cookie;  /* unique value which identifies element, will NOT change */
    1.20 +    long min;                   /* reported min value possible */
    1.21 +    long max;                   /* reported max value possible */
    1.22  #if 0
    1.23 -	/* TODO: maybe should handle the following stuff somehow? */
    1.24 +    /* TODO: maybe should handle the following stuff somehow? */
    1.25  
    1.26 -	long scaledMin;							/* reported scaled min value possible */
    1.27 -	long scaledMax;							/* reported scaled max value possible */
    1.28 -	long size;								/* size in bits of data return from element */
    1.29 -	Boolean relative;						/* are reports relative to last report (deltas) */
    1.30 -	Boolean wrapping;						/* does element wrap around (one value higher than max is min) */
    1.31 -	Boolean nonLinear;						/* are the values reported non-linear relative to element movement */
    1.32 -	Boolean preferredState;					/* does element have a preferred state (such as a button) */
    1.33 -	Boolean nullState;						/* does element have null state */
    1.34 -#endif /* 0 */
    1.35 +    long scaledMin;             /* reported scaled min value possible */
    1.36 +    long scaledMax;             /* reported scaled max value possible */
    1.37 +    long size;                  /* size in bits of data return from element */
    1.38 +    Boolean relative;           /* are reports relative to last report (deltas) */
    1.39 +    Boolean wrapping;           /* does element wrap around (one value higher than max is min) */
    1.40 +    Boolean nonLinear;          /* are the values reported non-linear relative to element movement */
    1.41 +    Boolean preferredState;     /* does element have a preferred state (such as a button) */
    1.42 +    Boolean nullState;          /* does element have null state */
    1.43 +#endif                          /* 0 */
    1.44  
    1.45 -	/* runtime variables used for auto-calibration */
    1.46 -	long minReport;							/* min returned value */
    1.47 -	long maxReport;							/* max returned value */
    1.48 -	
    1.49 -	struct recElement * pNext;				/* next element in list */
    1.50 +    /* runtime variables used for auto-calibration */
    1.51 +    long minReport;             /* min returned value */
    1.52 +    long maxReport;             /* max returned value */
    1.53 +
    1.54 +    struct recElement *pNext;   /* next element in list */
    1.55  };
    1.56  typedef struct recElement recElement;
    1.57  
    1.58  struct joystick_hwdata
    1.59  {
    1.60 -	IOHIDDeviceInterface ** interface;		/* interface to device, NULL = no interface */
    1.61 +    IOHIDDeviceInterface **interface;   /* interface to device, NULL = no interface */
    1.62  
    1.63 -	char product[256];							/* name of product */
    1.64 -	long usage;								/* usage page from IOUSBHID Parser.h which defines general usage */
    1.65 -	long usagePage;							/* usage within above page from IOUSBHID Parser.h which defines specific usage */
    1.66 +    char product[256];          /* name of product */
    1.67 +    long usage;                 /* usage page from IOUSBHID Parser.h which defines general usage */
    1.68 +    long usagePage;             /* usage within above page from IOUSBHID Parser.h which defines specific usage */
    1.69  
    1.70 -	long axes;								/* number of axis (calculated, not reported by device) */
    1.71 -	long buttons;							/* number of buttons (calculated, not reported by device) */
    1.72 -	long hats;								/* number of hat switches (calculated, not reported by device) */
    1.73 -	long elements;							/* number of total elements (shouldbe total of above) (calculated, not reported by device) */
    1.74 +    long axes;                  /* number of axis (calculated, not reported by device) */
    1.75 +    long buttons;               /* number of buttons (calculated, not reported by device) */
    1.76 +    long hats;                  /* number of hat switches (calculated, not reported by device) */
    1.77 +    long elements;              /* number of total elements (shouldbe total of above) (calculated, not reported by device) */
    1.78  
    1.79 -	recElement* firstAxis;
    1.80 -	recElement* firstButton;
    1.81 -	recElement* firstHat;
    1.82 +    recElement *firstAxis;
    1.83 +    recElement *firstButton;
    1.84 +    recElement *firstHat;
    1.85  
    1.86 -	int removed;
    1.87 -	int uncentered;
    1.88 +    int removed;
    1.89 +    int uncentered;
    1.90  
    1.91 -	struct joystick_hwdata* pNext;			/* next device */
    1.92 +    struct joystick_hwdata *pNext;      /* next device */
    1.93  };
    1.94  typedef struct joystick_hwdata recDevice;
    1.95  
    1.96 @@ -103,60 +103,65 @@
    1.97  static recDevice *gpDeviceList = NULL;
    1.98  
    1.99  
   1.100 -static void HIDReportErrorNum (char * strError, long numError)
   1.101 +static void
   1.102 +HIDReportErrorNum(char *strError, long numError)
   1.103  {
   1.104 -	SDL_SetError(strError);
   1.105 +    SDL_SetError(strError);
   1.106  }
   1.107  
   1.108 -static void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, recDevice *pDevice);
   1.109 +static void HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
   1.110 +                                     recDevice * pDevice);
   1.111  
   1.112  /* returns current value for element, polling element
   1.113   * will return 0 on error conditions which should be accounted for by application
   1.114   */
   1.115  
   1.116 -static SInt32 HIDGetElementValue (recDevice *pDevice, recElement *pElement)
   1.117 +static SInt32
   1.118 +HIDGetElementValue(recDevice * pDevice, recElement * pElement)
   1.119  {
   1.120 -	IOReturn result = kIOReturnSuccess;
   1.121 -	IOHIDEventStruct hidEvent;
   1.122 -	hidEvent.value = 0;
   1.123 -	
   1.124 -	if (NULL != pDevice && NULL != pElement && NULL != pDevice->interface)
   1.125 -	{
   1.126 -		result = (*(pDevice->interface))->getElementValue(pDevice->interface, pElement->cookie, &hidEvent);
   1.127 -		if (kIOReturnSuccess == result)
   1.128 -		{
   1.129 -			/* record min and max for auto calibration */
   1.130 -			if (hidEvent.value < pElement->minReport)
   1.131 -				pElement->minReport = hidEvent.value;
   1.132 -			if (hidEvent.value > pElement->maxReport)
   1.133 -				pElement->maxReport = hidEvent.value;
   1.134 -		}
   1.135 -	}
   1.136 +    IOReturn result = kIOReturnSuccess;
   1.137 +    IOHIDEventStruct hidEvent;
   1.138 +    hidEvent.value = 0;
   1.139  
   1.140 -	/* auto user scale */
   1.141 -	return hidEvent.value;
   1.142 +    if (NULL != pDevice && NULL != pElement && NULL != pDevice->interface) {
   1.143 +        result =
   1.144 +            (*(pDevice->interface))->getElementValue(pDevice->interface,
   1.145 +                                                     pElement->cookie,
   1.146 +                                                     &hidEvent);
   1.147 +        if (kIOReturnSuccess == result) {
   1.148 +            /* record min and max for auto calibration */
   1.149 +            if (hidEvent.value < pElement->minReport)
   1.150 +                pElement->minReport = hidEvent.value;
   1.151 +            if (hidEvent.value > pElement->maxReport)
   1.152 +                pElement->maxReport = hidEvent.value;
   1.153 +        }
   1.154 +    }
   1.155 +
   1.156 +    /* auto user scale */
   1.157 +    return hidEvent.value;
   1.158  }
   1.159  
   1.160 -static SInt32 HIDScaledCalibratedValue (recDevice *pDevice, recElement *pElement, long min, long max)
   1.161 +static SInt32
   1.162 +HIDScaledCalibratedValue(recDevice * pDevice, recElement * pElement,
   1.163 +                         long min, long max)
   1.164  {
   1.165 -	float deviceScale = max - min;
   1.166 -	float readScale = pElement->maxReport - pElement->minReport;
   1.167 -	SInt32 value = HIDGetElementValue(pDevice, pElement);
   1.168 -	if (readScale == 0)
   1.169 -		return value; /* no scaling at all */
   1.170 -	else
   1.171 -		return ((value - pElement->minReport) * deviceScale / readScale) + min;
   1.172 +    float deviceScale = max - min;
   1.173 +    float readScale = pElement->maxReport - pElement->minReport;
   1.174 +    SInt32 value = HIDGetElementValue(pDevice, pElement);
   1.175 +    if (readScale == 0)
   1.176 +        return value;           /* no scaling at all */
   1.177 +    else
   1.178 +        return ((value - pElement->minReport) * deviceScale / readScale) +
   1.179 +            min;
   1.180  }
   1.181  
   1.182  
   1.183 -static void HIDRemovalCallback(void * target,
   1.184 -                               IOReturn result,
   1.185 -                               void * refcon,
   1.186 -                               void * sender)
   1.187 +static void
   1.188 +HIDRemovalCallback(void *target, IOReturn result, void *refcon, void *sender)
   1.189  {
   1.190 -	recDevice *device = (recDevice *) refcon;
   1.191 -	device->removed = 1;
   1.192 -	device->uncentered = 1;
   1.193 +    recDevice *device = (recDevice *) refcon;
   1.194 +    device->removed = 1;
   1.195 +    device->uncentered = 1;
   1.196  }
   1.197  
   1.198  
   1.199 @@ -165,39 +170,50 @@
   1.200   * Note: appliction now owns the device and must close and release it prior to exiting
   1.201   */
   1.202  
   1.203 -static IOReturn HIDCreateOpenDeviceInterface (io_object_t hidDevice, recDevice *pDevice)
   1.204 +static IOReturn
   1.205 +HIDCreateOpenDeviceInterface(io_object_t hidDevice, recDevice * pDevice)
   1.206  {
   1.207 -	IOReturn result = kIOReturnSuccess;
   1.208 -	HRESULT plugInResult = S_OK;
   1.209 -	SInt32 score = 0;
   1.210 -	IOCFPlugInInterface ** ppPlugInInterface = NULL;
   1.211 -	
   1.212 -	if (NULL == pDevice->interface)
   1.213 -	{
   1.214 -		result = IOCreatePlugInInterfaceForService (hidDevice, kIOHIDDeviceUserClientTypeID,
   1.215 -													kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
   1.216 -		if (kIOReturnSuccess == result)
   1.217 -		{
   1.218 -			/* Call a method of the intermediate plug-in to create the device interface */
   1.219 -			plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
   1.220 -								CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void *) &(pDevice->interface));
   1.221 -			if (S_OK != plugInResult)
   1.222 -				HIDReportErrorNum ("CouldnŐt query HID class device interface from plugInInterface", plugInResult);
   1.223 -			(*ppPlugInInterface)->Release (ppPlugInInterface);
   1.224 -		}
   1.225 -		else
   1.226 -			HIDReportErrorNum ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.", result);
   1.227 -	}
   1.228 -	if (NULL != pDevice->interface)
   1.229 -	{
   1.230 -		result = (*(pDevice->interface))->open (pDevice->interface, 0);
   1.231 -		if (kIOReturnSuccess != result)
   1.232 -			HIDReportErrorNum ("Failed to open pDevice->interface via open.", result);
   1.233 -		else
   1.234 -			(*(pDevice->interface))->setRemovalCallback (pDevice->interface, HIDRemovalCallback, pDevice, pDevice);
   1.235 +    IOReturn result = kIOReturnSuccess;
   1.236 +    HRESULT plugInResult = S_OK;
   1.237 +    SInt32 score = 0;
   1.238 +    IOCFPlugInInterface **ppPlugInInterface = NULL;
   1.239  
   1.240 -	}
   1.241 -	return result;
   1.242 +    if (NULL == pDevice->interface) {
   1.243 +        result =
   1.244 +            IOCreatePlugInInterfaceForService(hidDevice,
   1.245 +                                              kIOHIDDeviceUserClientTypeID,
   1.246 +                                              kIOCFPlugInInterfaceID,
   1.247 +                                              &ppPlugInInterface, &score);
   1.248 +        if (kIOReturnSuccess == result) {
   1.249 +            /* Call a method of the intermediate plug-in to create the device interface */
   1.250 +            plugInResult =
   1.251 +                (*ppPlugInInterface)->QueryInterface(ppPlugInInterface,
   1.252 +                                                     CFUUIDGetUUIDBytes
   1.253 +                                                     (kIOHIDDeviceInterfaceID),
   1.254 +                                                     (void *) &(pDevice->
   1.255 +                                                                interface));
   1.256 +            if (S_OK != plugInResult)
   1.257 +                HIDReportErrorNum
   1.258 +                    ("CouldnŐt query HID class device interface from plugInInterface",
   1.259 +                     plugInResult);
   1.260 +            (*ppPlugInInterface)->Release(ppPlugInInterface);
   1.261 +        } else
   1.262 +            HIDReportErrorNum
   1.263 +                ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.",
   1.264 +                 result);
   1.265 +    }
   1.266 +    if (NULL != pDevice->interface) {
   1.267 +        result = (*(pDevice->interface))->open(pDevice->interface, 0);
   1.268 +        if (kIOReturnSuccess != result)
   1.269 +            HIDReportErrorNum
   1.270 +                ("Failed to open pDevice->interface via open.", result);
   1.271 +        else
   1.272 +            (*(pDevice->interface))->setRemovalCallback(pDevice->interface,
   1.273 +                                                        HIDRemovalCallback,
   1.274 +                                                        pDevice, pDevice);
   1.275 +
   1.276 +    }
   1.277 +    return result;
   1.278  }
   1.279  
   1.280  /* Closes and releases interface to device, should be done prior to exting application
   1.281 @@ -206,47 +222,48 @@
   1.282   * (device may have to be plug and re-plugged in different location to get it working again without a restart)
   1.283   */
   1.284  
   1.285 -static IOReturn HIDCloseReleaseInterface (recDevice *pDevice)
   1.286 +static IOReturn
   1.287 +HIDCloseReleaseInterface(recDevice * pDevice)
   1.288  {
   1.289 -	IOReturn result = kIOReturnSuccess;
   1.290 -	
   1.291 -	if ((NULL != pDevice) && (NULL != pDevice->interface))
   1.292 -	{
   1.293 -		/* close the interface */
   1.294 -		result = (*(pDevice->interface))->close (pDevice->interface);
   1.295 -		if (kIOReturnNotOpen == result)
   1.296 -		{
   1.297 -			/* do nothing as device was not opened, thus can't be closed */
   1.298 -		}
   1.299 -		else if (kIOReturnSuccess != result)
   1.300 -			HIDReportErrorNum ("Failed to close IOHIDDeviceInterface.", result);
   1.301 -		/* release the interface */
   1.302 -		result = (*(pDevice->interface))->Release (pDevice->interface);
   1.303 -		if (kIOReturnSuccess != result)
   1.304 -			HIDReportErrorNum ("Failed to release IOHIDDeviceInterface.", result);
   1.305 -		pDevice->interface = NULL;
   1.306 -	}	
   1.307 -	return result;
   1.308 +    IOReturn result = kIOReturnSuccess;
   1.309 +
   1.310 +    if ((NULL != pDevice) && (NULL != pDevice->interface)) {
   1.311 +        /* close the interface */
   1.312 +        result = (*(pDevice->interface))->close(pDevice->interface);
   1.313 +        if (kIOReturnNotOpen == result) {
   1.314 +            /* do nothing as device was not opened, thus can't be closed */
   1.315 +        } else if (kIOReturnSuccess != result)
   1.316 +            HIDReportErrorNum("Failed to close IOHIDDeviceInterface.",
   1.317 +                              result);
   1.318 +        /* release the interface */
   1.319 +        result = (*(pDevice->interface))->Release(pDevice->interface);
   1.320 +        if (kIOReturnSuccess != result)
   1.321 +            HIDReportErrorNum("Failed to release IOHIDDeviceInterface.",
   1.322 +                              result);
   1.323 +        pDevice->interface = NULL;
   1.324 +    }
   1.325 +    return result;
   1.326  }
   1.327  
   1.328  /* extracts actual specific element information from each element CF dictionary entry */
   1.329  
   1.330 -static void HIDGetElementInfo (CFTypeRef refElement, recElement *pElement)
   1.331 +static void
   1.332 +HIDGetElementInfo(CFTypeRef refElement, recElement * pElement)
   1.333  {
   1.334 -	long number;
   1.335 -	CFTypeRef refType;
   1.336 +    long number;
   1.337 +    CFTypeRef refType;
   1.338  
   1.339 -	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementCookieKey));
   1.340 -	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.341 -		pElement->cookie = (IOHIDElementCookie) number;
   1.342 -	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMinKey));
   1.343 -	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.344 -		pElement->min = number;
   1.345 -		pElement->maxReport = pElement->min;
   1.346 -	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMaxKey));
   1.347 -	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
   1.348 -		pElement->max = number;
   1.349 -		pElement->minReport = pElement->max;
   1.350 +    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementCookieKey));
   1.351 +    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.352 +        pElement->cookie = (IOHIDElementCookie) number;
   1.353 +    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMinKey));
   1.354 +    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.355 +        pElement->min = number;
   1.356 +    pElement->maxReport = pElement->min;
   1.357 +    refType = CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementMaxKey));
   1.358 +    if (refType && CFNumberGetValue(refType, kCFNumberLongType, &number))
   1.359 +        pElement->max = number;
   1.360 +    pElement->minReport = pElement->max;
   1.361  /*
   1.362  	TODO: maybe should handle the following stuff somehow?
   1.363  
   1.364 @@ -275,292 +292,319 @@
   1.365  	if (refType)
   1.366  		pElement->nullState = CFBooleanGetValue (refType);
   1.367  */
   1.368 -}			
   1.369 +}
   1.370  
   1.371  /* examines CF dictionary vlaue in device element hierarchy to determine if it is element of interest or a collection of more elements
   1.372   * if element of interest allocate storage, add to list and retrieve element specific info
   1.373   * if collection then pass on to deconstruction collection into additional individual elements
   1.374   */
   1.375  
   1.376 -static void HIDAddElement (CFTypeRef refElement, recDevice* pDevice)
   1.377 +static void
   1.378 +HIDAddElement(CFTypeRef refElement, recDevice * pDevice)
   1.379  {
   1.380 -	recElement* element = NULL;
   1.381 -	recElement** headElement = NULL;
   1.382 -	long elementType, usagePage, usage;
   1.383 -	CFTypeRef refElementType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementTypeKey));
   1.384 -	CFTypeRef refUsagePage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsagePageKey));
   1.385 -	CFTypeRef refUsage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsageKey));
   1.386 +    recElement *element = NULL;
   1.387 +    recElement **headElement = NULL;
   1.388 +    long elementType, usagePage, usage;
   1.389 +    CFTypeRef refElementType =
   1.390 +        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
   1.391 +    CFTypeRef refUsagePage =
   1.392 +        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
   1.393 +    CFTypeRef refUsage =
   1.394 +        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
   1.395  
   1.396  
   1.397 -	if ((refElementType) && (CFNumberGetValue (refElementType, kCFNumberLongType, &elementType)))
   1.398 -	{
   1.399 -		/* look at types of interest */
   1.400 -		if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
   1.401 -			(elementType == kIOHIDElementTypeInput_Axis))
   1.402 -		{
   1.403 -			if (refUsagePage && CFNumberGetValue (refUsagePage, kCFNumberLongType, &usagePage) &&
   1.404 -				refUsage && CFNumberGetValue (refUsage, kCFNumberLongType, &usage))
   1.405 -			{
   1.406 -				switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
   1.407 -				{
   1.408 -					case kHIDPage_GenericDesktop:
   1.409 -						{
   1.410 -							switch (usage) /* look at usage to determine function */
   1.411 -							{
   1.412 -								case kHIDUsage_GD_X:
   1.413 -								case kHIDUsage_GD_Y:
   1.414 -								case kHIDUsage_GD_Z:
   1.415 -								case kHIDUsage_GD_Rx:
   1.416 -								case kHIDUsage_GD_Ry:
   1.417 -								case kHIDUsage_GD_Rz:
   1.418 -								case kHIDUsage_GD_Slider:
   1.419 -								case kHIDUsage_GD_Dial:
   1.420 -								case kHIDUsage_GD_Wheel:
   1.421 -									element = (recElement *) NewPtrClear (sizeof (recElement));
   1.422 -									if (element)
   1.423 -									{
   1.424 -										pDevice->axes++;
   1.425 -										headElement = &(pDevice->firstAxis);
   1.426 -									}
   1.427 -								break;
   1.428 -								case kHIDUsage_GD_Hatswitch:
   1.429 -									element = (recElement *) NewPtrClear (sizeof (recElement));
   1.430 -									if (element)
   1.431 -									{
   1.432 -										pDevice->hats++;
   1.433 -										headElement = &(pDevice->firstHat);
   1.434 -									}
   1.435 -								break;
   1.436 -							}							
   1.437 -						}
   1.438 -						break;
   1.439 -					case kHIDPage_Button:
   1.440 -						element = (recElement *) NewPtrClear (sizeof (recElement));
   1.441 -						if (element)
   1.442 -						{
   1.443 -							pDevice->buttons++;
   1.444 -							headElement = &(pDevice->firstButton);
   1.445 -						}
   1.446 -						break;
   1.447 -					default:
   1.448 -						break;
   1.449 -				}
   1.450 -			}
   1.451 -		}
   1.452 -		else if (kIOHIDElementTypeCollection == elementType)
   1.453 -			HIDGetCollectionElements ((CFMutableDictionaryRef) refElement, pDevice);
   1.454 -	}
   1.455 +    if ((refElementType)
   1.456 +        &&
   1.457 +        (CFNumberGetValue(refElementType, kCFNumberLongType, &elementType))) {
   1.458 +        /* look at types of interest */
   1.459 +        if ((elementType == kIOHIDElementTypeInput_Misc)
   1.460 +            || (elementType == kIOHIDElementTypeInput_Button)
   1.461 +            || (elementType == kIOHIDElementTypeInput_Axis)) {
   1.462 +            if (refUsagePage
   1.463 +                && CFNumberGetValue(refUsagePage, kCFNumberLongType,
   1.464 +                                    &usagePage) && refUsage
   1.465 +                && CFNumberGetValue(refUsage, kCFNumberLongType, &usage)) {
   1.466 +                switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
   1.467 +                case kHIDPage_GenericDesktop:
   1.468 +                    {
   1.469 +                        switch (usage) {        /* look at usage to determine function */
   1.470 +                        case kHIDUsage_GD_X:
   1.471 +                        case kHIDUsage_GD_Y:
   1.472 +                        case kHIDUsage_GD_Z:
   1.473 +                        case kHIDUsage_GD_Rx:
   1.474 +                        case kHIDUsage_GD_Ry:
   1.475 +                        case kHIDUsage_GD_Rz:
   1.476 +                        case kHIDUsage_GD_Slider:
   1.477 +                        case kHIDUsage_GD_Dial:
   1.478 +                        case kHIDUsage_GD_Wheel:
   1.479 +                            element = (recElement *)
   1.480 +                                NewPtrClear(sizeof(recElement));
   1.481 +                            if (element) {
   1.482 +                                pDevice->axes++;
   1.483 +                                headElement = &(pDevice->firstAxis);
   1.484 +                            }
   1.485 +                            break;
   1.486 +                        case kHIDUsage_GD_Hatswitch:
   1.487 +                            element = (recElement *)
   1.488 +                                NewPtrClear(sizeof(recElement));
   1.489 +                            if (element) {
   1.490 +                                pDevice->hats++;
   1.491 +                                headElement = &(pDevice->firstHat);
   1.492 +                            }
   1.493 +                            break;
   1.494 +                        }
   1.495 +                    }
   1.496 +                    break;
   1.497 +                case kHIDPage_Button:
   1.498 +                    element = (recElement *)
   1.499 +                        NewPtrClear(sizeof(recElement));
   1.500 +                    if (element) {
   1.501 +                        pDevice->buttons++;
   1.502 +                        headElement = &(pDevice->firstButton);
   1.503 +                    }
   1.504 +                    break;
   1.505 +                default:
   1.506 +                    break;
   1.507 +                }
   1.508 +            }
   1.509 +        } else if (kIOHIDElementTypeCollection == elementType)
   1.510 +            HIDGetCollectionElements((CFMutableDictionaryRef) refElement,
   1.511 +                                     pDevice);
   1.512 +    }
   1.513  
   1.514 -	if (element && headElement) /* add to list */
   1.515 -	{
   1.516 -		pDevice->elements++;
   1.517 -		if (NULL == *headElement)
   1.518 -			*headElement = element;
   1.519 -		else
   1.520 -		{
   1.521 -			recElement *elementPrevious, *elementCurrent;
   1.522 -			elementCurrent = *headElement;
   1.523 -			while (elementCurrent)
   1.524 -			{
   1.525 -				elementPrevious = elementCurrent;
   1.526 -				elementCurrent = elementPrevious->pNext;
   1.527 -			}
   1.528 -			elementPrevious->pNext = element;
   1.529 -		}
   1.530 -		element->pNext = NULL;
   1.531 -		HIDGetElementInfo (refElement, element);
   1.532 -	}
   1.533 +    if (element && headElement) {       /* add to list */
   1.534 +        pDevice->elements++;
   1.535 +        if (NULL == *headElement)
   1.536 +            *headElement = element;
   1.537 +        else {
   1.538 +            recElement *elementPrevious, *elementCurrent;
   1.539 +            elementCurrent = *headElement;
   1.540 +            while (elementCurrent) {
   1.541 +                elementPrevious = elementCurrent;
   1.542 +                elementCurrent = elementPrevious->pNext;
   1.543 +            }
   1.544 +            elementPrevious->pNext = element;
   1.545 +        }
   1.546 +        element->pNext = NULL;
   1.547 +        HIDGetElementInfo(refElement, element);
   1.548 +    }
   1.549  }
   1.550  
   1.551  /* collects information from each array member in device element list (each array memeber = element) */
   1.552  
   1.553 -static void HIDGetElementsCFArrayHandler (const void * value, void * parameter)
   1.554 +static void
   1.555 +HIDGetElementsCFArrayHandler(const void *value, void *parameter)
   1.556  {
   1.557 -	if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
   1.558 -		HIDAddElement ((CFTypeRef) value, (recDevice *) parameter);
   1.559 +    if (CFGetTypeID(value) == CFDictionaryGetTypeID())
   1.560 +        HIDAddElement((CFTypeRef) value, (recDevice *) parameter);
   1.561  }
   1.562  
   1.563  /* handles retrieval of element information from arrays of elements in device IO registry information */
   1.564  
   1.565 -static void HIDGetElements (CFTypeRef refElementCurrent, recDevice *pDevice)
   1.566 +static void
   1.567 +HIDGetElements(CFTypeRef refElementCurrent, recDevice * pDevice)
   1.568  {
   1.569 -	CFTypeID type = CFGetTypeID (refElementCurrent);
   1.570 -	if (type == CFArrayGetTypeID()) /* if element is an array */
   1.571 -	{
   1.572 -		CFRange range = {0, CFArrayGetCount (refElementCurrent)};
   1.573 -		/* CountElementsCFArrayHandler called for each array member */
   1.574 -		CFArrayApplyFunction (refElementCurrent, range, HIDGetElementsCFArrayHandler, pDevice);
   1.575 -	}
   1.576 -}			
   1.577 +    CFTypeID type = CFGetTypeID(refElementCurrent);
   1.578 +    if (type == CFArrayGetTypeID()) {   /* if element is an array */
   1.579 +        CFRange range = { 0, CFArrayGetCount(refElementCurrent) };
   1.580 +        /* CountElementsCFArrayHandler called for each array member */
   1.581 +        CFArrayApplyFunction(refElementCurrent, range,
   1.582 +                             HIDGetElementsCFArrayHandler, pDevice);
   1.583 +    }
   1.584 +}
   1.585  
   1.586  /* handles extracting element information from element collection CF types
   1.587   * used from top level element decoding and hierarchy deconstruction to flatten device element list
   1.588   */
   1.589  
   1.590 -static void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, recDevice *pDevice)
   1.591 +static void
   1.592 +HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
   1.593 +                         recDevice * pDevice)
   1.594  {
   1.595 -	CFTypeRef refElementTop = CFDictionaryGetValue (deviceProperties, CFSTR(kIOHIDElementKey));
   1.596 -	if (refElementTop)
   1.597 -		HIDGetElements (refElementTop, pDevice);
   1.598 +    CFTypeRef refElementTop =
   1.599 +        CFDictionaryGetValue(deviceProperties, CFSTR(kIOHIDElementKey));
   1.600 +    if (refElementTop)
   1.601 +        HIDGetElements(refElementTop, pDevice);
   1.602  }
   1.603  
   1.604  /* use top level element usage page and usage to discern device usage page and usage setting appropriate vlaues in device record */
   1.605  
   1.606 -static void HIDTopLevelElementHandler (const void * value, void * parameter)
   1.607 +static void
   1.608 +HIDTopLevelElementHandler(const void *value, void *parameter)
   1.609  {
   1.610 -	CFTypeRef refCF = 0;
   1.611 -	if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
   1.612 -		return;
   1.613 -	refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsagePageKey));
   1.614 -	if (!CFNumberGetValue (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
   1.615 -		SDL_SetError ("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.616 -	refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsageKey));
   1.617 -	if (!CFNumberGetValue (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
   1.618 -		SDL_SetError ("CFNumberGetValue error retrieving pDevice->usage.");
   1.619 +    CFTypeRef refCF = 0;
   1.620 +    if (CFGetTypeID(value) != CFDictionaryGetTypeID())
   1.621 +        return;
   1.622 +    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsagePageKey));
   1.623 +    if (!CFNumberGetValue
   1.624 +        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
   1.625 +        SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.626 +    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsageKey));
   1.627 +    if (!CFNumberGetValue
   1.628 +        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
   1.629 +        SDL_SetError("CFNumberGetValue error retrieving pDevice->usage.");
   1.630  }
   1.631  
   1.632  /* extracts device info from CF dictionary records in IO registry */
   1.633  
   1.634 -static void HIDGetDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, recDevice *pDevice)
   1.635 +static void
   1.636 +HIDGetDeviceInfo(io_object_t hidDevice, CFMutableDictionaryRef hidProperties,
   1.637 +                 recDevice * pDevice)
   1.638  {
   1.639 -	CFMutableDictionaryRef usbProperties = 0;
   1.640 -	io_registry_entry_t parent1, parent2;
   1.641 -	
   1.642 -	/* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
   1.643 -	 * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
   1.644 -	 */
   1.645 -	if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
   1.646 -		(KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
   1.647 -		(KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
   1.648 -	{
   1.649 -		if (usbProperties)
   1.650 -		{
   1.651 -			CFTypeRef refCF = 0;
   1.652 -			/* get device info
   1.653 -			 * try hid dictionary first, if fail then go to usb dictionary
   1.654 -			 */
   1.655 -			
   1.656 -			
   1.657 -			/* get product name */
   1.658 -			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
   1.659 -			if (!refCF)
   1.660 -				refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
   1.661 -			if (refCF)
   1.662 -			{
   1.663 -				if (!CFStringGetCString (refCF, pDevice->product, 256, CFStringGetSystemEncoding ()))
   1.664 -					SDL_SetError ("CFStringGetCString error retrieving pDevice->product.");
   1.665 -			}
   1.666 -			
   1.667 -			/* get usage page and usage */
   1.668 -			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
   1.669 -			if (refCF)
   1.670 -			{
   1.671 -				if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage))
   1.672 -					SDL_SetError ("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.673 -				refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
   1.674 -				if (refCF)
   1.675 -					if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage))
   1.676 -						SDL_SetError ("CFNumberGetValue error retrieving pDevice->usage.");
   1.677 -			}
   1.678 +    CFMutableDictionaryRef usbProperties = 0;
   1.679 +    io_registry_entry_t parent1, parent2;
   1.680  
   1.681 -			if (NULL == refCF) /* get top level element HID usage page or usage */
   1.682 -			{
   1.683 -				/* use top level element instead */
   1.684 -				CFTypeRef refCFTopElement = 0;
   1.685 -				refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
   1.686 -				{
   1.687 -					/* refCFTopElement points to an array of element dictionaries */
   1.688 -					CFRange range = {0, CFArrayGetCount (refCFTopElement)};
   1.689 -					CFArrayApplyFunction (refCFTopElement, range, HIDTopLevelElementHandler, pDevice);
   1.690 -				}
   1.691 -			}
   1.692 +    /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
   1.693 +     * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
   1.694 +     */
   1.695 +    if ((KERN_SUCCESS ==
   1.696 +         IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent1))
   1.697 +        && (KERN_SUCCESS ==
   1.698 +            IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
   1.699 +        && (KERN_SUCCESS ==
   1.700 +            IORegistryEntryCreateCFProperties(parent2, &usbProperties,
   1.701 +                                              kCFAllocatorDefault,
   1.702 +                                              kNilOptions))) {
   1.703 +        if (usbProperties) {
   1.704 +            CFTypeRef refCF = 0;
   1.705 +            /* get device info
   1.706 +             * try hid dictionary first, if fail then go to usb dictionary
   1.707 +             */
   1.708  
   1.709 -			CFRelease (usbProperties);
   1.710 -		}
   1.711 -		else
   1.712 -			SDL_SetError ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
   1.713  
   1.714 -		if (kIOReturnSuccess != IOObjectRelease (parent2))
   1.715 -			SDL_SetError ("IOObjectRelease error with parent2.");
   1.716 -		if (kIOReturnSuccess != IOObjectRelease (parent1))
   1.717 -			SDL_SetError ("IOObjectRelease error with parent1.");
   1.718 -	}
   1.719 +            /* get product name */
   1.720 +            refCF =
   1.721 +                CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
   1.722 +            if (!refCF)
   1.723 +                refCF =
   1.724 +                    CFDictionaryGetValue(usbProperties,
   1.725 +                                         CFSTR("USB Product Name"));
   1.726 +            if (refCF) {
   1.727 +                if (!CFStringGetCString
   1.728 +                    (refCF, pDevice->product, 256,
   1.729 +                     CFStringGetSystemEncoding()))
   1.730 +                    SDL_SetError
   1.731 +                        ("CFStringGetCString error retrieving pDevice->product.");
   1.732 +            }
   1.733 +
   1.734 +            /* get usage page and usage */
   1.735 +            refCF =
   1.736 +                CFDictionaryGetValue(hidProperties,
   1.737 +                                     CFSTR(kIOHIDPrimaryUsagePageKey));
   1.738 +            if (refCF) {
   1.739 +                if (!CFNumberGetValue
   1.740 +                    (refCF, kCFNumberLongType, &pDevice->usagePage))
   1.741 +                    SDL_SetError
   1.742 +                        ("CFNumberGetValue error retrieving pDevice->usagePage.");
   1.743 +                refCF =
   1.744 +                    CFDictionaryGetValue(hidProperties,
   1.745 +                                         CFSTR(kIOHIDPrimaryUsageKey));
   1.746 +                if (refCF)
   1.747 +                    if (!CFNumberGetValue
   1.748 +                        (refCF, kCFNumberLongType, &pDevice->usage))
   1.749 +                        SDL_SetError
   1.750 +                            ("CFNumberGetValue error retrieving pDevice->usage.");
   1.751 +            }
   1.752 +
   1.753 +            if (NULL == refCF) {        /* get top level element HID usage page or usage */
   1.754 +                /* use top level element instead */
   1.755 +                CFTypeRef refCFTopElement = 0;
   1.756 +                refCFTopElement =
   1.757 +                    CFDictionaryGetValue(hidProperties,
   1.758 +                                         CFSTR(kIOHIDElementKey));
   1.759 +                {
   1.760 +                    /* refCFTopElement points to an array of element dictionaries */
   1.761 +                    CFRange range = { 0, CFArrayGetCount(refCFTopElement) };
   1.762 +                    CFArrayApplyFunction(refCFTopElement, range,
   1.763 +                                         HIDTopLevelElementHandler, pDevice);
   1.764 +                }
   1.765 +            }
   1.766 +
   1.767 +            CFRelease(usbProperties);
   1.768 +        } else
   1.769 +            SDL_SetError
   1.770 +                ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
   1.771 +
   1.772 +        if (kIOReturnSuccess != IOObjectRelease(parent2))
   1.773 +            SDL_SetError("IOObjectRelease error with parent2.");
   1.774 +        if (kIOReturnSuccess != IOObjectRelease(parent1))
   1.775 +            SDL_SetError("IOObjectRelease error with parent1.");
   1.776 +    }
   1.777  }
   1.778  
   1.779  
   1.780 -static recDevice *HIDBuildDevice (io_object_t hidDevice)
   1.781 +static recDevice *
   1.782 +HIDBuildDevice(io_object_t hidDevice)
   1.783  {
   1.784 -	recDevice *pDevice = (recDevice *) NewPtrClear (sizeof (recDevice));
   1.785 -	if (pDevice)
   1.786 -	{
   1.787 -		/* get dictionary for HID properties */
   1.788 -		CFMutableDictionaryRef hidProperties = 0;
   1.789 -		kern_return_t result = IORegistryEntryCreateCFProperties (hidDevice, &hidProperties, kCFAllocatorDefault, kNilOptions);
   1.790 -		if ((result == KERN_SUCCESS) && hidProperties)
   1.791 -		{
   1.792 -			/* create device interface */
   1.793 -			result = HIDCreateOpenDeviceInterface (hidDevice, pDevice);
   1.794 -			if (kIOReturnSuccess == result)
   1.795 -			{
   1.796 -				HIDGetDeviceInfo (hidDevice, hidProperties, pDevice); /* hidDevice used to find parents in registry tree */
   1.797 -				HIDGetCollectionElements (hidProperties, pDevice);
   1.798 -			}
   1.799 -			else
   1.800 -			{
   1.801 -				DisposePtr((Ptr)pDevice);
   1.802 -				pDevice = NULL;
   1.803 -			}
   1.804 -			CFRelease (hidProperties);
   1.805 -		}
   1.806 -		else
   1.807 -		{
   1.808 -			DisposePtr((Ptr)pDevice);
   1.809 -			pDevice = NULL;
   1.810 -		}
   1.811 -	}
   1.812 -	return pDevice;
   1.813 +    recDevice *pDevice = (recDevice *) NewPtrClear(sizeof(recDevice));
   1.814 +    if (pDevice) {
   1.815 +        /* get dictionary for HID properties */
   1.816 +        CFMutableDictionaryRef hidProperties = 0;
   1.817 +        kern_return_t result =
   1.818 +            IORegistryEntryCreateCFProperties(hidDevice, &hidProperties,
   1.819 +                                              kCFAllocatorDefault,
   1.820 +                                              kNilOptions);
   1.821 +        if ((result == KERN_SUCCESS) && hidProperties) {
   1.822 +            /* create device interface */
   1.823 +            result = HIDCreateOpenDeviceInterface(hidDevice, pDevice);
   1.824 +            if (kIOReturnSuccess == result) {
   1.825 +                HIDGetDeviceInfo(hidDevice, hidProperties, pDevice);    /* hidDevice used to find parents in registry tree */
   1.826 +                HIDGetCollectionElements(hidProperties, pDevice);
   1.827 +            } else {
   1.828 +                DisposePtr((Ptr) pDevice);
   1.829 +                pDevice = NULL;
   1.830 +            }
   1.831 +            CFRelease(hidProperties);
   1.832 +        } else {
   1.833 +            DisposePtr((Ptr) pDevice);
   1.834 +            pDevice = NULL;
   1.835 +        }
   1.836 +    }
   1.837 +    return pDevice;
   1.838  }
   1.839  
   1.840  /* disposes of the element list associated with a device and the memory associated with the list
   1.841   */
   1.842  
   1.843 -static void HIDDisposeElementList (recElement **elementList)
   1.844 +static void
   1.845 +HIDDisposeElementList(recElement ** elementList)
   1.846  {
   1.847 -	recElement *pElement = *elementList;
   1.848 -	while (pElement)
   1.849 -	{
   1.850 -		recElement *pElementNext = pElement->pNext;
   1.851 -		DisposePtr ((Ptr) pElement);
   1.852 -		pElement = pElementNext;
   1.853 -	}
   1.854 -	*elementList = NULL;
   1.855 +    recElement *pElement = *elementList;
   1.856 +    while (pElement) {
   1.857 +        recElement *pElementNext = pElement->pNext;
   1.858 +        DisposePtr((Ptr) pElement);
   1.859 +        pElement = pElementNext;
   1.860 +    }
   1.861 +    *elementList = NULL;
   1.862  }
   1.863  
   1.864  /* disposes of a single device, closing and releaseing interface, freeing memory fro device and elements, setting device pointer to NULL
   1.865   * all your device no longer belong to us... (i.e., you do not 'own' the device anymore)
   1.866   */
   1.867  
   1.868 -static recDevice *HIDDisposeDevice (recDevice **ppDevice)
   1.869 +static recDevice *
   1.870 +HIDDisposeDevice(recDevice ** ppDevice)
   1.871  {
   1.872 -	kern_return_t result = KERN_SUCCESS;
   1.873 -	recDevice *pDeviceNext = NULL;
   1.874 -	if (*ppDevice)
   1.875 -	{
   1.876 -		/* save next device prior to disposing of this device */
   1.877 -		pDeviceNext = (*ppDevice)->pNext;
   1.878 -		
   1.879 -		/* free element lists */
   1.880 -		HIDDisposeElementList (&(*ppDevice)->firstAxis);
   1.881 -		HIDDisposeElementList (&(*ppDevice)->firstButton);
   1.882 -		HIDDisposeElementList (&(*ppDevice)->firstHat);
   1.883 -		
   1.884 -		result = HIDCloseReleaseInterface (*ppDevice); /* function sanity checks interface value (now application does not own device) */
   1.885 -		if (kIOReturnSuccess != result)
   1.886 -			HIDReportErrorNum ("HIDCloseReleaseInterface failed when trying to dipose device.", result);
   1.887 -		DisposePtr ((Ptr)*ppDevice);
   1.888 -		*ppDevice = NULL;
   1.889 -	}
   1.890 -	return pDeviceNext;
   1.891 +    kern_return_t result = KERN_SUCCESS;
   1.892 +    recDevice *pDeviceNext = NULL;
   1.893 +    if (*ppDevice) {
   1.894 +        /* save next device prior to disposing of this device */
   1.895 +        pDeviceNext = (*ppDevice)->pNext;
   1.896 +
   1.897 +        /* free element lists */
   1.898 +        HIDDisposeElementList(&(*ppDevice)->firstAxis);
   1.899 +        HIDDisposeElementList(&(*ppDevice)->firstButton);
   1.900 +        HIDDisposeElementList(&(*ppDevice)->firstHat);
   1.901 +
   1.902 +        result = HIDCloseReleaseInterface(*ppDevice);   /* function sanity checks interface value (now application does not own device) */
   1.903 +        if (kIOReturnSuccess != result)
   1.904 +            HIDReportErrorNum
   1.905 +                ("HIDCloseReleaseInterface failed when trying to dipose device.",
   1.906 +                 result);
   1.907 +        DisposePtr((Ptr) * ppDevice);
   1.908 +        *ppDevice = NULL;
   1.909 +    }
   1.910 +    return pDeviceNext;
   1.911  }
   1.912  
   1.913  
   1.914 @@ -569,126 +613,122 @@
   1.915   * This function should return the number of available joysticks, or -1
   1.916   * on an unrecoverable fatal error.
   1.917   */
   1.918 -int SDL_SYS_JoystickInit(void)
   1.919 +int
   1.920 +SDL_SYS_JoystickInit(void)
   1.921  {
   1.922 -	IOReturn result = kIOReturnSuccess;
   1.923 -	mach_port_t masterPort = 0;
   1.924 -	io_iterator_t hidObjectIterator = 0;
   1.925 -	CFMutableDictionaryRef hidMatchDictionary = NULL;
   1.926 -	recDevice *device, *lastDevice;
   1.927 -	io_object_t ioHIDDeviceObject = 0;
   1.928 -	
   1.929 -	SDL_numjoysticks = 0;
   1.930 -	
   1.931 -	if (gpDeviceList)
   1.932 -	{
   1.933 -		SDL_SetError("Joystick: Device list already inited.");
   1.934 -		return -1;
   1.935 -	}
   1.936 -	
   1.937 -	result = IOMasterPort (bootstrap_port, &masterPort);
   1.938 -	if (kIOReturnSuccess != result)
   1.939 -	{
   1.940 -		SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
   1.941 -		return -1;
   1.942 -	}
   1.943 +    IOReturn result = kIOReturnSuccess;
   1.944 +    mach_port_t masterPort = 0;
   1.945 +    io_iterator_t hidObjectIterator = 0;
   1.946 +    CFMutableDictionaryRef hidMatchDictionary = NULL;
   1.947 +    recDevice *device, *lastDevice;
   1.948 +    io_object_t ioHIDDeviceObject = 0;
   1.949  
   1.950 -	/* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
   1.951 -	hidMatchDictionary = IOServiceMatching (kIOHIDDeviceKey);
   1.952 -	if (hidMatchDictionary)
   1.953 -	{
   1.954 -		/* Add key for device type (joystick, in this case) to refine the matching dictionary. */
   1.955 -		
   1.956 -		/* NOTE: we now perform this filtering later
   1.957 -		UInt32 usagePage = kHIDPage_GenericDesktop;
   1.958 -		UInt32 usage = kHIDUsage_GD_Joystick;
   1.959 -		CFNumberRef refUsage = NULL, refUsagePage = NULL;
   1.960 +    SDL_numjoysticks = 0;
   1.961  
   1.962 -		refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
   1.963 -		CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
   1.964 -		refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
   1.965 -		CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
   1.966 -		*/
   1.967 -	}
   1.968 -	else
   1.969 -	{
   1.970 -		SDL_SetError("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
   1.971 -		return -1;
   1.972 -	}
   1.973 -	
   1.974 -	/*/ Now search I/O Registry for matching devices. */
   1.975 -	result = IOServiceGetMatchingServices (masterPort, hidMatchDictionary, &hidObjectIterator);
   1.976 -	/* Check for errors */
   1.977 -	if (kIOReturnSuccess != result)
   1.978 -	{
   1.979 -		SDL_SetError("Joystick: Couldn't create a HID object iterator.");
   1.980 -		return -1;
   1.981 -	}
   1.982 -	if (!hidObjectIterator) /* there are no joysticks */
   1.983 -	{
   1.984 -		gpDeviceList = NULL;
   1.985 -		SDL_numjoysticks = 0;
   1.986 -		return 0;
   1.987 -	}
   1.988 -	/* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
   1.989 +    if (gpDeviceList) {
   1.990 +        SDL_SetError("Joystick: Device list already inited.");
   1.991 +        return -1;
   1.992 +    }
   1.993  
   1.994 -	/* build flat linked list of devices from device iterator */
   1.995 +    result = IOMasterPort(bootstrap_port, &masterPort);
   1.996 +    if (kIOReturnSuccess != result) {
   1.997 +        SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
   1.998 +        return -1;
   1.999 +    }
  1.1000  
  1.1001 -	gpDeviceList = lastDevice = NULL;
  1.1002 -	
  1.1003 -	while ((ioHIDDeviceObject = IOIteratorNext (hidObjectIterator)))
  1.1004 -	{
  1.1005 -		/* build a device record */
  1.1006 -		device = HIDBuildDevice (ioHIDDeviceObject);
  1.1007 -		if (!device)
  1.1008 -			continue;
  1.1009 +    /* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
  1.1010 +    hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
  1.1011 +    if (hidMatchDictionary) {
  1.1012 +        /* Add key for device type (joystick, in this case) to refine the matching dictionary. */
  1.1013  
  1.1014 -		/* dump device object, it is no longer needed */
  1.1015 -		result = IOObjectRelease (ioHIDDeviceObject);
  1.1016 +        /* NOTE: we now perform this filtering later
  1.1017 +           UInt32 usagePage = kHIDPage_GenericDesktop;
  1.1018 +           UInt32 usage = kHIDUsage_GD_Joystick;
  1.1019 +           CFNumberRef refUsage = NULL, refUsagePage = NULL;
  1.1020 +
  1.1021 +           refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
  1.1022 +           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
  1.1023 +           refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
  1.1024 +           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
  1.1025 +         */
  1.1026 +    } else {
  1.1027 +        SDL_SetError
  1.1028 +            ("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
  1.1029 +        return -1;
  1.1030 +    }
  1.1031 +
  1.1032 +    /*/ Now search I/O Registry for matching devices. */
  1.1033 +    result =
  1.1034 +        IOServiceGetMatchingServices(masterPort, hidMatchDictionary,
  1.1035 +                                     &hidObjectIterator);
  1.1036 +    /* Check for errors */
  1.1037 +    if (kIOReturnSuccess != result) {
  1.1038 +        SDL_SetError("Joystick: Couldn't create a HID object iterator.");
  1.1039 +        return -1;
  1.1040 +    }
  1.1041 +    if (!hidObjectIterator) {   /* there are no joysticks */
  1.1042 +        gpDeviceList = NULL;
  1.1043 +        SDL_numjoysticks = 0;
  1.1044 +        return 0;
  1.1045 +    }
  1.1046 +    /* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
  1.1047 +
  1.1048 +    /* build flat linked list of devices from device iterator */
  1.1049 +
  1.1050 +    gpDeviceList = lastDevice = NULL;
  1.1051 +
  1.1052 +    while ((ioHIDDeviceObject = IOIteratorNext(hidObjectIterator))) {
  1.1053 +        /* build a device record */
  1.1054 +        device = HIDBuildDevice(ioHIDDeviceObject);
  1.1055 +        if (!device)
  1.1056 +            continue;
  1.1057 +
  1.1058 +        /* dump device object, it is no longer needed */
  1.1059 +        result = IOObjectRelease(ioHIDDeviceObject);
  1.1060  /*		if (KERN_SUCCESS != result)
  1.1061  			HIDReportErrorNum ("IOObjectRelease error with ioHIDDeviceObject.", result);
  1.1062  */
  1.1063  
  1.1064 -		/* Filter device list to non-keyboard/mouse stuff */ 
  1.1065 -		if ( (device->usagePage != kHIDPage_GenericDesktop) ||
  1.1066 -		     ((device->usage != kHIDUsage_GD_Joystick &&
  1.1067 -		      device->usage != kHIDUsage_GD_GamePad)) ) {
  1.1068 +        /* Filter device list to non-keyboard/mouse stuff */
  1.1069 +        if ((device->usagePage != kHIDPage_GenericDesktop) ||
  1.1070 +            ((device->usage != kHIDUsage_GD_Joystick &&
  1.1071 +              device->usage != kHIDUsage_GD_GamePad))) {
  1.1072  
  1.1073 -			/* release memory for the device */
  1.1074 -			HIDDisposeDevice (&device);
  1.1075 -			DisposePtr((Ptr)device);
  1.1076 -			continue;
  1.1077 -		}
  1.1078 -		
  1.1079 -		/* Add device to the end of the list */
  1.1080 -		if (lastDevice)
  1.1081 -			lastDevice->pNext = device;
  1.1082 -		else
  1.1083 -			gpDeviceList = device;
  1.1084 -		lastDevice = device;
  1.1085 -	}
  1.1086 -	result = IOObjectRelease (hidObjectIterator); /* release the iterator */
  1.1087 +            /* release memory for the device */
  1.1088 +            HIDDisposeDevice(&device);
  1.1089 +            DisposePtr((Ptr) device);
  1.1090 +            continue;
  1.1091 +        }
  1.1092  
  1.1093 -	/* Count the total number of devices we found */
  1.1094 -	device = gpDeviceList;
  1.1095 -	while (device)
  1.1096 -	{
  1.1097 -		SDL_numjoysticks++;
  1.1098 -		device = device->pNext;
  1.1099 -	}
  1.1100 -	
  1.1101 -	return SDL_numjoysticks;
  1.1102 +        /* Add device to the end of the list */
  1.1103 +        if (lastDevice)
  1.1104 +            lastDevice->pNext = device;
  1.1105 +        else
  1.1106 +            gpDeviceList = device;
  1.1107 +        lastDevice = device;
  1.1108 +    }
  1.1109 +    result = IOObjectRelease(hidObjectIterator);        /* release the iterator */
  1.1110 +
  1.1111 +    /* Count the total number of devices we found */
  1.1112 +    device = gpDeviceList;
  1.1113 +    while (device) {
  1.1114 +        SDL_numjoysticks++;
  1.1115 +        device = device->pNext;
  1.1116 +    }
  1.1117 +
  1.1118 +    return SDL_numjoysticks;
  1.1119  }
  1.1120  
  1.1121  /* Function to get the device-dependent name of a joystick */
  1.1122 -const char *SDL_SYS_JoystickName(int index)
  1.1123 +const char *
  1.1124 +SDL_SYS_JoystickName(int index)
  1.1125  {
  1.1126 -	recDevice *device = gpDeviceList;
  1.1127 -	
  1.1128 -	for (; index > 0; index--)
  1.1129 -		device = device->pNext;
  1.1130 +    recDevice *device = gpDeviceList;
  1.1131  
  1.1132 -	return device->product;
  1.1133 +    for (; index > 0; index--)
  1.1134 +        device = device->pNext;
  1.1135 +
  1.1136 +    return device->product;
  1.1137  }
  1.1138  
  1.1139  /* Function to open a joystick for use.
  1.1140 @@ -696,23 +736,24 @@
  1.1141   * This should fill the nbuttons and naxes fields of the joystick structure.
  1.1142   * It returns 0, or -1 if there is an error.
  1.1143   */
  1.1144 -int SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
  1.1145 +int
  1.1146 +SDL_SYS_JoystickOpen(SDL_Joystick * joystick)
  1.1147  {
  1.1148 -	recDevice *device = gpDeviceList;
  1.1149 -	int index;
  1.1150 -	
  1.1151 -	for (index = joystick->index; index > 0; index--)
  1.1152 -		device = device->pNext;
  1.1153 +    recDevice *device = gpDeviceList;
  1.1154 +    int index;
  1.1155  
  1.1156 -	joystick->hwdata = device;
  1.1157 -	joystick->name = device->product;
  1.1158 +    for (index = joystick->index; index > 0; index--)
  1.1159 +        device = device->pNext;
  1.1160  
  1.1161 -	joystick->naxes = device->axes;
  1.1162 -	joystick->nhats = device->hats;
  1.1163 -	joystick->nballs = 0;
  1.1164 -	joystick->nbuttons = device->buttons;
  1.1165 +    joystick->hwdata = device;
  1.1166 +    joystick->name = device->product;
  1.1167  
  1.1168 -	return 0;
  1.1169 +    joystick->naxes = device->axes;
  1.1170 +    joystick->nhats = device->hats;
  1.1171 +    joystick->nballs = 0;
  1.1172 +    joystick->nbuttons = device->buttons;
  1.1173 +
  1.1174 +    return 0;
  1.1175  }
  1.1176  
  1.1177  /* Function to update the state of a joystick - called as a device poll.
  1.1178 @@ -720,123 +761,121 @@
  1.1179   * but instead should call SDL_PrivateJoystick*() to deliver events
  1.1180   * and update joystick device state.
  1.1181   */
  1.1182 -void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
  1.1183 +void
  1.1184 +SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
  1.1185  {
  1.1186 -	recDevice *device = joystick->hwdata;
  1.1187 -	recElement *element;
  1.1188 -	SInt32 value;
  1.1189 -	int i;
  1.1190 +    recDevice *device = joystick->hwdata;
  1.1191 +    recElement *element;
  1.1192 +    SInt32 value;
  1.1193 +    int i;
  1.1194  
  1.1195 -	if (device->removed)  /* device was unplugged; ignore it. */
  1.1196 -	{
  1.1197 -		if (device->uncentered)
  1.1198 -		{
  1.1199 -			device->uncentered = 0;
  1.1200 +    if (device->removed) {      /* device was unplugged; ignore it. */
  1.1201 +        if (device->uncentered) {
  1.1202 +            device->uncentered = 0;
  1.1203  
  1.1204 -			/* Tell the app that everything is centered/unpressed... */
  1.1205 -			for (i = 0; i < device->axes; i++)
  1.1206 -				SDL_PrivateJoystickAxis(joystick, i, 0);
  1.1207 +            /* Tell the app that everything is centered/unpressed... */
  1.1208 +            for (i = 0; i < device->axes; i++)
  1.1209 +                SDL_PrivateJoystickAxis(joystick, i, 0);
  1.1210  
  1.1211 -			for (i = 0; i < device->buttons; i++)
  1.1212 -				SDL_PrivateJoystickButton(joystick, i, 0);
  1.1213 +            for (i = 0; i < device->buttons; i++)
  1.1214 +                SDL_PrivateJoystickButton(joystick, i, 0);
  1.1215  
  1.1216 -			for (i = 0; i < device->hats; i++)
  1.1217 -				SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  1.1218 -		}
  1.1219 +            for (i = 0; i < device->hats; i++)
  1.1220 +                SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
  1.1221 +        }
  1.1222  
  1.1223 -		return;
  1.1224 -	}
  1.1225 +        return;
  1.1226 +    }
  1.1227  
  1.1228 -	element = device->firstAxis;
  1.1229 -	i = 0;
  1.1230 -	while (element)
  1.1231 -	{
  1.1232 -		value = HIDScaledCalibratedValue(device, element, -32768, 32767);
  1.1233 -		if ( value != joystick->axes[i] )
  1.1234 -			SDL_PrivateJoystickAxis(joystick, i, value);
  1.1235 -		element = element->pNext;
  1.1236 -		++i;
  1.1237 -	}
  1.1238 -	
  1.1239 -	element = device->firstButton;
  1.1240 -	i = 0;
  1.1241 -	while (element)
  1.1242 -	{
  1.1243 -		value = HIDGetElementValue(device, element);
  1.1244 -        if (value > 1)  /* handle pressure-sensitive buttons */
  1.1245 +    element = device->firstAxis;
  1.1246 +    i = 0;
  1.1247 +    while (element) {
  1.1248 +        value = HIDScaledCalibratedValue(device, element, -32768, 32767);
  1.1249 +        if (value != joystick->axes[i])
  1.1250 +            SDL_PrivateJoystickAxis(joystick, i, value);
  1.1251 +        element = element->pNext;
  1.1252 +        ++i;
  1.1253 +    }
  1.1254 +
  1.1255 +    element = device->firstButton;
  1.1256 +    i = 0;
  1.1257 +    while (element) {
  1.1258 +        value = HIDGetElementValue(device, element);
  1.1259 +        if (value > 1)          /* handle pressure-sensitive buttons */
  1.1260              value = 1;
  1.1261 -		if ( value != joystick->buttons[i] )
  1.1262 -			SDL_PrivateJoystickButton(joystick, i, value);
  1.1263 -		element = element->pNext;
  1.1264 -		++i;
  1.1265 -	}
  1.1266 -	    
  1.1267 -	element = device->firstHat;
  1.1268 -	i = 0;
  1.1269 -	while (element)
  1.1270 -	{
  1.1271 -		Uint8 pos = 0;
  1.1272 +        if (value != joystick->buttons[i])
  1.1273 +            SDL_PrivateJoystickButton(joystick, i, value);
  1.1274 +        element = element->pNext;
  1.1275 +        ++i;
  1.1276 +    }
  1.1277  
  1.1278 -		value = HIDGetElementValue(device, element);
  1.1279 -		if (element->max == 3) /* 4 position hatswitch - scale up value */
  1.1280 -			value *= 2;
  1.1281 -		else if (element->max != 7) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
  1.1282 -			value = -1;
  1.1283 -		switch(value)
  1.1284 -		{
  1.1285 -			case 0:
  1.1286 -				pos = SDL_HAT_UP;
  1.1287 -				break;
  1.1288 -			case 1:
  1.1289 -				pos = SDL_HAT_RIGHTUP;
  1.1290 -				break;
  1.1291 -			case 2:
  1.1292 -				pos = SDL_HAT_RIGHT;
  1.1293 -				break;
  1.1294 -			case 3:
  1.1295 -				pos = SDL_HAT_RIGHTDOWN;
  1.1296 -				break;
  1.1297 -			case 4:
  1.1298 -				pos = SDL_HAT_DOWN;
  1.1299 -				break;
  1.1300 -			case 5:
  1.1301 -				pos = SDL_HAT_LEFTDOWN;
  1.1302 -				break;
  1.1303 -			case 6:
  1.1304 -				pos = SDL_HAT_LEFT;
  1.1305 -				break;
  1.1306 -			case 7:
  1.1307 -				pos = SDL_HAT_LEFTUP;
  1.1308 -				break;
  1.1309 -			default:
  1.1310 -				/* Every other value is mapped to center. We do that because some
  1.1311 -				 * joysticks use 8 and some 15 for this value, and apparently
  1.1312 -				 * there are even more variants out there - so we try to be generous.
  1.1313 -				 */
  1.1314 -				pos = SDL_HAT_CENTERED;
  1.1315 -				break;
  1.1316 -		}
  1.1317 -		if ( pos != joystick->hats[i] )
  1.1318 -			SDL_PrivateJoystickHat(joystick, i, pos);
  1.1319 -		element = element->pNext;
  1.1320 -		++i;
  1.1321 -	}
  1.1322 -	
  1.1323 -	return;
  1.1324 +    element = device->firstHat;
  1.1325 +    i = 0;
  1.1326 +    while (element) {
  1.1327 +        Uint8 pos = 0;
  1.1328 +
  1.1329 +        value = HIDGetElementValue(device, element);
  1.1330 +        if (element->max == 3)  /* 4 position hatswitch - scale up value */
  1.1331 +            value *= 2;
  1.1332 +        else if (element->max != 7)     /* Neither a 4 nor 8 positions - fall back to default position (centered) */
  1.1333 +            value = -1;
  1.1334 +        switch (value) {
  1.1335 +        case 0:
  1.1336 +            pos = SDL_HAT_UP;
  1.1337 +            break;
  1.1338 +        case 1:
  1.1339 +            pos = SDL_HAT_RIGHTUP;
  1.1340 +            break;
  1.1341 +        case 2:
  1.1342 +            pos = SDL_HAT_RIGHT;
  1.1343 +            break;
  1.1344 +        case 3:
  1.1345 +            pos = SDL_HAT_RIGHTDOWN;
  1.1346 +            break;
  1.1347 +        case 4:
  1.1348 +            pos = SDL_HAT_DOWN;
  1.1349 +            break;
  1.1350 +        case 5:
  1.1351 +            pos = SDL_HAT_LEFTDOWN;
  1.1352 +            break;
  1.1353 +        case 6:
  1.1354 +            pos = SDL_HAT_LEFT;
  1.1355 +            break;
  1.1356 +        case 7:
  1.1357 +            pos = SDL_HAT_LEFTUP;
  1.1358 +            break;
  1.1359 +        default:
  1.1360 +            /* Every other value is mapped to center. We do that because some
  1.1361 +             * joysticks use 8 and some 15 for this value, and apparently
  1.1362 +             * there are even more variants out there - so we try to be generous.
  1.1363 +             */
  1.1364 +            pos = SDL_HAT_CENTERED;
  1.1365 +            break;
  1.1366 +        }
  1.1367 +        if (pos != joystick->hats[i])
  1.1368 +            SDL_PrivateJoystickHat(joystick, i, pos);
  1.1369 +        element = element->pNext;
  1.1370 +        ++i;
  1.1371 +    }
  1.1372 +
  1.1373 +    return;
  1.1374  }
  1.1375  
  1.1376  /* Function to close a joystick after use */
  1.1377 -void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
  1.1378 +void
  1.1379 +SDL_SYS_JoystickClose(SDL_Joystick * joystick)
  1.1380  {
  1.1381 -	/* Should we do anything here? */
  1.1382 -	return;
  1.1383 +    /* Should we do anything here? */
  1.1384 +    return;
  1.1385  }
  1.1386  
  1.1387  /* Function to perform any system-specific joystick related cleanup */
  1.1388 -void SDL_SYS_JoystickQuit(void)
  1.1389 +void
  1.1390 +SDL_SYS_JoystickQuit(void)
  1.1391  {
  1.1392 -	while (NULL != gpDeviceList)
  1.1393 -		gpDeviceList = HIDDisposeDevice (&gpDeviceList);
  1.1394 +    while (NULL != gpDeviceList)
  1.1395 +        gpDeviceList = HIDDisposeDevice(&gpDeviceList);
  1.1396  }
  1.1397  
  1.1398  #endif /* SDL_JOYSTICK_IOKIT */
  1.1399 +/* vi: set ts=4 sw=4 expandtab: */