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