src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 01 Nov 2017 10:06:58 -0700
changeset 11663 0d96acbd34f0
parent 11581 49393a9dffe4
child 11664 c677bf234773
permissions -rw-r--r--
Added controller mapping for Android TV remotes
Also fixed the back button on the remote exiting the application
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@11581
    27
#include "SDL_hints.h"
slouken@8582
    28
#include "SDL_sysjoystick.h"
slouken@10688
    29
#include "SDL_joystick_c.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@11579
   909
    const char *name;
slouken@11579
   910
    SDL_JoystickGUID guid;
slouken@11579
   911
    ControllerMapping_t *mapping;
slouken@11579
   912
slouken@11581
   913
    SDL_LockJoysticks();
slouken@11579
   914
    name = SDL_JoystickNameForIndex(device_index);
slouken@11579
   915
    guid = SDL_JoystickGetDeviceGUID(device_index);
slouken@11579
   916
    mapping = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
slouken@9956
   917
#if SDL_JOYSTICK_XINPUT
slouken@9956
   918
    if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
slouken@9956
   919
        mapping = s_pXInputMapping;
slouken@9956
   920
    }
slouken@9956
   921
#endif
slouken@11663
   922
#if defined(__ANDROID__)
slouken@11663
   923
    if (!mapping && SDL_SYS_IsDPAD_DeviceIndex(device_index)) {
slouken@11663
   924
        SDL_bool existing;
slouken@11663
   925
        char mapping_string[1024];
slouken@11663
   926
        SDL_snprintf(mapping_string, sizeof(mapping_string), "none,%s,a:b0,b:b1,dpdown:b12,dpleft:b13,dpright:b14,dpup:b11,", name);
slouken@11663
   927
        mapping = SDL_PrivateAddMappingForGUID(guid, mapping_string,
slouken@11663
   928
                          &existing, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@11663
   929
#endif /* __ANDROID__ */
slouken@11663
   930
    }
slouken@11581
   931
    SDL_UnlockJoysticks();
slouken@9956
   932
    return mapping;
slouken@9956
   933
}
slouken@9956
   934
slouken@9956
   935
/*
slouken@8582
   936
 * Add or update an entry into the Mappings Database
slouken@8582
   937
 */
slouken@8582
   938
int
slouken@8772
   939
SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
gabomdq@8042
   940
{
gabomdq@8042
   941
    const char *platform = SDL_GetPlatform();
gabomdq@8042
   942
    int controllers = 0;
gabomdq@8042
   943
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
gabomdq@8042
   944
    size_t db_size, platform_len;
gabomdq@8042
   945
    
gabomdq@8042
   946
    if (rw == NULL) {
gabomdq@8046
   947
        return SDL_SetError("Invalid RWops");
gabomdq@8042
   948
    }
slouken@8052
   949
    db_size = (size_t)SDL_RWsize(rw);
gabomdq@8042
   950
    
slouken@8052
   951
    buf = (char *)SDL_malloc(db_size + 1);
gabomdq@8042
   952
    if (buf == NULL) {
gabomdq@8046
   953
        if (freerw) {
gabomdq@8046
   954
            SDL_RWclose(rw);
gabomdq@8046
   955
        }
philipp@9194
   956
        return SDL_SetError("Could not allocate space to read DB into memory");
gabomdq@8042
   957
    }
gabomdq@8042
   958
    
gabomdq@8042
   959
    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
gabomdq@8046
   960
        if (freerw) {
gabomdq@8046
   961
            SDL_RWclose(rw);
gabomdq@8046
   962
        }
gabomdq@8042
   963
        SDL_free(buf);
gabomdq@8042
   964
        return SDL_SetError("Could not read DB");
gabomdq@8042
   965
    }
gabomdq@8046
   966
    
gabomdq@8046
   967
    if (freerw) {
gabomdq@8046
   968
        SDL_RWclose(rw);
gabomdq@8046
   969
    }
gabomdq@8042
   970
    
gabomdq@8042
   971
    buf[db_size] = '\0';
gabomdq@8042
   972
    line = buf;
gabomdq@8042
   973
    
gabomdq@8042
   974
    while (line < buf + db_size) {
slouken@8772
   975
        line_end = SDL_strchr(line, '\n');
gabomdq@8042
   976
        if (line_end != NULL) {
gabomdq@8042
   977
            *line_end = '\0';
slouken@8772
   978
        } else {
gabomdq@8042
   979
            line_end = buf + db_size;
gabomdq@8042
   980
        }
gabomdq@8042
   981
        
gabomdq@8042
   982
        /* Extract and verify the platform */
gabomdq@8042
   983
        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
slouken@8772
   984
        if (tmp != NULL) {
gabomdq@8042
   985
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
gabomdq@8042
   986
            comma = SDL_strchr(tmp, ',');
gabomdq@8042
   987
            if (comma != NULL) {
gabomdq@8042
   988
                platform_len = comma - tmp + 1;
gabomdq@8042
   989
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
gabomdq@8042
   990
                    SDL_strlcpy(line_platform, tmp, platform_len);
slouken@8772
   991
                    if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
slouken@8772
   992
                        SDL_GameControllerAddMapping(line) > 0) {
gabomdq@8042
   993
                        controllers++;
gabomdq@8042
   994
                    }
gabomdq@8042
   995
                }
gabomdq@8042
   996
            }
gabomdq@8042
   997
        }
gabomdq@8042
   998
        
gabomdq@8042
   999
        line = line_end + 1;
gabomdq@8042
  1000
    }
gabomdq@8042
  1001
gabomdq@8042
  1002
    SDL_free(buf);
gabomdq@8042
  1003
    return controllers;
gabomdq@8042
  1004
}
gabomdq@8042
  1005
gabomdq@8042
  1006
/*
slouken@10605
  1007
 * Add or update an entry into the Mappings Database with a priority
gabomdq@8042
  1008
 */
slouken@10605
  1009
static int
slouken@10605
  1010
