src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 09 Aug 2018 16:00:17 -0700
changeset 12088 399cc39583cc
parent 11998 76124cc14cbb
child 12174 8be6b6e98c3c
permissions -rw-r--r--
Added HIDAPI joystick drivers for more consistent support for Xbox, PS4 and Nintendo Switch Pro controller support across platforms.
Added SDL_GameControllerRumble() and SDL_JoystickRumble() for simple force feedback outside of the SDL haptics API
slouken@8582
     1
/*
slouken@8582
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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@11581
    27
#include "SDL_hints.h"
slouken@11934
    28
#include "SDL_timer.h"
slouken@8582
    29
#include "SDL_sysjoystick.h"
slouken@10688
    30
#include "SDL_joystick_c.h"
slouken@8582
    31
#include "SDL_gamecontrollerdb.h"
slouken@8582
    32
slouken@8582
    33
#if !SDL_EVENTS_DISABLED
slouken@8582
    34
#include "../events/SDL_events_c.h"
slouken@8582
    35
#endif
slouken@8582
    36
slouken@11532
    37
#if defined(__ANDROID__)
slouken@11532
    38
#include "SDL_system.h"
slouken@11532
    39
#endif
slouken@11532
    40
slouken@11532
    41
slouken@11934
    42
/* Many controllers turn the center button into an instantaneous button press */
slouken@11983
    43
#define SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS   250
slouken@11934
    44
gabomdq@8042
    45
#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
slouken@8582
    46
slouken@8582
    47
/* a list of currently opened game controllers */
slouken@8582
    48
static SDL_GameController *SDL_gamecontrollers = NULL;
slouken@8582
    49
slouken@10724
    50
typedef struct
slouken@8582
    51
{
slouken@10724
    52
    SDL_GameControllerBindType inputType;
slouken@10724
    53
    union
slouken@10724
    54
    {
slouken@10724
    55
        int button;
slouken@8582
    56
slouken@10724
    57
        struct {
slouken@10724
    58
            int axis;
slouken@10724
    59
            int axis_min;
slouken@10724
    60
            int axis_max;
slouken@10724
    61
        } axis;
slouken@8582
    62
slouken@10724
    63
        struct {
slouken@10724
    64
            int hat;
slouken@10724
    65
            int hat_mask;
slouken@10724
    66
        } hat;
slouken@8582
    67
slouken@10724
    68
    } input;
slouken@8582
    69
slouken@10724
    70
    SDL_GameControllerBindType outputType;
slouken@10724
    71
    union
slouken@10724
    72
    {
slouken@10724
    73
        SDL_GameControllerButton button;
slouken@8582
    74
slouken@10724
    75
        struct {
slouken@10724
    76
            SDL_GameControllerAxis axis;
slouken@10724
    77
            int axis_min;
slouken@10724
    78
            int axis_max;
slouken@10724
    79
        } axis;
slouken@8582
    80
slouken@10724
    81
    } output;
slouken@8582
    82
slouken@10724
    83
} SDL_ExtendedGameControllerBind;
slouken@8582
    84
slouken@8582
    85
/* our hard coded list of mapping support */
slouken@10605
    86
typedef enum
slouken@10605
    87
{
slouken@10605
    88
    SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT,
slouken@10605
    89
    SDL_CONTROLLER_MAPPING_PRIORITY_API,
slouken@10605
    90
    SDL_CONTROLLER_MAPPING_PRIORITY_USER,
slouken@10605
    91
} SDL_ControllerMappingPriority;
slouken@10605
    92
slouken@8582
    93
typedef struct _ControllerMapping_t
slouken@8582
    94
{
slouken@8582
    95
    SDL_JoystickGUID guid;
slouken@8582
    96
    char *name;
slouken@8582
    97
    char *mapping;
slouken@10605
    98
    SDL_ControllerMappingPriority priority;
slouken@8582
    99
    struct _ControllerMapping_t *next;
slouken@8582
   100
} ControllerMapping_t;
slouken@8582
   101
slouken@10661
   102
static SDL_JoystickGUID s_zeroGUID;
slouken@8582
   103
static ControllerMapping_t *s_pSupportedControllers = NULL;
slouken@11921
   104
static ControllerMapping_t *s_pDefaultMapping = NULL;
slouken@12088
   105
static ControllerMapping_t *s_pHIDAPIMapping = NULL;
slouken@8582
   106
static ControllerMapping_t *s_pXInputMapping = NULL;
slouken@8582
   107
slouken@8582
   108
/* The SDL game controller structure */
slouken@8582
   109
struct _SDL_GameController
slouken@8582
   110
{
slouken@8582
   111
    SDL_Joystick *joystick; /* underlying joystick device */
slouken@8582
   112
    int ref_count;
slouken@10724
   113
slouken@10724
   114
    SDL_JoystickGUID guid;
slouken@10724
   115
    const char *name;
slouken@10724
   116
    int num_bindings;
slouken@10724
   117
    SDL_ExtendedGameControllerBind *bindings;
slouken@10724
   118
    SDL_ExtendedGameControllerBind **last_match_axis;
slouken@10724
   119
    Uint8 *last_hat_mask;
slouken@11934
   120
    Uint32 guide_button_down;
slouken@10724
   121
slouken@8582
   122
    struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
slouken@8582
   123
};
slouken@8582
   124
slouken@8582
   125
slouken@11201
   126
typedef struct
slouken@11201
   127
{
slouken@11201
   128
    int num_entries;
slouken@11201
   129
    int max_entries;
slouken@11201
   130
    Uint32 *entries;
slouken@11201
   131
} SDL_vidpid_list;
slouken@11201
   132
slouken@11201
   133
static SDL_vidpid_list SDL_allowed_controllers;
slouken@11201
   134
static SDL_vidpid_list SDL_ignored_controllers;
slouken@11201
   135
slouken@11201
   136
static void
slouken@11201
   137
SDL_LoadVIDPIDListFromHint(const char *hint, SDL_vidpid_list *list)
slouken@11201
   138
{
slouken@11201
   139
    Uint32 entry;
slouken@11201
   140
    char *spot;
slouken@11201
   141
    char *file = NULL;
slouken@11201
   142
slouken@11201
   143
    list->num_entries = 0;
slouken@11201
   144
slouken@11201
   145
    if (hint && *hint == '@') {
slouken@11201
   146
        spot = file = (char *)SDL_LoadFile(hint+1, NULL);
slouken@11201
   147
    } else {
slouken@11201
   148
        spot = (char *)hint;
slouken@11201
   149
    }
slouken@11201
   150
slouken@11201
   151
    if (!spot) {
slouken@11201
   152
        return;
slouken@11201
   153
    }
slouken@11201
   154
slouken@11201
   155
    while ((spot = SDL_strstr(spot, "0x")) != NULL) {
slouken@11236
   156
        entry = (Uint16)SDL_strtol(spot, &spot, 0);
slouken@11201
   157
        entry <<= 16;
slouken@11201
   158
        spot = SDL_strstr(spot, "0x");
slouken@11201
   159
        if (!spot) {
slouken@11201
   160
            break;
slouken@11201
   161
        }
slouken@11236
   162
        entry |= (Uint16)SDL_strtol(spot, &spot, 0);
slouken@11201
   163
slouken@11201
   164
        if (list->num_entries == list->max_entries) {
slouken@11201
   165
            int max_entries = list->max_entries + 16;
slouken@11201
   166
            Uint32 *entries = (Uint32 *)SDL_realloc(list->entries, max_entries*sizeof(*list->entries));
slouken@11201
   167
            if (entries == NULL) {
slouken@11201
   168
                /* Out of memory, go with what we have already */
slouken@11201
   169
                break;
slouken@11201
   170
            }
slouken@11201
   171
            list->entries = entries;
slouken@11201
   172
            list->max_entries = max_entries;
slouken@11201
   173
        }
slouken@11201
   174
        list->entries[list->num_entries++] = entry;
slouken@11201
   175
    }
slouken@11201
   176
slouken@11201
   177
    if (file) {
slouken@11201
   178
        SDL_free(file);
slouken@11201
   179
    }
slouken@11201
   180
}
slouken@11201
   181
slouken@11284
   182
static void SDLCALL
slouken@11201
   183
SDL_GameControllerIgnoreDevicesChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@11201
   184
{
slouken@11201
   185
    SDL_LoadVIDPIDListFromHint(hint, &SDL_ignored_controllers);
slouken@11201
   186
}
slouken@11201
   187
slouken@11284
   188
static void SDLCALL
slouken@11201
   189
SDL_GameControllerIgnoreDevicesExceptChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
slouken@11201
   190
{
slouken@11201
   191
    SDL_LoadVIDPIDListFromHint(hint, &SDL_allowed_controllers);
slouken@11201
   192
}
slouken@11201
   193
slouken@10724
   194
static int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
slouken@10724
   195
static int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
slouken@8582
   196
slouken@8582
   197
/*
slouken@10226
   198
 * If there is an existing add event in the queue, it needs to be modified
slouken@10226
   199
 * to have the right value for which, because the number of controllers in
slouken@10226
   200
 * the system is now one less.
slouken@10226
   201
 */
slouken@10226
   202
static void UpdateEventsForDeviceRemoval()
slouken@10226
   203
{
slouken@10226
   204
    int i, num_events;
slouken@10226
   205
    SDL_Event *events;
slouken@10226
   206
slouken@10226
   207
    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
slouken@10226
   208
    if (num_events <= 0) {
slouken@10226
   209
        return;
slouken@10226
   210
    }
slouken@10226
   211
slouken@10226
   212
    events = SDL_stack_alloc(SDL_Event, num_events);
slouken@10226
   213
    if (!events) {
slouken@10226
   214
        return;
slouken@10226
   215
    }
slouken@10226
   216
slouken@10226
   217
    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
slouken@10226
   218
    for (i = 0; i < num_events; ++i) {
slouken@10226
   219
        --events[i].cdevice.which;
slouken@10226
   220
    }
slouken@10226
   221
    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
slouken@10226
   222
slouken@10226
   223
    SDL_stack_free(events);
slouken@10226
   224
}
slouken@10226
   225
slouken@10724
   226
static SDL_bool HasSameOutput(SDL_ExtendedGameControllerBind *a, SDL_ExtendedGameControllerBind *b)
slouken@10724
   227
{
slouken@10724
   228
    if (a->outputType != b->outputType) {
slouken@10724
   229
        return SDL_FALSE;
slouken@10724
   230
    }
slouken@10724
   231
slouken@10724
   232
    if (a->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
   233
        return (a->output.axis.axis == b->output.axis.axis);
slouken@10724
   234
    } else {
slouken@10724
   235
        return (a->output.button == b->output.button);
slouken@10724
   236
    }
slouken@10724
   237
}
slouken@10724
   238
slouken@10724
   239
static void ResetOutput(SDL_GameController *gamecontroller, SDL_ExtendedGameControllerBind *bind)
slouken@10724
   240
{
slouken@10724
   241
    if (bind->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
   242
        SDL_PrivateGameControllerAxis(gamecontroller, bind->output.axis.axis, 0);
slouken@10724
   243
    } else {
slouken@10724
   244
        SDL_PrivateGameControllerButton(gamecontroller, bind->output.button, SDL_RELEASED);
slouken@10724
   245
    }
slouken@10724
   246
}
slouken@10724
   247
slouken@10724
   248
static void HandleJoystickAxis(SDL_GameController *gamecontroller, int axis, int value)
slouken@10724
   249
{
slouken@10724
   250
    int i;
slouken@10724
   251
    SDL_ExtendedGameControllerBind *last_match = gamecontroller->last_match_axis[axis];
slouken@10724
   252
    SDL_ExtendedGameControllerBind *match = NULL;
slouken@10724
   253
slouken@10724
   254
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
   255
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
   256
        if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
slouken@10724
   257
            axis == binding->input.axis.axis) {
slouken@10724
   258
            if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
slouken@10724
   259
                if (value >= binding->input.axis.axis_min &&
slouken@10724
   260
                    value <= binding->input.axis.axis_max) {
slouken@10724
   261
                    match = binding;
slouken@10724
   262
                    break;
slouken@10724
   263
                }
slouken@10724
   264
            } else {
slouken@10724
   265
                if (value >= binding->input.axis.axis_max &&
slouken@10724
   266
                    value <= binding->input.axis.axis_min) {
slouken@10724
   267
                    match = binding;
slouken@10724
   268
                    break;
slouken@10724
   269
                }
slouken@10724
   270
            }
slouken@10724
   271
        }
slouken@10724
   272
    }
slouken@10724
   273
slouken@10724
   274
    if (last_match && (!match || !HasSameOutput(last_match, match))) {
slouken@10724
   275
        /* Clear the last input that this axis generated */
slouken@10724
   276
        ResetOutput(gamecontroller, last_match);
slouken@10724
   277
    }
slouken@10724
   278
