src/joystick/iphoneos/SDL_sysjoystick.m
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Oct 2016 23:40:44 -0700
changeset 10499 363c1c7e7a41
parent 10351 12f90eb6b52b
child 10617 346c02ff71b6
permissions -rw-r--r--
Implemented SDL_GetHintBoolean() to make it easier to check boolean hints
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
slouken@6707
   329
int SDL_SYS_NumJoysticks()
slouken@6707
   330
{
slime73@9489
   331
    return numjoysticks;
slouken@6707
   332
}
slouken@6707
   333
slouken@6707
   334
void SDL_SYS_JoystickDetect()
slouken@6707
   335
{
slouken@6707
   336
}
slouken@6707
   337
slouken@2765
   338
/* Function to get the device-dependent name of a joystick */
slouken@2765
   339
const char *
slouken@6707
   340
SDL_SYS_JoystickNameForDeviceIndex(int device_index)
slouken@2765
   341
{
slime73@9876
   342
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   343
    return device ? device->name : "Unknown";
slouken@6707
   344
}
slouken@6707
   345
slouken@6707
   346
/* Function to perform the mapping from device index to the instance id for this index */
slouken@6707
   347
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
slouken@6707
   348
{
slime73@9876
   349
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   350
    return device ? device->instance_id : 0;
slouken@2765
   351
}
slouken@2765
   352
slouken@2765
   353
/* Function to open a joystick for use.
philipp@9380
   354
   The joystick to open is specified by the device index.
slouken@2765
   355
   This should fill the nbuttons and naxes fields of the joystick structure.
slouken@2765
   356
   It returns 0, or -1 if there is an error.
slouken@2765
   357
 */
slouken@2765
   358
int
slouken@6693
   359
SDL_SYS_JoystickOpen(SDL_Joystick * joystick, int device_index)
slouken@2765
   360
{
slime73@9876
   361
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slime73@9876
   362
    if (device == NULL) {
slime73@9876
   363
        return SDL_SetError("Could not open Joystick: no hardware device for the specified index");
slime73@9876
   364
    }
slime73@9876
   365
slime73@9876
   366
    joystick->hwdata = device;
slime73@9876
   367
    joystick->instance_id = device->instance_id;
slime73@9876
   368
slime73@9876
   369
    joystick->naxes = device->naxes;
slime73@9876
   370
    joystick->nhats = device->nhats;
slime73@9876
   371
    joystick->nbuttons = device->nbuttons;
slouken@6707
   372
    joystick->nballs = 0;
slime73@9876
   373
slime73@9876
   374
    device->joystick = joystick;
slouken@8921
   375
slime73@9506
   376
    @autoreleasepool {
slime73@9876
   377
        if (device->accelerometer) {
slime73@10340
   378
#if !TARGET_OS_TV
slime73@9876
   379
            if (motionManager == nil) {
slime73@9876
   380
                motionManager = [[CMMotionManager alloc] init];
slime73@9876
   381
            }
slime73@9876
   382
slime73@9876
   383
            /* Shorter times between updates can significantly increase CPU usage. */
slime73@9876
   384
            motionManager.accelerometerUpdateInterval = 0.1;
slime73@9876
   385
            [motionManager startAccelerometerUpdates];
slime73@10340
   386
#endif /* !TARGET_OS_TV */
slime73@9876
   387
        } else {
slime73@9876
   388
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   389
            GCController *controller = device->controller;
slime73@9960
   390
            controller.controllerPausedHandler = ^(GCController *c) {
slime73@9876
   391
                if (joystick->hwdata) {
slime73@9876
   392
                    ++joystick->hwdata->num_pause_presses;
slime73@9876
   393
                }
slime73@9876
   394
            };
slime73@9876
   395
#endif /* SDL_JOYSTICK_MFI */
slime73@9506
   396
        }
slouken@8921
   397
    }
slouken@8921
   398
slouken@6707
   399
    return 0;
slouken@6707
   400
}
slouken@6707
   401
philipp@9561
   402
/* Function to determine if this joystick is attached to the system right now */
slime73@9879
   403
SDL_bool
slime73@9879
   404
SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
slouken@6707
   405
{
slime73@9876
   406
    return joystick->hwdata != NULL;
slouken@2765
   407
}
slouken@2765
   408
slime73@9879
   409
static void
slime73@9879
   410
