src/joystick/iphoneos/SDL_sysjoystick.m
author Alex Szpakowski <slime73@gmail.com>
Tue, 13 Sep 2016 22:18:06 -0300
changeset 10340 5724f5087acd
parent 10228 9a277db2806d
child 10341 75ac5b0ed013
permissions -rw-r--r--
Initial Apple TV / tvOS support.

The Apple TV remote is currently exposed as a joystick with its touch surface treated as two axes. Key presses are also generated when its buttons and touch surface are used.

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