slouken@10724
   279
    if (match) {
slouken@10724
   280
        if (match->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
   281
            if (match->input.axis.axis_min != match->output.axis.axis_min || match->input.axis.axis_max != match->output.axis.axis_max) {
slouken@10724
   282
                float normalized_value = (float)(value - match->input.axis.axis_min) / (match->input.axis.axis_max - match->input.axis.axis_min);
slouken@10724
   283
                value = match->output.axis.axis_min + (int)(normalized_value * (match->output.axis.axis_max - match->output.axis.axis_min));
slouken@10724
   284
            }
slouken@10724
   285
            SDL_PrivateGameControllerAxis(gamecontroller, match->output.axis.axis, (Sint16)value);
slouken@10724
   286
        } else {
slouken@10724
   287
            Uint8 state;
slouken@10724
   288
            int threshold = match->input.axis.axis_min + (match->input.axis.axis_max - match->input.axis.axis_min) / 2;
slouken@10724
   289
            if (match->input.axis.axis_max < match->input.axis.axis_min) {
slouken@10724
   290
                state = (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
   291
            } else {
slouken@10724
   292
                state = (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
   293
            }
slouken@10724
   294
            SDL_PrivateGameControllerButton(gamecontroller, match->output.button, state);
slouken@10724
   295
        }
slouken@10724
   296
    }
slouken@10724
   297
    gamecontroller->last_match_axis[axis] = match;
slouken@10724
   298
}
slouken@10724
   299
slouken@10724
   300
static void HandleJoystickButton(SDL_GameController *gamecontroller, int button, Uint8 state)
slouken@10724
   301
{
slouken@10724
   302
    int i;
slouken@10724
   303
slouken@10724
   304
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
   305
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
   306
        if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON &&
slouken@10724
   307
            button == binding->input.button) {
slouken@10724
   308
            if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
   309
                int value = state ? binding->output.axis.axis_max : binding->output.axis.axis_min;
slouken@10724
   310
                SDL_PrivateGameControllerAxis(gamecontroller, binding->output.axis.axis, (Sint16)value);
slouken@10724
   311
            } else {
slouken@10724
   312
                SDL_PrivateGameControllerButton(gamecontroller, binding->output.button, state);
slouken@10724
   313
            }
slouken@10724
   314
            break;
slouken@10724
   315
        }
slouken@10724
   316
    }
slouken@10724
   317
}
slouken@10724
   318
slouken@10724
   319
static void HandleJoystickHat(SDL_GameController *gamecontroller, int hat, Uint8 value)
slouken@10724
   320
{
slouken@10724
   321
    int i;
slouken@10724
   322
    Uint8 last_mask = gamecontroller->last_hat_mask[hat];
slouken@10724
   323
    Uint8 changed_mask = (last_mask ^ value);
slouken@10724
   324
slouken@10724
   325
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
   326
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
   327
        if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT && hat == binding->input.hat.hat) {
slouken@10724
   328
            if ((changed_mask & binding->input.hat.hat_mask) != 0) {
slouken@10724
   329
                if (value & binding->input.hat.hat_mask) {
slouken@10724
   330
                    if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
   331
                        SDL_PrivateGameControllerAxis(gamecontroller, binding->output.axis.axis, (Sint16)binding->output.axis.axis_max);
slouken@10724
   332
                    } else {
slouken@10724
   333
                        SDL_PrivateGameControllerButton(gamecontroller, binding->output.button, SDL_PRESSED);
slouken@10724
   334
                    }
slouken@10724
   335
                } else {
slouken@10724
   336
                    ResetOutput(gamecontroller, binding);
slouken@10724
   337
                }
slouken@10724
   338
            }
slouken@10724
   339
        }
slouken@10724
   340
    }
slouken@10724
   341
    gamecontroller->last_hat_mask[hat] = value;
slouken@10724
   342
}
slouken@10724
   343
slouken@10226
   344
/*
slouken@8582
   345
 * Event filter to fire controller events from joystick ones
slouken@8582
   346
 */
slouken@11272
   347
static int SDLCALL SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
slouken@8582
   348
{
slouken@8772
   349
    switch(event->type) {
slouken@8582
   350
    case SDL_JOYAXISMOTION:
slouken@8582
   351
        {
slouken@10724
   352
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@8772
   353
            while (controllerlist) {
slouken@8772
   354
                if (controllerlist->joystick->instance_id == event->jaxis.which) {
slouken@10724
   355
                    HandleJoystickAxis(controllerlist, event->jaxis.axis, event->jaxis.value);
slouken@8582
   356
                    break;
slouken@8582
   357
                }
slouken@8582
   358
                controllerlist = controllerlist->next;
slouken@8582
   359
            }
slouken@8582
   360
        }
slouken@8582
   361
        break;
slouken@8582
   362
    case SDL_JOYBUTTONDOWN:
slouken@8582
   363
    case SDL_JOYBUTTONUP:
slouken@8582
   364
        {
slouken@10724
   365
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@8772
   366
            while (controllerlist) {
slouken@8772
   367
                if (controllerlist->joystick->instance_id == event->jbutton.which) {
slouken@10724
   368
                    HandleJoystickButton(controllerlist, event->jbutton.button, event->jbutton.state);
slouken@8582
   369
                    break;
slouken@8582
   370
                }
slouken@8582
   371
                controllerlist = controllerlist->next;
slouken@8582
   372
            }
slouken@8582
   373
        }
slouken@8582
   374
        break;
slouken@8582
   375
    case SDL_JOYHATMOTION:
slouken@8582
   376
        {
slouken@10724
   377
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@8772
   378
            while (controllerlist) {
slouken@8772
   379
                if (controllerlist->joystick->instance_id == event->jhat.which) {
slouken@10724
   380
                    HandleJoystickHat(controllerlist, event->jhat.hat, event->jhat.value);
slouken@8582
   381
                    break;
slouken@8582
   382
                }
slouken@8582
   383
                controllerlist = controllerlist->next;
slouken@8582
   384
            }
slouken@8582
   385
        }
slouken@8582
   386
        break;
slouken@8582
   387
    case SDL_JOYDEVICEADDED:
slouken@8582
   388
        {
slouken@8772
   389
            if (SDL_IsGameController(event->jdevice.which)) {
slouken@8582
   390
                SDL_Event deviceevent;
slouken@8582
   391
                deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@8582
   392
                deviceevent.cdevice.which = event->jdevice.which;
slouken@8582
   393
                SDL_PushEvent(&deviceevent);
slouken@8582
   394
            }
slouken@8582
   395
        }
slouken@8582
   396
        break;
slouken@8582
   397
    case SDL_JOYDEVICEREMOVED:
slouken@8582
   398
        {
slouken@8582
   399
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@8772
   400
            while (controllerlist) {
slouken@8772
   401
                if (controllerlist->joystick->instance_id == event->jdevice.which) {
slouken@8582
   402
                    SDL_Event deviceevent;
slouken@10225
   403
slouken@8582
   404
                    deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
slouken@8582
   405
                    deviceevent.cdevice.which = event->jdevice.which;
slouken@8582
   406
                    SDL_PushEvent(&deviceevent);
slouken@10226
   407
slouken@10226
   408
                    UpdateEventsForDeviceRemoval();
slouken@8582
   409
                    break;
slouken@8582
   410
                }
slouken@8582
   411
                controllerlist = controllerlist->next;
slouken@8582
   412
            }
slouken@8582
   413
        }
slouken@8582
   414
        break;
slouken@8582
   415
    default:
slouken@8582
   416
        break;
slouken@8582
   417
    }
slouken@8582
   418
slouken@8582
   419
    return 1;
slouken@8582
   420
}
slouken@8582
   421
slouken@8582
   422
/*
slouken@8582
   423
 * Helper function to scan the mappings database for a controller with the specified GUID
slouken@8582
   424
 */
slouken@10609
   425
static ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
slouken@8582
   426
{
slouken@8582
   427
    ControllerMapping_t *pSupportedController = s_pSupportedControllers;
slouken@8772
   428
    while (pSupportedController) {
slouken@8772
   429
        if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
slouken@8582
   430
            return pSupportedController;
slouken@8582
   431
        }
slouken@8582
   432
        pSupportedController = pSupportedController->next;
slouken@8582
   433
    }
slouken@12088
   434
    if (guid->data[14] == 'h') {
slouken@12088
   435
        /* This is a HIDAPI device */
slouken@12088
   436
        return s_pHIDAPIMapping;
slouken@12088
   437
    }
slouken@11922
   438
#if SDL_JOYSTICK_XINPUT
slouken@11922
   439
    if (guid->data[14] == 'x') {
slouken@11922
   440
        /* This is an XInput device */
slouken@11922
   441
        return s_pXInputMapping;
slouken@11922
   442
    }
slouken@11922
   443
#endif
slouken@8582
   444
    return NULL;
slouken@8582
   445
}
slouken@8582
   446
slouken@8582
   447
static const char* map_StringForControllerAxis[] = {
slouken@8582
   448
    "leftx",
slouken@8582
   449
    "lefty",
slouken@8582
   450
    "rightx",
slouken@8582
   451
    "righty",
slouken@8582
   452
    "lefttrigger",
slouken@8582
   453
    "righttrigger",
slouken@8582
   454
    NULL
slouken@8582
   455
};
slouken@8582
   456
slouken@8582
   457
/*
slouken@8582
   458
 * convert a string to its enum equivalent
slouken@8582
   459
 */
slouken@8772
   460
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *pchString)
slouken@8582
   461
{
slouken@8582
   462
    int entry;
slouken@10724
   463
slouken@10724
   464
    if (pchString && (*pchString == '+' || *pchString == '-')) {
slouken@10724
   465
        ++pchString;
slouken@10724
   466
    }
slouken@10724
   467
slouken@10724
   468
    if (!pchString || !pchString[0]) {
slouken@8582
   469
        return SDL_CONTROLLER_AXIS_INVALID;
slouken@10724
   470
    }
slouken@8582
   471
slouken@8772
   472
    for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
slouken@8772
   473
        if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
icculus@10926
   474
            return (SDL_GameControllerAxis) entry;
slouken@8582
   475
    }
slouken@8582
   476
    return SDL_CONTROLLER_AXIS_INVALID;
slouken@8582
   477
}
slouken@8582
   478
slouken@8582
   479
/*
slouken@8582
   480
 * convert an enum to its string equivalent
slouken@8582
   481
 */
slouken@8772
   482
const char* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
slouken@8582
   483
{
slouken@8772
   484
    if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
slouken@8582
   485
        return map_StringForControllerAxis[axis];
slouken@8582
   486
    }
slouken@8582
   487
    return NULL;
slouken@8582
   488
}
slouken@8582
   489
slouken@8582
   490
static const char* map_StringForControllerButton[] = {
slouken@8582
   491
    "a",
slouken@8582
   492
    "b",
slouken@8582
   493
    "x",
slouken@8582
   494
    "y",
slouken@8582
   495
    "back",
slouken@8582
   496
    "guide",
slouken@8582
   497
    "start",
slouken@8582
   498
    "leftstick",
slouken@8582
   499
    "rightstick",
slouken@8582
   500
    "leftshoulder",
slouken@8582
   501
    "rightshoulder",
slouken@8582
   502
    "dpup",
slouken@8582
   503
    "dpdown",
slouken@8582
   504
    "dpleft",
slouken@8582
   505
    "dpright",
slouken@8582
   506
    NULL
slouken@8582
   507
};
slouken@8582
   508
slouken@8582
   509
/*
slouken@8582
   510
 * convert a string to its enum equivalent
slouken@8582
   511
 */
slouken@8772
   512
SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *pchString)
slouken@8582
   513
{
slouken@8582
   514
    int entry;
slouken@8772
   515
    if (!pchString || !pchString[0])
slouken@8582
   516
        return SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   517
slouken@8772
   518
    for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
slouken@8772
   519
        if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
icculus@10926
   520
            return (SDL_GameControllerButton) entry;
slouken@8582
   521
    }
slouken@8582
   522
    return SDL_CONTROLLER_BUTTON_INVALID;
slouken@8582
   523
}
slouken@8582
   524
slouken@8582
   525
/*
slouken@8582
   526
 * convert an enum to its string equivalent
slouken@8582
   527
 */
slouken@8772
   528
const char* SDL_GameControllerGetStringForButton(SDL_GameControllerButton axis)
slouken@8582
   529
{
slouken@8772
   530
    if (axis > SDL_CONTROLLER_BUTTON_INVALID && axis < SDL_CONTROLLER_BUTTON_MAX) {
slouken@8582
   531
        return map_StringForControllerButton[axis];
slouken@8582
   532
    }
slouken@8582
   533
    return NULL;
slouken@8582
   534
}
slouken@8582
   535
slouken@8582
   536
/*
slouken@8582
   537
 * given a controller button name and a joystick name update our mapping structure with it
slouken@8582
   538
 */
slouken@10724
   539
