src/joystick/iphoneos/SDL_sysjoystick.m
author Alex Szpakowski <slime73@gmail.com>
Fri, 11 Dec 2015 16:41:59 -0400
changeset 9960 a20484c998da
parent 9909 b2c000b256ea
child 9998 f67cf37e9cd4
permissions -rw-r--r--
iOS: Set the player index of MFi gamepads when the user first presses a button, rather than when it's programatically opened.
slouken@2765
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9619
     3
  Copyright (C) 1997-2015 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@2765
    29
#include "SDL_joystick.h"
slime73@9489
    30
#include "SDL_hints.h"
slouken@8921
    31
#include "SDL_stdinc.h"
slouken@2765
    32
#include "../SDL_sysjoystick.h"
slouken@2765
    33
#include "../SDL_joystick_c.h"
slouken@2765
    34
slime73@9876
    35
#if !SDL_EVENTS_DISABLED
slime73@9876
    36
#include "../../events/SDL_events_c.h"
slime73@9876
    37
#endif
slime73@9876
    38
slouken@8921
    39
#import <CoreMotion/CoreMotion.h>
slouken@8921
    40
slime73@9876
    41
#ifdef SDL_JOYSTICK_MFI
slime73@9876
    42
#import <GameController/GameController.h>
slouken@8921
    43
slime73@9876
    44
static id connectObserver = nil;
slime73@9876
    45
static id disconnectObserver = nil;
slime73@9876
    46
#endif /* SDL_JOYSTICK_MFI */
slouken@8921
    47
slime73@9876
    48
static const char *accelerometerName = "iOS Accelerometer";
slouken@8921
    49
static CMMotionManager *motionManager = nil;
slime73@9876
    50
slime73@9876
    51
static SDL_JoystickDeviceItem *deviceList = NULL;
slime73@9876
    52
slime73@9489
    53
static int numjoysticks = 0;
slime73@9876
    54
static SDL_JoystickID instancecounter = 0;
slime73@9876
    55
slime73@9876
    56
static SDL_JoystickDeviceItem *
slime73@9876
    57
GetDeviceForIndex(int device_index)
slime73@9876
    58
{
slime73@9876
    59
    SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
    60
    int i = 0;
slime73@9876
    61
slime73@9876
    62
    while (i < device_index) {
slime73@9876
    63
        if (device == NULL) {
slime73@9876
    64
            return NULL;
slime73@9876
    65
        }
slime73@9876
    66
        device = device->next;
slime73@9876
    67
        i++;
slime73@9876
    68
    }
slime73@9876
    69
slime73@9876
    70
    return device;
slime73@9876
    71
}
slime73@9876
    72
slime73@9876
    73
static void
slime73@9876
    74
SDL_SYS_AddMFIJoystickDevice(SDL_JoystickDeviceItem *device, GCController *controller)
slime73@9876
    75
{
slime73@9876
    76
#ifdef SDL_JOYSTICK_MFI
slime73@9876
    77
    const char *name = NULL;
slime73@9876
    78
    /* Explicitly retain the controller because SDL_JoystickDeviceItem is a
slime73@9876
    79
     * struct, and ARC doesn't work with structs. */
slime73@9876
    80
    device->controller = (__bridge GCController *) CFBridgingRetain(controller);
slime73@9876
    81
slime73@9876
    82
    if (controller.vendorName) {
slime73@9876
    83
        name = controller.vendorName.UTF8String;
slime73@9876
    84
    }
slime73@9876
    85
slime73@9876
    86
    if (!name) {
slime73@9876
    87
        name = "MFi Gamepad";
slime73@9876
    88
    }
slime73@9876
    89
slime73@9876
    90
    device->name = SDL_strdup(name);
slime73@9876
    91
slime73@9876
    92
    device->guid.data[0] = 'M';
slime73@9876
    93
    device->guid.data[1] = 'F';
slime73@9876
    94
    device->guid.data[2] = 'i';
slime73@9876
    95
    device->guid.data[3] = 'G';
slime73@9876
    96
    device->guid.data[4] = 'a';
slime73@9876
    97
    device->guid.data[5] = 'm';
slime73@9876
    98
    device->guid.data[6] = 'e';
slime73@9876
    99
    device->guid.data[7] = 'p';
slime73@9876
   100
    device->guid.data[8] = 'a';
slime73@9876
   101
    device->guid.data[9] = 'd';
slime73@9876
   102
slime73@9876
   103
    if (controller.extendedGamepad) {
slime73@9876
   104
        device->guid.data[10] = 1;
slime73@9876
   105
    } else if (controller.gamepad) {
slime73@9876
   106
        device->guid.data[10] = 2;
slime73@9876
   107
    }
slime73@9876
   108
slime73@9876
   109
    if (controller.extendedGamepad) {
slime73@9876
   110
        device->naxes = 6; /* 2 thumbsticks and 2 triggers */
slime73@9876
   111
        device->nhats = 1; /* d-pad */
slime73@9876
   112
        device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
slime73@9876
   113
    } else if (controller.gamepad) {
slime73@9876
   114
        device->naxes = 0; /* no traditional analog inputs */
slime73@9876
   115
        device->nhats = 1; /* d-pad */
slime73@9876
   116
        device->nbuttons = 7; /* ABXY, shoulder buttons, pause button */
slime73@9876
   117
    }
slime73@9876
   118
    /* TODO: Handle micro profiles on tvOS. */
slime73@9960
   119
slime73@9960
   120
    /* This will be set when the first button press of the controller is
slime73@9960
   121
     * detected. */
slime73@9960
   122
    controller.playerIndex = -1;
slime73@9876
   123
#endif
slime73@9876
   124
}
slime73@9876
   125
