src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Thu, 22 Dec 2016 18:43:00 -0700
changeset 10714 a9c15922ad7b
parent 10617 346c02ff71b6
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed compile errors on various platforms
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@10351
   133
slouken@10499
   134
        controller.microGamepad.allowsRotation = SDL_GetHintBoolean(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION, SDL_FALSE);
slime73@10340
   135
    }
slime73@10340
   136
#endif /* TARGET_OS_TV */
slime73@9960
   137
slime73@9960
   138
    /* This will be set when the first button press of the controller is
slime73@9960
   139
     * detected. */
slime73@9960
   140
    controller.playerIndex = -1;
slime73@10340
   141
slime73@10340
   142
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   143
}
slime73@9876
   144
slime73@9876
   145
static void
slime73@9876
   146
SDL_SYS_AddJoystickDevice(GCController *controller, SDL_bool accelerometer)
slime73@9876
   147
{
slime73@9876
   148
    SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   149
slime73@9876
   150
    while (device != NULL) {
slime73@9876
   151
        if (device->controller == controller) {
slime73@9876
   152
            return;
slime73@9876
   153
        }
slime73@9876
   154
        device = device->next;
slime73@9876
   155
    }
slime73@9876
   156
slime73@9876
   157
    device = (SDL_JoystickDeviceItem *) SDL_malloc(sizeof(SDL_JoystickDeviceItem));
slime73@9876
   158
    if (device == NULL) {
slime73@9876
   159
        return;
slime73@9876
   160
    }
slime73@9876
   161
slime73@9876
   162
    SDL_zerop(device);
slime73@9876
   163
slime73@9876
   164
    device->accelerometer = accelerometer;
slime73@9876
   165
    device->instance_id = instancecounter++;
slime73@9876
   166
slime73@9876
   167
    if (accelerometer) {
slime73@10340
   168
#if TARGET_OS_TV
slime73@10340
   169
        SDL_free(device);
slime73@10340
   170
        return;
slime73@10340
   171
#else
slime73@9876
   172
        device->name = SDL_strdup(accelerometerName);
slime73@9876
   173
        device->naxes = 3; /* Device acceleration in the x, y, and z axes. */
slime73@9876
   174
        device->nhats = 0;
slime73@9876
   175
        device->nbuttons = 0;
slime73@9876
   176
slime73@9876
   177
        /* Use the accelerometer name as a GUID. */
slime73@9876
   178
        SDL_memcpy(&device->guid.data, device->name, SDL_min(sizeof(SDL_JoystickGUID), SDL_strlen(device->name)));
slime73@10340
   179
#endif /* TARGET_OS_TV */
slime73@9876
   180
    } else if (controller) {
slime73@9876
   181
        SDL_SYS_AddMFIJoystickDevice(device, controller);
slime73@9876
   182
    }
slime73@9876
   183
slime73@9876
   184
    if (deviceList == NULL) {
slime73@9876
   185
        deviceList = device;
slime73@9876
   186
    } else {
slime73@9876
   187
        SDL_JoystickDeviceItem *lastdevice = deviceList;
slime73@9876
   188
        while (lastdevice->next != NULL) {
slime73@9876
   189
            lastdevice = lastdevice->next;
slime73@9876
   190
        }
slime73@9876
   191
        lastdevice->next = device;
slime73@9876
   192
    }
slime73@9876
   193
slime73@9876
   194
    ++numjoysticks;
slime73@9876
   195
slouken@10226
   196
    SDL_PrivateJoystickAdded(numjoysticks - 1);
slime73@9876
   197
}
slime73@9876
   198
slime73@9879
   199
static SDL_JoystickDeviceItem *
slime73@9876
   200
