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