src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 06 Dec 2013 08:24:00 -0800
changeset 8052 f0c6c1a45fb1
parent 8046 e7c89b29932a
child 8053 495f4a047be1
permissions -rw-r--r--
Fixed compiler warning on Visual Studio
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@8046
   661
SDL_GameControllerAddMappingsFromRW( SDL_RWops * rw, int freerw )
gabomdq@8042
   662
{
gabomdq@8042
   663
    const char *platform = SDL_GetPlatform();
gabomdq@8042
   664
    int controllers = 0;
gabomdq@8042
   665
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
gabomdq@8042
   666
    size_t db_size, platform_len;
gabomdq@8042
   667
    
gabomdq@8042
   668
    if (rw == NULL) {
gabomdq@8046
   669
        return SDL_SetError("Invalid RWops");
gabomdq@8042
   670
    }
slouken@8052
   671
    db_size = (size_t)SDL_RWsize(rw);
gabomdq@8042
   672
    
slouken@8052
   673
    buf = (char *)SDL_malloc(db_size + 1);
gabomdq@8042
   674
    if (buf == NULL) {
gabomdq@8046
   675
        if (freerw) {
gabomdq@8046
   676
            SDL_RWclose(rw);
gabomdq@8046
   677
        }
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@8046
   682
        if (freerw) {
gabomdq@8046
   683
            SDL_RWclose(rw);
gabomdq@8046
   684
        }
gabomdq@8042
   685
        SDL_free(buf);
gabomdq@8042
   686
        return SDL_SetError("Could not read DB");
gabomdq@8042
   687
    }
gabomdq@8046
   688
    
gabomdq@8046
   689
    if (freerw) {
gabomdq@8046
   690
        SDL_RWclose(rw);
gabomdq@8046
   691
    }
gabomdq@8042
   692
    
gabomdq@8042
   693
    buf[db_size] = '\0';
gabomdq@8042
   694
    line = buf;
gabomdq@8042
   695
    
gabomdq@8042
   696
    while (line < buf + db_size) {
gabomdq@8042
   697
        line_end = SDL_strchr( line, '\n' );
gabomdq@8042
   698
        if (line_end != NULL) {
gabomdq@8042
   699
            *line_end = '\0';
gabomdq@8042
   700
        }
gabomdq@8042
   701
        else {
gabomdq@8042
   702
            line_end = buf + db_size;
gabomdq@8042
   703
        }
gabomdq@8042
   704
        
gabomdq@8042
   705
        /* Extract and verify the platform */
gabomdq@8042
   706
        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
gabomdq@8042
   707
        if ( tmp != NULL ) {
gabomdq@8042
   708
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
gabomdq@8042
   709
            comma = SDL_strchr(tmp, ',');
gabomdq@8042
   710
            if (comma != NULL) {
gabomdq@8042
   711
                platform_len = comma - tmp + 1;
gabomdq@8042
   712
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
gabomdq@8042
   713
                    SDL_strlcpy(line_platform, tmp, platform_len);
gabomdq@8042
   714
                    if(SDL_strncasecmp(line_platform, platform, platform_len) == 0
gabomdq@8042
   715
                        && SDL_GameControllerAddMapping(line) > 0) {
gabomdq@8042
   716
                        controllers++;
gabomdq@8042
   717
                    }
gabomdq@8042
   718
                }
gabomdq@8042
   719
            }
gabomdq@8042
   720
        }
gabomdq@8042
   721
        
gabomdq@8042
   722
        line = line_end + 1;
gabomdq@8042
   723
    }
gabomdq@8042
   724
gabomdq@8042
   725
    SDL_free(buf);
gabomdq@8042
   726
    return controllers;
gabomdq@8042
   727
}
gabomdq@8042
   728
gabomdq@8042
   729
/*
gabomdq@8042
   730
 * Add or update an entry into the Mappings Database
gabomdq@8042
   731
 */
gabomdq@8042
   732
int
urkle@6964
   733