SDL_SYS_RemoveJoystickDevice(SDL_JoystickDeviceItem *device)
slime73@9876
   201
{
slime73@9876
   202
    SDL_JoystickDeviceItem *prev = NULL;
slime73@9876
   203
    SDL_JoystickDeviceItem *next = NULL;
slime73@9876
   204
    SDL_JoystickDeviceItem *item = deviceList;
slime73@9876
   205
slime73@9876
   206
    if (device == NULL) {
slime73@9876
   207
        return NULL;
slime73@9876
   208
    }
slime73@9876
   209
slime73@9876
   210
    next = device->next;
slime73@9876
   211
slime73@9876
   212
    while (item != NULL) {
slime73@9876
   213
        if (item == device) {
slime73@9876
   214
            break;
slime73@9876
   215
        }
slime73@9876
   216
        prev = item;
slime73@9876
   217
        item = item->next;
slime73@9876
   218
    }
slime73@9876
   219
slime73@9876
   220
    /* Unlink the device item from the device list. */
slime73@9876
   221
    if (prev) {
slime73@9876
   222
        prev->next = device->next;
slime73@9876
   223
    } else if (device == deviceList) {
slime73@9876
   224
        deviceList = device->next;
slime73@9876
   225
    }
slime73@9876
   226
slime73@9876
   227
    if (device->joystick) {
slime73@9876
   228
        device->joystick->hwdata = NULL;
slime73@9876
   229
    }
slime73@9876
   230
slime73@9876
   231
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   232
    @autoreleasepool {
slime73@9876
   233
        if (device->controller) {
slime73@9876
   234
            /* The controller was explicitly retained in the struct, so it
slime73@9876
   235
             * should be explicitly released before freeing the struct. */
slime73@9876
   236
            GCController *controller = CFBridgingRelease((__bridge CFTypeRef)(device->controller));
slime73@9876
   237
            controller.controllerPausedHandler = nil;
slime73@9876
   238
            device->controller = nil;
slime73@9876
   239
        }
slime73@9876
   240
    }
slime73@9876
   241
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   242
slime73@9876
   243
    --numjoysticks;
slime73@9876
   244
slouken@10226
   245
	SDL_PrivateJoystickRemoved(device->instance_id);
slime73@9876
   246
philipp@9878
   247
    SDL_free(device->name);
philipp@9878
   248
    SDL_free(device);
philipp@9878
   249
slime73@9876
   250
    return next;
slime73@9876
   251
}
slouken@2765
   252
slime73@10351
   253
#if TARGET_OS_TV
slime73@10351
   254
static void
slime73@10351
   255
SDL_AppleTVRemoteRotationHintChanged(void *udata, const char *name, const char *oldValue, const char *newValue)
slime73@10351
   256
{
slime73@10351
   257
    BOOL allowRotation = newValue != NULL && *newValue != '0';
slime73@10351
   258
slime73@10351
   259
    @autoreleasepool {
slime73@10351
   260
        for (GCController *controller in [GCController controllers]) {
slime73@10351
   261
            if (controller.microGamepad) {
slime73@10351
   262
                controller.microGamepad.allowsRotation = allowRotation;
slime73@10351
   263
            }
slime73@10351
   264
        }
slime73@10351
   265
    }
slime73@10351
   266
}
slime73@10351
   267
#endif /* TARGET_OS_TV */
slime73@10351
   268
slouken@2765
   269
/* Function to scan the system for joysticks.
philipp@9311
   270
 * Joystick 0 should be the system default joystick.
slouken@2765
   271
 * It should return 0, or -1 on an unrecoverable fatal error.
slouken@2765
   272
 */
slouken@2765
   273
int
slouken@2765
   274