static void SDL_PrivateGameControllerParseElement(SDL_GameController *gamecontroller, const char *szGameButton, const char *szJoystickButton)
slouken@8582
   540
{
slouken@10724
   541
    SDL_ExtendedGameControllerBind bind;
slouken@8582
   542
    SDL_GameControllerButton button;
slouken@8582
   543
    SDL_GameControllerAxis axis;
slouken@10724
   544
    SDL_bool invert_input = SDL_FALSE;
slouken@10724
   545
    char half_axis_input = 0;
slouken@10724
   546
    char half_axis_output = 0;
slouken@10724
   547
slouken@10724
   548
    if (*szGameButton == '+' || *szGameButton == '-') {
slouken@10724
   549
        half_axis_output = *szGameButton++;
slouken@10724
   550
    }
slouken@10724
   551
slouken@10724
   552
    axis = SDL_GameControllerGetAxisFromString(szGameButton);
slouken@8772
   553
    button = SDL_GameControllerGetButtonFromString(szGameButton);
slouken@10724
   554
    if (axis != SDL_CONTROLLER_AXIS_INVALID) {
slouken@10724
   555
        bind.outputType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@10724
   556
        bind.output.axis.axis = axis;
slouken@10724
   557
        if (axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT || axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT) {
slouken@10724
   558
            bind.output.axis.axis_min = 0;
slouken@10724
   559
            bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
slouken@10724
   560
        } else {
slouken@10724
   561
            if (half_axis_output == '+') {
slouken@10724
   562
                bind.output.axis.axis_min = 0;
slouken@10724
   563
                bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
slouken@10724
   564
            } else if (half_axis_output == '-') {
slouken@10724
   565
                bind.output.axis.axis_min = 0;
slouken@10724
   566
                bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
slouken@10724
   567
            } else {
slouken@10724
   568
                bind.output.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
slouken@10724
   569
                bind.output.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
slouken@10724
   570
            }
slouken@10724
   571
        }
slouken@10724
   572
    } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
slouken@10724
   573
        bind.outputType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@10724
   574
        bind.output.button = button;
slouken@10724
   575
    } else {
slouken@10724
   576
        SDL_SetError("Unexpected controller element %s", szGameButton);
slouken@10724
   577
        return;
slouken@10724
   578
    }
slouken@8582
   579
slouken@10724
   580
    if (*szJoystickButton == '+' || *szJoystickButton == '-') {
slouken@10724
   581
        half_axis_input = *szJoystickButton++;
slouken@10724
   582
    }
slouken@10724
   583
    if (szJoystickButton[SDL_strlen(szJoystickButton) - 1] == '~') {
slouken@10724
   584
        invert_input = SDL_TRUE;
slouken@10724
   585
    }
slouken@10724
   586
slouken@10724
   587
    if (szJoystickButton[0] == 'a' && SDL_isdigit(szJoystickButton[1])) {
slouken@10724
   588
        bind.inputType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@10724
   589
        bind.input.axis.axis = SDL_atoi(&szJoystickButton[1]);
slouken@10724
   590
        if (half_axis_input == '+') {
slouken@10724
   591
            bind.input.axis.axis_min = 0;
slouken@10724
   592
            bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
slouken@10724
   593
        } else if (half_axis_input == '-') {
slouken@10724
   594
            bind.input.axis.axis_min = 0;
slouken@10724
   595
            bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MIN;
slouken@10724
   596
        } else {
slouken@10724
   597
            bind.input.axis.axis_min = SDL_JOYSTICK_AXIS_MIN;
slouken@10724
   598
            bind.input.axis.axis_max = SDL_JOYSTICK_AXIS_MAX;
slouken@8582
   599
        }
slouken@10724
   600
        if (invert_input) {
slouken@10724
   601
            int tmp = bind.input.axis.axis_min;
slouken@10724
   602
            bind.input.axis.axis_min = bind.input.axis.axis_max;
slouken@10724
   603
            bind.input.axis.axis_max = tmp;
slouken@8582
   604
        }
slouken@10724
   605
    } else if (szJoystickButton[0] == 'b' && SDL_isdigit(szJoystickButton[1])) {
slouken@10724
   606
        bind.inputType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@10724
   607
        bind.input.button = SDL_atoi(&szJoystickButton[1]);
slouken@10724
   608
    } else if (szJoystickButton[0] == 'h' && SDL_isdigit(szJoystickButton[1]) &&
slouken@10724
   609
               szJoystickButton[2] == '.' && SDL_isdigit(szJoystickButton[3])) {
slouken@8772
   610
        int hat = SDL_atoi(&szJoystickButton[1]);
slouken@8772
   611
        int mask = SDL_atoi(&szJoystickButton[3]);
slouken@10724
   612
        bind.inputType = SDL_CONTROLLER_BINDTYPE_HAT;
slouken@10724
   613
        bind.input.hat.hat = hat;
slouken@10724
   614
        bind.input.hat.hat_mask = mask;
slouken@10724
   615
    } else {
slouken@10724
   616
        SDL_SetError("Unexpected joystick element: %s", szJoystickButton);
slouken@10724
   617
        return;
slouken@10724
   618
    }
slouken@8582
   619
slouken@10724
   620
    ++gamecontroller->num_bindings;
slouken@10724
   621
    gamecontroller->bindings = (SDL_ExtendedGameControllerBind *)SDL_realloc(gamecontroller->bindings, gamecontroller->num_bindings * sizeof(*gamecontroller->bindings));
slouken@10724
   622
    if (!gamecontroller->bindings) {
slouken@10724
   623
        gamecontroller->num_bindings = 0;
slouken@10724
   624
        SDL_OutOfMemory();
slouken@10724
   625
        return;
slouken@8582
   626
    }
slouken@10724
   627
    gamecontroller->bindings[gamecontroller->num_bindings - 1] = bind;
slouken@8582
   628
}
slouken@8582
   629
slouken@8582
   630
slouken@8582
   631
/*
slouken@8582
   632
 * given a controller mapping string update our mapping object
slouken@8582
   633
 */
slouken@8582
   634
static void
slouken@10724
   635
SDL_PrivateGameControllerParseControllerConfigString(SDL_GameController *gamecontroller, const char *pchString)
slouken@8582
   636
{
slouken@8582
   637
    char szGameButton[20];
slouken@8582
   638
    char szJoystickButton[20];
slouken@8582
   639
    SDL_bool bGameButton = SDL_TRUE;
slouken@8582
   640
    int i = 0;
slouken@8582
   641
    const char *pchPos = pchString;
slouken@8582
   642
slouken@10724
   643
    SDL_zero(szGameButton);
slouken@10724
   644
    SDL_zero(szJoystickButton);
slouken@8582
   645
slouken@8772
   646
    while (pchPos && *pchPos) {
slouken@8772
   647
        if (*pchPos == ':') {
slouken@8582
   648
            i = 0;
slouken@8582
   649
            bGameButton = SDL_FALSE;
slouken@8772
   650
        } else if (*pchPos == ' ') {
slouken@8582
   651
slouken@8772
   652
        } else if (*pchPos == ',') {
slouken@8582
   653
            i = 0;
slouken@8582
   654
            bGameButton = SDL_TRUE;
slouken@10724
   655
            SDL_PrivateGameControllerParseElement(gamecontroller, szGameButton, szJoystickButton);
slouken@10724
   656
            SDL_zero(szGameButton);
slouken@10724
   657
            SDL_zero(szJoystickButton);
slouken@8582
   658
slouken@8772
   659
        } else if (bGameButton) {
slouken@8772
   660
            if (i >= sizeof(szGameButton)) {
slouken@8772
   661
                SDL_SetError("Button name too large: %s", szGameButton);
slouken@8582
   662
                return;
slouken@8582
   663
            }
slouken@8582
   664
            szGameButton[i] = *pchPos;
slouken@8582
   665
            i++;
slouken@8772
   666
        } else {
slouken@8772
   667
            if (i >= sizeof(szJoystickButton)) {
slouken@8772
   668
                SDL_SetError("Joystick button name too large: %s", szJoystickButton);
slouken@8582
   669
                return;
slouken@8582
   670
            }
slouken@8582
   671
            szJoystickButton[i] = *pchPos;
slouken@8582
   672
            i++;
slouken@8582
   673
        }
slouken@8582
   674
        pchPos++;
slouken@8582
   675
    }
slouken@8582
   676
slouken@10724
   677
    SDL_PrivateGameControllerParseElement(gamecontroller, szGameButton, szJoystickButton);
slouken@8582
   678
slouken@8582
   679
}
slouken@8582
   680
slouken@8582
   681
/*
slouken@8582
   682
 * Make a new button mapping struct
slouken@8582
   683
 */
slouken@10724
   684
static void SDL_PrivateLoadButtonMapping(SDL_GameController *gamecontroller, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
slouken@8582
   685
{
slouken@10724
   686
    int i;
slouken@8582
   687
slouken@10724
   688
    gamecontroller->guid = guid;
slouken@10724
   689
    gamecontroller->name = pchName;
slouken@10724
   690
    gamecontroller->num_bindings = 0;
slouken@10724
   691
    SDL_memset(gamecontroller->last_match_axis, 0, gamecontroller->joystick->naxes * sizeof(*gamecontroller->last_match_axis));
slouken@8582
   692
slouken@10724
   693
    SDL_PrivateGameControllerParseControllerConfigString(gamecontroller, pchMapping);
slouken@10724
   694
slouken@10724
   695
    /* Set the zero point for triggers */
slouken@10724
   696
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
   697
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
   698
        if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
slouken@10724
   699
            binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS &&
slouken@10724
   700
            (binding->output.axis.axis == SDL_CONTROLLER_AXIS_TRIGGERLEFT ||
slouken@10724
   701
             binding->output.axis.axis == SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) {
slouken@10724
   702
            if (binding->input.axis.axis < gamecontroller->joystick->naxes) {
slouken@10724
   703
                gamecontroller->joystick->axes[binding->input.axis.axis].value =
slouken@10724
   704
                gamecontroller->joystick->axes[binding->input.axis.axis].zero = (Sint16)binding->input.axis.axis_min;
slouken@10724
   705
            }
slouken@10724
   706
        }
slouken@8582
   707
    }
slouken@8582
   708
}
slouken@8582
   709
slouken@8582
   710
slouken@8582
   711
/*
slouken@8582
   712
 * grab the guid string from a mapping string
slouken@8582
   713
 */
slouken@10609
   714
static char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
slouken@8582
   715
{
slouken@8772
   716
    const char *pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   717
    if (pFirstComma) {
slouken@8772
   718
        char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
slouken@8772
   719
        if (!pchGUID) {
slouken@8582
   720
            SDL_OutOfMemory();
slouken@8582
   721
            return NULL;
slouken@8582
   722
        }
slouken@8772
   723
        SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
slouken@10602
   724
        pchGUID[pFirstComma - pMapping] = '\0';
slouken@10602
   725
slouken@10602
   726
        /* Convert old style GUIDs to the new style in 2.0.5 */
slouken@10602
   727
#if __WIN32__
slouken@10602
   728
        if (SDL_strlen(pchGUID) == 32 &&
slouken@10602
   729
            SDL_memcmp(&pchGUID[20], "504944564944", 12) == 0) {
slouken@10602
   730
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
slouken@10602
   731
            SDL_memcpy(&pchGUID[16], &pchGUID[4], 4);
slouken@10602
   732
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
slouken@10602
   733
            SDL_memcpy(&pchGUID[0], "03000000", 8);
slouken@10602
   734
        }
slouken@10602
   735
#elif __MACOSX__
slouken@10602
   736
        if (SDL_strlen(pchGUID) == 32 &&
slouken@10602
   737
            SDL_memcmp(&pchGUID[4], "000000000000", 12) == 0 &&
slouken@10602
   738
            SDL_memcmp(&pchGUID[20], "000000000000", 12) == 0) {
slouken@10602
   739
            SDL_memcpy(&pchGUID[20], "000000000000", 12);
slouken@10602
   740
            SDL_memcpy(&pchGUID[8], &pchGUID[0], 4);
slouken@10602
   741
            SDL_memcpy(&pchGUID[0], "03000000", 8);
slouken@10602
   742
        }
slouken@10602
   743
#endif
slouken@8582
   744
        return pchGUID;
slouken@8582
   745
    }
slouken@8582
   746
    return NULL;
slouken@8582
   747
}
slouken@8582
   748
slouken@8582
   749
slouken@8582
   750
/*
slouken@8582
   751
 * grab the name string from a mapping string
slouken@8582
   752
 */
slouken@10609
   753
static char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
slouken@8582
   754
{
slouken@8582
   755
    const char *pFirstComma, *pSecondComma;
slouken@8582
   756
    char *pchName;
slouken@8582
   757
slouken@8772
   758
    pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   759
    if (!pFirstComma)
slouken@8582
   760
        return NULL;
slouken@8582
   761
slouken@8772
   762
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
slouken@8772
   763
    if (!pSecondComma)
slouken@8582
   764
        return NULL;
slouken@8582
   765
slouken@8772
   766
    pchName = SDL_malloc(pSecondComma - pFirstComma);
slouken@8772
   767
    if (!pchName) {
slouken@8582
   768
        SDL_OutOfMemory();
slouken@8582
   769
        return NULL;
slouken@8582
   770
    }
slouken@8772
   771
    SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
slouken@10602
   772
    pchName[pSecondComma - pFirstComma - 1] = 0;
slouken@8582
   773
    return pchName;
slouken@8582
   774
}
slouken@8582
   775
slouken@8582
   776
slouken@8582
   777
/*
slouken@8582
   778
 * grab the button mapping string from a mapping string
slouken@8582
   779
 */
