src/joystick/darwin/SDL_sysjoystick.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Aug 2016 12:17:53 -0700
changeset 10273 42768c568a50
parent 10176 3115d38204aa
child 10281 cb13d22b7f09
permissions -rw-r--r--
[ OSX ] enables Xbox One S Guide (System Main Menu) button detection. the one possible impact is that since button identifiers on OSX are just their sequence in the HID report descriptor we might change the button order, and any existing/saved mappings may get their order changed.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9976
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     5
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     7
  arising from the use of this software.
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    11
  freely, subject to the following restrictions:
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    14
     claim that you wrote the original software. If you use this software
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    16
     appreciated but is not required.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    18
     misrepresented as being the original software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5262
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 7947
diff changeset
    21
#include "../../SDL_internal.h"
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
    23
#ifdef SDL_JOYSTICK_IOKIT
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
    24
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
#include <IOKit/hid/IOHIDLib.h>
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
    27
/* For force feedback testing. */
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
    28
#include <ForceFeedback/ForceFeedback.h>
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
    29
#include <ForceFeedback/ForceFeedbackConstants.h>
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
    30
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
#include "SDL_joystick.h"
1361
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    32
#include "../SDL_sysjoystick.h"
19418e4422cb New configure-based build system. Still work in progress, but much improved
Sam Lantinga <slouken@libsdl.org>
parents: 1358
diff changeset
    33
#include "../SDL_joystick_c.h"
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
    34
#include "SDL_sysjoystick_c.h"
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    35
#include "SDL_events.h"
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
    36
#include "../../haptic/darwin/SDL_syshaptic_c.h"    /* For haptic hot plugging */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    37
#if !SDL_EVENTS_DISABLED
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    38
#include "../../events/SDL_events_c.h"
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    39
#endif
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    41
#define SDL_JOYSTICK_RUNLOOP_MODE CFSTR("SDLJoystick")
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    42
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    43
/* The base object of the HID Manager API */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    44
static IOHIDManagerRef hidman = NULL;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
/* Linked list of all available devices */
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
static recDevice *gpDeviceList = NULL;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    48
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    49
/* static incrementing counter for new joystick devices seen on the system. Devices should start with index 0 */
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    50
static int s_joystick_instance_id = -1;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    52
static recDevice *GetDeviceForIndex(int device_index)
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    53
{
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    54
    recDevice *device = gpDeviceList;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    55
    while (device) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    56
        if (!device->removed) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    57
            if (device_index == 0)
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    58
                break;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    59
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    60
            --device_index;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    61
        }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    62
        device = device->pNext;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    63
    }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    64
    return device;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
    65
}
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    66
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
    67
