src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Mar 2018 14:42:51 -0700
changeset 11934 a7061d1f4ee4
parent 11925 ddb8dcdaa8d0
child 12088 399cc39583cc
permissions -rw-r--r--
Generalized the handling of instantaneous guide button presses so there's a minimum of 100 ms between guide button press and release.
This happens with at least the following controllers: All Apple MFI controllers, ASUS Gamepad, XiaoMi Bluetooth Controller
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
slouken@2765
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@2765
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@2765
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@2765
    20
*/
icculus@8093
    21
#include "../../SDL_internal.h"
slouken@2765
    22
philipp@8138
    23
/* This is the iOS implementation of the SDL joystick API */
slime73@9876
    24
#include "SDL_sysjoystick_c.h"
slime73@9876
    25
slime73@9876
    26
/* needed for SDL_IPHONE_MAX_GFORCE macro */
slime73@9876
    27
#include "SDL_config_iphoneos.h"
slouken@2765
    28
slouken@11532
    29
#include "SDL_assert.h"
icculus@10228
    30
#include "SDL_events.h"
slouken@2765
    31
#include "SDL_joystick.h"
slime73@9489
    32
#include "SDL_hints.h"
slouken@8921
    33
#include "SDL_stdinc.h"
slouken@2765
    34
#include "../SDL_sysjoystick.h"
slouken@2765
    35
#include "../SDL_joystick_c.h"
slouken@11532
    36
#include "../steam/SDL_steamcontroller.h"
slouken@11532
    37
slouken@2765
    38
slime73@10340
    39
#if !SDL_EVENTS_DISABLED
slime73@10340
    40
#include "../../events/SDL_events_c.h"
slime73@10340
    41
#endif
slime73@10340
    42
slime73@10340
    43
#if !TARGET_OS_TV
slouken@8921
    44
#import <CoreMotion/CoreMotion.h>
slime73@10340
    45
#endif
slouken@8921
    46
slime73@9876
    47
#ifdef SDL_JOYSTICK_MFI
slime73@9876
    48
#import <GameController/GameController.h>
slouken@8921
    49
slime73@9876
    50
static id connectObserver = nil;
slime73@9876
    51
static id disconnectObserver = nil;
slime73@9876
    52
#endif /* SDL_JOYSTICK_MFI */
slouken@8921
    53
slime73@10340
    54
#if !TARGET_OS_TV
slime73@9876
    55
static const char *accelerometerName = "iOS Accelerometer";
slouken@8921
    56
static CMMotionManager *motionManager = nil;
slime73@10340
    57
#endif /* !TARGET_OS_TV */
slime73@9876
    58
slime73@9876
    59
static SDL_JoystickDeviceItem *deviceList = NULL;
slime73@9876
    60
slime73@9489
    61
static int numjoysticks = 0;
slime73@9876
    62
static SDL_JoystickID instancecounter = 0;
slouken@11846
    63
int SDL_AppleTVRemoteOpenedAsJoystick = 0;
slime73@9876
    64
slime73@9876
    65
static SDL_JoystickDeviceItem *
slime73@9876
    66
GetDeviceForIndex(int device_index)
slime73@9876
    67
{
slime73@9876
    68
    SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
    69
    int i = 0;
slime73@9876
    70
slime73@9876
    71
    while (i < device_index) {
slime73@9876
    72
        if (device == NULL) {
slime73@9876
    73
            return NULL;
slime73@9876
    74
        }
slime73@9876
    75
        device = device->next;
slime73@9876
    76
        i++;
slime73@9876
    77
    }
slime73@9876
    78
slime73@9876
    79
    return device;
slime73@9876
    80
}
slime73@9876
    81
slime73@9876
    82
static void
slime73@9876
    83