SDL_SYS_JoystickInit(void)
slouken@2765
   275
{
slime73@9876
   276
    @autoreleasepool {
slime73@9876
   277
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@10340
   278
slime73@10340
   279
#if !TARGET_OS_TV
slouken@10499
   280
        if (SDL_GetHintBoolean(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, SDL_TRUE)) {
slime73@9876
   281
            /* Default behavior, accelerometer as joystick */
slime73@9876
   282
            SDL_SYS_AddJoystickDevice(nil, SDL_TRUE);
slime73@9876
   283
        }
slime73@10340
   284
#endif /* !TARGET_OS_TV */
slime73@9876
   285
slime73@9876
   286
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   287
        /* GameController.framework was added in iOS 7. */
slime73@9876
   288
        if (![GCController class]) {
slime73@9876
   289
            return numjoysticks;
slime73@9876
   290
        }
slime73@9876
   291
slime73@9876
   292
        for (GCController *controller in [GCController controllers]) {
slime73@9876
   293
            SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   294
        }
slime73@9876
   295
slime73@10351
   296
#if TARGET_OS_TV
slime73@10351
   297
        SDL_AddHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
slime73@10351
   298
                            SDL_AppleTVRemoteRotationHintChanged, NULL);
slime73@10351
   299
#endif /* TARGET_OS_TV */
slime73@10351
   300
slime73@9876
   301
        connectObserver = [center addObserverForName:GCControllerDidConnectNotification
slime73@9876
   302
                                              object:nil
slime73@9876
   303
                                               queue:nil
slime73@9876
   304
                                          usingBlock:^(NSNotification *note) {
slime73@9876
   305
                                              GCController *controller = note.object;
slime73@9876
   306
                                              SDL_SYS_AddJoystickDevice(controller, SDL_FALSE);
slime73@9876
   307
                                          }];
slime73@9876
   308
slime73@9876
   309
        disconnectObserver = [center addObserverForName:GCControllerDidDisconnectNotification
slime73@9876
   310
                                                 object:nil
slime73@9876
   311
                                                  queue:nil
slime73@9876
   312
                                             usingBlock:^(NSNotification *note) {
slime73@9876
   313
                                                 GCController *controller = note.object;
slime73@9876
   314
                                                 SDL_JoystickDeviceItem *device = deviceList;
slime73@9876
   315
                                                 while (device != NULL) {
slime73@9876
   316
                                                     if (device->controller == controller) {
slime73@9876
   317
                                                         SDL_SYS_RemoveJoystickDevice(device);
slime73@9876
   318
                                                         break;
slime73@9876
   319
                                                     }
slime73@9876
   320
                                                     device = device->next;
slime73@9876
   321
                                                 }
slime73@9876
   322
                                             }];
slime73@9876
   323
#endif /* SDL_JOYSTICK_MFI */
slime73@9489
   324
    }
slime73@9489
   325
slime73@9489
   326
    return numjoysticks;
slouken@2765
   327
}
slouken@2765
   328
philipp@10617
   329
int
philipp@10617
   330
SDL_SYS_NumJoysticks(void)
slouken@6707
   331
{
slime73@9489
   332
    return numjoysticks;
slouken@6707
   333
}
slouken@6707
   334
philipp@10617
   335
void
philipp@10617
   336
SDL_SYS_JoystickDetect(void)
slouken@6707
   337
{
slouken@6707
   338
}
slouken@6707
   339
slouken@2765
   340
/* Function to get the device-dependent name of a joystick */
slouken@2765
   341
const char *
slouken@6707
   342
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@2765
   343
{
slime73@9876
   344
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   345
    return device ? device->name : "Unknown";
slouken@6707
   346
}
slouken@6707
   347
slouken@6707
   348
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   349
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   350
{
slime73@9876
   351
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   352
    return device ? device->instance_id : 0;
slouken@2765
   353
}
slouken@2765
   354
slouken@2765
   355
/* Function to open a joystick for use.
philipp@9380
   356
   The joystick to open is specified by the device index.
slouken@2765
   357
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
   358
   It returns 0, or -1 if there is an error.
slouken@2765
   359
 */
slouken@2765
   360