static void
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    68
FreeElementList(recElement *pElement)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    70
    while (pElement) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    71
        recElement *pElementNext = pElement->pNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    72
        SDL_free(pElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    73
        pElement = pElementNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    74
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    77
static recDevice *
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    78
FreeDevice(recDevice *removeDevice)
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    79
{
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    80
    recDevice *pDeviceNext = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    81
    if (removeDevice) {
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    82
        if (removeDevice->deviceRef) {
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    83
            IOHIDDeviceUnscheduleFromRunLoop(removeDevice->deviceRef, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    84
            removeDevice->deviceRef = NULL;
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    85
        }
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
    86
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    87
        /* save next device prior to disposing of this device */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    88
        pDeviceNext = removeDevice->pNext;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    90
        if ( gpDeviceList == removeDevice ) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    91
            gpDeviceList = pDeviceNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    92
        } else {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    93
            recDevice *device = gpDeviceList;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    94
            while (device->pNext != removeDevice) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    95
                device = device->pNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    96
            }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    97
            device->pNext = pDeviceNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    98
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    99
        removeDevice->pNext = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   100
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   101
        /* free element lists */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   102
        FreeElementList(removeDevice->firstAxis);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   103
        FreeElementList(removeDevice->firstButton);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   104
        FreeElementList(removeDevice->firstHat);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   105
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   106
        SDL_free(removeDevice);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   107
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   108
    return pDeviceNext;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   109
}
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   111
static SInt32
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   112
GetHIDElementState(recDevice *pDevice, recElement *pElement)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   114
    SInt32 value = 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   116
    if (pDevice && pElement) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   117
        IOHIDValueRef valueRef;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   118
        if (IOHIDDeviceGetValue(pDevice->deviceRef, pElement->elementRef, &valueRef) == kIOReturnSuccess) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   119
            value = (SInt32) IOHIDValueGetIntegerValue(valueRef);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   120
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   121
            /* record min and max for auto calibration */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   122
            if (value < pElement->minReport) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   123
                pElement->minReport = value;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   124
            }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   125
            if (value > pElement->maxReport) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   126
                pElement->maxReport = value;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   127
            }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   128
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   129
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   130
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   131
    return value;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   132
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   133
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   134
static SInt32
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   135
GetHIDScaledCalibratedState(recDevice * pDevice, recElement * pElement, SInt32 min, SInt32 max)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   136
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   137
    const float deviceScale = max - min;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   138
    const float readScale = pElement->maxReport - pElement->minReport;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   139
    const SInt32 value = GetHIDElementState(pDevice, pElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   140
    if (readScale == 0) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   141
        return value;           /* no scaling at all */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   142
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   143
    return ((value - pElement->minReport) * deviceScale / readScale) + min;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   146
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   147
static void
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   148
JoystickDeviceWasRemovedCallback(void *ctx, IOReturn result, void *sender)
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   149
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   150
    recDevice *device = (recDevice *) ctx;
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   151
    device->removed = SDL_TRUE;
8858
a13c7a84c57f - fixed crash if you removed a device twice, the deviceRef is invalid if removed from the removed device callback (added in http://hg.libsdl.org/SDL/rev/153077041e4b ).
Alfred Reynolds <alfred@valvesoftware.com>
parents: 8739
diff changeset
   152
    device->deviceRef = NULL; // deviceRef was invalidated due to the remove
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   153
#if SDL_HAPTIC_IOKIT
8176
9efac2fd6ed6 Cleanup some vi footer comments, rename new PRIVATE_* funcs to MacHaptic_*.
Ryan C. Gordon <icculus@icculus.org>
parents: 8173
diff changeset
   154
    MacHaptic_MaybeRemoveDevice(device->ffservice);
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   155
#endif
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   156
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   157
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   158
#if !SDL_EVENTS_DISABLED
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   159
    {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   160
        SDL_Event event;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   161
        event.type = SDL_JOYDEVICEREMOVED;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   162
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   163
        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   164
            event.jdevice.which = device->instance_id;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   165
            if ((SDL_EventOK == NULL)
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   166
                || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   167
                SDL_PushEvent(&event);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   168
            }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   169
        }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   170
    }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   171
#endif /* !SDL_EVENTS_DISABLED */
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   172
}
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   173
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   174
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   175
static void AddHIDElement(const void *value, void *parameter);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   176
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   177
/* Call AddHIDElement() on all elements in an array of IOHIDElementRefs */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   178
static void
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   179
AddHIDElements(CFArrayRef array, recDevice *pDevice)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   181
    const CFRange range = { 0, CFArrayGetCount(array) };
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   182
    CFArrayApplyFunction(array, range, AddHIDElement, pDevice);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   183
}
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   185
static SDL_bool
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   186
ElementAlreadyAdded(const IOHIDElementCookie cookie, const recElement *listitem) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   187
    while (listitem) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   188
        if (listitem->cookie == cookie) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   189
            return SDL_TRUE;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   190
        }
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   191
        listitem = listitem->pNext;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   192
    }
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   193
    return SDL_FALSE;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   194
}
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   195
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   196
/* See if we care about this HID element, and if so, note it in our recDevice. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   197
static void
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   198
AddHIDElement(const void *value, void *parameter)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   199
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   200
    recDevice *pDevice = (recDevice *) parameter;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   201
    IOHIDElementRef refElement = (IOHIDElementRef) value;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   202
    const CFTypeID elementTypeID = refElement ? CFGetTypeID(refElement) : 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   204
    if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   205
        const IOHIDElementCookie cookie = IOHIDElementGetCookie(refElement);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   206
        const uint32_t usagePage = IOHIDElementGetUsagePage(refElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   207
        const uint32_t usage = IOHIDElementGetUsage(refElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   208
        recElement *element = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   209
        recElement **headElement = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   210
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   211
        /* look at types of interest */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   212
        switch (IOHIDElementGetType(refElement)) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   213
            case kIOHIDElementTypeInput_Misc:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   214
            case kIOHIDElementTypeInput_Button:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   215
            case kIOHIDElementTypeInput_Axis: {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   216
                switch (usagePage) {    /* only interested in kHIDPage_GenericDesktop and kHIDPage_Button */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   217
                    case kHIDPage_GenericDesktop:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   218
                        switch (usage) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   219
                            case kHIDUsage_GD_X:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   220
                            case kHIDUsage_GD_Y:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   221
                            case kHIDUsage_GD_Z:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   222
                            case kHIDUsage_GD_Rx:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   223
                            case kHIDUsage_GD_Ry:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   224
                            case kHIDUsage_GD_Rz:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   225
                            case kHIDUsage_GD_Slider:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   226
                            case kHIDUsage_GD_Dial:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   227
                            case kHIDUsage_GD_Wheel:
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   228
                                if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   229
                                    element = (recElement *) SDL_calloc(1, sizeof (recElement));
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   230
                                    if (element) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   231
                                        pDevice->axes++;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   232
                                        headElement = &(pDevice->firstAxis);
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   233
                                    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   234
                                }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   235
                                break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   236
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   237
                            case kHIDUsage_GD_Hatswitch:
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   238
                                if (!ElementAlreadyAdded(cookie, pDevice->firstHat)) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   239
                                    element = (recElement *) SDL_calloc(1, sizeof (recElement));
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   240
                                    if (element) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   241
                                        pDevice->hats++;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   242
                                        headElement = &(pDevice->firstHat);
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   243
                                    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   244
                                }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   245
                                break;