SDL_SYS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
slime73@9876
    84
{
slime73@9876
    85
#ifdef SDL_JOYSTICK_MFI
slouken@11923
    86
    const Uint16 BUS_BLUETOOTH = 0x05;
slouken@11925
    87
    const Uint16 VENDOR_APPLE = 0x05AC;
slouken@11923
    88
    Uint16 *guid16 = (Uint16 *)device->guid.data;
slouken@11925
    89
    Uint16 vendor = 0;
slouken@11925
    90
    Uint16 product = 0;
slouken@11925
    91
    Uint16 version = 0;
slouken@11925
    92
    Uint8 subtype = 0;
slouken@11923
    93
slime73@9876
    94
    const char *name = NULL;
slime73@9876
    95
    /* Explicitly retain the controller because SDL_JoystickDeviceItem is a
slime73@9876
    96
     * struct, and ARC doesn't work with structs. */
slime73@9876
    97
    device->controller = (__bridge GCController *) CFBridgingRetain(controller);
slime73@9876
    98
slime73@9876
    99
    if (controller.vendorName) {
slime73@9876
   100
        name = controller.vendorName.UTF8String;
slime73@9876
   101
    }
slime73@9876
   102
slime73@9876
   103
    if (!name) {
slime73@9876
   104
        name = "MFi Gamepad";
slime73@9876
   105
    }
slime73@9876
   106
slime73@9876
   107
    device->name = SDL_strdup(name);
slime73@9876
   108
slime73@9876
   109
    if (controller.extendedGamepad) {
slouken@11925
   110
        vendor = VENDOR_APPLE;
slouken@11925
   111
        product = 1;
slouken@11925
   112
        subtype = 1;
slime73@9876
   113
        device->naxes = 6; /* 2 thumbsticks and 2 triggers */
slime73@9876
   114
        device->nhats = 1; /* d-pad */
slime73@9876
   115
        device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
slime73@9876
   116
    } else if (controller.gamepad) {
slouken@11925
   117
        vendor = VENDOR_APPLE;
slouken@11925
   118
        product = 2;
slouken@11925
   119
        subtype = 2;
slime73@9876
   120
        device->naxes = 0; /* no traditional analog inputs */
slime73@9876
   121
        device->nhats = 1; /* d-pad */
slime73@9876
   122
        device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
slime73@9876
   123
    }
slime73@10340
   124
#if TARGET_OS_TV
slime73@10340
   125
    else if (controller.microGamepad) {
slouken@11925
   126
        vendor = VENDOR_APPLE;
slouken@11925
   127
        product = 3;
slouken@11925
   128
        subtype = 3;
slime73@10340
   129
        device->naxes = 2; /* treat the touch surface as two axes */
slime73@10340
   130
        device->nhats = 0; /* apparently the touch surface-as-dpad is buggy */
slime73@10340
   131
        device->nbuttons = 3; /* AX, pause button */
slime73@10351
   132
slouken@10499
   133
        controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
slime73@10340
   134
    }
slime73@10340
   135
#endif /* TARGET_OS_TV */
slime73@9960
   136
slouken@11923
   137
    /* We only need 16 bits for each of these; space them out to fill 128. */
slouken@11923
   138
    /* Byteswap so devices get same GUID on little/big endian platforms. */
slouken@11923
   139
    *guid16++ = SDL_SwapLE16(BUS_BLUETOOTH);
slouken@11923
   140
    *guid16++ = 0;
slouken@11925
   141
    *guid16++ = SDL_SwapLE16(vendor);
slouken@11925
   142
    *guid16++ = 0;
slouken@11925
   143
    *guid16++ = SDL_SwapLE16(product);
slouken@11925
   144
    *guid16++ = 0;
slouken@11925
   145
    *guid16++ = SDL_SwapLE16(version);
slouken@11925
   146
    *guid16++ = 0;
slouken@11923
   147
slouken@11925
   148
    /* Note that this is an MFI controller and what subtype it is */
slouken@11925
   149
    device->guid.data[14] = 'm';
slouken@11925
   150
    device->guid.data[15] = subtype;
slouken@11923
   151
slime73@9960
   152
    /* This will be set when the first button press of the controller is
slime73@9960
   153
     * detected. */
slime73@9960
   154
    controller.playerIndex = -1;
slime73@10340
   155
slime73@10340
   156
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   157
}
slime73@9876
   158
slime73@9876
   159
static void
slime73@9876
   160