SDL_GameControllerAddMapping( const char *mappingString )
urkle@6964
   734
{
slouken@7191
   735
    char *pchGUID;
slouken@7191
   736
    char *pchName;
slouken@7191
   737
    char *pchMapping;
urkle@6964
   738
    SDL_JoystickGUID jGUID;
urkle@6964
   739
    ControllerMapping_t *pControllerMapping;
urkle@6964
   740
#ifdef SDL_JOYSTICK_DINPUT
urkle@6964
   741
    SDL_bool is_xinput_mapping = SDL_FALSE;
urkle@6964
   742
#endif
urkle@6964
   743
slouken@6975
   744
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString( mappingString );
slouken@6975
   745
    if (!pchGUID) {
slouken@6975
   746
        return -1;
slouken@6975
   747
    }
urkle@6964
   748
#ifdef SDL_JOYSTICK_DINPUT
urkle@6964
   749
    if ( !SDL_strcasecmp( pchGUID, "xinput" ) ) {
urkle@6964
   750
        is_xinput_mapping = SDL_TRUE;
urkle@6964
   751
    }
urkle@6964
   752
#endif
slouken@6975
   753
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
urkle@6964
   754
    SDL_free(pchGUID);
urkle@6964
   755
slouken@7191
   756
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
urkle@6964
   757
slouken@7191
   758
    pchName = SDL_PrivateGetControllerNameFromMappingString( mappingString );
slouken@7191
   759
    if (!pchName) return -1;
urkle@6964
   760
slouken@7191
   761
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString( mappingString );
slouken@7191
   762
    if (!pchMapping) {
slouken@7191
   763
        SDL_free( pchName );
slouken@7191
   764
        return -1;
slouken@7191
   765
    }
urkle@6964
   766
slouken@7191
   767
    if (pControllerMapping) {
slouken@7191
   768
        /* Update existing mapping */
slouken@7191
   769
        SDL_free( pControllerMapping->name );
slouken@7191
   770
        pControllerMapping->name = pchName;
slouken@7191
   771
        SDL_free( pControllerMapping->mapping );
slouken@7191
   772
        pControllerMapping->mapping = pchMapping;
slouken@7191
   773
        /* refresh open controllers */
slouken@7191
   774
        SDL_PrivateGameControllerRefreshMapping( pControllerMapping );
slouken@7191
   775
        return 0;
slouken@7191
   776
    } else {
slouken@7191
   777
        pControllerMapping = SDL_malloc( sizeof(*pControllerMapping) );
slouken@7191
   778
        if (!pControllerMapping) {
slouken@7191
   779
            SDL_free( pchName );
slouken@7191
   780
            SDL_free( pchMapping );
slouken@7191
   781
            return SDL_OutOfMemory();
slouken@7191
   782
        }
urkle@6964
   783
#ifdef SDL_JOYSTICK_DINPUT
slouken@7191
   784
        if ( is_xinput_mapping )
slouken@7191
   785
        {
slouken@7191
   786
            s_pXInputMapping = pControllerMapping;
slouken@7191
   787
        }
urkle@6964
   788
#endif
slouken@7191
   789
        pControllerMapping->guid = jGUID;
slouken@7191
   790
        pControllerMapping->name = pchName;
slouken@7191
   791
        pControllerMapping->mapping = pchMapping;
slouken@7191
   792
        pControllerMapping->next = s_pSupportedControllers;
slouken@7191
   793
        s_pSupportedControllers = pControllerMapping;
slouken@7191
   794
        return 1;
slouken@7191
   795
    }
urkle@6964
   796
}
urkle@6964
   797
urkle@6964
   798
/*
urkle@6964
   799
 * Get the mapping string for this GUID
urkle@6964
   800
 */
urkle@6964
   801
char *
urkle@6964
   802
SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid )
urkle@6964
   803
{
slouken@7191
   804
    char *pMappingString = NULL;
slouken@7191
   805
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@7191
   806
    if (mapping) {
slouken@7191
   807
        char pchGUID[33];
urkle@6964
   808
        size_t needed;
slouken@7191
   809
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@7191
   810
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@7191
   811
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@7191
   812
        pMappingString = SDL_malloc( needed );
slouken@7191
   813
        SDL_snprintf( pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping );
slouken@7191
   814
    }
slouken@7191
   815
    return pMappingString;
urkle@6964
   816
}
urkle@6964
   817
