src/joystick/iphoneos/SDL_sysjoystick.m
author Alex Szpakowski <slime73@gmail.com>
Sun, 20 Sep 2015 23:08:36 -0300
changeset 9876 1496e502e51d
parent 9619 b94b6d0bff0f
child 9878 63f03a567fe3
permissions -rw-r--r--
Added initial support for MFi game controllers on iOS.
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@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
#if !SDL_EVENTS_DISABLED
slime73@9876
   127
    SDL_Event event;
slime73@9876
   128
#endif
slime73@9876
   129
slime73@9876
   130
    while (device != NULL) {
slime73@9876
   131
        if (device->controller == controller) {
slime73@9876
   132
            return;
slime73@9876
   133
        }
slime73@9876
   134
        device = device->next;
slime73@9876
   135
    }
slime73@9876
   136
slime73@9876
   137
    device = (SDL_JoystickDeviceItem *) SDL_malloc(sizeof(SDL_JoystickDeviceItem));
slime73@9876
   138
    if (device == NULL) {
slime73@9876
   139
        return;
slime73@9876
   140
    }
slime73@9876
   141
slime73@9876
   142
    SDL_zerop(device);
slime73@9876
   143
slime73@9876
   144
    device->accelerometer = accelerometer;
slime73@9876
   145
    device->instance_id = instancecounter++;
slime73@9876
   146
slime73@9876
   147
    if (accelerometer) {
slime73@9876
   148
        device->name = SDL_strdup(accelerometerName);
slime73@9876
   149
        device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
slime73@9876
   150
        device->nhats = 0;
slime73@9876
   151
        device->nbuttons = 0;
slime73@9876
   152
slime73@9876
   153
        /* Use the accelerometer name as a GUID. */
slime73@9876
   154
        SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
slime73@9876
   155
    } else if (controller) {
slime73@9876
   156
        SDL_SYS_AddMFIJoystickDevice(device, controller);
slime73@9876
   157
    }
slime73@9876
   158
slime73@9876
   159
    if (deviceList == NULL) {
slime73@9876
   160
        deviceList = device;
slime73@9876
   161
    } else {
slime73@9876
   162
        SDL_JoystickDeviceItem *lastdevice = deviceList;
slime73@9876
   163
        while (lastdevice->next != NULL) {
slime73@9876
   164
            lastdevice = lastdevice->next;
slime73@9876
   165
        }
slime73@9876
   166
        lastdevice->next = device;
slime73@9876
   167
    }
slime73@9876
   168
slime73@9876
   169
    ++numjoysticks;
slime73@9876
   170
slime73@9876
   171
#if !SDL_EVENTS_DISABLED
slime73@9876
   172
    event.type = SDL_JOYDEVICEADDED;
slime73@9876
   173
slime73@9876
   174
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slime73@9876
   175
        event.jdevice.which = numjoysticks - 1;
slime73@9876
   176
        if ((SDL_EventOK == NULL) ||
slime73@9876
   177
            (*SDL_EventOK)(SDL_EventOKParam, &event)) {
slime73@9876
   178
            SDL_PushEvent(&event);
slime73@9876
   179
        }
slime73@9876
   180
    }
slime73@9876
   181
#endif /* !SDL_EVENTS_DISABLED */
slime73@9876
   182
}
slime73@9876
   183
slime73@9876
   184
SDL_JoystickDeviceItem *
slime73@9876
   185
SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
slime73@9876
   186
{
slime73@9876
   187
    SDL_JoystickDeviceItem *prev = NULL;
slime73@9876
   188
    SDL_JoystickDeviceItem *next = NULL;
slime73@9876
   189
    SDL_JoystickDeviceItem *item = deviceList;
slime73@9876
   190
#if !SDL_EVENTS_DISABLED
slime73@9876
   191
    SDL_Event event;
slime73@9876
   192
#endif
slime73@9876
   193
slime73@9876
   194
    if (device == NULL) {
slime73@9876
   195
        return NULL;
slime73@9876
   196
    }
slime73@9876
   197
slime73@9876
   198
    next = device->next;
slime73@9876
   199
slime73@9876
   200
    while (item != NULL) {
slime73@9876
   201
        if (item == device) {
slime73@9876
   202
            break;
slime73@9876
   203
        }
slime73@9876
   204
        prev = item;
slime73@9876
   205
        item = item->next;
slime73@9876
   206
    }
slime73@9876
   207
slime73@9876
   208
    /* Unlink the device item from the device list. */
slime73@9876
   209
    if (prev) {
slime73@9876
   210
        prev->next = device->next;
slime73@9876
   211
    } else if (device == deviceList) {
slime73@9876
   212
        deviceList = device->next;
slime73@9876
   213
    }
slime73@9876
   214
slime73@9876
   215
    if (device->joystick) {
slime73@9876
   216
        device->joystick->hwdata = NULL;
slime73@9876
   217
    }
slime73@9876
   218
slime73@9876
   219
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   220
    @autoreleasepool {
slime73@9876
   221
        if (device->controller) {
slime73@9876
   222
            /* The controller was explicitly retained in the struct, so it
slime73@9876
   223
             * should be explicitly released before freeing the struct. */
slime73@9876
   224
            GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
slime73@9876
   225
            controller.controllerPausedHandler = nil;
slime73@9876
   226
            device->controller = nil;
slime73@9876
   227
        }
slime73@9876
   228
    }
slime73@9876
   229
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   230
slime73@9876
   231
    SDL_free(device->name);
slime73@9876
   232
    SDL_free(device);
slime73@9876
   233
slime73@9876
   234
    --numjoysticks;
slime73@9876
   235
slime73@9876
   236
#if !SDL_EVENTS_DISABLED
slime73@9876
   237
    event.type = SDL_JOYDEVICEREMOVED;
slime73@9876
   238
slime73@9876
   239
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slime73@9876
   240
        event.jdevice.which = device->instance_id;
slime73@9876
   241
        if ((SDL_EventOK == NULL) ||
slime73@9876
   242
            (*SDL_EventOK)(SDL_EventOKParam, &event)) {
slime73@9876
   243
            SDL_PushEvent(&event);
slime73@9876
   244
        }
slime73@9876
   245
    }
slime73@9876
   246
#endif /* !SDL_EVENTS_DISABLED */
slime73@9876
   247
slime73@9876
   248
    return next;
slime73@9876
   249
}
slouken@2765
   250
slouken@2765
   251
/* Function to scan the system for joysticks.
philipp@9311
   252
 * Joystick 0 should be the system default joystick.
slouken@2765
   253
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@2765
   254
 */
slouken@2765
   255
int
slouken@2765
   256
SDL_SYS_JoystickInit(void)
slouken@2765
   257
{
slime73@9876
   258
    @autoreleasepool {
slime73@9876
   259
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   260
        const char *hint = SDL_GetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK);
slime73@9876
   261
slime73@9876
   262
        if (!hint || SDL_atoi(hint)) {
slime73@9876
   263
            /* Default behavior, accelerometer as joystick */
slime73@9876
   264
            SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
slime73@9876
   265
        }
slime73@9876
   266
slime73@9876
   267
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   268
        /* GameController.framework was added in iOS 7. */
slime73@9876
   269
        if (![GCController class]) {
slime73@9876
   270
            return numjoysticks;
slime73@9876
   271
        }
slime73@9876
   272
slime73@9876
   273
        for (GCController *controller in [GCController controllers]) {
slime73@9876
   274
            SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   275
        }
slime73@9876
   276
slime73@9876
   277
        connectObserver = [center addObserverForName:GCControllerDidConnectNotification
slime73@9876
   278
                                              object:nil
slime73@9876
   279
                                               queue:nil
slime73@9876
   280
                                          usingBlock:^(NSNotification *note) {
slime73@9876
   281
                                              GCController *controller = note.object;
slime73@9876
   282
                                              SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   283
                                          }];
slime73@9876
   284
slime73@9876
   285
        disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
slime73@9876
   286
                                                 object:nil
slime73@9876
   287
                                                  queue:nil
slime73@9876
   288
                                             usingBlock:^(NSNotification *note) {
slime73@9876
   289
                                                 GCController *controller = note.object;
slime73@9876
   290
                                                 SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   291
                                                 while (device != NULL) {
slime73@9876
   292
                                                     if (device->controller == controller) {
slime73@9876
   293
                                                         SDL_SYS_RemoveJoystickDevice(device);
slime73@9876
   294
                                                         break;
slime73@9876
   295
                                                     }
slime73@9876
   296
                                                     device = device->next;
slime73@9876
   297
                                                 }
slime73@9876
   298
                                             }];
