src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 08 Dec 2016 10:13:45 -0800
changeset 10688 f910d0498992
parent 10662 c6756f2a187b
child 10713 580fcf8605e3
permissions -rw-r--r--
Protect the game controller API the same way the joystick API is protected from multi-threaded access
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@8582
     4
slouken@8582
     5
  This software is provided 'as-is', without any express or implied
slouken@8582
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@8582
     7
  arising from the use of this software.
slouken@8582
     8
slouken@8582
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@8582
    10
  including commercial applications, and to alter it and redistribute it
slouken@8582
    11
  freely, subject to the following restrictions:
slouken@8582
    12
slouken@8582
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@8582
    14
     claim that you wrote the original software. If you use this software
slouken@8582
    15
     in a product, an acknowledgment in the product documentation would be
slouken@8582
    16
     appreciated but is not required.
slouken@8582
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@8582
    18
     misrepresented as being the original software.
slouken@8582
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@8582
    20
*/
icculus@8093
    21
#include "../SDL_internal.h"
slouken@8582
    22
slouken@8582
    23
/* This is the game controller API for Simple DirectMedia Layer */
slouken@8582
    24
slouken@8582
    25
#include "SDL_events.h"
slouken@8582
    26
#include "SDL_assert.h"
slouken@8582
    27
#include "SDL_sysjoystick.h"
slouken@10688
    28
#include "SDL_joystick_c.h"
slouken@8582
    29
#include "SDL_hints.h"
slouken@8582
    30
#include "SDL_gamecontrollerdb.h"
slouken@8582
    31
slouken@8582
    32
#if !SDL_EVENTS_DISABLED
slouken@8582
    33
#include "../events/SDL_events_c.h"
slouken@8582
    34
#endif
slouken@8582
    35
#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
slouken@8582
    36
gabomdq@8042
    37
#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
slouken@8582
    38
slouken@8582
    39
/* a list of currently opened game controllers */
slouken@8582
    40
static SDL_GameController *SDL_gamecontrollers = NULL;
slouken@8582
    41
slouken@8582
    42
/* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
slouken@8582
    43
struct _SDL_HatMapping
slouken@8582
    44
{
slouken@8582
    45
    int hat;
slouken@8582
    46
    Uint8 mask;
slouken@8582
    47
};
slouken@8582
    48
slouken@10423
    49
/* We need 36 entries for Android (as of SDL v2.0.4) */
slouken@10423
    50
#define k_nMaxReverseEntries 48
slouken@8582
    51
slouken@8582
    52
/**
slouken@8582
    53
 * We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask
slouken@8582
    54
 * MAX 4 hats supported
slouken@8582
    55
 */
slouken@8582
    56
#define k_nMaxHatEntries 0x3f + 1
slouken@8582
    57
slouken@8582
    58
/* our in memory mapping db between joystick objects and controller mappings */
slouken@8582
    59
struct _SDL_ControllerMapping
slouken@8582
    60
{
slouken@8582
    61
    SDL_JoystickGUID guid;
slouken@8582
    62
    const char *name;
slouken@8582
    63
slouken@8582
    64
    /* mapping of axis/button id to controller version */
slouken@8582
    65
    int axes[SDL_CONTROLLER_AXIS_MAX];
slouken@8582
    66
    int buttonasaxis[SDL_CONTROLLER_AXIS_MAX];
slouken@8582
    67
slouken@8582
    68
    int buttons[SDL_CONTROLLER_BUTTON_MAX];
slouken@8582
    69
    int axesasbutton[SDL_CONTROLLER_BUTTON_MAX];
slouken@8582
    70
    struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
slouken@8582
    71
slouken@8582
    72
    /* reverse mapping, joystick indices to buttons */
slouken@8582
    73
    SDL_GameControllerAxis raxes[k_nMaxReverseEntries];
slouken@8582
    74
    SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries];
slouken@8582
    75
slouken@8582
    76
    SDL_GameControllerButton rbuttons[k_nMaxReverseEntries];
slouken@8582
    77
    SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries];
slouken@8582
    78
    SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries];
slouken@8582
    79
slouken@8582
    80
};
slouken@8582
    81
slouken@8582
    82
slouken@8582
    83
/* our hard coded list of mapping support */
slouken@10605
    84
typedef enum
slouken@10605
    85
{
slouken@10605
    86
    SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT,
slouken@10605
    87
    SDL_CONTROLLER_MAPPING_PRIORITY_API,
slouken@10605
    88
    SDL_CONTROLLER_MAPPING_PRIORITY_USER,
slouken@10605
    89
} SDL_ControllerMappingPriority;
slouken@10605
    90
slouken@8582
    91
typedef struct _ControllerMapping_t
slouken@8582
    92
{
slouken@8582
    93
    SDL_JoystickGUID guid;
slouken@8582
    94
    char *name;
slouken@8582
    95
    char *mapping;
slouken@10605
    96
    SDL_ControllerMappingPriority priority;
slouken@8582
    97
    struct _ControllerMapping_t *next;
slouken@8582
    98
} ControllerMapping_t;
slouken@8582
    99
slouken@10661
   100
static SDL_JoystickGUID s_zeroGUID;
slouken@8582
   101
static ControllerMapping_t *s_pSupportedControllers = NULL;
slouken@8582
   102
static ControllerMapping_t *s_pXInputMapping = NULL;
icculus@9278
   103
static ControllerMapping_t *s_pEmscriptenMapping = NULL;
slouken@8582
   104
slouken@8582
   105
/* The SDL game controller structure */
slouken@8582
   106
struct _SDL_GameController
slouken@8582
   107
{
slouken@8582
   108
    SDL_Joystick *joystick; /* underlying joystick device */
slouken@8582
   109
    int ref_count;
slouken@8582
   110
    Uint8 hatState[4]; /* the current hat state for this controller */
slouken@8582
   111
    struct _SDL_ControllerMapping mapping; /* the mapping object for this controller */
slouken@8582
   112
    struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
slouken@8582
   113
};
slouken@8582
   114
slouken@8582
   115
slouken@8582
   116
int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
slouken@8582
   117
int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
slouken@8582
   118
slouken@8582
   119
/*
slouken@10226
   120
 * If there is an existing add event in the queue, it needs to be modified
slouken@10226
   121
 * to have the right value for which, because the number of controllers in
slouken@10226
   122
 * the system is now one less.
slouken@10226
   123
 */
slouken@10226
   124
static void UpdateEventsForDeviceRemoval()
slouken@10226
   125
{
slouken@10226
   126
    int i, num_events;
slouken@10226
   127
    SDL_Event *events;
slouken@10226
   128
slouken@10226
   129
    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
slouken@10226
   130
    if (num_events <= 0) {
slouken@10226
   131
        return;
slouken@10226
   132
    }
slouken@10226
   133
slouken@10226
   134
    events = SDL_stack_alloc(SDL_Event, num_events);
slouken@10226
   135
    if (!events) {
slouken@10226
   136
        return;
slouken@10226
   137
    }
slouken@10226
   138
slouken@10226
   139
    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
slouken@10226
   140
    for (i = 0; i < num_events; ++i) {
slouken@10226
   141
        --events[i].cdevice.which;
slouken@10226
   142
    }
slouken@10226
   143
    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
slouken@10226
   144
slouken@10226
   145
    SDL_stack_free(events);
slouken@10226
   146
}
slouken@10226
   147
slouken@10226
   148
/*
slouken@8582
   149
 * Event filter to fire controller events from joystick ones
slouken@8582
   150
 */
slouken@10609
   151
