src/joystick/darwin/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1635 92947e3a18db
child 2097 b0048df1701a
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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))
slouken@1895
   261
        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))
slouken@1895
   265
        pElement->max = number;
slouken@1895
   266
    pElement->minReport = pElement->max;
slouken@172
   267
/*
slouken@172
   268
	TODO: maybe should handle the following stuff somehow?
slouken@172
   269
slouken@172
   270
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMinKey));
slouken@172
   271
	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
slouken@172
   272
		pElement->scaledMin = number;
slouken@172
   273
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementScaledMaxKey));
slouken@172
   274
	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
slouken@172
   275
		pElement->scaledMax = number;
slouken@172
   276
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementSizeKey));
slouken@172
   277
	if (refType && CFNumberGetValue (refType, kCFNumberLongType, &number))
slouken@172
   278
		pElement->size = number;
slouken@172
   279
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsRelativeKey));
slouken@172
   280
	if (refType)
slouken@172
   281
		pElement->relative = CFBooleanGetValue (refType);
slouken@172
   282
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsWrappingKey));
slouken@172
   283
	if (refType)
slouken@172
   284
		pElement->wrapping = CFBooleanGetValue (refType);
slouken@172
   285
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementIsNonLinearKey));
slouken@172
   286
	if (refType)
slouken@172
   287
		pElement->nonLinear = CFBooleanGetValue (refType);
slouken@172
   288
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasPreferedStateKey));
slouken@172
   289
	if (refType)
slouken@172
   290
		pElement->preferredState = CFBooleanGetValue (refType);
slouken@172
   291
	refType = CFDictionaryGetValue (refElement, CFSTR(kIOHIDElementHasNullStateKey));
slouken@172
   292
	if (refType)
slouken@172
   293
		pElement->nullState = CFBooleanGetValue (refType);
slouken@172
   294
*/
slouken@1895
   295
}
slouken@172
   296
slouken@172
   297
/* examines CF dictionary vlaue in device element hierarchy to determine if it is element of interest or a collection of more elements
slouken@172
   298
 * if element of interest allocate storage, add to list and retrieve element specific info
slouken@172
   299
 * if collection then pass on to deconstruction collection into additional individual elements
slouken@172
   300
 */
slouken@172
   301
slouken@1895
   302
static void
slouken@1895
   303
HIDAddElement(CFTypeRef refElement, recDevice * pDevice)
slouken@172
   304
{
slouken@1895
   305
    recElement *element = NULL;
slouken@1895
   306
    recElement **headElement = NULL;
slouken@1895
   307
    long elementType, usagePage, usage;
slouken@1895
   308
    CFTypeRef refElementType =
slouken@1895
   309
        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementTypeKey));
slouken@1895
   310
    CFTypeRef refUsagePage =
slouken@1895
   311
        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsagePageKey));
slouken@1895
   312
    CFTypeRef refUsage =
slouken@1895
   313
        CFDictionaryGetValue(refElement, CFSTR(kIOHIDElementUsageKey));
slouken@172
   314
slouken@172
   315
