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