int
slouken@6693
   361
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@2765
   362
{
slime73@9876
   363
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   364
    if (device == NULL) {
slime73@9876
   365
        return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
slime73@9876
   366
    }
slime73@9876
   367
slime73@9876
   368
    joystick->hwdata = device;
slime73@9876
   369
    joystick->instance_id = device->instance_id;
slime73@9876
   370
slime73@9876
   371
    joystick->naxes = device->naxes;
slime73@9876
   372
    joystick->nhats = device->nhats;
slime73@9876
   373
    joystick->nbuttons = device->nbuttons;
slouken@6707
   374
    joystick->nballs = 0;
slime73@9876
   375
slime73@9876
   376
    device->joystick = joystick;
slouken@8921
   377
slime73@9506
   378
    @autoreleasepool {
slime73@9876
   379
        if (device->accelerometer) {
slime73@10340
   380
#if !TARGET_OS_TV
slime73@9876
   381
            if (motionManager == nil) {
slime73@9876
   382
                motionManager = [[CMMotionManager alloc] init];
slime73@9876
   383
            }
slime73@9876
   384
slime73@9876
   385
            /* Shorter times between updates can significantly increase CPU usage. */
slime73@9876
   386
            motionManager.accelerometerUpdateInterval = 0.1;
slime73@9876
   387
            [motionManager startAccelerometerUpdates];
slime73@10340
   388
#endif /* !TARGET_OS_TV */
slime73@9876
   389
        } else {
slime73@9876
   390
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   391
            GCController *controller = device->controller;
slime73@9960
   392
            controller.controllerPausedHandler = ^(GCController *c) {
slime73@9876
   393
                if (joystick->hwdata) {
slime73@9876
   394
                    ++joystick->hwdata->num_pause_presses;
slime73@9876
   395
                }
slime73@9876
   396
            };
slime73@9876
   397
#endif /* SDL_JOYSTICK_MFI */
slime73@9506
   398
        }
slouken@8921
   399
    }
slouken@8921
   400
slouken@6707
   401
    return 0;
slouken@6707
   402
}
slouken@6707
   403
philipp@9561
   404
/* Function to determine if this joystick is attached to the system right now */
slime73@9879
   405
SDL_bool
slime73@9879
   406
SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   407
{
slime73@9876
   408
    return joystick->hwdata != NULL;
slouken@2765
   409
}
slouken@2765
   410
slime73@9879
   411
static void
slime73@9879
   412
SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
slouken@8921
   413
{
slime73@10340
   414
#if !TARGET_OS_TV
slouken@8921
   415
    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
slouken@8921
   416
    const SInt16 maxsint16 = 0x7FFF;
slouken@8921
   417
    CMAcceleration accel;
slouken@8921
   418
slime73@9506
   419
    @autoreleasepool {
slime73@9876
   420
        if (!motionManager.isAccelerometerActive) {
slime73@9506
   421
            return;
slime73@9506
   422
        }
slime73@9506
   423
slime73@9506
   424
        accel = motionManager.accelerometerData.acceleration;
slouken@8921
   425
    }
slouken@8921
   426
slouken@8921
   427
    /*
slouken@8921
   428
     Convert accelerometer data from floating point to Sint16, which is what
slouken@8921
   429
     the joystick system expects.
slouken@8921
   430
slouken@8921
   431
     To do the conversion, the data is first clamped onto the interval
slouken@8921
   432
     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@8921
   433
     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@8921
   434
slouken@8921
   435
     You can customize the clamped range of this function by modifying the
slouken@8921
   436
     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@8921
   437
slouken@8921
   438
     Once converted to Sint16, the accelerometer data no longer has coherent
slouken@8921
   439
     units. You can convert the data back to units of g-force by multiplying
slouken@8921
   440
     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@8921
   441
     */
slouken@8921
   442
slouken@8921
   443
    /* clamp the data */
slouken@8921
   444
    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
slouken@8921
   445
    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
slouken@8921
   446
    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
slouken@8921
   447
slouken@8921
   448
    /* pass in data mapped to range of SInt16 */
slime73@9876
   449
    SDL_PrivateJoystickAxis(joystick, 0,  (accel.x / maxgforce) * maxsint16);
slouken@8921
   450
    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
slime73@9876
   451
    SDL_PrivateJoystickAxis(joystick, 2,  (accel.z / maxgforce) * maxsint16);
slime73@10340
   452
#endif /* !TARGET_OS_TV */
slime73@9876
   453
}
slime73@9876
   454
