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