9912
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   246
                            case kHIDUsage_GD_DPadUp:
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   247
                            case kHIDUsage_GD_DPadDown:
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   248
                            case kHIDUsage_GD_DPadRight:
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   249
                            case kHIDUsage_GD_DPadLeft:
9976
9637657107a3 Mac: expose joystick buttons that report themselves as having 'Start' and 'Select' HID usages.
Alex Szpakowski <slime73@gmail.com>
parents: 9912
diff changeset
   250
                            case kHIDUsage_GD_Start:
9637657107a3 Mac: expose joystick buttons that report themselves as having 'Start' and 'Select' HID usages.
Alex Szpakowski <slime73@gmail.com>
parents: 9912
diff changeset
   251
                            case kHIDUsage_GD_Select:
10273
42768c568a50 [ OSX ] enables Xbox One S Guide (System Main Menu) button detection. the one possible impact is that since button identifiers on OSX are just their sequence in the HID report descriptor we might change the button order, and any existing/saved mappings may get their order changed.
Sam Lantinga <slouken@libsdl.org>
parents: 10176
diff changeset
   252
                            case kHIDUsage_GD_SystemMainMenu:
9912
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   253
                                if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   254
                                    element = (recElement *) SDL_calloc(1, sizeof (recElement));
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   255
                                    if (element) {
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   256
                                        pDevice->buttons++;
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   257
                                        headElement = &(pDevice->firstButton);
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   258
                                    }
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   259
                                }
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   260
                                break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   261
                        }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   262
                        break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   263
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   264
                    case kHIDPage_Simulation:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   265
                        switch (usage) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   266
                            case kHIDUsage_Sim_Rudder:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   267
                            case kHIDUsage_Sim_Throttle:
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   268
                                if (!ElementAlreadyAdded(cookie, pDevice->firstAxis)) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   269
                                    element = (recElement *) SDL_calloc(1, sizeof (recElement));
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   270
                                    if (element) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   271
                                        pDevice->axes++;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   272
                                        headElement = &(pDevice->firstAxis);
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   273
                                    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   274
                                }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   275
                                break;
7626
36e493277423 Fixed bug 2024 - Update OSX Joystick code to fully support Saitek p2500 gamepad
Sam Lantinga <slouken@libsdl.org>
parents: 7191
diff changeset
   276
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   277
                            default:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   278
                                break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   279
                        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   280
                        break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   281
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   282
                    case kHIDPage_Button:
9912
628225876708 Mac: Detect the d-pad and pause buttons on Steelseries MFi gamepads (bug #3124.)
Alex Szpakowski <slime73@gmail.com>
parents: 9862
diff changeset
   283
                    case kHIDPage_Consumer: /* e.g. 'pause' button on Steelseries MFi gamepads. */
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   284
                        if (!ElementAlreadyAdded(cookie, pDevice->firstButton)) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   285
                            element = (recElement *) SDL_calloc(1, sizeof (recElement));
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   286
                            if (element) {
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   287
                                pDevice->buttons++;
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   288
                                headElement = &(pDevice->firstButton);
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   289
                            }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   290
                        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   291
                        break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   292
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   293
                    default:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   294
                        break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   295
                }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   296
            }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   297
            break;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   298
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   299
            case kIOHIDElementTypeCollection: {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   300
                CFArrayRef array = IOHIDElementGetChildren(refElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   301
                if (array) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   302
                    AddHIDElements(array, pDevice);
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   303
                }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   304
            }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   305
            break;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   306
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   307
            default:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   308
                break;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   309
        }
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   310
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   311
        if (element && headElement) {       /* add to list */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   312
            recElement *elementPrevious = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   313
            recElement *elementCurrent = *headElement;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   314
            while (elementCurrent && usage >= elementCurrent->usage) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   315
                elementPrevious = elementCurrent;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   316
                elementCurrent = elementCurrent->pNext;
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   317
            }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   318
            if (elementPrevious) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   319
                elementPrevious->pNext = element;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   320
            } else {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   321
                *headElement = element;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   322
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   323
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   324
            element->elementRef = refElement;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   325
            element->usagePage = usagePage;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   326
            element->usage = usage;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   327
            element->pNext = elementCurrent;
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   328
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   329
            element->minReport = element->min = (SInt32) IOHIDElementGetLogicalMin(refElement);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   330
            element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
8242
f6a2b1a6932b Mac joystick: ignore duplicate HID elements.
Ryan C. Gordon <icculus@icculus.org>
parents: 8230
diff changeset
   331
            element->cookie = IOHIDElementGetCookie(refElement);
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   332
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   333
            pDevice->elements++;
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   334
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   335
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   336
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   337
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   338
static SDL_bool
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   339
GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   340
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   341
    Uint32 *guid32 = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   342
    CFTypeRef refCF = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   343
    CFArrayRef array = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   344
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   345
    /* get usage page and usage */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   346
    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsagePageKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   347
    if (refCF) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   348
        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usagePage);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   349
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   350
    if (pDevice->usagePage != kHIDPage_GenericDesktop) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   351
        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   352
    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   353
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   354
    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDPrimaryUsageKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   355
    if (refCF) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   356
        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->usage);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   357
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   358
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   359
    if ((pDevice->usage != kHIDUsage_GD_Joystick &&
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   360
         pDevice->usage != kHIDUsage_GD_GamePad &&
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   361
         pDevice->usage != kHIDUsage_GD_MultiAxisController)) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   362
        return SDL_FALSE; /* Filter device list to non-keyboard/mouse stuff */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   363
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   364
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   365
    pDevice->deviceRef = hidDevice;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   366
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   367
    /* get device name */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   368
    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   369
    if (!refCF) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   370
        /* Maybe we can't get "AwesomeJoystick2000", but we can get "Logitech"? */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   371
        refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDManufacturerKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   372
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   373
    if ((!refCF) || (!CFStringGetCString(refCF, pDevice->product, sizeof (pDevice->product), kCFStringEncodingUTF8))) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   374
        SDL_strlcpy(pDevice->product, "Unidentified joystick", sizeof (pDevice->product));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   375
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   376
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   377
    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDVendorIDKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   378
    if (refCF) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   379
        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[0]);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   380
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   381
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   382
    refCF = IOHIDDeviceGetProperty(hidDevice, CFSTR(kIOHIDProductIDKey));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   383
    if (refCF) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   384
        CFNumberGetValue(refCF, kCFNumberSInt32Type, &pDevice->guid.data[8]);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   385
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
   386
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   387
    /* Check to make sure we have a vendor and product ID
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   388
       If we don't, use the same algorithm as the Linux code for Bluetooth devices */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   389
    guid32 = (Uint32*)pDevice->guid.data;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   390
    if (!guid32[0] && !guid32[1]) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   391
        /* If we don't have a vendor and product ID this is probably a Bluetooth device */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   392
        const Uint16 BUS_BLUETOOTH = 0x05;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   393
        Uint16 *guid16 = (Uint16 *)guid32;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   394
        *guid16++ = BUS_BLUETOOTH;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   395
        *guid16++ = 0;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   396
        SDL_strlcpy((char*)guid16, pDevice->product, sizeof(pDevice->guid.data) - 4);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   397
    }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   398
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   399
    array = IOHIDDeviceCopyMatchingElements(hidDevice, NULL, kIOHIDOptionsTypeNone);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   400
    if (array) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   401
        AddHIDElements(array, pDevice);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   402
        CFRelease(array);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   403
    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   404
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   405
    return SDL_TRUE;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   406
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   407
9120
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   408
static SDL_bool
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   409
JoystickAlreadyKnown(IOHIDDeviceRef ioHIDDeviceObject)
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   410
{
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   411
    recDevice *i;
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   412
    for (i = gpDeviceList; i != NULL; i = i->pNext) {
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   413
        if (i->deviceRef == ioHIDDeviceObject) {
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   414
            return SDL_TRUE;
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   415
        }
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   416
    }
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   417
    return SDL_FALSE;
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   418
}
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   419
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   420
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   421
static void
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   422
JoystickDeviceWasAddedCallback(void *ctx, IOReturn res, void *sender, IOHIDDeviceRef ioHIDDeviceObject)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   423
{
9120
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   424
    recDevice *device;
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   425
    int device_index = 0;
10176
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   426
    io_service_t ioservice;
9120
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   427
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   428
    if (res != kIOReturnSuccess) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   429
        return;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   430
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   431
9120
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   432
    if (JoystickAlreadyKnown(ioHIDDeviceObject)) {
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   433
        return;  /* IOKit sent us a duplicate. */
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   434
    }
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   435
b186c0df3c18 Mac: Don't add the same joystick twice if IOKit reports a duplicate device.
Ryan C. Gordon <icculus@icculus.org>
parents: 8986
diff changeset
   436
    device = (recDevice *) SDL_calloc(1, sizeof(recDevice));
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   437
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   438
    if (!device) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   439
        SDL_OutOfMemory();
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   440
        return;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   441
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   442
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   443
    if (!GetDeviceInfo(ioHIDDeviceObject, device)) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   444
        SDL_free(device);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   445
        return;   /* not a device we care about, probably. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   446
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   447
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   448
    /* Get notified when this device is disconnected. */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   449
    IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, device);
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
   450
    IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   451