slouken@1895
   316
    if ((refElementType)
slouken@1895
   317
        &&
slouken@1895
   318
        (CFNumberGetValue(refElementType, kCFNumberLongType, &elementType))) {
slouken@1895
   319
        /* look at types of interest */
slouken@1895
   320
        if ((elementType == kIOHIDElementTypeInput_Misc)
slouken@1895
   321
            || (elementType == kIOHIDElementTypeInput_Button)
slouken@1895
   322
            || (elementType == kIOHIDElementTypeInput_Axis)) {
slouken@1895
   323
            if (refUsagePage
slouken@1895
   324
                && CFNumberGetValue(refUsagePage, kCFNumberLongType,
slouken@1895
   325
                                    &usagePage) && refUsage
slouken@1895
   326
                && CFNumberGetValue(refUsage, kCFNumberLongType, &usage)) {
slouken@1895
   327
                switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
slouken@1895
   328
                case kHIDPage_GenericDesktop:
slouken@1895
   329
                    {
slouken@1895
   330
                        switch (usage) {        /* look at usage to determine function */
slouken@1895
   331
                        case kHIDUsage_GD_X:
slouken@1895
   332
                        case kHIDUsage_GD_Y:
slouken@1895
   333
                        case kHIDUsage_GD_Z:
slouken@1895
   334
                        case kHIDUsage_GD_Rx:
slouken@1895
   335
                        case kHIDUsage_GD_Ry:
slouken@1895
   336
                        case kHIDUsage_GD_Rz:
slouken@1895
   337
                        case kHIDUsage_GD_Slider:
slouken@1895
   338
                        case kHIDUsage_GD_Dial:
slouken@1895
   339
                        case kHIDUsage_GD_Wheel:
slouken@1895
   340
                            element = (recElement *)
slouken@1895
   341
                                NewPtrClear(sizeof(recElement));
slouken@1895
   342
                            if (element) {
slouken@1895
   343
                                pDevice->axes++;
slouken@1895
   344
                                headElement = &(pDevice->firstAxis);
slouken@1895
   345
                            }
slouken@1895
   346
                            break;
slouken@1895
   347
                        case kHIDUsage_GD_Hatswitch:
slouken@1895
   348
                            element = (recElement *)
slouken@1895
   349
                                NewPtrClear(sizeof(recElement));
slouken@1895
   350
                            if (element) {
slouken@1895
   351
                                pDevice->hats++;
slouken@1895
   352
                                headElement = &(pDevice->firstHat);
slouken@1895
   353
                            }
slouken@1895
   354
                            break;
slouken@1895
   355
                        }
slouken@1895
   356
                    }
slouken@1895
   357
                    break;
slouken@1895
   358
                case kHIDPage_Button:
slouken@1895
   359
                    element = (recElement *)
slouken@1895
   360
                        NewPtrClear(sizeof(recElement));
slouken@1895
   361
                    if (element) {
slouken@1895
   362
                        pDevice->buttons++;
slouken@1895
   363
                        headElement = &(pDevice->firstButton);
slouken@1895
   364
                    }
slouken@1895
   365
                    break;
slouken@1895
   366
                default:
slouken@1895
   367
                    break;
slouken@1895
   368
                }
slouken@1895
   369
            }
slouken@1895
   370
        } else if (kIOHIDElementTypeCollection == elementType)
slouken@1895
   371
            HIDGetCollectionElements((CFMutableDictionaryRef) refElement,
slouken@1895
   372
                                     pDevice);
slouken@1895
   373
    }
slouken@172
   374
slouken@1895
   375
    if (element && headElement) {       /* add to list */
slouken@1895
   376
        pDevice->elements++;
slouken@1895
   377
        if (NULL == *headElement)
slouken@1895
   378
            *headElement = element;
slouken@1895
   379
        else {
slouken@1895
   380
            recElement *elementPrevious, *elementCurrent;
slouken@1895
   381
            elementCurrent = *headElement;
slouken@1895
   382
            while (elementCurrent) {
slouken@1895
   383
                elementPrevious = elementCurrent;
slouken@1895
   384
                elementCurrent = elementPrevious->pNext;
slouken@1895
   385
            }
slouken@1895
   386
            elementPrevious->pNext = element;
slouken@1895
   387
        }
slouken@1895
   388
        element->pNext = NULL;
slouken@1895
   389
        HIDGetElementInfo(refElement, element);
slouken@1895
   390
    }
slouken@172
   391
}
slouken@172
   392
slouken@172
   393
/* collects information from each array member in device element list (each array memeber = element) */
slouken@172
   394
slouken@1895
   395
static void
slouken@1895
   396
HIDGetElementsCFArrayHandler(const void *value, void *parameter)
slouken@172
   397
{
slouken@1895
   398
    if (CFGetTypeID(value) == CFDictionaryGetTypeID())
slouken@1895
   399
        HIDAddElement((CFTypeRef) value, (recDevice *) parameter);
slouken@172
   400
}
slouken@172
   401
slouken@172
   402
/* handles retrieval of element information from arrays of elements in device IO registry information */
slouken@172
   403
slouken@1895
   404
static void
slouken@1895
   405
HIDGetElements(CFTypeRef refElementCurrent, recDevice * pDevice)
slouken@172
   406
{
slouken@1895
   407
    CFTypeID type = CFGetTypeID(refElementCurrent);
slouken@1895
   408
    if (type == CFArrayGetTypeID()) {   /* if element is an array */
slouken@1895
   409
        CFRange range = { 0, CFArrayGetCount(refElementCurrent) };
slouken@1895
   410
        /* CountElementsCFArrayHandler called for each array member */
slouken@1895
   411
        CFArrayApplyFunction(refElementCurrent, range,
slouken@1895
   412
                             HIDGetElementsCFArrayHandler, pDevice);
slouken@1895
   413
    }
slouken@1895
   414
}
slouken@172
   415
