src/joystick/SDL_gamecontroller.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 09 Apr 2015 22:33:39 -0400
branchiOS-improvements
changeset 9542 618662dc9e82
parent 9278 8900afb78a19
child 9596 ac646c8a73ae
permissions -rw-r--r--
Closing the iOS-improvements branch.
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
8149
681eb46b8ac4 Fixed bug 2374 - Update copyright for 2014...
Sam Lantinga <slouken@libsdl.org>
parents: 8093
diff changeset
     3
  Copyright (C) 1997-2014 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
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   109
 * 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
   110
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   111
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
   112
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   113
    switch(event->type) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   114
    case SDL_JOYAXISMOTION:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   115
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   116
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   117
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   118
            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
   119
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   120
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   121
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   122
                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
   123
                    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
   124
                        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
   125
                        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
   126
                        switch (axis) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   127
                            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   128
                            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   129
                                /* Shift it to be 0 - 32767. */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   130
                                value = value / 2 + 16384;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   131
                            default:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   132
                                break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   133
                        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   134
                        SDL_PrivateGameControllerAxis(controllerlist, axis, value);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   135
                    } 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
   136
                        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
   137
                    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   138
                    break;
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
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   141
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   142
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   143
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   144
    case SDL_JOYBUTTONDOWN:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   145
    case SDL_JOYBUTTONUP:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   146
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   147
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   148
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   149
            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
   150
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   151
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   152
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   153
                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
   154
                    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
   155
                        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
   156
                    } 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
   157
                        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
   158
                    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   159
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   160
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   161
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   162
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   163
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   164
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   165
    case SDL_JOYHATMOTION:
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
            SDL_GameController *controllerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   168
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   169
            if (event->jhat.hat >= 4) break;