slime73@9876
   455
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   456
static Uint8
slime73@9876
   457
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
slime73@9876
   458
{
slime73@9876
   459
    Uint8 hat = 0;
slime73@9876
   460
slime73@9876
   461
    if (dpad.up.isPressed) {
slime73@9876
   462
        hat |= SDL_HAT_UP;
slime73@9876
   463
    } else if (dpad.down.isPressed) {
slime73@9876
   464
        hat |= SDL_HAT_DOWN;
slime73@9876
   465
    }
slime73@9876
   466
slime73@9876
   467
    if (dpad.left.isPressed) {
slime73@9876
   468
        hat |= SDL_HAT_LEFT;
slime73@9876
   469
    } else if (dpad.right.isPressed) {
slime73@9876
   470
        hat |= SDL_HAT_RIGHT;
slime73@9876
   471
    }
slime73@9876
   472
slime73@9876
   473
    if (hat == 0) {
slime73@9876
   474
        return SDL_HAT_CENTERED;
slime73@9876
   475
    }
slime73@9876
   476
slime73@9876
   477
    return hat;
slime73@9876
   478
}
slime73@9876
   479
#endif
slime73@9876
   480
slime73@9876
   481
static void
slime73@9876
   482
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
slime73@9876
   483
{
slime73@10340
   484
#if SDL_JOYSTICK_MFI
slime73@9876
   485
    @autoreleasepool {
slime73@9876
   486
        GCController *controller = joystick->hwdata->controller;
slime73@9876
   487
        Uint8 hatstate = SDL_HAT_CENTERED;
slime73@9876
   488
        int i;
slime73@9960
   489
        int updateplayerindex = 0;
slime73@9876
   490
slime73@9876
   491
        if (controller.extendedGamepad) {
slime73@9876
   492
            GCExtendedGamepad *gamepad = controller.extendedGamepad;
slime73@9876
   493
slime73@9876
   494
            /* Axis order matches the XInput Windows mappings. */
slime73@9960
   495
            Sint16 axes[] = {
slime73@9960
   496
                (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
slime73@9960
   497
                (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
slime73@9960
   498
                (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
slime73@9960
   499
                (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
slime73@9960
   500
                (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
slime73@9960
   501
                (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
slime73@9960
   502
            };
slime73@9960
   503
slime73@9960
   504
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   505
            Uint8 buttons[] = {
slime73@9960
   506
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   507
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   508
                gamepad.leftShoulder.isPressed,
slime73@9960
   509
                gamepad.rightShoulder.isPressed,
slime73@9960
   510
            };
slime73@9876
   511
slime73@9876
   512
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   513
slime73@9960
   514
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@9960
   515
                /* The triggers (axes 2 and 5) are resting at -32768 but SDL
slime73@9960
   516
                 * initializes its values to 0. We only want to make sure the
slime73@9960
   517
                 * player index is up to date if the user actually moves an axis. */
slime73@9960
   518
                if ((i != 2 && i != 5) || axes[i] != -32768) {
slouken@10714
   519
                    updateplayerindex |= (joystick->axes[i].value != axes[i]);
slime73@9960
   520
                }
slime73@9960
   521
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@9960
   522
            }
slime73@9960
   523
slime73@9960
   524
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   525
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   526
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   527
            }
slime73@9876
   528
        } else if (controller.gamepad) {
slime73@9876
   529
            GCGamepad *gamepad = controller.gamepad;
slime73@9876
   530
slime73@9960
   531
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   532
            Uint8 buttons[] = {
slime73@9960
   533
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   534
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   535
                gamepad.leftShoulder.isPressed,
slime73@9960
   536
                gamepad.rightShoulder.isPressed,
slime73@9960
   537
            };
slime73@9960
   538
slime73@9876
   539
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   540
slime73@9960
   541
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   542
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   543
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   544
            }
slime73@9876
   545
        }
slime73@10340
   546
#if TARGET_OS_TV
slime73@10340
   547
        else if (controller.microGamepad) {
slime73@10340
   548
            GCMicroGamepad *gamepad = controller.microGamepad;
slime73@10340
   549
slime73@10340
   550
            Sint16 axes[] = {
slime73@10340
   551
                (Sint16) (gamepad.dpad.xAxis.value * 32767),
slime73@10340
   552
                (Sint16) (gamepad.dpad.yAxis.value * -32767),
slime73@10340
   553
            };
slime73@10340
   554
slime73@10340
   555
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@10340
   556
                updateplayerindex |= (joystick->axes[i] != axes[i]);
slime73@10340
   557
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@10340
   558
            }
slime73@10340
   559
slime73@10340
   560
            /* Apparently the dpad values are not accurate enough to be useful. */
slime73@10340
   561
            /* hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad); */
slime73@10340
   562
slime73@10340
   563
            Uint8 buttons[] = {
slime73@10340
   564
                gamepad.buttonA.isPressed,
slime73@10340
   565
                gamepad.buttonX.isPressed,
slime73@10340
   566
            };
slime73@10340
   567
slime73@10340
   568
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@10340
   569
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@10340
   570
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@10340
   571
            }
slime73@10340
   572
slime73@10340
   573
            /* TODO: Figure out what to do with reportsAbsoluteDpadValues */
slime73@10340
   574
        }
slime73@10340
   575
#endif /* TARGET_OS_TV */
slime73@9876
   576
slime73@9960
   577
        if (joystick->nhats > 0) {
slime73@9960
   578
            updateplayerindex |= (joystick->hats[0] != hatstate);
slime73@9960
   579
            SDL_PrivateJoystickHat(joystick, 0, hatstate);
slime73@9960
   580
        }
slime73@9876
   581
slime73@9876
   582
        for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
slime73@9876
   583
            /* The pause button is always last. */
slime73@9876
   584
            Uint8 pausebutton = joystick->nbuttons - 1;
slime73@9876
   585
slime73@9960
   586
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
slime73@9960
   587
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
slime73@9960
   588
slime73@9960
   589
            updateplayerindex = YES;
slime73@9876
   590
        }
slime73@9876
   591
slime73@9876
   592
        joystick->hwdata->num_pause_presses = 0;
slime73@9960
   593
slime73@9960
   594
        if (updateplayerindex && controller.playerIndex == -1) {
slime73@9960
   595
            BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
slime73@9960
   596
slime73@9960
   597
            /* Find the player index of all other connected controllers. */
slime73@9960
   598
            for (GCController *c in [GCController controllers]) {
slime73@9960
   599
                if (c != controller && c.playerIndex >= 0) {
slime73@9960
   600
                    usedPlayerIndexSlots[c.playerIndex] = YES;
slime73@9960
   601
                }
slime73@9960
   602
            }
slime73@9960
   603
slime73@9960
   604
            /* Set this controller's player index to the first unused index.
slime73@9960
   605
             * FIXME: This logic isn't great... but SDL doesn't expose this
slime73@9960
   606
             * concept in its external API, so we don't have much to go on. */
slime73@9960
   607
            for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
slime73@9960
   608
                if (!usedPlayerIndexSlots[i]) {
slime73@9960
   609
                    controller.playerIndex = i;
slime73@9960
   610
                    break;
slime73@9960
   611
                }
slime73@9960
   612
            }
slime73@9960
   613
        }
slime73@9876
   614
    }