urkle@6964
   818
/*
urkle@6964
   819
 * Get the mapping string for this device
urkle@6964
   820
 */
urkle@6964
   821
char *
urkle@6964
   822
SDL_GameControllerMapping( SDL_GameController * gamecontroller )
urkle@6964
   823
{
slouken@7191
   824
    return SDL_GameControllerMappingForGUID( gamecontroller->mapping.guid );
urkle@6964
   825
}
slouken@6690
   826
slouken@6976
   827
static void
slouken@6976
   828
SDL_GameControllerLoadHints()
slouken@6976
   829
{
slouken@6976
   830
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
slouken@6976
   831
    if ( hint && hint[0] ) {
slouken@7870
   832
        size_t nchHints = SDL_strlen( hint );
slouken@6976
   833
        char *pUserMappings = SDL_malloc( nchHints + 1 );
slouken@6976
   834
        char *pTempMappings = pUserMappings;
slouken@6976
   835
        SDL_memcpy( pUserMappings, hint, nchHints );
slouken@6976
   836
        while ( pUserMappings ) {
slouken@6976
   837
            char *pchNewLine = NULL;
slouken@6976
   838
slouken@6976
   839
            pchNewLine = SDL_strchr( pUserMappings, '\n' );
slouken@6976
   840
            if ( pchNewLine )
slouken@6976
   841
                *pchNewLine = '\0';
slouken@6976
   842
slouken@6976
   843
            SDL_GameControllerAddMapping( pUserMappings );
slouken@6976
   844
slouken@6976
   845
            if ( pchNewLine )
slouken@6976
   846
                pUserMappings = pchNewLine + 1;
slouken@6976
   847
            else
slouken@6976
   848
                pUserMappings = NULL;
slouken@6976
   849
        }
slouken@6976
   850
        SDL_free(pTempMappings);
slouken@6976
   851
    }
slouken@6976
   852
}
slouken@6976
   853
slouken@6690
   854
/*
slouken@6690
   855
 * Initialize the game controller system, mostly load our DB of controller config mappings
slouken@6690
   856
 */
slouken@6690
   857
int
slouken@6690
   858
SDL_GameControllerInit(void)
slouken@6690
   859
{
slouken@7191
   860
    int i = 0;
slouken@7191
   861
    const char *pMappingString = NULL;
slouken@7191
   862
    s_pSupportedControllers = NULL;
slouken@7191
   863
    pMappingString = s_ControllerMappings[i];
slouken@7191
   864
    while ( pMappingString )
slouken@7191
   865
    {
slouken@7191
   866
        SDL_GameControllerAddMapping( pMappingString );
slouken@6690
   867
slouken@7191
   868
        i++;
slouken@7191
   869
        pMappingString = s_ControllerMappings[i];
slouken@7191
   870
    }
slouken@6690
   871
slouken@7191
   872
    /* load in any user supplied config */
slouken@6976
   873
    SDL_GameControllerLoadHints();
slouken@6690
   874
slouken@7191
   875
    /* watch for joy events and fire controller ones if needed */
slouken@7191
   876
    SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
slouken@6976
   877
slouken@7191
   878
    return (0);
slouken@6690
   879
}
slouken@6690
   880
slouken@6690
   881
slouken@6690
   882
/*
slouken@6690
   883
 * Get the implementation dependent name of a controller
slouken@6690
   884
 */
slouken@6690
   885
const char *
slouken@6690
   886
SDL_GameControllerNameForIndex(int device_index)
slouken@6690
   887
{
slouken@7191
   888
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   889
    if ( pSupportedController )
slouken@7191
   890
    {
slouken@7191
   891
        return pSupportedController->name;
slouken@7191
   892
    }
slouken@6690
   893
    return NULL;
slouken@6690
   894
}
slouken@6690
   895