slouken@172
   416
/* handles extracting element information from element collection CF types
slouken@172
   417
 * used from top level element decoding and hierarchy deconstruction to flatten device element list
slouken@172
   418
 */
slouken@172
   419
slouken@1895
   420
static void
slouken@1895
   421
HIDGetCollectionElements(CFMutableDictionaryRef deviceProperties,
slouken@1895
   422
                         recDevice * pDevice)
slouken@172
   423
{
slouken@1895
   424
    CFTypeRef refElementTop =
slouken@1895
   425
        CFDictionaryGetValue(deviceProperties, CFSTR(kIOHIDElementKey));
slouken@1895
   426
    if (refElementTop)
slouken@1895
   427
        HIDGetElements(refElementTop, pDevice);
slouken@172
   428
}
slouken@172
   429
slouken@172
   430
/* use top level element usage page and usage to discern device usage page and usage setting appropriate vlaues in device record */
slouken@172
   431
slouken@1895
   432
static void
slouken@1895
   433
HIDTopLevelElementHandler(const void *value, void *parameter)
slouken@172
   434
{
slouken@1895
   435
    CFTypeRef refCF = 0;
slouken@1895
   436
    if (CFGetTypeID(value) != CFDictionaryGetTypeID())
slouken@1895
   437
        return;
slouken@1895
   438
    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsagePageKey));
slouken@1895
   439
    if (!CFNumberGetValue
slouken@1895
   440
        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usagePage))
slouken@1895
   441
        SDL_SetError("CFNumberGetValue error retrieving pDevice->usagePage.");
slouken@1895
   442
    refCF = CFDictionaryGetValue(value, CFSTR(kIOHIDElementUsageKey));
slouken@1895
   443
    if (!CFNumberGetValue
slouken@1895
   444
        (refCF, kCFNumberLongType, &((recDevice *) parameter)->usage))
slouken@1895
   445
        SDL_SetError("CFNumberGetValue error retrieving pDevice->usage.");
slouken@172
   446
}
slouken@172
   447
slouken@172
   448
/* extracts device info from CF dictionary records in IO registry */
slouken@172
   449
slouken@1895
   450
static void
slouken@1895
   451
HIDGetDeviceInfo(io_object_t hidDevice, CFMutableDictionaryRef hidProperties,
slouken@1895
   452
                 recDevice * pDevice)
