src/joystick/darwin/SDL_sysjoystick.c
author Ryan C. Gordon <icculus@icculus.org>
Sat, 22 Feb 2014 00:55:28 -0500
changeset 8230 1c185bacf915
parent 8176 9efac2fd6ed6
child 8242 f6a2b1a6932b
permissions -rw-r--r--
Reworked Mac OS X joystick code to use the 10.5+ HID Manager API. Besides being a little more simple to use than the earlier IOKit HID API, and less likely to be deprecated, it also has the added benefit of working with the Sony DualShock 4 controller in Bluetooth mode out of the box, whereas the previous API has a bug that makes it report bad data for the controller. Cleaned up several other things in this code, having gone over every line of it. The remaining deprecated calls are also gone.
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
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 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
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
    41
/* 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
    42
static IOHIDManagerRef hidman = NULL;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
/* Linked list of all available devices */
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
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
    46
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    47
/* if SDL_TRUE then a device was added since the last update call */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
    48
static SDL_bool s_bDeviceAdded = SDL_FALSE;
7086
141ffce8bb59 - make sure to send a joy removed event even if the joystick wasn't opened under OSX
VALVE\alfred@alfredlinux.valvesoftware.com
parents: 7037
diff changeset
    49
static SDL_bool s_bDeviceRemoved = SDL_FALSE;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
    51