SDL_SYS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
slime73@9876
   161
{
slime73@9876
   162
    SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   163
slouken@11845
   164
#if TARGET_OS_TV
slouken@11845
   165
    if (!SDL_GetHintBoolean(SDL_HINT_TV_REMOTE_AS_JOYSTICK, SDL_TRUE)) {
slouken@11845
   166
        /* Ignore devices that aren't actually controllers (e.g. remotes), they'll be handled as keyboard input */
slouken@11845
   167
        if (controller && !controller.extendedGamepad && !controller.gamepad && controller.microGamepad) {
slouken@11845
   168
            return;
slouken@11845
   169
        }
slouken@11845
   170
    }
slouken@11845
   171
#endif
slouken@11845
   172
slime73@9876
   173
    while (device != NULL) {
slime73@9876
   174
        if (device->controller == controller) {
slime73@9876
   175
            return;
slime73@9876
   176
        }
slime73@9876
   177
        device = device->next;
slime73@9876
   178
    }
slime73@9876
   179
slouken@11845
   180
    device = (SDL_JoystickDeviceItem *) SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
slime73@9876
   181
    if (device == NULL) {
slime73@9876
   182
        return;
slime73@9876
   183
    }
slime73@9876
   184
slime73@9876
   185
    device->accelerometer = accelerometer;
slime73@9876
   186
    device->instance_id = instancecounter++;
slime73@9876
   187
slime73@9876
   188
    if (accelerometer) {
slime73@10340
   189
#if TARGET_OS_TV
slime73@10340
   190
        SDL_free(device);
slime73@10340
   191
        return;
slime73@10340
   192
#else
slime73@9876
   193
        device->name = SDL_strdup(accelerometerName);
slime73@9876
   194
        device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
slime73@9876
   195
        device->nhats = 0;
slime73@9876
   196
        device->nbuttons = 0;
slime73@9876
   197
slime73@9876
   198
        /* Use the accelerometer name as a GUID. */
slime73@9876
   199
        SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
slime73@10340
   200
#endif /* TARGET_OS_TV */
slime73@9876
   201
    } else if (controller) {
slime73@9876
   202
        SDL_SYS_AddMFIJoystickDevice(device, controller);
slime73@9876
   203
    }
slime73@9876
   204
slime73@9876
   205
    if (deviceList == NULL) {
slime73@9876
   206
        deviceList = device;
slime73@9876
   207
    } else {
slime73@9876
   208
        SDL_JoystickDeviceItem *lastdevice = deviceList;
slime73@9876
   209
        while (lastdevice->next != NULL) {
slime73@9876
   210
            lastdevice = lastdevice->next;
slime73@9876
   211
        }
slime73@9876
   212
        lastdevice->next = device;
slime73@9876
   213
    }
slime73@9876
   214
slime73@9876
   215
    ++numjoysticks;
slime73@9876
   216
slouken@10226
   217
    SDL_PrivateJoystickAdded(numjoysticks - 1);
slime73@9876
   218
}
slime73@9876
   219
slime73@9879
   220
static SDL_JoystickDeviceItem *
slime73@9876
   221
SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
slime73@9876
   222
{
slime73@9876
   223
    SDL_JoystickDeviceItem *prev = NULL;
slime73@9876
   224
    SDL_JoystickDeviceItem *next = NULL;
slime73@9876
   225
    SDL_JoystickDeviceItem *item = deviceList;
slime73@9876
   226
slime73@9876
   227
    if (device == NULL) {
slime73@9876
   228
        return NULL;
slime73@9876
   229
    }
slime73@9876
   230
slime73@9876
   231
    next = device->next;
slime73@9876
   232
slime73@9876
   233
    while (item != NULL) {
slime73@9876
   234
        if (item == device) {
slime73@9876
   235
            break;
slime73@9876
   236
        }
slime73@9876
   237
        prev = item;
slime73@9876
   238
        item = item->next;
slime73@9876
   239
    }
slime73@9876
   240
slime73@9876
   241
    /* Unlink the device item from the device list. */
slime73@9876
   242
    if (prev) {
slime73@9876
   243
        prev->next = device->next;
slime73@9876
   244
    } else if (device == deviceList) {
slime73@9876
   245
        deviceList = device->next;
slime73@9876
   246
    }
slime73@9876
   247
slime73@9876
   248
    if (device->joystick) {
slime73@9876
   249
        device->joystick->hwdata = NULL;
slime73@9876
   250
    }
slime73@9876
   251
slime73@9876
   252
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   253
    @autoreleasepool {
slime73@9876
   254
        if (device->controller) {
slime73@9876
   255
            /* The controller was explicitly retained in the struct, so it
slime73@9876
   256
             * should be explicitly released before freeing the struct. */
slime73@9876
   257
            GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
slime73@9876
   258
            controller.controllerPausedHandler = nil;
slime73@9876
   259
            device->controller = nil;
slime73@9876
   260
        }
slime73@9876
   261
    }
slime73@9876
   262
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   263
slime73@9876
   264
    --numjoysticks;
slime73@9876
   265
slouken@11532
   266
    SDL_PrivateJoystickRemoved(device->instance_id);
slime73@9876
   267
philipp@9878
   268
    SDL_free(device->name);
philipp@9878
   269
    SDL_free(device);
philipp@9878
   270
slime73@9876
   271
    return next;
slime73@9876
   272
}
slouken@2765
   273
slime73@10351
   274
#if TARGET_OS_TV
slouken@11284
   275
static void SDLCALL
slime73@10351
   276
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
slime73@10351
   277
{
slime73@10351
   278
    BOOL allowRotation = newValue != NULL && *newValue != '0';
slime73@10351
   279
slime73@10351
   280
    @autoreleasepool {
slime73@10351
   281
        for (GCController *controller in [GCController controllers]) {
slime73@10351
   282
            if (controller.microGamepad) {
slime73@10351
   283
                controller.microGamepad.allowsRotation = allowRotation;
slime73@10351
   284
            }
slime73@10351
   285
        }
slime73@10351
   286
    }
slime73@10351
   287
}
slime73@10351
   288