static int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
slouken@8582
   152
{
slouken@8772
   153
    switch(event->type) {
slouken@8582
   154
    case SDL_JOYAXISMOTION:
slouken@8582
   155
        {
slouken@8582
   156
            SDL_GameController *controllerlist;
slouken@8582
   157
slouken@10495
   158
            if (event->jaxis.axis >= k_nMaxReverseEntries)
slouken@10495
   159
            {
slouken@10495
   160
                SDL_SetError("SDL_GameControllerEventWatcher: Axis index %d too large, ignoring motion", (int)event->jaxis.axis);
slouken@10495
   161
                break;
slouken@10495
   162
            }
slouken@8582
   163
slouken@8582
   164
            controllerlist = SDL_gamecontrollers;
slouken@8772
   165
            while (controllerlist) {
slouken@8772
   166
                if (controllerlist->joystick->instance_id == event->jaxis.which) {
slouken@8772
   167
                    if (controllerlist->mapping.raxes[event->jaxis.axis] >= 0) /* simple axis to axis, send it through */ {
slouken@8582
   168
                        SDL_GameControllerAxis axis = controllerlist->mapping.raxes[event->jaxis.axis];
slouken@8582
   169
                        Sint16 value = event->jaxis.value;
slouken@8772
   170
                        switch (axis) {
slouken@8582
   171
                            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
slouken@8582
   172
                            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
slouken@10477
   173
                                value = value / 2 + 16384;
slouken@10476
   174
                                break;
slouken@8582
   175
                            default:
slouken@8582
   176
                                break;
slouken@8582
   177
                        }
slouken@8772
   178
                        SDL_PrivateGameControllerAxis(controllerlist, axis, value);
slouken@8772
   179
                    } else if (controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0) { /* simulate an axis as a button */
slouken@8772
   180
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED);
slouken@8582
   181
                    }
slouken@8582
   182
                    break;
slouken@8582
   183
                }
slouken@8582
   184
                controllerlist = controllerlist->next;
slouken@8582
   185
            }
slouken@8582
   186
        }
slouken@8582
   187
        break;
slouken@8582
   188
    case SDL_JOYBUTTONDOWN:
slouken@8582
   189
    case SDL_JOYBUTTONUP:
slouken@8582
   190
        {
slouken@8582
   191
            SDL_GameController *controllerlist;
slouken@8582
   192
slouken@10495
   193
            if (event->jbutton.button >= k_nMaxReverseEntries)
slouken@10495
   194
            {
slouken@10495
   195
                SDL_SetError("SDL_GameControllerEventWatcher: Button index %d too large, ignoring update", (int)event->jbutton.button);
slouken@10495
   196
                break;
slouken@10495
   197
            }
slouken@8582
   198
slouken@8582
   199
            controllerlist = SDL_gamecontrollers;
slouken@8772
   200
            while (controllerlist) {
slouken@8772
   201
                if (controllerlist->joystick->instance_id == event->jbutton.which) {
slouken@8772
   202
                    if (controllerlist->mapping.rbuttons[event->jbutton.button] >= 0) { /* simple button as button */
slouken@8772
   203
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rbuttons[event->jbutton.button], event->jbutton.state);
slouken@8772
   204
                    } else if (controllerlist->mapping.rbuttonasaxis[event->jbutton.button] >= 0) { /* an button pretending to be an axis */
slouken@8772
   205
                        SDL_PrivateGameControllerAxis(controllerlist, controllerlist->mapping.rbuttonasaxis[event->jbutton.button], event->jbutton.state > 0 ? 32767 : 0);
slouken@8582
   206
                    }
slouken@8582
   207
                    break;
slouken@8582
   208
                }
slouken@8582
   209
                controllerlist = controllerlist->next;
slouken@8582
   210
            }
slouken@8582
   211
        }
slouken@8582
   212
        break;
slouken@8582
   213
    case SDL_JOYHATMOTION:
slouken@8582
   214
        {
slouken@8582
   215
            SDL_GameController *controllerlist;
slouken@8582
   216
slouken@8772
   217
            if (event->jhat.hat >= 4) break;
slouken@8582
   218
slouken@8582
   219
            controllerlist = SDL_gamecontrollers;
slouken@8772
   220
            while (controllerlist) {
slouken@8772
   221
                if (controllerlist->joystick->instance_id == event->jhat.which) {
slouken@8582
   222
                    Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
slouken@8582
   223
                    /* Get list of removed bits (button release) */
slouken@8582
   224
                    Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
slouken@8582
   225
                    /* the hat idx in the high nibble */
slouken@8582
   226
                    int bHighHat = event->jhat.hat << 4;
slouken@8582
   227
slouken@8772
   228
                    if (bChanged & SDL_HAT_DOWN)
slouken@8772
   229
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED);
slouken@8772
   230
                    if (bChanged & SDL_HAT_UP)
slouken@8772
   231
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED);
slouken@8772
   232
                    if (bChanged & SDL_HAT_LEFT)
slouken@8772
   233
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED);
slouken@8772
   234
                    if (bChanged & SDL_HAT_RIGHT)
slouken@8772
   235
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED);
slouken@8582
   236
slouken@8582
   237
                    /* Get list of added bits (button press) */
slouken@8582
   238
                    bChanged = event->jhat.value ^ bSame;
slouken@8582
   239
slouken@8772
   240
                    if (bChanged & SDL_HAT_DOWN)
slouken@8772
   241
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED);
slouken@8772
   242
                    if (bChanged & SDL_HAT_UP)
slouken@8772
   243
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED);
slouken@8772
   244
                    if (bChanged & SDL_HAT_LEFT)
slouken@8772
   245
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED);
slouken@8772
   246
                    if (bChanged & SDL_HAT_RIGHT)
slouken@8772
   247
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED);
slouken@8582
   248
slouken@8582
   249
                    /* update our state cache */
slouken@8582
   250
                    controllerlist->hatState[event->jhat.hat] = event->jhat.value;
slouken@8582
   251
slouken@8582
   252
                    break;
slouken@8582
   253
                }
slouken@8582
   254
                controllerlist = controllerlist->next;
slouken@8582
   255
            }
slouken@8582
   256
        }
slouken@8582
   257
        break;
slouken@8582
   258
    case SDL_JOYDEVICEADDED:
slouken@8582
   259
        {
slouken@8772
   260
            if (SDL_IsGameController(event->jdevice.which)) {
slouken@8582
   261
                SDL_Event deviceevent;
slouken@8582
   262
                deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@8582
   263
                deviceevent.cdevice.which = event->jdevice.which;
slouken@8582
   264
                SDL_PushEvent(&deviceevent);
slouken@8582
   265
            }
slouken@8582
   266
        }
slouken@8582
   267
        break;
slouken@8582
   268
    case SDL_JOYDEVICEREMOVED:
slouken@8582
   269
        {
slouken@8582
   270
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@8772
   271
            while (controllerlist) {
slouken@8772
   272
                if (controllerlist->joystick->instance_id == event->jdevice.which) {
slouken@8582
   273
                    SDL_Event deviceevent;
slouken@10225
   274
slouken@8582
   275
                    deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
slouken@8582
   276
                    deviceevent.cdevice.which = event->jdevice.which;
slouken@8582
   277
                    SDL_PushEvent(&deviceevent);
slouken@10226
   278
slouken@10226
   279
                    UpdateEventsForDeviceRemoval();
slouken@8582
   280
                    break;
slouken@8582
   281
                }
slouken@8582
   282
                controllerlist = controllerlist->next;
slouken@8582
   283
            }
slouken@8582
   284
        }
slouken@8582
   285
        break;
slouken@8582
   286
    default:
slouken@8582
   287
        break;
slouken@8582
   288
    }
slouken@8582
   289
slouken@8582
   290
    return 1;
slouken@8582
   291
}
slouken@8582
   292
slouken@8582
   293
/*
slouken@8582
   294
 * Helper function to scan the mappings database for a controller with the specified GUID
slouken@8582
   295
 */
slouken@10609
   296
static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
slouken@8582
   297
{
slouken@8582
   298
    ControllerMapping_t *pSupportedController = s_pSupportedControllers;
slouken@8772
   299
    while (pSupportedController) {
slouken@8772
   300
        if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
slouken@8582
   301
            return pSupportedController;
slouken@8582
   302
        }
slouken@8582
   303
        pSupportedController = pSupportedController->next;
slouken@8582
   304
    }
slouken@8582
   305
    return NULL;
slouken@8582
   306
}
slouken@8582
   307
slouken@8582
   308
static const char* map_StringForControllerAxis[] = {
slouken@8582
   309
    "leftx",
slouken@8582
   310
    "lefty",
slouken@8582
   311
    "rightx",
slouken@8582
   312
    "righty",
slouken@8582
   313
    "lefttrigger",
slouken@8582
   314
    "righttrigger",
slouken@8582
   315
    NULL
slouken@8582
   316
};
slouken@8582
   317
slouken@8582
   318
/*
slouken@8582
   319
 * convert a string to its enum equivalent
slouken@8582
   320
 */
slouken@8772
   321
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *pchString)
slouken@8582
   322
{
slouken@8582
   323
    int entry;
slouken@8772
   324
    if (!pchString || !pchString[0])
slouken@8582
   325
        return SDL_CONTROLLER_AXIS_INVALID;
slouken@8582
   326
slouken@8772
   327
    for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
slouken@8772
   328
        if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
slouken@8582
   329
            return entry;
slouken@8582
   330
    }
slouken@8582
   331
    return SDL_CONTROLLER_AXIS_INVALID;
slouken@8582
   332
}
slouken@8582
   333
slouken@8582
   334
/*
slouken@8582
   335
 * convert an enum to its string equivalent
slouken@8582
   336
 */
slouken@8772
   337
const char* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
slouken@8582
   338
{
slouken@8772
   339
    if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
slouken@8582
   340
        return map_StringForControllerAxis[axis];
slouken@8582
   341
    }
slouken@8582
   342
    return NULL;
slouken@8582
   343
}
slouken@8582
   344
slouken@8582
   345
static const char* map_StringForControllerButton[] = {
slouken@8582
   346
    "a",
slouken@8582
   347
    "b",
slouken@8582
   348
    "x",
slouken@8582
   349
    "y",
slouken@8582
   350
    "back",
slouken@8582
   351
    "guide",
slouken@8582
   352
    "start",
slouken@8582
   353
    "leftstick",
slouken@8582
   354
    "rightstick",
slouken@8582
   355
    "leftshoulder",
slouken@8582
   356
    "rightshoulder",
slouken@8582
   357
    "dpup",
slouken@8582
   358
    "dpdown",
slouken@8582
   359
    "dpleft",
slouken@8582
   360
    "dpright",
slouken@8582
   361
    NULL
slouken@8582
   362
};
slouken@8582
   363
