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