#endif /* TARGET_OS_TV */
slime73@10351
   289
slouken@11532
   290
static SDL_bool SteamControllerConnectedCallback(const char *name, SDL_JoystickGUID guid, int *device_instance)
slouken@11532
   291
{
slouken@11532
   292
    SDL_JoystickDeviceItem *device = (SDL_JoystickDeviceItem *)SDL_calloc(1, sizeof(SDL_JoystickDeviceItem));
slouken@11532
   293
    if (device == NULL) {
slouken@11532
   294
        return SDL_FALSE;
slouken@11532
   295
    }
slouken@11532
   296
slouken@11532
   297
    *device_instance = device->instance_id = instancecounter++;
slouken@11845
   298
    device->name = SDL_strdup(name);
slouken@11845
   299
    device->guid = guid;
slouken@11845
   300
    SDL_GetSteamControllerInputs(&device->nbuttons,
slouken@11845
   301
                                 &device->naxes,
slouken@11845
   302
                                 &device->nhats);
slouken@11532
   303
    device->m_bSteamController = SDL_TRUE;
slouken@11532
   304
slouken@11532
   305
    if (deviceList == NULL) {
slouken@11532
   306
        deviceList = device;
slouken@11532
   307
    } else {
slouken@11532
   308
        SDL_JoystickDeviceItem *lastdevice = deviceList;
slouken@11532
   309
        while (lastdevice->next != NULL) {
slouken@11532
   310
            lastdevice = lastdevice->next;
slouken@11532
   311
        }
slouken@11532
   312
        lastdevice->next = device;
slouken@11532
   313
    }
slouken@11532
   314
slouken@11532
   315
    ++numjoysticks;
slouken@11532
   316
slouken@11532
   317
    SDL_PrivateJoystickAdded(numjoysticks - 1);
slouken@11532
   318
slouken@11532
   319
    return SDL_TRUE;
slouken@11532
   320
}
slouken@11532
   321
slouken@11532
   322
static void SteamControllerDisconnectedCallback(int device_instance)
slouken@11532
   323
{
slouken@11532
   324
    SDL_JoystickDeviceItem *item;
slouken@11532
   325
slouken@11845
   326
    for (item = deviceList; item; item = item->next) {
slouken@11532
   327
        if (item->instance_id == device_instance) {
slouken@11845
   328
            SDL_SYS_RemoveJoystickDevice(item);
slouken@11845
   329
            break;
slouken@11532
   330
        }
slouken@11532
   331
    }
slouken@11532
   332
}
slouken@11532
   333
slouken@2765
   334
/* Function to scan the system for joysticks.
philipp@9311
   335
 * Joystick 0 should be the system default joystick.
slouken@2765
   336
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@2765
   337
 */
slouken@2765
   338
int
slouken@2765
   339
SDL_SYS_JoystickInit(void)
slouken@2765
   340
{
slime73@9876
   341
    @autoreleasepool {
slime73@9876
   342
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@10340
   343
slouken@11532
   344
        SDL_InitSteamControllers(SteamControllerConnectedCallback,
slouken@11532
   345
                                 SteamControllerDisconnectedCallback);
slouken@11532
   346
slime73@10340
   347
#if !TARGET_OS_TV
slouken@10499
   348
        if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
slime73@9876
   349
            /* Default behavior, accelerometer as joystick */
slime73@9876
   350
            SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
slime73@9876
   351
        }
slime73@10340
   352
#endif /* !TARGET_OS_TV */
slime73@9876
   353
slime73@9876
   354
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   355
        /* GameController.framework was added in iOS 7. */
slime73@9876
   356
        if (![GCController class]) {
slime73@9876
   357
            return numjoysticks;
slime73@9876
   358
        }
slime73@9876
   359
slime73@9876
   360
        for (GCController *controller in [GCController controllers]) {
slime73@9876
   361
            SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   362
        }
slime73@9876
   363
slime73@10351
   364
#if TARGET_OS_TV
slime73@10351
   365
        SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
slime73@10351
   366
                            SDL_AppleTVRemoteRotationHintChanged, NULL);
slime73@10351
   367
#endif /* TARGET_OS_TV */
slime73@10351
   368
slime73@9876
   369
        connectObserver = [center addObserverForName:GCControllerDidConnectNotification
slime73@9876
   370
                                              object:nil
slime73@9876
   371
                                               queue:nil
slime73@9876
   372
                                          usingBlock:^(NSNotification *note) {
slime73@9876
   373
                                              GCController *controller = note.object;
slime73@9876
   374
                                              SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   375
                                          }];
slime73@9876
   376
slime73@9876
   377
        disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
slime73@9876
   378
                                                 object:nil
slime73@9876
   379
                                                  queue:nil