slouken@8582
   364
/*
slouken@8582
   365
 * convert a string to its enum equivalent
slouken@8582
   366
 */
slouken@8772
   367
SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *pchString)
slouken@8582
   368
{
slouken@8582
   369
    int entry;
slouken@8772
   370
    if (!pchString || !pchString[0])
slouken@8582
   371
        return SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   372
slouken@8772
   373
    for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
slouken@8772
   374
        if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
slouken@8772
   375
            return entry;
slouken@8582
   376
    }
slouken@8582
   377
    return SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   378
}
slouken@8582
   379
slouken@8582
   380
/*
slouken@8582
   381
 * convert an enum to its string equivalent
slouken@8582
   382
 */
slouken@8772
   383
const char* SDL_GameControllerGetStringForButton(SDL_GameControllerButton axis)
slouken@8582
   384
{
slouken@8772
   385
    if (axis > SDL_CONTROLLER_BUTTON_INVALID && axis < SDL_CONTROLLER_BUTTON_MAX) {
slouken@8582
   386
        return map_StringForControllerButton[axis];
slouken@8582
   387
    }
slouken@8582
   388
    return NULL;
slouken@8582
   389
}
slouken@8582
   390
slouken@8582
   391
/*
slouken@8582
   392
 * given a controller button name and a joystick name update our mapping structure with it
slouken@8582
   393
 */
slouken@10609
   394
static void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
slouken@8582
   395
{
slouken@8582
   396
    int iSDLButton = 0;
slouken@8582
   397
    SDL_GameControllerButton button;
slouken@8582
   398
    SDL_GameControllerAxis axis;
slouken@8772
   399
    button = SDL_GameControllerGetButtonFromString(szGameButton);
slouken@8772
   400
    axis = SDL_GameControllerGetAxisFromString(szGameButton);
slouken@8772
   401
    iSDLButton = SDL_atoi(&szJoystickButton[1]);
slouken@8582
   402
slouken@8772
   403
    if (szJoystickButton[0] == 'a') {
slouken@8772
   404
        if (iSDLButton >= k_nMaxReverseEntries) {
slouken@8772
   405
            SDL_SetError("Axis index too large: %d", iSDLButton);
slouken@8582
   406
            return;
slouken@8582
   407
        }
slouken@8772
   408
        if (axis != SDL_CONTROLLER_AXIS_INVALID) {
slouken@10602
   409
            pMapping->axes[axis] = iSDLButton;
slouken@10602
   410
            pMapping->raxes[iSDLButton] = axis;
slouken@8772
   411
        } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
slouken@10602
   412
            pMapping->axesasbutton[button] = iSDLButton;
slouken@10602
   413
            pMapping->raxesasbutton[iSDLButton] = button;
slouken@8772
   414
        } else {
slouken@8772
   415
            SDL_assert(!"How did we get here?");
slouken@8582
   416
        }
slouken@8582
   417
slouken@8772
   418
    } else if (szJoystickButton[0] == 'b') {
slouken@8772
   419
        if (iSDLButton >= k_nMaxReverseEntries) {
slouken@8772
   420
            SDL_SetError("Button index too large: %d", iSDLButton);
slouken@8582
   421
            return;
slouken@8582
   422
        }
slouken@8772
   423
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
slouken@10602
   424
            pMapping->buttons[button] = iSDLButton;
slouken@10602
   425
            pMapping->rbuttons[iSDLButton] = button;
slouken@8772
   426
        } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
slouken@10602
   427
            pMapping->buttonasaxis[axis] = iSDLButton;
slouken@10602
   428
            pMapping->rbuttonasaxis[iSDLButton] = axis;
slouken@8772
   429
        } else {
slouken@8772
   430
            SDL_assert(!"How did we get here?");
slouken@8582
   431
        }
slouken@8772
   432
    } else if (szJoystickButton[0] == 'h') {
slouken@8772
   433
        int hat = SDL_atoi(&szJoystickButton[1]);
slouken@8772
   434
        int mask = SDL_atoi(&szJoystickButton[3]);
slouken@8582
   435
        if (hat >= 4) {
slouken@8772
   436
            SDL_SetError("Hat index too large: %d", iSDLButton);
slouken@8582
   437
        }
slouken@8582
   438
slouken@8772
   439
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
slouken@8582
   440
            int ridx;
slouken@10602
   441
            pMapping->hatasbutton[button].hat = hat;
slouken@10602
   442
            pMapping->hatasbutton[button].mask = mask;
slouken@8582
   443
            ridx = (hat << 4) | mask;
slouken@10602
   444
            pMapping->rhatasbutton[ridx] = button;
slouken@8772
   445
        } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
slouken@8772
   446
            SDL_assert(!"Support hat as axis");
slouken@8772
   447
        } else {
slouken@8772
   448
            SDL_assert(!"How did we get here?");
slouken@8582
   449
        }
slouken@8582
   450
    }
slouken@8582
   451
}
slouken@8582
   452
slouken@8582
   453
slouken@8582
   454
/*
slouken@8582
   455
 * given a controller mapping string update our mapping object
slouken@8582
   456
 */
slouken@8582
   457
static void
slouken@8772
   458
SDL_PrivateGameControllerParseControllerConfigString(struct _SDL_ControllerMapping *pMapping, const char *pchString)
slouken@8582
   459
{
slouken@8582
   460
    char szGameButton[20];
slouken@8582
   461
    char szJoystickButton[20];
slouken@8582
   462
    SDL_bool bGameButton = SDL_TRUE;
slouken@8582
   463
    int i = 0;
slouken@8582
   464
    const char *pchPos = pchString;
slouken@8582
   465
slouken@8772
   466
    SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
slouken@8772
   467
    SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
slouken@8582
   468
slouken@8772
   469
    while (pchPos && *pchPos) {
slouken@8772
   470
        if (*pchPos == ':') {
slouken@8582
   471
            i = 0;
slouken@8582
   472
            bGameButton = SDL_FALSE;
slouken@8772
   473
        } else if (*pchPos == ' ') {
slouken@8582
   474
slouken@8772
   475
        } else if (*pchPos == ',') {
slouken@8582
   476
            i = 0;
slouken@8582
   477
            bGameButton = SDL_TRUE;
slouken@8772
   478
            SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
slouken@8772
   479
            SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
slouken@8772
   480
            SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
slouken@8582
   481
slouken@8772
   482
        } else if (bGameButton) {
slouken@8772
   483
            if (i >= sizeof(szGameButton)) {
slouken@8772
   484
                SDL_SetError("Button name too large: %s", szGameButton);
slouken@8582
   485
                return;
slouken@8582
   486
            }
slouken@8582
   487
            szGameButton[i] = *pchPos;
slouken@8582
   488
            i++;
slouken@8772
   489
        } else {
slouken@8772
   490
            if (i >= sizeof(szJoystickButton)) {
slouken@8772
   491
                SDL_SetError("Joystick button name too large: %s", szJoystickButton);
slouken@8582
   492
                return;
slouken@8582
   493
            }
slouken@8582
   494
            szJoystickButton[i] = *pchPos;
slouken@8582
   495
            i++;
slouken@8582
   496
        }
slouken@8582
   497
        pchPos++;
slouken@8582
   498
    }
slouken@8582
   499
slouken@8772
   500
    SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
slouken@8582
   501
slouken@8582
   502
}
slouken@8582
   503
slouken@8582
   504
/*
slouken@8582
   505
 * Make a new button mapping struct
slouken@8582
   506
 */
slouken@10609
   507
static void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
slouken@8582
   508
{
slouken@8582
   509
    int j;
slouken@8582
   510
slouken@8582
   511
    pMapping->guid = guid;
slouken@8582
   512
    pMapping->name = pchName;
slouken@8582
   513
slouken@8582
   514
    /* set all the button mappings to non defaults */
slouken@8772
   515
    for (j = 0; j < SDL_CONTROLLER_AXIS_MAX; j++) {
slouken@8582
   516
        pMapping->axes[j] = -1;
slouken@8582
   517
        pMapping->buttonasaxis[j] = -1;
slouken@8582
   518
    }
slouken@8772
   519
    for (j = 0; j < SDL_CONTROLLER_BUTTON_MAX; j++) {
slouken@8582
   520
        pMapping->buttons[j] = -1;
slouken@8582
   521
        pMapping->axesasbutton[j] = -1;
slouken@8582
   522
        pMapping->hatasbutton[j].hat = -1;
slouken@8582
   523
    }
slouken@8582
   524
slouken@8772
   525
    for (j = 0; j < k_nMaxReverseEntries; j++) {
slouken@8582
   526
        pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
slouken@8582
   527
        pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
slouken@8582
   528
        pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   529
        pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   530
    }
slouken@8582
   531
slouken@8772
   532
    for (j = 0; j < k_nMaxHatEntries; j++) {
slouken@8582
   533
        pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   534
    }
slouken@8582
   535
slouken@8772
   536
    SDL_PrivateGameControllerParseControllerConfigString(pMapping, pchMapping);
slouken@8582
   537
}
slouken@8582
   538
slouken@8582
   539
slouken@8582
   540