SDL_SYS_AccelerometerUpdate(SDL_Joystick * joystick)
slouken@8921
   411
{
slime73@10340
   412
#if !TARGET_OS_TV
slouken@8921
   413
    const float maxgforce = SDL_IPHONE_MAX_GFORCE;
slouken@8921
   414
    const SInt16 maxsint16 = 0x7FFF;
slouken@8921
   415
    CMAcceleration accel;
slouken@8921
   416
slime73@9506
   417
    @autoreleasepool {
slime73@9876
   418
        if (!motionManager.isAccelerometerActive) {
slime73@9506
   419
            return;
slime73@9506
   420
        }
slime73@9506
   421
slime73@9506
   422
        accel = motionManager.accelerometerData.acceleration;
slouken@8921
   423
    }
slouken@8921
   424
slouken@8921
   425
    /*
slouken@8921
   426
     Convert accelerometer data from floating point to Sint16, which is what
slouken@8921
   427
     the joystick system expects.
slouken@8921
   428
slouken@8921
   429
     To do the conversion, the data is first clamped onto the interval
slouken@8921
   430
     [-SDL_IPHONE_MAX_G_FORCE, SDL_IPHONE_MAX_G_FORCE], then the data is multiplied
slouken@8921
   431
     by MAX_SINT16 so that it is mapped to the full range of an Sint16.
slouken@8921
   432
slouken@8921
   433
     You can customize the clamped range of this function by modifying the
slouken@8921
   434
     SDL_IPHONE_MAX_GFORCE macro in SDL_config_iphoneos.h.
slouken@8921
   435
slouken@8921
   436
     Once converted to Sint16, the accelerometer data no longer has coherent
slouken@8921
   437
     units. You can convert the data back to units of g-force by multiplying
slouken@8921
   438
     it in your application's code by SDL_IPHONE_MAX_GFORCE / 0x7FFF.
slouken@8921
   439
     */
slouken@8921
   440
slouken@8921
   441
    /* clamp the data */
slouken@8921
   442
    accel.x = SDL_min(SDL_max(accel.x, -maxgforce), maxgforce);
slouken@8921
   443
    accel.y = SDL_min(SDL_max(accel.y, -maxgforce), maxgforce);
slouken@8921
   444
    accel.z = SDL_min(SDL_max(accel.z, -maxgforce), maxgforce);
slouken@8921
   445
slouken@8921
   446
    /* pass in data mapped to range of SInt16 */
slime73@9876
   447
    SDL_PrivateJoystickAxis(joystick, 0,  (accel.x / maxgforce) * maxsint16);
slouken@8921
   448
    SDL_PrivateJoystickAxis(joystick, 1, -(accel.y / maxgforce) * maxsint16);
slime73@9876
   449
    SDL_PrivateJoystickAxis(joystick, 2,  (accel.z / maxgforce) * maxsint16);
slime73@10340
   450
#endif /* !TARGET_OS_TV */
slime73@9876
   451
}
slime73@9876
   452
slime73@9876
   453
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   454
static Uint8
slime73@9876
   455
SDL_SYS_MFIJoystickHatStateForDPad(GCControllerDirectionPad *dpad)
slime73@9876
   456
{
slime73@9876
   457
    Uint8 hat = 0;
slime73@9876
   458
slime73@9876
   459
    if (dpad.up.isPressed) {
slime73@9876
   460
        hat |= SDL_HAT_UP;
slime73@9876
   461
    } else if (dpad.down.isPressed) {
slime73@9876
   462
        hat |= SDL_HAT_DOWN;
slime73@9876
   463
    }
slime73@9876
   464
slime73@9876
   465
    if (dpad.left.isPressed) {
slime73@9876
   466
        hat |= SDL_HAT_LEFT;
slime73@9876
   467
    } else if (dpad.right.isPressed) {
slime73@9876
   468
        hat |= SDL_HAT_RIGHT;
slime73@9876
   469
    }
slime73@9876
   470
slime73@9876
   471
    if (hat == 0) {
slime73@9876
   472
        return SDL_HAT_CENTERED;
slime73@9876
   473
    }
slime73@9876
   474
slime73@9876
   475
    return hat;
slime73@9876
   476
}
slime73@9876
   477
#endif
slime73@9876
   478
slime73@9876
   479
static void
slime73@9876
   480