slouken@6690
   896
slouken@6690
   897
/*
slouken@6690
   898
 * Return 1 if the joystick at this device index is a supported controller
slouken@6690
   899
 */
jorgen@6871
   900
SDL_bool
jorgen@6871
   901
SDL_IsGameController(int device_index)
slouken@6690
   902
{
slouken@7191
   903
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   904
    if ( pSupportedController )
slouken@7191
   905
    {
slouken@7191
   906
        return SDL_TRUE;
slouken@7191
   907
    }
jorgen@6871
   908
slouken@7191
   909
    return SDL_FALSE;
slouken@6690
   910
}
slouken@6690
   911
slouken@6690
   912
/*
slouken@6690
   913
 * Open a controller for use - the index passed as an argument refers to
slouken@6690
   914
 * the N'th controller on the system.  This index is the value which will
slouken@6690
   915
 * identify this controller in future controller events.
slouken@6690
   916
 *
slouken@6690
   917
 * This function returns a controller identifier, or NULL if an error occurred.
slouken@6690
   918
 */
slouken@6690
   919
SDL_GameController *
slouken@6690
   920
SDL_GameControllerOpen(int device_index)
slouken@6690
   921
{
slouken@6690
   922
    SDL_GameController *gamecontroller;
slouken@7191
   923
    SDL_GameController *gamecontrollerlist;
slouken@7191
   924
    ControllerMapping_t *pSupportedController = NULL;
slouken@6690
   925
slouken@6690
   926
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@6690
   927
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@6690
   928
        return (NULL);
slouken@6690
   929
    }
slouken@6690
   930
slouken@7191
   931
    gamecontrollerlist = SDL_gamecontrollers;
slouken@7191
   932
    /* If the controller is already open, return it */
slouken@7191
   933
    while ( gamecontrollerlist )
slouken@7191
   934
    {
slouken@7191
   935
        if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id ) {
slouken@7191
   936
                gamecontroller = gamecontrollerlist;
slouken@7191
   937
                ++gamecontroller->ref_count;
slouken@7191
   938
                return (gamecontroller);
slouken@7191
   939
        }
slouken@7191
   940
        gamecontrollerlist = gamecontrollerlist->next;
slouken@6690
   941
    }
slouken@6690
   942
slouken@7191
   943
    /* Find a controller mapping */
slouken@7191
   944
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   945
    if ( !pSupportedController ) {
slouken@7191
   946
        SDL_SetError("Couldn't find mapping for device (%d)", device_index );
slouken@7191
   947
        return (NULL);
slouken@7191
   948
    }
slouken@6690
   949
slouken@7191
   950
    /* Create and initialize the joystick */
slouken@7191
   951
    gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
slouken@7191
   952
    if (gamecontroller == NULL) {
slouken@7191
   953
        SDL_OutOfMemory();
slouken@7191
   954
        return NULL;
slouken@7191
   955
    }
slouken@6841
   956
slouken@6690
   957
    SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
slouken@6690
   958
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
slouken@7191
   959
    if ( !gamecontroller->joystick ) {
slouken@6690
   960
        SDL_free(gamecontroller);
slouken@6690
   961
        return NULL;
slouken@6690
   962
    }
slouken@6690
   963
slouken@7191
   964
    SDL_PrivateLoadButtonMapping( &gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping );
slouken@6690
   965
slouken@7191
   966
    /* Add joystick to list */
slouken@6690
   967
    ++gamecontroller->ref_count;
slouken@7191
   968
    /* Link the joystick in the list */
slouken@7191
   969
    gamecontroller->next = SDL_gamecontrollers;
slouken@7191
   970
    SDL_gamecontrollers = gamecontroller;
slouken@6690
   971
slouken@7191
   972
    SDL_SYS_JoystickUpdate( gamecontroller->joystick );
slouken@6690
   973
slouken@6690
   974
    return (gamecontroller);
slouken@6690
   975
}
slouken@6690
   976
icculus@6919
   977
/*
icculus@6919
   978
 * Manually pump for controller updates.
icculus@6919
   979
 */