SDL_PrivateGameControllerAddMapping(const char *mappingString, SDL_ControllerMappingPriority priority)
slouken@8582
  1011
{
slouken@8582
  1012
    char *pchGUID;
slouken@8582
  1013
    SDL_JoystickGUID jGUID;
slouken@8582
  1014
    SDL_bool is_xinput_mapping = SDL_FALSE;
icculus@9278
  1015
    SDL_bool is_emscripten_mapping = SDL_FALSE;
slouken@9956
  1016
    SDL_bool existing = SDL_FALSE;
slouken@9956
  1017
    ControllerMapping_t *pControllerMapping;
slouken@8582
  1018
philipp@9198
  1019
    if (!mappingString) {
philipp@9198
  1020
        return SDL_InvalidParamError("mappingString");
philipp@9198
  1021
    }
philipp@9198
  1022
slouken@8772
  1023
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
slouken@8582
  1024
    if (!pchGUID) {
philipp@8081
  1025
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
slouken@8582
  1026
    }
slouken@8772
  1027
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
slouken@8582
  1028
        is_xinput_mapping = SDL_TRUE;
slouken@8582
  1029
    }
icculus@9278
  1030
    if (!SDL_strcasecmp(pchGUID, "emscripten")) {
icculus@9278
  1031
        is_emscripten_mapping = SDL_TRUE;
icculus@9278
  1032
    }
slouken@8582
  1033
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
slouken@8582
  1034
    SDL_free(pchGUID);
slouken@8582
  1035
slouken@10605
  1036
    pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing, priority);
slouken@9956
  1037
    if (!pControllerMapping) {
slouken@9956
  1038
        return -1;
slouken@8053
  1039
    }
slouken@8582
  1040
slouken@9956
  1041
    if (existing) {
slouken@8582
  1042
        return 0;
slouken@8582
  1043
    } else {
slouken@8772
  1044
        if (is_xinput_mapping) {
slouken@8582
  1045
            s_pXInputMapping = pControllerMapping;
slouken@8582
  1046
        }
icculus@9278
  1047
        if (is_emscripten_mapping) {
icculus@9278
  1048
            s_pEmscriptenMapping = pControllerMapping;
icculus@9278
  1049
        }
slouken@8582
  1050
        return 1;
slouken@8582
  1051
    }
slouken@8582
  1052
}
slouken@8582
  1053
slouken@8582
  1054
/*
slouken@10605
  1055
 * Add or update an entry into the Mappings Database
slouken@10605
  1056
 */
slouken@10605
  1057
int
slouken@10605
  1058
SDL_GameControllerAddMapping(const char *mappingString)
slouken@10605
  1059
{
slouken@10605
  1060
    return SDL_PrivateGameControllerAddMapping(mappingString, SDL_CONTROLLER_MAPPING_PRIORITY_API);
slouken@10605
  1061
}
slouken@10605
  1062
philipp@10813
  1063
/*
slouken@10661
  1064
 *  Get the number of mappings installed
slouken@10661
  1065
 */
slouken@10661
  1066
int
slouken@10721
  1067
SDL_GameControllerNumMappings(void)
slouken@10661
  1068
{
slouken@10661
  1069
    int num_mappings = 0;
slouken@10661
  1070
    ControllerMapping_t *mapping;
slouken@10661
  1071
slouken@10661
  1072
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
  1073
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
  1074
            continue;
slouken@10661
  1075
        }
slouken@10661
  1076
        ++num_mappings;
slouken@10661
  1077
    }
slouken@10661
  1078
    return num_mappings;
slouken@10661
  1079
}
slouken@10661
  1080
philipp@10813
  1081
/*
slouken@10661
  1082
 *  Get the mapping at a particular index.
slouken@10661
  1083
 */
slouken@10661
  1084
char *
slouken@10661
  1085
SDL_GameControllerMappingForIndex(int mapping_index)
slouken@10661
  1086
{
slouken@10661
  1087
    ControllerMapping_t *mapping;
slouken@10661
  1088
slouken@10661
  1089
    for (mapping = s_pSupportedControllers; mapping; mapping = mapping->next) {
slouken@10661
  1090
        if (SDL_memcmp(&mapping->guid, &s_zeroGUID, sizeof(mapping->guid)) == 0) {
slouken@10661
  1091
            continue;
slouken@10661
  1092
        }
slouken@10661
  1093
        if (mapping_index == 0) {
slouken@10661
  1094
            char *pMappingString;
slouken@10661
  1095
            char pchGUID[33];
slouken@10661
  1096
            size_t needed;
slouken@10661
  1097
slouken@10661
  1098
            SDL_JoystickGetGUIDString(mapping->guid, pchGUID, sizeof(pchGUID));
slouken@10661
  1099
            /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@10661
  1100
            needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@10661
  1101
            pMappingString = SDL_malloc(needed);
slouken@10661
  1102
            if (!pMappingString) {
slouken@10661
  1103
                SDL_OutOfMemory();
slouken@10661
  1104
                return NULL;
slouken@10661
  1105
            }
slouken@10661
  1106
            SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@10661
  1107
            return pMappingString;
slouken@10661
  1108
        }
slouken@10661
  1109
        --mapping_index;
slouken@10661
  1110
    }
slouken@10661
  1111
    return NULL;
slouken@10661
  1112
}
slouken@10661
  1113
slouken@10605
  1114
/*
slouken@8582
  1115
 * Get the mapping string for this GUID
slouken@8582
  1116
 */
slouken@8582
  1117
char *
slouken@8772
  1118
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
slouken@8582
  1119
{
slouken@8582
  1120
    char *pMappingString = NULL;
slouken@8582
  1121
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@8582
  1122
    if (mapping) {
slouken@8582
  1123
        char pchGUID[33];
slouken@8582
  1124
        size_t needed;
slouken@8582
  1125
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@8582
  1126
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@8582
  1127
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@8772
  1128
        pMappingString = SDL_malloc(needed);
philipp@9197
  1129
        if (!pMappingString) {
philipp@9197
  1130
            SDL_OutOfMemory();
philipp@9197
  1131
            return NULL;
philipp@9197
  1132
        }
slouken@8772
  1133
        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
slouken@8582
  1134
    }
slouken@8582
  1135
    return pMappingString;
slouken@8582
  1136
}
slouken@8582
  1137
slouken@8582
  1138
/*
slouken@8582
  1139
 * Get the mapping string for this device
slouken@8582
  1140
 */
slouken@8582
  1141
char *
slouken@8772
  1142
SDL_GameControllerMapping(SDL_GameController * gamecontroller)
slouken@8582
  1143
{
philipp@9196
  1144
    if (!gamecontroller) {
philipp@9196
  1145
        return NULL;
philipp@9196
  1146
    }
philipp@9196
  1147
slouken@10724
  1148
    return SDL_GameControllerMappingForGUID(gamecontroller->guid);
slouken@8582
  1149
}
slouken@8582
  1150
