src/joystick/SDL_gamecontroller.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 26 May 2013 11:34:04 -0700
changeset 7223 76fa20889de8
parent 7191 75360622e65f
child 7247 6a4570f12c20
permissions -rw-r--r--
Fixed compile errors building with mingw64
slouken@6690
     1
/*
slouken@6690
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@6690
     4
slouken@6690
     5
  This software is provided 'as-is', without any express or implied
slouken@6690
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6690
     7
  arising from the use of this software.
slouken@6690
     8
slouken@6690
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6690
    10
  including commercial applications, and to alter it and redistribute it
slouken@6690
    11
  freely, subject to the following restrictions:
slouken@6690
    12
slouken@6690
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6690
    14
     claim that you wrote the original software. If you use this software
slouken@6690
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6690
    16
     appreciated but is not required.
slouken@6690
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6690
    18
     misrepresented as being the original software.
slouken@6690
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6690
    20
*/
slouken@6690
    21
#include "SDL_config.h"
slouken@6690
    22
slouken@6690
    23
/* This is the game controller API for Simple DirectMedia Layer */
slouken@6690
    24
slouken@6690
    25
#include "SDL_events.h"
slouken@6690
    26
#include "SDL_assert.h"
slouken@6690
    27
#include "SDL_sysjoystick.h"
slouken@6690
    28
#include "SDL_hints.h"
slouken@6690
    29
slouken@6690
    30
#if !SDL_EVENTS_DISABLED
slouken@6690
    31
#include "../events/SDL_events_c.h"
slouken@6690
    32
#endif
slouken@6690
    33
#define ABS(_x) ((_x) < 0 ? -(_x) : (_x))
slouken@6690
    34
slouken@6690
    35
slouken@6690
    36
/* a list of currently opened game controllers */
slouken@6690
    37
static SDL_GameController *SDL_gamecontrollers = NULL;
slouken@6690
    38
slouken@6843
    39
/* keep track of the hat and mask value that transforms this hat movement into a button/axis press */
slouken@6843
    40
struct _SDL_HatMapping
slouken@6690
    41
{
slouken@7191
    42
    int hat;
slouken@7191
    43
    Uint8 mask;
slouken@6690
    44
};
slouken@6690
    45
slouken@6690
    46
#define k_nMaxReverseEntries 20
slouken@6690
    47
urkle@6964
    48
/**
urkle@6964
    49
 * We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask
urkle@6964
    50
 * MAX 4 hats supported
urkle@6964
    51
 */
slouken@6843
    52
#define k_nMaxHatEntries 0x3f + 1
slouken@6843
    53
slouken@6690
    54
/* our in memory mapping db between joystick objects and controller mappings*/
slouken@6690
    55
struct _SDL_ControllerMapping
slouken@6690
    56
{
slouken@7191
    57
    SDL_JoystickGUID guid;
slouken@7191
    58
    const char *name;
slouken@6690
    59
slouken@7191
    60
    /* mapping of axis/button id to controller version */
slouken@7191
    61
    int axes[SDL_CONTROLLER_AXIS_MAX];
slouken@7191
    62
    int buttonasaxis[SDL_CONTROLLER_AXIS_MAX];
slouken@6842
    63
slouken@7191
    64
    int buttons[SDL_CONTROLLER_BUTTON_MAX];
slouken@7191
    65
    int axesasbutton[SDL_CONTROLLER_BUTTON_MAX];
slouken@7191
    66
    struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX];
slouken@6690
    67
slouken@7191
    68
    /* reverse mapping, joystick indices to buttons */
slouken@7191
    69
    SDL_GameControllerAxis raxes[k_nMaxReverseEntries];
slouken@7191
    70
    SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries];
slouken@6842
    71
slouken@7191
    72
    SDL_GameControllerButton rbuttons[k_nMaxReverseEntries];
slouken@7191
    73
    SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries];
slouken@7191
    74
    SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries];
slouken@6842
    75
slouken@6690
    76
};
slouken@6690
    77
slouken@6690
    78
slouken@6690
    79
/* our hard coded list of mapping support */
slouken@6690
    80
typedef struct _ControllerMapping_t
slouken@6690
    81
{
slouken@7191
    82
    SDL_JoystickGUID guid;
slouken@7191
    83
    char *name;
slouken@7191
    84
    char *mapping;
slouken@7191
    85
    struct _ControllerMapping_t *next;
slouken@6690
    86
} ControllerMapping_t;
slouken@6690
    87
slouken@6690
    88
slouken@6690
    89
/* default mappings we support */
slouken@6690
    90
const char *s_ControllerMappings [] =
slouken@6690
    91
{
slouken@6701
    92
#ifdef SDL_JOYSTICK_DINPUT
slouken@7191
    93
    "xinput,X360 Controller,a:b10,b:b11,y:b13,x:b12,start:b4,guide:b14,back:b5,dpup:b0,dpleft:b2,dpdown:b1,dpright:b3,leftshoulder:b8,rightshoulder:b9,leftstick:b6,rightstick:b7,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:a4,righttrigger:a5",
slouken@7191
    94
    "341a3608000000000000504944564944,Afterglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7",
slouken@7191
    95
    "88880803000000000000504944564944,PS3 Controller,a:b2,b:b1,x:b0,y:b3,start:b11,back:b8,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.4,dpdown:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:b6,righttrigger:b7,guide:b12",
slouken@7191
    96
    "4c056802000000000000504944564944,PS3 Controller,a:b14,b:b13,y:b12,x:b15,start:b3,guide:b16,back:b0,leftstick:b1,rightstick:b2,leftshoulder:b10,rightshoulder:b11,dpup:b4,dpleft:b7,dpdown:b6,dpright:b5,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9,",
slouken@7191
    97
    "25090500000000000000504944564944,PS3 DualShock,a:b2,b:b1,x:b0,y:b3,start:b8,guide:,back:b9,leftstick:b10,rightstick:b11,leftshoulder:b6,rightshoulder:b7,dpup:h0.1,dpleft:h0.4,dpdown:h0.8,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b4,righttrigger:b5",
slouken@7027
    98
    "ffff0000000000000000504944564944,GameStop Gamepad,a:b0,b:b1,y:b3,x:b2,start:b9,guide:,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,",
jorgen@7047
    99
    "6d0416c2000000000000504944564944,Generic DirectInput Controller,a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,",
slouken@6690
   100
#elif defined(__MACOSX__)
slouken@7191
   101
    "5e040000000000008e02000000000000,X360 Controller,a:b0,b:b1,y:b3,x:b2,start:b8,guide:b10,back:b9,dpup:b11,dpleft:b13,dpdown:b12,dpright:b14,leftshoulder:b4,rightshoulder:b5,leftstick:b6,rightstick:b7,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5",
slouken@7191
   102
    "4c050000000000006802000000000000,PS3 Controller,a:b14,b:b13,x:b12,y:b15,start:b3,guide:b16,back:b0,leftstick:b1,rightstick:b2,leftshoulder:b10,rightshoulder:b11,dpup:b4,dpleft:b6,dpdown:b7,dpright:b5,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9",
slouken@7027
   103
    "0500000047532047616d657061640000,GameStop Gamepad,a:b0,b:b1,y:b3,x:b2,start:b9,guide:,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,",
jorgen@7055
   104
    "6d040000000000001fc2000000000000,Logitech F710 Gamepad Controller (XInput),a:b0,b:b1,y:b3,x:b2,start:b8,guide:b10,back:b9,leftstick:b6,rightstick:b7,leftshoulder:b4,rightshoulder:b5,dpup:b11,dpleft:b13,dpdown:b12,dpright:b14,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,",
slouken@7191
   105
    "6d0400000000000016c2000000000000,Logitech F310 Gamepad Controller (DInput),a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,", /* Guide button doesn't seem to be sent in DInput mode. */
slouken@7191
   106
    "6d0400000000000019c2000000000000,Logitech Wireless Gamepad Controller (DInput),a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,", /* This includes F710 in DInput mode and the "Logitech Cordless RumblePad 2", at the very least. */
slouken@6690
   107
#elif defined(__LINUX__)
slouken@7191
   108
    "030000005e0400008e02000014010000,X360 Controller,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b9,rightstick:b10,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5",
jorgen@6997
   109
    "030000005e0400008e02000010010000,X360 Controller,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,",
jorgen@6997
   110
    "030000005e0400001907000000010000,X360 Wireless Controller,a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:b13,dpleft:b11,dpdown:b14,dpright:b12,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,",
slouken@7191
   111
    "030000004c0500006802000011010000,PS3 Controller,a:b14,b:b13,x:b15,y:b12,start:b3,guide:b16,back:b0,leftstick:b1,rightstick:b2,leftshoulder:b10,rightshoulder:b11,dpup:b4,dpleft:b7,dpdown:b6,dpright:b5,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b8,righttrigger:b9",
jorgen@6997
   112
    "030000006d0400001fc2000005030000,Logitech F710 Gamepad Controller (XInput),a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,",
slouken@7191
   113
    "030000006d04000019c2000011010000,Logitech F710 Gamepad Controller (DInput),a:b1,b:b2,y:b3,x:b0,start:b9,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,", /* Guide button doesn't seem to be sent in DInput mode. */
jorgen@6997
   114
    "030000006d0400001dc2000014400000,Logitech F310 Gamepad (XInput),a:b0,b:b1,y:b3,x:b2,start:b7,guide:b8,back:b6,leftstick:b9,rightstick:b10,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a4,lefttrigger:a2,righttrigger:a5,",
jorgen@6998
   115
    "030000006d04000019c2000010010000,Logitech Cordless RumblePad 2,a:b1,b:b2,y:b3,x:b0,start:b9,guide:,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,",
slouken@7027
   116
    "0500000047532047616d657061640000,GameStop Gamepad,a:b0,b:b1,y:b3,x:b2,start:b9,guide:,back:b8,leftstick:b10,rightstick:b11,leftshoulder:b4,rightshoulder:b5,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7,",
slouken@7181
   117
    "03000000ba2200002010000001010000,Jess Technology USB Game Controller,start:b9,a:b2,b:b1,x:b3,y:b0,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftx:a0,lefty:a1,rightx:a3,righty:a2,lefttrigger:b6,righttrigger:b7,leftshoulder:b4,rightshoulder:b5,guide:,back:b8",
slouken@6690
   118
#endif
slouken@7191
   119
    NULL
slouken@6690
   120
};
slouken@6690
   121