slime73@9876
   299
#endif /* SDL_JOYSTICK_MFI */
slime73@9489
   300
    }
slime73@9489
   301
slime73@9489
   302
    return numjoysticks;
slouken@2765
   303
}
slouken@2765
   304
slouken@6707
   305
int SDL_SYS_NumJoysticks()
slouken@6707
   306
{
slime73@9489
   307
    return numjoysticks;
slouken@6707
   308
}
slouken@6707
   309
slouken@6707
   310
void SDL_SYS_JoystickDetect()
slouken@6707
   311
{
slouken@6707
   312
}
slouken@6707
   313
slouken@2765
   314
/* Function to get the device-dependent name of a joystick */
slouken@2765
   315
const char *
slouken@6707
   316
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@2765
   317
{
slime73@9876
   318
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   319
    return device ? device->name : "Unknown";
slouken@6707
   320
}
slouken@6707
   321
slouken@6707
   322
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   323
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   324
{
slime73@9876
   325
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   326
    return device ? device->instance_id : 0;
slouken@2765
   327
}
slouken@2765
   328
slouken@2765
   329
/* Function to open a joystick for use.
philipp@9380
   330
   The joystick to open is specified by the device index.
slouken@2765
   331
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
   332
   It returns 0, or -1 if there is an error.
slouken@2765
   333
 */
slouken@2765
   334
int
slouken@6693
   335
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@2765
   336
{
slime73@9876
   337
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   338
    if (device == NULL) {
slime73@9876
   339
        return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
slime73@9876
   340
    }
slime73@9876
   341
slime73@9876
   342
    joystick->hwdata = device;
slime73@9876
   343
    joystick->instance_id = device->instance_id;
slime73@9876
   344
slime73@9876
   345
    joystick->naxes = device->naxes;
slime73@9876
   346
    joystick->nhats = device->nhats;
slime73@9876
   347
    joystick->nbuttons = device->nbuttons;
slouken@6707
   348
    joystick->nballs = 0;
slime73@9876
   349
slime73@9876
   350
    device->joystick = joystick;
slouken@8921
   351
slime73@9506
   352
    @autoreleasepool {
slime73@9876
   353
        if (device->accelerometer) {
slime73@9876
   354
            if (motionManager == nil) {
slime73@9876
   355
                motionManager = [[CMMotionManager alloc] init];
slime73@9876
   356
            }
slime73@9876
   357
slime73@9876
   358
            /* Shorter times between updates can significantly increase CPU usage. */
slime73@9876
   359
            motionManager.accelerometerUpdateInterval = 0.1;
slime73@9876
   360
            [motionManager startAccelerometerUpdates];
slime73@9876
   361
        } else {
slime73@9876
   362
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   363
            GCController *controller = device->controller;
slime73@9876
   364
            controller.controllerPausedHandler = ^(GCController *controller) {
slime73@9876
   365
                if (joystick->hwdata) {
slime73@9876
   366
                    ++joystick->hwdata->num_pause_presses;
slime73@9876
   367
                }
slime73@9876
   368
            };
slime73@9876
   369
#endif /* SDL_JOYSTICK_MFI */
slime73@9506
   370
        }
slouken@8921
   371
    }
slouken@8921
   372
slouken@6707
   373
    return 0;
slouken@6707
   374
}
slouken@6707
   375