slouken@8582
  1151
static void
slouken@8582
  1152
SDL_GameControllerLoadHints()
slouken@8582
  1153
{
slouken@8582
  1154
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
slouken@8772
  1155
    if (hint && hint[0]) {
slouken@8772
  1156
        size_t nchHints = SDL_strlen(hint);
slouken@8772
  1157
        char *pUserMappings = SDL_malloc(nchHints + 1);
slouken@8582
  1158
        char *pTempMappings = pUserMappings;
slouken@8772
  1159
        SDL_memcpy(pUserMappings, hint, nchHints);
slouken@8267
  1160
        pUserMappings[nchHints] = '\0';
slouken@8772
  1161
        while (pUserMappings) {
slouken@8582
  1162
            char *pchNewLine = NULL;
slouken@8582
  1163
slouken@8772
  1164
            pchNewLine = SDL_strchr(pUserMappings, '\n');
slouken@8772
  1165
            if (pchNewLine)
slouken@8582
  1166
                *pchNewLine = '\0';
slouken@8582
  1167
slouken@10605
  1168
            SDL_PrivateGameControllerAddMapping(pUserMappings, SDL_CONTROLLER_MAPPING_PRIORITY_USER);
slouken@8582
  1169
slouken@8772
  1170
            if (pchNewLine) {
slouken@8582
  1171
                pUserMappings = pchNewLine + 1;
slouken@8772
  1172
            } else {
slouken@8582
  1173
                pUserMappings = NULL;
slouken@8772
  1174
            }
slouken@8582
  1175
        }
slouken@8582
  1176
        SDL_free(pTempMappings);
slouken@8582
  1177
    }
slouken@8582
  1178
}
slouken@8582
  1179
slouken@8582
  1180
/*
slouken@11532
  1181
 * Fill the given buffer with the expected controller mapping filepath. 
slouken@11532
  1182
 * Usually this will just be CONTROLLER_MAPPING_FILE, but for Android,
slouken@11532
  1183
 * we want to get the internal storage path.
slouken@11532
  1184
 */
slouken@11532
  1185
static SDL_bool SDL_GetControllerMappingFilePath(char *path, size_t size)
slouken@11532
  1186
{
slouken@11532
  1187
#ifdef CONTROLLER_MAPPING_FILE
slouken@11532
  1188
#define STRING(X) SDL_STRINGIFY_ARG(X)
slouken@11532
  1189
    return SDL_strlcpy(path, STRING(CONTROLLER_MAPPING_FILE), size) < size;
slouken@11532
  1190
#elif defined(__ANDROID__)
slouken@11532
  1191
    return SDL_snprintf(path, size, "%s/controller_map.txt", SDL_AndroidGetInternalStoragePath()) < size;
slouken@11532
  1192
#else
slouken@11532
  1193
    return SDL_FALSE;
slouken@11532
  1194
#endif
slouken@11532
  1195
}
slouken@11532
  1196
slouken@11532
  1197
/*
slouken@8582
  1198
 * Initialize the game controller system, mostly load our DB of controller config mappings
slouken@8582
  1199
 */
slouken@8582
  1200
int
slouken@11201
  1201
SDL_GameControllerInitMappings(void)
slouken@8582
  1202
{
slouken@11532
  1203
    char szControllerMapPath[1024];
slouken@8582
  1204
    int i = 0;
slouken@8582
  1205
    const char *pMappingString = NULL;
slouken@8582
  1206
    pMappingString = s_ControllerMappings[i];
slouken@8772
  1207
    while (pMappingString) {
slouken@10605
  1208
        SDL_PrivateGameControllerAddMapping(pMappingString, SDL_CONTROLLER_MAPPING_PRIORITY_DEFAULT);
slouken@8582
  1209
slouken@8582
  1210
        i++;
slouken@8582
  1211
        pMappingString = s_ControllerMappings[i];
slouken@8582
  1212
    }
slouken@8582
  1213
slouken@11532
  1214
    if (SDL_GetControllerMappingFilePath(szControllerMapPath, sizeof(szControllerMapPath))) {
slouken@11532
  1215
        SDL_GameControllerAddMappingsFromFile(szControllerMapPath);        
slouken@11532
  1216
    }
slouken@11532
  1217
slouken@8582
  1218
    /* load in any user supplied config */
slouken@8582
  1219
    SDL_GameControllerLoadHints();
slouken@8582
  1220
slouken@11201
  1221
    SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
slouken@11201
  1222
                        SDL_GameControllerIgnoreDevicesChanged, NULL);
slouken@11201
  1223
    SDL_AddHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
slouken@11201
  1224
                        SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
slouken@11201
  1225
slouken@11201
  1226
    return (0);
slouken@11201
  1227
}
slouken@11201
  1228
slouken@11201
  1229
int
slouken@11201
  1230
SDL_GameControllerInit(void)
slouken@11201
  1231
{
slouken@11201
  1232
    int i;
slouken@11201
  1233
slouken@8582
  1234
    /* watch for joy events and fire controller ones if needed */
slouken@8772
  1235
    SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1236
slouken@8067
  1237
    /* Send added events for controllers currently attached */
slouken@8067
  1238
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
slouken@8067
  1239
        if (SDL_IsGameController(i)) {
slouken@8067
  1240
            SDL_Event deviceevent;
slouken@8067
  1241
            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@8067
  1242
            deviceevent.cdevice.which = i;
slouken@8067
  1243
            SDL_PushEvent(&deviceevent);
slouken@8067
  1244
        }
slouken@8067
  1245
    }
slouken@8067
  1246
slouken@8582
  1247
    return (0);
slouken@8582
  1248
}
slouken@8582
  1249
slouken@8582
  1250
slouken@8582
  1251
/*
slouken@8582
  1252
 * Get the implementation dependent name of a controller
slouken@8582
  1253
 */
slouken@8582
  1254
const char *
slouken@8582
  1255
SDL_GameControllerNameForIndex(int device_index)
slouken@8582
  1256
{
slouken@10724
  1257
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1258
    if (pSupportedController) {
slouken@8582
  1259
        return pSupportedController->name;
slouken@8582
  1260
    }
slouken@8582
  1261
    return NULL;
slouken@8582
  1262
}
slouken@8582
  1263