icculus@6919
   980
void
icculus@6919
   981
SDL_GameControllerUpdate(void)
icculus@6919
   982
{
icculus@6919
   983
    /* Just for API completeness; the joystick API does all the work. */
icculus@6919
   984
    SDL_JoystickUpdate();
icculus@6919
   985
}
icculus@6919
   986
slouken@6690
   987
slouken@6690
   988
/*
slouken@6690
   989
 * Get the current state of an axis control on a controller
slouken@6690
   990
 */
slouken@6690
   991
Sint16
icculus@6917
   992
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@6690
   993
{
slouken@7191
   994
    if ( !gamecontroller )
slouken@7191
   995
        return 0;
slouken@6690
   996
slouken@7191
   997
    if (gamecontroller->mapping.axes[axis] >= 0 )
slouken@7191
   998
    {
urkle@7248
   999
        Sint16 value = ( SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axes[axis]) );
urkle@7248
  1000
        switch (axis)
urkle@7248
  1001
        {
urkle@7248
  1002
            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
urkle@7248
  1003
            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
urkle@7248
  1004
                /* Shift it to be 0 - 32767. */
urkle@7248
  1005
                value = value / 2 + 16384;
urkle@7248
  1006
            default:
urkle@7248
  1007
                break;
urkle@7248
  1008
        }
urkle@7248
  1009
        return value;
slouken@7191
  1010
    }
slouken@7191
  1011
    else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
slouken@7191
  1012
    {
slouken@7191
  1013
        Uint8 value;
slouken@7191
  1014
        value = SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis] );
slouken@7191
  1015
        if ( value > 0 )
slouken@7191
  1016
            return 32767;
slouken@7191
  1017
        return 0;
slouken@7191
  1018
    }
slouken@7191
  1019
    return 0;
slouken@6690
  1020
}
slouken@6690
  1021
slouken@6690
  1022
slouken@6690
  1023
/*
slouken@6690
  1024
 * Get the current state of a button on a controller
slouken@6690
  1025
 */
slouken@6690
  1026
Uint8
icculus@6917
  1027
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@6690
  1028
{
slouken@7191
  1029
    if ( !gamecontroller )
slouken@7191
  1030
        return 0;
slouken@6690
  1031
slouken@7191
  1032
    if ( gamecontroller->mapping.buttons[button] >= 0 )
slouken@7191
  1033
    {
slouken@7191
  1034
        return ( SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttons[button] ) );
slouken@7191
  1035
    }
slouken@7191
  1036
    else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
slouken@7191
  1037
    {
slouken@7191
  1038
        Sint16 value;
slouken@7191
  1039
        value = SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button] );
slouken@7191
  1040
        if ( ABS(value) > 32768/2 )
slouken@7191
  1041
            return 1;
slouken@7191
  1042
        return 0;
slouken@7191
  1043
    }
slouken@7191
  1044
    else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
slouken@7191
  1045
    {
slouken@7191
  1046
        Uint8 value;
slouken@7191
  1047
        value = SDL_JoystickGetHat( gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat );
slouken@6690
  1048
slouken@7191
  1049
        if ( value & gamecontroller->mapping.hatasbutton[button].mask )
slouken@7191
  1050
            return 1;
slouken@7191
  1051
        return 0;
slouken@7191
  1052
    }
slouken@7191
  1053
slouken@7191
  1054
    return 0;
slouken@6690
  1055
}
slouken@6690
  1056
slouken@6690
  1057
/*
slouken@6690
  1058
 * Return if the joystick in question is currently attached to the system,
slouken@6690
  1059
 *  \return 0 if not plugged in, 1 if still present.
slouken@6690
  1060
 */
jorgen@6871
  1061
SDL_bool
slouken@6690
  1062
SDL_GameControllerGetAttached( SDL_GameController * gamecontroller )
slouken@6690
  1063
{
slouken@7191
  1064
    if ( !gamecontroller )
slouken@7191
  1065
        return SDL_FALSE;
slouken@6690
  1066
slouken@7191
  1067
    return SDL_JoystickGetAttached(gamecontroller->joystick);
slouken@6690
  1068
}
slouken@6690
  1069