slime73@9876
   380
                                             usingBlock:^(NSNotification *note) {
slime73@9876
   381
                                                 GCController *controller = note.object;
slime73@9876
   382
                                                 SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   383
                                                 while (device != NULL) {
slime73@9876
   384
                                                     if (device->controller == controller) {
slime73@9876
   385
                                                         SDL_SYS_RemoveJoystickDevice(device);
slime73@9876
   386
                                                         break;
slime73@9876
   387
                                                     }
slime73@9876
   388
                                                     device = device->next;
slime73@9876
   389
                                                 }
slime73@9876
   390
                                             }];
slime73@9876
   391
#endif /* SDL_JOYSTICK_MFI */
slime73@9489
   392
    }
slime73@9489
   393
slime73@9489
   394
    return numjoysticks;
slouken@2765
   395
}
slouken@2765
   396
philipp@10617
   397
int
philipp@10617
   398
SDL_SYS_NumJoysticks(void)
slouken@6707
   399
{
slime73@9489
   400
    return numjoysticks;
slouken@6707
   401
}
slouken@6707
   402
philipp@10617
   403
void
philipp@10617
   404
SDL_SYS_JoystickDetect(void)
slouken@6707
   405
{
slouken@11532
   406
    SDL_UpdateSteamControllers();
slouken@6707
   407
}
slouken@6707
   408
slouken@2765
   409
/* Function to get the device-dependent name of a joystick */
slouken@2765
   410
const char *
slouken@6707
   411
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@2765
   412
{
slime73@9876
   413
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   414
    return device ? device->name : "Unknown";
slouken@6707
   415
}
slouken@6707
   416
slouken@6707
   417
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   418
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   419
{
slime73@9876
   420
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   421
    return device ? device->instance_id : 0;
slouken@2765
   422
}
slouken@2765
   423
slouken@2765
   424
/* Function to open a joystick for use.
philipp@9380
   425
   The joystick to open is specified by the device index.
slouken@2765
   426
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
   427
   It returns 0, or -1 if there is an error.
slouken@2765
   428
 */
slouken@2765
   429
int
slouken@6693
   430
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@2765
   431
{
slime73@9876
   432
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   433
    if (device == NULL) {
slime73@9876
   434
        return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
slime73@9876
   435
    }
slime73@9876
   436
slime73@9876
   437
    joystick->hwdata = device;
slime73@9876
   438
    joystick->instance_id = device->instance_id;
slime73@9876
   439
slime73@9876
   440
    joystick->naxes = device->naxes;
slime73@9876
   441
    joystick->nhats = device->nhats;
slime73@9876
   442
    joystick->nbuttons = device->nbuttons;
slouken@6707
   443
    joystick->nballs = 0;
slime73@9876
   444
slime73@9876
   445
    device->joystick = joystick;
slouken@8921
   446
slime73@9506
   447
    @autoreleasepool {
slime73@9876
   448
        if (device->accelerometer) {
slime73@10340
   449
#if !TARGET_OS_TV
slime73@9876
   450
            if (motionManager == nil) {
slime73@9876
   451
                motionManager = [[CMMotionManager alloc] init];
slime73@9876
   452
            }
slime73@9876
   453
slime73@9876
   454
            /* Shorter times between updates can significantly increase CPU usage. */
slime73@9876
   455
            motionManager.accelerometerUpdateInterval = 0.1;
slime73@9876
   456
            [motionManager startAccelerometerUpdates];
slime73@10340
   457
#endif /* !TARGET_OS_TV */
slime73@9876
   458
        } else {
slime73@9876
   459
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   460
            GCController *controller = device->controller;
slime73@9960
   461
            controller.controllerPausedHandler = ^(GCController *c) {
slime73@9876
   462
                if (joystick->hwdata) {
slime73@9876
   463
                    ++joystick->hwdata->num_pause_presses;
slime73@9876
   464
                }
slime73@9876
   465
            };
slime73@9876
   466
#endif /* SDL_JOYSTICK_MFI */
slime73@9506
   467
        }
slouken@8921
   468
    }
slouken@11846
   469
    if (device->remote) {
slouken@11846
   470
        ++SDL_AppleTVRemoteOpenedAsJoystick;
slouken@11846
   471
    }
slouken@8921
   472
slouken@6707
   473
    return 0;
slouken@6707
   474
}
slouken@6707
   475
philipp@9561
   476
/* Function to determine if this joystick is attached to the system right now */
slime73@9879
   477
SDL_bool
slime73@9879
   478
SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   479
{
slime73@9876
   480
    return joystick->hwdata != NULL;
slouken@2765
   481
}
slouken@2765
   482
slime73@9879
   483
static void
slime73@9879
   484
SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
slouken@8921
   485
{
slime73@10340
   486
#if !TARGET_OS_TV
slouken@8921
   487
    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
slouken@8921
   488
    const SInt16 maxsint16 = 0x7FFF;
slouken@8921
   489
    CMAcceleration accel;
slouken@8921
   490
slime73@9506
   491
    @autoreleasepool {
slime73@9876
   492
        if (!motionManager.isAccelerometerActive) {
slime73@9506
   493
            return;
slime73@9506
   494
        }
slime73@9506
   495
slime73@9506
   496
        accel = motionManager.accelerometerData.acceleration;
slouken@8921
   497
    }
slouken@8921
   498
slouken@8921
   499
    /*
slouken@8921
   500
     Convert accelerometer data from floating point to Sint16, which is what
slouken@8921
   501
     the joystick system expects.
slouken@8921
   502
slouken@8921
   503
     To do the conversion, the data is first clamped onto the interval
slouken@8921
   504
     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@8921
   505
     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@8921
   506
slouken@8921
   507
     You can customize the clamped range of this function by modifying the
slouken@8921
   508
     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@8921
   509
slouken@8921
   510
     Once converted to Sint16, the accelerometer data no longer has coherent
slouken@8921
   511
     units. You can convert the data back to units of g-force by multiplying
slouken@8921
   512
     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@8921
   513
     */
slouken@8921
   514
slouken@8921
   515
    /* clamp the data */
slouken@8921
   516
    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
slouken@8921
   517
    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
slouken@8921
   518
    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
slouken@8921
   519
slouken@8921
   520
    /* pass in data mapped to range of SInt16 */
slime73@9876
   521
    SDL_PrivateJoystickAxis(joystick, 0,  (accel.x / maxgforce) * maxsint16);
slouken@8921
   522
    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
slime73@9876
   523
    SDL_PrivateJoystickAxis(joystick, 2,  (accel.z / maxgforce) * maxsint16);
slime73@10340
   524
#endif /* !TARGET_OS_TV */
slime73@9876
   525
}
slime73@9876
   526
slime73@9876
   527
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   528
static Uint8
slime73@9876
   529
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
slime73@9876
   530
{
slime73@9876
   531
    Uint8 hat = 0;
slime73@9876
   532
slime73@9876
   533
    if (dpad.up.isPressed) {
slime73@9876
   534
        hat |= SDL_HAT_UP;
slime73@9876
   535
    } else if (dpad.down.isPressed) {
slime73@9876
   536
        hat |= SDL_HAT_DOWN;
slime73@9876
   537
    }
slime73@9876
   538
slime73@9876
   539
    if (dpad.left.isPressed) {
slime73@9876
   540
        hat |= SDL_HAT_LEFT;
slime73@9876
   541
    } else if (dpad.right.isPressed) {
slime73@9876
   542
        hat |= SDL_HAT_RIGHT;
slime73@9876
   543
    }
slime73@9876
   544
slime73@9876
   545
    if (hat == 0) {
slime73@9876
   546
        return SDL_HAT_CENTERED;
slime73@9876
   547
    }
slime73@9876
   548
slime73@9876
   549
    return hat;
slime73@9876
   550
}
slime73@9876
   551
#endif
slime73@9876
   552
slime73@9876
   553