slime73@10340
   615
#endif /* SDL_JOYSTICK_MFI */
slouken@8921
   616
}
slouken@8921
   617
slouken@2765
   618
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
   619
 * This function shouldn't update the joystick structure directly,
slouken@2765
   620
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
   621
 * and update joystick device state.
slouken@2765
   622
 */
slouken@2765
   623
void
slouken@2765
   624
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
   625
{
slime73@9876
   626
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   627
slime73@9876
   628
    if (device == NULL) {
slime73@9876
   629
        return;
slime73@9876
   630
    }
slime73@9876
   631
slime73@9876
   632
    if (device->accelerometer) {
slime73@9876
   633
        SDL_SYS_AccelerometerUpdate(joystick);
slime73@9876
   634
    } else if (device->controller) {
slime73@9876
   635
        SDL_SYS_MFIJoystickUpdate(joystick);
slime73@9876
   636
    }
slouken@2765
   637
}
slouken@2765
   638
slouken@2765
   639
/* Function to close a joystick after use */
slouken@2765
   640
void
slouken@2765
   641
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   642
{
slime73@9876
   643
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   644
slime73@9876
   645
    if (device == NULL) {
slime73@9876
   646
        return;
slime73@9876
   647
    }
slime73@9876
   648
slime73@9876
   649
    device->joystick = NULL;
slime73@9876
   650
slime73@9506
   651
    @autoreleasepool {
slime73@9876
   652
        if (device->accelerometer) {
slime73@10340
   653
#if !TARGET_OS_TV
slime73@9876
   654
            [motionManager stopAccelerometerUpdates];
slime73@10340
   655
#endif /* !TARGET_OS_TV */
slime73@9876
   656
        } else if (device->controller) {
slime73@9876
   657
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   658
            GCController *controller = device->controller;
slime73@9876
   659
            controller.controllerPausedHandler = nil;
slime73@9960
   660
            controller.playerIndex = -1;
slime73@9876
   661
#endif
slime73@9876
   662
        }
slime73@9506
   663
    }
slouken@2765
   664
}
slouken@2765
   665