slouken@6690
   122
static ControllerMapping_t *s_pSupportedControllers = NULL;
slouken@6708
   123
#ifdef SDL_JOYSTICK_DINPUT
slouken@6690
   124
static ControllerMapping_t *s_pXInputMapping = NULL;
slouken@6708
   125
#endif
slouken@6690
   126
slouken@6690
   127
/* The SDL game controller structure */
slouken@6690
   128
struct _SDL_GameController
slouken@6690
   129
{
slouken@7191
   130
    SDL_Joystick *joystick; /* underlying joystick device */
slouken@7191
   131
    int ref_count;
slouken@7191
   132
    Uint8 hatState[4]; /* the current hat state for this controller */
slouken@7191
   133
    struct _SDL_ControllerMapping mapping; /* the mapping object for this controller */
slouken@7191
   134
    struct _SDL_GameController *next; /* pointer to next game controller we have allocated */
slouken@6690
   135
};
slouken@6690
   136
slouken@6690
   137
icculus@6917
   138
int SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value);
slouken@7191
   139
int SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state);
slouken@6690
   140
slouken@6690
   141
/*
slouken@6690
   142
 * Event filter to fire controller events from joystick ones
slouken@6690
   143
 */
slouken@6690
   144
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event * event)
slouken@6690
   145
{
slouken@7191
   146
    switch( event->type )
slouken@7191
   147
    {
slouken@7191
   148
    case SDL_JOYAXISMOTION:
slouken@7191
   149
        {
slouken@7191
   150
            SDL_GameController *controllerlist;
slouken@6850
   151
slouken@7191
   152
            if ( event->jaxis.axis >= k_nMaxReverseEntries ) break;
slouken@6843
   153
slouken@7191
   154
            controllerlist = SDL_gamecontrollers;
slouken@7191
   155
            while ( controllerlist )
slouken@7191
   156
            {
slouken@7191
   157
                if ( controllerlist->joystick->instance_id == event->jaxis.which )
slouken@7191
   158
                {
slouken@7191
   159
                    if ( controllerlist->mapping.raxes[event->jaxis.axis] >= 0 ) /* simple axis to axis, send it through */
slouken@7191
   160
                    {
slouken@7191
   161
                        SDL_GameControllerAxis axis = controllerlist->mapping.raxes[event->jaxis.axis];
jorgen@7080
   162
                        Sint16 value = event->jaxis.value;
slouken@7191
   163
                        switch (axis)
slouken@7191
   164
                        {
slouken@7191
   165
                            case SDL_CONTROLLER_AXIS_TRIGGERLEFT:
slouken@7191
   166
                            case SDL_CONTROLLER_AXIS_TRIGGERRIGHT:
slouken@7191
   167
                                /* Shift it to be 0 - 32767. */
slouken@7191
   168
                                value = value / 2 + 16384;
slouken@7191
   169
                            default:
slouken@7191
   170
                                break;
slouken@7191
   171
                        }
slouken@7191
   172
                        SDL_PrivateGameControllerAxis( controllerlist, axis, value );
slouken@7191
   173
                    }
slouken@7191
   174
                    else if ( controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0 ) /* simulate an axis as a button */
slouken@7191
   175
                    {
slouken@7191
   176
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED );
slouken@7191
   177
                    }
slouken@7191
   178
                    break;
slouken@7191
   179
                }
slouken@7191
   180
                controllerlist = controllerlist->next;
slouken@7191
   181
            }
slouken@7191
   182
        }
slouken@7191
   183
        break;
slouken@7191
   184
    case SDL_JOYBUTTONDOWN:
slouken@7191
   185
    case SDL_JOYBUTTONUP:
slouken@7191
   186
        {
slouken@7191
   187
            SDL_GameController *controllerlist;
slouken@6850
   188
slouken@7191
   189
            if ( event->jbutton.button >= k_nMaxReverseEntries ) break;
slouken@6843
   190
slouken@7191
   191
            controllerlist = SDL_gamecontrollers;
slouken@7191
   192
            while ( controllerlist )
slouken@7191
   193
            {
slouken@7191
   194
                if ( controllerlist->joystick->instance_id == event->jbutton.which )
slouken@7191
   195
                {
slouken@7191
   196
                    if ( controllerlist->mapping.rbuttons[event->jbutton.button] >= 0 ) /* simple button as button */
slouken@7191
   197
                    {
slouken@7191
   198
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rbuttons[event->jbutton.button], event->jbutton.state );
slouken@7191
   199
                    }
slouken@7191
   200
                    else if ( controllerlist->mapping.rbuttonasaxis[event->jbutton.button] >= 0 ) /* an button pretending to be an axis */
slouken@7191
   201
                    {
slouken@7191
   202
                        SDL_PrivateGameControllerAxis( controllerlist, controllerlist->mapping.rbuttonasaxis[event->jbutton.button], event->jbutton.state > 0 ? 32767 : 0 );
slouken@7191
   203
                    }
slouken@7191
   204
                    break;
slouken@7191
   205
                }
slouken@7191
   206
                controllerlist = controllerlist->next;
slouken@7191
   207
            }
slouken@7191
   208
        }
slouken@7191
   209
        break;
slouken@7191
   210
    case SDL_JOYHATMOTION:
slouken@7191
   211
        {
slouken@7191
   212
            SDL_GameController *controllerlist;
slouken@6850
   213
slouken@7191
   214
            if ( event->jhat.hat >= 4 ) break;
slouken@6843
   215
slouken@7191
   216
            controllerlist = SDL_gamecontrollers;
slouken@7191
   217
            while ( controllerlist )
slouken@7191
   218
            {
slouken@7191
   219
                if ( controllerlist->joystick->instance_id == event->jhat.which )
slouken@7191
   220
                {
slouken@7191
   221
                    Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
slouken@7191
   222
                    /* Get list of removed bits (button release) */
slouken@7191
   223
                    Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
slouken@7191
   224
                    /* the hat idx in the high nibble */
slouken@7191
   225
                    int bHighHat = event->jhat.hat << 4;
slouken@6843
   226
slouken@7191
   227
                    if ( bChanged & SDL_HAT_DOWN )
slouken@7191
   228
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED );
slouken@7191
   229
                    if ( bChanged & SDL_HAT_UP )
slouken@7191
   230
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED );
slouken@7191
   231
                    if ( bChanged & SDL_HAT_LEFT )
slouken@7191
   232
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED );
slouken@7191
   233
                    if ( bChanged & SDL_HAT_RIGHT )
slouken@7191
   234
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED );
slouken@6843
   235
slouken@7191
   236
                    /* Get list of added bits (button press) */
slouken@7191
   237
                    bChanged = event->jhat.value ^ bSame;
slouken@6843
   238
slouken@7191
   239
                    if ( bChanged & SDL_HAT_DOWN )
slouken@7191
   240
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED );
slouken@7191
   241
                    if ( bChanged & SDL_HAT_UP )
slouken@7191
   242
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED );
slouken@7191
   243
                    if ( bChanged & SDL_HAT_LEFT )
slouken@7191
   244
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED );
slouken@7191
   245
                    if ( bChanged & SDL_HAT_RIGHT )
slouken@7191
   246
                        SDL_PrivateGameControllerButton( controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED );
slouken@6843
   247
slouken@7191
   248
                    /* update our state cache */
slouken@7191
   249
                    controllerlist->hatState[event->jhat.hat] = event->jhat.value;
slouken@6843
   250
slouken@7191
   251
                    break;
slouken@7191
   252
                }
slouken@7191
   253
                controllerlist = controllerlist->next;
slouken@7191
   254
            }
slouken@7191
   255
        }
slouken@7191
   256
        break;
slouken@7191
   257
    case SDL_JOYDEVICEADDED:
slouken@7191
   258
        {
slouken@7191
   259
            if ( SDL_IsGameController(event->jdevice.which ) )
slouken@7191
   260
            {
slouken@7191
   261
                SDL_Event deviceevent;
slouken@7191
   262
                deviceevent.type = SDL_CONTROLLERDEVICEADDED;
slouken@7191
   263
                deviceevent.cdevice.which = event->jdevice.which;
slouken@7191
   264
                SDL_PushEvent(&deviceevent);
slouken@7191
   265
            }
slouken@7191
   266
        }
slouken@7191
   267
        break;
slouken@7191
   268
    case SDL_JOYDEVICEREMOVED:
slouken@7191
   269
        {
slouken@7191
   270
            SDL_GameController *controllerlist = SDL_gamecontrollers;
slouken@7191
   271
            while ( controllerlist )
slouken@7191
   272
            {
slouken@7191
   273
                if ( controllerlist->joystick->instance_id == event->jdevice.which )
slouken@7191
   274
                {
slouken@7191
   275
                    SDL_Event deviceevent;
slouken@7191
   276
                    deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
slouken@7191
   277
                    deviceevent.cdevice.which = event->jdevice.which;
slouken@7191
   278
                    SDL_PushEvent(&deviceevent);
slouken@7191
   279
                    break;
slouken@7191
   280
                }
slouken@7191
   281
                controllerlist = controllerlist->next;
slouken@7191
   282
            }
slouken@7191
   283
        }
slouken@7191
   284
        break;
slouken@7191
   285
    default:
slouken@7191
   286
        break;
slouken@7191
   287
    }
slouken@6690
   288
slouken@7191
   289
    return 1;
slouken@6690
   290
}
slouken@6690
   291
slouken@6690
   292
/*
urkle@6964
   293
 * Helper function to scan the mappings database for a controller with the specified GUID
urkle@6964
   294
 */
urkle@6964
   295
ControllerMapping_t *SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
urkle@6964
   296
{
slouken@7191
   297
    ControllerMapping_t *pSupportedController = s_pSupportedControllers;
slouken@7191
   298
    while ( pSupportedController )
slouken@7191
   299
    {
slouken@7191
   300
        if ( !SDL_memcmp( guid, &pSupportedController->guid, sizeof(*guid) ) )
slouken@7191
   301
        {
slouken@7191
   302
            return pSupportedController;
slouken@7191
   303
        }
slouken@7191
   304
        pSupportedController = pSupportedController->next;
slouken@7191
   305
    }
philipp@7129
   306
    return NULL;
philipp@7129
   307
}
urkle@6964
   308
urkle@6964
   309
/*
philipp@7133
   310
 * Helper function to determine pre-calculated offset to certain joystick mappings
slouken@6690
   311
 */
slouken@6690
   312
ControllerMapping_t *SDL_PrivateGetControllerMapping(int device_index)
slouken@6690
   313
{
slouken@6701
   314
#ifdef SDL_JOYSTICK_DINPUT
slouken@7191
   315
    if ( SDL_SYS_IsXInputDeviceIndex(device_index) && s_pXInputMapping )
slouken@7191
   316
    {
slouken@7191
   317
        return s_pXInputMapping;
slouken@7191
   318
    }
slouken@7191
   319
    else
slouken@6690
   320
#endif
slouken@7191
   321
    {
slouken@7191
   322
        SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID( device_index );
slouken@7191
   323
        return SDL_PrivateGetControllerMappingForGUID(&jGUID);
slouken@7191
   324
    }
slouken@6841
   325
slouken@7191
   326
    return NULL;
slouken@6690
   327
}
slouken@6690
   328
urkle@6964
   329
static const char* map_StringForControllerAxis[] = {
urkle@6964
   330
    "leftx",
urkle@6964
   331
    "lefty",
urkle@6964
   332
    "rightx",
urkle@6964
   333
    "righty",
urkle@6964
   334
    "lefttrigger",
urkle@6964
   335
    "righttrigger",
urkle@6964
   336
    NULL
urkle@6964
   337
};
urkle@6964
   338
slouken@6690
   339
/*
slouken@6690
   340
 * convert a string to its enum equivalent
slouken@6690
   341
 */
icculus@6917
   342
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString( const char *pchString )
slouken@6690
   343
{
urkle@6964
   344
    int entry;
slouken@7191
   345
    if ( !pchString || !pchString[0] )
slouken@7191
   346
        return SDL_CONTROLLER_AXIS_INVALID;
slouken@6690
   347
urkle@6964
   348
    for ( entry = 0; map_StringForControllerAxis[entry]; ++entry)
urkle@6964
   349
    {
urkle@6964
   350
        if ( !SDL_strcasecmp( pchString, map_StringForControllerAxis[entry] ) )
urkle@6964
   351
            return entry;
urkle@6964
   352
    }
philipp@7129
   353
    return SDL_CONTROLLER_AXIS_INVALID;
slouken@6690
   354
}
slouken@6690
   355
urkle@6964
   356
/*
urkle@6964
   357
 * convert an enum to its string equivalent
urkle@6964
   358
 */
urkle@6964
   359
const char* SDL_GameControllerGetStringForAxis( SDL_GameControllerAxis axis )
urkle@6964
   360
{
urkle@6964
   361
    if (axis > SDL_CONTROLLER_AXIS_INVALID && axis < SDL_CONTROLLER_AXIS_MAX)
urkle@6964
   362
    {
urkle@6964
   363
        return map_StringForControllerAxis[axis];
urkle@6964
   364
    }
urkle@6964
   365
    return NULL;
urkle@6964
   366
}
urkle@6964
   367
urkle@6964
   368
static const char* map_StringForControllerButton[] = {
urkle@6964
   369
    "a",
urkle@6964
   370
    "b",
urkle@6964
   371
    "x",
urkle@6964
   372
    "y",
urkle@6964
   373
    "back",
urkle@6964
   374
    "guide",
urkle@6964
   375
    "start",
urkle@6964
   376
    "leftstick",
urkle@6964
   377
    "rightstick",
urkle@6964
   378
    "leftshoulder",
urkle@6964
   379
    "rightshoulder",
urkle@6964
   380
    "dpup",
urkle@6964
   381
    "dpdown",
urkle@6964
   382
    "dpleft",
urkle@6964
   383
    "dpright",
urkle@6964
   384
    NULL
urkle@6964
   385
};
slouken@6690
   386
slouken@6690
   387
/*
slouken@6690
   388
 * convert a string to its enum equivalent
slouken@6690
   389
 */
icculus@6917
   390
SDL_GameControllerButton SDL_GameControllerGetButtonFromString( const char *pchString )
slouken@6690
   391
{
urkle@6964
   392
    int entry;
slouken@7191
   393
    if ( !pchString || !pchString[0] )
slouken@7191
   394
        return SDL_CONTROLLER_BUTTON_INVALID;
slouken@6690
   395
urkle@6964
   396
    for ( entry = 0; map_StringForControllerButton[entry]; ++entry)
slouken@7191
   397
    {
slouken@7191
   398
        if ( !SDL_strcasecmp( pchString, map_StringForControllerButton[entry] ) )
slouken@7191
   399
        return entry;
slouken@7191
   400
    }
slouken@7191
   401
    return SDL_CONTROLLER_BUTTON_INVALID;
slouken@6690
   402
}
slouken@6690
   403
urkle@6964
   404
/*
urkle@6964
   405
 * convert an enum to its string equivalent
urkle@6964
   406
 */
urkle@6964
   407
const char* SDL_GameControllerGetStringForButton( SDL_GameControllerButton axis )
urkle@6964
   408
{
urkle@6964
   409
    if (axis > SDL_CONTROLLER_BUTTON_INVALID && axis < SDL_CONTROLLER_BUTTON_MAX)
urkle@6964
   410
    {
urkle@6964
   411
        return map_StringForControllerButton[axis];
urkle@6964
   412
    }
urkle@6964
   413
    return NULL;
urkle@6964
   414
}
slouken@6690
   415
slouken@6690
   416
/*
slouken@6690
   417
 * given a controller button name and a joystick name update our mapping structure with it
slouken@6690
   418
 */
slouken@6690
   419