static void
slime73@9876
   554
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
slime73@9876
   555
{
slime73@10340
   556
#if SDL_JOYSTICK_MFI
slime73@9876
   557
    @autoreleasepool {
slime73@9876
   558
        GCController *controller = joystick->hwdata->controller;
slime73@9876
   559
        Uint8 hatstate = SDL_HAT_CENTERED;
slime73@9876
   560
        int i;
slime73@9960
   561
        int updateplayerindex = 0;
slime73@9876
   562
slime73@9876
   563
        if (controller.extendedGamepad) {
slime73@9876
   564
            GCExtendedGamepad *gamepad = controller.extendedGamepad;
slime73@9876
   565
slime73@9876
   566
            /* Axis order matches the XInput Windows mappings. */
slime73@9960
   567
            Sint16 axes[] = {
slime73@9960
   568
                (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
slime73@9960
   569
                (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
slime73@9960
   570
                (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
slime73@9960
   571
                (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
slime73@9960
   572
                (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
slime73@9960
   573
                (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
slime73@9960
   574
            };
slime73@9960
   575
slime73@9960
   576
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   577
            Uint8 buttons[] = {
slime73@9960
   578
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   579
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   580
                gamepad.leftShoulder.isPressed,
slime73@9960
   581
                gamepad.rightShoulder.isPressed,
slime73@9960
   582
            };
slime73@9876
   583
slime73@9876
   584
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   585
slime73@9960
   586
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@9960
   587
                /* The triggers (axes 2 and 5) are resting at -32768 but SDL
slime73@9960
   588
                 * initializes its values to 0. We only want to make sure the
slime73@9960
   589
                 * player index is up to date if the user actually moves an axis. */
slime73@9960
   590
                if ((i != 2 && i != 5) || axes[i] != -32768) {
slouken@10714
   591
                    updateplayerindex |= (joystick->axes[i].value != axes[i]);
slime73@9960
   592
                }
slime73@9960
   593
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@9960
   594
            }
slime73@9960
   595
slime73@9960
   596
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   597
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   598
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   599
            }
slime73@9876
   600
        } else if (controller.gamepad) {
slime73@9876
   601
            GCGamepad *gamepad = controller.gamepad;
slime73@9876
   602
slime73@9960
   603
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   604
            Uint8 buttons[] = {
slime73@9960
   605
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   606
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   607
                gamepad.leftShoulder.isPressed,
slime73@9960
   608
                gamepad.rightShoulder.isPressed,
slime73@9960
   609
            };
slime73@9960
   610
slime73@9876
   611
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   612
slime73@9960
   613
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   614
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   615
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   616
            }
slime73@9876
   617
        }
slime73@10340
   618
#if TARGET_OS_TV
slime73@10340
   619
        else if (controller.microGamepad) {
slime73@10340
   620
            GCMicroGamepad *gamepad = controller.microGamepad;
slime73@10340
   621
slime73@10340
   622
            Sint16 axes[] = {
slime73@10340
   623
                (Sint16) (gamepad.dpad.xAxis.value * 32767),
slime73@10340
   624
                (Sint16) (gamepad.dpad.yAxis.value * -32767),
slime73@10340
   625
            };
slime73@10340
   626
slime73@10340
   627
            for (i = 0; i < SDL_arraysize(axes); i++) {
slouken@10873
   628
                updateplayerindex |= (joystick->axes[i].value != axes[i]);
slime73@10340
   629
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@10340
   630
            }
slime73@10340
   631
slime73@10340
   632
            Uint8 buttons[] = {
slime73@10340
   633
                gamepad.buttonA.isPressed,
slime73@10340
   634
                gamepad.buttonX.isPressed,
slime73@10340
   635
            };
slime73@10340
   636
slime73@10340
   637
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@10340
   638
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@10340
   639
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@10340
   640
            }
slime73@10340
   641
        }
slime73@10340
   642
#endif /* TARGET_OS_TV */
slime73@9876
   643
slime73@9960
   644
        if (joystick->nhats > 0) {
slime73@9960
   645
            updateplayerindex |= (joystick->hats[0] != hatstate);
slime73@9960
   646
            SDL_PrivateJoystickHat(joystick, 0, hatstate);
slime73@9960
   647
        }
slime73@9876
   648
slime73@9876
   649
        for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
slouken@11934
   650
            const Uint8 pausebutton = joystick->nbuttons - 1; /* The pause button is always last. */
slime73@9960
   651
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
slouken@11934
   652
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
slime73@9960
   653
            updateplayerindex = YES;
slime73@9876
   654
        }
slouken@11925
   655
        joystick->hwdata->num_pause_presses = 0;
slime73@9876
   656
slime73@9960
   657
        if (updateplayerindex && controller.playerIndex == -1) {
slime73@9960
   658
            BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
slime73@9960
   659
slime73@9960
   660
            /* Find the player index of all other connected controllers. */
slime73@9960
   661
            for (GCController *c in [GCController controllers]) {
slime73@9960
   662
                if (c != controller && c.playerIndex >= 0) {
slime73@9960
   663
                    usedPlayerIndexSlots[c.playerIndex] = YES;
slime73@9960
   664
                }
slime73@9960
   665
            }
slime73@9960
   666
slime73@9960
   667
            /* Set this controller's player index to the first unused index.
slime73@9960
   668
             * FIXME: This logic isn't great... but SDL doesn't expose this
slime73@9960
   669
             * concept in its external API, so we don't have much to go on. */
slime73@9960
   670
            for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
slime73@9960
   671
                if (!usedPlayerIndexSlots[i]) {
slime73@9960
   672
                    controller.playerIndex = i;
slime73@9960
   673
                    break;
slime73@9960
   674
                }
slime73@9960
   675
            }
slime73@9960
   676
        }
slime73@9876
   677
    }
slime73@10340
   678
#endif /* SDL_JOYSTICK_MFI */
slouken@8921
   679
}
slouken@8921
   680
slouken@2765
   681
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
   682
 * This function shouldn't update the joystick structure directly,
slouken@2765
   683
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
   684
 * and update joystick device state.
slouken@2765
   685
 */
slouken@2765
   686