slouken@8582
  1264
slouken@8582
  1265
/*
slouken@11201
  1266
 * Return 1 if the joystick with this name and GUID is a supported controller
slouken@11201
  1267
 */
slouken@11201
  1268
SDL_bool
slouken@11201
  1269
SDL_IsGameControllerNameAndGUID(const char *name, SDL_JoystickGUID guid)
slouken@11201
  1270
{
slouken@11201
  1271
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMappingForNameAndGUID(name, guid);
slouken@11201
  1272
    if (pSupportedController) {
slouken@11201
  1273
        return SDL_TRUE;
slouken@11201
  1274
    }
slouken@11201
  1275
    return SDL_FALSE;
slouken@11201
  1276
}
slouken@11201
  1277
slouken@11201
  1278
/*
slouken@8582
  1279
 * Return 1 if the joystick at this device index is a supported controller
slouken@8582
  1280
 */
slouken@8582
  1281
SDL_bool
slouken@8582
  1282
SDL_IsGameController(int device_index)
slouken@8582
  1283
{
slouken@10724
  1284
    ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1285
    if (pSupportedController) {
slouken@8582
  1286
        return SDL_TRUE;
slouken@8582
  1287
    }
slouken@8582
  1288
    return SDL_FALSE;
slouken@8582
  1289
}
slouken@8582
  1290
slouken@8582
  1291
/*
slouken@11201
  1292
 * Return 1 if the game controller should be ignored by SDL
slouken@11201
  1293
 */
slouken@11201
  1294
SDL_bool SDL_ShouldIgnoreGameController(const char *name, SDL_JoystickGUID guid)
slouken@11201
  1295
{
slouken@11201
  1296
    int i;
slouken@11201
  1297
    Uint16 vendor;
slouken@11201
  1298
    Uint16 product;
slouken@11201
  1299
    Uint32 vidpid;
slouken@11201
  1300
slouken@11201
  1301
    if (SDL_allowed_controllers.num_entries == 0 &&
slouken@11201
  1302
        SDL_ignored_controllers.num_entries == 0) {
slouken@11201
  1303
        return SDL_FALSE;
slouken@11201
  1304
    }
slouken@11201
  1305
slouken@11201
  1306
    SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
slouken@11201
  1307
    vidpid = MAKE_VIDPID(vendor, product);
slouken@11201
  1308
slouken@11203
  1309
    if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", SDL_FALSE)) {
slouken@11203
  1310
        /* 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
  1311
        SDL_bool bSteamVirtualGamepad = SDL_FALSE;
slouken@11203
  1312
#if defined(__LINUX__)
slouken@11203
  1313
        bSteamVirtualGamepad = (vendor == 0x28DE && product == 0x11FF);
slouken@11203
  1314
#elif defined(__MACOSX__)
slouken@11203
  1315
        bSteamVirtualGamepad = (SDL_strncmp(name, "GamePad-", 8) == 0);
slouken@11203
  1316
#elif defined(__WIN32__)
slouken@11203
  1317
        /* We can't tell on Windows, but Steam will block others in input hooks */
slouken@11203
  1318
        bSteamVirtualGamepad = SDL_TRUE;
slouken@11203
  1319
#endif
slouken@11203
  1320
        if (bSteamVirtualGamepad) {
slouken@11203
  1321
            return SDL_FALSE;
slouken@11203
  1322
        }
slouken@11203
  1323
    }
slouken@11203
  1324
slouken@11201
  1325
    if (SDL_allowed_controllers.num_entries > 0) {
slouken@11201
  1326
        for (i = 0; i < SDL_allowed_controllers.num_entries; ++i) {
slouken@11201
  1327
            if (vidpid == SDL_allowed_controllers.entries[i]) {
slouken@11201
  1328
                return SDL_FALSE;
slouken@11201
  1329
            }
slouken@11201
  1330
        }
slouken@11201
  1331
        return SDL_TRUE;
slouken@11201
  1332
    } else {
slouken@11201
  1333
        for (i = 0; i < SDL_ignored_controllers.num_entries; ++i) {
slouken@11201
  1334
            if (vidpid == SDL_ignored_controllers.entries[i]) {
slouken@11201
  1335
                return SDL_TRUE;
slouken@11201
  1336
            }
slouken@11201
  1337
        }
slouken@11201
  1338
        return SDL_FALSE;
slouken@11201
  1339
    }
slouken@11201
  1340
}
slouken@11201
  1341
slouken@11201
  1342
/*
slouken@8582
  1343
 * Open a controller for use - the index passed as an argument refers to
slouken@8582
  1344
 * the N'th controller on the system.  This index is the value which will
slouken@8582
  1345
 * identify this controller in future controller events.
slouken@8582
  1346
 *
slouken@8582
  1347
 * This function returns a controller identifier, or NULL if an error occurred.
slouken@8582
  1348
 */
slouken@8582
  1349
SDL_GameController *
slouken@8582
  1350
SDL_GameControllerOpen(int device_index)
slouken@8582
  1351
{
slouken@8582
  1352
    SDL_GameController *gamecontroller;
slouken@8582
  1353
    SDL_GameController *gamecontrollerlist;
slouken@8582
  1354
    ControllerMapping_t *pSupportedController = NULL;
slouken@8582
  1355
slouken@8582
  1356
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@8582
  1357
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@8582
  1358
        return (NULL);
slouken@8582
  1359
    }
slouken@8582
  1360
slouken@11581
  1361
    SDL_LockJoysticks();
slouken@10688
  1362
slouken@8582
  1363
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1364
    /* If the controller is already open, return it */
slouken@8772
  1365
    while (gamecontrollerlist) {
slouken@8772
  1366
        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
slouken@8582
  1367
                gamecontroller = gamecontrollerlist;
slouken@8582
  1368
                ++gamecontroller->ref_count;
slouken@11581
  1369
                SDL_UnlockJoysticks();
slouken@8582
  1370
                return (gamecontroller);
slouken@8582
  1371
        }
slouken@8582
  1372
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1373
    }
slouken@8582
  1374
slouken@8582
  1375
    /* Find a controller mapping */
slouken@8582
  1376
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@8772
  1377
    if (!pSupportedController) {
slouken@8772
  1378
        SDL_SetError("Couldn't find mapping for device (%d)", device_index);
slouken@11581
  1379
        SDL_UnlockJoysticks();
slouken@10688
  1380
        return NULL;
slouken@8582
  1381
    }