void SDL_PrivateGameControllerParseButton( const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping )
slouken@6690
   420
{
slouken@7191
   421
    int iSDLButton = 0;
slouken@7191
   422
    SDL_GameControllerButton button;
slouken@7191
   423
    SDL_GameControllerAxis axis;
slouken@7191
   424
    button = SDL_GameControllerGetButtonFromString( szGameButton );
slouken@7191
   425
    axis = SDL_GameControllerGetAxisFromString( szGameButton );
slouken@7191
   426
    iSDLButton = SDL_atoi( &szJoystickButton[1] );
slouken@6690
   427
slouken@7191
   428
    if ( szJoystickButton[0] == 'a' )
slouken@7191
   429
    {
slouken@7191
   430
        if ( iSDLButton >= k_nMaxReverseEntries )
slouken@7191
   431
        {
slouken@7191
   432
            SDL_SetError("Axis index too large: %d", iSDLButton );
slouken@7191
   433
            return;
slouken@7191
   434
        }
slouken@7191
   435
        if ( axis != SDL_CONTROLLER_AXIS_INVALID )
slouken@7191
   436
        {
slouken@7191
   437
            pMapping->axes[ axis ] = iSDLButton;
slouken@7191
   438
            pMapping->raxes[ iSDLButton ] = axis;
slouken@7191
   439
        }
slouken@7191
   440
        else if ( button != SDL_CONTROLLER_BUTTON_INVALID )
slouken@7191
   441
        {
slouken@7191
   442
            pMapping->axesasbutton[ button ] = iSDLButton;
slouken@7191
   443
            pMapping->raxesasbutton[ iSDLButton ] = button;
slouken@7191
   444
        }
slouken@7191
   445
        else
slouken@7191
   446
        {
slouken@7191
   447
            SDL_assert( !"How did we get here?" );
slouken@7191
   448
        }
slouken@6690
   449
slouken@7191
   450
    }
slouken@7191
   451
    else if ( szJoystickButton[0] == 'b' )
slouken@7191
   452
    {
slouken@7191
   453
        if ( iSDLButton >= k_nMaxReverseEntries )
slouken@7191
   454
        {
slouken@7191
   455
            SDL_SetError("Button index too large: %d", iSDLButton );
slouken@7191
   456
            return;
slouken@7191
   457
        }
slouken@7191
   458
        if ( button != SDL_CONTROLLER_BUTTON_INVALID )
slouken@7191
   459
        {
slouken@7191
   460
            pMapping->buttons[ button ] = iSDLButton;
slouken@7191
   461
            pMapping->rbuttons[ iSDLButton ] = button;
slouken@7191
   462
        }
slouken@7191
   463
        else if ( axis != SDL_CONTROLLER_AXIS_INVALID )
slouken@7191
   464
        {
slouken@7191
   465
            pMapping->buttonasaxis[ axis ] = iSDLButton;
slouken@7191
   466
            pMapping->rbuttonasaxis[ iSDLButton ] = axis;
slouken@7191
   467
        }
slouken@7191
   468
        else
slouken@7191
   469
        {
slouken@7191
   470
            SDL_assert( !"How did we get here?" );
slouken@7191
   471
        }
slouken@7191
   472
    }
slouken@7191
   473
    else if ( szJoystickButton[0] == 'h' )
slouken@7191
   474
    {
slouken@7191
   475
        int hat = SDL_atoi( &szJoystickButton[1] );
slouken@7191
   476
        int mask = SDL_atoi( &szJoystickButton[3] );
slouken@7191
   477
        if (hat >= 4) {
slouken@7191
   478
            SDL_SetError("Hat index too large: %d", iSDLButton );
slouken@7191
   479
        }
slouken@6690
   480
slouken@7191
   481
        if ( button != SDL_CONTROLLER_BUTTON_INVALID )
slouken@7191
   482
        {
slouken@6854
   483
            int ridx;
slouken@7191
   484
            pMapping->hatasbutton[ button ].hat = hat;
slouken@7191
   485
            pMapping->hatasbutton[ button ].mask = mask;
slouken@7191
   486
            ridx = (hat << 4) | mask;
slouken@7191
   487
            pMapping->rhatasbutton[ ridx ] = button;
slouken@7191
   488
        }
slouken@7191
   489
        else if ( axis != SDL_CONTROLLER_AXIS_INVALID )
slouken@7191
   490
        {
slouken@7191
   491
            SDL_assert( !"Support hat as axis" );
slouken@7191
   492
        }
slouken@7191
   493
        else
slouken@7191
   494
        {
slouken@7191
   495
            SDL_assert( !"How did we get here?" );
slouken@7191
   496
        }
slouken@7191
   497
    }
slouken@6690
   498
}
slouken@6690
   499
slouken@6690
   500
slouken@6690
   501
/*
slouken@6690
   502
 * given a controller mapping string update our mapping object
slouken@6690
   503
 */
slouken@6690
   504
static void
slouken@6690
   505
SDL_PrivateGameControllerParseControllerConfigString( struct _SDL_ControllerMapping *pMapping, const char *pchString )
slouken@6690
   506
{
slouken@7191
   507
    char szGameButton[20];
slouken@7191
   508
    char szJoystickButton[20];
slouken@7191
   509
    SDL_bool bGameButton = SDL_TRUE;
slouken@7191
   510
    int i = 0;
slouken@7191
   511
    const char *pchPos = pchString;
slouken@6690
   512
slouken@7191
   513
    SDL_memset( szGameButton, 0x0, sizeof(szGameButton) );
slouken@7191
   514
    SDL_memset( szJoystickButton, 0x0, sizeof(szJoystickButton) );
slouken@6690
   515
slouken@7191
   516
    while ( pchPos && *pchPos )
slouken@7191
   517
    {
slouken@7191
   518
        if ( *pchPos == ':' )
slouken@7191
   519
        {
slouken@7191
   520
            i = 0;
slouken@7191
   521
            bGameButton = SDL_FALSE;
slouken@7191
   522
        }
slouken@7191
   523
        else if ( *pchPos == ' ' )
slouken@7191
   524
        {
slouken@6690
   525
slouken@7191
   526
        }
slouken@7191
   527
        else if ( *pchPos == ',' )
slouken@7191
   528
        {
slouken@7191
   529
            i = 0;
slouken@7191
   530
            bGameButton = SDL_TRUE;
slouken@7191
   531
            SDL_PrivateGameControllerParseButton( szGameButton, szJoystickButton, pMapping );
slouken@7191
   532
            SDL_memset( szGameButton, 0x0, sizeof(szGameButton) );
slouken@7191
   533
            SDL_memset( szJoystickButton, 0x0, sizeof(szJoystickButton) );
slouken@6690
   534
slouken@7191
   535
        }
slouken@7191
   536
        else if ( bGameButton )
slouken@7191
   537
        {
slouken@7191
   538
            if ( i >=  sizeof(szGameButton))
slouken@7191
   539
            {
slouken@7191
   540
                SDL_SetError( "Button name too large: %s", szGameButton );
slouken@7191
   541
                return;
slouken@7191
   542
            }
slouken@7191
   543
            szGameButton[i] = *pchPos;
slouken@7191
   544
            i++;
slouken@7191
   545
        }
slouken@7191
   546
        else
slouken@7191
   547
        {
slouken@7191
   548
            if ( i >=  sizeof(szJoystickButton))
slouken@7191
   549
            {
slouken@7191
   550
                SDL_SetError( "Joystick button name too large: %s", szJoystickButton );
slouken@7191
   551
                return;
slouken@7191
   552
            }
slouken@7191
   553
            szJoystickButton[i] = *pchPos;
slouken@7191
   554
            i++;
slouken@7191
   555
        }
slouken@7191
   556
        pchPos++;
slouken@7191
   557
    }
slouken@6690
   558
slouken@7191
   559
    SDL_PrivateGameControllerParseButton( szGameButton, szJoystickButton, pMapping );
slouken@6690
   560
slouken@6690
   561
}
slouken@6690
   562
slouken@6690
   563
/*
slouken@6690
   564
 * Make a new button mapping struct
slouken@6690
   565
 */
slouken@6738
   566