void
slouken@2765
   687
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
   688
{
slime73@9876
   689
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   690
slime73@9876
   691
    if (device == NULL) {
slime73@9876
   692
        return;
slime73@9876
   693
    }
slouken@11532
   694
    
slouken@11532
   695
    if (device->m_bSteamController) {
slouken@11532
   696
        SDL_UpdateSteamController(joystick);
slouken@11532
   697
        return;
slouken@11532
   698
    }
slime73@9876
   699
slime73@9876
   700
    if (device->accelerometer) {
slime73@9876
   701
        SDL_SYS_AccelerometerUpdate(joystick);
slime73@9876
   702
    } else if (device->controller) {
slime73@9876
   703
        SDL_SYS_MFIJoystickUpdate(joystick);
slime73@9876
   704
    }
slouken@2765
   705
}
slouken@2765
   706
slouken@2765
   707
/* Function to close a joystick after use */
slouken@2765
   708
void
slouken@2765
   709
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   710
{
slime73@9876
   711
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   712
slime73@9876
   713
    if (device == NULL) {
slime73@9876
   714
        return;
slime73@9876
   715
    }
slime73@9876
   716
slime73@9876
   717
    device->joystick = NULL;
slime73@9876
   718
slime73@9506
   719
    @autoreleasepool {
slime73@9876
   720
        if (device->accelerometer) {
slime73@10340
   721
#if !TARGET_OS_TV
slime73@9876
   722
            [motionManager stopAccelerometerUpdates];
slime73@10340
   723
#endif /* !TARGET_OS_TV */
slime73@9876
   724
        } else if (device->controller) {
slime73@9876
   725
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   726
            GCController *controller = device->controller;
slime73@9876
   727
            controller.controllerPausedHandler = nil;
slime73@9960
   728
            controller.playerIndex = -1;
slime73@9876
   729
#endif
slime73@9876
   730
        }
slime73@9506
   731
    }
slouken@11846
   732
    if (device->remote) {
slouken@11846
   733
        --SDL_AppleTVRemoteOpenedAsJoystick;
slouken@11846
   734
    }
slouken@2765
   735
}
slouken@2765
   736
slouken@2765
   737
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   738
void
slouken@2765
   739
SDL_SYS_JoystickQuit(void)
slouken@2765
   740
{
slime73@9506
   741
    @autoreleasepool {
slime73@9876
   742
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   743
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   744
slime73@9876
   745
        if (connectObserver) {
slime73@9876
   746
            [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
slime73@9876
   747
            connectObserver = nil;
slime73@9876
   748
        }
slime73@9876
   749
slime73@9876
   750
        if (disconnectObserver) {
slime73@9876
   751
            [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
slime73@9876
   752
            disconnectObserver = nil;
slime73@9876
   753
        }
slime73@10351
   754
slime73@10351
   755
#if TARGET_OS_TV
slime73@10351
   756
        SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
slime73@10351
   757
                            SDL_AppleTVRemoteRotationHintChanged, NULL);
slime73@10351
   758
#endif /* TARGET_OS_TV */
slime73@9876
   759
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   760
slime73@9876
   761
        while (deviceList != NULL) {
slime73@9876
   762
            SDL_SYS_RemoveJoystickDevice(deviceList);
slime73@9876
   763
        }
slime73@9876
   764
slime73@10340
   765
#if !TARGET_OS_TV
slouken@8921
   766
        motionManager = nil;
slime73@10340
   767
#endif /* !TARGET_OS_TV */
slouken@8921
   768
    }
slime73@9489
   769
slouken@11532
   770
    SDL_QuitSteamControllers();
slouken@11532
   771
slime73@9489
   772
    numjoysticks = 0;
slouken@2765
   773
}
slouken@6693
   774
slime73@9879
   775
SDL_JoystickGUID
slime73@9879
   776
SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6693
   777
{
slime73@9876
   778
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slouken@6744
   779
    SDL_JoystickGUID guid;
slime73@9876
   780
    if (device) {
slime73@9876
   781
        guid = device->guid;
slime73@9876
   782
    } else {
slime73@9876
   783
        SDL_zero(guid);
slime73@9876
   784
    }
slouken@6693
   785
    return guid;
slouken@6693
   786
}
slouken@6693
   787
slime73@9879
   788
SDL_JoystickGUID
slime73@9879
   789
SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   790
{
slouken@6744
   791
    SDL_JoystickGUID guid;
slime73@9876
   792
    if (joystick->hwdata) {
slime73@9876
   793
        guid = joystick->hwdata->guid;
slime73@9876
   794
    } else {
slime73@9876
   795
        SDL_zero(guid);
slime73@9876
   796
    }
slouken@6693
   797
    return guid;
slouken@6693
   798
}
slouken@6693
   799
slouken@2765
   800
/* vi: set ts=4 sw=4 expandtab: */