/*
slouken@8582
   541
 * grab the guid string from a mapping string
slouken@8582
   542
 */
slouken@10609
   543
static char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
slouken@8582
   544
{
slouken@8772
   545
    const char *pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   546
    if (pFirstComma) {
slouken@8772
   547
        char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
slouken@8772
   548
        if (!pchGUID) {
slouken@8582
   549
            SDL_OutOfMemory();
slouken@8582
   550
            return NULL;
slouken@8582
   551
        }
slouken@8772
   552
        SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
slouken@10602
   553
        pchGUID[pFirstComma - pMapping] = '\0';
slouken@10602
   554
slouken@10602
   555
        /* Convert old style GUIDs to the new style in 2.0.5 */
slouken@10602
   556
#if __WIN32__
slouken@10602
   557
        if (SDL_strlen(pchGUID) == 32 &&
slouken@10602
   558
            SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
slouken@10602
   559
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
slouken@10602
   560
            SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
slouken@10602
   561
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
slouken@10602
   562
            SDL_memcpy(&pchGUID[0], "03000000", 8);
slouken@10602
   563
        }
slouken@10602
   564
#elif __MACOSX__
slouken@10602
   565
        if (SDL_strlen(pchGUID) == 32 &&
slouken@10602
   566
            SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
slouken@10602
   567
            SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
slouken@10602
   568
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
slouken@10602
   569
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
slouken@10602
   570
            SDL_memcpy(&pchGUID[0], "03000000", 8);
slouken@10602
   571
        }
slouken@10602
   572
#endif
slouken@8582
   573
        return pchGUID;
slouken@8582
   574
    }
slouken@8582
   575
    return NULL;
slouken@8582
   576
}
slouken@8582
   577
slouken@8582
   578
slouken@8582
   579
/*
slouken@8582
   580
 * grab the name string from a mapping string
slouken@8582
   581
 */
slouken@10609
   582
static char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
slouken@8582
   583
{
slouken@8582
   584
    const char *pFirstComma, *pSecondComma;
slouken@8582
   585
    char *pchName;
slouken@8582
   586
slouken@8772
   587
    pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   588
    if (!pFirstComma)
slouken@8582
   589
        return NULL;
slouken@8582
   590
slouken@8772
   591
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
slouken@8772
   592
    if (!pSecondComma)
slouken@8582
   593
        return NULL;
slouken@8582
   594
slouken@8772
   595
    pchName = SDL_malloc(pSecondComma - pFirstComma);
slouken@8772
   596
    if (!pchName) {
slouken@8582
   597
        SDL_OutOfMemory();
slouken@8582
   598
        return NULL;
slouken@8582
   599
    }
slouken@8772
   600
    SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
slouken@10602
   601
    pchName[pSecondComma - pFirstComma - 1] = 0;
slouken@8582
   602
    return pchName;
slouken@8582
   603
}
slouken@8582
   604
slouken@8582
   605
slouken@8582
   606
/*
slouken@8582
   607
 * grab the button mapping string from a mapping string
slouken@8582
   608
 */
slouken@10609
   609
static char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
slouken@8582
   610
{
slouken@8582
   611
    const char *pFirstComma, *pSecondComma;
slouken@8582
   612
slouken@8772
   613
    pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   614
    if (!pFirstComma)
slouken@8582
   615
        return NULL;
slouken@8582
   616
slouken@8772
   617
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
slouken@8772
   618
    if (!pSecondComma)
slouken@8582
   619
        return NULL;
slouken@8582
   620
slouken@8582
   621
    return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
slouken@8582
   622
}
slouken@8582
   623
slouken@9956
   624
/*
slouken@9956
   625
 * Helper function to refresh a mapping
slouken@9956
   626
 */
slouken@10609
   627
static void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
slouken@8582
   628
{
slouken@8582
   629
    SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
slouken@8772
   630
    while (gamecontrollerlist) {
slouken@8772
   631
        if (!SDL_memcmp(&gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
slouken@8582
   632
            SDL_Event event;
slouken@8582
   633
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
slouken@8582
   634
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
slouken@8582
   635
            SDL_PushEvent(&event);
slouken@8582
   636
slouken@8582
   637
            /* Not really threadsafe.  Should this lock access within SDL_GameControllerEventWatcher? */
slouken@8582
   638
            SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
slouken@8582
   639
        }
slouken@8582
   640
slouken@8582
   641
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
   642
    }
slouken@8582
   643
}
slouken@8582
   644
slouken@8582
   645
/*
slouken@9956
   646
 * Helper function to add a mapping for a guid
slouken@9956
   647
 */
slouken@9956
   648
static ControllerMapping_t *
slouken@10605
   649
SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing, SDL_ControllerMappingPriority priority)
slouken@9956
   650
{
slouken@9956
   651
    char *pchName;
slouken@9956
   652
    char *pchMapping;
slouken@9956
   653
    ControllerMapping_t *pControllerMapping;
slouken@9956
   654
slouken@9956
   655
    pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
slouken@9956
   656
    if (!pchName) {
slouken@9956
   657
        SDL_SetError("Couldn't parse name from %s", mappingString);
slouken@9956
   658
        return NULL;
slouken@9956
   659
    }
slouken@9956
   660
slouken@9956
   661
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
slouken@9956
   662
    if (!pchMapping) {
slouken@9956
   663
        SDL_free(pchName);
slouken@9956
   664
        SDL_SetError("Couldn't parse %s", mappingString);
slouken@9956
   665
        return NULL;
slouken@9956
   666
    }
slouken@9956
   667
slouken@9956
   668
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
slouken@9956
   669
    if (pControllerMapping) {
slouken@10605
   670
        /* Only overwrite the mapping if the priority is the same or higher. */
slouken@10605
   671
        if (pControllerMapping->priority <= priority) {
slouken@10605
   672
            /* Update existing mapping */
slouken@10605
   673
            SDL_free(pControllerMapping->name);
slouken@10605
   674
            pControllerMapping->name = pchName;
slouken@10605
   675
            SDL_free(pControllerMapping->mapping);
slouken@10605
   676
            pControllerMapping->mapping = pchMapping;
slouken@10605
   677
            pControllerMapping->priority = priority;
slouken@10605
   678
            /* refresh open controllers */
slouken@10605
   679
            SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
philipp@10632
   680
        } else {
philipp@10632
   681
            SDL_free(pchName);
philipp@10632
   682
            SDL_free(pchMapping);
slouken@10605
   683
        }
slouken@9956
   684
        *existing = SDL_TRUE;
slouken@9956
   685
    } else {
slouken@9956
   686
        pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
slouken@9956
   687
        if (!pControllerMapping) {
slouken@9956
   688
            SDL_free(pchName);
slouken@9956
   689
            SDL_free(pchMapping);
slouken@9956
   690
            SDL_OutOfMemory();
slouken@9956
   691
            return NULL;
slouken@9956
   692
        }
slouken@9956
   693
        pControllerMapping->guid = jGUID;
slouken@9956
   694
        pControllerMapping->name = pchName;
slouken@9956
   695
        pControllerMapping->mapping = pchMapping;
slouken@10662
   696
        pControllerMapping->next = NULL;
slouken@10605
   697
        pControllerMapping->priority = priority;
slouken@10662
   698
slouken@10662
   699
        if (s_pSupportedControllers) {
slouken@10662
   700
            /* Add the mapping to the end of the list */
slouken@10662
   701
            ControllerMapping_t *pCurrMapping, *pPrevMapping;
slouken@10662
   702
slouken@10662
   703
            for ( pPrevMapping = s_pSupportedControllers, pCurrMapping = pPrevMapping->next;
slouken@10662
   704
                  pCurrMapping; 
slouken@10662
   705
                  pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next ) {
slouken@10662
   706
                continue;
slouken@10662
   707
            }
slouken@10662
   708
            pPrevMapping->next = pControllerMapping;
slouken@10662
   709
        } else {
slouken@10662
   710
            s_pSupportedControllers = pControllerMapping;
slouken@10662
   711
        }
slouken@9956
   712
        *existing = SDL_FALSE;
slouken@9956
   713
    }
slouken@9956
   714
    return pControllerMapping;
slouken@9956
   715
}
slouken@9956
   716
slouken@9956
   717
/*
slouken@9956
   718
 * Helper function to determine pre-calculated offset to certain joystick mappings
slouken@9956
   719
 */
slouken@10609
   720
static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
slouken@9956
   721
{
slouken@9956
   722
    SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID(device_index);
slouken@9956
   723
    ControllerMapping_t *mapping;
slouken@9956
   724
slouken@9956
   725
    mapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
slouken@9956
   726
#if SDL_JOYSTICK_XINPUT
slouken@9956
   727
    if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
slouken@9956
   728
        mapping = s_pXInputMapping;
slouken@9956
   729
    }
slouken@9956
   730
#endif
slouken@9956
   731
#if defined(SDL_JOYSTICK_EMSCRIPTEN)
slouken@9956
   732
    if (!mapping && s_pEmscriptenMapping) {
slouken@9956
   733
        mapping = s_pEmscriptenMapping;
slouken@9956
   734
    }
slouken@9956
   735
#endif
slouken@9956
   736