slouken@8582
  1382
philipp@10813
  1383
    /* Create and initialize the controller */
slouken@10724
  1384
    gamecontroller = (SDL_GameController *) SDL_calloc(1, sizeof(*gamecontroller));
slouken@8582
  1385
    if (gamecontroller == NULL) {
slouken@8582
  1386
        SDL_OutOfMemory();
slouken@11581
  1387
        SDL_UnlockJoysticks();
slouken@8582
  1388
        return NULL;
slouken@8582
  1389
    }
slouken@8582
  1390
slouken@8582
  1391
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
slouken@8772
  1392
    if (!gamecontroller->joystick) {
slouken@8582
  1393
        SDL_free(gamecontroller);
slouken@11581
  1394
        SDL_UnlockJoysticks();
slouken@8582
  1395
        return NULL;
slouken@8582
  1396
    }
slouken@8582
  1397
philipp@10728
  1398
    if (gamecontroller->joystick->naxes) {
philipp@10728
  1399
        gamecontroller->last_match_axis = (SDL_ExtendedGameControllerBind **)SDL_calloc(gamecontroller->joystick->naxes, sizeof(*gamecontroller->last_match_axis));
philipp@10728
  1400
        if (!gamecontroller->last_match_axis) {
philipp@10728
  1401
            SDL_OutOfMemory();
philipp@10728
  1402
            SDL_JoystickClose(gamecontroller->joystick);
philipp@10728
  1403
            SDL_free(gamecontroller);
slouken@11581
  1404
            SDL_UnlockJoysticks();
philipp@10728
  1405
            return NULL;
philipp@10728
  1406
        }
philipp@10728
  1407
    }
philipp@10728
  1408
    if (gamecontroller->joystick->nhats) {
philipp@10728
  1409
        gamecontroller->last_hat_mask = (Uint8 *)SDL_calloc(gamecontroller->joystick->nhats, sizeof(*gamecontroller->last_hat_mask));
philipp@10728
  1410
        if (!gamecontroller->last_hat_mask) {
philipp@10728
  1411
            SDL_OutOfMemory();
philipp@10728
  1412
            SDL_JoystickClose(gamecontroller->joystick);
philipp@10728
  1413
            SDL_free(gamecontroller->last_match_axis);
philipp@10728
  1414
            SDL_free(gamecontroller);
slouken@11581
  1415
            SDL_UnlockJoysticks();
philipp@10728
  1416
            return NULL;
philipp@10728
  1417
        }
philipp@10728
  1418
    }
slouken@8582
  1419
slouken@10724
  1420
    SDL_PrivateLoadButtonMapping(gamecontroller, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
slouken@10476
  1421
philipp@10813
  1422
    /* Add the controller to list */
slouken@8582
  1423
    ++gamecontroller->ref_count;
philipp@10813
  1424
    /* Link the controller in the list */
slouken@8582
  1425
    gamecontroller->next = SDL_gamecontrollers;
slouken@8582
  1426
    SDL_gamecontrollers = gamecontroller;
slouken@8582
  1427
slouken@11581
  1428
    SDL_UnlockJoysticks();
slouken@8582
  1429
slouken@8582
  1430
    return (gamecontroller);
slouken@8582
  1431
}
slouken@8582
  1432
slouken@8582
  1433
/*
slouken@8582
  1434
 * Manually pump for controller updates.
slouken@8582
  1435
 */
slouken@8582
  1436
void
slouken@8582
  1437
SDL_GameControllerUpdate(void)
slouken@8582
  1438
{
slouken@8582
  1439
    /* Just for API completeness; the joystick API does all the work. */
slouken@8582
  1440
    SDL_JoystickUpdate();
slouken@8582
  1441
}
slouken@8582
  1442
slouken@8582
  1443
/*
slouken@8582
  1444
 * Get the current state of an axis control on a controller
slouken@8582
  1445
 */
slouken@8582
  1446
Sint16
slouken@8582
  1447
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1448
{
slouken@10724
  1449
    int i;
slouken@10724
  1450
slouken@8772
  1451
    if (!gamecontroller)
slouken@8582
  1452
        return 0;
slouken@8582
  1453
slouken@10724
  1454
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1455
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1456
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
slouken@10724
  1457
            int value = 0;
slouken@10724
  1458
            SDL_bool valid_input_range;
slouken@10724
  1459
            SDL_bool valid_output_range;
slouken@10724
  1460
slouken@10724
  1461
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1462
                value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
slouken@10724
  1463
                if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
slouken@10724
  1464
                    valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
slouken@10724
  1465
                } else {
slouken@10724
  1466
                    valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
slouken@10724
  1467
                }
slouken@10724
  1468
                if (valid_input_range) {
slouken@10724
  1469
                    if (binding->input.axis.axis_min != binding->output.axis.axis_min || binding->input.axis.axis_max != binding->output.axis.axis_max) {
slouken@10724
  1470
                        float normalized_value = (float)(value - binding->input.axis.axis_min) / (binding->input.axis.axis_max - binding->input.axis.axis_min);
slouken@10724
  1471
                        value = binding->output.axis.axis_min + (int)(normalized_value * (binding->output.axis.axis_max - binding->output.axis.axis_min));
slouken@10724
  1472
                    }
slouken@10724
  1473
                }
slouken@10724
  1474
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1475
                value = SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
slouken@10724
  1476
                if (value == SDL_PRESSED) {
slouken@10724
  1477
                    value = binding->output.axis.axis_max;
slouken@10724
  1478
                }
slouken@10724
  1479
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1480
                int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
slouken@10724
  1481
                if (hat_mask & binding->input.hat.hat_mask) {
slouken@10724
  1482
                    value = binding->output.axis.axis_max;
slouken@10724
  1483
                }
slouken@10724
  1484
            }
slouken@10724
  1485
slouken@10724
  1486
            if (binding->output.axis.axis_min < binding->output.axis.axis_max) {
slouken@10724
  1487
                valid_output_range = (value >= binding->output.axis.axis_min && value <= binding->output.axis.axis_max);
slouken@10724
  1488
            } else {
slouken@10724
  1489
                valid_output_range = (value >= binding->output.axis.axis_max && value <= binding->output.axis.axis_min);
slouken@10724
  1490
            }
slouken@10725
  1491
            /* If the value is zero, there might be another binding that makes it non-zero */