void SDL_PrivateLoadButtonMapping( struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping )
slouken@6690
   567
{
slouken@7191
   568
    int j;
slouken@6690
   569
slouken@7191
   570
    pMapping->guid = guid;
slouken@7191
   571
    pMapping->name = pchName;
slouken@6690
   572
slouken@7191
   573
    /* set all the button mappings to non defaults */
slouken@7191
   574
    for ( j = 0; j < SDL_CONTROLLER_AXIS_MAX; j++ )
slouken@7191
   575
    {
slouken@7191
   576
        pMapping->axes[j] = -1;
slouken@7191
   577
        pMapping->buttonasaxis[j] = -1;
slouken@7191
   578
    }
slouken@7191
   579
    for ( j = 0; j < SDL_CONTROLLER_BUTTON_MAX; j++ )
slouken@7191
   580
    {
slouken@7191
   581
        pMapping->buttons[j] = -1;
slouken@7191
   582
        pMapping->axesasbutton[j] = -1;
slouken@7191
   583
        pMapping->hatasbutton[j].hat = -1;
slouken@7191
   584
    }
slouken@6690
   585
slouken@7191
   586
    for ( j = 0; j < k_nMaxReverseEntries; j++ )
slouken@7191
   587
    {
slouken@7191
   588
        pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
slouken@7191
   589
        pMapping->rbuttonasaxis[j] = SDL_CONTROLLER_AXIS_INVALID;
slouken@7191
   590
        pMapping->rbuttons[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@7191
   591
        pMapping->raxesasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@7191
   592
    }
slouken@6843
   593
slouken@7191
   594
    for (j = 0; j < k_nMaxHatEntries; j++)
slouken@7191
   595
    {
slouken@7191
   596
        pMapping->rhatasbutton[j] = SDL_CONTROLLER_BUTTON_INVALID;
slouken@7191
   597
    }
slouken@6690
   598
slouken@7191
   599
    SDL_PrivateGameControllerParseControllerConfigString( pMapping, pchMapping );
slouken@6690
   600
}
slouken@6690
   601
slouken@6690
   602
slouken@6690
   603
/*
slouken@6690
   604
 * grab the guid string from a mapping string
slouken@6690
   605
 */
slouken@6690
   606
char *SDL_PrivateGetControllerGUIDFromMappingString( const char *pMapping )
slouken@6690
   607
{
slouken@7191
   608
    const char *pFirstComma = SDL_strchr( pMapping, ',' );
slouken@7191
   609
    if ( pFirstComma )
slouken@7191
   610
    {
slouken@7191
   611
        char *pchGUID = SDL_malloc( pFirstComma - pMapping + 1 );
slouken@7191
   612
        if ( !pchGUID )
slouken@7191
   613
        {
slouken@7191
   614
            SDL_OutOfMemory();
slouken@7191
   615
            return NULL;
slouken@7191
   616
        }
slouken@7191
   617
        SDL_memcpy( pchGUID, pMapping, pFirstComma - pMapping );
slouken@7191
   618
        pchGUID[ pFirstComma - pMapping ] = 0;
slouken@7191
   619
        return pchGUID;
slouken@7191
   620
    }
slouken@7191
   621
    return NULL;
slouken@6690
   622
}
slouken@6690
   623
slouken@6690
   624
slouken@6690
   625
/*
slouken@6690
   626
 * grab the name string from a mapping string
slouken@6690
   627
 */
slouken@6690
   628
char *SDL_PrivateGetControllerNameFromMappingString( const char *pMapping )
slouken@6690
   629
{
slouken@7191
   630
    const char *pFirstComma, *pSecondComma;
slouken@6823
   631
    char *pchName;
slouken@6823
   632
slouken@6823
   633
    pFirstComma = SDL_strchr( pMapping, ',' );
slouken@6823
   634
    if ( !pFirstComma )
slouken@6823
   635
        return NULL;
slouken@6823
   636
slouken@7191
   637
    pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
slouken@6823
   638
    if ( !pSecondComma )
slouken@6823
   639
        return NULL;
slouken@6823
   640
slouken@6823
   641
    pchName = SDL_malloc( pSecondComma - pFirstComma );
slouken@6823
   642
    if ( !pchName )
slouken@6823
   643
    {
slouken@6823
   644
        SDL_OutOfMemory();
slouken@6823
   645
        return NULL;
slouken@6823
   646
    }
slouken@6823
   647
    SDL_memcpy( pchName, pFirstComma + 1, pSecondComma - pFirstComma );
slouken@6823
   648
    pchName[ pSecondComma - pFirstComma - 1 ] = 0;
slouken@6823
   649
    return pchName;
slouken@6690
   650
}
slouken@6690
   651
slouken@6690
   652
slouken@6690
   653
/*
slouken@6690
   654
 * grab the button mapping string from a mapping string
slouken@6690
   655
 */
urkle@6964
   656
char *SDL_PrivateGetControllerMappingFromMappingString( const char *pMapping )
slouken@6690
   657
{
slouken@7191
   658
    const char *pFirstComma, *pSecondComma;
slouken@6823
   659
slouken@6823
   660
    pFirstComma = SDL_strchr( pMapping, ',' );
slouken@6823
   661
    if ( !pFirstComma )
slouken@6823
   662
        return NULL;
slouken@6823
   663
slouken@7191
   664
    pSecondComma = SDL_strchr( pFirstComma + 1, ',' );
slouken@6823
   665
    if ( !pSecondComma )
slouken@6823
   666
        return NULL;
slouken@6823
   667
urkle@6964
   668
    return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
urkle@6964
   669
}
urkle@6964
   670
urkle@6964
   671
void SDL_PrivateGameControllerRefreshMapping( ControllerMapping_t *pControllerMapping )
urkle@6964
   672
{
urkle@6964
   673
    SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
urkle@6964
   674
    while ( gamecontrollerlist )
urkle@6964
   675
    {
urkle@6964
   676
        if ( !SDL_memcmp( &gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid) ) )
urkle@6964
   677
        {
urkle@6964
   678
            SDL_Event event;
urkle@6964
   679
            event.type = SDL_CONTROLLERDEVICEREMAPPED;
urkle@6964
   680
            event.cdevice.which = gamecontrollerlist->joystick->instance_id;
urkle@6964
   681
            SDL_PushEvent(&event);
slouken@7191
   682
slouken@7191
   683
            /* Not really threadsafe.  Should this lock access within SDL_GameControllerEventWatcher? */
urkle@6964
   684
            SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
urkle@6964
   685
        }
slouken@7191
   686
urkle@6964
   687
        gamecontrollerlist = gamecontrollerlist->next;
urkle@6964
   688
    }
slouken@6690
   689
}
slouken@6690
   690
urkle@6964
   691
/*
urkle@6964
   692
 * Add or update an entry into the Mappings Database
urkle@6964
   693
 */
urkle@6964
   694
int
urkle@6964
   695
SDL_GameControllerAddMapping( const char *mappingString )
urkle@6964
   696
{
slouken@7191
   697
    char *pchGUID;
slouken@7191
   698
    char *pchName;
slouken@7191
   699
    char *pchMapping;
urkle@6964
   700
    SDL_JoystickGUID jGUID;
urkle@6964
   701
    ControllerMapping_t *pControllerMapping;
urkle@6964
   702
#ifdef SDL_JOYSTICK_DINPUT
urkle@6964
   703
    SDL_bool is_xinput_mapping = SDL_FALSE;
urkle@6964
   704
#endif
urkle@6964
   705
slouken@6975
   706
    pchGUID = SDL_PrivateGetControllerGUIDFromMappingString( mappingString );
slouken@6975
   707
    if (!pchGUID) {
slouken@6975
   708
        return -1;
slouken@6975
   709
    }
urkle@6964
   710
#ifdef SDL_JOYSTICK_DINPUT
urkle@6964
   711
    if ( !SDL_strcasecmp( pchGUID, "xinput" ) ) {
urkle@6964
   712
        is_xinput_mapping = SDL_TRUE;
urkle@6964
   713
    }
urkle@6964
   714
#endif
slouken@6975
   715
    jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
urkle@6964
   716
    SDL_free(pchGUID);
urkle@6964
   717
slouken@7191
   718
    pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
urkle@6964
   719
slouken@7191
   720
    pchName = SDL_PrivateGetControllerNameFromMappingString( mappingString );
slouken@7191
   721
    if (!pchName) return -1;
urkle@6964
   722
slouken@7191
   723
    pchMapping = SDL_PrivateGetControllerMappingFromMappingString( mappingString );
slouken@7191
   724
    if (!pchMapping) {
slouken@7191
   725
        SDL_free( pchName );
slouken@7191
   726
        return -1;
slouken@7191
   727
    }
urkle@6964
   728
slouken@7191
   729
    if (pControllerMapping) {
slouken@7191
   730
        /* Update existing mapping */
slouken@7191
   731
        SDL_free( pControllerMapping->name );
slouken@7191
   732
        pControllerMapping->name = pchName;
slouken@7191
   733
        SDL_free( pControllerMapping->mapping );
slouken@7191
   734
        pControllerMapping->mapping = pchMapping;
slouken@7191
   735
        /* refresh open controllers */
slouken@7191
   736
        SDL_PrivateGameControllerRefreshMapping( pControllerMapping );
slouken@7191
   737
        return 0;
slouken@7191
   738
    } else {
slouken@7191
   739
        pControllerMapping = SDL_malloc( sizeof(*pControllerMapping) );
slouken@7191
   740
        if (!pControllerMapping) {
slouken@7191
   741
            SDL_free( pchName );
slouken@7191
   742
            SDL_free( pchMapping );
slouken@7191
   743
            return SDL_OutOfMemory();
slouken@7191
   744
        }
urkle@6964
   745
#ifdef SDL_JOYSTICK_DINPUT
slouken@7191
   746
        if ( is_xinput_mapping )
slouken@7191
   747
        {
slouken@7191
   748
            s_pXInputMapping = pControllerMapping;
slouken@7191
   749
        }
urkle@6964
   750
#endif
slouken@7191
   751
        pControllerMapping->guid = jGUID;
slouken@7191
   752
        pControllerMapping->name = pchName;
slouken@7191
   753
        pControllerMapping->mapping = pchMapping;
slouken@7191
   754
        pControllerMapping->next = s_pSupportedControllers;
slouken@7191
   755
        s_pSupportedControllers = pControllerMapping;
slouken@7191
   756
        return 1;
slouken@7191
   757
    }
urkle@6964
   758
}
urkle@6964
   759
