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