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