slouken@10724
  1492
            if (value != 0 && valid_output_range) {
slouken@10724
  1493
                return (Sint16)value;
slouken@10724
  1494
            }
slouken@8582
  1495
        }
slouken@8582
  1496
    }
slouken@8582
  1497
    return 0;
slouken@8582
  1498
}
slouken@8582
  1499
slouken@8582
  1500
/*
slouken@8582
  1501
 * Get the current state of a button on a controller
slouken@8582
  1502
 */
slouken@8582
  1503
Uint8
slouken@8582
  1504
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1505
{
slouken@10724
  1506
    int i;
slouken@10724
  1507
slouken@8772
  1508
    if (!gamecontroller)
slouken@8582
  1509
        return 0;
slouken@8582
  1510
slouken@10724
  1511
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1512
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1513
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
slouken@10724
  1514
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1515
                SDL_bool valid_input_range;
slouken@8582
  1516
slouken@10724
  1517
                int value = SDL_JoystickGetAxis(gamecontroller->joystick, binding->input.axis.axis);
slouken@10724
  1518
                int threshold = binding->input.axis.axis_min + (binding->input.axis.axis_max - binding->input.axis.axis_min) / 2;
slouken@10724
  1519
                if (binding->input.axis.axis_min < binding->input.axis.axis_max) {
slouken@10724
  1520
                    valid_input_range = (value >= binding->input.axis.axis_min && value <= binding->input.axis.axis_max);
slouken@10724
  1521
                    if (valid_input_range) {
slouken@10724
  1522
                        return (value >= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1523
                    }
slouken@10724
  1524
                } else {
slouken@10724
  1525
                    valid_input_range = (value >= binding->input.axis.axis_max && value <= binding->input.axis.axis_min);
slouken@10724
  1526
                    if (valid_input_range) {
slouken@10724
  1527
                        return (value <= threshold) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1528
                    }
slouken@10724
  1529
                }
slouken@10724
  1530
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1531
                return SDL_JoystickGetButton(gamecontroller->joystick, binding->input.button);
slouken@10724
  1532
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1533
                int hat_mask = SDL_JoystickGetHat(gamecontroller->joystick, binding->input.hat.hat);
slouken@10724
  1534
                return (hat_mask & binding->input.hat.hat_mask) ? SDL_PRESSED : SDL_RELEASED;
slouken@10724
  1535
            }
slouken@10724
  1536
        }
slouken@8582
  1537
    }
slouken@10724
  1538
    return SDL_RELEASED;
slouken@8582
  1539
}
slouken@8582
  1540
slouken@10595
  1541
const char *
slouken@10595
  1542
SDL_GameControllerName(SDL_GameController * gamecontroller)
slouken@10595
  1543
{
slouken@10595
  1544
    if (!gamecontroller)
slouken@10595
  1545
        return NULL;
slouken@10595
  1546
slouken@10724
  1547
    return gamecontroller->name;
slouken@10595
  1548
}
slouken@10595
  1549
slouken@10595
  1550
Uint16
slouken@10595
  1551
SDL_GameControllerGetVendor(SDL_GameController * gamecontroller)
slouken@10595
  1552
{
slouken@10595
  1553
    return SDL_JoystickGetVendor(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1554
}
slouken@10595
  1555
slouken@10595
  1556
Uint16
slouken@10595
  1557
SDL_GameControllerGetProduct(SDL_GameController * gamecontroller)
slouken@10595
  1558
{
slouken@10595
  1559
    return SDL_JoystickGetProduct(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1560
}
slouken@10595
  1561
slouken@10595
  1562
Uint16
slouken@10595
  1563
SDL_GameControllerGetProductVersion(SDL_GameController * gamecontroller)
slouken@10595
  1564
{
slouken@10595
  1565
    return SDL_JoystickGetProductVersion(SDL_GameControllerGetJoystick(gamecontroller));
slouken@10595
  1566
}
slouken@10595
  1567
slouken@8582
  1568
/*
philipp@10813
  1569
 * Return if the controller in question is currently attached to the system,
slouken@8582
  1570
 *  \return 0 if not plugged in, 1 if still present.
slouken@8582
  1571
 */
slouken@8582
  1572
SDL_bool
slouken@8772
  1573
SDL_GameControllerGetAttached(SDL_GameController * gamecontroller)
slouken@8582
  1574
{
slouken@8772
  1575
    if (!gamecontroller)
slouken@8582
  1576
        return SDL_FALSE;
slouken@8582
  1577
slouken@8582
  1578
    return SDL_JoystickGetAttached(gamecontroller->joystick);
slouken@8582
  1579
}
slouken@8582
  1580
slouken@8582
  1581
/*
slouken@8582
  1582
 * Get the joystick for this controller
slouken@8582
  1583
 */
slouken@8582
  1584
SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
slouken@8582
  1585
{
slouken@8772
  1586
    if (!gamecontroller)
slouken@8582
  1587
        return NULL;
slouken@8582
  1588
slouken@8582
  1589
    return gamecontroller->joystick;
slouken@8582
  1590
}
slouken@8582
  1591
icculus@9916
  1592
icculus@9916
  1593
/*
icculus@9916
  1594
 * Find the SDL_GameController that owns this instance id
icculus@9916
  1595
 */
icculus@9916
  1596
SDL_GameController *
icculus@9916
  1597
SDL_GameControllerFromInstanceID(SDL_JoystickID joyid)
icculus@9916
  1598
{
slouken@10688
  1599
    SDL_GameController *gamecontroller;
slouken@10688
  1600
slouken@11581
  1601
    SDL_LockJoysticks();
slouken@10688
  1602
    gamecontroller = SDL_gamecontrollers;
icculus@9916
  1603
    while (gamecontroller) {
icculus@9916
  1604
        if (gamecontroller->joystick->instance_id == joyid) {
slouken@11581
  1605
            SDL_UnlockJoysticks();
icculus@9916
  1606
            return gamecontroller;
icculus@9916
  1607
        }
icculus@9916
  1608
        gamecontroller = gamecontroller->next;
icculus@9916
  1609
    }
slouken@11581
  1610
    SDL_UnlockJoysticks();
icculus@9916
  1611
    return NULL;
icculus@9916
  1612
}
icculus@9916
  1613
icculus@9916
  1614
philipp@10813
  1615
/*
slouken@8582
  1616
 * Get the SDL joystick layer binding for this controller axis mapping
slouken@8582
  1617
 */
slouken@8582
  1618
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@8582
  1619
{
slouken@10724
  1620
    int i;
slouken@8582
  1621
    SDL_GameControllerButtonBind bind;
slouken@10724
  1622
    SDL_zero(bind);
slouken@8582
  1623
slouken@8772
  1624
    if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
slouken@8582
  1625
        return bind;
slouken@8582
  1626
slouken@10724
  1627
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1628
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1629
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_AXIS && binding->output.axis.axis == axis) {
slouken@10724
  1630
            bind.bindType = binding->inputType;
slouken@10724
  1631
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1632
                /* FIXME: There might be multiple axes bound now that we have axis ranges... */
slouken@10724
  1633
                bind.value.axis = binding->input.axis.axis;
slouken@10724
  1634
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1635
                bind.value.button = binding->input.button;
slouken@10724
  1636
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1637
                bind.value.hat.hat = binding->input.hat.hat;
slouken@10724
  1638
                bind.value.hat.hat_mask = binding->input.hat.hat_mask;
slouken@10724
  1639
            }
slouken@10724
  1640
            break;
slouken@10724
  1641
        }
slouken@8582
  1642
    }