#ifdef __LINUX__
slouken@9956
   737
    if (!mapping) {
slouken@9956
   738
        const char *name = SDL_JoystickNameForIndex(device_index);
slouken@9956
   739
        if (name) {
slouken@9956
   740
            if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
slouken@9956
   741
                /* The Linux driver xpad.c maps the wireless dpad to buttons */
slouken@9956
   742
                SDL_bool existing;
slouken@9956
   743
                mapping = SDL_PrivateAddMappingForGUID(jGUID,
slouken@9956
   744
"none,X360 Wireless Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b11,dpright:b12,dpup:b13,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
slouken@10605
   745
                              &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@9956
   746
            }
slouken@9956
   747
        }
slouken@9956
   748
    }
slouken@9956
   749
#endif /* __LINUX__ */
slouken@9956
   750
slouken@9956
   751
    if (!mapping) {
slouken@9956
   752
        const char *name = SDL_JoystickNameForIndex(device_index);
slouken@9956
   753
        if (name) {
slouken@9956
   754
            if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box")) {
slouken@9956
   755
                mapping = s_pXInputMapping;
slouken@9956
   756
            }
slouken@9956
   757
        }
slouken@9956
   758
    }
slouken@9956
   759
    return mapping;
slouken@9956
   760
}
slouken@9956
   761
slouken@9956
   762
/*
slouken@8582
   763
 * Add or update an entry into the Mappings Database
slouken@8582
   764
 */
slouken@8582
   765
int
slouken@8772
   766
SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
gabomdq@8042
   767
{
gabomdq@8042
   768
    const char *platform = SDL_GetPlatform();
gabomdq@8042
   769
    int controllers = 0;
gabomdq@8042
   770
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
gabomdq@8042
   771
    size_t db_size, platform_len;
gabomdq@8042
   772
    
gabomdq@8042
   773
    if (rw == NULL) {
gabomdq@8046
   774
        return SDL_SetError("Invalid RWops");
gabomdq@8042
   775
    }
slouken@8052
   776
    db_size = (size_t)SDL_RWsize(rw);
gabomdq@8042
   777
    
slouken@8052
   778
    buf = (char *)SDL_malloc(db_size + 1);
gabomdq@8042
   779
    if (buf == NULL) {
gabomdq@8046
   780
        if (freerw) {
gabomdq@8046
   781
            SDL_RWclose(rw);
gabomdq@8046
   782
        }
philipp@9194
   783
        return SDL_SetError("Could not allocate space to read DB into memory");
gabomdq@8042
   784
    }
gabomdq@8042
   785
    
gabomdq@8042
   786
    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
gabomdq@8046
   787
        if (freerw) {
gabomdq@8046
   788
            SDL_RWclose(rw);
gabomdq@8046
   789
        }
gabomdq@8042
   790
        SDL_free(buf);
gabomdq@8042
   791
        return SDL_SetError("Could not read DB");
gabomdq@8042
   792
    }
gabomdq@8046
   793
    
gabomdq@8046
   794
    if (freerw) {
gabomdq@8046
   795
        SDL_RWclose(rw);
gabomdq@8046
   796
    }
gabomdq@8042
   797
    
gabomdq@8042
   798
    buf[db_size] = '\0';
gabomdq@8042
   799
    line = buf;
gabomdq@8042
   800
    
gabomdq@8042
   801
    while (line < buf + db_size) {
slouken@8772
   802
        line_end = SDL_strchr(line, '\n');
gabomdq@8042
   803
        if (line_end != NULL) {
gabomdq@8042
   804
            *line_end = '\0';
slouken@8772
   805
        } else {
gabomdq@8042
   806
            line_end = buf + db_size;
gabomdq@8042
   807
        }
gabomdq@8042
   808
        
gabomdq@8042
   809
        /* Extract and verify the platform */
gabomdq@8042
   810
        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
slouken@8772
   811
        if (tmp != NULL) {
gabomdq@8042
   812
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
gabomdq@8042
   813
            comma = SDL_strchr(tmp, ',');
gabomdq@8042
   814
            if (comma != NULL) {
gabomdq@8042
   815
                platform_len = comma - tmp + 1;
gabomdq@8042
   816
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
gabomdq@8042
   817
                    SDL_strlcpy(line_platform, tmp, platform_len);
slouken@8772
   818
                    if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
slouken@8772
   819
                        SDL_GameControllerAddMapping(line) > 0) {
gabomdq@8042
   820
                        controllers++;
gabomdq@8042
   821
                    }
gabomdq@8042
   822
                }
gabomdq@8042
   823
            }
gabomdq@8042
   824
        }
gabomdq@8042
   825
        
gabomdq@8042
   826
        line = line_end + 1;
gabomdq@8042
   827
    }
gabomdq@8042
   828
gabomdq@8042
   829
    SDL_free(buf);
gabomdq@8042
   830
    return controllers;
gabomdq@8042
   831
}
gabomdq@8042
   832
gabomdq@8042
   833
/*
slouken@10605
   834
 * Add or update an entry into the Mappings Database with a priority
gabomdq@8042
   835
 */
slouken@10605
   836
static int
slouken@10605
   837
SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMappingPriority priority)
slouken@8582
   838
{
slouken@8582
   839
    char *pchGUID;
slouken@8582
   840
    SDL_JoystickGUID jGUID;
slouken@8582
   841
    SDL_bool is_xinput_mapping = SDL_FALSE;
icculus@9278
   842
    SDL_bool is_emscripten_mapping = SDL_FALSE;
slouken@9956
   843
    SDL_bool existing = SDL_FALSE;
slouken@9956
   844
    ControllerMapping_t *pControllerMapping;
slouken@8582
   845
philipp@9198
   846
    if (!mappingString) {
philipp@9198
   847
        return SDL_InvalidParamError("mappingString");
philipp@9198
   848
    }
philipp@9198
   849
slouken@8772
   850
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
slouken@8582
   851
    if (!pchGUID) {
philipp@8081
   852
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
slouken@8582
   853
    }
slouken@8772
   854
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
slouken@8582
   855
        is_xinput_mapping = SDL_TRUE;
slouken@8582
   856
    }
icculus@9278
   857
    if (!SDL_strcasecmp(pchGUID, "emscripten")) {
icculus@9278
   858
        is_emscripten_mapping = SDL_TRUE;
icculus@9278
   859
    }
slouken@8582
   860
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
slouken@8582
   861
    SDL_free(pchGUID);
slouken@8582
   862
slouken@10605
   863
    pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
slouken@9956
   864
    if (!pControllerMapping) {
slouken@9956
   865
        return -1;
slouken@8053
   866
    }
slouken@8582
   867
slouken@9956
   868
    if (existing) {
slouken@8582
   869
        return 0;
slouken@8582
   870
    } else {
slouken@8772
   871
        if (is_xinput_mapping) {
slouken@8582
   872
            s_pXInputMapping = pControllerMapping;
slouken@8582
   873
        }
icculus@9278
   874
        if (is_emscripten_mapping) {
icculus@9278
   875
            s_pEmscriptenMapping = pControllerMapping;
icculus@9278
   876
        }
slouken@8582
   877
        return 1;
slouken@8582
   878
    }
slouken@8582
   879
}
slouken@8582
   880
slouken@8582
   881
/*
slouken@10605
   882
 * Add or update an entry into the Mappings Database
slouken@10605
   883
 */
slouken@10605
   884
int
slouken@10605
   885
SDL_GameControllerAddMapping(const char *mappingString)
slouken@10605
   886
{
slouken@10605
   887
    return SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
slouken@10605
   888
}
slouken@10605
   889
slouken@10661
   890
/**
slouken@10661
   891
 *  Get the number of mappings installed
slouken@10661
   892
 */
slouken@10661
   893
int
slouken@10661
   894
SDL_GameControllerNumMappings()
slouken@10661
   895
{
slouken@10661
   896
    int num_mappings = 0;
slouken@10661
   897
    ControllerMapping_t *mapping;
slouken@10661
   898
slouken@10661
   899
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
   900
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
   901
            continue;
slouken@10661
   902
        }
slouken@10661
   903
        ++num_mappings;
slouken@10661
   904
    }
slouken@10661
   905
    return num_mappings;
slouken@10661
   906
}
slouken@10661
   907
slouken@10661
   908
/**
slouken@10661
   909
 *  Get the mapping at a particular index.
slouken@10661
   910
 */
slouken@10661
   911
char *
slouken@10661
   912
SDL_GameControllerMappingForIndex(int mapping_index)
slouken@10661
   913
{
slouken@10661
   914
    ControllerMapping_t *mapping;
slouken@10661
   915
slouken@10661
   916
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
   917
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
   918
            continue;
slouken@10661
   919
        }
slouken@10661
   920
        if (mapping_index == 0) {
slouken@10661
   921
            char *pMappingString;
slouken@10661
   922
            char pchGUID[33];
slouken@10661
   923
            size_t needed;
slouken@10661
   924
slouken@10661
   925
            SDL_JoystickGetGUIDString(mapping->guid, pchGUID, sizeof(pchGUID));
slouken@10661
   926
            /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@10661
   927
            needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@10661
   928
            pMappingString = SDL_malloc(needed);
slouken@10661
   929
            if (!pMappingString) {
slouken@10661
   930
                SDL_OutOfMemory();
slouken@10661
   931
                return NULL;
slouken@10661
   932
            }
slouken@10661
   933
            SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@10661
   934
            return pMappingString;
slouken@10661
   935
        }