slouken@10609
   780
static char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
slouken@8582
   781
{
slouken@8582
   782
    const char *pFirstComma, *pSecondComma;
slouken@8582
   783
slouken@8772
   784
    pFirstComma = SDL_strchr(pMapping, ',');
slouken@8772
   785
    if (!pFirstComma)
slouken@8582
   786
        return NULL;
slouken@8582
   787
slouken@8772
   788
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
slouken@8772
   789
    if (!pSecondComma)
slouken@8582
   790
        return NULL;
slouken@8582
   791
slouken@8582
   792
    return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
slouken@8582
   793
}
slouken@8582
   794
slouken@9956
   795
/*
slouken@9956
   796
 * Helper function to refresh a mapping
slouken@9956
   797
 */
slouken@10609
   798
static void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
slouken@8582
   799
{
slouken@8582
   800
    SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
slouken@8772
   801
    while (gamecontrollerlist) {
slouken@10724
   802
        if (!SDL_memcmp(&gamecontrollerlist->guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
slouken@8582
   803
            SDL_Event event;
slouken@8582
   804
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
slouken@8582
   805
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
slouken@8582
   806
            SDL_PushEvent(&event);
slouken@8582
   807
slouken@8582
   808
            /* Not really threadsafe.  Should this lock access within SDL_GameControllerEventWatcher? */
slouken@10724
   809
            SDL_PrivateLoadButtonMapping(gamecontrollerlist, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
slouken@8582
   810
        }
slouken@8582
   811
slouken@8582
   812
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
   813
    }
slouken@8582
   814
}
slouken@8582
   815
slouken@8582
   816
/*
slouken@9956
   817
 * Helper function to add a mapping for a guid
slouken@9956
   818
 */
slouken@9956
   819
static ControllerMapping_t *
slouken@10605
   820
SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing, SDL_ControllerMappingPriority priority)
slouken@9956
   821
{
slouken@9956
   822
    char *pchName;
slouken@9956
   823
    char *pchMapping;
slouken@9956
   824
    ControllerMapping_t *pControllerMapping;
slouken@9956
   825
slouken@9956
   826
    pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
slouken@9956
   827
    if (!pchName) {
slouken@9956
   828
        SDL_SetError("Couldn't parse name from %s", mappingString);
slouken@9956
   829
        return NULL;
slouken@9956
   830
    }
slouken@9956
   831
slouken@9956
   832
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
slouken@9956
   833
    if (!pchMapping) {
slouken@9956
   834
        SDL_free(pchName);
slouken@9956
   835
        SDL_SetError("Couldn't parse %s", mappingString);
slouken@9956
   836
        return NULL;
slouken@9956
   837
    }
slouken@9956
   838
slouken@9956
   839
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
slouken@9956
   840
    if (pControllerMapping) {
slouken@10605
   841
        /* Only overwrite the mapping if the priority is the same or higher. */
slouken@10605
   842
        if (pControllerMapping->priority <= priority) {
slouken@10605
   843
            /* Update existing mapping */
slouken@10605
   844
            SDL_free(pControllerMapping->name);
slouken@10605
   845
            pControllerMapping->name = pchName;
slouken@10605
   846
            SDL_free(pControllerMapping->mapping);
slouken@10605
   847
            pControllerMapping->mapping = pchMapping;
slouken@10605
   848
            pControllerMapping->priority = priority;
slouken@10605
   849
            /* refresh open controllers */
slouken@10605
   850
            SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
philipp@10632
   851
        } else {
philipp@10632
   852
            SDL_free(pchName);
philipp@10632
   853
            SDL_free(pchMapping);
slouken@10605
   854
        }
slouken@9956
   855
        *existing = SDL_TRUE;
slouken@9956
   856
    } else {
slouken@9956
   857
        pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
slouken@9956
   858
        if (!pControllerMapping) {
slouken@9956
   859
            SDL_free(pchName);
slouken@9956
   860
            SDL_free(pchMapping);
slouken@9956
   861
            SDL_OutOfMemory();
slouken@9956
   862
            return NULL;
slouken@9956
   863
        }
slouken@9956
   864
        pControllerMapping->guid = jGUID;
slouken@9956
   865
        pControllerMapping->name = pchName;
slouken@9956
   866
        pControllerMapping->mapping = pchMapping;
slouken@10662
   867
        pControllerMapping->next = NULL;
slouken@10605
   868
        pControllerMapping->priority = priority;
slouken@10662
   869
slouken@10662
   870
        if (s_pSupportedControllers) {
slouken@10662
   871
            /* Add the mapping to the end of the list */
slouken@10662
   872
            ControllerMapping_t *pCurrMapping, *pPrevMapping;
slouken@10662
   873
slouken@10662
   874
            for ( pPrevMapping = s_pSupportedControllers, pCurrMapping = pPrevMapping->next;
slouken@10662
   875
                  pCurrMapping; 
slouken@10662
   876
                  pPrevMapping = pCurrMapping, pCurrMapping = pCurrMapping->next ) {
slouken@10662
   877
                continue;
slouken@10662
   878
            }
slouken@10662
   879
            pPrevMapping->next = pControllerMapping;
slouken@10662
   880
        } else {
slouken@10662
   881
            s_pSupportedControllers = pControllerMapping;
slouken@10662
   882
        }
slouken@9956
   883
        *existing = SDL_FALSE;
slouken@9956
   884
    }
slouken@9956
   885
    return pControllerMapping;
slouken@9956
   886
}
slouken@9956
   887
slouken@11921
   888
#ifdef __ANDROID__
slouken@11921
   889
/*
slouken@11921
   890
 * Helper function to guess at a mapping based on the elements reported for this controller
slouken@11921
   891
 */
slouken@11921
   892
static ControllerMapping_t *SDL_CreateMappingForAndroidController(const char *name, SDL_JoystickGUID guid)
slouken@11921
   893
{
slouken@11921
   894
    SDL_bool existing;
slouken@11921
   895
    char name_string[128];
slouken@11921
   896
    char mapping_string[1024];
slouken@11921
   897
    int button_mask;
slouken@11921
   898
    int axis_mask;
slouken@11921
   899
slouken@11921
   900
    button_mask = SDL_SwapLE16(*(Uint16*)(&guid.data[sizeof(guid.data)-4]));
slouken@11921
   901
    axis_mask = SDL_SwapLE16(*(Uint16*)(&guid.data[sizeof(guid.data)-2]));
slouken@11921
   902
    if (!button_mask && !axis_mask) {
slouken@11921
   903
        /* Accelerometer, shouldn't have a game controller mapping */
slouken@11921
   904
        return NULL;
slouken@11921
   905
    }
slouken@11921
   906
slouken@11921
   907
    /* Remove any commas in the name */
slouken@11921
   908
    SDL_strlcpy(name_string, name, sizeof(name_string));
slouken@11921
   909
    {
slouken@11921
   910
        char *spot;
slouken@11921
   911
        for (spot = name_string; *spot; ++spot) {
slouken@11921
   912
            if (*spot == ',') {
slouken@11921
   913
                *spot = ' ';
slouken@11921
   914
            }
slouken@11921
   915
        }
slouken@11921
   916
    }
slouken@11921
   917
    SDL_snprintf(mapping_string, sizeof(mapping_string), "none,%s,", name_string);
slouken@11921
   918
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_A)) {
slouken@11921
   919
        SDL_strlcat(mapping_string, "a:b0,", sizeof(mapping_string));
slouken@11921
   920
    }
slouken@11921
   921
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_B)) {
slouken@11921
   922
        SDL_strlcat(mapping_string, "b:b1,", sizeof(mapping_string));
slouken@11921
   923
    } else if (button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
slouken@11921
   924
        /* Use the back button as "B" for easy UI navigation with TV remotes */
slouken@11921
   925
        SDL_strlcat(mapping_string, "b:b4,", sizeof(mapping_string));
slouken@11921
   926
        button_mask &= ~(1 << SDL_CONTROLLER_BUTTON_BACK);
slouken@11921
   927
    }
slouken@11921
   928
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_X)) {
slouken@11921
   929
        SDL_strlcat(mapping_string, "x:b2,", sizeof(mapping_string));
slouken@11921
   930
    }
slouken@11921
   931
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_Y)) {
slouken@11921
   932
        SDL_strlcat(mapping_string, "y:b3,", sizeof(mapping_string));
slouken@11921
   933
    }
slouken@11921
   934
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_BACK)) {
slouken@11921
   935
        SDL_strlcat(mapping_string, "back:b4,", sizeof(mapping_string));
slouken@11921
   936
    }
slouken@11983
   937
#if 0 /* The guide button generally isn't functional (or acts as a home button) on most Android controllers */
slouken@11921
   938
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_GUIDE)) {
slouken@11921
   939
        SDL_strlcat(mapping_string, "guide:b5,", sizeof(mapping_string));
slouken@11921
   940
#if 0 /* Actually this will be done in Steam */
slouken@11921
   941
    } else if (button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
slouken@11921
   942
        /* The guide button doesn't exist, use the start button instead,
slouken@11921
   943
           so you can do Steam guide button chords and open the Steam overlay.
slouken@11921
   944
         */
slouken@11921
   945
        SDL_strlcat(mapping_string, "guide:b6,", sizeof(mapping_string));
slouken@11921
   946
        button_mask &= ~(1 << SDL_CONTROLLER_BUTTON_START);
slouken@11921
   947
#endif
slouken@11921
   948
    }
slouken@11983
   949
#endif
slouken@11921
   950
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_START)) {
slouken@11921
   951
        SDL_strlcat(mapping_string, "start:b6,", sizeof(mapping_string));
slouken@11921
   952
    }
slouken@11921
   953
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSTICK)) {
slouken@11921
   954
        SDL_strlcat(mapping_string, "leftstick:b7,", sizeof(mapping_string));
slouken@11921
   955
    }
slouken@11921
   956
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSTICK)) {
slouken@11921
   957
        SDL_strlcat(mapping_string, "rightstick:b8,", sizeof(mapping_string));
slouken@11921
   958
    }
slouken@11921
   959
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_LEFTSHOULDER)) {
slouken@11921
   960
        SDL_strlcat(mapping_string, "leftshoulder:b9,", sizeof(mapping_string));
slouken@11921
   961
    }
slouken@11921
   962
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)) {
slouken@11921
   963
        SDL_strlcat(mapping_string, "rightshoulder:b10,", sizeof(mapping_string));
slouken@11921
   964
    }
slouken@11921
   965
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_UP)) {
slouken@11921
   966
        SDL_strlcat(mapping_string, "dpup:b11,", sizeof(mapping_string));
slouken@11921
   967
    }
slouken@11921
   968
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_DOWN)) {
slouken@11921
   969
        SDL_strlcat(mapping_string, "dpdown:b12,", sizeof(mapping_string));
slouken@11921
   970
    }
slouken@11921
   971
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_LEFT)) {
slouken@11921
   972
        SDL_strlcat(mapping_string, "dpleft:b13,", sizeof(mapping_string));
slouken@11921
   973
    }
slouken@11921
   974
    if (button_mask & (1 << SDL_CONTROLLER_BUTTON_DPAD_RIGHT)) {
slouken@11921
   975
        SDL_strlcat(mapping_string, "dpright:b14,", sizeof(mapping_string));
slouken@11921
   976
    }
slouken@11921
   977
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_LEFTX)) {
slouken@11921
   978
        SDL_strlcat(mapping_string, "leftx:a0,", sizeof(mapping_string));
slouken@11921
   979
    }
slouken@11921
   980
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_LEFTY)) {
slouken@11921
   981
        SDL_strlcat(mapping_string, "lefty:a1,", sizeof(mapping_string));
slouken@11921
   982
    }
slouken@11921
   983
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_RIGHTX)) {
slouken@11921
   984
        SDL_strlcat(mapping_string, "rightx:a2,", sizeof(mapping_string));
slouken@11921
   985
    }
slouken@11921
   986
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_RIGHTY)) {
slouken@11921
   987
        SDL_strlcat(mapping_string, "righty:a3,", sizeof(mapping_string));
slouken@11921
   988
    }
slouken@11921
   989
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_TRIGGERLEFT)) {
slouken@11998
   990
        SDL_strlcat(mapping_string, "lefttrigger:a4,", sizeof(mapping_string));
slouken@11921
   991
    }
