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