src/joystick/SDL_gamecontroller.c
author Ryan C. Gordon <icculus@icculus.org>
Sun, 24 Nov 2013 23:56:17 -0500
changeset 8093 b43765095a6f
parent 8081 402b8c313246
child 8149 681eb46b8ac4
permissions -rw-r--r--
Make internal SDL sources include SDL_internal.h instead of SDL_config.h

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