slouken@6690
  1070
slouken@6690
  1071
/*
slouken@6690
  1072
 * Get the number of multi-dimensional axis controls on a joystick
slouken@6690
  1073
 */
slouken@6690
  1074
const char *
slouken@6690
  1075
SDL_GameControllerName(SDL_GameController * gamecontroller)
slouken@6690
  1076
{
slouken@7191
  1077
    if ( !gamecontroller )
slouken@7191
  1078
        return NULL;
slouken@6690
  1079
slouken@6690
  1080
    return (gamecontroller->mapping.name);
slouken@6690
  1081
}
slouken@6690
  1082
slouken@6690
  1083
slouken@6690
  1084
/*
slouken@6690
  1085
 * Get the joystick for this controller
slouken@6690
  1086
 */
slouken@6690
  1087
SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
slouken@6690
  1088
{
slouken@7191
  1089
    if ( !gamecontroller )
slouken@7191
  1090
        return NULL;
slouken@6690
  1091
slouken@7191
  1092
    return gamecontroller->joystick;
slouken@6690
  1093
}
slouken@6690
  1094
slouken@6690
  1095
/**
philipp@7133
  1096
 * Get the SDL joystick layer binding for this controller axis mapping
slouken@6690
  1097
 */
icculus@6917
  1098
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@6690
  1099
{
slouken@7191
  1100
    SDL_GameControllerButtonBind bind;
slouken@7191
  1101
    SDL_memset( &bind, 0x0, sizeof(bind) );
slouken@6690
  1102
slouken@7191
  1103
    if ( !gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID )
slouken@7191
  1104
        return bind;
slouken@6690
  1105
slouken@7191
  1106
    if (gamecontroller->mapping.axes[axis] >= 0 )
slouken@7191
  1107
    {
slouken@7191
  1108
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@7191
  1109
        bind.value.button = gamecontroller->mapping.axes[axis];
slouken@7191
  1110
    }
slouken@7191
  1111
    else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
slouken@7191
  1112
    {
slouken@7191
  1113
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@7191
  1114
        bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
slouken@7191
  1115
    }
slouken@6690
  1116
slouken@7191
  1117
    return bind;
slouken@6690
  1118
}
slouken@6690
  1119
slouken@6690
  1120
slouken@6690
  1121
/**
philipp@7133
  1122
 * Get the SDL joystick layer binding for this controller button mapping
slouken@6690
  1123
 */
icculus@6917
  1124
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@6690
  1125
{
slouken@7191
  1126
    SDL_GameControllerButtonBind bind;
slouken@7191
  1127
    SDL_memset( &bind, 0x0, sizeof(bind) );
slouken@6690
  1128
slouken@7191
  1129
    if ( !gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID )
slouken@7191
  1130
        return bind;
slouken@6690
  1131
slouken@7191
  1132
    if ( gamecontroller->mapping.buttons[button] >= 0 )
slouken@7191
  1133
    {
slouken@7191
  1134
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@7191
  1135
        bind.value.button = gamecontroller->mapping.buttons[button];
slouken@7191
  1136
    }
slouken@7191
  1137
    else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
slouken@7191
  1138
    {
slouken@7191
  1139
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@7191
  1140
        bind.value.axis = gamecontroller->mapping.axesasbutton[button];
slouken@7191
  1141
    }
slouken@7191
  1142
    else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
slouken@7191
  1143
    {
slouken@7191
  1144
        bind.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
slouken@7191
  1145
        bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
slouken@7191
  1146
        bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
slouken@7191
  1147
    }
slouken@6690
  1148
slouken@7191
  1149
    return bind;
slouken@6690
  1150
}
slouken@6690
  1151
slouken@6690
  1152
slouken@6690
  1153
/*
slouken@6690
  1154
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@6690
  1155
 */
slouken@6690
  1156
void
slouken@6690
  1157