slouken@172
   453
{
slouken@1895
   454
    CFMutableDictionaryRef usbProperties = 0;
slouken@1895
   455
    io_registry_entry_t parent1, parent2;
slouken@172
   456
slouken@1895
   457
    /* Mac OS X currently is not mirroring all USB properties to HID page so need to look at USB device page also
slouken@1895
   458
     * get dictionary for usb properties: step up two levels and get CF dictionary for USB properties
slouken@1895
   459
     */
slouken@1895
   460
    if ((KERN_SUCCESS ==
slouken@1895
   461
         IORegistryEntryGetParentEntry(hidDevice, kIOServicePlane, &parent1))
slouken@1895
   462
        && (KERN_SUCCESS ==
slouken@1895
   463
            IORegistryEntryGetParentEntry(parent1, kIOServicePlane, &parent2))
slouken@1895
   464
        && (KERN_SUCCESS ==
slouken@1895
   465
            IORegistryEntryCreateCFProperties(parent2, &usbProperties,
slouken@1895
   466
                                              kCFAllocatorDefault,
slouken@1895
   467
                                              kNilOptions))) {
slouken@1895
   468
        if (usbProperties) {
slouken@1895
   469
            CFTypeRef refCF = 0;
slouken@1895
   470
            /* get device info
slouken@1895
   471
             * try hid dictionary first, if fail then go to usb dictionary
slouken@1895
   472
             */
slouken@172
   473
slouken@172
   474
slouken@1895
   475
            /* get product name */
slouken@1895
   476
            refCF =
slouken@1895
   477
                CFDictionaryGetValue(hidProperties, CFSTR(kIOHIDProductKey));
slouken@1895
   478
            if (!refCF)
slouken@1895
   479
                refCF =
slouken@1895
   480
                    CFDictionaryGetValue(usbProperties,
slouken@1895
   481
                                         CFSTR("USB Product Name"));
slouken@1895
   482
            if (refCF) {
slouken@1895
   483
                if (!CFStringGetCString
slouken@1895
   484
                    (refCF, pDevice->product, 256,
slouken@1895
   485
                     CFStringGetSystemEncoding()))
slouken@1895
   486
                    SDL_SetError
slouken@1895
   487
                        ("CFStringGetCString error retrieving pDevice->product.");
slouken@1895
   488
            }
slouken@1895
   489
slouken@1895
   490
            /* get usage page and usage */
slouken@1895
   491
            refCF =
slouken@1895
   492
                CFDictionaryGetValue(hidProperties,
slouken@1895
   493
                                     CFSTR(kIOHIDPrimaryUsagePageKey));
slouken@1895
   494
            if (refCF) {
slouken@1895
   495
                if (!CFNumberGetValue
slouken@1895
   496
                    (refCF, kCFNumberLongType, &pDevice->usagePage))
slouken@1895
   497
                    SDL_SetError
slouken@1895
   498
                        ("CFNumberGetValue error retrieving pDevice->usagePage.");
slouken@1895
   499
                refCF =
slouken@1895
   500
                    CFDictionaryGetValue(hidProperties,
slouken@1895
   501
                                         CFSTR(kIOHIDPrimaryUsageKey));
slouken@1895
   502
                if (refCF)
slouken@1895
   503
                    if (!CFNumberGetValue
slouken@1895
   504
                        (refCF, kCFNumberLongType, &pDevice->usage))
slouken@1895
   505
                        SDL_SetError
slouken@1895
   506
                            ("CFNumberGetValue error retrieving pDevice->usage.");
slouken@1895
   507
            }
slouken@1895
   508
slouken@1895
   509
            if (NULL == refCF) {        /* get top level element HID usage page or usage */
slouken@1895
   510
                /* use top level element instead */
slouken@1895
   511
                CFTypeRef refCFTopElement = 0;
slouken@1895
   512
                refCFTopElement =
slouken@1895
   513
                    CFDictionaryGetValue(hidProperties,
slouken@1895
   514
                                         CFSTR(kIOHIDElementKey));
slouken@1895
   515
                {
slouken@1895
   516
                    /* refCFTopElement points to an array of element dictionaries */
slouken@1895
   517
                    CFRange range = { 0, CFArrayGetCount(refCFTopElement) };
slouken@1895
   518
                    CFArrayApplyFunction(refCFTopElement, range,
slouken@1895
   519
                                         HIDTopLevelElementHandler, pDevice);
slouken@1895
   520
                }
slouken@1895
   521
            }
slouken@1895
   522
slouken@1895
   523
            CFRelease(usbProperties);
slouken@1895
   524
        } else
slouken@1895
   525
            SDL_SetError
slouken@1895
   526
                ("IORegistryEntryCreateCFProperties failed to create usbProperties.");
slouken@1895
   527
slouken@1895
   528
        if (kIOReturnSuccess != IOObjectRelease(parent2))
slouken@1895
   529
            SDL_SetError("IOObjectRelease error with parent2.");
slouken@1895
   530
        if (kIOReturnSuccess != IOObjectRelease(parent1))
slouken@1895
   531
            SDL_SetError("IOObjectRelease error with parent1.");
slouken@1895
   532
    }
slouken@172
   533
}
slouken@172
   534
slouken@172
   535
slouken@1895
   536
static recDevice *
slouken@1895
   537
HIDBuildDevice(io_object_t hidDevice)
slouken@172
   538
{
slouken@1895
   539
    recDevice *pDevice = (recDevice *) NewPtrClear(sizeof(recDevice));
slouken@1895
   540
    if (pDevice) {
slouken@1895
   541
        /* get dictionary for HID properties */
slouken@1895
   542
        CFMutableDictionaryRef hidProperties = 0;
slouken@1895
   543
        kern_return_t result =
slouken@1895
   544
            IORegistryEntryCreateCFProperties(hidDevice, &hidProperties,
slouken@1895
   545
                                              kCFAllocatorDefault,
slouken@1895
   546
                                              kNilOptions);
slouken@1895
   547
        if ((result == KERN_SUCCESS) && hidProperties) {
slouken@1895
   548
            /* create device interface */
slouken@1895
   549
            result = HIDCreateOpenDeviceInterface(hidDevice, pDevice);
slouken@1895
   550
            if (kIOReturnSuccess == result) {
slouken@1895
   551
                HIDGetDeviceInfo(hidDevice, hidProperties, pDevice);    /* hidDevice used to find parents in registry tree */
slouken@1895
   552
                HIDGetCollectionElements(hidProperties, pDevice);
slouken@1895
   553
            } else {
slouken@1895
   554
                DisposePtr((Ptr) pDevice);
slouken@1895
   555
                pDevice = NULL;
slouken@1895
   556
            }
slouken@1895
   557
            CFRelease(hidProperties);
slouken@1895
   558
        } else {
slouken@1895
   559
            DisposePtr((Ptr) pDevice);
slouken@1895
   560
            pDevice = NULL;
slouken@1895
   561
        }
slouken@1895
   562
    }
slouken@1895
   563
    return pDevice;
slouken@172
   564
}
slouken@172
   565
