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