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