slouken@8582
  1643
    return bind;
slouken@8582
  1644
}
slouken@8582
  1645
slouken@8582
  1646
philipp@10813
  1647
/*
slouken@8582
  1648
 * Get the SDL joystick layer binding for this controller button mapping
slouken@8582
  1649
 */
slouken@8582
  1650
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@8582
  1651
{
slouken@10724
  1652
    int i;
slouken@8582
  1653
    SDL_GameControllerButtonBind bind;
slouken@10724
  1654
    SDL_zero(bind);
slouken@8582
  1655
slouken@8772
  1656
    if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1657
        return bind;
slouken@8582
  1658
slouken@10724
  1659
    for (i = 0; i < gamecontroller->num_bindings; ++i) {
slouken@10724
  1660
        SDL_ExtendedGameControllerBind *binding = &gamecontroller->bindings[i];
slouken@10724
  1661
        if (binding->outputType == SDL_CONTROLLER_BINDTYPE_BUTTON && binding->output.button == button) {
slouken@10724
  1662
            bind.bindType = binding->inputType;
slouken@10724
  1663
            if (binding->inputType == SDL_CONTROLLER_BINDTYPE_AXIS) {
slouken@10724
  1664
                bind.value.axis = binding->input.axis.axis;
slouken@10724
  1665
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_BUTTON) {
slouken@10724
  1666
                bind.value.button = binding->input.button;
slouken@10724
  1667
            } else if (binding->inputType == SDL_CONTROLLER_BINDTYPE_HAT) {
slouken@10724
  1668
                bind.value.hat.hat = binding->input.hat.hat;
slouken@10724
  1669
                bind.value.hat.hat_mask = binding->input.hat.hat_mask;
slouken@10724
  1670
            }
slouken@10724
  1671
            break;
slouken@10724
  1672
        }
slouken@8582
  1673
    }
slouken@8582
  1674
    return bind;
slouken@8582
  1675
}
slouken@8582
  1676
slouken@8582
  1677
slouken@8582
  1678
void
slouken@8582
  1679
SDL_GameControllerClose(SDL_GameController * gamecontroller)
slouken@8582
  1680
{
slouken@8582
  1681
    SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
slouken@8582
  1682
slouken@8772
  1683
    if (!gamecontroller)
slouken@8582
  1684
        return;
slouken@8582
  1685
slouken@11581
  1686
    SDL_LockJoysticks();
slouken@10688
  1687
slouken@8582
  1688
    /* First decrement ref count */
slouken@8582
  1689
    if (--gamecontroller->ref_count > 0) {
slouken@11581
  1690
        SDL_UnlockJoysticks();
slouken@8582
  1691
        return;
slouken@8582
  1692
    }
slouken@8582
  1693
slouken@8772
  1694
    SDL_JoystickClose(gamecontroller->joystick);
slouken@8582
  1695
slouken@8582
  1696
    gamecontrollerlist = SDL_gamecontrollers;
slouken@8582
  1697
    gamecontrollerlistprev = NULL;
slouken@8772
  1698
    while (gamecontrollerlist) {
slouken@8772
  1699
        if (gamecontroller == gamecontrollerlist) {
slouken@8772
  1700
            if (gamecontrollerlistprev) {
slouken@8582
  1701
                /* unlink this entry */
slouken@8582
  1702
                gamecontrollerlistprev->next = gamecontrollerlist->next;
slouken@8772
  1703
            } else {
slouken@8582
  1704
                SDL_gamecontrollers = gamecontroller->next;
slouken@8582
  1705
            }
slouken@8582
  1706
            break;
slouken@8582
  1707
        }
slouken@8582
  1708
        gamecontrollerlistprev = gamecontrollerlist;
slouken@8582
  1709
        gamecontrollerlist = gamecontrollerlist->next;
slouken@8582
  1710
    }
slouken@8582
  1711
slouken@10724
  1712
    SDL_free(gamecontroller->bindings);
slouken@10724
  1713
    SDL_free(gamecontroller->last_match_axis);
slouken@10724
  1714
    SDL_free(gamecontroller->last_hat_mask);
slouken@8582
  1715
    SDL_free(gamecontroller);
slouken@10688
  1716
slouken@11581
  1717
    SDL_UnlockJoysticks();
slouken@8582
  1718
}
slouken@8582
  1719
slouken@8582
  1720
slouken@8582
  1721
/*
slouken@8582
  1722
 * Quit the controller subsystem
slouken@8582
  1723
 */
slouken@8582
  1724
void
slouken@8582
  1725
SDL_GameControllerQuit(void)
slouken@8582
  1726
{
slouken@11581
  1727
    SDL_LockJoysticks();
slouken@8772
  1728
    while (SDL_gamecontrollers) {
slouken@8582
  1729
        SDL_gamecontrollers->ref_count = 1;
slouken@8582
  1730
        SDL_GameControllerClose(SDL_gamecontrollers);
slouken@8582
  1731
    }
slouken@11581
  1732
    SDL_UnlockJoysticks();
slouken@11201
  1733
}
slouken@11201
  1734
slouken@11201
  1735
void
slouken@11201
  1736