8582
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
            controllerlist = SDL_gamecontrollers;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   172
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   173
                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
   174
                    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
   175
                    /* Get list of removed bits (button release) */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   176
                    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
   177
                    /* the hat idx in the high nibble */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   178
                    int bHighHat = event->jhat.hat << 4;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   179
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   180
                    if (bChanged & SDL_HAT_DOWN)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   181
                        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
   182
                    if (bChanged & SDL_HAT_UP)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   183
                        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
   184
                    if (bChanged & SDL_HAT_LEFT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   185
                        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
   186
                    if (bChanged & SDL_HAT_RIGHT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   187
                        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
   188
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   189
                    /* Get list of added bits (button press) */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   190
                    bChanged = event->jhat.value ^ bSame;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   191
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   192
                    if (bChanged & SDL_HAT_DOWN)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   193
                        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
   194
                    if (bChanged & SDL_HAT_UP)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   195
                        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
   196
                    if (bChanged & SDL_HAT_LEFT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   197
                        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
   198
                    if (bChanged & SDL_HAT_RIGHT)
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   199
                        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
   200
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   201
                    /* update our state cache */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   202
                    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
   203
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   204
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   205
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   206
                controllerlist = controllerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   207
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   208
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   209
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   210
    case SDL_JOYDEVICEADDED:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   211
        {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   212
            if (SDL_IsGameController(event->jdevice.which)) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   213
                SDL_Event deviceevent;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   214
                deviceevent.type = SDL_CONTROLLERDEVICEADDED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   215
                deviceevent.cdevice.which = event->jdevice.which;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   216
                SDL_PushEvent(&deviceevent);
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
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   219
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   220
    case SDL_JOYDEVICEREMOVED:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   221
        {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   222
            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
   223
            while (controllerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   224
                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
   225
                    SDL_Event deviceevent;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   226
                    deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   227
                    deviceevent.cdevice.which = event->jdevice.which;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   228
                    SDL_PushEvent(&deviceevent);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   229
                    break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   230
                }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   231
                controllerlist = controllerlist->next;
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
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   234
        break;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   235
    default:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   236
        break;
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   239
    return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   240
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   241
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   242
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   243
 * 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
   244
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   245
ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
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
    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
   248
    while (pSupportedController) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   249
        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
   250
            return pSupportedController;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   251
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   252
        pSupportedController = pSupportedController->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   253
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   254
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   255
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   256
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   257
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   258
 * Helper function to determine pre-calculated offset to certain joystick mappings
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   259
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   260
ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   261
{
8976
1a5d959d7b32 Fixed mingw64 build and warnings
Sam Lantinga <slouken@libsdl.org>
parents: 8972
diff changeset
   262
#if SDL_JOYSTICK_XINPUT
8920
21ccd40c778a Fixed bug 2553 - Add support to all XInput devices
Sam Lantinga <slouken@libsdl.org>
parents: 8772
diff changeset
   263
    if (SDL_SYS_IsXInputGamepad_DeviceIndex(device_index) && s_pXInputMapping) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   264
        return s_pXInputMapping;
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
    else
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   267
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   268
#if defined(SDL_JOYSTICK_EMSCRIPTEN)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   269
    if (s_pEmscriptenMapping) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   270
        return s_pEmscriptenMapping;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   271
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   272
    else
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   273
#endif
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   274
    {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   275
        SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID(device_index);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   276
        return SDL_PrivateGetControllerMappingForGUID(&jGUID);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   277
    }
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   280
static const char* map_StringForControllerAxis[] = {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   281
    "leftx",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   282
    "lefty",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   283
    "rightx",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   284
    "righty",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   285
    "lefttrigger",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   286
    "righttrigger",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   287
    NULL
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   290
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   291
 * convert a string to its enum equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   292
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   293
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
   294
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   295
    int entry;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   296
    if (!pchString || !pchString[0])
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   297
        return SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   298
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   299
    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
   300
        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
   301
            return entry;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   302
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   303
    return SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   304
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   305
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   306
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   307
 * convert an enum to its string equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   308
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   309
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
   310
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   311
    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
   312
        return map_StringForControllerAxis[axis];
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
    return NULL;
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   317
static const char* map_StringForControllerButton[] = {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   318
    "a",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   319
    "b",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   320
    "x",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   321
    "y",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   322
    "back",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   323
    "guide",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   324
    "start",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   325
    "leftstick",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   326
    "rightstick",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   327
    "leftshoulder",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   328
    "rightshoulder",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   329
    "dpup",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   330
    "dpdown",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   331
    "dpleft",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   332
    "dpright",
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   333
    NULL
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   334
};
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   335
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   336
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   337
 * convert a string to its enum equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   338
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   339
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
   340
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   341
    int entry;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   342
    if (!pchString || !pchString[0])
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   343
        return SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   344
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   345
    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
   346
        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
   347
            return entry;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   348
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   349
    return SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   350
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   351
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   352
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   353
 * convert an enum to its string equivalent
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   354
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   355
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
   356
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   357
    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
   358
        return map_StringForControllerButton[axis];
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
    return NULL;
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   363
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   364
 * 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
   365
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   366
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
   367
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   368
    int iSDLButton = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   369
    SDL_GameControllerButton button;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   370
    SDL_GameControllerAxis axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   371
    button = SDL_GameControllerGetButtonFromString(szGameButton);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   372
    axis = SDL_GameControllerGetAxisFromString(szGameButton);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   373
    iSDLButton = SDL_atoi(&szJoystickButton[1]);
8582
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
    if (szJoystickButton[0] == 'a') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   376
        if (iSDLButton >= k_nMaxReverseEntries) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   377
            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
   378
            return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   379
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   380
        if (axis != SDL_CONTROLLER_AXIS_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   381
            pMapping->axes[ axis ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   382
            pMapping->raxes[ iSDLButton ] = axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   383
        } 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
   384
            pMapping->axesasbutton[ button ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   385
            pMapping->raxesasbutton[ iSDLButton ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   386
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   387
            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
   388
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   389
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   390
    } else if (szJoystickButton[0] == 'b') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   391
        if (iSDLButton >= k_nMaxReverseEntries) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   392
            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
   393
            return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   394
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   395
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   396
            pMapping->buttons[ button ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   397
            pMapping->rbuttons[ iSDLButton ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   398
        } 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
   399
            pMapping->buttonasaxis[ axis ] = iSDLButton;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   400
            pMapping->rbuttonasaxis[ iSDLButton ] = axis;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   401
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   402
            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
   403
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   404
    } else if (szJoystickButton[0] == 'h') {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   405
        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
   406
        int mask = SDL_atoi(&szJoystickButton[3]);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   407
        if (hat >= 4) {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   408
            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
   409
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   410
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   411
        if (button != SDL_CONTROLLER_BUTTON_INVALID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   412
            int ridx;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   413
            pMapping->hatasbutton[ button ].hat = hat;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   414
            pMapping->hatasbutton[ button ].mask = mask;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   415
            ridx = (hat << 4) | mask;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   416
            pMapping->rhatasbutton[ ridx ] = button;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   417
        } 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
   418
            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
   419
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   420
            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
   421
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   422
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   423
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   424
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   425
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   426
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   427
 * 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
   428
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   429
static void
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   430
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
   431
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   432
    char szGameButton[20];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   433
    char szJoystickButton[20];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   434
    SDL_bool bGameButton = SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   435
    int i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   436
    const char *pchPos = pchString;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   437
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   438
    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
   439
    SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   440
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   441
    while (pchPos && *pchPos) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   442
        if (*pchPos == ':') {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   443
            i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   444
            bGameButton = SDL_FALSE;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   445
        } else if (*pchPos == ' ') {
8582
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
        } else if (*pchPos == ',') {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   448
            i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   449
            bGameButton = SDL_TRUE;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   450
            SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   451
            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
   452
            SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   453
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   454
        } else if (bGameButton) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   455
            if (i >= sizeof(szGameButton)) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   456
                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
   457
                return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   458
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   459
            szGameButton[i] = *pchPos;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   460
            i++;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   461
        } else {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   462
            if (i >= sizeof(szJoystickButton)) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   463
                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
   464
                return;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   465
            }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   466
            szJoystickButton[i] = *pchPos;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   467
            i++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   468
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   469
        pchPos++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   470
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   471
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   472
    SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   473
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   476
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   477
 * Make a new button mapping struct
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   478
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   479
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
   480
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   481
    int j;
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
    pMapping->guid = guid;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   484
    pMapping->name = pchName;
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
    /* 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
   487
    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
   488
        pMapping->axes[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   489
        pMapping->buttonasaxis[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   490
    }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   491
    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
   492
        pMapping->buttons[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   493
        pMapping->axesasbutton[j] = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   494
        pMapping->hatasbutton[j].hat = -1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   495
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   496
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   497
    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
   498
        pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   499
        pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   500
        pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   501
        pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   502
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   503
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   504
    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
   505
        pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   506
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   507
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   508
    SDL_PrivateGameControllerParseControllerConfigString(pMapping, pchMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   509
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   510
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
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   513
 * 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
   514
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   515
char *SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
8582
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
    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
   518
    if (pFirstComma) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   519
        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
   520
        if (!pchGUID) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   521
            SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   522
            return NULL;
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
        SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   525
        pchGUID[ pFirstComma - pMapping ] = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   526
        return pchGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   527
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   528
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   529
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   530
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   531
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   532
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   533
 * 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
   534
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   535
char *SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
8582
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
    const char *pFirstComma, *pSecondComma;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   538
    char *pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   539
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   540
    pFirstComma = SDL_strchr(pMapping, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   541
    if (!pFirstComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   542
        return NULL;
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
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   545
    if (!pSecondComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   546
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   547
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   548
    pchName = SDL_malloc(pSecondComma - pFirstComma);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   549
    if (!pchName) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   550
        SDL_OutOfMemory();
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
    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
   554
    pchName[ pSecondComma - pFirstComma - 1 ] = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   555
    return pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   556
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   557
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   558
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   559
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   560
 * 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
   561
 */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   562
char *SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   563
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   564
    const char *pFirstComma, *pSecondComma;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   565
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   566
    pFirstComma = SDL_strchr(pMapping, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   567
    if (!pFirstComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   568
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   569
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   570
    pSecondComma = SDL_strchr(pFirstComma + 1, ',');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   571
    if (!pSecondComma)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   572
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   573
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   574
    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
   575
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   576
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   577
void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   578
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   579
    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
   580
    while (gamecontrollerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   581
        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
   582
            SDL_Event event;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   583
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   584
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   585
            SDL_PushEvent(&event);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   586
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   587
            /* 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
   588
            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
   589
        }
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
        gamecontrollerlist = gamecontrollerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   592
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   593
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   594
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   595
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   596
 * 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
   597
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   598
int
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   599
SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw)
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   600
{
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   601
    const char *platform = SDL_GetPlatform();
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   602
    int controllers = 0;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   603
    char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   604
    size_t db_size, platform_len;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   605
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   606
    if (rw == NULL) {
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   607
        return SDL_SetError("Invalid RWops");
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   608
    }
8052
f0c6c1a45fb1 Fixed compiler warning on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 8046
diff changeset
   609
    db_size = (size_t)SDL_RWsize(rw);
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   610
    
8052
f0c6c1a45fb1 Fixed compiler warning on Visual Studio
Sam Lantinga <slouken@libsdl.org>
parents: 8046
diff changeset
   611
    buf = (char *)SDL_malloc(db_size + 1);
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   612
    if (buf == NULL) {
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   613
        if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   614
            SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   615
        }
9194
583176c5982f Fixed wording in SDL_GameControllerAddMappingsFromRW() error message.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8976
diff changeset
   616
        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
   617
    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   618
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   619
    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
   620
        if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   621
            SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   622
        }
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   623
        SDL_free(buf);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   624
        return SDL_SetError("Could not read DB");
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   625
    }
8046
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   626
    
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   627
    if (freerw) {
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   628
        SDL_RWclose(rw);
e7c89b29932a Adds SDL_GameControllerAddMappingsFromRW, updates controllermap
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8042
diff changeset
   629
    }
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   630
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   631
    buf[db_size] = '\0';
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   632
    line = buf;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   633
    
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   634
    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
   635
        line_end = SDL_strchr(line, '\n');
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   636
        if (line_end != NULL) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   637
            *line_end = '\0';
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   638
        } else {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   639
            line_end = buf + db_size;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   640
        }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   641
        
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   642
        /* Extract and verify the platform */
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   643
        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
   644
        if (tmp != NULL) {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   645
            tmp += SDL_strlen(SDL_CONTROLLER_PLATFORM_FIELD);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   646
            comma = SDL_strchr(tmp, ',');
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   647
            if (comma != NULL) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   648
                platform_len = comma - tmp + 1;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   649
                if (platform_len + 1 < SDL_arraysize(line_platform)) {
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   650
                    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
   651
                    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
   652
                        SDL_GameControllerAddMapping(line) > 0) {
8042
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   653
                        controllers++;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   654
                    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   655
                }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   656
            }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   657
        }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   658
        
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   659
        line = line_end + 1;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   660
    }
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   661
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   662
    SDL_free(buf);
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   663
    return controllers;
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   664
}
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   665
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   666
/*
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   667
 * Add or update an entry into the Mappings Database
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   668
 */
7863121cb7d5 Adds SDL_GameControllerAddMappingsFromFile
Gabriel Jacobo <gabomdq@gmail.com>
parents: 8009
diff changeset
   669
int
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   670
SDL_GameControllerAddMapping(const char *mappingString)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   671
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   672
    char *pchGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   673
    char *pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   674
    char *pchMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   675
    SDL_JoystickGUID jGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   676
    ControllerMapping_t *pControllerMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   677
    SDL_bool is_xinput_mapping = SDL_FALSE;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   678
    SDL_bool is_emscripten_mapping = SDL_FALSE;
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   679
9198
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   680
    if (!mappingString) {
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   681
        return SDL_InvalidParamError("mappingString");
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   682
    }
7260f8242948 Added handling of NULL as input for SDL_GameControllerAddMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9197
diff changeset
   683
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   684
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   685
    if (!pchGUID) {
8081
402b8c313246 Changed return -1 after SDL_SetError() to return SDL_SetError().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8067
diff changeset
   686
        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
   687
    }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   688
    if (!SDL_strcasecmp(pchGUID, "xinput")) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   689
        is_xinput_mapping = SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   690
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   691
    if (!SDL_strcasecmp(pchGUID, "emscripten")) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   692
        is_emscripten_mapping = SDL_TRUE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   693
    }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   694
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   695
    SDL_free(pchGUID);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   696
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   697
    pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 8052
diff changeset
   698
    if (!pchName) {
8081
402b8c313246 Changed return -1 after SDL_SetError() to return SDL_SetError().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8067
diff changeset
   699
        return SDL_SetError("Couldn't parse name from %s", mappingString);
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 8052
diff changeset
   700
    }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   701
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   702
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   703
    if (!pchMapping) {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   704
        SDL_free(pchName);
8081
402b8c313246 Changed return -1 after SDL_SetError() to return SDL_SetError().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8067
diff changeset
   705
        return SDL_SetError("Couldn't parse %s", mappingString);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   706
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   707
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 8052
diff changeset
   708
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 8052
diff changeset
   709
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   710
    if (pControllerMapping) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   711
        /* Update existing mapping */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   712
        SDL_free(pControllerMapping->name);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   713
        pControllerMapping->name = pchName;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   714
        SDL_free(pControllerMapping->mapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   715
        pControllerMapping->mapping = pchMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   716
        /* refresh open controllers */
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   717
        SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   718
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   719
    } else {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   720
        pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   721
        if (!pControllerMapping) {
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   722
            SDL_free(pchName);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   723
            SDL_free(pchMapping);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   724
            return SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   725
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   726
        if (is_xinput_mapping) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   727
            s_pXInputMapping = pControllerMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   728
        }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   729
        if (is_emscripten_mapping) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   730
            s_pEmscriptenMapping = pControllerMapping;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 9198
diff changeset
   731
        }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   732
        pControllerMapping->guid = jGUID;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   733
        pControllerMapping->name = pchName;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   734
        pControllerMapping->mapping = pchMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   735
        pControllerMapping->next = s_pSupportedControllers;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   736
        s_pSupportedControllers = pControllerMapping;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   737
        return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   738
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   739
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   740
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   741
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   742
 * Get the mapping string for this GUID
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   743
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   744
char *
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   745
SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   746
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   747
    char *pMappingString = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   748
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   749
    if (mapping) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   750
        char pchGUID[33];
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   751
        size_t needed;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   752
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   753
        /* 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
   754
        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
   755
        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
   756
        if (!pMappingString) {
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   757
            SDL_OutOfMemory();
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   758
            return NULL;
90bb83c49dea Fixed SDL_GameControllerMappingForGUID() crashing if no more memory available.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9196
diff changeset
   759
        }
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   760
        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
   761
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   762
    return pMappingString;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   763
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   764
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   765
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   766
 * Get the mapping string for this device
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   767
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   768
char *
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   769
SDL_GameControllerMapping(SDL_GameController * gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   770
{
9196
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   771
    if (!gamecontroller) {
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   772
        return NULL;
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   773
    }
3c3a84b56da0 Added handling of NULL as input for SDL_GameControllerMapping().
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9195
diff changeset
   774
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   775
    return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   776
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   777
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   778
static void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   779
SDL_GameControllerLoadHints()
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   780
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   781
    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
   782
    if (hint && hint[0]) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   783
        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
   784
        char *pUserMappings = SDL_malloc(nchHints + 1);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   785
        char *pTempMappings = pUserMappings;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   786
        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
   787
        pUserMappings[nchHints] = '\0';
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   788
        while (pUserMappings) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   789
            char *pchNewLine = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   790
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   791
            pchNewLine = SDL_strchr(pUserMappings, '\n');
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   792
            if (pchNewLine)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   793
                *pchNewLine = '\0';
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
            SDL_GameControllerAddMapping(pUserMappings);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   796
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   797
            if (pchNewLine) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   798
                pUserMappings = pchNewLine + 1;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   799
            } else {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   800
                pUserMappings = NULL;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   801
            }
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   802
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   803
        SDL_free(pTempMappings);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   804
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   805
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   806
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
 * 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
   809
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   810
int
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   811
SDL_GameControllerInit(void)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   812
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   813
    int i = 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   814
    const char *pMappingString = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   815
    s_pSupportedControllers = NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   816
    pMappingString = s_ControllerMappings[i];
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   817
    while (pMappingString) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   818
        SDL_GameControllerAddMapping(pMappingString);
8582
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
        i++;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   821
        pMappingString = s_ControllerMappings[i];
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   824
    /* load in any user supplied config */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   825
    SDL_GameControllerLoadHints();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   826
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   827
    /* 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
   828
    SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   829
8067
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   830
    /* 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
   831
    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
   832
        if (SDL_IsGameController(i)) {
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   833
            SDL_Event deviceevent;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   834
            deviceevent.type = SDL_CONTROLLERDEVICEADDED;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   835
            deviceevent.cdevice.which = i;
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   836
            SDL_PushEvent(&deviceevent);
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   837
        }
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   838
    }
8314a9008bf1 Generate SDL_CONTROLLERDEVICEADDED events for controllers connected at startup
Sam Lantinga <slouken@libsdl.org>
parents: 8053
diff changeset
   839
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   840
    return (0);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   841
}
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
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
 * 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
   846
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   847
const char *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   848
SDL_GameControllerNameForIndex(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   849
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   850
    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
   851
    if (pSupportedController) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   852
        return pSupportedController->name;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   853
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   854
    return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   855
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   856
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
 * 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
   860
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   861
SDL_bool
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   862
SDL_IsGameController(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   863
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   864
    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
   865
    if (pSupportedController) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   866
        return SDL_TRUE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   867
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   868
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   869
    return SDL_FALSE;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   870
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   871
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   872
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   873
 * 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
   874
 * 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
   875
 * identify this controller in future controller events.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   876
 *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   877
 * 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
   878
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   879
SDL_GameController *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   880
SDL_GameControllerOpen(int device_index)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   881
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   882
    SDL_GameController *gamecontroller;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   883
    SDL_GameController *gamecontrollerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   884
    ControllerMapping_t *pSupportedController = NULL;
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
    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
   887
        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
   888
        return (NULL);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   889
    }
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
    gamecontrollerlist = SDL_gamecontrollers;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   892
    /* 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
   893
    while (gamecontrollerlist) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   894
        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
   895
                gamecontroller = gamecontrollerlist;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   896
                ++gamecontroller->ref_count;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   897
                return (gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   898
        }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   899
        gamecontrollerlist = gamecontrollerlist->next;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   900
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   901
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   902
    /* Find a controller mapping */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   903
    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
   904
    if (!pSupportedController) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   905
        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
   906
        return (NULL);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   907
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   908
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   909
    /* Create and initialize the joystick */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   910
    gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   911
    if (gamecontroller == NULL) {
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   912
        SDL_OutOfMemory();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   913
        return NULL;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   914
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   915
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   916
    SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   917
    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
   918
    if (!gamecontroller->joystick) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   919
        SDL_free(gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   920
        return NULL;
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
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   923
    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
   924
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   925
    /* Add joystick to list */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   926
    ++gamecontroller->ref_count;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   927
    /* Link the joystick in the list */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   928
    gamecontroller->next = SDL_gamecontrollers;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   929
    SDL_gamecontrollers = gamecontroller;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   930
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   931
    SDL_SYS_JoystickUpdate(gamecontroller->joystick);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   932
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   933
    return (gamecontroller);
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   934
}
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
 * Manually pump for controller updates.
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
void
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   940
SDL_GameControllerUpdate(void)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   941
{
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   942
    /* 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
   943
    SDL_JoystickUpdate();
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   944
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   945
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   946
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
 * Get the current state of an axis control on a controller
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   949
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   950
Sint16
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   951
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   952
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   953
    if (!gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   954
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   955
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   956
    if (gamecontroller->mapping.axes[axis] >= 0) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   957
        Sint16 value = (SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axes[axis]));
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   958
        switch (axis) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   959
            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   960
            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   961
                /* Shift it to be 0 - 32767. */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   962
                value = value / 2 + 16384;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   963
            default:
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   964
                break;
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
        return value;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   967
    } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   968
        Uint8 value;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   969
        value = SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis]);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   970
        if (value > 0)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   971
            return 32767;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   972
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   973
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   974
    return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   975
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   976
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   977
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
 * Get the current state of a button on a controller
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
Uint8
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   982
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   983
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   984
    if (!gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   985
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   986
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   987
    if (gamecontroller->mapping.buttons[button] >= 0) {
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   988
        return (SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttons[button]));
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   989
    } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   990
        Sint16 value;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   991
        value = SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button]);
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   992
        if (ABS(value) > 32768/2)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   993
            return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   994
        return 0;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   995
    } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   996
        Uint8 value;
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   997
        value = SDL_JoystickGetHat(gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat);
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
   998
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
   999
        if (value & gamecontroller->mapping.hatasbutton[button].mask)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1000
            return 1;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1001
        return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1002
    }
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1003
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1004
    return 0;
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1005
}
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1006
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1007
/*
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1008
 * Return if the joystick in question is currently attached to the system,
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1009
 *  \return 0 if not plugged in, 1 if still present.
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1010
 */
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1011
SDL_bool
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
  1012
SDL_GameControllerGetAttached(SDL_GameController * gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1013
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
  1014
    if (!gamecontroller)
8582
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1015
        return SDL_FALSE;
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
    return SDL_JoystickGetAttached(gamecontroller->joystick);
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
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1020
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1021
const char *
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1022
SDL_GameControllerName(SDL_GameController * gamecontroller)
c3e9a2b93517 Fixed line endings on WinRT source code
Sam Lantinga <slouken@libsdl.org>
parents: 8543
diff changeset
  1023
{
8772
4f34097233f9 Updated coding style to match the rest of SDL
Sam Lantinga <slouken@libsdl.org>
parents: 8583
diff changeset
  1024
    if (!gamecontroller)