src/joystick/darwin/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 29 Dec 2007 19:45:09 +0000
changeset 2287 15a242fca6c4
parent 2120 2c835d58faad
child 2295 dbc6d1893869
permissions -rw-r--r--
Fixed bug #478

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