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