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