slouken@172
   566
/* disposes of the element list associated with a device and the memory associated with the list
slouken@172
   567
 */
slouken@172
   568
slouken@1895
   569
static void
slouken@1895
   570
HIDDisposeElementList(recElement ** elementList)
slouken@172
   571
{
slouken@1895
   572
    recElement *pElement = *elementList;
slouken@1895
   573
    while (pElement) {
slouken@1895
   574
        recElement *pElementNext = pElement->pNext;
slouken@1895
   575
        DisposePtr((Ptr) pElement);
slouken@1895
   576
        pElement = pElementNext;
slouken@1895
   577
    }
slouken@1895
   578
    *elementList = NULL;
slouken@172
   579
}
slouken@172
   580
slouken@172
   581
/* disposes of a single device, closing and releaseing interface, freeing memory fro device and elements, setting device pointer to NULL
slouken@172
   582
 * all your device no longer belong to us... (i.e., you do not 'own' the device anymore)
slouken@172
   583
 */
slouken@172
   584
slouken@1895
   585
static recDevice *
slouken@1895
   586
HIDDisposeDevice(recDevice ** ppDevice)
slouken@172
   587
{
slouken@1895
   588
    kern_return_t result = KERN_SUCCESS;
slouken@1895
   589
    recDevice *pDeviceNext = NULL;
slouken@1895
   590
    if (*ppDevice) {
slouken@1895
   591
        /* save next device prior to disposing of this device */
slouken@1895
   592
        pDeviceNext = (*ppDevice)->pNext;
slouken@1895
   593
slouken@1895
   594
        /* free element lists */
slouken@1895
   595
        HIDDisposeElementList(&(*ppDevice)->firstAxis);
slouken@1895
   596
        HIDDisposeElementList(&(*ppDevice)->firstButton);
slouken@1895
   597
        HIDDisposeElementList(&(*ppDevice)->firstHat);
slouken@1895
   598
slouken@1895
   599
        result = HIDCloseReleaseInterface(*ppDevice);   /* function sanity checks interface value (now application does not own device) */
slouken@1895
   600
        if (kIOReturnSuccess != result)
slouken@1895
   601
            HIDReportErrorNum
slouken@1895
   602
                ("HIDCloseReleaseInterface failed when trying to dipose device.",
slouken@1895
   603
                 result);
slouken@1895
   604
        DisposePtr((Ptr) * ppDevice);
slouken@1895
   605
        *ppDevice = NULL;
slouken@1895
   606
    }
slouken@1895
   607
    return pDeviceNext;
slouken@172
   608
}
slouken@172
   609
slouken@172
   610
slouken@172
   611
/* Function to scan the system for joysticks.
slouken@172
   612
 * Joystick 0 should be the system default joystick.
slouken@172
   613
 * This function should return the number of available joysticks, or -1
slouken@172
   614
 * on an unrecoverable fatal error.
slouken@172
   615
 */
slouken@1895
   616
int
slouken@1895
   617