SDL_GameControllerQuitMappings(void)
slouken@11201
  1737
{
slouken@11201
  1738
    ControllerMapping_t *pControllerMap;
slouken@8582
  1739
slouken@8772
  1740
    while (s_pSupportedControllers) {
slouken@8582
  1741
        pControllerMap = s_pSupportedControllers;
slouken@8582
  1742
        s_pSupportedControllers = s_pSupportedControllers->next;
slouken@8772
  1743
        SDL_free(pControllerMap->name);
slouken@8772
  1744
        SDL_free(pControllerMap->mapping);
slouken@8772
  1745
        SDL_free(pControllerMap);
slouken@8582
  1746
    }
slouken@8582
  1747
slouken@8772
  1748
    SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
slouken@8582
  1749
slouken@11201
  1750
    SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES,
slouken@11201
  1751
                        SDL_GameControllerIgnoreDevicesChanged, NULL);
slouken@11201
  1752
    SDL_DelHintCallback(SDL_HINT_GAMECONTROLLER_IGNORE_DEVICES_EXCEPT,
slouken@11201
  1753
                        SDL_GameControllerIgnoreDevicesExceptChanged, NULL);
slouken@11201
  1754
slouken@11201
  1755
    if (SDL_allowed_controllers.entries) {
slouken@11201
  1756
        SDL_free(SDL_allowed_controllers.entries);
slouken@11201
  1757
        SDL_zero(SDL_allowed_controllers);
slouken@11201
  1758
    }
slouken@11201
  1759
    if (SDL_ignored_controllers.entries) {
slouken@11201
  1760
        SDL_free(SDL_ignored_controllers.entries);
slouken@11201
  1761
        SDL_zero(SDL_ignored_controllers);
slouken@11201
  1762
    }
slouken@8582
  1763
}
slouken@8582
  1764
slouken@8582
  1765
/*
slouken@8582
  1766
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1767
 */
slouken@10724
  1768
static int
slouken@8582
  1769
SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
slouken@8582
  1770
{
slouken@8582
  1771
    int posted;
slouken@8582
  1772
slouken@8582
  1773
    /* translate the event, if desired */
slouken@8582
  1774
    posted = 0;
slouken@8582
  1775
#if !SDL_EVENTS_DISABLED
slouken@8582
  1776
    if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
slouken@8582
  1777
        SDL_Event event;
slouken@8582
  1778
        event.type = SDL_CONTROLLERAXISMOTION;
slouken@8582
  1779
        event.caxis.which = gamecontroller->joystick->instance_id;
slouken@8582
  1780
        event.caxis.axis = axis;
slouken@8582
  1781
        event.caxis.value = value;
slouken@8582
  1782
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  1783
    }
slouken@8582
  1784
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1785
    return (posted);
slouken@8582
  1786
}
slouken@8582
  1787
slouken@8582
  1788
slouken@8582
  1789
/*
slouken@8582
  1790
 * Event filter to transform joystick events into appropriate game controller ones
slouken@8582
  1791
 */
slouken@10724
  1792
static int
slouken@8582
  1793
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
slouken@8582
  1794
{
slouken@8582
  1795
    int posted;
slouken@8582
  1796
#if !SDL_EVENTS_DISABLED
slouken@8582
  1797
    SDL_Event event;
slouken@8582
  1798
slouken@8772
  1799
    if (button == SDL_CONTROLLER_BUTTON_INVALID)
slouken@8582
  1800
        return (0);
slouken@8582
  1801
slouken@8582
  1802
    switch (state) {
slouken@8582
  1803
    case SDL_PRESSED:
slouken@8582
  1804
        event.type = SDL_CONTROLLERBUTTONDOWN;
slouken@8582
  1805
        break;
slouken@8582
  1806
    case SDL_RELEASED:
slouken@8582
  1807
        event.type = SDL_CONTROLLERBUTTONUP;
slouken@8582
  1808
        break;
slouken@8582
  1809
    default:
slouken@8582
  1810
        /* Invalid state -- bail */
slouken@8582
  1811
        return (0);
slouken@8582
  1812
    }
slouken@8582
  1813
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1814
slouken@8582
  1815
    /* translate the event, if desired */
slouken@8582
  1816
    posted = 0;
slouken@8582
  1817
#if !SDL_EVENTS_DISABLED
slouken@8582
  1818
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@8582
  1819
        event.cbutton.which = gamecontroller->joystick->instance_id;
slouken@8582
  1820
        event.cbutton.button = button;
slouken@8582
  1821
        event.cbutton.state = state;
slouken@8582
  1822
        posted = SDL_PushEvent(&event) == 1;
slouken@8582
  1823
    }
slouken@8582
  1824
#endif /* !SDL_EVENTS_DISABLED */
slouken@8582
  1825
    return (posted);
slouken@8582
  1826
}
slouken@8582
  1827
slouken@8582
  1828
/*
slouken@8582
  1829
 * Turn off controller events
slouken@8582
  1830
 */
slouken@8582
  1831
int
slouken@8582
  1832
SDL_GameControllerEventState(int state)
slouken@8582
  1833
{
slouken@8582
  1834
#if SDL_EVENTS_DISABLED
slouken@8582
  1835
    return SDL_IGNORE;
slouken@8582
  1836
#else
slouken@8582
  1837
    const Uint32 event_list[] = {
slouken@8582
  1838
        SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
slouken@8582
  1839
        SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
slouken@8582
  1840
    };
slouken@8582
  1841
    unsigned int i;
slouken@8582
  1842
slouken@8582
  1843
    switch (state) {
slouken@8582
  1844
    case SDL_QUERY:
slouken@8582
  1845
        state = SDL_IGNORE;
slouken@8582
  1846
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  1847
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@8582
  1848
            if (state == SDL_ENABLE) {
slouken@8582
  1849
                break;
slouken@8582
  1850
            }
slouken@8582
  1851
        }
slouken@8582
  1852
        break;
slouken@8582
  1853
    default:
slouken@8582
  1854
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@8582
  1855
            SDL_EventState(event_list[i], state);
slouken@8582
  1856
        }
slouken@8582
  1857
        break;
slouken@8582
  1858
    }
slouken@8582
  1859
    return (state);
slouken@8582
  1860
#endif /* SDL_EVENTS_DISABLED */
slouken@8582
  1861
}
slouken@8582
  1862
slouken@8582
  1863
/* vi: set ts=4 sw=4 expandtab: */