slime73@9876
   126
static void
slime73@9876
   127
SDL_SYS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
slime73@9876
   128
{
slime73@9876
   129
    SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   130
#if !SDL_EVENTS_DISABLED
slime73@9876
   131
    SDL_Event event;
slime73@9876
   132
#endif
slime73@9876
   133
slime73@9876
   134
    while (device != NULL) {
slime73@9876
   135
        if (device->controller == controller) {
slime73@9876
   136
            return;
slime73@9876
   137
        }
slime73@9876
   138
        device = device->next;
slime73@9876
   139
    }
slime73@9876
   140
slime73@9876
   141
    device = (SDL_JoystickDeviceItem *) SDL_malloc(sizeof(SDL_JoystickDeviceItem));
slime73@9876
   142
    if (device == NULL) {
slime73@9876
   143
        return;
slime73@9876
   144
    }
slime73@9876
   145
slime73@9876
   146
    SDL_zerop(device);
slime73@9876
   147
slime73@9876
   148
    device->accelerometer = accelerometer;
slime73@9876
   149
    device->instance_id = instancecounter++;
slime73@9876
   150
slime73@9876
   151
    if (accelerometer) {
slime73@9876
   152
        device->name = SDL_strdup(accelerometerName);
slime73@9876
   153
        device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
slime73@9876
   154
        device->nhats = 0;
slime73@9876
   155
        device->nbuttons = 0;
slime73@9876
   156
slime73@9876
   157
        /* Use the accelerometer name as a GUID. */
slime73@9876
   158
        SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
slime73@9876
   159
    } else if (controller) {
slime73@9876
   160
        SDL_SYS_AddMFIJoystickDevice(device, controller);
slime73@9876
   161
    }
slime73@9876
   162
slime73@9876
   163
    if (deviceList == NULL) {
slime73@9876
   164
        deviceList = device;
slime73@9876
   165
    } else {
slime73@9876
   166
        SDL_JoystickDeviceItem *lastdevice = deviceList;
slime73@9876
   167
        while (lastdevice->next != NULL) {
slime73@9876
   168
            lastdevice = lastdevice->next;
slime73@9876
   169
        }
slime73@9876
   170
        lastdevice->next = device;
slime73@9876
   171
    }
slime73@9876
   172
slime73@9876
   173
    ++numjoysticks;
slime73@9876
   174
slime73@9876
   175
#if !SDL_EVENTS_DISABLED
slime73@9876
   176
    event.type = SDL_JOYDEVICEADDED;
slime73@9876
   177
slime73@9876
   178
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slime73@9876
   179
        event.jdevice.which = numjoysticks - 1;
slime73@9876
   180
        if ((SDL_EventOK == NULL) ||
slime73@9876
   181
            (*SDL_EventOK)(SDL_EventOKParam, &event)) {
slime73@9876
   182
            SDL_PushEvent(&event);
slime73@9876
   183
        }
slime73@9876
   184
    }
