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