src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 11 Nov 2016 13:29:23 -0800
changeset 10605 8a059b7b01c4
parent 10602 20855a38e048
child 10609 d702ecbd8ba7
permissions -rw-r--r--
Fixed bug 3079 - Allow non destructive SDL_GameControllerAddMappingsFromFile

x414e54

It is a bit of a pain to update the library or rely on whatever version the user has on their computer for default mappings.

So providing an easily updatable text file via SDL_GameControllerAddMappingsFromFile is still currently the most viable way. However using this replaces all mappings provided by the SDL_HINT_GAMECONTROLLERCONFIG environment variable which may have come from the user's custom Steam mapping.

There should be an easy way for games to supply extra game controller mappings to fill in the differences between SDL versions without it clobbering the SDL_HINT_GAMECONTROLLERCONFIG environment variable.

Internally the mappings could use a priority system and if the priority is lower then it will not overwrite the mappings.

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