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