urkle@6964
   760
/*
urkle@6964
   761
 * Get the mapping string for this GUID
urkle@6964
   762
 */
urkle@6964
   763
char *
urkle@6964
   764
SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid )
urkle@6964
   765
{
slouken@7191
   766
    char *pMappingString = NULL;
slouken@7191
   767
    ControllerMapping_t *mapping = SDL_PrivateGetControllerMappingForGUID(&guid);
slouken@7191
   768
    if (mapping) {
slouken@7191
   769
        char pchGUID[33];
urkle@6964
   770
        size_t needed;
slouken@7191
   771
        SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
slouken@7191
   772
        /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
slouken@7191
   773
        needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
slouken@7191
   774
        pMappingString = SDL_malloc( needed );
slouken@7191
   775
        SDL_snprintf( pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping );
slouken@7191
   776
    }
slouken@7191
   777
    return pMappingString;
urkle@6964
   778
}
urkle@6964
   779
urkle@6964
   780
/*
urkle@6964
   781
 * Get the mapping string for this device
urkle@6964
   782
 */
urkle@6964
   783
char *
urkle@6964
   784
SDL_GameControllerMapping( SDL_GameController * gamecontroller )
urkle@6964
   785
{
slouken@7191
   786
    return SDL_GameControllerMappingForGUID( gamecontroller->mapping.guid );
urkle@6964
   787
}
slouken@6690
   788
slouken@6976
   789
static void
slouken@6976
   790
SDL_GameControllerLoadHints()
slouken@6976
   791
{
slouken@6976
   792
    const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
slouken@6976
   793
    if ( hint && hint[0] ) {
slouken@6976
   794
        int nchHints = SDL_strlen( hint );
slouken@6976
   795
        char *pUserMappings = SDL_malloc( nchHints + 1 );
slouken@6976
   796
        char *pTempMappings = pUserMappings;
slouken@6976
   797
        SDL_memcpy( pUserMappings, hint, nchHints );
slouken@6976
   798
        while ( pUserMappings ) {
slouken@6976
   799
            char *pchNewLine = NULL;
slouken@6976
   800
slouken@6976
   801
            pchNewLine = SDL_strchr( pUserMappings, '\n' );
slouken@6976
   802
            if ( pchNewLine )
slouken@6976
   803
                *pchNewLine = '\0';
slouken@6976
   804
slouken@6976
   805
            SDL_GameControllerAddMapping( pUserMappings );
slouken@6976
   806
slouken@6976
   807
            if ( pchNewLine )
slouken@6976
   808
                pUserMappings = pchNewLine + 1;
slouken@6976
   809
            else
slouken@6976
   810
                pUserMappings = NULL;
slouken@6976
   811
        }
slouken@6976
   812
        SDL_free(pTempMappings);
slouken@6976
   813
    }
slouken@6976
   814
}
slouken@6976
   815
slouken@6690
   816
/*
slouken@6690
   817
 * Initialize the game controller system, mostly load our DB of controller config mappings
slouken@6690
   818
 */
slouken@6690
   819
int
slouken@6690
   820
SDL_GameControllerInit(void)
slouken@6690
   821
{
slouken@7191
   822
    int i = 0;
slouken@7191
   823
    const char *pMappingString = NULL;
slouken@7191
   824
    s_pSupportedControllers = NULL;
slouken@7191
   825
    pMappingString = s_ControllerMappings[i];
slouken@7191
   826
    while ( pMappingString )
slouken@7191
   827
    {
slouken@7191
   828
        SDL_GameControllerAddMapping( pMappingString );
slouken@6690
   829
slouken@7191
   830
        i++;
slouken@7191
   831
        pMappingString = s_ControllerMappings[i];
slouken@7191
   832
    }
slouken@6690
   833
slouken@7191
   834
    /* load in any user supplied config */
slouken@6976
   835
    SDL_GameControllerLoadHints();
slouken@6690
   836
slouken@7191
   837
    /* watch for joy events and fire controller ones if needed */
slouken@7191
   838
    SDL_AddEventWatch( SDL_GameControllerEventWatcher, NULL );
slouken@6976
   839
slouken@7191
   840
    return (0);
slouken@6690
   841
}
slouken@6690
   842
slouken@6690
   843
slouken@6690
   844
/*
slouken@6690
   845
 * Get the implementation dependent name of a controller
slouken@6690
   846
 */
slouken@6690
   847
const char *
slouken@6690
   848
SDL_GameControllerNameForIndex(int device_index)
slouken@6690
   849
{
slouken@7191
   850
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   851
    if ( pSupportedController )
slouken@7191
   852
    {
slouken@7191
   853
        return pSupportedController->name;
slouken@7191
   854
    }
slouken@6690
   855
    return NULL;
slouken@6690
   856
}
slouken@6690
   857
slouken@6690
   858
slouken@6690
   859
/*
slouken@6690
   860
 * Return 1 if the joystick at this device index is a supported controller
slouken@6690
   861
 */
jorgen@6871
   862
SDL_bool
jorgen@6871
   863
SDL_IsGameController(int device_index)
slouken@6690
   864
{
slouken@7191
   865
    ControllerMapping_t *pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   866
    if ( pSupportedController )
slouken@7191
   867
    {
slouken@7191
   868
        return SDL_TRUE;
slouken@7191
   869
    }
jorgen@6871
   870
slouken@7191
   871
    return SDL_FALSE;
slouken@6690
   872
}
slouken@6690
   873
slouken@6690
   874
/*
slouken@6690
   875
 * Open a controller for use - the index passed as an argument refers to
slouken@6690
   876
 * the N'th controller on the system.  This index is the value which will
slouken@6690
   877
 * identify this controller in future controller events.
slouken@6690
   878
 *
slouken@6690
   879
 * This function returns a controller identifier, or NULL if an error occurred.
slouken@6690
   880
 */
slouken@6690
   881
SDL_GameController *
slouken@6690
   882
SDL_GameControllerOpen(int device_index)
slouken@6690
   883
{
slouken@6690
   884
    SDL_GameController *gamecontroller;
slouken@7191
   885
    SDL_GameController *gamecontrollerlist;
slouken@7191
   886
    ControllerMapping_t *pSupportedController = NULL;
slouken@6690
   887
slouken@6690
   888
    if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
slouken@6690
   889
        SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
slouken@6690
   890
        return (NULL);
slouken@6690
   891
    }
slouken@6690
   892
slouken@7191
   893
    gamecontrollerlist = SDL_gamecontrollers;
slouken@7191
   894
    /* If the controller is already open, return it */
slouken@7191
   895
    while ( gamecontrollerlist )
slouken@7191
   896
    {
slouken@7191
   897
        if ( SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id ) {
slouken@7191
   898
                gamecontroller = gamecontrollerlist;
slouken@7191
   899
                ++gamecontroller->ref_count;
slouken@7191
   900
                return (gamecontroller);
slouken@7191
   901
        }
slouken@7191
   902
        gamecontrollerlist = gamecontrollerlist->next;
slouken@6690
   903
    }
slouken@6690
   904
slouken@7191
   905
    /* Find a controller mapping */
slouken@7191
   906
    pSupportedController =  SDL_PrivateGetControllerMapping(device_index);
slouken@7191
   907
    if ( !pSupportedController ) {
slouken@7191
   908
        SDL_SetError("Couldn't find mapping for device (%d)", device_index );
slouken@7191
   909
        return (NULL);
slouken@7191
   910
    }
slouken@6690
   911
slouken@7191
   912
    /* Create and initialize the joystick */
slouken@7191
   913
    gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
slouken@7191
   914
    if (gamecontroller == NULL) {
slouken@7191
   915
        SDL_OutOfMemory();
slouken@7191
   916
        return NULL;
slouken@7191
   917
    }
slouken@6841
   918
slouken@6690
   919
    SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
slouken@6690
   920
    gamecontroller->joystick = SDL_JoystickOpen(device_index);
slouken@7191
   921
    if ( !gamecontroller->joystick ) {
slouken@6690
   922
        SDL_free(gamecontroller);
slouken@6690
   923
        return NULL;
slouken@6690
   924
    }