slime73@9876
   185
#endif /* !SDL_EVENTS_DISABLED */
slime73@9876
   186
}
slime73@9876
   187
slime73@9879
   188
static SDL_JoystickDeviceItem *
slime73@9876
   189
SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
slime73@9876
   190
{
slime73@9876
   191
    SDL_JoystickDeviceItem *prev = NULL;
slime73@9876
   192
    SDL_JoystickDeviceItem *next = NULL;
slime73@9876
   193
    SDL_JoystickDeviceItem *item = deviceList;
slime73@9876
   194
#if !SDL_EVENTS_DISABLED
slime73@9876
   195
    SDL_Event event;
slime73@9876
   196
#endif
slime73@9876
   197
slime73@9876
   198
    if (device == NULL) {
slime73@9876
   199
        return NULL;
slime73@9876
   200
    }
slime73@9876
   201
slime73@9876
   202
    next = device->next;
slime73@9876
   203
slime73@9876
   204
    while (item != NULL) {
slime73@9876
   205
        if (item == device) {
slime73@9876
   206
            break;
slime73@9876
   207
        }
slime73@9876
   208
        prev = item;
slime73@9876
   209
        item = item->next;
slime73@9876
   210
    }
slime73@9876
   211
slime73@9876
   212
    /* Unlink the device item from the device list. */
slime73@9876
   213
    if (prev) {
slime73@9876
   214
        prev->next = device->next;
slime73@9876
   215
    } else if (device == deviceList) {
slime73@9876
   216
        deviceList = device->next;
slime73@9876
   217
    }
slime73@9876
   218
slime73@9876
   219
    if (device->joystick) {
slime73@9876
   220
        device->joystick->hwdata = NULL;
slime73@9876
   221
    }
slime73@9876
   222
slime73@9876
   223
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   224
    @autoreleasepool {
slime73@9876
   225
        if (device->controller) {
slime73@9876
   226
            /* The controller was explicitly retained in the struct, so it
slime73@9876
   227
             * should be explicitly released before freeing the struct. */
slime73@9876
   228
            GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
slime73@9876
   229
            controller.controllerPausedHandler = nil;
slime73@9876
   230
            device->controller = nil;
slime73@9876
   231
        }
slime73@9876
   232
    }
slime73@9876
   233
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   234
slime73@9876
   235
    --numjoysticks;
slime73@9876
   236
slime73@9876
   237
#if !SDL_EVENTS_DISABLED
slime73@9876
   238
    event.type = SDL_JOYDEVICEREMOVED;
slime73@9876
   239
slime73@9876
   240
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slime73@9876
   241
        event.jdevice.which = device->instance_id;
slime73@9876
   242
        if ((SDL_EventOK == NULL) ||
slime73@9876
   243
            (*SDL_EventOK)(SDL_EventOKParam, &event)) {
slime73@9876
   244
            SDL_PushEvent(&event);
slime73@9876
   245
        }
slime73@9876
   246
    }
slime73@9876
   247
#endif /* !SDL_EVENTS_DISABLED */
slime73@9876
   248
philipp@9878
   249
    SDL_free(device->name);
philipp@9878
   250
    SDL_free(device);
philipp@9878
   251
slime73@9876
   252
    return next;
slime73@9876
   253
}
slouken@2765
   254
slouken@2765
   255
/* Function to scan the system for joysticks.
philipp@9311
   256
 * Joystick 0 should be the system default joystick.
slouken@2765
   257
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@2765
   258
 */
slouken@2765
   259
int
slouken@2765
   260