7791
d9f0067a5421 Only allocate a joystick instance ID once we know it's a joystick.
Sam Lantinga <slouken@libsdl.org>
parents: 7681
diff changeset
   452
    /* Allocate an instance ID for this device */
d9f0067a5421 Only allocate a joystick instance ID once we know it's a joystick.
Sam Lantinga <slouken@libsdl.org>
parents: 7681
diff changeset
   453
    device->instance_id = ++s_joystick_instance_id;
d9f0067a5421 Only allocate a joystick instance ID once we know it's a joystick.
Sam Lantinga <slouken@libsdl.org>
parents: 7681
diff changeset
   454
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   455
    /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
10176
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   456
    ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
9432
5e8151f59e34 Mac: patched to compile with haptic subsystem disabled (thanks, Rodrigo!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9380
diff changeset
   457
#if SDL_HAPTIC_IOKIT
10176
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   458
    if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   459
        device->ffservice = ioservice;
3115d38204aa Removed Mac OS 10.5 support (bug #3137). Also fixed a warning about deprecated Carbon code when using SDL_audio (bug #3127, thanks Dominik!)
Alex Szpakowski <slime73@gmail.com>
parents: 9998
diff changeset
   460
        MacHaptic_MaybeAddDevice(ioservice);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   461
    }
9632
6ae6b0b46e78 Mac: Fix compiler warning when building with a min target >= 10.6.
Ryan C. Gordon <icculus@icculus.org>
parents: 9629
diff changeset
   462
#endif
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   463
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   464
    /* Add device to the end of the list */
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   465
    if ( !gpDeviceList ) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   466
        gpDeviceList = device;
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   467
    } else {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   468
        recDevice *curdevice;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   469
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   470
        curdevice = gpDeviceList;
8986
1c81316ac642 Fixed bug 2631 - Mac: minor code cleanup
Sam Lantinga <slouken@libsdl.org>
parents: 8860
diff changeset
   471
        while ( curdevice->pNext ) {
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   472
            ++device_index;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   473
            curdevice = curdevice->pNext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   474
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   475
        curdevice->pNext = device;
9862
c3c3cdaf37f9 Mac: Fixed off-by-one bug when plugging in a joystick (thanks, Konstantin!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9632
diff changeset
   476
        ++device_index;  /* bump by one since we counted by pNext. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   477
    }
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   478
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   479
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   480
#if !SDL_EVENTS_DISABLED
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   481
    {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   482
        SDL_Event event;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   483
        event.type = SDL_JOYDEVICEADDED;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   484
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   485
        if (SDL_GetEventState(event.type) == SDL_ENABLE) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   486
            event.jdevice.which = device_index;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   487
            if ((SDL_EventOK == NULL)
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   488
                || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   489
                SDL_PushEvent(&event);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   490
            }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   491
        }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   492
    }
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   493
#endif /* !SDL_EVENTS_DISABLED */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   494
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   495
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   496
static SDL_bool
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   497
ConfigHIDManager(CFArrayRef matchingArray)
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   498
{
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   499
    CFRunLoopRef runloop = CFRunLoopGetCurrent();
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   500
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   501
    if (IOHIDManagerOpen(hidman, kIOHIDOptionsTypeNone) != kIOReturnSuccess) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   502
        return SDL_FALSE;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   503
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   504
9370
795d4866e4db Mac OS X: Better way to stop duplicate joystick reports (thanks, Kyungdahm!).
Ryan C. Gordon <icculus@icculus.org>
parents: 9120
diff changeset
   505
    IOHIDManagerSetDeviceMatchingMultiple(hidman, matchingArray);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   506
    IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, NULL);
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
   507
    IOHIDManagerScheduleWithRunLoop(hidman, runloop, SDL_JOYSTICK_RUNLOOP_MODE);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   508
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
   509
    while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE,0,TRUE) == kCFRunLoopRunHandledSource) {
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   510
        /* no-op. Callback fires once per existing device. */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   511
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   512
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
   513
    /* future hotplug events will come through SDL_JOYSTICK_RUNLOOP_MODE now. */
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   514
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   515
    return SDL_TRUE;  /* good to go. */
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   516
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   517
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   518
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   519
static CFDictionaryRef
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   520
CreateHIDDeviceMatchDictionary(const UInt32 page, const UInt32 usage, int *okay)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   521
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   522
    CFDictionaryRef retval = NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   523
    CFNumberRef pageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &page);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   524
    CFNumberRef usageNumRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   525
    const void *keys[2] = { (void *) CFSTR(kIOHIDDeviceUsagePageKey), (void *) CFSTR(kIOHIDDeviceUsageKey) };
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   526
    const void *vals[2] = { (void *) pageNumRef, (void *) usageNumRef };
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   527
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   528
    if (pageNumRef && usageNumRef) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   529
        retval = CFDictionaryCreate(kCFAllocatorDefault, keys, vals, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   530
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   531
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   532
    if (pageNumRef) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   533
        CFRelease(pageNumRef);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   534
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   535
    if (usageNumRef) {
8245
2c63c27c4d1d Free the correct variable.
Ryan C. Gordon <icculus@icculus.org>
parents: 8242
diff changeset
   536
        CFRelease(usageNumRef);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   537
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   538
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   539
    if (!retval) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   540
        *okay = 0;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   541
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   542
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   543
    return retval;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   544
}
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   545
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   546
static SDL_bool
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   547
CreateHIDManager(void)
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   548
{
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   549
    SDL_bool retval = SDL_FALSE;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   550
    int okay = 1;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   551
    const void *vals[] = {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   552
        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick, &okay),
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   553
        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad, &okay),
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   554
        (void *) CreateHIDDeviceMatchDictionary(kHIDPage_GenericDesktop, kHIDUsage_GD_MultiAxisController, &okay),
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   555
    };
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   556
    const size_t numElements = SDL_arraysize(vals);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   557
    CFArrayRef array = okay ? CFArrayCreate(kCFAllocatorDefault, vals, numElements, &kCFTypeArrayCallBacks) : NULL;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   558
    size_t i;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   559
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   560
    for (i = 0; i < numElements; i++) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   561
        if (vals[i]) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   562
            CFRelease((CFTypeRef) vals[i]);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   563
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   564
    }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   565
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   566
    if (array) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   567
        hidman = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   568
        if (hidman != NULL) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   569
            retval = ConfigHIDManager(array);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   570
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   571
        CFRelease(array);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   572
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   573
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   574
    return retval;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   575
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   576
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   577
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   578
/* Function to scan the system for joysticks.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   579
 * Joystick 0 should be the system default joystick.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   580
 * This function should return the number of available joysticks, or -1
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   581
 * on an unrecoverable fatal error.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   582
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   583
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   584
SDL_SYS_JoystickInit(void)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   586
    if (gpDeviceList) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7026
diff changeset
   587
        return SDL_SetError("Joystick: Device list already inited.");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   588
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   589
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   590
    if (!CreateHIDManager()) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   591
        return SDL_SetError("Joystick: Couldn't initialize HID Manager");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   592
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   593
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   594
    return SDL_SYS_NumJoysticks();
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   595
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   597
/* Function to return the number of joystick devices plugged in right now */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   598
int
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   599
SDL_SYS_NumJoysticks()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   600
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   601
    recDevice *device = gpDeviceList;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   602
    int nJoySticks = 0;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   603
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   604
    while (device) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   605
        if (!device->removed) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   606
            nJoySticks++;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   607
        }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   608
        device = device->pNext;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   609
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   610
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   611
    return nJoySticks;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   612
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   613
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   614
/* Function to cause any queued joystick insertions to be processed
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   615
 */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   616