SDL_SYS_MFIJoystickUpdate(SDL_Joystick * joystick)
slime73@9876
   481
{
slime73@10340
   482
#if SDL_JOYSTICK_MFI
slime73@9876
   483
    @autoreleasepool {
slime73@9876
   484
        GCController *controller = joystick->hwdata->controller;
slime73@9876
   485
        Uint8 hatstate = SDL_HAT_CENTERED;
slime73@9876
   486
        int i;
slime73@9960
   487
        int updateplayerindex = 0;
slime73@9876
   488
slime73@9876
   489
        if (controller.extendedGamepad) {
slime73@9876
   490
            GCExtendedGamepad *gamepad = controller.extendedGamepad;
slime73@9876
   491
slime73@9876
   492
            /* Axis order matches the XInput Windows mappings. */
slime73@9960
   493
            Sint16 axes[] = {
slime73@9960
   494
                (Sint16) (gamepad.leftThumbstick.xAxis.value * 32767),
slime73@9960
   495
                (Sint16) (gamepad.leftThumbstick.yAxis.value * -32767),
slime73@9960
   496
                (Sint16) ((gamepad.leftTrigger.value * 65535) - 32768),
slime73@9960
   497
                (Sint16) (gamepad.rightThumbstick.xAxis.value * 32767),
slime73@9960
   498
                (Sint16) (gamepad.rightThumbstick.yAxis.value * -32767),
slime73@9960
   499
                (Sint16) ((gamepad.rightTrigger.value * 65535) - 32768),
slime73@9960
   500
            };
slime73@9960
   501
slime73@9960
   502
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   503
            Uint8 buttons[] = {
slime73@9960
   504
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   505
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   506
                gamepad.leftShoulder.isPressed,
slime73@9960
   507
                gamepad.rightShoulder.isPressed,
slime73@9960
   508
            };
slime73@9876
   509
slime73@9876
   510
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   511
slime73@9960
   512
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@9960
   513
                /* The triggers (axes 2 and 5) are resting at -32768 but SDL
slime73@9960
   514
                 * initializes its values to 0. We only want to make sure the
slime73@9960
   515
                 * player index is up to date if the user actually moves an axis. */
slime73@9960
   516
                if ((i != 2 && i != 5) || axes[i] != -32768) {
slime73@9960
   517
                    updateplayerindex |= (joystick->axes[i] != axes[i]);
slime73@9960
   518
                }
slime73@9960
   519
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@9960
   520
            }
slime73@9960
   521
slime73@9960
   522
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   523
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   524
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   525
            }
slime73@9876
   526
        } else if (controller.gamepad) {
slime73@9876
   527
            GCGamepad *gamepad = controller.gamepad;
slime73@9876
   528
slime73@9960
   529
            /* Button order matches the XInput Windows mappings. */
slime73@9960
   530
            Uint8 buttons[] = {
slime73@9960
   531
                gamepad.buttonA.isPressed, gamepad.buttonB.isPressed,
slime73@9960
   532
                gamepad.buttonX.isPressed, gamepad.buttonY.isPressed,
slime73@9960
   533
                gamepad.leftShoulder.isPressed,
slime73@9960
   534
                gamepad.rightShoulder.isPressed,
slime73@9960
   535
            };
slime73@9960
   536
slime73@9876
   537
            hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad);
slime73@9876
   538
slime73@9960
   539
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@9960
   540
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@9960
   541
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@9960
   542
            }
slime73@9876
   543
        }
slime73@10340
   544
#if TARGET_OS_TV
slime73@10340
   545
        else if (controller.microGamepad) {
slime73@10340
   546
            GCMicroGamepad *gamepad = controller.microGamepad;
slime73@10340
   547
slime73@10340
   548
            Sint16 axes[] = {
slime73@10340
   549
                (Sint16) (gamepad.dpad.xAxis.value * 32767),
slime73@10340
   550
                (Sint16) (gamepad.dpad.yAxis.value * -32767),
slime73@10340
   551
            };
slime73@10340
   552
slime73@10340
   553
            for (i = 0; i < SDL_arraysize(axes); i++) {
slime73@10340
   554
                updateplayerindex |= (joystick->axes[i] != axes[i]);
slime73@10340
   555
                SDL_PrivateJoystickAxis(joystick, i, axes[i]);
slime73@10340
   556
            }
slime73@10340
   557
slime73@10340
   558
            /* Apparently the dpad values are not accurate enough to be useful. */
slime73@10340
   559
            /* hatstate = SDL_SYS_MFIJoystickHatStateForDPad(gamepad.dpad); */
slime73@10340
   560
slime73@10340
   561
            Uint8 buttons[] = {
slime73@10340
   562
                gamepad.buttonA.isPressed,
slime73@10340
   563
                gamepad.buttonX.isPressed,
slime73@10340
   564
            };
slime73@10340
   565
slime73@10340
   566
            for (i = 0; i < SDL_arraysize(buttons); i++) {
slime73@10340
   567
                updateplayerindex |= (joystick->buttons[i] != buttons[i]);
slime73@10340
   568
                SDL_PrivateJoystickButton(joystick, i, buttons[i]);
slime73@10340
   569
            }
slime73@10340
   570
slime73@10340
   571
            /* TODO: Figure out what to do with reportsAbsoluteDpadValues */
slime73@10340
   572
        }