SDL_SYS_JoystickInit(void)
slouken@2765
   261
{
slime73@9876
   262
    @autoreleasepool {
slime73@9876
   263
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   264
        const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
slime73@9876
   265
slime73@9876
   266
        if (!hint || SDL_atoi(hint)) {
slime73@9876
   267
            /* Default behavior, accelerometer as joystick */
slime73@9876
   268
            SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
slime73@9876
   269
        }
slime73@9876
   270
slime73@9876
   271
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   272
        /* GameController.framework was added in iOS 7. */
slime73@9876
   273
        if (![GCController class]) {
slime73@9876
   274
            return numjoysticks;
slime73@9876
   275
        }
slime73@9876
   276
slime73@9876
   277
        for (GCController *controller in [GCController controllers]) {
slime73@9876
   278
            SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   279
        }
slime73@9876
   280
slime73@9876
   281
        connectObserver = [center addObserverForName:GCControllerDidConnectNotification
slime73@9876
   282
                                              object:nil
slime73@9876
   283
                                               queue:nil
slime73@9876
   284
                                          usingBlock:^(NSNotification *note) {
slime73@9876
   285
                                              GCController *controller = note.object;
slime73@9876
   286
                                              SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   287
                                          }];
slime73@9876
   288
slime73@9876
   289
        disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
slime73@9876
   290
                                                 object:nil
slime73@9876
   291
                                                  queue:nil
slime73@9876
   292
                                             usingBlock:^(NSNotification *note) {
slime73@9876
   293
                                                 GCController *controller = note.object;
slime73@9876
   294
                                                 SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   295
                                                 while (device != NULL) {
slime73@9876
   296
                                                     if (device->controller == controller) {
slime73@9876
   297
                                                         SDL_SYS_RemoveJoystickDevice(device);
slime73@9876
   298
                                                         break;
slime73@9876
   299
                                                     }
slime73@9876
   300
                                                     device = device->next;
slime73@9876
   301
                                                 }
slime73@9876
   302
                                             }];
slime73@9876
   303
#endif /* SDL_JOYSTICK_MFI */
slime73@9489
   304
    }
slime73@9489
   305
slime73@9489
   306
    return numjoysticks;
slouken@2765
   307
}
slouken@2765
   308
slouken@6707
   309
int SDL_SYS_NumJoysticks()
slouken@6707
   310
{
slime73@9489
   311
    return numjoysticks;
slouken@6707
   312
}
slouken@6707
   313
slouken@6707
   314
void SDL_SYS_JoystickDetect()
slouken@6707
   315
{
slouken@6707
   316
}
slouken@6707
   317
slouken@2765
   318
/* Function to get the device-dependent name of a joystick */
slouken@2765
   319
const char *
slouken@6707
   320
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@2765
   321
{
slime73@9876
   322
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   323
    return device ? device->name : "Unknown";
slouken@6707
   324
}
slouken@6707
   325
slouken@6707
   326
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   327
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   328
{
slime73@9876
   329
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   330
    return device ? device->instance_id : 0;
slouken@2765
   331
}
slouken@2765
   332
slouken@2765
   333
/* Function to open a joystick for use.
philipp@9380
   334
   The joystick to open is specified by the device index.
slouken@2765
   335
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
   336
   It returns 0, or -1 if there is an error.
slouken@2765
   337
 */
slouken@2765
   338
int
slouken@6693
   339
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@2765
   340
{
slime73@9876
   341
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   342
    if (device == NULL) {
slime73@9876
   343
        return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
slime73@9876
   344
    }
slime73@9876
   345
slime73@9876
   346
    joystick->hwdata = device;
slime73@9876
   347
    joystick->instance_id = device->instance_id;
slime73@9876
   348
slime73@9876
   349
    joystick->naxes = device->naxes;
slime73@9876
   350
    joystick->nhats = device->nhats;
slime73@9876
   351
    joystick->nbuttons = device->nbuttons;
slouken@6707
   352
    joystick->nballs = 0;
slime73@9876
   353
slime73@9876
   354
    device->joystick = joystick;
slouken@8921
   355
slime73@9506
   356
    @autoreleasepool {
slime73@9876
   357
        if (device->accelerometer) {
slime73@9876
   358
            if (motionManager == nil) {
slime73@9876
   359
                motionManager = [[CMMotionManager alloc] init];
slime73@9876
   360
            }
slime73@9876
   361
slime73@9876
   362
            /* Shorter times between updates can significantly increase CPU usage. */
slime73@9876
   363
            motionManager.accelerometerUpdateInterval = 0.1;
slime73@9876
   364
            [motionManager startAccelerometerUpdates];
slime73@9876
   365
        } else {
slime73@9876
   366
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   367
            GCController *controller = device->controller;
slime73@9960
   368
            controller.controllerPausedHandler = ^(GCController *c) {
slime73@9876
   369
                if (joystick->hwdata) {
slime73@9876
   370
                    ++joystick->hwdata->num_pause_presses;
slime73@9876
   371
                }
slime73@9876
   372
            };
slime73@9876
   373
#endif /* SDL_JOYSTICK_MFI */
slime73@9506
   374
        }
slouken@8921
   375
    }
slouken@8921
   376
slouken@6707
   377
    return 0;
slouken@6707
   378
}
slouken@6707
   379