slouken@10661
   936
        --mapping_index;
slouken@10661
   937
    }
slouken@10661
   938
    return NULL;
slouken@10661
   939
}
slouken@10661
   940
slouken@10605
   941
/*
slouken@8582
   942
 * Get the mapping string for this GUID
slouken@8582
   943
 */
slouken@8582
   944
char *
slouken@8772
   945
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
slouken@8582
   946
{
slouken@8582
   947
    char *pMappingString = NULL;
slouken@8582
   948
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@8582
   949
    if (mapping) {
slouken@8582
   950
        char pchGUID[33];
slouken@8582
   951
        size_t needed;
slouken@8582
   952
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@8582
   953
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@8582
   954
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@8772
   955
        pMappingString = SDL_malloc(needed);
philipp@9197
   956
        if (!pMappingString) {
philipp@9197
   957
            SDL_OutOfMemory();
philipp@9197
   958
            return NULL;
philipp@9197
   959
        }
slouken@8772
   960
        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@8582
   961
    }
slouken@8582
   962
    return pMappingString;
slouken@8582
   963
}
slouken@8582
   964
slouken@8582
   965
/*
slouken@8582
   966
 * Get the mapping string for this device
slouken@8582
   967
 */
slouken@8582
   968
char *
slouken@8772
   969
SDL_GameControllerMapping(SDL_GameController * gamecontroller)
slouken@8582
   970
{
philipp@9196
   971
    if (!gamecontroller) {
philipp@9196
   972
        return NULL;
philipp@9196
   973
    }
philipp@9196
   974
slouken@8772
   975
    return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
slouken@8582
   976
}
slouken@8582
   977
slouken@8582
   978
static void
slouken@8582
   979
SDL_GameControllerLoadHints()
slouken@8582
   980
{
slouken@8582
   981
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
slouken@8772
   982
    if (hint && hint[0]) {
slouken@8772
   983
        size_t nchHints = SDL_strlen(hint);
slouken@8772
   984
        char *pUserMappings = SDL_malloc(nchHints + 1);
slouken@8582
   985
        char *pTempMappings = pUserMappings;
slouken@8772
   986
        SDL_memcpy(pUserMappings, hint, nchHints);
slouken@8267
   987
        pUserMappings[nchHints] = '\0';
slouken@8772
   988
        while (pUserMappings) {
slouken@8582
   989
            char *pchNewLine = NULL;
slouken@8582
   990
slouken@8772
   991
            pchNewLine = SDL_strchr(pUserMappings, '\n');
slouken@8772
   992
            if (pchNewLine)
slouken@8582
   993
                *pchNewLine = '\0';
slouken@8582
   994
slouken@10605
   995
            SDL_PrivateGameControllerAddMapping(pUserMappings, SDL_CONTROLLER_MAPPING_PRIORITY_USER);
slouken@8582
   996
slouken@8772
   997
            if (pchNewLine) {
slouken@8582
   998
                pUserMappings = pchNewLine + 1;
slouken@8772
   999
            } else {
slouken@8582
  1000
                pUserMappings = NULL;
slouken@8772
  1001
            }
slouken@8582
  1002
        }
slouken@8582
  1003
        SDL_free(pTempMappings);
slouken@8582
  1004
    }
slouken@8582
  1005
}
slouken@8582
  1006
slouken@8582
  1007
/*
slouken@8582
  1008
 * Initialize the game controller system, mostly load our DB of controller config mappings
slouken@8582
  1009
 */
slouken@8582
  1010
int
slouken@8582
  1011
SDL_GameControllerInit(void)
slouken@8582
  1012
{
slouken@8582
  1013
    int i = 0;
slouken@8582
  1014
    const char *pMappingString = NULL;
slouken@8582
  1015
    pMappingString = s_ControllerMappings[i];
slouken@8772
  1016
    while (pMappingString) {
slouken@10605
  1017
        SDL_PrivateGameControllerAddMapping(pMappingString, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@8582
  1018
slouken@8582
  1019
        i++;
slouken@8582
  1020
        pMappingString = s_ControllerMappings[i];
slouken@8582
  1021
    }
slouken@8582
  1022
slouken@8582
  1023
    /* load in any user supplied config */
slouken@8582
  1024
    SDL_GameControllerLoadHints();
slouken@8582
  1025
slouken@8582
  1026
    /* watch for joy events and fire controller ones if needed */
slouken@8772
  1027
    SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1028
slouken@8067
  1029
    /* Send added events for controllers currently attached */
slouken@8067
  1030
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@8067
  1031
        if (SDL_IsGameController(i)) {
slouken@8067
  1032
            SDL_Event deviceevent;
slouken@8067
  1033
            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@8067
  1034
            deviceevent.cdevice.which = i;
slouken@8067
  1035
            SDL_PushEvent(&deviceevent);
slouken@8067
  1036
        }
slouken@8067
  1037
    }
slouken@8067
  1038
slouken@8582
  1039
    return (0);
slouken@8582
  1040
}
slouken@8582
  1041
slouken@8582
  1042
slouken@8582
  1043
/*
slouken@8582
  1044
 * Get the implementation dependent name of a controller
slouken@8582
  1045
 */
slouken@8582
  1046
const char *
slouken@8582
  1047
SDL_GameControllerNameForIndex(int device_index)
slouken@8582
  1048
{
slouken@8582
  1049
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1050
    if (pSupportedController) {
slouken@8582
  1051
        return pSupportedController->name;
slouken@8582
  1052
    }
slouken@8582
  1053
    return NULL;
slouken@8582
  1054
}
slouken@8582
  1055
slouken@8582
  1056
slouken@8582
  1057
/*
slouken@8582
  1058
 * Return 1 if the joystick at this device index is a supported controller
slouken@8582
  1059
 */
slouken@8582
  1060
SDL_bool
slouken@8582
  1061
SDL_IsGameController(int device_index)
slouken@8582
  1062
{
slouken@8582
  1063
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1064
    if (pSupportedController) {
slouken@8582
  1065
        return SDL_TRUE;
slouken@8582
  1066
    }
slouken@8582
  1067
slouken@8582
  1068
    return SDL_FALSE;
slouken@8582
  1069
}
slouken@8582
  1070
slouken@8582
  1071
/*
slouken@8582
  1072
 * Open a controller for use - the index passed as an argument refers to
slouken@8582
  1073
 * the N'th controller on the system.  This index is the value which will
slouken@8582
  1074
 * identify this controller in future controller events.
slouken@8582
  1075
 *
slouken@8582
  1076
 * This function returns a controller identifier, or NULL if an error occurred.
slouken@8582
  1077
 */
slouken@8582
  1078
SDL_GameController *
slouken@8582
  1079
SDL_GameControllerOpen(int device_index)
slouken@8582
  1080
{
slouken@8582
  1081
    SDL_GameController *gamecontroller;
slouken@8582
  1082
    SDL_GameController *gamecontrollerlist;
slouken@8582
  1083
    ControllerMapping_t *pSupportedController = NULL;
slouken@8582
  1084
slouken@8582
  1085
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@8582
  1086
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@8582
  1087
        return (NULL);
slouken@8582
  1088
    }
slouken@8582
  1089
slouken@10688
  1090
    SDL_LockJoystickList();
slouken@10688
  1091
slouken@8582
  1092
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1093
    /* If the controller is already open, return it */
slouken@8772
  1094
    while (gamecontrollerlist) {
slouken@8772
  1095
        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
slouken@8582
  1096
                gamecontroller = gamecontrollerlist;
slouken@8582
  1097
                ++gamecontroller->ref_count;
slouken@10688
  1098
                SDL_UnlockJoystickList();
slouken@8582
  1099
                return (gamecontroller);
slouken@8582
  1100
        }
slouken@8582
  1101
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1102
    }
slouken@8582
  1103
slouken@8582
  1104
    /* Find a controller mapping */
slouken@8582
  1105
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1106
    if (!pSupportedController) {
slouken@8772
  1107
        SDL_SetError("Couldn't find mapping for device (%d)", device_index);
slouken@10688
  1108
        SDL_UnlockJoystickList();
slouken@10688
  1109
        return NULL;
slouken@8582
  1110
    }
slouken@8582
  1111
slouken@8582
  1112
    /* Create and initialize the joystick */
slouken@8582
  1113
    gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
slouken@8582
  1114
    if (gamecontroller == NULL) {
slouken@8582
  1115
        SDL_OutOfMemory();
slouken@10688
  1116
        SDL_UnlockJoystickList();
slouken@8582
  1117
        return NULL;
slouken@8582
  1118
    }
slouken@8582
  1119
slouken@8582
  1120
    SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
slouken@8582
  1121
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
slouken@8772
  1122
    if (!gamecontroller->joystick) {
slouken@8582
  1123
        SDL_free(gamecontroller);
slouken@10688
  1124
        SDL_UnlockJoystickList();
slouken@8582
  1125
        return NULL;
slouken@8582
  1126
    }
