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