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