slouken@11921
   992
    if (axis_mask & (1 << SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) {
slouken@11998
   993
        SDL_strlcat(mapping_string, "righttrigger:a5,", sizeof(mapping_string));
slouken@11921
   994
    }
slouken@11921
   995
    return SDL_PrivateAddMappingForGUID(guid, mapping_string,
slouken@11921
   996
                      &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@11921
   997
}
slouken@11921
   998
#endif /* __ANDROID__ */
slouken@11921
   999
slouken@11921
  1000
slouken@9956
  1001
/*
slouken@9956
  1002
 * Helper function to determine pre-calculated offset to certain joystick mappings
slouken@9956
  1003
 */
slouken@11201
  1004
static ControllerMapping_t *SDL_PrivateGetControllerMappingForNameAndGUID(const char *name, SDL_JoystickGUID guid)
slouken@11201
  1005
{
slouken@11201
  1006
    ControllerMapping_t *mapping;
slouken@11201
  1007
slouken@11201
  1008
    mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@11201
  1009
#ifdef __LINUX__
slouken@11201
  1010
    if (!mapping && name) {
slouken@11201
  1011
        if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
slouken@11201
  1012
            /* The Linux driver xpad.c maps the wireless dpad to buttons */
slouken@11201
  1013
            SDL_bool existing;
slouken@11202
  1014
            mapping = SDL_PrivateAddMappingForGUID(guid,
slouken@11201
  1015
"none,X360 Wireless Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b11,dpright:b12,dpup:b13,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
slouken@11201
  1016
                          &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@11201
  1017
        }
slouken@11201
  1018
    }
slouken@11201
  1019
#endif /* __LINUX__ */
slouken@11201
  1020
slouken@11201
  1021
    if (!mapping && name) {
slouken@11267
  1022
        if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box") || SDL_strstr(name, "XBOX")) {
slouken@11201
  1023
            mapping = s_pXInputMapping;
slouken@11201
  1024
        }
slouken@11201
  1025
    }
slouken@11921
  1026
#ifdef __ANDROID__
slouken@11921
  1027
    if (!mapping) {
slouken@11921
  1028
        mapping = SDL_CreateMappingForAndroidController(name, guid);
slouken@11921
  1029
    }
slouken@11921
  1030
#endif
slouken@11921
  1031
    if (!mapping) {
slouken@11921
  1032
        mapping = s_pDefaultMapping;
slouken@11921
  1033
    }
slouken@11201
  1034
    return mapping;
slouken@11201
  1035
}
slouken@11201
  1036
slouken@10609
  1037
static ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
slouken@9956
  1038
{
slouken@11579
  1039
    const char *name;
slouken@11579
  1040
    SDL_JoystickGUID guid;
slouken@11579
  1041
    ControllerMapping_t *mapping;
slouken@11579
  1042
slouken@11581
  1043
    SDL_LockJoysticks();
slouken@11781
  1044
slouken@11781
  1045
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@11781
  1046
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@11781
  1047
        SDL_UnlockJoysticks();
slouken@11781
  1048
        return (NULL);
slouken@11781
  1049
    }
slouken@11781
  1050
slouken@11579
  1051
    name = SDL_JoystickNameForIndex(device_index);
slouken@11579
  1052
    guid = SDL_JoystickGetDeviceGUID(device_index);
slouken@11579
  1053
    mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
slouken@11581
  1054
    SDL_UnlockJoysticks();
slouken@9956
  1055
    return mapping;
slouken@9956
  1056
}
slouken@9956
  1057
slouken@9956
  1058
/*
slouken@8582
  1059
 * Add or update an entry into the Mappings Database
slouken@8582
  1060
 */
slouken@8582
  1061
int
slouken@8772
  1062
SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
gabomdq@8042
  1063
{
gabomdq@8042
  1064
    const char *platform = SDL_GetPlatform();
gabomdq@8042
  1065
    int controllers = 0;
gabomdq@8042
  1066
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
gabomdq@8042
  1067
    size_t db_size, platform_len;
gabomdq@8042
  1068
    
gabomdq@8042
  1069
    if (rw == NULL) {
gabomdq@8046
  1070
        return SDL_SetError("Invalid RWops");
gabomdq@8042
  1071
    }
slouken@8052
  1072
    db_size = (size_t)SDL_RWsize(rw);
gabomdq@8042
  1073
    
slouken@8052
  1074
    buf = (char *)SDL_malloc(db_size + 1);
gabomdq@8042
  1075
    if (buf == NULL) {
gabomdq@8046
  1076
        if (freerw) {
gabomdq@8046
  1077
            SDL_RWclose(rw);
gabomdq@8046
  1078
        }
philipp@9194
  1079
        return SDL_SetError("Could not allocate space to read DB into memory");
gabomdq@8042
  1080
    }
gabomdq@8042
  1081
    
gabomdq@8042
  1082
    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
gabomdq@8046
  1083
        if (freerw) {
gabomdq@8046
  1084
            SDL_RWclose(rw);
gabomdq@8046
  1085
        }
gabomdq@8042
  1086
        SDL_free(buf);
gabomdq@8042
  1087
        return SDL_SetError("Could not read DB");
gabomdq@8042
  1088
    }
gabomdq@8046
  1089
    
gabomdq@8046
  1090
    if (freerw) {
gabomdq@8046
  1091
        SDL_RWclose(rw);
gabomdq@8046
  1092
    }
gabomdq@8042
  1093
    
gabomdq@8042
  1094
    buf[db_size] = '\0';
gabomdq@8042
  1095
    line = buf;
gabomdq@8042
  1096
    
gabomdq@8042
  1097
    while (line < buf + db_size) {
slouken@8772
  1098
        line_end = SDL_strchr(line, '\n');
gabomdq@8042
  1099
        if (line_end != NULL) {
gabomdq@8042
  1100
            *line_end = '\0';
slouken@8772
  1101
        } else {
gabomdq@8042
  1102
            line_end = buf + db_size;
gabomdq@8042
  1103
        }
gabomdq@8042
  1104
        
gabomdq@8042
  1105
        /* Extract and verify the platform */
gabomdq@8042
  1106
        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
slouken@8772
  1107
        if (tmp != NULL) {
gabomdq@8042
  1108
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
gabomdq@8042
  1109
            comma = SDL_strchr(tmp, ',');
gabomdq@8042
  1110
            if (comma != NULL) {
gabomdq@8042
  1111
                platform_len = comma - tmp + 1;
gabomdq@8042
  1112
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
gabomdq@8042
  1113
                    SDL_strlcpy(line_platform, tmp, platform_len);
slouken@8772
  1114
                    if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
slouken@8772
  1115
                        SDL_GameControllerAddMapping(line) > 0) {
gabomdq@8042
  1116
                        controllers++;
gabomdq@8042
  1117
                    }
gabomdq@8042
  1118
                }
gabomdq@8042
  1119
            }
gabomdq@8042
  1120
        }
gabomdq@8042
  1121
        
gabomdq@8042
  1122
        line = line_end + 1;
gabomdq@8042
  1123
    }
gabomdq@8042
  1124
gabomdq@8042
  1125
    SDL_free(buf);
gabomdq@8042
  1126
    return controllers;
gabomdq@8042
  1127
}
gabomdq@8042
  1128
gabomdq@8042
  1129
/*
slouken@10605
  1130
 * Add or update an entry into the Mappings Database with a priority
gabomdq@8042
  1131
 */
slouken@10605
  1132
static int
slouken@10605
  1133
SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMappingPriority priority)
slouken@8582
  1134
{
slouken@8582
  1135
    char *pchGUID;
slouken@8582
  1136
    SDL_JoystickGUID jGUID;
slouken@11921
  1137
    SDL_bool is_default_mapping = SDL_FALSE;
slouken@12088
  1138
    SDL_bool is_hidapi_mapping = SDL_FALSE;
slouken@8582
  1139
    SDL_bool is_xinput_mapping = SDL_FALSE;
slouken@9956
  1140
    SDL_bool existing = SDL_FALSE;
slouken@9956
  1141
    ControllerMapping_t *pControllerMapping;
slouken@8582
  1142
philipp@9198
  1143
    if (!mappingString) {
philipp@9198
  1144
        return SDL_InvalidParamError("mappingString");
philipp@9198
  1145
    }
philipp@9198
  1146
slouken@8772
  1147
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
slouken@8582
  1148
    if (!pchGUID) {
philipp@8081
  1149
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
slouken@8582
  1150
    }
slouken@11921
  1151
    if (!SDL_strcasecmp(pchGUID, "default")) {
slouken@11921
  1152
        is_default_mapping = SDL_TRUE;
slouken@12088
  1153
    } else if (!SDL_strcasecmp(pchGUID, "hidapi")) {
slouken@12088
  1154
        is_hidapi_mapping = SDL_TRUE;
slouken@11921
  1155
    } else if (!SDL_strcasecmp(pchGUID, "xinput")) {
slouken@8582
  1156
        is_xinput_mapping = SDL_TRUE;
slouken@8582
  1157
    }
slouken@8582
  1158
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
slouken@8582
  1159
    SDL_free(pchGUID);
slouken@8582
  1160
slouken@10605
  1161
    pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
slouken@9956
  1162
    if (!pControllerMapping) {
slouken@9956
  1163
        return -1;
slouken@8053
  1164
    }
slouken@8582
  1165
slouken@9956
  1166
    if (existing) {
slouken@8582
  1167
        return 0;
slouken@8582
  1168
    } else {
slouken@11921
  1169
        if (is_default_mapping) {
slouken@11921
  1170
            s_pDefaultMapping = pControllerMapping;
slouken@12088
  1171
        } else if (is_hidapi_mapping) {
slouken@12088
  1172
            s_pHIDAPIMapping = pControllerMapping;
slouken@11921
  1173
        } else if (is_xinput_mapping) {
slouken@8582
  1174
            s_pXInputMapping = pControllerMapping;
slouken@8582
  1175
        }
slouken@8582
  1176
        return 1;
slouken@8582
  1177
    }
slouken@8582
  1178
}
slouken@8582
  1179
slouken@8582
  1180
/*
slouken@10605
  1181
 * Add or update an entry into the Mappings Database
slouken@10605
  1182
 */
slouken@10605
  1183
int
slouken@10605
  1184
SDL_GameControllerAddMapping(const char *mappingString)
slouken@10605
  1185
{
slouken@10605
  1186
    return SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
slouken@10605
  1187
}
slouken@10605
  1188
philipp@10813
  1189
/*
slouken@10661
  1190
 *  Get the number of mappings installed
slouken@10661
  1191
 */
slouken@10661
  1192
int
slouken@10721
  1193
SDL_GameControllerNumMappings(void)
slouken@10661
  1194
{
slouken@10661
  1195
    int num_mappings = 0;
slouken@10661
  1196
    ControllerMapping_t *mapping;
slouken@10661
  1197
slouken@10661
  1198
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
  1199
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
  1200
            continue;
slouken@10661
  1201
        }
slouken@10661
  1202
        ++num_mappings;
slouken@10661
  1203
    }
slouken@10661
  1204
    return num_mappings;
slouken@10661
  1205
}
slouken@10661
  1206
philipp@10813
  1207
/*
slouken@10661
  1208
 *  Get the mapping at a particular index.
slouken@10661
  1209
 */
slouken@10661
  1210
char *
slouken@10661
  1211
SDL_GameControllerMappingForIndex(int mapping_index)
slouken@10661
  1212
{
slouken@10661
  1213
    ControllerMapping_t *mapping;
slouken@10661
  1214
slouken@10661
  1215
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
  1216
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
  1217
            continue;
slouken@10661
  1218
        }
slouken@10661
  1219
        if (mapping_index == 0) {
slouken@10661
  1220
            char *pMappingString;
slouken@10661
  1221
            char pchGUID[33];
slouken@10661
  1222
            size_t needed;
slouken@10661
  1223
slouken@10661
  1224
            SDL_JoystickGetGUIDString(mapping->guid, pchGUID, sizeof(pchGUID));
slouken@10661
  1225
            /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@10661
  1226
            needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@10661
  1227
            pMappingString = SDL_malloc(needed);
slouken@10661
  1228
            if (!pMappingString) {
slouken@10661
  1229
                SDL_OutOfMemory();
slouken@10661
  1230
                return NULL;
slouken@10661
  1231
            }
slouken@10661
  1232
            SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@10661
  1233
            return pMappingString;
slouken@10661
  1234
        }
slouken@10661
  1235
        --mapping_index;
slouken@10661
  1236
    }
slouken@10661
  1237
    return NULL;
slouken@10661
  1238
}
slouken@10661
  1239
slouken@10605
  1240
/*
slouken@8582
  1241
 * Get the mapping string for this GUID
slouken@8582
  1242
 */
slouken@8582
  1243
char *
slouken@8772
  1244
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
slouken@8582
  1245
{
slouken@8582
  1246
    char *pMappingString = NULL;
slouken@8582
  1247
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@8582
  1248
    if (mapping) {
slouken@8582
  1249
        char pchGUID[33];
slouken@8582
  1250
        size_t needed;
slouken@8582
  1251
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@8582
  1252
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@8582
  1253
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@8772
  1254
        pMappingString = SDL_malloc(needed);
philipp@9197
  1255
        if (!pMappingString) {
philipp@9197
  1256
            SDL_OutOfMemory();
philipp@9197
  1257
            return NULL;
philipp@9197
  1258
        }
slouken@8772
  1259
        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@8582
  1260
    }
slouken@8582
  1261
    return pMappingString;
slouken@8582
  1262
}
slouken@8582
  1263
slouken@8582
  1264
/*
slouken@8582
  1265
 * Get the mapping string for this device
slouken@8582
  1266
 */
slouken@8582
  1267
char *
slouken@8772
  1268
SDL_GameControllerMapping(SDL_GameController * gamecontroller)
slouken@8582
  1269
{
philipp@9196
  1270
    if (!gamecontroller) {
philipp@9196
  1271
        return NULL;
philipp@9196
  1272
    }
philipp@9196
  1273
slouken@10724
  1274
    return SDL_GameControllerMappingForGUID(gamecontroller->guid);
slouken@8582
  1275
}
slouken@8582
  1276
