src/joystick/darwin/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 26 Feb 2004 13:45:22 +0000
changeset 858 5db50aa5bf08
parent 769 b8d311d90021
child 1077 f122afdfa025
permissions -rw-r--r--
Date: Wed, 25 Feb 2004 06:41:17 -0500
From: "Ryan C. Gordon"
Subject: Re: MacOS X bugs...

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