philipp@9561
   380
/* Function to determine if this joystick is attached to the system right now */
slime73@9879
   381
SDL_bool
slime73@9879
   382
SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   383
{
slime73@9876
   384
    return joystick->hwdata != NULL;
slouken@2765
   385
}
slouken@2765
   386
slime73@9879
   387
static void
slime73@9879
   388
SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
slouken@8921
   389
{
slouken@8921
   390
    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
slouken@8921
   391
    const SInt16 maxsint16 = 0x7FFF;
slouken@8921
   392
    CMAcceleration accel;
slouken@8921
   393
slime73@9506
   394
    @autoreleasepool {
slime73@9876
   395
        if (!motionManager.isAccelerometerActive) {
slime73@9506
   396
            return;
slime73@9506
   397
        }
slime73@9506
   398
slime73@9506
   399
        accel = motionManager.accelerometerData.acceleration;
slouken@8921
   400
    }
slouken@8921
   401
slouken@8921
   402
    /*
slouken@8921
   403
     Convert accelerometer data from floating point to Sint16, which is what
slouken@8921
   404
     the joystick system expects.
slouken@8921
   405
slouken@8921
   406
     To do the conversion, the data is first clamped onto the interval
slouken@8921
   407
     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@8921
   408
     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@8921
   409
slouken@8921
   410
     You can customize the clamped range of this function by modifying the
slouken@8921
   411
     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@8921
   412
slouken@8921
   413
     Once converted to Sint16, the accelerometer data no longer has coherent
slouken@8921
   414
     units. You can convert the data back to units of g-force by multiplying
slouken@8921
   415
     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@8921
   416
     */
slouken@8921
   417
slouken@8921
   418
    /* clamp the data */
slouken@8921
   419
    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
slouken@8921
   420
    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
slouken@8921
   421
    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
slouken@8921
   422
slouken@8921
   423
    /* pass in data mapped to range of SInt16 */
slime73@9876
   424
    SDL_PrivateJoystickAxis(joystick, 0,  (accel.x / maxgforce) * maxsint16);
slouken@8921
   425
    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
slime73@9876
   426
    SDL_PrivateJoystickAxis(joystick, 2,  (accel.z / maxgforce) * maxsint16);
slime73@9876
   427
}
slime73@9876
   428
slime73@9876
   429
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   430
static Uint8
slime73@9876
   431
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
slime73@9876
   432
{
slime73@9876
   433
    Uint8 hat = 0;
slime73@9876
   434
slime73@9876
   435
    if (dpad.up.isPressed) {
slime73@9876
   436
        hat |= SDL_HAT_UP;
slime73@9876
   437
    } else if (dpad.down.isPressed) {
slime73@9876
   438
        hat |= SDL_HAT_DOWN;
slime73@9876
   439
    }
slime73@9876
   440
slime73@9876
   441
    if (dpad.left.isPressed) {
slime73@9876
   442
        hat |= SDL_HAT_LEFT;
slime73@9876
   443
    } else if (dpad.right.isPressed) {
slime73@9876
   444
        hat |= SDL_HAT_RIGHT;
slime73@9876
   445
    }
slime73@9876
   446
slime73@9876
   447
    if (hat == 0) {
slime73@9876
   448
        return SDL_HAT_CENTERED;
slime73@9876
   449
    }
slime73@9876
   450
slime73@9876
   451
    return hat;
slime73@9876
   452
}
slime73@9876
   453
#endif
slime73@9876
   454
slime73@9876
   455