slouken@8582
  1277
static void
slouken@8582
  1278
SDL_GameControllerLoadHints()
slouken@8582
  1279
{
slouken@8582
  1280
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
slouken@8772
  1281
    if (hint && hint[0]) {
slouken@8772
  1282
        size_t nchHints = SDL_strlen(hint);
slouken@8772
  1283
        char *pUserMappings = SDL_malloc(nchHints + 1);
slouken@8582
  1284
        char *pTempMappings = pUserMappings;
slouken@8772
  1285
        SDL_memcpy(pUserMappings, hint, nchHints);
slouken@8267
  1286
        pUserMappings[nchHints] = '\0';
slouken@8772
  1287
        while (pUserMappings) {
slouken@8582
  1288
            char *pchNewLine = NULL;
slouken@8582
  1289
slouken@8772
  1290
            pchNewLine = SDL_strchr(pUserMappings, '\n');
slouken@8772
  1291
            if (pchNewLine)
slouken@8582
  1292
                *pchNewLine = '\0';
slouken@8582
  1293
slouken@10605
  1294
            SDL_PrivateGameControllerAddMapping(pUserMappings, SDL_CONTROLLER_MAPPING_PRIORITY_USER);
slouken@8582
  1295
slouken@8772
  1296
            if (pchNewLine) {
slouken@8582
  1297
                pUserMappings = pchNewLine + 1;
slouken@8772
  1298
            } else {
slouken@8582
  1299
                pUserMappings = NULL;
slouken@8772
  1300
            }
slouken@8582
  1301
        }
slouken@8582
  1302
        SDL_free(pTempMappings);
slouken@8582
  1303
    }
slouken@8582
  1304
}
slouken@8582
  1305
slouken@8582
  1306
/*
slouken@11532
  1307
 * Fill the given buffer with the expected controller mapping filepath. 
slouken@11532
  1308
 * Usually this will just be CONTROLLER_MAPPING_FILE, but for Android,
slouken@11532
  1309
 * we want to get the internal storage path.
slouken@11532
  1310
 */
slouken@11532
  1311
static SDL_bool SDL_GetControllerMappingFilePath(char *path, size_t size)
slouken@11532
  1312
{
slouken@11532
  1313
#ifdef CONTROLLER_MAPPING_FILE
slouken@11532
  1314
#define STRING(X) SDL_STRINGIFY_ARG(X)
slouken@11532
  1315
    return SDL_strlcpy(path, STRING(CONTROLLER_MAPPING_FILE), size) < size;
slouken@11532
  1316
#elif defined(__ANDROID__)
slouken@11532
  1317
    return SDL_snprintf(path, size, "%s/controller_map.txt", SDL_AndroidGetInternalStoragePath()) < size;
slouken@11532
  1318
#else
slouken@11532
  1319
    return SDL_FALSE;
slouken@11532
  1320
#endif
slouken@11532
  1321
}
slouken@11532
  1322
slouken@11532
  1323
/*
slouken@8582
  1324
 * Initialize the game controller system, mostly load our DB of controller config mappings
slouken@8582
  1325
 */
slouken@8582
  1326
int
slouken@11201
  1327
SDL_GameControllerInitMappings(void)
slouken@8582
  1328
{
slouken@11532
  1329
    char szControllerMapPath[1024];
slouken@8582
  1330
    int i = 0;
slouken@8582
  1331
    const char *pMappingString = NULL;
slouken@8582
  1332
    pMappingString = s_ControllerMappings[i];
slouken@8772
  1333
    while (pMappingString) {
slouken@10605
  1334
        SDL_PrivateGameControllerAddMapping(pMappingString, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@8582
  1335
slouken@8582
  1336
        i++;
slouken@8582
  1337
        pMappingString = s_ControllerMappings[i];
slouken@8582
  1338
    }
slouken@8582
  1339
slouken@11532
  1340
    if (SDL_GetControllerMappingFilePath(szControllerMapPath, sizeof(szControllerMapPath))) {
slouken@11532
  1341
        SDL_GameControllerAddMappingsFromFile(szControllerMapPath);        
slouken@11532
  1342
    }
slouken@11532
  1343
slouken@8582
  1344
    /* load in any user supplied config */
slouken@8582
  1345
    SDL_GameControllerLoadHints();
slouken@8582
  1346
slouken@11201
  1347
    SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
slouken@11201
  1348
                        SDL_GameControllerIgnoreDevicesChanged, NULL);
slouken@11201
  1349
    SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
slouken@11201
  1350
                        SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
slouken@11201
  1351
slouken@11201
  1352
    return (0);
slouken@11201
  1353
}
slouken@11201
  1354
slouken@11201
  1355
int
slouken@11201
  1356
SDL_GameControllerInit(void)
slouken@11201
  1357
{
slouken@11201
  1358
    int i;
slouken@11201
  1359
slouken@8582
  1360
    /* watch for joy events and fire controller ones if needed */
slouken@8772
  1361
    SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1362
slouken@8067
  1363
    /* Send added events for controllers currently attached */
slouken@8067
  1364
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@8067
  1365
        if (SDL_IsGameController(i)) {
slouken@8067
  1366
            SDL_Event deviceevent;
slouken@8067
  1367
            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@8067
  1368
            deviceevent.cdevice.which = i;
slouken@8067
  1369
            SDL_PushEvent(&deviceevent);
slouken@8067
  1370
        }
slouken@8067
  1371
    }
slouken@8067
  1372
slouken@8582
  1373
    return (0);
slouken@8582
  1374
}
slouken@8582
  1375
slouken@8582
  1376
slouken@8582
  1377
/*
slouken@8582
  1378
 * Get the implementation dependent name of a controller
slouken@8582
  1379
 */
slouken@8582
  1380
const char *
slouken@8582
  1381
SDL_GameControllerNameForIndex(int device_index)
slouken@8582
  1382
{
slouken@10724
  1383
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1384
    if (pSupportedController) {
slouken@11916
  1385
        if (SDL_strcmp(pSupportedController->name, "*") == 0) {
slouken@11916
  1386
            return SDL_JoystickNameForIndex(device_index);
slouken@11916
  1387
        } else {
slouken@11916
  1388
            return pSupportedController->name;
slouken@11916
  1389
        }
slouken@8582
  1390
    }
slouken@8582
  1391
    return NULL;
slouken@8582
  1392
}
slouken@8582
  1393
slouken@8582
  1394
slouken@11922
  1395
/**
slouken@11922
  1396
 *  Get the mapping of a game controller.
slouken@11922
  1397
 *  This can be called before any controllers are opened.
slouken@11922
  1398
 *  If no mapping can be found, this function returns NULL.
slouken@11922
  1399
 */
slouken@11922
  1400
char *
slouken@11922
  1401
SDL_GameControllerMappingForDeviceIndex(int joystick_index)
slouken@11922
  1402
{
slouken@11922
  1403
    char *pMappingString = NULL;
slouken@11922
  1404
    ControllerMapping_t *mapping;
slouken@11922
  1405
slouken@11922
  1406
    SDL_LockJoysticks();
slouken@11922
  1407
    mapping = SDL_PrivateGetControllerMapping(joystick_index);
slouken@11922
  1408
    if (mapping) {
slouken@11922
  1409
        SDL_JoystickGUID guid;
slouken@11922
  1410
        char pchGUID[33];
slouken@11922
  1411
        size_t needed;
slouken@11922
  1412
        guid = SDL_JoystickGetDeviceGUID(joystick_index);
slouken@11922
  1413
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@11922
  1414
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@11922
  1415
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@11922
  1416
        pMappingString = SDL_malloc(needed);
slouken@11922
  1417
        if (!pMappingString) {
slouken@11922
  1418
            SDL_OutOfMemory();
slouken@11922
  1419
            SDL_UnlockJoysticks();
slouken@11922
  1420
            return NULL;
slouken@11922
  1421
        }
slouken@11922
  1422
        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@11922
  1423
    }
slouken@11922
  1424
    SDL_UnlockJoysticks();
slouken@11922
  1425
    return pMappingString;
slouken@11922
  1426
}
slouken@11922
  1427
slouken@11922
  1428
slouken@8582
  1429
/*
slouken@11201
  1430
 * Return 1 if the joystick with this name and GUID is a supported controller
slouken@11201
  1431
 */
slouken@11201
  1432
SDL_bool
slouken@11201
  1433
SDL_IsGameControllerNameAndGUID(const char *name, SDL_JoystickGUID guid)
slouken@11201
  1434
{
slouken@11201
  1435
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
slouken@11201
  1436
    if (pSupportedController) {
slouken@11201
  1437
        return SDL_TRUE;
slouken@11201
  1438
    }
slouken@11201
  1439
    return SDL_FALSE;
slouken@11201
  1440
}
slouken@11201
  1441
slouken@11201
  1442
/*
slouken@8582
  1443
 * Return 1 if the joystick at this device index is a supported controller
slouken@8582
  1444
 */
slouken@8582
  1445
SDL_bool
slouken@8582
  1446
SDL_IsGameController(int device_index)
slouken@8582
  1447
{
slouken@10724
  1448
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1449
    if (pSupportedController) {
slouken@8582
  1450
        return SDL_TRUE;
slouken@8582
  1451
    }
slouken@8582
  1452
    return SDL_FALSE;
slouken@8582
  1453
}
slouken@8582
  1454
slouken@8582
  1455
/*
slouken@11201
  1456
 * Return 1 if the game controller should be ignored by SDL
slouken@11201
  1457
 */
slouken@11201
  1458
SDL_bool SDL_ShouldIgnoreGameController(const char *name, SDL_JoystickGUID guid)
slouken@11201
  1459
{
slouken@11201
  1460
    int i;
slouken@11201
  1461
    Uint16 vendor;
slouken@11201
  1462
    Uint16 product;
slouken@11201
  1463
    Uint32 vidpid;
slouken@11201
  1464
slouken@11201
  1465
    if (SDL_allowed_controllers.num_entries == 0 &&
slouken@11201
  1466
        SDL_ignored_controllers.num_entries == 0) {
slouken@11201
  1467
        return SDL_FALSE;
slouken@11201
  1468
    }
slouken@11201
  1469
slouken@11201
  1470
    SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
slouken@11201
  1471
slouken@11203
  1472
    if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", SDL_FALSE)) {
slouken@11203
  1473
        /* We shouldn't ignore Steam's virtual gamepad since it's using the hints to filter out the real controllers so it can remap input for the virtual controller */
slouken@11203
  1474
        SDL_bool bSteamVirtualGamepad = SDL_FALSE;
slouken@11203
  1475
#if defined(__LINUX__)
slouken@11203
  1476
        bSteamVirtualGamepad = (vendor == 0x28DE && product == 0x11FF);
slouken@11203
  1477
#elif defined(__MACOSX__)
slouken@11203
  1478
        bSteamVirtualGamepad = (SDL_strncmp(name, "GamePad-", 8) == 0);
slouken@11203
  1479
#elif defined(__WIN32__)
slouken@11203
  1480
        /* We can't tell on Windows, but Steam will block others in input hooks */
slouken@11203
  1481
        bSteamVirtualGamepad = SDL_TRUE;
slouken@11203
  1482
#endif
slouken@11203
  1483
        if (bSteamVirtualGamepad) {
slouken@11203
  1484
            return SDL_FALSE;
slouken@11203
  1485
        }
slouken@11203
  1486
    }
slouken@11203
  1487
slouken@12088
  1488
    vidpid = MAKE_VIDPID(vendor, product);
slouken@12088
  1489
slouken@11201
  1490
    if (SDL_allowed_controllers.num_entries > 0) {
slouken@11201
  1491
        for (i = 0; i < SDL_allowed_controllers.num_entries; ++i) {
slouken@11201
  1492
            if (vidpid == SDL_allowed_controllers.entries[i]) {
slouken@11201
  1493
                return SDL_FALSE;
slouken@11201
  1494
            }
slouken@11201
  1495
        }
slouken@11201
  1496
        return SDL_TRUE;
slouken@11201
  1497
    } else {
slouken@11201
  1498
        for (i = 0; i < SDL_ignored_controllers.num_entries; ++i) {
slouken@11201
  1499
            if (vidpid == SDL_ignored_controllers.entries[i]) {
slouken@11201
  1500
                return SDL_TRUE;
slouken@11201
  1501
            }
slouken@11201
  1502
        }
slouken@11201
  1503
        return SDL_FALSE;
slouken@11201
  1504
    }
slouken@11201
  1505
}
slouken@11201
  1506
slouken@11201
  1507
/*
slouken@8582
  1508
 * Open a controller for use - the index passed as an argument refers to
slouken@8582
  1509
 * the N'th controller on the system.  This index is the value which will
slouken@8582
  1510
 * identify this controller in future controller events.
slouken@8582
  1511
 *
slouken@8582
  1512
 * This function returns a controller identifier, or NULL if an error occurred.
slouken@8582
  1513
 */
slouken@8582
  1514
SDL_GameController *
slouken@8582
  1515