philipp@9561
   376
/* Function to determine if this joystick is attached to the system right now */
slouken@6707
   377
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   378
{
slime73@9876
   379
    return joystick->hwdata != NULL;
slouken@2765
   380
}
slouken@2765
   381
slouken@8921
   382
static void SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
slouken@8921
   383
{
slouken@8921
   384
    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
slouken@8921
   385
    const SInt16 maxsint16 = 0x7FFF;
slouken@8921
   386
    CMAcceleration accel;
slouken@8921
   387
slime73@9506
   388
    @autoreleasepool {
slime73@9876
   389
        if (!motionManager.isAccelerometerActive) {
slime73@9506
   390
            return;
slime73@9506
   391
        }
slime73@9506
   392
slime73@9506
   393
        accel = motionManager.accelerometerData.acceleration;
slouken@8921
   394
    }
slouken@8921
   395
slouken@8921
   396
    /*
slouken@8921
   397
     Convert accelerometer data from floating point to Sint16, which is what
slouken@8921
   398
     the joystick system expects.
slouken@8921
   399
slouken@8921
   400
     To do the conversion, the data is first clamped onto the interval
slouken@8921
   401
     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@8921
   402
     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@8921
   403
slouken@8921
   404
     You can customize the clamped range of this function by modifying the
slouken@8921
   405
     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@8921
   406
slouken@8921
   407
     Once converted to Sint16, the accelerometer data no longer has coherent
slouken@8921
   408
     units. You can convert the data back to units of g-force by multiplying
slouken@8921
   409
     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@8921
   410
     */
slouken@8921
   411
slouken@8921
   412
    /* clamp the data */
slouken@8921
   413
    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
slouken@8921
   414
    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
slouken@8921
   415
    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
slouken@8921
   416
slouken@8921
   417
    /* pass in data mapped to range of SInt16 */
slime73@9876
   418
    SDL_PrivateJoystickAxis(joystick, 0,  (accel.x / maxgforce) * maxsint16);
slouken@8921
   419
    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
slime73@9876
   420
    SDL_PrivateJoystickAxis(joystick, 2,  (accel.z / maxgforce) * maxsint16);
slime73@9876
   421
}
slime73@9876
   422
slime73@9876
   423
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   424
static Uint8
slime73@9876
   425
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
slime73@9876
   426
{
slime73@9876
   427
    Uint8 hat = 0;
slime73@9876
   428
slime73@9876
   429
    if (dpad.up.isPressed) {
slime73@9876
   430
        hat |= SDL_HAT_UP;
slime73@9876
   431
    } else if (dpad.down.isPressed) {
slime73@9876
   432
        hat |= SDL_HAT_DOWN;
slime73@9876
   433
    }
slime73@9876
   434
slime73@9876
   435
    if (dpad.left.isPressed) {
slime73@9876
   436
        hat |= SDL_HAT_LEFT;
slime73@9876
   437
    } else if (dpad.right.isPressed) {
slime73@9876
   438
        hat |= SDL_HAT_RIGHT;
slime73@9876
   439
    }
slime73@9876
   440
slime73@9876
   441
    if (hat == 0) {
slime73@9876
   442
        return SDL_HAT_CENTERED;
slime73@9876
   443
    }
slime73@9876
   444
slime73@9876
   445
    return hat;
slime73@9876
   446
}
slime73@9876
   447
#endif
slime73@9876
   448
slime73@9876
   449