slouken@2765
   666
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   667
void
slouken@2765
   668
SDL_SYS_JoystickQuit(void)
slouken@2765
   669
{
slime73@9506
   670
    @autoreleasepool {
slime73@9876
   671
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   672
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   673
slime73@9876
   674
        if (connectObserver) {
slime73@9876
   675
            [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
slime73@9876
   676
            connectObserver = nil;
slime73@9876
   677
        }
slime73@9876
   678
slime73@9876
   679
        if (disconnectObserver) {
slime73@9876
   680
            [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
slime73@9876
   681
            disconnectObserver = nil;
slime73@9876
   682
        }
slime73@10351
   683
slime73@10351
   684
#if TARGET_OS_TV
slime73@10351
   685
        SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
slime73@10351
   686
                            SDL_AppleTVRemoteRotationHintChanged, NULL);
slime73@10351
   687
#endif /* TARGET_OS_TV */
slime73@9876
   688
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   689
slime73@9876
   690
        while (deviceList != NULL) {
slime73@9876
   691
            SDL_SYS_RemoveJoystickDevice(deviceList);
slime73@9876
   692
        }
slime73@9876
   693
slime73@10340
   694
#if !TARGET_OS_TV
slouken@8921
   695
        motionManager = nil;
slime73@10340
   696
#endif /* !TARGET_OS_TV */
slouken@8921
   697
    }
slime73@9489
   698
slime73@9489
   699
    numjoysticks = 0;
slouken@2765
   700
}
slouken@6693
   701
slime73@9879
   702
SDL_JoystickGUID
slime73@9879
   703
SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6693
   704
{
slime73@9876
   705
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slouken@6744
   706
    SDL_JoystickGUID guid;
slime73@9876
   707
    if (device) {
slime73@9876
   708
        guid = device->guid;
slime73@9876
   709
    } else {
slime73@9876
   710
        SDL_zero(guid);
slime73@9876
   711
    }
slouken@6693
   712
    return guid;
slouken@6693
   713
}
slouken@6693
   714
slime73@9879
   715
SDL_JoystickGUID
slime73@9879
   716
SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   717
{
slouken@6744
   718
    SDL_JoystickGUID guid;
slime73@9876
   719
    if (joystick->hwdata) {
slime73@9876
   720
        guid = joystick->hwdata->guid;
slime73@9876
   721
    } else {
slime73@9876
   722
        SDL_zero(guid);
slime73@9876
   723
    }
slouken@6693
   724
    return guid;
slouken@6693
   725
}
slouken@6693
   726
slouken@2765
   727
/* vi: set ts=4 sw=4 expandtab: */