SDL_GameControllerOpen(int device_index)
slouken@8582
  1516
{
slouken@12088
  1517
    SDL_JoystickID instance_id;
slouken@8582
  1518
    SDL_GameController *gamecontroller;
slouken@8582
  1519
    SDL_GameController *gamecontrollerlist;
slouken@8582
  1520
    ControllerMapping_t *pSupportedController = NULL;
slouken@8582
  1521
slouken@11781
  1522
    SDL_LockJoysticks();
slouken@11781
  1523
slouken@8582
  1524
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1525
    /* If the controller is already open, return it */
slouken@12088
  1526
    instance_id = SDL_JoystickGetDeviceInstanceID(device_index);
slouken@8772
  1527
    while (gamecontrollerlist) {
slouken@12088
  1528
        if (instance_id == gamecontrollerlist->joystick->instance_id) {
slouken@8582
  1529
                gamecontroller = gamecontrollerlist;
slouken@8582
  1530
                ++gamecontroller->ref_count;
slouken@11581
  1531
                SDL_UnlockJoysticks();
slouken@8582
  1532
                return (gamecontroller);
slouken@8582
  1533
        }
slouken@8582
  1534
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1535
    }
slouken@8582
  1536
slouken@8582
  1537
    /* Find a controller mapping */
slouken@8582
  1538
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1539
    if (!pSupportedController) {
slouken@8772
  1540
        SDL_SetError("Couldn't find mapping for device (%d)", device_index);
slouken@11581
  1541
        SDL_UnlockJoysticks();
slouken@10688
  1542
        return NULL;
slouken@8582
  1543
    }
slouken@8582
  1544
philipp@10813
  1545
    /* Create and initialize the controller */
slouken@10724
  1546
    gamecontroller = (SDL_GameController *) SDL_calloc(1, sizeof(*gamecontroller));
slouken@8582
  1547
    if (gamecontroller == NULL) {
slouken@8582
  1548
        SDL_OutOfMemory();
slouken@11581
  1549
        SDL_UnlockJoysticks();
slouken@8582
  1550
        return NULL;
slouken@8582
  1551
    }
slouken@8582
  1552
slouken@8582
  1553
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
slouken@8772
  1554
    if (!gamecontroller->joystick) {
slouken@8582
  1555
        SDL_free(gamecontroller);
slouken@11581
  1556
        SDL_UnlockJoysticks();
slouken@8582
  1557
        return NULL;
slouken@8582
  1558
    }
slouken@8582
  1559
philipp@10728
  1560
    if (gamecontroller->joystick->naxes) {
philipp@10728
  1561
        gamecontroller->last_match_axis = (SDL_ExtendedGameControllerBind **)SDL_calloc(gamecontroller->joystick->naxes, sizeof(*gamecontroller->last_match_axis));
philipp@10728
  1562
        if (!gamecontroller->last_match_axis) {
philipp@10728
  1563
            SDL_OutOfMemory();
philipp@10728
  1564
            SDL_JoystickClose(gamecontroller->joystick);
philipp@10728
  1565
            SDL_free(gamecontroller);
slouken@11581
  1566
            SDL_UnlockJoysticks();
philipp@10728
  1567
            return NULL;
philipp@10728
  1568
        }
philipp@10728
  1569
    }
philipp@10728
  1570
    if (gamecontroller->joystick->nhats) {
philipp@10728
  1571
        gamecontroller->last_hat_mask = (Uint8 *)SDL_calloc(gamecontroller->joystick->nhats, sizeof(*gamecontroller->last_hat_mask));
philipp@10728
  1572
        if (!gamecontroller->last_hat_mask) {
philipp@10728
  1573
            SDL_OutOfMemory();
philipp@10728
  1574
            SDL_JoystickClose(gamecontroller->joystick);
philipp@10728
  1575
            SDL_free(gamecontroller->last_match_axis);
philipp@10728
  1576
            SDL_free(gamecontroller);
slouken@11581
  1577
            SDL_UnlockJoysticks();
philipp@10728
  1578
            return NULL;
philipp@10728
  1579
        }
philipp@10728
  1580
    }
slouken@8582
  1581
slouken@10724
  1582
    SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
slouken@10476
  1583
philipp@10813
  1584
    /* Add the controller to list */
slouken@8582
  1585
    ++gamecontroller->ref_count;
philipp@10813
  1586
    /* Link the controller in the list */
slouken@8582
  1587
    gamecontroller->next = SDL_gamecontrollers;
slouken@8582
  1588
    SDL_gamecontrollers = gamecontroller;
slouken@8582
  1589
slouken@11581
  1590
    SDL_UnlockJoysticks();
slouken@8582
  1591
slouken@8582
  1592
    return (gamecontroller);
slouken@8582
  1593
}
slouken@8582
  1594
slouken@8582
  1595
/*
slouken@8582
  1596
 * Manually pump for controller updates.
slouken@8582
  1597
 */
slouken@8582
  1598
void
slouken@8582
  1599
SDL_GameControllerUpdate(void)
slouken@8582
  1600
{
slouken@8582
  1601
    /* Just for API completeness; the joystick API does all the work. */
slouken@8582
  1602
    SDL_JoystickUpdate();
slouken@8582
  1603
}
slouken@8582
  1604
slouken@8582
  1605
/*
slouken@8582
  1606
 * Get the current state of an axis control on a controller
slouken@8582
  1607
 */
slouken@8582
  1608
Sint16
slouken@8582
  1609
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1610
{
slouken@10724
  1611
    int i;
slouken@10724
  1612
slouken@8772
  1613
    if (!gamecontroller)
slouken@8582
  1614
        return 0;
slouken@8582
  1615
slouken@10724
  1616
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1617
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1618
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
slouken@10724
  1619
            int value = 0;
slouken@10724
  1620
            SDL_bool valid_input_range;
slouken@10724
  1621
            SDL_bool valid_output_range;
slouken@10724
  1622
slouken@10724
  1623
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1624
                value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
slouken@10724
  1625
                if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
slouken@10724
  1626
                    valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
slouken@10724
  1627
                } else {
slouken@10724
  1628
                    valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
slouken@10724
  1629
                }
slouken@10724
  1630
                if (valid_input_range) {
slouken@10724
  1631
                    if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
slouken@10724
  1632
                        float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
slouken@10724
  1633
                        value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
slouken@10724
  1634
                    }
slouken@10724
  1635
                }
slouken@10724
  1636
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1637
                value = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
slouken@10724
  1638
                if (value == SDL_PRESSED) {
slouken@10724
  1639
                    value = binding->output.axis.axis_max;
slouken@10724
  1640
                }
slouken@10724
  1641
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1642
                int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
slouken@10724
  1643
                if (hat_mask & binding->input.hat.hat_mask) {
slouken@10724
  1644
                    value = binding->output.axis.axis_max;
slouken@10724
  1645
                }
slouken@10724
  1646
            }
slouken@10724
  1647
slouken@10724
  1648
            if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
slouken@10724
  1649
                valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
slouken@10724
  1650
            } else {
slouken@10724
  1651
                valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
slouken@10724
  1652
            }
slouken@10725
  1653
            /* If the value is zero, there might be another binding that makes it non-zero */
slouken@10724
  1654
            if (value != 0 && valid_output_range) {
slouken@10724
  1655
                return (Sint16)value;
slouken@10724
  1656
            }
slouken@8582
  1657
        }
slouken@8582
  1658
    }
slouken@8582
  1659
    return 0;
slouken@8582
  1660
}
slouken@8582
  1661
slouken@8582
  1662
/*
slouken@8582
  1663
 * Get the current state of a button on a controller
slouken@8582
  1664
 */
slouken@8582
  1665
Uint8
slouken@8582
  1666
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1667
{
slouken@10724
  1668
    int i;
slouken@10724
  1669
slouken@8772
  1670
    if (!gamecontroller)
slouken@8582
  1671
        return 0;
slouken@8582
  1672
slouken@10724
  1673
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1674
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1675
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
slouken@10724
  1676
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1677
                SDL_bool valid_input_range;
slouken@8582
  1678
slouken@10724
  1679
                int value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
slouken@10724
  1680
                int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
slouken@10724
  1681
                if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
slouken@10724
  1682
                    valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
slouken@10724
  1683
                    if (valid_input_range) {
slouken@10724
  1684
                        return (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1685
                    }
slouken@10724
  1686
                } else {
slouken@10724
  1687
                    valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
slouken@10724
  1688
                    if (valid_input_range) {
slouken@10724
  1689
                        return (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1690
                    }
slouken@10724
  1691
                }
slouken@10724
  1692
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1693
                return SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
slouken@10724
  1694
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1695
                int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
slouken@10724
  1696
                return (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1697
            }
slouken@10724
  1698
        }
slouken@8582
  1699
    }
slouken@10724
  1700
    return SDL_RELEASED;
slouken@8582
  1701
}
slouken@8582
  1702
slouken@10595
  1703
const char *
slouken@10595
  1704
SDL_GameControllerName(SDL_GameController * gamecontroller)
slouken@10595
  1705
{
slouken@10595
  1706
    if (!gamecontroller)
slouken@10595
  1707
        return NULL;
slouken@10595
  1708
slouken@11916
  1709
    if (SDL_strcmp(gamecontroller->name, "*") == 0) {
slouken@11916
  1710
        return SDL_JoystickName(SDL_GameControllerGetJoystick(gamecontroller));
slouken@11916
  1711
    } else {
slouken@11916
  1712
        return gamecontroller->name;
slouken@11916
  1713
    }
slouken@10595
  1714
}
slouken@10595
  1715
slouken@10595
  1716
Uint16
slouken@10595
  1717
SDL_GameControllerGetVendor(SDL_GameController * gamecontroller)
slouken@10595
  1718
{
slouken@10595
  1719
    return SDL_JoystickGetVendor(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1720
}
slouken@10595
  1721
slouken@10595
  1722
Uint16
slouken@10595
  1723
SDL_GameControllerGetProduct(SDL_GameController * gamecontroller)
slouken@10595
  1724
{
slouken@10595
  1725
    return SDL_JoystickGetProduct(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1726
}
slouken@10595
  1727
slouken@10595
  1728
Uint16
slouken@10595
  1729
SDL_GameControllerGetProductVersion(SDL_GameController * gamecontroller)
slouken@10595
  1730
{
slouken@10595
  1731
    return SDL_JoystickGetProductVersion(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1732
}
slouken@10595
  1733
slouken@8582
  1734
/*
philipp@10813
  1735
 * Return if the controller in question is currently attached to the system,
slouken@8582
  1736
 *  \return 0 if not plugged in, 1 if still present.
slouken@8582
  1737
 */
slouken@8582
  1738
SDL_bool
slouken@8772
  1739
SDL_GameControllerGetAttached(SDL_GameController * gamecontroller)
slouken@8582
  1740
{
slouken@8772
  1741
    if (!gamecontroller)
slouken@8582
  1742
        return SDL_FALSE;
slouken@8582
  1743
slouken@8582
  1744
    return SDL_JoystickGetAttached(gamecontroller->joystick);
slouken@8582
  1745
}
slouken@8582
  1746
slouken@8582
  1747
/*
slouken@8582
  1748
 * Get the joystick for this controller
slouken@8582
  1749
 */
slouken@8582
  1750
SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
slouken@8582
  1751
{
slouken@8772
  1752
    if (!gamecontroller)
slouken@8582
  1753
        return NULL;
slouken@8582
  1754
slouken@8582
  1755
    return gamecontroller->joystick;
slouken@8582
  1756
}
slouken@8582
  1757
icculus@9916
  1758
icculus@9916
  1759
/*
icculus@9916
  1760
 * Find the SDL_GameController that owns this instance id
icculus@9916
  1761
 */
icculus@9916
  1762
SDL_GameController *
icculus@9916
  1763
SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
icculus@9916
  1764
{
slouken@10688
  1765
    SDL_GameController *gamecontroller;
slouken@10688
  1766
slouken@11581
  1767
    SDL_LockJoysticks();
slouken@10688
  1768
    gamecontroller = SDL_gamecontrollers;
icculus@9916
  1769
    while (gamecontroller) {
icculus@9916
  1770
        if (gamecontroller->joystick->instance_id == joyid) {
slouken@11581
  1771
            SDL_UnlockJoysticks();
icculus@9916
  1772
            return gamecontroller;
icculus@9916
  1773
        }
icculus@9916
  1774
        gamecontroller = gamecontroller->next;
icculus@9916
  1775
    }
slouken@11581
  1776
    SDL_UnlockJoysticks();
icculus@9916
  1777
    return NULL;
icculus@9916
  1778
}
icculus@9916
  1779
icculus@9916
  1780
philipp@10813
  1781
/*
slouken@8582
  1782
 * Get the SDL joystick layer binding for this controller axis mapping
slouken@8582
  1783
 */
slouken@8582
  1784
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1785
{
slouken@10724
  1786
    int i;
slouken@8582
  1787
    SDL_GameControllerButtonBind bind;
slouken@10724
  1788
    SDL_zero(bind);
slouken@8582
  1789
slouken@8772
  1790
    if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
slouken@8582
  1791
        return bind;
slouken@8582
  1792
slouken@10724
  1793
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1794
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1795
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
slouken@10724
  1796
            bind.bindType = binding->inputType;
slouken@10724
  1797
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1798
                /* FIXME: There might be multiple axes bound now that we have axis ranges... */
slouken@10724
  1799
                bind.value.axis = binding->input.axis.axis;
slouken@10724
  1800
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1801
                bind.value.button = binding->input.button;
slouken@10724
  1802
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1803
                bind.value.hat.hat = binding->input.hat.hat;
slouken@10724
  1804
                bind.value.hat.hat_mask = binding->input.hat.hat_mask;
slouken@10724
  1805
            }
slouken@10724
  1806
            break;
slouken@10724
  1807
        }
slouken@8582
  1808
    }
slouken@8582
  1809
    return bind;
slouken@8582
  1810
}
slouken@8582
  1811
