src/joystick/SDL_gamecontroller.c
author Edward Rudd <urkle@outoforder.cc>
Thu, 30 May 2013 22:14:24 -0400
changeset 7248 52d3427f8b5a
parent 7247 6a4570f12c20
child 7277 1290cd7f34af
permissions -rw-r--r--
Add trigger value adjustment code to the SDL_GameControllerGetAxis code as well.

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