static void
slime73@9876
   456
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
slime73@9876
   457
{
slime73@9876
   458
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   459
    @autoreleasepool {
slime73@9876
   460
        GCController *controller = joystick->hwdata->controller;
slime73@9876
   461
        Uint8 hatstate = SDL_HAT_CENTERED;
slime73@9876
   462
        int i;
slime73@9960
   463
        int updateplayerindex = 0;
slime73@9876
   464
slime73@9876
   465
        if (controller.extendedGamepad) {
slime73@9876
   466
            GCExtendedGamepad *gamepad = controller.extendedGamepad;
slime73@9876
   467
slime73@9876
   468
            /* Axis order matches the XInput Windows mappings. */
slime73@9960
   469
            Sint16 axes[] = {
slime73@9960
   470
                (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
slime73@9960
   471
                (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
slime73@9960
   472
                (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
slime73@9960
   473
                (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
slime73@9960
   474
                (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
slime73@9960
   475
                (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
slime73@9960
   476
            };
slime73@9960
   477
slime73@9960
   478
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   479
            Uint8 buttons[] = {
slime73@9960
   480
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   481
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   482
                gamepad.leftShoulder.isPressed,
slime73@9960
   483
                gamepad.rightShoulder.isPressed,
slime73@9960
   484
            };
slime73@9876
   485
slime73@9876
   486
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   487
slime73@9960
   488
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@9960
   489
                /* The triggers (axes 2 and 5) are resting at -32768 but SDL
slime73@9960
   490
                 * initializes its values to 0. We only want to make sure the
slime73@9960
   491
                 * player index is up to date if the user actually moves an axis. */
slime73@9960
   492
                if ((i != 2 && i != 5) || axes[i] != -32768) {
slime73@9960
   493
                    updateplayerindex |= (joystick->axes[i] != axes[i]);
slime73@9960
   494
                }
slime73@9960
   495
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@9960
   496
            }
slime73@9960
   497
slime73@9960
   498
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   499
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   500
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   501
            }
slime73@9876
   502
        } else if (controller.gamepad) {
slime73@9876
   503
            GCGamepad *gamepad = controller.gamepad;
slime73@9876
   504
slime73@9960
   505
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   506
            Uint8 buttons[] = {
slime73@9960
   507
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   508
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   509
                gamepad.leftShoulder.isPressed,
slime73@9960
   510
                gamepad.rightShoulder.isPressed,
slime73@9960
   511
            };
slime73@9960
   512
slime73@9876
   513
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   514
slime73@9960
   515
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   516
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   517
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   518
            }
slime73@9876
   519
        }
slime73@9876
   520
        /* TODO: Handle micro profiles on tvOS. */
slime73@9876
   521
slime73@9960
   522
        if (joystick->nhats > 0) {
slime73@9960
   523
            updateplayerindex |= (joystick->hats[0] != hatstate);
slime73@9960
   524
            SDL_PrivateJoystickHat(joystick, 0, hatstate);
slime73@9960
   525
        }
slime73@9876
   526
slime73@9876
   527
        for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
slime73@9876
   528
            /* The pause button is always last. */
slime73@9876
   529
            Uint8 pausebutton = joystick->nbuttons - 1;
slime73@9876
   530
slime73@9960
   531
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
slime73@9960
   532
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
slime73@9960
   533
slime73@9960
   534
            updateplayerindex = YES;
slime73@9876
   535
        }
slime73@9876
   536
slime73@9876
   537
        joystick->hwdata->num_pause_presses = 0;
slime73@9960
   538
slime73@9960
   539
        if (updateplayerindex && controller.playerIndex == -1) {
slime73@9960
   540
            BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
slime73@9960
   541
slime73@9960
   542
            /* Find the player index of all other connected controllers. */
slime73@9960
   543
            for (GCController *c in [GCController controllers]) {
slime73@9960
   544
                if (c != controller && c.playerIndex >= 0) {
slime73@9960
   545
                    usedPlayerIndexSlots[c.playerIndex] = YES;
slime73@9960
   546
                }
slime73@9960
   547
            }
slime73@9960
   548
slime73@9960
   549
            /* Set this controller's player index to the first unused index.
slime73@9960
   550
             * FIXME: This logic isn't great... but SDL doesn't expose this
slime73@9960
   551
             * concept in its external API, so we don't have much to go on. */
slime73@9960
   552
            for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
slime73@9960
   553
                if (!usedPlayerIndexSlots[i]) {
slime73@9960
   554
                    controller.playerIndex = i;
slime73@9960
   555
                    break;
slime73@9960
   556
                }
slime73@9960
   557
            }
slime73@9960
   558
        }
slime73@9876
   559
    }
slime73@9876
   560
#endif
slouken@8921
   561
}
slouken@8921
   562
slouken@2765
   563
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
   564
 * This function shouldn't update the joystick structure directly,
slouken@2765
   565
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
   566
 * and update joystick device state.
slouken@2765
   567
 */
slouken@2765
   568
void
slouken@2765
   569
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
   570
{
slime73@9876
   571
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   572
slime73@9876
   573
    if (device == NULL) {
slime73@9876
   574
        return;
slime73@9876
   575
    }
slime73@9876
   576
slime73@9876
   577
    if (device->accelerometer) {
slime73@9876
   578
        SDL_SYS_AccelerometerUpdate(joystick);
slime73@9876
   579
    } else if (device->controller) {
slime73@9876
   580
        SDL_SYS_MFIJoystickUpdate(joystick);
slime73@9876
   581
    }
slouken@2765
   582
}
slouken@2765
   583
slouken@2765
   584
/* Function to close a joystick after use */
slouken@2765
   585
void
slouken@2765
   586
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   587
{
slime73@9876
   588
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   589
slime73@9876
   590
    if (device == NULL) {
slime73@9876
   591
        return;
slime73@9876
   592
    }
slime73@9876
   593
slime73@9876
   594
    device->joystick = NULL;
slime73@9876
   595
slime73@9506
   596
    @autoreleasepool {
slime73@9876
   597
        if (device->accelerometer) {
slime73@9876
   598
            [motionManager stopAccelerometerUpdates];
slime73@9876
   599
        } else if (device->controller) {
slime73@9876
   600
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   601
            GCController *controller = device->controller;
slime73@9876
   602
            controller.controllerPausedHandler = nil;
slime73@9960
   603
            controller.playerIndex = -1;
slime73@9876
   604
#endif
slime73@9876
   605
        }
slime73@9506
   606
    }
slouken@2765
   607
}
slouken@2765
   608
slouken@2765
   609
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   610
void
slouken@2765
   611
SDL_SYS_JoystickQuit(void)
slouken@2765
   612
{
slime73@9506
   613
    @autoreleasepool {
slime73@9876
   614
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   615
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   616
slime73@9876
   617
        if (connectObserver) {
slime73@9876
   618
            [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
slime73@9876
   619
            connectObserver = nil;
slime73@9876
   620
        }
slime73@9876
   621
slime73@9876
   622
        if (disconnectObserver) {
slime73@9876
   623
            [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
slime73@9876
   624
            disconnectObserver = nil;
slime73@9876
   625
        }
slime73@9876
   626
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   627
slime73@9876
   628
        while (deviceList != NULL) {
slime73@9876
   629
            SDL_SYS_RemoveJoystickDevice(deviceList);
slime73@9876
   630
        }
slime73@9876
   631
slouken@8921
   632
        motionManager = nil;
slouken@8921
   633
    }
slime73@9489
   634
slime73@9489
   635
    numjoysticks = 0;
slouken@2765
   636
}
slouken@6693
   637
slime73@9879
   638
SDL_JoystickGUID
slime73@9879
   639
SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6693
   640
{
slime73@9876
   641
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slouken@6744
   642
    SDL_JoystickGUID guid;
slime73@9876
   643
    if (device) {
slime73@9876
   644
        guid = device->guid;
slime73@9876
   645
    } else {
slime73@9876
   646
        SDL_zero(guid);
slime73@9876
   647
    }
slouken@6693
   648
    return guid;
slouken@6693
   649
}
slouken@6693
   650
slime73@9879
   651
SDL_JoystickGUID
slime73@9879
   652
SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   653
{
slouken@6744
   654
    SDL_JoystickGUID guid;
slime73@9876
   655
    if (joystick->hwdata) {
slime73@9876
   656
        guid = joystick->hwdata->guid;
slime73@9876
   657
    } else {
slime73@9876
   658
        SDL_zero(guid);
slime73@9876
   659
    }
slouken@6693
   660
    return guid;
slouken@6693
   661
}
slouken@6693
   662
slouken@2765
   663
/* vi: set ts=4 sw=4 expandtab: */