src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 26 Aug 2016 12:18:08 -0700
changeset 10281 cb13d22b7f09
parent 10280 3134026517cb
permissions -rw-r--r--
Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved() Updated the removal code to iterate over all joystick add messages instead of just the first one.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     1
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     2
  Simple DirectMedia Layer
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9956
diff changeset
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     4
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     5
  This software is provided 'as-is', without any express or implied
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     6
  warranty.  In no event will the authors be held liable for any damages
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     7
  arising from the use of this software.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     8
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
     9
  Permission is granted to anyone to use this software for any purpose,
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    10
  including commercial applications, and to alter it and redistribute it
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    11
  freely, subject to the following restrictions:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    12
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    13
  1. The origin of this software must not be misrepresented; you must not
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    14
     claim that you wrote the original software. If you use this software
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    15
     in a product, an acknowledgment in the product documentation would be
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    16
     appreciated but is not required.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    17
  2. Altered source versions must be plainly marked as such, and must not be
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    18
     misrepresented as being the original software.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    19
  3. This notice may not be removed or altered from any source distribution.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    20
*/
8093
b43765095a6f Make internal SDL sources include SDL_internal.h instead of SDL_config.h
Ryan C. Gordon <icculus@icculus.org>
parents: 8081
diff changeset
    21
#include "../SDL_internal.h"
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    22
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    23
/* This is the game controller API for Simple DirectMedia Layer */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    24
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    25
#include "SDL_events.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    26
#include "SDL_assert.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    27
#include "SDL_sysjoystick.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    28
#include "SDL_hints.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    29
#include "SDL_gamecontrollerdb.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    30
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    31
#if !SDL_EVENTS_DISABLED
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    32
#include "../events/SDL_events_c.h"
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    33
#endif
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    34
#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    35
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
    36
#define SDL_CONTROLLER_PLATFORM_FIELD "platform:"
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    37
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    38
/* a list of currently opened game controllers */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    39
static SDL_GameController *SDL_gamecontrollers = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    40
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    41
/* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    42
struct _SDL_HatMapping
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    43
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    44
    int hat;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    45
    Uint8 mask;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    46
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    47
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    48
#define k_nMaxReverseEntries 20
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    49
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    50
/**
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    51
 * We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    52
 * MAX 4 hats supported
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    53
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    54
#define k_nMaxHatEntries 0x3f + 1
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    55
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    56
/* our in memory mapping db between joystick objects and controller mappings */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    57
struct _SDL_ControllerMapping
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    58
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    59
    SDL_JoystickGUID guid;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    60
    const char *name;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    61
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    62
    /* mapping of axis/button id to controller version */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    63
    int axes[SDL_CONTROLLER_AXIS_MAX];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    64
    int buttonasaxis[SDL_CONTROLLER_AXIS_MAX];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    65
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    66
    int buttons[SDL_CONTROLLER_BUTTON_MAX];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    67
    int axesasbutton[SDL_CONTROLLER_BUTTON_MAX];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    68
    struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    69
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    70
    /* reverse mapping, joystick indices to buttons */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    71
    SDL_GameControllerAxis raxes[k_nMaxReverseEntries];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    72
    SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    73
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    74
    SDL_GameControllerButton rbuttons[k_nMaxReverseEntries];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    75
    SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    76
    SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    77
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    78
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    79
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    80
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    81
/* our hard coded list of mapping support */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    82
typedef struct _ControllerMapping_t
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    83
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    84
    SDL_JoystickGUID guid;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    85
    char *name;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    86
    char *mapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    87
    struct _ControllerMapping_t *next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    88
} ControllerMapping_t;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    89
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    90
static ControllerMapping_t *s_pSupportedControllers = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    91
static ControllerMapping_t *s_pXInputMapping = NULL;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
    92
