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