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