static ControllerMapping_t *s_pEmscriptenMapping = NULL;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    93
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    94
/* The SDL game controller structure */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    95
struct _SDL_GameController
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    96
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    97
    SDL_Joystick *joystick; /* underlying joystick device */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    98
    int ref_count;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
    99
    Uint8 hatState[4]; /* the current hat state for this controller */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   100
    struct _SDL_ControllerMapping mapping; /* the mapping object for this controller */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   101
    struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   102
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   103
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   104
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   105
int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   106
int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   107
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   108
/*
10281
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   109
 * If there is an existing add event in the queue, it needs to be modified
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   110
 * to have the right value for which, because the number of controllers in
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   111
 * the system is now one less.
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   112
 */
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   113
static void UpdateEventsForDeviceRemoval()
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   114
{
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   115
    int i, num_events;
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   116
    SDL_Event *events;
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   117
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   118
    num_events = SDL_PeepEvents(NULL, 0, SDL_PEEKEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   119
    if (num_events <= 0) {
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   120
        return;
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   121
    }
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   122
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   123
    events = SDL_stack_alloc(SDL_Event, num_events);
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   124
    if (!events) {
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   125
        return;
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   126
    }
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   127
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   128
    num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEADDED);
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   129
    for (i = 0; i < num_events; ++i) {
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   130
        --events[i].cdevice.which;
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   131
    }
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   132
    SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   133
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   134
    SDL_stack_free(events);
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   135
}
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   136
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   137
/*
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   138
 * Event filter to fire controller events from joystick ones
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   139
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   140
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   141
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   142
    switch(event->type) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   143
    case SDL_JOYAXISMOTION:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   144
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   145
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   146
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   147
            if (event->jaxis.axis >= k_nMaxReverseEntries) break;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   148
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   149
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   150
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   151
                if (controllerlist->joystick->instance_id == event->jaxis.which) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   152
                    if (controllerlist->mapping.raxes[event->jaxis.axis] >= 0) /* simple axis to axis, send it through */ {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   153
                        SDL_GameControllerAxis axis = controllerlist->mapping.raxes[event->jaxis.axis];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   154
                        Sint16 value = event->jaxis.value;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   155
                        switch (axis) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   156
                            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   157
                            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   158
                                /* Shift it to be 0 - 32767. */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   159
                                value = value / 2 + 16384;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   160
                            default:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   161
                                break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   162
                        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   163
                        SDL_PrivateGameControllerAxis(controllerlist, axis, value);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   164
                    } else if (controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0) { /* simulate an axis as a button */
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   165
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   166
                    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   167
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   168
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   169
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   170
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   171
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   172
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   173
    case SDL_JOYBUTTONDOWN:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   174
    case SDL_JOYBUTTONUP:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   175
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   176
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   177
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   178
            if (event->jbutton.button >= k_nMaxReverseEntries) break;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   179
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   180
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   181
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   182
                if (controllerlist->joystick->instance_id == event->jbutton.which) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   183
                    if (controllerlist->mapping.rbuttons[event->jbutton.button] >= 0) { /* simple button as button */
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   184
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rbuttons[event->jbutton.button], event->jbutton.state);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   185
                    } else if (controllerlist->mapping.rbuttonasaxis[event->jbutton.button] >= 0) { /* an button pretending to be an axis */
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   186
                        SDL_PrivateGameControllerAxis(controllerlist, controllerlist->mapping.rbuttonasaxis[event->jbutton.button], event->jbutton.state > 0 ? 32767 : 0);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   187
                    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   188
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   189
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   190
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   191
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   192
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   193
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   194
    case SDL_JOYHATMOTION:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   195
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   196
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   197
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   198
            if (event->jhat.hat >= 4) break;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   199
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   200
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   201
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   202
                if (controllerlist->joystick->instance_id == event->jhat.which) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   203
                    Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   204
                    /* Get list of removed bits (button release) */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   205
                    Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   206
                    /* the hat idx in the high nibble */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   207
                    int bHighHat = event->jhat.hat << 4;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   208
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   209
                    if (bChanged & SDL_HAT_DOWN)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   210
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   211
                    if (bChanged & SDL_HAT_UP)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   212
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   213
                    if (bChanged & SDL_HAT_LEFT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   214
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   215
                    if (bChanged & SDL_HAT_RIGHT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   216
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   217
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   218
                    /* Get list of added bits (button press) */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   219
                    bChanged = event->jhat.value ^ bSame;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   220
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   221
                    if (bChanged & SDL_HAT_DOWN)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   222
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   223
                    if (bChanged & SDL_HAT_UP)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   224
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   225
                    if (bChanged & SDL_HAT_LEFT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   226
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   227
                    if (bChanged & SDL_HAT_RIGHT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   228
                        SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   229
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   230
                    /* update our state cache */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   231
                    controllerlist->hatState[event->jhat.hat] = event->jhat.value;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   232
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   233
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   234
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   235
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   236
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   237
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   238
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   239
    case SDL_JOYDEVICEADDED:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   240
        {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   241
            if (SDL_IsGameController(event->jdevice.which)) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   242
                SDL_Event deviceevent;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   243
                deviceevent.type = SDL_CONTROLLERDEVICEADDED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   244
                deviceevent.cdevice.which = event->jdevice.which;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   245
                SDL_PushEvent(&deviceevent);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   246
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   247
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   248
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   249
    case SDL_JOYDEVICEREMOVED:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   250
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   251
            SDL_GameController *controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   252
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   253
                if (controllerlist->joystick->instance_id == event->jdevice.which) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   254
                    SDL_Event deviceevent;
10280
3134026517cb commit 1170112da3776fdb06425f62d57b63144c33dc51
Sam Lantinga <slouken@libsdl.org>
parents: 10105
diff changeset
   255
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   256
                    deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   257
                    deviceevent.cdevice.which = event->jdevice.which;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   258
                    SDL_PushEvent(&deviceevent);
10281
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   259
cb13d22b7f09 Added SDL_PrivateJoystickAdded() and SDL_PrivateJoystickRemoved()
Sam Lantinga <slouken@libsdl.org>
parents: 10280
diff changeset
   260
                    UpdateEventsForDeviceRemoval();
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   261
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   262
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   263
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   264
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   265
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   266
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   267
    default:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   268
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   269
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   270
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   271
    return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   272
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   273
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   274
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   275
 * Helper function to scan the mappings database for a controller with the specified GUID
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   276
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   277
ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   278
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   279
    ControllerMapping_t *pSupportedController = s_pSupportedControllers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   280
    while (pSupportedController) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   281
        if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   282
            return pSupportedController;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   283
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   284
        pSupportedController = pSupportedController->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   285
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   286
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   287
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   288
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   289
static const char* map_StringForControllerAxis[] = {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   290
    "leftx",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   291
    "lefty",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   292
    "rightx",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   293
    "righty",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   294
    "lefttrigger",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   295
    "righttrigger",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   296
    NULL
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   297
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   298
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   299
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   300
 * convert a string to its enum equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   301
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   302
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *pchString)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   303
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   304
    int entry;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   305
    if (!pchString || !pchString[0])
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   306
        return SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   307
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   308
    for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   309
        if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   310
            return entry;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   311
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   312
    return SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   313
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   314
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   315
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   316
 * convert an enum to its string equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   317
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   318
const char* SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   319
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   320
    if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   321
        return map_StringForControllerAxis[axis];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   322
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   323
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   324
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   325
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   326
static const char* map_StringForControllerButton[] = {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   327
    "a",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   328
    "b",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   329
    "x",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   330
    "y",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   331
    "back",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   332
    "guide",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   333
    "start",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   334
    "leftstick",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   335
    "rightstick",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   336
    "leftshoulder",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   337
    "rightshoulder",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   338
    "dpup",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   339
    "dpdown",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   340
    "dpleft",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   341
    "dpright",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   342
    NULL
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   343
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   344
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   345
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   346
 * convert a string to its enum equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   347
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   348
SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *pchString)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   349
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   350
    int entry;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   351
    if (!pchString || !pchString[0])
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   352
        return SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   353
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   354
    for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   355
        if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   356
            return entry;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   357
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   358
    return SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   359
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   360
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   361
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   362
 * convert an enum to its string equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   363
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   364
const char* SDL_GameControllerGetStringForButton(SDL_GameControllerButton axis)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   365
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   366
    if (axis > SDL_CONTROLLER_BUTTON_INVALID && axis < SDL_CONTROLLER_BUTTON_MAX) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   367
        return map_StringForControllerButton[axis];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   368
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   369
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   370
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   371
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   372
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   373
 * given a controller button name and a joystick name update our mapping structure with it
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   374
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   375
void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   376
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   377
    int iSDLButton = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   378
    SDL_GameControllerButton button;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   379
    SDL_GameControllerAxis axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   380
    button = SDL_GameControllerGetButtonFromString(szGameButton);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   381
    axis = SDL_GameControllerGetAxisFromString(szGameButton);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   382
    iSDLButton = SDL_atoi(&szJoystickButton[1]);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   383
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   384
    if (szJoystickButton[0] == 'a') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   385
        if (iSDLButton >= k_nMaxReverseEntries) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   386
            SDL_SetError("Axis index too large: %d", iSDLButton);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   387
            return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   388
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   389
        if (axis != SDL_CONTROLLER_AXIS_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   390
            pMapping->axes[ axis ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   391
            pMapping->raxes[ iSDLButton ] = axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   392
        } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   393
            pMapping->axesasbutton[ button ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   394
            pMapping->raxesasbutton[ iSDLButton ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   395
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   396
            SDL_assert(!"How did we get here?");
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   397
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   398
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   399
    } else if (szJoystickButton[0] == 'b') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   400
        if (iSDLButton >= k_nMaxReverseEntries) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   401
            SDL_SetError("Button index too large: %d", iSDLButton);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   402
            return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   403
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   404
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   405
            pMapping->buttons[ button ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   406
            pMapping->rbuttons[ iSDLButton ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   407
        } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   408
            pMapping->buttonasaxis[ axis ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   409
            pMapping->rbuttonasaxis[ iSDLButton ] = axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   410
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   411
            SDL_assert(!"How did we get here?");
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   412
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   413
    } else if (szJoystickButton[0] == 'h') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   414
        int hat = SDL_atoi(&szJoystickButton[1]);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   415
        int mask = SDL_atoi(&szJoystickButton[3]);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   416
        if (hat >= 4) {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   417
            SDL_SetError("Hat index too large: %d", iSDLButton);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   418
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   419
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   420
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   421
            int ridx;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   422
            pMapping->hatasbutton[ button ].hat = hat;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   423
            pMapping->hatasbutton[ button ].mask = mask;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   424
            ridx = (hat << 4) | mask;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   425
            pMapping->rhatasbutton[ ridx ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   426
        } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   427
            SDL_assert(!"Support hat as axis");
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   428
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   429
            SDL_assert(!"How did we get here?");
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   430
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   431
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   432
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   433
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   434
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   435
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   436
 * given a controller mapping string update our mapping object
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   437
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   438
static void
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   439
SDL_PrivateGameControllerParseControllerConfigString(struct _SDL_ControllerMapping *pMapping, const char *pchString)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   440
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   441
    char szGameButton[20];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   442
    char szJoystickButton[20];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   443
    SDL_bool bGameButton = SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   444
    int i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   445
    const char *pchPos = pchString;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   446
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   447
    SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   448
    SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   449
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   450
    while (pchPos && *pchPos) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   451
        if (*pchPos == ':') {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   452
            i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   453
            bGameButton = SDL_FALSE;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   454
        } else if (*pchPos == ' ') {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   455
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   456
        } else if (*pchPos == ',') {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   457
            i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   458
            bGameButton = SDL_TRUE;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   459
            SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   460
            SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   461
            SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   462
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   463
        } else if (bGameButton) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   464
            if (i >= sizeof(szGameButton)) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   465
                SDL_SetError("Button name too large: %s", szGameButton);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   466
                return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   467
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   468
            szGameButton[i] = *pchPos;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   469
            i++;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   470
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   471
            if (i >= sizeof(szJoystickButton)) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   472
                SDL_SetError("Joystick button name too large: %s", szJoystickButton);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   473
                return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   474
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   475
            szJoystickButton[i] = *pchPos;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   476
            i++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   477
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   478
        pchPos++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   479
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   480
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   481
    SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   482
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   483
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   484
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   485
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   486
 * Make a new button mapping struct
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   487
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   488
void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   489
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   490
    int j;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   491
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   492
    pMapping->guid = guid;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   493
    pMapping->name = pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   494
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   495
    /* set all the button mappings to non defaults */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   496
    for (j = 0; j < SDL_CONTROLLER_AXIS_MAX; j++) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   497
        pMapping->axes[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   498
        pMapping->buttonasaxis[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   499
    }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   500
    for (j = 0; j < SDL_CONTROLLER_BUTTON_MAX; j++) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   501
        pMapping->buttons[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   502
        pMapping->axesasbutton[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   503
        pMapping->hatasbutton[j].hat = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   504
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   505
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   506
    for (j = 0; j < k_nMaxReverseEntries; j++) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   507
        pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   508
        pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   509
        pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   510
        pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   511
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   512
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   513
    for (j = 0; j < k_nMaxHatEntries; j++) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   514
        pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   515
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   516
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   517
    SDL_PrivateGameControllerParseControllerConfigString(pMapping, pchMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   518
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   519
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   520
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   521
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   522
 * grab the guid string from a mapping string
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   523
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   524
char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   525
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   526
    const char *pFirstComma = SDL_strchr(pMapping, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   527
    if (pFirstComma) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   528
        char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   529
        if (!pchGUID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   530
            SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   531
            return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   532
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   533
        SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   534
        pchGUID[ pFirstComma - pMapping ] = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   535
        return pchGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   536
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   537
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   538
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   539
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   540
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   541
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   542
 * grab the name string from a mapping string
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   543
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   544
char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   545
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   546
    const char *pFirstComma, *pSecondComma;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   547
    char *pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   548
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   549
    pFirstComma = SDL_strchr(pMapping, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   550
    if (!pFirstComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   551
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   552
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   553
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   554
    if (!pSecondComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   555
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   556
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   557
    pchName = SDL_malloc(pSecondComma - pFirstComma);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   558
    if (!pchName) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   559
        SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   560
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   561
    }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   562
    SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   563
    pchName[ pSecondComma - pFirstComma - 1 ] = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   564
    return pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   565
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   566
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   567
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   568
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   569
 * grab the button mapping string from a mapping string
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   570
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   571
char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   572
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   573
    const char *pFirstComma, *pSecondComma;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   574
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   575
    pFirstComma = SDL_strchr(pMapping, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   576
    if (!pFirstComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   577
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   578
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   579
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   580
    if (!pSecondComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   581
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   583
    return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   584
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   585
9956
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   586
/*
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   587
 * Helper function to refresh a mapping
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   588
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   589
void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   590
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   591
    SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   592
    while (gamecontrollerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   593
        if (!SDL_memcmp(&gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   594
            SDL_Event event;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   595
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   596
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   597
            SDL_PushEvent(&event);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   598
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   599
            /* Not really threadsafe.  Should this lock access within SDL_GameControllerEventWatcher? */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   600
            SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   601
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   602
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   603
        gamecontrollerlist = gamecontrollerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   604
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   605
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   606
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   607
/*
9956
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   608
 * Helper function to add a mapping for a guid
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   609
 */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   610
static ControllerMapping_t *
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   611
SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing)
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   612
{
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   613
    char *pchName;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   614
    char *pchMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   615
    ControllerMapping_t *pControllerMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   616
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   617
    pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   618
    if (!pchName) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   619
        SDL_SetError("Couldn't parse name from %s", mappingString);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   620
        return NULL;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   621
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   622
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   623
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   624
    if (!pchMapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   625
        SDL_free(pchName);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   626
        SDL_SetError("Couldn't parse %s", mappingString);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   627
        return NULL;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   628
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   629
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   630
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   631
    if (pControllerMapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   632
        /* Update existing mapping */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   633
        SDL_free(pControllerMapping->name);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   634
        pControllerMapping->name = pchName;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   635
        SDL_free(pControllerMapping->mapping);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   636
        pControllerMapping->mapping = pchMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   637
        /* refresh open controllers */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   638
        SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   639
        *existing = SDL_TRUE;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   640
    } else {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   641
        pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   642
        if (!pControllerMapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   643
            SDL_free(pchName);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   644
            SDL_free(pchMapping);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   645
            SDL_OutOfMemory();
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   646
            return NULL;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   647
        }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   648
        pControllerMapping->guid = jGUID;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   649
        pControllerMapping->name = pchName;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   650
        pControllerMapping->mapping = pchMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   651
        pControllerMapping->next = s_pSupportedControllers;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   652
        s_pSupportedControllers = pControllerMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   653
        *existing = SDL_FALSE;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   654
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   655
    return pControllerMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   656
}
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   657
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   658
/*
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   659
 * Helper function to determine pre-calculated offset to certain joystick mappings
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   660
 */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   661
ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   662
{
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   663
    SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID(device_index);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   664
    ControllerMapping_t *mapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   665
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   666
    mapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   667
#if SDL_JOYSTICK_XINPUT
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   668
    if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   669
        mapping = s_pXInputMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   670
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   671
#endif
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   672
#if defined(SDL_JOYSTICK_EMSCRIPTEN)
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   673
    if (!mapping && s_pEmscriptenMapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   674
        mapping = s_pEmscriptenMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   675
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   676
#endif
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   677
#ifdef __LINUX__
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   678
    if (!mapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   679
        const char *name = SDL_JoystickNameForIndex(device_index);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   680
        if (name) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   681
            if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   682
                /* The Linux driver xpad.c maps the wireless dpad to buttons */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   683
                SDL_bool existing;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   684
                mapping = SDL_PrivateAddMappingForGUID(jGUID,
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   685
"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,",
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   686
                              &existing);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   687
            }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   688
        }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   689
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   690
#endif /* __LINUX__ */
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   691
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   692
    if (!mapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   693
        const char *name = SDL_JoystickNameForIndex(device_index);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   694
        if (name) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   695
            if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box")) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   696
                mapping = s_pXInputMapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   697
            }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   698
        }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   699
    }
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   700
    return mapping;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   701
}
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   702
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   703
/*
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   704
 * Add or update an entry into the Mappings Database
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   705
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   706
int
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   707
SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   708
{
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   709
    const char *platform = SDL_GetPlatform();
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   710
    int controllers = 0;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   711
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   712
    size_t db_size, platform_len;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   713
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   714
    if (rw == NULL) {
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   715
        return SDL_SetError("Invalid RWops");
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   716
    }
8052
f0c6c1a45fb1 Fixed compiler warning on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 8046
diff changeset
   717
    db_size = (size_t)SDL_RWsize(rw);
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   718
    
8052
f0c6c1a45fb1 Fixed compiler warning on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 8046
diff changeset
   719
    buf = (char *)SDL_malloc(db_size + 1);
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   720
    if (buf == NULL) {
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   721
        if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   722
            SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   723
        }
9194
583176c5982f Fixed wording in SDL_GameControllerAddMappingsFromRW() error message.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8976
diff changeset
   724
        return SDL_SetError("Could not allocate space to read DB into memory");
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   725
    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   726
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   727
    if (SDL_RWread(rw, buf, db_size, 1) != 1) {
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   728
        if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   729
            SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   730
        }
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   731
        SDL_free(buf);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   732
        return SDL_SetError("Could not read DB");
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   733
    }
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   734
    
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   735
    if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   736
        SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   737
    }
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   738
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   739
    buf[db_size] = '\0';
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   740
    line = buf;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   741
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   742
    while (line < buf + db_size) {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   743
        line_end = SDL_strchr(line, '\n');
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   744
        if (line_end != NULL) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   745
            *line_end = '\0';
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   746
        } else {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   747
            line_end = buf + db_size;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   748
        }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   749
        
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   750
        /* Extract and verify the platform */
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   751
        tmp = SDL_strstr(line, SDL_CONTROLLER_PLATFORM_FIELD);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   752
        if (tmp != NULL) {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   753
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   754
            comma = SDL_strchr(tmp, ',');
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   755
            if (comma != NULL) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   756
                platform_len = comma - tmp + 1;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   757
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   758
                    SDL_strlcpy(line_platform, tmp, platform_len);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   759
                    if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   760
                        SDL_GameControllerAddMapping(line) > 0) {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   761
                        controllers++;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   762
                    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   763
                }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   764
            }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   765
        }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   766
        
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   767
        line = line_end + 1;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   768
    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   769
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   770
    SDL_free(buf);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   771
    return controllers;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   772
}
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   773
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   774
/*
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   775
 * Add or update an entry into the Mappings Database
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   776
 */
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   777
int
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   778
SDL_GameControllerAddMapping(const char *mappingString)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   779
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   780
    char *pchGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   781
    SDL_JoystickGUID jGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   782
    SDL_bool is_xinput_mapping = SDL_FALSE;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   783
    SDL_bool is_emscripten_mapping = SDL_FALSE;
9956
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   784
    SDL_bool existing = SDL_FALSE;
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   785
    ControllerMapping_t *pControllerMapping;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   786
9198
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   787
    if (!mappingString) {
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   788
        return SDL_InvalidParamError("mappingString");
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   789
    }
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   790
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   791
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   792
    if (!pchGUID) {
8081
402b8c313246 Changed return -1 after SDL_SetError() to return SDL_SetError().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8067
diff changeset
   793
        return SDL_SetError("Couldn't parse GUID from %s", mappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   794
    }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   795
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   796
        is_xinput_mapping = SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   797
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   798
    if (!SDL_strcasecmp(pchGUID, "emscripten")) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   799
        is_emscripten_mapping = SDL_TRUE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   800
    }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   801
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   802
    SDL_free(pchGUID);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   803
9956
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   804
    pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing);
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   805
    if (!pControllerMapping) {
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   806
        return -1;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   807
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   808
9956
0b3b1f767eb1 Added broad support for wireless XBox 360 controllers on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 9916
diff changeset
   809
    if (existing) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   810
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   811
    } else {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   812
        if (is_xinput_mapping) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   813
            s_pXInputMapping = pControllerMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   814
        }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   815
        if (is_emscripten_mapping) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   816
            s_pEmscriptenMapping = pControllerMapping;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   817
        }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   818
        return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   819
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   820
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   821
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   822
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   823
 * Get the mapping string for this GUID
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   824
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   825
char *
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   826
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   827
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   828
    char *pMappingString = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   829
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   830
    if (mapping) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   831
        char pchGUID[33];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   832
        size_t needed;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   833
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   834
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   835
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   836
        pMappingString = SDL_malloc(needed);
9197
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   837
        if (!pMappingString) {
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   838
            SDL_OutOfMemory();
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   839
            return NULL;
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   840
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   841
        SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   842
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   843
    return pMappingString;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   844
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   845
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   846
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   847
 * Get the mapping string for this device
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   848
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   849
char *
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   850
SDL_GameControllerMapping(SDL_GameController * gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   851
{
9196
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   852
    if (!gamecontroller) {
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   853
        return NULL;
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   854
    }
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   855
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   856
    return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   857
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   858
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   859
static void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   860
SDL_GameControllerLoadHints()
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   861
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   862
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   863
    if (hint && hint[0]) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   864
        size_t nchHints = SDL_strlen(hint);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   865
        char *pUserMappings = SDL_malloc(nchHints + 1);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   866
        char *pTempMappings = pUserMappings;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   867
        SDL_memcpy(pUserMappings, hint, nchHints);
8267
3e857d08cd02 Null termnate the pUserMappings variable to prevent memory corruption.
Sam Lantinga <slouken@libsdl.org>
parents: 8246
diff changeset
   868
        pUserMappings[nchHints] = '\0';
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   869
        while (pUserMappings) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   870
            char *pchNewLine = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   871
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   872
            pchNewLine = SDL_strchr(pUserMappings, '\n');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   873
            if (pchNewLine)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   874
                *pchNewLine = '\0';
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   875
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   876
            SDL_GameControllerAddMapping(pUserMappings);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   877
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   878
            if (pchNewLine) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   879
                pUserMappings = pchNewLine + 1;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   880
            } else {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   881
                pUserMappings = NULL;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   882
            }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   883
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   884
        SDL_free(pTempMappings);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   885
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   886
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   887
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   888
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   889
 * Initialize the game controller system, mostly load our DB of controller config mappings
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   890
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   891
int
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   892
SDL_GameControllerInit(void)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   893
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   894
    int i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   895
    const char *pMappingString = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   896
    pMappingString = s_ControllerMappings[i];
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   897
    while (pMappingString) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   898
        SDL_GameControllerAddMapping(pMappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   899
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   900
        i++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   901
        pMappingString = s_ControllerMappings[i];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   902
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   903
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   904
    /* load in any user supplied config */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   905
    SDL_GameControllerLoadHints();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   906
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   907
    /* watch for joy events and fire controller ones if needed */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   908
    SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   909
8067
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   910
    /* Send added events for controllers currently attached */
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   911
    for (i = 0; i < SDL_NumJoysticks(); ++i) {
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   912
        if (SDL_IsGameController(i)) {
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   913
            SDL_Event deviceevent;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   914
            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   915
            deviceevent.cdevice.which = i;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   916
            SDL_PushEvent(&deviceevent);
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   917
        }
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   918
    }
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   919
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   920
    return (0);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   921
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   922
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   923
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   924
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   925
 * Get the implementation dependent name of a controller
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   926
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   927
const char *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   928
SDL_GameControllerNameForIndex(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   929
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   930
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   931
    if (pSupportedController) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   932
        return pSupportedController->name;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   933
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   934
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   935
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   936
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   937
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   938
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   939
 * Return 1 if the joystick at this device index is a supported controller
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   940
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   941
SDL_bool
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   942
SDL_IsGameController(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   943
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   944
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   945
    if (pSupportedController) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   946
        return SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   947
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   948
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   949
    return SDL_FALSE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   950
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   951
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   952
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   953
 * Open a controller for use - the index passed as an argument refers to
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   954
 * the N'th controller on the system.  This index is the value which will
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   955
 * identify this controller in future controller events.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   956
 *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   957
 * This function returns a controller identifier, or NULL if an error occurred.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   958
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   959
SDL_GameController *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   960
SDL_GameControllerOpen(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   961
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   962
    SDL_GameController *gamecontroller;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   963
    SDL_GameController *gamecontrollerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   964
    ControllerMapping_t *pSupportedController = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   965
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   966
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   967
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   968
        return (NULL);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   969
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   970
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   971
    gamecontrollerlist = SDL_gamecontrollers;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   972
    /* If the controller is already open, return it */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   973
    while (gamecontrollerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   974
        if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   975
                gamecontroller = gamecontrollerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   976
                ++gamecontroller->ref_count;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   977
                return (gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   978
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   979
        gamecontrollerlist = gamecontrollerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   980
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   981
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   982
    /* Find a controller mapping */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   983
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   984
    if (!pSupportedController) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   985
        SDL_SetError("Couldn't find mapping for device (%d)", device_index);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   986
        return (NULL);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   987
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   988
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   989
    /* Create and initialize the joystick */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   990
    gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   991
    if (gamecontroller == NULL) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   992
        SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   993
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   994
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   995
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   996
    SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   997
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   998
    if (!gamecontroller->joystick) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   999
        SDL_free(gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1000
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1001
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1002
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
  1003
    SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1004
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1005
    /* Add joystick to list */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1006
    ++gamecontroller->ref_count;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1007
    /* Link the joystick in the list */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1008
    gamecontroller->next = SDL_gamecontrollers;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1009
    SDL_gamecontrollers = gamecontroller;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1010
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
  1011
    SDL_SYS_JoystickUpdate(gamecontroller->joystick);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1012
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1013
    return (gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1014
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1015
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1016
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1017
 * Manually pump for controller updates.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1018
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1019
void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1020
SDL_GameControllerUpdate(void)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1021
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1022
    /* Just for API completeness; the joystick API does all the work. */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1023
    SDL_JoystickUpdate();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1024
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1025
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.