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