/* 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
    52
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
    53
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
    54
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
    55
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
    56
FreeElementList(recElement *pElement)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
{
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
    58
    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
    59
        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
    60
        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
    61
        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
    62
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
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
    65
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
    66
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
    67
{
1c185bacf915 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
    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
    69
    if (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
    70
        /* 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
    71
        pDeviceNext = removeDevice->pNext;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
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
    73
        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
    74
            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
    75
        } 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
    76
            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
    77
            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
    78
                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
    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
            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
    81
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    82
        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
    83
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    84
        /* 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
    85
        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
    86
        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
    87
        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
    88
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
    89
        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
    90
    }
1c185bacf915 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
    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
    92
}
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
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
    94
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
    95
GetHIDElementState(recDevice *pDevice, recElement *pElement)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
{
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
    97
    SInt32 value = 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
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
    99
    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
   100
        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
   101
        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
   102
            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
   103
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
   104
            /* 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
   105
            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
   106
                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
   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
            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
   109
                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
   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
        }
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
   112
    }
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
   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
    return value;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   115
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   116
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
   117
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
   118
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
   119
{
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
   120
    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
   121
    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
   122
    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
   123
    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
   124
        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
   125
    }
1c185bacf915 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
    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
   127
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   128
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   129
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
   130
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
   131
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
   132
{
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
   133
    recDevice *device = (recDevice *) ctx;
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
    device->removed = 1;
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   135
#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
   136
    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
   137
#endif
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   138
    s_bDeviceRemoved = SDL_TRUE;
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   139
}
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   140
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   141
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
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
   143
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
   144
/* 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
   145
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
   146
AddHIDElements(CFArrayRef array, recDevice *pDevice)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
{
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
    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
   149
    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
   150
}
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
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
   152
/* 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
   153
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
   154
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
   155
{
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
   156
    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
   157
    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
   158
    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
   159
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
   160
    if (refElement && (elementTypeID == IOHIDElementGetTypeID())) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   161
        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
   162
        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
   163
        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
   164
        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
   165
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
   166
        /* 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
   167
        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
   168
            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
   169
            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
   170
            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
   171
                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
   172
                    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
   173
                        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
   174
                            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
   175
                            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
   176
                            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
   177
                            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
   178
                            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
   179
                            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
   180
                            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
   181
                            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
   182
                            case kHIDUsage_GD_Wheel:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   183
                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   184
                                if (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
   185
                                    pDevice->axes++;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   186
                                    headElement = &(pDevice->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
   187
                                }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   188
                                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
   189
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   190
                            case kHIDUsage_GD_Hatswitch:
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   191
                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   192
                                if (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
   193
                                    pDevice->hats++;
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   194
                                    headElement = &(pDevice->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
   195
                                }
1c185bacf915 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
                                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
   197
                        }
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
                        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
   199
1c185bacf915 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
                    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
   201
                        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
   202
                            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
   203
                            case kHIDUsage_Sim_Throttle:
1c185bacf915 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
                                element = (recElement *) SDL_calloc(1, sizeof (recElement));
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   205
                                if (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
   206
                                    pDevice->axes++;
1c185bacf915 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
                                    headElement = &(pDevice->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
   208
                                }
1c185bacf915 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
                                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
   210
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
   211
                            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
   212
                                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
   213
                        }
1c185bacf915 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
                        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
   215
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   216
                    case kHIDPage_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
   217
                        element = (recElement *) SDL_calloc(1, sizeof (recElement));
1c185bacf915 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
                        if (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
   219
                            pDevice->buttons++;
1c185bacf915 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
                            headElement = &(pDevice->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
   221
                        }
1c185bacf915 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
                        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
   223
1c185bacf915 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
                    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
   225
                        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
   226
                }
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
   227
            }
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
   228
            break;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   229
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
   230
            #if 0  /* !!! FIXME: this causes everything to get added twice on a DualShock 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
   231
            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
   232
                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
   233
                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
   234
                    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
   235
                }
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
   236
            }
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
   237
            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
   238
            #endif
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   239
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
   240
            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
   241
                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
   242
        }
7026
bc089599bd82 Added GUIDs for Bluetooth devices, using the same algorithm as the Linux code.
Sam Lantinga <slouken@libsdl.org>
parents: 6885
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
        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
   245
            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
   246
            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
   247
            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
   248
                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
   249
                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
   250
            }
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
   251
            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
   252
                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
   253
            } 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
   254
                *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
   255
            }
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   256
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
   257
            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
   258
            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
   259
            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
   260
            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
   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
            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
   263
            element->maxReport = element->max = (SInt32) IOHIDElementGetLogicalMax(refElement);
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   264
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
   265
            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
   266
        }
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
   267
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   268
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   269
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
   270
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
   271
GetDeviceInfo(IOHIDDeviceRef hidDevice, recDevice *pDevice)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   272
{
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
   273
    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
   274
    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
   275
    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
   276
1c185bacf915 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
    /* 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
   278
    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
   279
    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
   280
        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
   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
    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
   283
        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
   284
    }
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
   285
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   286
    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
   287
    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
   288
        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
   289
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   290
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
   291
    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
   292
         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
   293
         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
   294
        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
   295
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   296
1c185bacf915 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
    pDevice->deviceRef = hidDevice;
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
    /* 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
   300
    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
   301
    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
   302
        /* 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
   303
        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
   304
    }
1c185bacf915 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
    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
   306
        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
   307
    }
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
   308
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
   309
    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
   310
    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
   311
        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
   312
    }
1c185bacf915 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
1c185bacf915 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
    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
   315
    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
   316
        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
   317
    }
2713
0906692aa6a4 Final merge of Google Summer of Code 2008 work...
Sam Lantinga <slouken@libsdl.org>
parents: 2295
diff changeset
   318
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
   319
    /* 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
   320
       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
   321
    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
   322
    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
   323
        /* 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
   324
        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
   325
        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
   326
        *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
   327
        *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
   328
        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
   329
    }
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
   330
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
   331
    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
   332
    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
   333
        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
   334
        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
   335
    }
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
   336
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   337
    return SDL_TRUE;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   338
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   339
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
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
   342
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
   343
{
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
   344
    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
   345
        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
   346
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   347
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
   348
    recDevice *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
   349
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
   350
    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
   351
        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
   352
        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
   353
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   354
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
   355
    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
   356
        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
   357
        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
   358
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   359
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
   360
    /* 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
   361
    IOHIDDeviceRegisterRemovalCallback(ioHIDDeviceObject, JoystickDeviceWasRemovedCallback, 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
   362
    IOHIDDeviceScheduleWithRunLoop(ioHIDDeviceObject, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
1c185bacf915 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
7791
d9f0067a5421 Only allocate a joystick instance ID once we know it's a joystick.
Sam Lantinga <slouken@libsdl.org>
parents: 7681
diff changeset
   364
    /* 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
   365
    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
   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
    /* We have to do some storage of the io_service_t for SDL_HapticOpenFromJoystick */