slouken@8582
  1127
slouken@8772
  1128
    SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
slouken@8582
  1129
slouken@10476
  1130
    /* The triggers are mapped from -32768 to 32767, where -32768 is the 'unpressed' value */
slouken@10476
  1131
    {
slouken@10476
  1132
        int leftTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT];
slouken@10476
  1133
        int rightTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT];
slouken@10476
  1134
        if (leftTriggerMapping >= 0) {
slouken@10477
  1135
            gamecontroller->joystick->axes[leftTriggerMapping] =
slouken@10477
  1136
            gamecontroller->joystick->axes_zero[leftTriggerMapping] = (Sint16)-32768;
slouken@10476
  1137
        }
slouken@10476
  1138
        if (rightTriggerMapping >= 0) {
slouken@10477
  1139
            gamecontroller->joystick->axes[rightTriggerMapping] =
slouken@10477
  1140
            gamecontroller->joystick->axes_zero[rightTriggerMapping] = (Sint16)-32768;
slouken@10476
  1141
        }
slouken@10476
  1142
    }
slouken@10476
  1143
slouken@8582
  1144
    /* Add joystick to list */
slouken@8582
  1145
    ++gamecontroller->ref_count;
slouken@8582
  1146
    /* Link the joystick in the list */
slouken@8582
  1147
    gamecontroller->next = SDL_gamecontrollers;
slouken@8582
  1148
    SDL_gamecontrollers = gamecontroller;
slouken@8582
  1149
slouken@10688
  1150
    SDL_UnlockJoystickList();
slouken@8582
  1151
slouken@8582
  1152
    return (gamecontroller);
slouken@8582
  1153
}
slouken@8582
  1154
slouken@8582
  1155
/*
slouken@8582
  1156
 * Manually pump for controller updates.
slouken@8582
  1157
 */
slouken@8582
  1158
void
slouken@8582
  1159
SDL_GameControllerUpdate(void)
slouken@8582
  1160
{
slouken@8582
  1161
    /* Just for API completeness; the joystick API does all the work. */
slouken@8582
  1162
    SDL_JoystickUpdate();
slouken@8582
  1163
}
slouken@8582
  1164
slouken@8582
  1165
slouken@8582
  1166
/*
slouken@8582
  1167
 * Get the current state of an axis control on a controller
slouken@8582
  1168
 */
slouken@8582
  1169
Sint16
slouken@8582
  1170
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1171
{
slouken@8772
  1172
    if (!gamecontroller)
slouken@8582
  1173
        return 0;
slouken@8582
  1174
slouken@8772
  1175
    if (gamecontroller->mapping.axes[axis] >= 0) {
slouken@8772
  1176
        Sint16 value = (SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axes[axis]));
slouken@8772
  1177
        switch (axis) {
slouken@8582
  1178
            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
slouken@8582
  1179
            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
slouken@10476
  1180
                /* Shift it to be 0 - 32767 */
slouken@8582
  1181
                value = value / 2 + 16384;
slouken@8582
  1182
            default:
slouken@8582
  1183
                break;
slouken@8582
  1184
        }
slouken@8582
  1185
        return value;
slouken@8772
  1186
    } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
slouken@8582
  1187
        Uint8 value;
slouken@8772
  1188
        value = SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis]);
slouken@8772
  1189
        if (value > 0)
slouken@8582
  1190
            return 32767;
slouken@8582
  1191
        return 0;
slouken@8582
  1192
    }
slouken@8582
  1193
    return 0;
slouken@8582
  1194
}
slouken@8582
  1195
slouken@8582
  1196
slouken@8582
  1197
/*
slouken@8582
  1198
 * Get the current state of a button on a controller
slouken@8582
  1199
 */
slouken@8582
  1200
Uint8
slouken@8582
  1201
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1202
{
slouken@8772
  1203
    if (!gamecontroller)
slouken@8582
  1204
        return 0;
slouken@8582
  1205
slouken@8772
  1206
    if (gamecontroller->mapping.buttons[button] >= 0) {
slouken@8772
  1207
        return (SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttons[button]));
slouken@8772
  1208
    } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
slouken@8582
  1209
        Sint16 value;
slouken@8772
  1210
        value = SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button]);
slouken@8772
  1211
        if (ABS(value) > 32768/2)
slouken@8582
  1212
            return 1;
slouken@8582
  1213
        return 0;
slouken@8772
  1214
    } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
slouken@8582
  1215
        Uint8 value;
slouken@8772
  1216
        value = SDL_JoystickGetHat(gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat);
slouken@8582
  1217
slouken@8772
  1218
        if (value & gamecontroller->mapping.hatasbutton[button].mask)
slouken@8582
  1219
            return 1;
slouken@8582
  1220
        return 0;
slouken@8582
  1221
    }
slouken@8582
  1222
slouken@8582
  1223
    return 0;
slouken@8582
  1224
}
slouken@8582
  1225
slouken@10595
  1226
const char *
slouken@10595
  1227
SDL_GameControllerName(SDL_GameController * gamecontroller)
slouken@10595
  1228
{
slouken@10595
  1229
    if (!gamecontroller)
slouken@10595
  1230
        return NULL;
slouken@10595
  1231
slouken@10595
  1232
    return gamecontroller->mapping.name;
slouken@10595
  1233
}
slouken@10595
  1234
slouken@10595
  1235
Uint16
slouken@10595
  1236
SDL_GameControllerGetVendor(SDL_GameController * gamecontroller)
slouken@10595
  1237
{
slouken@10595
  1238
    return SDL_JoystickGetVendor(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1239
}
slouken@10595
  1240
slouken@10595
  1241
Uint16
slouken@10595
  1242
SDL_GameControllerGetProduct(SDL_GameController * gamecontroller)
slouken@10595
  1243
{
slouken@10595
  1244
    return SDL_JoystickGetProduct(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1245
}
slouken@10595
  1246
slouken@10595
  1247
Uint16
slouken@10595
  1248
SDL_GameControllerGetProductVersion(SDL_GameController * gamecontroller)
slouken@10595
  1249
{
slouken@10595
  1250
    return SDL_JoystickGetProductVersion(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1251
}
slouken@10595
  1252
slouken@8582
  1253
/*
slouken@8582
  1254
 * Return if the joystick in question is currently attached to the system,
slouken@8582
  1255
 *  \return 0 if not plugged in, 1 if still present.
slouken@8582
  1256
 */
slouken@8582
  1257
SDL_bool
slouken@8772
  1258
SDL_GameControllerGetAttached(SDL_GameController * gamecontroller)
slouken@8582
  1259
{
slouken@8772
  1260
    if (!gamecontroller)
slouken@8582
  1261
        return SDL_FALSE;
slouken@8582
  1262
slouken@8582
  1263
    return SDL_JoystickGetAttached(gamecontroller->joystick);
slouken@8582
  1264
}
slouken@8582
  1265
slouken@8582
  1266
/*
slouken@8582
  1267
 * Get the joystick for this controller
slouken@8582
  1268
 */
slouken@8582
  1269
SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
slouken@8582
  1270
{
slouken@8772
  1271
    if (!gamecontroller)
slouken@8582
  1272
        return NULL;
slouken@8582
  1273
slouken@8582
  1274
    return gamecontroller->joystick;
slouken@8582
  1275
}
slouken@8582
  1276
icculus@9916
  1277
icculus@9916
  1278
/*
icculus@9916
  1279
 * Find the SDL_GameController that owns this instance id
icculus@9916
  1280
 */
icculus@9916
  1281
SDL_GameController *
icculus@9916
  1282
SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
icculus@9916
  1283
{
slouken@10688
  1284
    SDL_GameController *gamecontroller;
slouken@10688
  1285
slouken@10688
  1286
    SDL_LockJoystickList();
slouken@10688
  1287
    gamecontroller = SDL_gamecontrollers;
icculus@9916
  1288
    while (gamecontroller) {
icculus@9916
  1289
        if (gamecontroller->joystick->instance_id == joyid) {
slouken@10688
  1290
            SDL_UnlockJoystickList();
icculus@9916
  1291
            return gamecontroller;
icculus@9916
  1292
        }
icculus@9916
  1293
        gamecontroller = gamecontroller->next;
icculus@9916
  1294
    }
slouken@10688
  1295
    SDL_UnlockJoystickList();
icculus@9916
  1296
    return NULL;
icculus@9916
  1297
}
icculus@9916
  1298
icculus@9916
  1299
slouken@8582
  1300
/**
slouken@8582
  1301
 * Get the SDL joystick layer binding for this controller axis mapping
slouken@8582
  1302
 */
slouken@8582
  1303
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1304
{
slouken@8582
  1305
    SDL_GameControllerButtonBind bind;
slouken@8772
  1306
    SDL_memset(&bind, 0x0, sizeof(bind));
slouken@8582
  1307
slouken@8772
  1308
    if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
slouken@8582
  1309
        return bind;
slouken@8582
  1310
slouken@8772
  1311
    if (gamecontroller->mapping.axes[axis] >= 0) {
slouken@8582
  1312
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@8582
  1313
        bind.value.button = gamecontroller->mapping.axes[axis];
slouken@8772
  1314
    } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
slouken@8582
  1315
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@8582
  1316
        bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
slouken@8582
  1317
    }
slouken@8582
  1318
slouken@8582
  1319
    return bind;
slouken@8582
  1320
}
slouken@8582
  1321
slouken@8582
  1322
slouken@8582
  1323
/**
slouken@8582
  1324
 * Get the SDL joystick layer binding for this controller button mapping
slouken@8582
  1325
 */
slouken@8582
  1326
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1327
{
slouken@8582
  1328
    SDL_GameControllerButtonBind bind;
slouken@8772
  1329
    SDL_memset(&bind, 0x0, sizeof(bind));
slouken@8582
  1330
slouken@8772
  1331
    if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1332
        return bind;
slouken@8582
  1333
slouken@8772
  1334
    if (gamecontroller->mapping.buttons[button] >= 0) {
slouken@8582
  1335
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@8582
  1336
        bind.value.button = gamecontroller->mapping.buttons[button];
slouken@8772
  1337
    } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
slouken@8582
  1338
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@8582
  1339
        bind.value.axis = gamecontroller->mapping.axesasbutton[button];
slouken@8772
  1340
    } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
slouken@8582
  1341
        bind.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
slouken@8582
  1342
        bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
slouken@8582
  1343
        bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
slouken@8582
  1344
    }
slouken@8582
  1345
slouken@8582
  1346
    return bind;
slouken@8582
  1347
}
slouken@8582
  1348