SDL_GameControllerClose(SDL_GameController * gamecontroller)
slouken@6690
  1158
{
slouken@7191
  1159
    SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
slouken@6690
  1160
slouken@7191
  1161
    if ( !gamecontroller )
slouken@7191
  1162
        return;
slouken@6690
  1163
slouken@7191
  1164
    /* First decrement ref count */
slouken@6690
  1165
    if (--gamecontroller->ref_count > 0) {
slouken@6690
  1166
        return;
slouken@6690
  1167
    }
slouken@6690
  1168
slouken@7191
  1169
    SDL_JoystickClose( gamecontroller->joystick );
slouken@6690
  1170
slouken@7191
  1171
    gamecontrollerlist = SDL_gamecontrollers;
slouken@7191
  1172
    gamecontrollerlistprev = NULL;
slouken@7191
  1173
    while ( gamecontrollerlist )
slouken@7191
  1174
    {
slouken@7191
  1175
        if (gamecontroller == gamecontrollerlist)
slouken@7191
  1176
        {
slouken@7191
  1177
            if ( gamecontrollerlistprev )
slouken@7191
  1178
            {
slouken@7191
  1179
                /* unlink this entry */
slouken@7191
  1180
                gamecontrollerlistprev->next = gamecontrollerlist->next;
slouken@7191
  1181
            }
slouken@7191
  1182
            else
slouken@7191
  1183
            {
slouken@7191
  1184
                SDL_gamecontrollers = gamecontroller->next;
slouken@7191
  1185
            }
slouken@7191
  1186
slouken@7191
  1187
            break;
slouken@7191
  1188
        }
slouken@7191
  1189
        gamecontrollerlistprev = gamecontrollerlist;
slouken@7191
  1190
        gamecontrollerlist = gamecontrollerlist->next;
slouken@7191
  1191
    }
philipp@7129
  1192
slouken@6690
  1193
    SDL_free(gamecontroller);
slouken@6690
  1194
}
slouken@6690
  1195
slouken@6690
  1196
slouken@6690
  1197
/*
slouken@6690
  1198
 * Quit the controller subsystem
slouken@6690
  1199
 */
slouken@6690
  1200
void
slouken@6690
  1201
SDL_GameControllerQuit(void)
slouken@6690
  1202
{
slouken@7191
  1203
    ControllerMapping_t *pControllerMap;
slouken@7191
  1204
    while ( SDL_gamecontrollers )
slouken@7191
  1205
    {
slouken@7191
  1206
        SDL_gamecontrollers->ref_count = 1;
slouken@6690
  1207
        SDL_GameControllerClose(SDL_gamecontrollers);
slouken@7191
  1208
    }
slouken@6690
  1209
slouken@7191
  1210
    while ( s_pSupportedControllers )
slouken@7191
  1211
    {
slouken@7191
  1212
        pControllerMap = s_pSupportedControllers;
slouken@7191
  1213
        s_pSupportedControllers = s_pSupportedControllers->next;
slouken@7191
  1214
        SDL_free( pControllerMap->name );
slouken@7191
  1215
        SDL_free( pControllerMap );
slouken@7191
  1216
    }
slouken@6690
  1217
slouken@7191
  1218
    SDL_DelEventWatch( SDL_GameControllerEventWatcher, NULL );
slouken@6690
  1219
slouken@6690
  1220
}
slouken@6690
  1221
slouken@6690
  1222
/*
slouken@6690
  1223
 * Event filter to transform joystick events into appropriate game controller ones
slouken@6690
  1224
 */
slouken@6690
  1225
int
icculus@6917
  1226
SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
slouken@6690
  1227
{
slouken@7191
  1228
    int posted;
slouken@6690
  1229
slouken@6690
  1230
    /* translate the event, if desired */
slouken@6690
  1231
    posted = 0;
slouken@6690
  1232
#if !SDL_EVENTS_DISABLED
slouken@6690
  1233
    if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
slouken@6690
  1234
        SDL_Event event;
slouken@6690
  1235
        event.type = SDL_CONTROLLERAXISMOTION;
slouken@6690
  1236
        event.caxis.which = gamecontroller->joystick->instance_id;
slouken@6690
  1237
        event.caxis.axis = axis;
slouken@6690
  1238
        event.caxis.value = value;
slouken@7191
  1239
        posted = SDL_PushEvent(&event) == 1;
slouken@6690
  1240
    }
