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