static void
slime73@9876
   450
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
slime73@9876
   451
{
slime73@9876
   452
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   453
    @autoreleasepool {
slime73@9876
   454
        GCController *controller = joystick->hwdata->controller;
slime73@9876
   455
        Uint8 hatstate = SDL_HAT_CENTERED;
slime73@9876
   456
        int i;
slime73@9876
   457
slime73@9876
   458
        if (controller.extendedGamepad) {
slime73@9876
   459
            GCExtendedGamepad *gamepad = controller.extendedGamepad;
slime73@9876
   460
slime73@9876
   461
            /* Axis order matches the XInput Windows mappings. */
slime73@9876
   462
            SDL_PrivateJoystickAxis(joystick, 0, (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767));
slime73@9876
   463
            SDL_PrivateJoystickAxis(joystick, 1, (Sint16) (gamepad.leftThumbstick.yAxis.value * 32767));
slime73@9876
   464
            SDL_PrivateJoystickAxis(joystick, 2, (Sint16) (gamepad.leftTrigger.value * 32767));
slime73@9876
   465
            SDL_PrivateJoystickAxis(joystick, 3, (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767));
slime73@9876
   466
            SDL_PrivateJoystickAxis(joystick, 4, (Sint16) (gamepad.rightThumbstick.yAxis.value * 32767));
slime73@9876
   467
            SDL_PrivateJoystickAxis(joystick, 5, (Sint16) (gamepad.rightTrigger.value * 32767));
slime73@9876
   468
slime73@9876
   469
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   470
slime73@9876
   471
            /* Button order matches the XInput Windows mappings. */
slime73@9876
   472
            SDL_PrivateJoystickButton(joystick, 0, gamepad.buttonA.isPressed);
slime73@9876
   473
            SDL_PrivateJoystickButton(joystick, 1, gamepad.buttonB.isPressed);
slime73@9876
   474
            SDL_PrivateJoystickButton(joystick, 2, gamepad.buttonX.isPressed);
slime73@9876
   475
            SDL_PrivateJoystickButton(joystick, 3, gamepad.buttonY.isPressed);
slime73@9876
   476
            SDL_PrivateJoystickButton(joystick, 4, gamepad.leftShoulder.isPressed);
slime73@9876
   477
            SDL_PrivateJoystickButton(joystick, 5, gamepad.rightShoulder.isPressed);
slime73@9876
   478
        } else if (controller.gamepad) {
slime73@9876
   479
            GCGamepad *gamepad = controller.gamepad;
slime73@9876
   480
slime73@9876
   481
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   482
slime73@9876
   483
            /* Button order matches the XInput Windows mappings. */
slime73@9876
   484
            SDL_PrivateJoystickButton(joystick, 0, gamepad.buttonA.isPressed);
slime73@9876
   485
            SDL_PrivateJoystickButton(joystick, 1, gamepad.buttonB.isPressed);
slime73@9876
   486
            SDL_PrivateJoystickButton(joystick, 2, gamepad.buttonX.isPressed);
slime73@9876
   487
            SDL_PrivateJoystickButton(joystick, 3, gamepad.buttonY.isPressed);
slime73@9876
   488
            SDL_PrivateJoystickButton(joystick, 4, gamepad.leftShoulder.isPressed);
slime73@9876
   489
            SDL_PrivateJoystickButton(joystick, 5, gamepad.rightShoulder.isPressed);
slime73@9876
   490
        }
slime73@9876
   491
        /* TODO: Handle micro profiles on tvOS. */
slime73@9876
   492
slime73@9876
   493
        SDL_PrivateJoystickHat(joystick, 0, hatstate);
slime73@9876
   494
slime73@9876
   495
        for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
slime73@9876
   496
            /* The pause button is always last. */
slime73@9876
   497
            Uint8 pausebutton = joystick->nbuttons - 1;
slime73@9876
   498
slime73@9876
   499
            SDL_PrivateJoystickButton(joystick, pausebutton, 1);
slime73@9876
   500
            SDL_PrivateJoystickButton(joystick, pausebutton, 0);
slime73@9876
   501
        }
slime73@9876
   502
slime73@9876
   503
        joystick->hwdata->num_pause_presses = 0;
slime73@9876
   504
    }