slouken@6690
   925
slouken@7191
   926
    SDL_PrivateLoadButtonMapping( &gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping );
slouken@6690
   927
slouken@7191
   928
    /* Add joystick to list */
slouken@6690
   929
    ++gamecontroller->ref_count;
slouken@7191
   930
    /* Link the joystick in the list */
slouken@7191
   931
    gamecontroller->next = SDL_gamecontrollers;
slouken@7191
   932
    SDL_gamecontrollers = gamecontroller;
slouken@6690
   933
slouken@7191
   934
    SDL_SYS_JoystickUpdate( gamecontroller->joystick );
slouken@6690
   935
slouken@6690
   936
    return (gamecontroller);
slouken@6690
   937
}
slouken@6690
   938
icculus@6919
   939
/*
icculus@6919
   940
 * Manually pump for controller updates.
icculus@6919
   941
 */
icculus@6919
   942
void
icculus@6919
   943
SDL_GameControllerUpdate(void)
icculus@6919
   944
{
icculus@6919
   945
    /* Just for API completeness; the joystick API does all the work. */
icculus@6919
   946
    SDL_JoystickUpdate();
icculus@6919
   947
}
icculus@6919
   948
slouken@6690
   949
slouken@6690
   950
/*
slouken@6690
   951
 * Get the current state of an axis control on a controller
slouken@6690
   952
 */
slouken@6690
   953
Sint16
icculus@6917
   954
SDL_GameControllerGetAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@6690
   955
{
slouken@7191
   956
    if ( !gamecontroller )
slouken@7191
   957
        return 0;
slouken@6690
   958
slouken@7191
   959
    if (gamecontroller->mapping.axes[axis] >= 0 )
slouken@7191
   960
    {
slouken@7191
   961
        return ( SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axes[axis]) );
slouken@7191
   962
    }
slouken@7191
   963
    else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
slouken@7191
   964
    {
slouken@7191
   965
        Uint8 value;
slouken@7191
   966
        value = SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis] );
slouken@7191
   967
        if ( value > 0 )
slouken@7191
   968
            return 32767;
slouken@7191
   969
        return 0;
slouken@7191
   970
    }
slouken@7191
   971
    return 0;
slouken@6690
   972
}
slouken@6690
   973
slouken@6690
   974
slouken@6690
   975
/*
slouken@6690
   976
 * Get the current state of a button on a controller
slouken@6690
   977
 */
slouken@6690
   978
Uint8
icculus@6917
   979
SDL_GameControllerGetButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@6690
   980
{
slouken@7191
   981
    if ( !gamecontroller )
slouken@7191
   982
        return 0;
slouken@6690
   983
slouken@7191
   984
    if ( gamecontroller->mapping.buttons[button] >= 0 )
slouken@7191
   985
    {
slouken@7191
   986
        return ( SDL_JoystickGetButton( gamecontroller->joystick, gamecontroller->mapping.buttons[button] ) );
slouken@7191
   987
    }
slouken@7191
   988
    else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
slouken@7191
   989
    {
slouken@7191
   990
        Sint16 value;
slouken@7191
   991
        value = SDL_JoystickGetAxis( gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button] );
slouken@7191
   992
        if ( ABS(value) > 32768/2 )
slouken@7191
   993
            return 1;
slouken@7191
   994
        return 0;
slouken@7191
   995
    }
slouken@7191
   996
    else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
slouken@7191
   997
    {
slouken@7191
   998
        Uint8 value;
slouken@7191
   999
        value = SDL_JoystickGetHat( gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat );
slouken@6690
  1000
slouken@7191
  1001
        if ( value & gamecontroller->mapping.hatasbutton[button].mask )
slouken@7191
  1002
            return 1;
slouken@7191
  1003
        return 0;
slouken@7191
  1004
    }
slouken@7191
  1005
slouken@7191
  1006
    return 0;
slouken@6690
  1007
}
slouken@6690
  1008
slouken@6690
  1009
/*
slouken@6690
  1010
 * Return if the joystick in question is currently attached to the system,
slouken@6690
  1011
 *  \return 0 if not plugged in, 1 if still present.
slouken@6690
  1012
 */
jorgen@6871
  1013
SDL_bool
slouken@6690
  1014
SDL_GameControllerGetAttached( SDL_GameController * gamecontroller )
slouken@6690
  1015
{
slouken@7191
  1016
    if ( !gamecontroller )
slouken@7191
  1017
        return SDL_FALSE;
slouken@6690
  1018
slouken@7191
  1019
    return SDL_JoystickGetAttached(gamecontroller->joystick);
slouken@6690
  1020
}
slouken@6690
  1021
slouken@6690
  1022
slouken@6690
  1023
/*
slouken@6690
  1024
 * Get the number of multi-dimensional axis controls on a joystick
slouken@6690
  1025
 */
slouken@6690
  1026
const char *
slouken@6690
  1027
SDL_GameControllerName(SDL_GameController * gamecontroller)
slouken@6690
  1028
{
slouken@7191
  1029
    if ( !gamecontroller )
slouken@7191
  1030
        return NULL;
slouken@6690
  1031
slouken@6690
  1032
    return (gamecontroller->mapping.name);
slouken@6690
  1033
}
slouken@6690
  1034
slouken@6690
  1035
slouken@6690
  1036
/*
slouken@6690
  1037
 * Get the joystick for this controller
slouken@6690
  1038
 */
slouken@6690
  1039
SDL_Joystick *SDL_GameControllerGetJoystick(SDL_GameController * gamecontroller)
slouken@6690
  1040
{
slouken@7191
  1041
    if ( !gamecontroller )
slouken@7191
  1042
        return NULL;
slouken@6690
  1043
slouken@7191
  1044
    return gamecontroller->joystick;
slouken@6690
  1045
}
slouken@6690
  1046
slouken@6690
  1047
/**
philipp@7133
  1048
 * Get the SDL joystick layer binding for this controller axis mapping
slouken@6690
  1049
 */
icculus@6917
  1050
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis)
slouken@6690
  1051
{
slouken@7191
  1052
    SDL_GameControllerButtonBind bind;
slouken@7191
  1053
    SDL_memset( &bind, 0x0, sizeof(bind) );
slouken@6690
  1054
slouken@7191
  1055
    if ( !gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID )
slouken@7191
  1056
        return bind;
slouken@6690
  1057
slouken@7191
  1058
    if (gamecontroller->mapping.axes[axis] >= 0 )
slouken@7191
  1059
    {
slouken@7191
  1060
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@7191
  1061
        bind.value.button = gamecontroller->mapping.axes[axis];
slouken@7191
  1062
    }
slouken@7191
  1063
    else if (gamecontroller->mapping.buttonasaxis[axis] >= 0 )
slouken@7191
  1064
    {
slouken@7191
  1065
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@7191
  1066
        bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
slouken@7191
  1067
    }
slouken@6690
  1068
slouken@7191
  1069
    return bind;
slouken@6690
  1070
}
slouken@6690
  1071
slouken@6690
  1072
slouken@6690
  1073
/**
philipp@7133
  1074
 * Get the SDL joystick layer binding for this controller button mapping
slouken@6690
  1075
 */
icculus@6917
  1076
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button)
slouken@6690
  1077
{
slouken@7191
  1078
    SDL_GameControllerButtonBind bind;
slouken@7191
  1079
    SDL_memset( &bind, 0x0, sizeof(bind) );
slouken@6690
  1080
slouken@7191
  1081
    if ( !gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID )
slouken@7191
  1082
        return bind;
slouken@6690
  1083
slouken@7191
  1084
    if ( gamecontroller->mapping.buttons[button] >= 0 )
slouken@7191
  1085
    {
slouken@7191
  1086
        bind.bindType = SDL_CONTROLLER_BINDTYPE_BUTTON;
slouken@7191
  1087
        bind.value.button = gamecontroller->mapping.buttons[button];
slouken@7191
  1088
    }
slouken@7191
  1089
    else if ( gamecontroller->mapping.axesasbutton[button] >= 0 )
slouken@7191
  1090
    {
slouken@7191
  1091
        bind.bindType = SDL_CONTROLLER_BINDTYPE_AXIS;
slouken@7191
  1092
        bind.value.axis = gamecontroller->mapping.axesasbutton[button];
slouken@7191
  1093
    }
slouken@7191
  1094
    else if ( gamecontroller->mapping.hatasbutton[button].hat >= 0 )
slouken@7191
  1095
    {
slouken@7191
  1096
        bind.bindType = SDL_CONTROLLER_BINDTYPE_HAT;
slouken@7191
  1097
        bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
slouken@7191
  1098
        bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
slouken@7191
  1099
    }
slouken@6690
  1100
slouken@7191
  1101
    return bind;
slouken@6690
  1102
}
slouken@6690
  1103
slouken@6690
  1104