SDL_SYS_JoystickInit(void)
slouken@172
   618
{
slouken@1895
   619
    IOReturn result = kIOReturnSuccess;
slouken@1895
   620
    mach_port_t masterPort = 0;
slouken@1895
   621
    io_iterator_t hidObjectIterator = 0;
slouken@1895
   622
    CFMutableDictionaryRef hidMatchDictionary = NULL;
slouken@1895
   623
    recDevice *device, *lastDevice;
slouken@1895
   624
    io_object_t ioHIDDeviceObject = 0;
slouken@172
   625
slouken@1895
   626
    SDL_numjoysticks = 0;
slouken@172
   627
slouken@1895
   628
    if (gpDeviceList) {
slouken@1895
   629
        SDL_SetError("Joystick: Device list already inited.");
slouken@1895
   630
        return -1;
slouken@1895
   631
    }
slouken@172
   632
slouken@1895
   633
    result = IOMasterPort(bootstrap_port, &masterPort);
slouken@1895
   634
    if (kIOReturnSuccess != result) {
slouken@1895
   635
        SDL_SetError("Joystick: IOMasterPort error with bootstrap_port.");
slouken@1895
   636
        return -1;
slouken@1895
   637
    }
slouken@172
   638
slouken@1895
   639
    /* Set up a matching dictionary to search I/O Registry by class name for all HID class devices. */
slouken@1895
   640
    hidMatchDictionary = IOServiceMatching(kIOHIDDeviceKey);
slouken@1895
   641
    if (hidMatchDictionary) {
slouken@1895
   642
        /* Add key for device type (joystick, in this case) to refine the matching dictionary. */
slouken@172
   643
slouken@1895
   644
        /* NOTE: we now perform this filtering later
slouken@1895
   645
           UInt32 usagePage = kHIDPage_GenericDesktop;
slouken@1895
   646
           UInt32 usage = kHIDUsage_GD_Joystick;
slouken@1895
   647
           CFNumberRef refUsage = NULL, refUsagePage = NULL;
slouken@1895
   648
slouken@1895
   649
           refUsage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usage);
slouken@1895
   650
           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsageKey), refUsage);
slouken@1895
   651
           refUsagePage = CFNumberCreate (kCFAllocatorDefault, kCFNumberIntType, &usagePage);
slouken@1895
   652
           CFDictionarySetValue (hidMatchDictionary, CFSTR (kIOHIDPrimaryUsagePageKey), refUsagePage);
slouken@1895
   653
         */
slouken@1895
   654
    } else {
slouken@1895
   655
        SDL_SetError
slouken@1895
   656
            ("Joystick: Failed to get HID CFMutableDictionaryRef via IOServiceMatching.");
slouken@1895
   657
        return -1;
slouken@1895
   658
    }
slouken@1895
   659
slouken@1895
   660
    /*/ Now search I/O Registry for matching devices. */
slouken@1895
   661
    result =
slouken@1895
   662
        IOServiceGetMatchingServices(masterPort, hidMatchDictionary,
slouken@1895
   663
                                     &hidObjectIterator);
slouken@1895
   664
    /* Check for errors */
slouken@1895
   665
    if (kIOReturnSuccess != result) {
slouken@1895
   666
        SDL_SetError("Joystick: Couldn't create a HID object iterator.");
slouken@1895
   667
        return -1;
slouken@1895
   668
    }
slouken@1895
   669
    if (!hidObjectIterator) {   /* there are no joysticks */
slouken@1895
   670
        gpDeviceList = NULL;
slouken@1895
   671
        SDL_numjoysticks = 0;
slouken@1895
   672
        return 0;
slouken@1895
   673
    }
slouken@1895
   674
    /* IOServiceGetMatchingServices consumes a reference to the dictionary, so we don't need to release the dictionary ref. */
slouken@1895
   675
slouken@1895
   676
    /* build flat linked list of devices from device iterator */
slouken@1895
   677
slouken@1895
   678
    gpDeviceList = lastDevice = NULL;
slouken@1895
   679
slouken@1895
   680
    while ((ioHIDDeviceObject = IOIteratorNext(hidObjectIterator))) {
slouken@1895
   681
        /* build a device record */
slouken@1895
   682
        device = HIDBuildDevice(ioHIDDeviceObject);
slouken@1895
   683
        if (!device)
slouken@1895
   684
            continue;
slouken@1895
   685
slouken@1895
   686
        /* dump device object, it is no longer needed */
slouken@1895
   687
        result = IOObjectRelease(ioHIDDeviceObject);
slouken@1487
   688
/*		if (KERN_SUCCESS != result)
slouken@1487
   689
			HIDReportErrorNum ("IOObjectRelease error with ioHIDDeviceObject.", result);
slouken@1487
   690
*/
slouken@495
   691
slouken@1895
   692
        /* Filter device list to non-keyboard/mouse stuff */
slouken@1895
   693
        if ((device->usagePage != kHIDPage_GenericDesktop) ||
slouken@1895
   694
            ((device->usage != kHIDUsage_GD_Joystick &&
slouken@1895
   695
              device->usage != kHIDUsage_GD_GamePad))) {
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: */