slime73@9876
   505
#endif
slouken@8921
   506
}
slouken@8921
   507
slouken@2765
   508
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
   509
 * This function shouldn't update the joystick structure directly,
slouken@2765
   510
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
   511
 * and update joystick device state.
slouken@2765
   512
 */
slouken@2765
   513
void
slouken@2765
   514
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
   515
{
slime73@9876
   516
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   517
slime73@9876
   518
    if (device == NULL) {
slime73@9876
   519
        return;
slime73@9876
   520
    }
slime73@9876
   521
slime73@9876
   522
    if (device->accelerometer) {
slime73@9876
   523
        SDL_SYS_AccelerometerUpdate(joystick);
slime73@9876
   524
    } else if (device->controller) {
slime73@9876
   525
        SDL_SYS_MFIJoystickUpdate(joystick);
slime73@9876
   526
    }
slouken@2765
   527
}
slouken@2765
   528
slouken@2765
   529
/* Function to close a joystick after use */
slouken@2765
   530
void
slouken@2765
   531
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   532
{
slime73@9876
   533
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   534
slime73@9876
   535
    if (device == NULL) {
slime73@9876
   536
        return;
slime73@9876
   537
    }
slime73@9876
   538
slime73@9876
   539
    device->joystick = NULL;
slime73@9876
   540
slime73@9506
   541
    @autoreleasepool {
slime73@9876
   542
        if (device->accelerometer) {
slime73@9876
   543
            [motionManager stopAccelerometerUpdates];
slime73@9876
   544
        } else if (device->controller) {
slime73@9876
   545
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   546
            GCController *controller = device->controller;
slime73@9876
   547
            controller.controllerPausedHandler = nil;
slime73@9876
   548
#endif
slime73@9876
   549
        }
slime73@9506
   550
    }
slouken@2765
   551
}
slouken@2765
   552
slouken@2765
   553
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   554
void
slouken@2765
   555
SDL_SYS_JoystickQuit(void)
slouken@2765
   556
{
slime73@9506
   557
    @autoreleasepool {
slime73@9876
   558
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   559
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   560
slime73@9876
   561
        if (connectObserver) {
slime73@9876
   562
            [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
slime73@9876
   563
            connectObserver = nil;
slime73@9876
   564
        }
slime73@9876
   565
slime73@9876
   566
        if (disconnectObserver) {
slime73@9876
   567
            [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
slime73@9876
   568
            disconnectObserver = nil;
slime73@9876
   569
        }
slime73@9876
   570
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   571
slime73@9876
   572
        while (deviceList != NULL) {
slime73@9876
   573
            SDL_SYS_RemoveJoystickDevice(deviceList);
slime73@9876
   574
        }
slime73@9876
   575
slouken@8921
   576
        motionManager = nil;
slouken@8921
   577
    }
slime73@9489
   578
slime73@9489
   579
    numjoysticks = 0;
slouken@2765
   580
}
slouken@6693
   581
slouken@6744
   582
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6693
   583
{
slime73@9876
   584
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slouken@6744
   585
    SDL_JoystickGUID guid;
slime73@9876
   586
    if (device) {
slime73@9876
   587
        guid = device->guid;
slime73@9876
   588
    } else {
slime73@9876
   589
        SDL_zero(guid);
slime73@9876
   590
    }
slouken@6693
   591
    return guid;
slouken@6693
   592
}
slouken@6693
   593
slouken@6744
   594
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   595
{
slouken@6744
   596
    SDL_JoystickGUID guid;
slime73@9876
   597
    if (joystick->hwdata) {
slime73@9876
   598
        guid = joystick->hwdata->guid;
slime73@9876
   599
    } else {
slime73@9876
   600
        SDL_zero(guid);
slime73@9876
   601
    }
slouken@6693
   602
    return guid;
slouken@6693
   603
}
slouken@6693
   604
slouken@2765
   605
/* vi: set ts=4 sw=4 expandtab: */