19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef SDL_JOYSTICK_IOKIT
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* SDL joystick driver for Darwin / Mac OS X, based on the IOKit HID API */
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Written 2001 by Max Horn */
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <ctype.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sysexits.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mach/mach.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <mach/mach_error.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <IOKit/IOKitLib.h>
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <IOKit/IOCFPlugIn.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef MACOS_10_0_4
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <IOKit/hidsystem/IOHIDUsageTables.h>
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The header was moved here in Mac OS X 10.1 */
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Kernel/IOKit/hidsystem/IOHIDUsageTables.h>
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <IOKit/hid/IOHIDLib.h>
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <IOKit/hid/IOHIDKeys.h>
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <CoreFoundation/CoreFoundation.h>
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <Carbon/Carbon.h> /* for NewPtrClear, DisposePtr */
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_joystick.h"
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysjoystick.h"
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_joystick_c.h"
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct recElement
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOHIDElementCookie cookie;				/* unique value which identifies element, will NOT change */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long min;								/* reported min value possible */
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long max;								/* reported max value possible */
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if 0
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* TODO: maybe should handle the following stuff somehow? */
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long scaledMin;							/* reported scaled min value possible */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long scaledMax;							/* reported scaled max value possible */
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long size;								/* size in bits of data return from element */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean relative;						/* are reports relative to last report (deltas) */
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean wrapping;						/* does element wrap around (one value higher than max is min) */
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean nonLinear;						/* are the values reported non-linear relative to element movement */
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean preferredState;					/* does element have a preferred state (such as a button) */
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	Boolean nullState;						/* does element have null state */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* 0 */
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* runtime variables used for auto-calibration */
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long minReport;							/* min returned value */
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long maxReport;							/* max returned value */
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct recElement * pNext;				/* next element in list */
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct recElement recElement;
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct joystick_hwdata
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOHIDDeviceInterface ** interface;		/* interface to device, NULL = no interface */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	char product[256];							/* name of product */
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long usage;								/* usage page from IOUSBHID Parser.h which defines general usage */
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long usagePage;							/* usage within above page from IOUSBHID Parser.h which defines specific usage */
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long axes;								/* number of axis (calculated, not reported by device) */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long buttons;							/* number of buttons (calculated, not reported by device) */
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long hats;								/* number of hat switches (calculated, not reported by device) */
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long elements;							/* number of total elements (shouldbe total of above) (calculated, not reported by device) */
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement* firstAxis;
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement* firstButton;
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement* firstHat;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int removed;
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int uncentered;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct joystick_hwdata* pNext;			/* next device */
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Halltypedef struct joystick_hwdata recDevice;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Linked list of all available devices */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic recDevice *gpDeviceList = NULL;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDReportErrorNum (char * strError, long numError)
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_SetError(strError);
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, recDevice *pDevice);
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* returns current value for element, polling element
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * will return 0 on error conditions which should be accounted for by application
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SInt32 HIDGetElementValue (recDevice *pDevice, recElement *pElement)
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOReturn result = kIOReturnSuccess;
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOHIDEventStruct hidEvent;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hidEvent.value = 0;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (NULL != pDevice && NULL != pElement && NULL != pDevice->interface)
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = (*(pDevice->interface))->getElementValue(pDevice->interface, pElement->cookie, &hidEvent);
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess == result)
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* record min and max for auto calibration */
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (hidEvent.value < pElement->minReport)
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pElement->minReport = hidEvent.value;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (hidEvent.value > pElement->maxReport)
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pElement->maxReport = hidEvent.value;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* auto user scale */
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return hidEvent.value;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SInt32 HIDScaledCalibratedValue (recDevice *pDevice, recElement *pElement, long min, long max)
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	float deviceScale = max - min;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	float readScale = pElement->maxReport - pElement->minReport;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SInt32 value = HIDGetElementValue(pDevice, pElement);
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (readScale == 0)
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return value; /* no scaling at all */
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return ((value - pElement->minReport) * deviceScale / readScale) + min;
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDRemovalCallback(void * target,
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               IOReturn result,
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               void * refcon,
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                               void * sender)
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *device = (recDevice *) refcon;
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->removed = 1;
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device->uncentered = 1;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Create and open an interface to device, required prior to extracting values or building queues.
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Note: appliction now owns the device and must close and release it prior to exiting
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic IOReturn HIDCreateOpenDeviceInterface (io_object_t hidDevice, recDevice *pDevice)
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOReturn result = kIOReturnSuccess;
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	HRESULT plugInResult = S_OK;
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SInt32 score = 0;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOCFPlugInInterface ** ppPlugInInterface = NULL;
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (NULL == pDevice->interface)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IOCreatePlugInInterfaceForService (hidDevice, kIOHIDDeviceUserClientTypeID,
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall													kIOCFPlugInInterfaceID, &ppPlugInInterface, &score);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess == result)
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Call a method of the intermediate plug-in to create the device interface */
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			plugInResult = (*ppPlugInInterface)->QueryInterface (ppPlugInInterface,
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								CFUUIDGetUUIDBytes (kIOHIDDeviceInterfaceID), (void *) &(pDevice->interface));
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (S_OK != plugInResult)
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				HIDReportErrorNum ("Couldn�t query HID class device interface from plugInInterface", plugInResult);
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(*ppPlugInInterface)->Release (ppPlugInInterface);
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("Failed to create **plugInInterface via IOCreatePlugInInterfaceForService.", result);
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (NULL != pDevice->interface)
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = (*(pDevice->interface))->open (pDevice->interface, 0);
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess != result)
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("Failed to open pDevice->interface via open.", result);
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(*(pDevice->interface))->setRemovalCallback (pDevice->interface, HIDRemovalCallback, pDevice, pDevice);
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return result;
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Closes and releases interface to device, should be done prior to exting application
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Note: will have no affect if device or interface do not exist
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * application will "own" the device if interface is not closed
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * (device may have to be plug and re-plugged in different location to get it working again without a restart)
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic IOReturn HIDCloseReleaseInterface (recDevice *pDevice)
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOReturn result = kIOReturnSuccess;
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((NULL != pDevice) && (NULL != pDevice->interface))
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* close the interface */
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = (*(pDevice->interface))->close (pDevice->interface);
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnNotOpen == result)
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* do nothing as device was not opened, thus can't be closed */
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if (kIOReturnSuccess != result)
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("Failed to close IOHIDDeviceInterface.", result);
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* release the interface */
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = (*(pDevice->interface))->Release (pDevice->interface);
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess != result)
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("Failed to release IOHIDDeviceInterface.", result);
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pDevice->interface = NULL;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return result;
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* extracts actual specific element information from each element CF dictionary entry */
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetElementInfo (CFTypeRef refElement, recElement *pElement)
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long number;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refType;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementCookieKey));
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->cookie = (IOHIDElementCookie) number;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMinKey));
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->minReport = pElement->min = number;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementMaxKey));
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->maxReport = pElement->max = number;
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	TODO: maybe should handle the following stuff somehow?
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMinKey));
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->scaledMin = number;
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMaxKey));
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->scaledMax = number;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementSizeKey));
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->size = number;
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsRelativeKey));
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType)
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->relative = CFBooleanGetValue (refType);
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsWrappingKey));
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType)
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->wrapping = CFBooleanGetValue (refType);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsNonLinearKey));
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType)
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->nonLinear = CFBooleanGetValue (refType);
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferedStateKey));
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType)
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->preferredState = CFBooleanGetValue (refType);
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasNullStateKey));
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refType)
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement->nullState = CFBooleanGetValue (refType);
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* examines CF dictionary vlaue in device element hierarchy to determine if it is element of interest or a collection of more elements
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * if element of interest allocate storage, add to list and retrieve element specific info
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * if collection then pass on to deconstruction collection into additional individual elements
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDAddElement (CFTypeRef refElement, recDevice* pDevice)
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement* element = NULL;
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement** headElement = NULL;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	long elementType, usagePage, usage;
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refElementType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementTypeKey));
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refUsagePage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsagePageKey));
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refUsage = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementUsageKey));
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((refElementType) && (CFNumberGetValue (refElementType, kCFNumberLongType, &elementType)))
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* look at types of interest */
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((elementType == kIOHIDElementTypeInput_Misc) || (elementType == kIOHIDElementTypeInput_Button) ||
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			(elementType == kIOHIDElementTypeInput_Axis))
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (refUsagePage && CFNumberGetValue (refUsagePage, kCFNumberLongType, &usagePage) &&
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				refUsage && CFNumberGetValue (refUsage, kCFNumberLongType, &usage))
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				switch (usagePage) /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case kHIDPage_GenericDesktop:
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						{
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							switch (usage) /* look at usage to determine function */
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							{
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_X:
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Y:
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Z:
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Rx:
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Ry:
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Rz:
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Slider:
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Dial:
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Wheel:
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									element = (recElement *) NewPtrClear (sizeof (recElement));
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									if (element)
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									{
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall										pDevice->axes++;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall										headElement = &(pDevice->firstAxis);
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									}
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								break;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								case kHIDUsage_GD_Hatswitch:
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									element = (recElement *) NewPtrClear (sizeof (recElement));
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									if (element)
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									{
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall										pDevice->hats++;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall										headElement = &(pDevice->firstHat);
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall									}
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall								break;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							}
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					case kHIDPage_Button:
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						element = (recElement *) NewPtrClear (sizeof (recElement));
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						if (element)
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						{
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							pDevice->buttons++;
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall							headElement = &(pDevice->firstButton);
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						}
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					default:
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						break;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if (kIOHIDElementTypeCollection == elementType)
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDGetCollectionElements ((CFMutableDictionaryRef) refElement, pDevice);
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (element && headElement) /* add to list */
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pDevice->elements++;
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (NULL == *headElement)
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*headElement = element;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			recElement *elementPrevious, *elementCurrent;
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			elementCurrent = *headElement;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			while (elementCurrent)
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				elementPrevious = elementCurrent;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				elementCurrent = elementPrevious->pNext;
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			elementPrevious->pNext = element;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		element->pNext = NULL;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDGetElementInfo (refElement, element);
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* collects information from each array member in device element list (each array memeber = element) */
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetElementsCFArrayHandler (const void * value, void * parameter)
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (CFGetTypeID (value) == CFDictionaryGetTypeID ())
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDAddElement ((CFTypeRef) value, (recDevice *) parameter);
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* handles retrieval of element information from arrays of elements in device IO registry information */
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetElements (CFTypeRef refElementCurrent, recDevice *pDevice)
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeID type = CFGetTypeID (refElementCurrent);
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (type == CFArrayGetTypeID()) /* if element is an array */
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFRange range = {0, CFArrayGetCount (refElementCurrent)};
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* CountElementsCFArrayHandler called for each array member */
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFArrayApplyFunction (refElementCurrent, range, HIDGetElementsCFArrayHandler, pDevice);
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* handles extracting element information from element collection CF types
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * used from top level element decoding and hierarchy deconstruction to flatten device element list
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetCollectionElements (CFMutableDictionaryRef deviceProperties, recDevice *pDevice)
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refElementTop = CFDictionaryGetValue (deviceProperties, CFSTR(kIOHIDElementKey));
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (refElementTop)
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDGetElements (refElementTop, pDevice);
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* use top level element usage page and usage to discern device usage page and usage setting appropriate vlaues in device record */
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDTopLevelElementHandler (const void * value, void * parameter)
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFTypeRef refCF = 0;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (CFGetTypeID (value) != CFDictionaryGetTypeID ())
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsagePageKey));
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!CFNumberGetValue (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError ("CFNumberGetValue error retrieving pDevice->usagePage.");
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	refCF = CFDictionaryGetValue (value, CFSTR(kIOHIDElementUsageKey));
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!CFNumberGetValue (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError ("CFNumberGetValue error retrieving pDevice->usage.");
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* extracts device info from CF dictionary records in IO registry */
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDGetDeviceInfo (io_object_t hidDevice, CFMutableDictionaryRef hidProperties, recDevice *pDevice)
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFMutableDictionaryRef usbProperties = 0;
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	io_registry_entry_t parent1, parent2;
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 */
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if ((KERN_SUCCESS == IORegistryEntryGetParentEntry (hidDevice, kIOServicePlane, &parent1)) &&
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(KERN_SUCCESS == IORegistryEntryGetParentEntry (parent1, kIOServicePlane, &parent2)) &&
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		(KERN_SUCCESS == IORegistryEntryCreateCFProperties (parent2, &usbProperties, kCFAllocatorDefault, kNilOptions)))
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (usbProperties)
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CFTypeRef refCF = 0;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* get device info
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 * try hid dictionary first, if fail then go to usb dictionary
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 */
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* get product name */
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDProductKey));
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (!refCF)
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				refCF = CFDictionaryGetValue (usbProperties, CFSTR("USB Product Name"));
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (refCF)
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (!CFStringGetCString (refCF, pDevice->product, 256, CFStringGetSystemEncoding ()))
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_SetError ("CFStringGetCString error retrieving pDevice->product.");
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* get usage page and usage */
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsagePageKey));
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (refCF)
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usagePage))
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					SDL_SetError ("CFNumberGetValue error retrieving pDevice->usagePage.");
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				refCF = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDPrimaryUsageKey));
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				if (refCF)
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					if (!CFNumberGetValue (refCF, kCFNumberLongType, &pDevice->usage))
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall						SDL_SetError ("CFNumberGetValue error retrieving pDevice->usage.");
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (NULL == refCF) /* get top level element HID usage page or usage */
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* use top level element instead */
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				CFTypeRef refCFTopElement = 0;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				refCFTopElement = CFDictionaryGetValue (hidProperties, CFSTR(kIOHIDElementKey));
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				{
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					/* refCFTopElement points to an array of element dictionaries */
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					CFRange range = {0, CFArrayGetCount (refCFTopElement)};
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall					CFArrayApplyFunction (refCFTopElement, range, HIDTopLevelElementHandler, pDevice);
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				}
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CFRelease (usbProperties);
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess != IOObjectRelease (parent2))
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError ("IOObjectRelease error with parent2.");
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess != IOObjectRelease (parent1))
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_SetError ("IOObjectRelease error with parent1.");
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic recDevice *HIDBuildDevice (io_object_t hidDevice)
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *pDevice = (recDevice *) NewPtrClear (sizeof (recDevice));
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (pDevice)
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* get dictionary for HID properties */
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFMutableDictionaryRef hidProperties = 0;
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		kern_return_t result = IORegistryEntryCreateCFProperties (hidDevice, &hidProperties, kCFAllocatorDefault, kNilOptions);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ((result == KERN_SUCCESS) && hidProperties)
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* create device interface */
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			result = HIDCreateOpenDeviceInterface (hidDevice, pDevice);
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (kIOReturnSuccess == result)
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				HIDGetDeviceInfo (hidDevice, hidProperties, pDevice); /* hidDevice used to find parents in registry tree */
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				HIDGetCollectionElements (hidProperties, pDevice);
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			else
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				DisposePtr((Ptr)pDevice);
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pDevice = NULL;
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			CFRelease (hidProperties);
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DisposePtr((Ptr)pDevice);
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			pDevice = NULL;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return pDevice;
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* disposes of the element list associated with a device and the memory associated with the list
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void HIDDisposeElementList (recElement **elementList)
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement *pElement = *elementList;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (pElement)
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		recElement *pElementNext = pElement->pNext;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DisposePtr ((Ptr) pElement);
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pElement = pElementNext;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	*elementList = NULL;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* disposes of a single device, closing and releaseing interface, freeing memory fro device and elements, setting device pointer to NULL
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * all your device no longer belong to us... (i.e., you do not 'own' the device anymore)
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic recDevice *HIDDisposeDevice (recDevice **ppDevice)
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	kern_return_t result = KERN_SUCCESS;
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *pDeviceNext = NULL;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (*ppDevice)
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* save next device prior to disposing of this device */
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		pDeviceNext = (*ppDevice)->pNext;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* free element lists */
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDDisposeElementList (&(*ppDevice)->firstAxis);
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDDisposeElementList (&(*ppDevice)->firstButton);
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		HIDDisposeElementList (&(*ppDevice)->firstHat);
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = HIDCloseReleaseInterface (*ppDevice); /* function sanity checks interface value (now application does not own device) */
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (kIOReturnSuccess != result)
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("HIDCloseReleaseInterface failed when trying to dipose device.", result);
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		DisposePtr ((Ptr)*ppDevice);
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*ppDevice = NULL;
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return pDeviceNext;
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to scan the system for joysticks.
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Joystick 0 should be the system default joystick.
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function should return the number of available joysticks, or -1
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * on an unrecoverable fatal error.
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickInit(void)
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	IOReturn result = kIOReturnSuccess;
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	mach_port_t masterPort = 0;
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	io_iterator_t hidObjectIterator = 0;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CFMutableDictionaryRef hidMatchDictionary = NULL;
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *device, *lastDevice;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	io_object_t ioHIDDeviceObject = 0;
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_numjoysticks = 0;
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (gpDeviceList)
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Joystick: Device list already inited.");
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IOMasterPort (bootstrap_port, &masterPort);
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (kIOReturnSuccess != result)
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	hidMatchDictionary = IOServiceMatching (kIOHIDDeviceKey);
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (hidMatchDictionary)
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Add key for device type (joystick, in this case) to refine the matching dictionary. */
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* NOTE: we now perform this filtering later
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UInt32 usagePage = kHIDPage_GenericDesktop;
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		UInt32 usage = kHIDUsage_GD_Joystick;
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFNumberRef refUsage = NULL, refUsagePage = NULL;
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		*/
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	else
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/*/ Now search I/O Registry for matching devices. */
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IOServiceGetMatchingServices (masterPort, hidMatchDictionary, &hidObjectIterator);
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Check for errors */
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (kIOReturnSuccess != result)
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_SetError("Joystick: Couldn't create a HID object iterator.");
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return -1;
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (!hidObjectIterator) /* there are no joysticks */
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gpDeviceList = NULL;
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_numjoysticks = 0;
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return 0;
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* build flat linked list of devices from device iterator */
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	gpDeviceList = lastDevice = NULL;
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while ((ioHIDDeviceObject = IOIteratorNext (hidObjectIterator)))
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* build a device record */
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device = HIDBuildDevice (ioHIDDeviceObject);
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (!device)
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			continue;
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* dump device object, it is no longer needed */
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		result = IOObjectRelease (ioHIDDeviceObject);
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*		if (KERN_SUCCESS != result)
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDReportErrorNum ("IOObjectRelease error with ioHIDDeviceObject.", result);
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Filter device list to non-keyboard/mouse stuff */
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( (device->usagePage != kHIDPage_GenericDesktop) ||
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		     ((device->usage != kHIDUsage_GD_Joystick &&
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      device->usage != kHIDUsage_GD_GamePad &&
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      device->usage != kHIDUsage_GD_MultiAxisController)) ) {
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* release memory for the device */
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			HIDDisposeDevice (&device);
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			DisposePtr((Ptr)device);
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			continue;
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Add device to the end of the list */
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (lastDevice)
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			lastDevice->pNext = device;
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			gpDeviceList = device;
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		lastDevice = device;
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	result = IOObjectRelease (hidObjectIterator); /* release the iterator */
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Count the total number of devices we found */
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	device = gpDeviceList;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (device)
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		SDL_numjoysticks++;
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device = device->pNext;
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return SDL_numjoysticks;
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to get the device-dependent name of a joystick */
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallconst char *SDL_SYS_JoystickName(int index)
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *device = gpDeviceList;
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (; index > 0; index--)
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device = device->pNext;
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return device->product;
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to open a joystick for use.
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * The joystick to open is specified by the index field of the joystick.
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This should fill the nbuttons and naxes fields of the joystick structure.
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * It returns 0, or -1 if there is an error.
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint SDL_SYS_JoystickOpen(SDL_Joystick *joystick)
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *device = gpDeviceList;
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int index;
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	for (index = joystick->index; index > 0; index--)
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		device = device->pNext;
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->hwdata = device;
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->name = device->product;
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->naxes = device->axes;
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nhats = device->hats;
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nballs = 0;
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	joystick->nbuttons = device->buttons;
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return 0;
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to update the state of a joystick - called as a device poll.
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * This function shouldn't update the joystick structure directly,
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * but instead should call SDL_PrivateJoystick*() to deliver events
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * and update joystick device state.
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recDevice *device = joystick->hwdata;
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	recElement *element;
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SInt32 value, range;
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int i;
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (device->removed)  /* device was unplugged; ignore it. */
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (device->uncentered)
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			device->uncentered = 0;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Tell the app that everything is centered/unpressed... */
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (i = 0; i < device->axes; i++)
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateJoystickAxis(joystick, i, 0);
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (i = 0; i < device->buttons; i++)
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateJoystickButton(joystick, i, 0);
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			for (i = 0; i < device->hats; i++)
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateJoystickHat(joystick, i, SDL_HAT_CENTERED);
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		return;
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	element = device->firstAxis;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = 0;
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (element)
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		value = HIDScaledCalibratedValue(device, element, -32768, 32767);
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( value != joystick->axes[i] )
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateJoystickAxis(joystick, i, value);
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		element = element->pNext;
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++i;
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	element = device->firstButton;
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = 0;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (element)
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		value = HIDGetElementValue(device, element);
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (value > 1)  /* handle pressure-sensitive buttons */
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            value = 1;
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( value != joystick->buttons[i] )
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateJoystickButton(joystick, i, value);
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		element = element->pNext;
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++i;
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	element = device->firstHat;
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	i = 0;
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (element)
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		Uint8 pos = 0;
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		range = (element->max - element->min + 1);
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		value = HIDGetElementValue(device, element) - element->min;
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (range == 4) /* 4 position hatswitch - scale up value */
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			value *= 2;
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		else if (range != 8) /* Neither a 4 nor 8 positions - fall back to default position (centered) */
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			value = -1;
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		switch(value)
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 0:
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_UP;
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 1:
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_RIGHTUP;
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 2:
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_RIGHT;
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 3:
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_RIGHTDOWN;
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 4:
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_DOWN;
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 5:
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_LEFTDOWN;
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 6:
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_LEFT;
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 7:
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_LEFTUP;
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			default:
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Every other value is mapped to center. We do that because some
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 * joysticks use 8 and some 15 for this value, and apparently
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 * there are even more variants out there - so we try to be generous.
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 */
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				pos = SDL_HAT_CENTERED;
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if ( pos != joystick->hats[i] )
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_PrivateJoystickHat(joystick, i, pos);
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		element = element->pNext;
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		++i;
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to close a joystick after use */
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickClose(SDL_Joystick *joystick)
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* Should we do anything here? */
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Function to perform any system-specific joystick related cleanup */
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SDL_SYS_JoystickQuit(void)
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	while (NULL != gpDeviceList)
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		gpDeviceList = HIDDisposeDevice (&gpDeviceList);
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif /* SDL_JOYSTICK_IOKIT */
843