slouken@6690
  1241
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1242
    return (posted);
slouken@6690
  1243
}
slouken@6690
  1244
slouken@6690
  1245
slouken@6690
  1246
/*
slouken@6690
  1247
 * Event filter to transform joystick events into appropriate game controller ones
slouken@6690
  1248
 */
slouken@6690
  1249
int
icculus@6917
  1250
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
slouken@6690
  1251
{
slouken@6690
  1252
    int posted;
slouken@6690
  1253
#if !SDL_EVENTS_DISABLED
slouken@7191
  1254
    SDL_Event event;
slouken@6690
  1255
slouken@6854
  1256
    if ( button == SDL_CONTROLLER_BUTTON_INVALID )
slouken@6854
  1257
        return (0);
slouken@6854
  1258
slouken@6690
  1259
    switch (state) {
slouken@6690
  1260
    case SDL_PRESSED:
slouken@6690
  1261
        event.type = SDL_CONTROLLERBUTTONDOWN;
slouken@6690
  1262
        break;
slouken@6690
  1263
    case SDL_RELEASED:
slouken@6690
  1264
        event.type = SDL_CONTROLLERBUTTONUP;
slouken@6690
  1265
        break;
slouken@6690
  1266
    default:
slouken@6690
  1267
        /* Invalid state -- bail */
slouken@6690
  1268
        return (0);
slouken@6690
  1269
    }
slouken@6690
  1270
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1271
slouken@6690
  1272
    /* translate the event, if desired */
slouken@6690
  1273
    posted = 0;
slouken@6690
  1274
#if !SDL_EVENTS_DISABLED
slouken@6690
  1275
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
  1276
        event.cbutton.which = gamecontroller->joystick->instance_id;
slouken@6690
  1277
        event.cbutton.button = button;
slouken@6690
  1278
        event.cbutton.state = state;
slouken@7191
  1279
        posted = SDL_PushEvent(&event) == 1;
slouken@6690
  1280
    }
slouken@6690
  1281
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1282
    return (posted);
slouken@6690
  1283
}
slouken@6690
  1284
slouken@6690
  1285
/*
slouken@6690
  1286
 * Turn off controller events
slouken@6690
  1287
 */
slouken@6690
  1288
int
slouken@6690
  1289
SDL_GameControllerEventState(int state)
slouken@6690
  1290
{
slouken@6690
  1291
#if SDL_EVENTS_DISABLED
slouken@6690
  1292
    return SDL_IGNORE;
slouken@6690
  1293
#else
slouken@6690
  1294
    const Uint32 event_list[] = {
slouken@6690
  1295
        SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
urkle@6964
  1296
        SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
slouken@6690
  1297
    };
slouken@6690
  1298
    unsigned int i;
slouken@6690
  1299
slouken@6690
  1300
    switch (state) {
slouken@6690
  1301
    case SDL_QUERY:
slouken@6690
  1302
        state = SDL_IGNORE;
slouken@6690
  1303
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@6690
  1304
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@6690
  1305
            if (state == SDL_ENABLE) {
slouken@6690
  1306
                break;
slouken@6690
  1307
            }
slouken@6690
  1308
        }
slouken@6690
  1309
        break;
slouken@6690
  1310
    default:
slouken@6690
  1311
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@6690
  1312
            SDL_EventState(event_list[i], state);
slouken@6690
  1313
        }
slouken@6690
  1314
        break;
slouken@6690
  1315
    }
slouken@6690
  1316
    return (state);
slouken@6690
  1317
#endif /* SDL_EVENTS_DISABLED */
slouken@6690
  1318
}
slouken@6690
  1319
slouken@6690
  1320
/* vi: set ts=4 sw=4 expandtab: */