slime73@10340
   573
#endif /* TARGET_OS_TV */
slime73@9876
   574
slime73@9960
   575
        if (joystick->nhats > 0) {
slime73@9960
   576
            updateplayerindex |= (joystick->hats[0] != hatstate);
slime73@9960
   577
            SDL_PrivateJoystickHat(joystick, 0, hatstate);
slime73@9960
   578
        }
slime73@9876
   579
slime73@9876
   580
        for (i = 0; i < joystick->hwdata->num_pause_presses; i++) {
slime73@9876
   581
            /* The pause button is always last. */
slime73@9876
   582
            Uint8 pausebutton = joystick->nbuttons - 1;
slime73@9876
   583
slime73@9960
   584
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_PRESSED);
slime73@9960
   585
            SDL_PrivateJoystickButton(joystick, pausebutton, SDL_RELEASED);
slime73@9960
   586
slime73@9960
   587
            updateplayerindex = YES;
slime73@9876
   588
        }
slime73@9876
   589
slime73@9876
   590
        joystick->hwdata->num_pause_presses = 0;
slime73@9960
   591
slime73@9960
   592
        if (updateplayerindex && controller.playerIndex == -1) {
slime73@9960
   593
            BOOL usedPlayerIndexSlots[4] = {NO, NO, NO, NO};
slime73@9960
   594
slime73@9960
   595
            /* Find the player index of all other connected controllers. */
slime73@9960
   596
            for (GCController *c in [GCController controllers]) {
slime73@9960
   597
                if (c != controller && c.playerIndex >= 0) {
slime73@9960
   598
                    usedPlayerIndexSlots[c.playerIndex] = YES;
slime73@9960
   599
                }
slime73@9960
   600
            }
slime73@9960
   601
slime73@9960
   602
            /* Set this controller's player index to the first unused index.
slime73@9960
   603
             * FIXME: This logic isn't great... but SDL doesn't expose this
slime73@9960
   604
             * concept in its external API, so we don't have much to go on. */
slime73@9960
   605
            for (i = 0; i < SDL_arraysize(usedPlayerIndexSlots); i++) {
slime73@9960
   606
                if (!usedPlayerIndexSlots[i]) {
slime73@9960
   607
                    controller.playerIndex = i;
slime73@9960
   608
                    break;
slime73@9960
   609
                }
slime73@9960
   610
            }
slime73@9960
   611
        }
slime73@9876
   612
    }
slime73@10340
   613
#endif /* SDL_JOYSTICK_MFI */
slouken@8921
   614
}
slouken@8921
   615
slouken@2765
   616
/* Function to update the state of a joystick - called as a device poll.
slouken@2765
   617
 * This function shouldn't update the joystick structure directly,
slouken@2765
   618
 * but instead should call SDL_PrivateJoystick*() to deliver events
slouken@2765
   619
 * and update joystick device state.
slouken@2765
   620
 */
slouken@2765
   621
void
slouken@2765
   622
SDL_SYS_JoystickUpdate(SDL_Joystick * joystick)
slouken@2765
   623
{
slime73@9876
   624
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   625
slime73@9876
   626
    if (device == NULL) {
slime73@9876
   627
        return;
slime73@9876
   628
    }
slime73@9876
   629
slime73@9876
   630
    if (device->accelerometer) {
slime73@9876
   631
        SDL_SYS_AccelerometerUpdate(joystick);
slime73@9876
   632
    } else if (device->controller) {
slime73@9876
   633
        SDL_SYS_MFIJoystickUpdate(joystick);
slime73@9876
   634
    }
slouken@2765
   635
}
slouken@2765
   636
slouken@2765
   637
/* Function to close a joystick after use */
slouken@2765
   638
void
slouken@2765
   639
