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