slouken@6690
  1105
/*
slouken@6690
  1106
 * Close a joystick previously opened with SDL_JoystickOpen()
slouken@6690
  1107
 */
slouken@6690
  1108
void
slouken@6690
  1109
SDL_GameControllerClose(SDL_GameController * gamecontroller)
slouken@6690
  1110
{
slouken@7191
  1111
    SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
slouken@6690
  1112
slouken@7191
  1113
    if ( !gamecontroller )
slouken@7191
  1114
        return;
slouken@6690
  1115
slouken@7191
  1116
    /* First decrement ref count */
slouken@6690
  1117
    if (--gamecontroller->ref_count > 0) {
slouken@6690
  1118
        return;
slouken@6690
  1119
    }
slouken@6690
  1120
slouken@7191
  1121
    SDL_JoystickClose( gamecontroller->joystick );
slouken@6690
  1122
slouken@7191
  1123
    gamecontrollerlist = SDL_gamecontrollers;
slouken@7191
  1124
    gamecontrollerlistprev = NULL;
slouken@7191
  1125
    while ( gamecontrollerlist )
slouken@7191
  1126
    {
slouken@7191
  1127
        if (gamecontroller == gamecontrollerlist)
slouken@7191
  1128
        {
slouken@7191
  1129
            if ( gamecontrollerlistprev )
slouken@7191
  1130
            {
slouken@7191
  1131
                /* unlink this entry */
slouken@7191
  1132
                gamecontrollerlistprev->next = gamecontrollerlist->next;
slouken@7191
  1133
            }
slouken@7191
  1134
            else
slouken@7191
  1135
            {
slouken@7191
  1136
                SDL_gamecontrollers = gamecontroller->next;
slouken@7191
  1137
            }
slouken@7191
  1138
slouken@7191
  1139
            break;
slouken@7191
  1140
        }
slouken@7191
  1141
        gamecontrollerlistprev = gamecontrollerlist;
slouken@7191
  1142
        gamecontrollerlist = gamecontrollerlist->next;
slouken@7191
  1143
    }
philipp@7129
  1144
slouken@6690
  1145
    SDL_free(gamecontroller);
slouken@6690
  1146
}
slouken@6690
  1147
slouken@6690
  1148
slouken@6690
  1149
/*
slouken@6690
  1150
 * Quit the controller subsystem
slouken@6690
  1151
 */
slouken@6690
  1152
void
slouken@6690
  1153
SDL_GameControllerQuit(void)
slouken@6690
  1154
{
slouken@7191
  1155
    ControllerMapping_t *pControllerMap;
slouken@7191
  1156
    while ( SDL_gamecontrollers )
slouken@7191
  1157
    {
slouken@7191
  1158
        SDL_gamecontrollers->ref_count = 1;
slouken@6690
  1159
        SDL_GameControllerClose(SDL_gamecontrollers);
slouken@7191
  1160
    }
slouken@6690
  1161
slouken@7191
  1162
    while ( s_pSupportedControllers )
slouken@7191
  1163
    {
slouken@7191
  1164
        pControllerMap = s_pSupportedControllers;
slouken@7191
  1165
        s_pSupportedControllers = s_pSupportedControllers->next;
slouken@7191
  1166
        SDL_free( pControllerMap->name );
slouken@7191
  1167
        SDL_free( pControllerMap );
slouken@7191
  1168
    }
slouken@6690
  1169
slouken@7191
  1170
    SDL_DelEventWatch( SDL_GameControllerEventWatcher, NULL );
slouken@6690
  1171
slouken@6690
  1172
}
slouken@6690
  1173
slouken@6690
  1174
/*
slouken@6690
  1175
 * Event filter to transform joystick events into appropriate game controller ones
slouken@6690
  1176
 */
slouken@6690
  1177
int
icculus@6917
  1178
SDL_PrivateGameControllerAxis(SDL_GameController * gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
slouken@6690
  1179
{
slouken@7191
  1180
    int posted;
slouken@6690
  1181
slouken@6690
  1182
    /* translate the event, if desired */
slouken@6690
  1183
    posted = 0;
slouken@6690
  1184
#if !SDL_EVENTS_DISABLED
slouken@6690
  1185
    if (SDL_GetEventState(SDL_CONTROLLERAXISMOTION) == SDL_ENABLE) {
slouken@6690
  1186
        SDL_Event event;
slouken@6690
  1187
        event.type = SDL_CONTROLLERAXISMOTION;
slouken@6690
  1188
        event.caxis.which = gamecontroller->joystick->instance_id;
slouken@6690
  1189
        event.caxis.axis = axis;
slouken@6690
  1190
        event.caxis.value = value;
slouken@7191
  1191
        posted = SDL_PushEvent(&event) == 1;
slouken@6690
  1192
    }
slouken@6690
  1193
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1194
    return (posted);
slouken@6690
  1195
}
slouken@6690
  1196
slouken@6690
  1197
slouken@6690
  1198
/*
slouken@6690
  1199
 * Event filter to transform joystick events into appropriate game controller ones
slouken@6690
  1200
 */
slouken@6690
  1201
int
icculus@6917
  1202
SDL_PrivateGameControllerButton(SDL_GameController * gamecontroller, SDL_GameControllerButton button, Uint8 state)
slouken@6690
  1203
{
slouken@6690
  1204
    int posted;
slouken@6690
  1205
#if !SDL_EVENTS_DISABLED
slouken@7191
  1206
    SDL_Event event;
slouken@6690
  1207
slouken@6854
  1208
    if ( button == SDL_CONTROLLER_BUTTON_INVALID )
slouken@6854
  1209
        return (0);
slouken@6854
  1210
slouken@6690
  1211
    switch (state) {
slouken@6690
  1212
    case SDL_PRESSED:
slouken@6690
  1213
        event.type = SDL_CONTROLLERBUTTONDOWN;
slouken@6690
  1214
        break;
slouken@6690
  1215
    case SDL_RELEASED:
slouken@6690
  1216
        event.type = SDL_CONTROLLERBUTTONUP;
slouken@6690
  1217
        break;
slouken@6690
  1218
    default:
slouken@6690
  1219
        /* Invalid state -- bail */
slouken@6690
  1220
        return (0);
slouken@6690
  1221
    }
slouken@6690
  1222
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1223
slouken@6690
  1224
    /* translate the event, if desired */
slouken@6690
  1225
    posted = 0;
slouken@6690
  1226
#if !SDL_EVENTS_DISABLED
slouken@6690
  1227
    if (SDL_GetEventState(event.type) == SDL_ENABLE) {
slouken@6690
  1228
        event.cbutton.which = gamecontroller->joystick->instance_id;
slouken@6690
  1229
        event.cbutton.button = button;
slouken@6690
  1230
        event.cbutton.state = state;
slouken@7191
  1231
        posted = SDL_PushEvent(&event) == 1;
slouken@6690
  1232
    }
slouken@6690
  1233
#endif /* !SDL_EVENTS_DISABLED */
slouken@6690
  1234
    return (posted);
slouken@6690
  1235
}
slouken@6690
  1236
slouken@6690
  1237
/*
slouken@6690
  1238
 * Turn off controller events
slouken@6690
  1239
 */
slouken@6690
  1240
int
slouken@6690
  1241
SDL_GameControllerEventState(int state)
slouken@6690
  1242
{
slouken@6690
  1243
#if SDL_EVENTS_DISABLED
slouken@6690
  1244
    return SDL_IGNORE;
slouken@6690
  1245
#else
slouken@6690
  1246
    const Uint32 event_list[] = {
slouken@6690
  1247
        SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP,
urkle@6964
  1248
        SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_CONTROLLERDEVICEREMAPPED,
slouken@6690
  1249
    };
slouken@6690
  1250
    unsigned int i;
slouken@6690
  1251
slouken@6690
  1252
    switch (state) {
slouken@6690
  1253
    case SDL_QUERY:
slouken@6690
  1254
        state = SDL_IGNORE;
slouken@6690
  1255
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@6690
  1256
            state = SDL_EventState(event_list[i], SDL_QUERY);
slouken@6690
  1257
            if (state == SDL_ENABLE) {
slouken@6690
  1258
                break;
slouken@6690
  1259
            }
slouken@6690
  1260
        }
slouken@6690
  1261
        break;
slouken@6690
  1262
    default:
slouken@6690
  1263
        for (i = 0; i < SDL_arraysize(event_list); ++i) {
slouken@6690
  1264
            SDL_EventState(event_list[i], state);
slouken@6690
  1265
        }
slouken@6690
  1266
        break;
slouken@6690
  1267
    }
slouken@6690
  1268
    return (state);
slouken@6690
  1269
#endif /* SDL_EVENTS_DISABLED */
slouken@6690
  1270
}
slouken@6690
  1271
slouken@6690
  1272
/* vi: set ts=4 sw=4 expandtab: */