void
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   617
SDL_SYS_JoystickDetect()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   618
{
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   619
    recDevice *device = gpDeviceList;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   620
    while (device) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   621
        if (device->removed) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   622
            device = FreeDevice(device);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   623
        } else {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   624
            device = device->pNext;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   625
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   626
    }
9375
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   627
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   628
	// run this after the checks above so we don't set device->removed and delete the device before
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   629
	// SDL_SYS_JoystickUpdate can run to clean up the SDL_Joystick object that owns this device
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   630
	while (CFRunLoopRunInMode(SDL_JOYSTICK_RUNLOOP_MODE,0,TRUE) == kCFRunLoopRunHandledSource) {
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   631
		/* no-op. Pending callbacks will fire in CFRunLoopRunInMode(). */
Alfred Reynolds <alfred@valvesoftware.com>
parents: 9370
diff changeset
   632
	}
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   633
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   634
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   635
/* Function to get the device-dependent name of a joystick */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   636
const char *
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   637
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
{
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   639
    recDevice *device = GetDeviceForIndex(device_index);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   640
    return device ? device->product : "UNKNOWN";
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   641
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   642
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   643
/* Function to return the instance id of the joystick at device_index
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   644
 */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   645
SDL_JoystickID
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   646
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   647
{
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   648
    recDevice *device = GetDeviceForIndex(device_index);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   649
    return device ? device->instance_id : 0;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   650
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   651
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   652
/* Function to open a joystick for use.
9380
07b7c1005a23 Updated internal documentation comments.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9375
diff changeset
   653
 * The joystick to open is specified by the device index.
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   654
 * This should fill the nbuttons and naxes fields of the joystick structure.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   655
 * It returns 0, or -1 if there is an error.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   656
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   657
int
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   658
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   659
{
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   660
    recDevice *device = GetDeviceForIndex(device_index);
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   662
    joystick->instance_id = device->instance_id;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   663
    joystick->hwdata = device;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   664
    joystick->name = device->product;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   665
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   666
    joystick->naxes = device->axes;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   667
    joystick->nhats = device->hats;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   668
    joystick->nballs = 0;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   669
    joystick->nbuttons = device->buttons;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   670
    return 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   671
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   672
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   673
/* Function to query if the joystick is currently attached
9380
07b7c1005a23 Updated internal documentation comments.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9375
diff changeset
   674
 * It returns SDL_TRUE if attached, SDL_FALSE otherwise.
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   675
 */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   676
SDL_bool
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   677
SDL_SYS_JoystickAttached(SDL_Joystick * joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   678
{
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   679
    return joystick->hwdata != NULL;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   680
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   681
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   682
/* Function to update the state of a joystick - called as a device poll.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   683
 * This function shouldn't update the joystick structure directly,
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   684
 * but instead should call SDL_PrivateJoystick*() to deliver events
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   685
 * and update joystick device state.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   686
 */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   687
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   688
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   689
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   690
    recDevice *device = joystick->hwdata;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   691
    recElement *element;
2287
15a242fca6c4 Fixed bug #478
Sam Lantinga <slouken@libsdl.org>
parents: 2120
diff changeset
   692
    SInt32 value, range;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   693
    int i;
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   694
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   695
    if (!device) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   696
        return;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   697
    }
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   698
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   699
    if (device->removed) {      /* device was unplugged; ignore it. */
9433
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   700
        if (joystick->hwdata) {
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   701
            joystick->force_recentering = SDL_TRUE;
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   702
            joystick->hwdata = NULL;
bd062398b648 Cleanups in the joystick code.
Ryan C. Gordon <icculus@icculus.org>
parents: 9432
diff changeset
   703
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   704
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   705
    }
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   706
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   707
    element = device->firstAxis;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   708
    i = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   709
    while (element) {
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   710
        value = GetHIDScaledCalibratedState(device, element, -32768, 32767);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   711
        if (value != joystick->axes[i]) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   712
            SDL_PrivateJoystickAxis(joystick, i, value);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   713
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   714
        element = element->pNext;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   715
        ++i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   716
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   717
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   718
    element = device->firstButton;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   719
    i = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   720
    while (element) {
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   721
        value = GetHIDElementState(device, element);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   722
        if (value > 1) {          /* handle pressure-sensitive buttons */
629
3fa401bb4bb5 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 623
diff changeset
   723
            value = 1;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   724
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   725
        if (value != joystick->buttons[i]) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   726
            SDL_PrivateJoystickButton(joystick, i, value);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   727
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   728
        element = element->pNext;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   729
        ++i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   730
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   731
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   732
    element = device->firstHat;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   733
    i = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   734
    while (element) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   735
        Uint8 pos = 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   736
2287
15a242fca6c4 Fixed bug #478
Sam Lantinga <slouken@libsdl.org>
parents: 2120
diff changeset
   737
        range = (element->max - element->min + 1);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   738
        value = GetHIDElementState(device, element) - element->min;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   739
        if (range == 4) {         /* 4 position hatswitch - scale up value */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   740
            value *= 2;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   741
        } else if (range != 8) {    /* Neither a 4 nor 8 positions - fall back to default position (centered) */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   742
            value = -1;
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   743
        }
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   744
        switch (value) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   745
        case 0:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   746
            pos = SDL_HAT_UP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   747
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   748
        case 1:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   749
            pos = SDL_HAT_RIGHTUP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   750
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   751
        case 2:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   752
            pos = SDL_HAT_RIGHT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   753
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   754
        case 3:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   755
            pos = SDL_HAT_RIGHTDOWN;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   756
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   757
        case 4:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   758
            pos = SDL_HAT_DOWN;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   759
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   760
        case 5:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   761
            pos = SDL_HAT_LEFTDOWN;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   762
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   763
        case 6:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   764
            pos = SDL_HAT_LEFT;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   765
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   766
        case 7:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   767
            pos = SDL_HAT_LEFTUP;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   768
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   769
        default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   770
            /* Every other value is mapped to center. We do that because some
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   771
             * joysticks use 8 and some 15 for this value, and apparently
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   772
             * there are even more variants out there - so we try to be generous.
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   773
             */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   774
            pos = SDL_HAT_CENTERED;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   775
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   776
        }
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   777
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   778
        if (pos != joystick->hats[i]) {
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   779
            SDL_PrivateJoystickHat(joystick, i, pos);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   780
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   781
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   782
        element = element->pNext;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   783
        ++i;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   784
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   785
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   786
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   787
/* Function to close a joystick after use */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   788
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   789
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   790
{
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   791
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   792
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   793
/* Function to perform any system-specific joystick related cleanup */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   794
void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   795
SDL_SYS_JoystickQuit(void)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   796
{
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   797
    while (FreeDevice(gpDeviceList)) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   798
        /* spin */
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   799
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   800
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   801
    if (hidman) {
8739
153077041e4b Mac OS X: Look for joystick hotplug in its own CFRunLoop.
Ryan C. Gordon <icculus@icculus.org>
parents: 8245
diff changeset
   802
        IOHIDManagerUnscheduleFromRunLoop(hidman, CFRunLoopGetCurrent(), SDL_JOYSTICK_RUNLOOP_MODE);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   803
        IOHIDManagerClose(hidman, kIOHIDOptionsTypeNone);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   804
        CFRelease(hidman);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   805
        hidman = NULL;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   806
    }
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   807
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   808
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   809
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6718
diff changeset
   810
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   811
{
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   812
    recDevice *device = GetDeviceForIndex(device_index);
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   813
    SDL_JoystickGUID guid;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   814
    if (device) {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   815
        guid = device->guid;
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   816
    } else {
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   817
        SDL_zero(guid);
8230
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   818
    }
9629
9242a9b29c7d Fixed bug 2869 - Controllers connected on launch are reported twice.
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
   819
    return guid;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   820
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   821
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6718
diff changeset
   822
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   823
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   824
    return joystick->hwdata->guid;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   825
}
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
   826
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
   827
#endif /* SDL_JOYSTICK_IOKIT */
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
diff changeset
   828
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1635
diff changeset
   829
/* vi: set ts=4 sw=4 expandtab: */