slouken@8582
  1812
philipp@10813
  1813
/*
slouken@8582
  1814
 * Get the SDL joystick layer binding for this controller button mapping
slouken@8582
  1815
 */
slouken@8582
  1816
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1817
{
slouken@10724
  1818
    int i;
slouken@8582
  1819
    SDL_GameControllerButtonBind bind;
slouken@10724
  1820
    SDL_zero(bind);
slouken@8582
  1821
slouken@8772
  1822
    if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1823
        return bind;
slouken@8582
  1824
slouken@10724
  1825
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1826
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1827
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
slouken@10724
  1828
            bind.bindType = binding->inputType;
slouken@10724
  1829
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1830
                bind.value.axis = binding->input.axis.axis;
slouken@10724
  1831
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1832
                bind.value.button = binding->input.button;
slouken@10724
  1833
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1834
                bind.value.hat.hat = binding->input.hat.hat;
slouken@10724
  1835
                bind.value.hat.hat_mask = binding->input.hat.hat_mask;
slouken@10724
  1836
            }
slouken@10724
  1837
            break;
slouken@10724
  1838
        }
slouken@8582
  1839
    }
slouken@8582
  1840
    return bind;
slouken@8582
  1841
}
slouken@8582
  1842
slouken@8582
  1843
slouken@12088
  1844
int
slouken@12088
  1845
SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
slouken@12088
  1846
{
slouken@12088
  1847
    return SDL_JoystickRumble(SDL_GameControllerGetJoystick(gamecontroller), low_frequency_rumble, high_frequency_rumble, duration_ms);
slouken@12088
  1848
}
slouken@12088
  1849
slouken@8582
  1850
void
slouken@8582
  1851
SDL_GameControllerClose(SDL_GameController * gamecontroller)
slouken@8582
  1852
{
slouken@8582
  1853
    SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
slouken@8582
  1854
slouken@8772
  1855
    if (!gamecontroller)
slouken@8582
  1856
        return;
slouken@8582
  1857
slouken@11581
  1858
    SDL_LockJoysticks();
slouken@10688
  1859
slouken@8582
  1860
    /* First decrement ref count */
slouken@8582
  1861
    if (--gamecontroller->ref_count > 0) {
slouken@11581
  1862
        SDL_UnlockJoysticks();
slouken@8582
  1863
        return;
slouken@8582
  1864
    }
slouken@8582
  1865
slouken@8772
  1866
    SDL_JoystickClose(gamecontroller->joystick);
slouken@8582
  1867
slouken@8582
  1868
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1869
    gamecontrollerlistprev = NULL;
slouken@8772
  1870
    while (gamecontrollerlist) {
slouken@8772
  1871
        if (gamecontroller == gamecontrollerlist) {
slouken@8772
  1872
            if (gamecontrollerlistprev) {
slouken@8582
  1873
                /* unlink this entry */
slouken@8582
  1874
                gamecontrollerlistprev->next = gamecontrollerlist->next;
slouken@8772
  1875
            } else {
slouken@8582
  1876
                SDL_gamecontrollers = gamecontroller->next;
slouken@8582
  1877
            }
slouken@8582
  1878
            break;
slouken@8582
  1879
        }
slouken@8582
  1880
        gamecontrollerlistprev = gamecontrollerlist;
slouken@8582
  1881
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1882
    }
slouken@8582
  1883
slouken@10724
  1884
    SDL_free(gamecontroller->bindings);
slouken@10724
  1885
    SDL_free(gamecontroller->last_match_axis);
slouken@10724
  1886
    SDL_free(gamecontroller->last_hat_mask);
slouken@8582
  1887
    SDL_free(gamecontroller);
slouken@10688
  1888
slouken@11581
  1889
    SDL_UnlockJoysticks();
slouken@8582
  1890
}
slouken@8582
  1891
slouken@8582
  1892
slouken@8582
  1893
/*
slouken@8582
  1894
 * Quit the controller subsystem
slouken@8582
  1895
 */
slouken@8582
  1896
void
slouken@8582
  1897
SDL_GameControllerQuit(void)
slouken@8582
  1898
{
slouken@11581
  1899
    SDL_LockJoysticks();
slouken@8772
  1900
    while (SDL_gamecontrollers) {
slouken@8582
  1901
        SDL_gamecontrollers->ref_count = 1;
slouken@8582
  1902
        SDL_GameControllerClose(SDL_gamecontrollers);
slouken@8582
  1903
    }
slouken@11581
  1904
    SDL_UnlockJoysticks();
slouken@11201
  1905
}
slouken@11201
  1906
slouken@11201
  1907
void
slouken@11201
  1908
SDL_GameControllerQuitMappings(void)
slouken@11201
  1909
{
slouken@11201
  1910
    ControllerMapping_t *pControllerMap;
slouken@8582
  1911
slouken@8772
  1912
    while (s_pSupportedControllers) {
slouken@8582
  1913
        pControllerMap = s_pSupportedControllers;
slouken@8582
  1914
        s_pSupportedControllers = s_pSupportedControllers->next;
slouken@8772
  1915
        SDL_free(pControllerMap->name);
slouken@8772
  1916
        SDL_free(pControllerMap->mapping);
slouken@8772
  1917
        SDL_free(pControllerMap);
slouken@8582
  1918
    }
slouken@8582
  1919
slouken@8772
  1920
    SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1921
slouken@11201
  1922
    SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
slouken@11201
  1923
                        SDL_GameControllerIgnoreDevicesChanged, NULL);
slouken@11201
  1924
    SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
slouken@11201
  1925
                        SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
slouken@11201
  1926
slouken@11201
  1927
    if (SDL_allowed_controllers.entries) {
slouken@11201
  1928
        SDL_free(SDL_allowed_controllers.entries);
slouken@11201
  1929
        SDL_zero(SDL_allowed_controllers);
slouken@11201
  1930
    }
slouken@11201
  1931
    if (SDL_ignored_controllers.entries) {
slouken@11201
  1932
        SDL_free(SDL_ignored_controllers.entries);
slouken@11201
  1933
        SDL_zero(SDL_ignored_controllers);
slouken@11201
  1934
    }
slouken@8582
  1935
}
slouken@8582
  1936
slouken@8582
  1937
/*
slouken@8582
  1938
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1939
 */
slouken@10724
  1940
static int
slouken@8582
  1941
SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
slouken@8582
  1942
{
slouken@8582
  1943
    int posted;
slouken@8582
  1944
slouken@8582
  1945
    /* translate the event, if desired */
slouken@8582
  1946
    posted = 0;
slouken@8582
  1947
#if !SDL_EVENTS_DISABLED
slouken@8582
  1948
    if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
slouken@8582
  1949
        SDL_Event event;
slouken@8582
  1950
        event.type = SDL_CONTROLLERAXISMOTION;
slouken@8582
  1951
        event.caxis.which = gamecontroller->joystick->instance_id;
slouken@8582
  1952
        event.caxis.axis = axis;
slouken@8582
  1953
        event.caxis.value = value;
slouken@8582
  1954
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  1955
    }
slouken@8582
  1956
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1957
    return (posted);
slouken@8582
  1958
}
slouken@8582
  1959
slouken@8582
  1960
slouken@8582
  1961
/*
slouken@8582
  1962
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1963
 */
slouken@10724
  1964
static int
slouken@8582
  1965
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
slouken@8582
  1966
{
slouken@8582
  1967
    int posted;
slouken@8582
  1968
#if !SDL_EVENTS_DISABLED
slouken@8582
  1969
    SDL_Event event;
slouken@8582
  1970
slouken@8772
  1971
    if (button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1972
        return (0);
slouken@8582
  1973
slouken@8582
  1974
    switch (state) {
slouken@8582
  1975
    case SDL_PRESSED:
slouken@8582
  1976
        event.type = SDL_CONTROLLERBUTTONDOWN;
slouken@8582
  1977
        break;
slouken@8582
  1978
    case SDL_RELEASED:
slouken@8582
  1979
        event.type = SDL_CONTROLLERBUTTONUP;
slouken@8582
  1980
        break;
slouken@8582
  1981
    default:
slouken@8582
  1982
        /* Invalid state -- bail */
slouken@8582
  1983
        return (0);
slouken@8582
  1984
    }
slouken@8582
  1985
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1986
slouken@11934
  1987
    if (button == SDL_CONTROLLER_BUTTON_GUIDE) {
slouken@11934
  1988
        Uint32 now = SDL_GetTicks();
slouken@11934
  1989
        if (state == SDL_PRESSED) {
slouken@11934
  1990
            gamecontroller->guide_button_down = now;
slouken@11934
  1991
slouken@11934
  1992
            if (gamecontroller->joystick->delayed_guide_button) {
slouken@11934
  1993
                /* Skip duplicate press */
slouken@11935
  1994
                return (0);
slouken@11934
  1995
            }
slouken@11934
  1996
        } else {
slouken@11934
  1997
            if (!SDL_TICKS_PASSED(now, gamecontroller->guide_button_down+SDL_MINIMUM_GUIDE_BUTTON_DELAY_MS) && !gamecontroller->joystick->force_recentering) {
slouken@11934
  1998
                gamecontroller->joystick->delayed_guide_button = SDL_TRUE;
slouken@11935
  1999
                return (0);
slouken@11934
  2000
            }
slouken@11934
  2001
            gamecontroller->joystick->delayed_guide_button = SDL_FALSE;
slouken@11934
  2002
        }
slouken@11934
  2003
    }
slouken@11934
  2004
slouken@8582
  2005
    /* translate the event, if desired */
slouken@8582
  2006
    posted = 0;
slouken@8582
  2007
#if !SDL_EVENTS_DISABLED
slouken@8582
  2008
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@8582
  2009
        event.cbutton.which = gamecontroller->joystick->instance_id;
slouken@8582
  2010
        event.cbutton.button = button;
slouken@8582
  2011
        event.cbutton.state = state;
slouken@8582
  2012
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  2013
    }
slouken@8582
  2014
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  2015
    return (posted);
slouken@8582
  2016
}
slouken@8582
  2017
slouken@8582
  2018
/*
slouken@8582
  2019
 * Turn off controller events
slouken@8582
  2020
 */
slouken@8582
  2021
int
slouken@8582
  2022
SDL_GameControllerEventState(int state)
slouken@8582
  2023
{
slouken@8582
  2024
#if SDL_EVENTS_DISABLED
slouken@8582
  2025
    return SDL_IGNORE;
slouken@8582
  2026
#else
slouken@8582
  2027
    const Uint32 event_list[] = {
slouken@8582
  2028
        SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
slouken@8582
  2029
        SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
slouken@8582
  2030
    };
slouken@8582
  2031
    unsigned int i;
slouken@8582
  2032
slouken@8582
  2033
    switch (state) {
slouken@8582
  2034
    case SDL_QUERY:
slouken@8582
  2035
        state = SDL_IGNORE;
slouken@8582
  2036
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  2037
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@8582
  2038
            if (state == SDL_ENABLE) {
slouken@8582
  2039
                break;
slouken@8582
  2040
            }
slouken@8582
  2041
        }
slouken@8582
  2042
        break;
slouken@8582
  2043
    default:
slouken@8582
  2044
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  2045
            SDL_EventState(event_list[i], state);
slouken@8582
  2046
        }
slouken@8582
  2047
        break;
slouken@8582
  2048
    }
slouken@8582
  2049
    return (state);
slouken@8582
  2050
#endif /* SDL_EVENTS_DISABLED */
slouken@8582
  2051
}
slouken@8582
  2052
slouken@11934
  2053
void
slouken@11934
  2054
SDL_GameControllerHandleDelayedGuideButton(SDL_Joystick *joystick)
slouken@11934
  2055
{
slouken@11934
  2056
    SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@11934
  2057
    while (controllerlist) {
slouken@11934
  2058
        if (controllerlist->joystick == joystick) {
slouken@11934
  2059
            SDL_PrivateGameControllerButton(controllerlist, SDL_CONTROLLER_BUTTON_GUIDE, SDL_RELEASED);
slouken@11934
  2060
            break;
slouken@11934
  2061
        }
slouken@11934
  2062
        controllerlist = controllerlist->next;
slouken@11934
  2063
    }
slouken@11934
  2064
}
slouken@11934
  2065
slouken@8582
  2066
/* vi: set ts=4 sw=4 expandtab: */