1c185bacf915 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
    if (IOHIDDeviceGetService != NULL) {  /* weak reference: available in 10.6 and later. */
1c185bacf915 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
        const io_service_t ioservice = IOHIDDeviceGetService(ioHIDDeviceObject);
1c185bacf915 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
        if ((ioservice) && (FFIsForceFeedback(ioservice) == FF_OK)) {
1c185bacf915 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
            device->ffservice = ioservice;
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   372
#if SDL_HAPTIC_IOKIT
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
   373
            MacHaptic_MaybeAddDevice(ioservice);
8173
23919d993046 Implement new backend methods for haptic and hot plugging on OS X
Edward Rudd <urkle@outoforder.cc>
parents: 8149
diff changeset
   374
#endif
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
   375
        }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   376
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   377
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   378
    device->send_open_event = 1;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   379
    s_bDeviceAdded = SDL_TRUE;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   380
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   381
    /* Add device to the end of the list */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   382
    if ( !gpDeviceList )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   383
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   384
        gpDeviceList = device;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   385
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   386
    else
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   387
    {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   388
        recDevice *curdevice;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   389
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   390
        curdevice = gpDeviceList;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   391
        while ( curdevice->pNext )
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   392
        {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   393
            curdevice = curdevice->pNext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   394
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   395
        curdevice->pNext = device;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   396
    }
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
   397
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
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
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
   400
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
   401
{
1c185bacf915 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
    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
   403
1c185bacf915 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
    /* Run in a custom RunLoop mode just while initializing,
1c185bacf915 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
       so we can detect sticks without messing with everything 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
   406
    CFStringRef tempRunLoopMode = CFSTR("SDLJoystickInit");
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   407
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   408
    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
   409
        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
   410
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   411
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   412
    IOHIDManagerRegisterDeviceMatchingCallback(hidman, JoystickDeviceWasAddedCallback, 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
   413
    IOHIDManagerScheduleWithRunLoop(hidman, runloop, tempRunLoopMode);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   414
    IOHIDManagerSetDeviceMatchingMultiple(hidman, 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
   415
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   416
    while (CFRunLoopRunInMode(tempRunLoopMode,0,TRUE)==kCFRunLoopRunHandledSource) {
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   417
        /* 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
   418
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   419
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   420
    /* Put this in the normal RunLoop mode now, for future hotplug events. */
1c185bacf915 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
    IOHIDManagerUnscheduleFromRunLoop(hidman, runloop, tempRunLoopMode);
1c185bacf915 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
    IOHIDManagerScheduleWithRunLoop(hidman, runloop, kCFRunLoopDefaultMode);
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   423
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   424
    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
   425
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   426
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
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
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
   429
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
   430
{
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
   431
    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
   432
    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
   433
    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
   434
    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
   435
    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
   436
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   437
    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
   438
        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
   439
    }
1c185bacf915 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
1c185bacf915 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
    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
   442
        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
   443
    }
1c185bacf915 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
    if (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
   445
        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
   446
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   447
1c185bacf915 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
    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
   449
        *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
   450
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   451
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
   452
    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
   453
}
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   454
1c185bacf915 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
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
   456
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
   457
{
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   458
    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
   459
    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
   460
    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
   461
        (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
   462
        (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
   463
        (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
   464
    };
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   465
    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
   466
    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
   467
    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
   468
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   469
    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
   470
        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
   471
            CFRelease((CFTypeRef) vals[i]);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   472
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   473
    }
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
   474
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   475
    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
   476
        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
   477
        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
   478
            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
   479
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   480
        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
   481
    }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   482
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   483
    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
   484
}
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   485
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   486
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   487
/* 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
   488
 * 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
   489
 * 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
   490
 * on an unrecoverable fatal error.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   491
 */
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
   492
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
   493
SDL_SYS_JoystickInit(void)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   494
{
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
   495
    if (gpDeviceList) {
7037
3fedf1f25b94 Make SDL_SetError and friends unconditionally return -1.
Ryan C. Gordon <icculus@icculus.org>
parents: 7026
diff changeset
   496
        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
   497
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   498
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
   499
    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
   500
        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
   501
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   502
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   503
    return SDL_SYS_NumJoysticks();
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   504
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   505
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   506
/* 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
   507
int
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   508
SDL_SYS_NumJoysticks()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   509
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   510
    recDevice *device = gpDeviceList;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   511
    int nJoySticks = 0;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   512
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
   513
    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
   514
        if (!device->removed) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   515
            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
   516
        }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   517
        device = device->pNext;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   518
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   519
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   520
    return nJoySticks;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   521
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   522
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   523
/* 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
   524
 */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   525
void
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   526
SDL_SYS_JoystickDetect()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   527
{
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
   528
    if (s_bDeviceAdded || s_bDeviceRemoved) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   529
        recDevice *device = gpDeviceList;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   530
        s_bDeviceAdded = SDL_FALSE;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   531
        s_bDeviceRemoved = SDL_FALSE;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   532
        int device_index = 0;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   533
        /* send notifications */
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
   534
        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
   535
            if (device->send_open_event) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   536
                device->send_open_event = 0;
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
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceAdded()? */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   538
#if !SDL_EVENTS_DISABLED
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   539
                SDL_Event event;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   540
                event.type = SDL_JOYDEVICEADDED;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   541
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   542
                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   543
                    event.jdevice.which = device_index;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   544
                    if ((SDL_EventOK == NULL)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   545
                        || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   546
                        SDL_PushEvent(&event);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   547
                    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   548
                }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   549
#endif /* !SDL_EVENTS_DISABLED */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   550
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   551
            }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   552
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
   553
            if (device->removed) {
1c185bacf915 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
                const int instance_id = device->instance_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
   555
                device = FreeDevice(device);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   556
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
   557
/* !!! FIXME: why isn't there an SDL_PrivateJoyDeviceRemoved()? */
7086
141ffce8bb59 - make sure to send a joy removed event even if the joystick wasn't opened under OSX
VALVE\alfred@alfredlinux.valvesoftware.com
parents: 7037
diff changeset
   558
#if !SDL_EVENTS_DISABLED
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   559
                SDL_Event event;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   560
                event.type = SDL_JOYDEVICEREMOVED;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   561
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   562
                if (SDL_GetEventState(event.type) == SDL_ENABLE) {
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
   563
                    event.jdevice.which = instance_id;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   564
                    if ((SDL_EventOK == NULL)
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   565
                        || (*SDL_EventOK) (SDL_EventOKParam, &event)) {
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   566
                        SDL_PushEvent(&event);
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   567
                    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   568
                }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   569
#endif /* !SDL_EVENTS_DISABLED */
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   570
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
   571
            } else {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   572
                device = device->pNext;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   573
                device_index++;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   574
            }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   575
        }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   576
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   577
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   578
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   579
SDL_bool
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   580
SDL_SYS_JoystickNeedsPolling()
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   581
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   582
    return s_bDeviceAdded || s_bDeviceRemoved;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   583
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   584
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   585
/* 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
   586
const char *
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   587
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
   588
{
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
   589
    recDevice *device = gpDeviceList;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   590
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
   591
    while (device_index-- > 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
   592
        device = device->pNext;
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
   593
    }
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
   594
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   595
    return device->product;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   596
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   597
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   598
/* 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
   599
 */
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   600
SDL_JoystickID
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   601
SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   602
{
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   603
    recDevice *device = gpDeviceList;
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   604
    int index;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   605
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
   606
    for (index = device_index; index > 0; index--) {
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   607
        device = device->pNext;
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
   608
    }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   609
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   610
    return device->instance_id;
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   611
}
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   612
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   613
/* Function to open a joystick for use.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   614
 * The joystick to open is specified by the index field of the joystick.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   615
 * 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
   616
 * 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
   617
 */
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
   618
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
   619
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
   620
{
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
   621
    recDevice *device = gpDeviceList;
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
   622
    int index;
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
   623
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
   624
    for (index = device_index; index > 0; index--) {
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
   625
        device = device->pNext;
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
   626
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   627
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   628
    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
   629
    joystick->hwdata = device;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   630
    joystick->name = device->product;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   631
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   632
    joystick->naxes = device->axes;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   633
    joystick->nhats = device->hats;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   634
    joystick->nballs = 0;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   635
    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
   636
    return 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   637
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   638
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   639
/* Function to query if the joystick is currently attached
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   640
 *   It returns 1 if attached, 0 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
   641
 */
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   642
SDL_bool
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   643
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
   644
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   645
    recDevice *device = gpDeviceList;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   646
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
   647
    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
   648
        if (joystick->instance_id == device->instance_id) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   649
            return SDL_TRUE;
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
   650
        }
6707
70eeb7e3ec2f Organized joystick hotplug code a bit.
Sam Lantinga <slouken@libsdl.org>
parents: 6698
diff changeset
   651
        device = device->pNext;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   652
    }
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   653
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   654
    return SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   655
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   656
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   657
/* 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
   658
 * 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
   659
 * 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
   660
 * and update joystick device state.
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   661
 */
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
   662
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
   663
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
   664
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   665
    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
   666
    recElement *element;
2287
15a242fca6c4 Fixed bug #478
Sam Lantinga <slouken@libsdl.org>
parents: 2120
diff changeset
   667
    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
   668
    int i;
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   669
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
   670
    if (!device) {
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   671
        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
   672
    }
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   673
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   674
    if (device->removed) {      /* device was unplugged; ignore it. */
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   675
        joystick->closed = 1;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   676
        joystick->uncentered = 1;
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   677
        joystick->hwdata = NULL;
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
   678
        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
   679
    }
858
5db50aa5bf08 Date: Wed, 25 Feb 2004 06:41:17 -0500
Sam Lantinga <slouken@libsdl.org>
parents: 769
diff changeset
   680
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
   681
    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
   682
    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
   683
    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
   684
        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
   685
        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
   686
            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
   687
        }
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
   688
        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
   689
        ++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
   690
    }
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
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
   692
    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
   693
    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
   694
    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
   695
        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
   696
        if (value > 1) {          /* handle pressure-sensitive buttons */
629
3fa401bb4bb5 *** empty log message ***
Sam Lantinga <slouken@libsdl.org>
parents: 623
diff changeset
   697
            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
   698
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   699
        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
   700
            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
   701
        }
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
   702
        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
   703
        ++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
   704
    }
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
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
   706
    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
   707
    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
   708
    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
   709
        Uint8 pos = 0;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   710
2287
15a242fca6c4 Fixed bug #478
Sam Lantinga <slouken@libsdl.org>
parents: 2120
diff changeset
   711
        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
   712
        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
   713
        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
   714
            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
   715
        } 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
   716
            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
   717
        }
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
   718
        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
   719
        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
   720
            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
   721
            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
   722
        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
   723
            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
   724
            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
   725
        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
   726
            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
   727
            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
   728
        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
   729
            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
   730
            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
   731
        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
   732
            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
   733
            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
   734
        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
   735
            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
   736
            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
   737
        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
   738
            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
   739
            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
   740
        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
   741
            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
   742
            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
   743
        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
   744
            /* 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
   745
             * 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
   746
             * 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
   747
             */
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
            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
   749
            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
   750
        }
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
   751
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   752
        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
   753
            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
   754
        }
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   755
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
   756
        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
   757
        ++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
   758
    }
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   759
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   760
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   761
/* 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
   762
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
   763
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
   764
{
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   765
    joystick->closed = 1;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   766
}
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   767
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   768
/* 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
   769
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
   770
SDL_SYS_JoystickQuit(void)
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   771
{
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
   772
    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
   773
        /* 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
   774
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   775
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
   776
    if (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
   777
        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
   778
        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
   779
        hidman = NULL;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   780
    }
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
   781
1c185bacf915 Reworked Mac OS X joystick code to use the 10.5+ HID Manager API.
Ryan C. Gordon <icculus@icculus.org>
parents: 8176
diff changeset
   782
    s_bDeviceAdded = s_bDeviceRemoved = SDL_FALSE;
6690
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   783
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   784
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   785
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6718
diff changeset
   786
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
   787
{
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   788
    recDevice *device = gpDeviceList;
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   789
    int index;
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   790
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
   791
    for (index = device_index; index > 0; 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
   792
        device = device->pNext;
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
   793
    }
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   794
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   795
    return device->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
   796
}
9548c8a58103 Added hotplug joystick support and simplified game controller API, courtesy of Alfred Reynolds
Sam Lantinga <slouken@libsdl.org>
parents: 6138
diff changeset
   797
6738
b408d5a406a3 Changes from Alfred:
Sam Lantinga <slouken@libsdl.org>
parents: 6718
diff changeset
   798
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
   799
{
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 7133
diff changeset
   800
    return joystick->hwdata->guid;
172
37e3ca9254c7 Date: Sat, 8 Sep 2001 04:42:23 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   801
}
1635
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
   802
92947e3a18db Make sure code is only compiled if the appropriate subsystem is enabled
Sam Lantinga <slouken@libsdl.org>
parents: 1621
diff changeset
   803
#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
   804
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
   805
/* vi: set ts=4 sw=4 expandtab: */