slouken@8582
  1349
slouken@8582
  1350
void
slouken@8582
  1351
SDL_GameControllerClose(SDL_GameController * gamecontroller)
slouken@8582
  1352
{
slouken@8582
  1353
    SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
slouken@8582
  1354
slouken@8772
  1355
    if (!gamecontroller)
slouken@8582
  1356
        return;
slouken@8582
  1357
slouken@10688
  1358
    SDL_LockJoystickList();
slouken@10688
  1359
slouken@8582
  1360
    /* First decrement ref count */
slouken@8582
  1361
    if (--gamecontroller->ref_count > 0) {
slouken@10688
  1362
        SDL_UnlockJoystickList();
slouken@8582
  1363
        return;
slouken@8582
  1364
    }
slouken@8582
  1365
slouken@8772
  1366
    SDL_JoystickClose(gamecontroller->joystick);
slouken@8582
  1367
slouken@8582
  1368
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1369
    gamecontrollerlistprev = NULL;
slouken@8772
  1370
    while (gamecontrollerlist) {
slouken@8772
  1371
        if (gamecontroller == gamecontrollerlist) {
slouken@8772
  1372
            if (gamecontrollerlistprev) {
slouken@8582
  1373
                /* unlink this entry */
slouken@8582
  1374
                gamecontrollerlistprev->next = gamecontrollerlist->next;
slouken@8772
  1375
            } else {
slouken@8582
  1376
                SDL_gamecontrollers = gamecontroller->next;
slouken@8582
  1377
            }
slouken@8582
  1378
            break;
slouken@8582
  1379
        }
slouken@8582
  1380
        gamecontrollerlistprev = gamecontrollerlist;
slouken@8582
  1381
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1382
    }
slouken@8582
  1383
slouken@8582
  1384
    SDL_free(gamecontroller);
slouken@10688
  1385
slouken@10688
  1386
    SDL_UnlockJoystickList();
slouken@8582
  1387
}
slouken@8582
  1388
slouken@8582
  1389
slouken@8582
  1390
/*
slouken@8582
  1391
 * Quit the controller subsystem
slouken@8582
  1392
 */
slouken@8582
  1393
void
slouken@8582
  1394
SDL_GameControllerQuit(void)
slouken@8582
  1395
{
slouken@8582
  1396
    ControllerMapping_t *pControllerMap;
slouken@10688
  1397
slouken@10688
  1398
    SDL_LockJoystickList();
slouken@8772
  1399
    while (SDL_gamecontrollers) {
slouken@8582
  1400
        SDL_gamecontrollers->ref_count = 1;
slouken@8582
  1401
        SDL_GameControllerClose(SDL_gamecontrollers);
slouken@8582
  1402
    }
slouken@10688
  1403
    SDL_UnlockJoystickList();
slouken@8582
  1404
slouken@8772
  1405
    while (s_pSupportedControllers) {
slouken@8582
  1406
        pControllerMap = s_pSupportedControllers;
slouken@8582
  1407
        s_pSupportedControllers = s_pSupportedControllers->next;
slouken@8772
  1408
        SDL_free(pControllerMap->name);
slouken@8772
  1409
        SDL_free(pControllerMap->mapping);
slouken@8772
  1410
        SDL_free(pControllerMap);
slouken@8582
  1411
    }
slouken@8582
  1412
slouken@8772
  1413
    SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1414
slouken@8582
  1415
}
slouken@8582
  1416
slouken@8582
  1417
/*
slouken@8582
  1418
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1419
 */
slouken@8582
  1420
int
slouken@8582
  1421
SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
slouken@8582
  1422
{
slouken@8582
  1423
    int posted;
slouken@8582
  1424
slouken@8582
  1425
    /* translate the event, if desired */
slouken@8582
  1426
    posted = 0;
slouken@8582
  1427
#if !SDL_EVENTS_DISABLED
slouken@8582
  1428
    if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
slouken@8582
  1429
        SDL_Event event;
slouken@8582
  1430
        event.type = SDL_CONTROLLERAXISMOTION;
slouken@8582
  1431
        event.caxis.which = gamecontroller->joystick->instance_id;
slouken@8582
  1432
        event.caxis.axis = axis;
slouken@8582
  1433
        event.caxis.value = value;
slouken@8582
  1434
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  1435
    }
slouken@8582
  1436
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1437
    return (posted);
slouken@8582
  1438
}
slouken@8582
  1439
slouken@8582
  1440
slouken@8582
  1441
/*
slouken@8582
  1442
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1443
 */
slouken@8582
  1444
int
slouken@8582
  1445
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
slouken@8582
  1446
{
slouken@8582
  1447
    int posted;
slouken@8582
  1448
#if !SDL_EVENTS_DISABLED
slouken@8582
  1449
    SDL_Event event;
slouken@8582
  1450
slouken@8772
  1451
    if (button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1452
        return (0);
slouken@8582
  1453
slouken@8582
  1454
    switch (state) {
slouken@8582
  1455
    case SDL_PRESSED:
slouken@8582
  1456
        event.type = SDL_CONTROLLERBUTTONDOWN;
slouken@8582
  1457
        break;
slouken@8582
  1458
    case SDL_RELEASED:
slouken@8582
  1459
        event.type = SDL_CONTROLLERBUTTONUP;
slouken@8582
  1460
        break;
slouken@8582
  1461
    default:
slouken@8582
  1462
        /* Invalid state -- bail */
slouken@8582
  1463
        return (0);
slouken@8582
  1464
    }
slouken@8582
  1465
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1466
slouken@8582
  1467
    /* translate the event, if desired */
slouken@8582
  1468
    posted = 0;
slouken@8582
  1469
#if !SDL_EVENTS_DISABLED
slouken@8582
  1470
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@8582
  1471
        event.cbutton.which = gamecontroller->joystick->instance_id;
slouken@8582
  1472
        event.cbutton.button = button;
slouken@8582
  1473
        event.cbutton.state = state;
slouken@8582
  1474
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  1475
    }
slouken@8582
  1476
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1477
    return (posted);
slouken@8582
  1478
}
slouken@8582
  1479
slouken@8582
  1480
/*
slouken@8582
  1481
 * Turn off controller events
slouken@8582
  1482
 */
slouken@8582
  1483
int
slouken@8582
  1484
SDL_GameControllerEventState(int state)
slouken@8582
  1485
{
slouken@8582
  1486
#if SDL_EVENTS_DISABLED
slouken@8582
  1487
    return SDL_IGNORE;
slouken@8582
  1488
#else
slouken@8582
  1489
    const Uint32 event_list[] = {
slouken@8582
  1490
        SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
slouken@8582
  1491
        SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
slouken@8582
  1492
    };
slouken@8582
  1493
    unsigned int i;
slouken@8582
  1494
slouken@8582
  1495
    switch (state) {
slouken@8582
  1496
    case SDL_QUERY:
slouken@8582
  1497
        state = SDL_IGNORE;
slouken@8582
  1498
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  1499
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@8582
  1500
            if (state == SDL_ENABLE) {
slouken@8582
  1501
                break;
slouken@8582
  1502
            }
slouken@8582
  1503
        }
slouken@8582
  1504
        break;
slouken@8582
  1505
    default:
slouken@8582
  1506
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  1507
            SDL_EventState(event_list[i], state);
slouken@8582
  1508
        }
slouken@8582
  1509
        break;
slouken@8582
  1510
    }
slouken@8582
  1511
    return (state);
slouken@8582
  1512
#endif /* SDL_EVENTS_DISABLED */
slouken@8582
  1513
}
slouken@8582
  1514
slouken@8582
  1515
/* vi: set ts=4 sw=4 expandtab: */