SDL_SYS_JoystickClose(SDL_Joystick * joystick)
slouken@2765
   640
{
slime73@9876
   641
    SDL_JoystickDeviceItem *device = joystick->hwdata;
slime73@9876
   642
slime73@9876
   643
    if (device == NULL) {
slime73@9876
   644
        return;
slime73@9876
   645
    }
slime73@9876
   646
slime73@9876
   647
    device->joystick = NULL;
slime73@9876
   648
slime73@9506
   649
    @autoreleasepool {
slime73@9876
   650
        if (device->accelerometer) {
slime73@10340
   651
#if !TARGET_OS_TV
slime73@9876
   652
            [motionManager stopAccelerometerUpdates];
slime73@10340
   653
#endif /* !TARGET_OS_TV */
slime73@9876
   654
        } else if (device->controller) {
slime73@9876
   655
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   656
            GCController *controller = device->controller;
slime73@9876
   657
            controller.controllerPausedHandler = nil;
slime73@9960
   658
            controller.playerIndex = -1;
slime73@9876
   659
#endif
slime73@9876
   660
        }
slime73@9506
   661
    }
slouken@2765
   662
}
slouken@2765
   663
slouken@2765
   664
/* Function to perform any system-specific joystick related cleanup */
slouken@2765
   665
void
slouken@2765
   666
SDL_SYS_JoystickQuit(void)
slouken@2765
   667
{
slime73@9506
   668
    @autoreleasepool {
slime73@9876
   669
#ifdef SDL_JOYSTICK_MFI
slime73@9876
   670
        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
slime73@9876
   671
slime73@9876
   672
        if (connectObserver) {
slime73@9876
   673
            [center removeObserver:connectObserver name:GCControllerDidConnectNotification object:nil];
slime73@9876
   674
            connectObserver = nil;
slime73@9876
   675
        }
slime73@9876
   676
slime73@9876
   677
        if (disconnectObserver) {
slime73@9876
   678
            [center removeObserver:disconnectObserver name:GCControllerDidDisconnectNotification object:nil];
slime73@9876
   679
            disconnectObserver = nil;
slime73@9876
   680
        }
slime73@10351
   681
slime73@10351
   682
#if TARGET_OS_TV
slime73@10351
   683
        SDL_DelHintCallback(SDL_HINT_APPLE_TV_REMOTE_ALLOW_ROTATION,
slime73@10351
   684
                            SDL_AppleTVRemoteRotationHintChanged, NULL);
slime73@10351
   685
#endif /* TARGET_OS_TV */
slime73@9876
   686
#endif /* SDL_JOYSTICK_MFI */
slime73@9876
   687
slime73@9876
   688
        while (deviceList != NULL) {
slime73@9876
   689
            SDL_SYS_RemoveJoystickDevice(deviceList);
slime73@9876
   690
        }
slime73@9876
   691
slime73@10340
   692
#if !TARGET_OS_TV
slouken@8921
   693
        motionManager = nil;
slime73@10340
   694
#endif /* !TARGET_OS_TV */
slouken@8921
   695
    }
slime73@9489
   696
slime73@9489
   697
    numjoysticks = 0;
slouken@2765
   698
}
slouken@6693
   699
slime73@9879
   700
SDL_JoystickGUID
slime73@9879
   701
SDL_SYS_JoystickGetDeviceGUID( int device_index )
slouken@6693
   702
{
slime73@9876
   703
    SDL_JoystickDeviceItem *device = GetDeviceForIndex(device_index);
slouken@6744
   704
    SDL_JoystickGUID guid;
slime73@9876
   705
    if (device) {
slime73@9876
   706
        guid = device->guid;
slime73@9876
   707
    } else {
slime73@9876
   708
        SDL_zero(guid);
slime73@9876
   709
    }
slouken@6693
   710
    return guid;
slouken@6693
   711
}
slouken@6693
   712
slime73@9879
   713
SDL_JoystickGUID
slime73@9879
   714
SDL_SYS_JoystickGetGUID(SDL_Joystick * joystick)
slouken@6693
   715
{
slouken@6744
   716
    SDL_JoystickGUID guid;
slime73@9876
   717
    if (joystick->hwdata) {
slime73@9876
   718
        guid = joystick->hwdata->guid;
slime73@9876
   719
    } else {
slime73@9876
   720
        SDL_zero(guid);
slime73@9876
   721
    }
slouken@6693
   722
    return guid;
slouken@6693
   723
}
slouken@6693
   724
slouken@2765
   725
/* vi: set ts=4 sw=4 expandtab: */