include/SDL_gamecontroller.h
author Jørgen P. Tjernø <jorgen@valvesoftware.com>
Wed, 05 Jun 2013 12:48:44 -0700
changeset 7277 1290cd7f34af
parent 7191 75360622e65f
child 7341 675c85d46f30
permissions -rw-r--r--
Mac: Fix invalid PS3 controller mapping.

PS3 controller had swapped D-Pad down & left buttons, as well as X & Y buttons.
Thanks to Alex Szpakowski for the bug report and fix.

Fixes http://bugzilla.libsdl.org/show_bug.cgi?id=1891
slouken@6690
     1
/*
slouken@6690
     2
  Simple DirectMedia Layer
slouken@6885
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@6690
     4
slouken@6690
     5
  This software is provided 'as-is', without any express or implied
slouken@6690
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6690
     7
  arising from the use of this software.
slouken@6690
     8
slouken@6690
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6690
    10
  including commercial applications, and to alter it and redistribute it
slouken@6690
    11
  freely, subject to the following restrictions:
slouken@6690
    12
slouken@6690
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6690
    14
     claim that you wrote the original software. If you use this software
slouken@6690
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6690
    16
     appreciated but is not required.
slouken@6690
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6690
    18
     misrepresented as being the original software.
slouken@6690
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6690
    20
*/
slouken@6690
    21
slouken@6690
    22
/**
slouken@6690
    23
 *  \file SDL_gamecontroller.h
jorgen@6873
    24
 *
slouken@6690
    25
 *  Include file for SDL game controller event handling
slouken@6690
    26
 */
slouken@6690
    27
slouken@6690
    28
#ifndef _SDL_gamecontroller_h
slouken@6690
    29
#define _SDL_gamecontroller_h
slouken@6690
    30
slouken@6690
    31
#include "SDL_stdinc.h"
slouken@6690
    32
#include "SDL_error.h"
slouken@6690
    33
#include "SDL_joystick.h"
slouken@6690
    34
slouken@6690
    35
#include "begin_code.h"
slouken@6690
    36
/* Set up for C function definitions, even when using C++ */
slouken@6690
    37
#ifdef __cplusplus
slouken@6690
    38
extern "C" {
slouken@6690
    39
#endif
slouken@6690
    40
slouken@6690
    41
/**
slouken@6690
    42
 *  \file SDL_gamecontroller.h
slouken@6690
    43
 *
slouken@6690
    44
 *  In order to use these functions, SDL_Init() must have been called
slouken@6690
    45
 *  with the ::SDL_INIT_JOYSTICK flag.  This causes SDL to scan the system
slouken@6690
    46
 *  for game controllers, and load appropriate drivers.
slouken@6690
    47
 */
slouken@6690
    48
slouken@6690
    49
/* The gamecontroller structure used to identify an SDL game controller */
slouken@6690
    50
struct _SDL_GameController;
slouken@6690
    51
typedef struct _SDL_GameController SDL_GameController;
slouken@6690
    52
slouken@6690
    53
jorgen@6873
    54
typedef enum
slouken@6690
    55
{
slouken@7191
    56
    SDL_CONTROLLER_BINDTYPE_NONE = 0,
slouken@7191
    57
    SDL_CONTROLLER_BINDTYPE_BUTTON,
slouken@7191
    58
    SDL_CONTROLLER_BINDTYPE_AXIS,
slouken@7191
    59
    SDL_CONTROLLER_BINDTYPE_HAT
icculus@6917
    60
} SDL_GameControllerBindType;
icculus@6917
    61
slouken@6690
    62
/**
slouken@6932
    63
 *  Get the SDL joystick layer binding for this controller button/axis mapping
slouken@6690
    64
 */
slouken@6932
    65
typedef struct SDL_GameControllerButtonBind
slouken@6690
    66
{
slouken@7191
    67
    SDL_GameControllerBindType bindType;
slouken@7191
    68
    union
slouken@7191
    69
    {
slouken@7191
    70
        int button;
slouken@7191
    71
        int axis;
slouken@7191
    72
        struct {
slouken@6932
    73
            int hat;
slouken@6932
    74
            int hat_mask;
slouken@6932
    75
        } hat;
slouken@7191
    76
    } value;
slouken@6690
    77
slouken@6690
    78
} SDL_GameControllerButtonBind;
slouken@6690
    79
slouken@6690
    80
slouken@6690
    81
/**
slouken@6690
    82
 *  To count the number of game controllers in the system for the following:
slouken@7191
    83
 *  int nJoysticks = SDL_NumJoysticks();
slouken@7191
    84
 *  int nGameControllers = 0;
slouken@7191
    85
 *  for ( int i = 0; i < nJoysticks; i++ ) {
slouken@7191
    86
 *      if ( SDL_IsGameController(i) ) {
slouken@7191
    87
 *          nGameControllers++;
slouken@7191
    88
 *      }
slouken@6690
    89
 *  }
slouken@6690
    90
 *
urkle@6964
    91
 *  Using the SDL_HINT_GAMECONTROLLERCONFIG hint or the SDL_GameControllerAddMapping you can add support for controllers SDL is unaware of or cause an existing controller to have a different binding. The format is:
slouken@7191
    92
 *  guid,name,mappings
slouken@6690
    93
 *
slouken@6690
    94
 *  Where GUID is the string value from SDL_JoystickGetGUIDString(), name is the human readable string for the device and mappings are controller mappings to joystick ones.
slouken@6690
    95
 *  Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
slouken@7191
    96
 *  The mapping format for joystick is:
slouken@7191
    97
 *      bX - a joystick button, index X
slouken@7191
    98
 *      hX.Y - hat X with value Y
slouken@7191
    99
 *      aX - axis X of the joystick
slouken@6690
   100
 *  Buttons can be used as a controller axis and vice versa.
slouken@6690
   101
 *
slouken@6690
   102
 *  This string shows an example of a valid mapping for a controller
slouken@7191
   103
 *  "341a3608000000000000504944564944,Afterglow PS3 Controller,a:b1,b:b2,y:b3,x:b0,start:b9,guide:b12,back:b8,dpup:h0.1,dpleft:h0.8,dpdown:h0.4,dpright:h0.2,leftshoulder:b4,rightshoulder:b5,leftstick:b10,rightstick:b11,leftx:a0,lefty:a1,rightx:a2,righty:a3,lefttrigger:b6,righttrigger:b7",
slouken@6690
   104
 *
slouken@6690
   105
 */
slouken@6690
   106
urkle@6964
   107
/**
urkle@6964
   108
 *  Add or update an existing mapping configuration
urkle@6964
   109
 *
urkle@6964
   110
 * \return 1 if mapping is added, 0 if updated, -1 on error
urkle@6964
   111
 */
icculus@7061
   112
extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping( const char* mappingString );
urkle@6964
   113
urkle@6964
   114
/**
urkle@6964
   115
 *  Get a mapping string for a GUID
urkle@6964
   116
 *
urkle@6964
   117
 *  \return the mapping string.  Must be freed with SDL_free.  Returns NULL if no mapping is available
urkle@6964
   118
 */
urkle@6964
   119
extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID( SDL_JoystickGUID guid );
urkle@6964
   120
urkle@6964
   121
/**
urkle@6964
   122
 *  Get a mapping string for an open GameController
urkle@6964
   123
 *
urkle@6964
   124
 *  \return the mapping string.  Must be freed with SDL_free.  Returns NULL if no mapping is available
urkle@6964
   125
 */
urkle@6964
   126
extern DECLSPEC char * SDLCALL SDL_GameControllerMapping( SDL_GameController * gamecontroller );
slouken@6690
   127
slouken@6690
   128
/**
slouken@6690
   129
 *  Is the joystick on this index supported by the game controller interface?
slouken@6690
   130
 */
jorgen@6871
   131
extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
slouken@6690
   132
slouken@6690
   133
slouken@6690
   134
/**
slouken@6690
   135
 *  Get the implementation dependent name of a game controller.
slouken@6690
   136
 *  This can be called before any controllers are opened.
slouken@6690
   137
 *  If no name can be found, this function returns NULL.
slouken@6690
   138
 */
slouken@6690
   139
extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
slouken@6690
   140
slouken@6690
   141
/**
jorgen@6873
   142
 *  Open a game controller for use.
jorgen@6873
   143
 *  The index passed as an argument refers to the N'th game controller on the system.
slouken@6690
   144
 *  This index is the value which will identify this controller in future controller
slouken@6690
   145
 *  events.
jorgen@6873
   146
 *
slouken@6690
   147
 *  \return A controller identifier, or NULL if an error occurred.
slouken@6690
   148
 */
slouken@6690
   149
extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
slouken@6690
   150
slouken@6690
   151
/**
slouken@6690
   152
 *  Return the name for this currently opened controller
slouken@6690
   153
 */
jorgen@6873
   154
extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
jorgen@6873
   155
slouken@6690
   156
/**
jorgen@6873
   157
 *  Returns SDL_TRUE if the controller has been opened and currently connected,
jorgen@6873
   158
 *  or SDL_FALSE if it has not.
slouken@6690
   159
 */
jorgen@6873
   160
extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
slouken@6690
   161
slouken@6690
   162
/**
slouken@6690
   163
 *  Get the underlying joystick object used by a controller
slouken@6690
   164
 */
jorgen@6873
   165
extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
slouken@6690
   166
slouken@6690
   167
/**
slouken@6690
   168
 *  Enable/disable controller event polling.
jorgen@6873
   169
 *
slouken@6690
   170
 *  If controller events are disabled, you must call SDL_GameControllerUpdate()
slouken@6690
   171
 *  yourself and check the state of the controller when you want controller
slouken@6690
   172
 *  information.
jorgen@6873
   173
 *
slouken@6690
   174
 *  The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
slouken@6690
   175
 */
slouken@6690
   176
extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
slouken@6690
   177
slouken@6690
   178
/**
icculus@6919
   179
 *  Update the current state of the open game controllers.
slouken@7191
   180
 *
icculus@6919
   181
 *  This is called automatically by the event loop if any game controller
icculus@6919
   182
 *  events are enabled.
icculus@6919
   183
 */
icculus@6919
   184
extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
icculus@6919
   185
icculus@6919
   186
icculus@6919
   187
/**
philipp@7174
   188
 *  The list of axes available from a controller
slouken@6690
   189
 */
jorgen@6873
   190
typedef enum
slouken@6690
   191
{
slouken@7191
   192
    SDL_CONTROLLER_AXIS_INVALID = -1,
slouken@7191
   193
    SDL_CONTROLLER_AXIS_LEFTX,
slouken@7191
   194
    SDL_CONTROLLER_AXIS_LEFTY,
slouken@7191
   195
    SDL_CONTROLLER_AXIS_RIGHTX,
slouken@7191
   196
    SDL_CONTROLLER_AXIS_RIGHTY,
slouken@7191
   197
    SDL_CONTROLLER_AXIS_TRIGGERLEFT,
slouken@7191
   198
    SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
slouken@7191
   199
    SDL_CONTROLLER_AXIS_MAX
icculus@6917
   200
} SDL_GameControllerAxis;
slouken@6690
   201
slouken@6690
   202
/**
slouken@6690
   203
 *  turn this string into a axis mapping
slouken@6690
   204
 */
icculus@6917
   205
extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString);
slouken@6690
   206
slouken@6690
   207
/**
urkle@6964
   208
 *  turn this axis enum into a string mapping
urkle@6964
   209
 */
urkle@6964
   210
extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
urkle@6964
   211
urkle@6964
   212
/**
slouken@6932
   213
 *  Get the SDL joystick layer binding for this controller button mapping
slouken@6690
   214
 */
jorgen@6873
   215
extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
jorgen@6873
   216
SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller,
icculus@6917
   217
                                 SDL_GameControllerAxis axis);
slouken@6690
   218
slouken@6690
   219
/**
slouken@6690
   220
 *  Get the current state of an axis control on a game controller.
jorgen@6873
   221
 *
slouken@6690
   222
 *  The state is a value ranging from -32768 to 32767.
jorgen@6873
   223
 *
slouken@6690
   224
 *  The axis indices start at index 0.
slouken@6690
   225
 */
jorgen@6873
   226
extern DECLSPEC Sint16 SDLCALL
jorgen@6873
   227
SDL_GameControllerGetAxis(SDL_GameController *gamecontroller,
icculus@6917
   228
                          SDL_GameControllerAxis axis);
slouken@6690
   229
slouken@6690
   230
/**
slouken@6690
   231
 *  The list of buttons available from a controller
slouken@6690
   232
 */
slouken@6690
   233
typedef enum
slouken@6690
   234
{
slouken@7191
   235
    SDL_CONTROLLER_BUTTON_INVALID = -1,
slouken@7191
   236
    SDL_CONTROLLER_BUTTON_A,
slouken@7191
   237
    SDL_CONTROLLER_BUTTON_B,
slouken@7191
   238
    SDL_CONTROLLER_BUTTON_X,
slouken@7191
   239
    SDL_CONTROLLER_BUTTON_Y,
slouken@7191
   240
    SDL_CONTROLLER_BUTTON_BACK,
slouken@7191
   241
    SDL_CONTROLLER_BUTTON_GUIDE,
slouken@7191
   242
    SDL_CONTROLLER_BUTTON_START,
slouken@7191
   243
    SDL_CONTROLLER_BUTTON_LEFTSTICK,
slouken@7191
   244
    SDL_CONTROLLER_BUTTON_RIGHTSTICK,
slouken@7191
   245
    SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
slouken@7191
   246
    SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
slouken@7191
   247
    SDL_CONTROLLER_BUTTON_DPAD_UP,
slouken@7191
   248
    SDL_CONTROLLER_BUTTON_DPAD_DOWN,
slouken@7191
   249
    SDL_CONTROLLER_BUTTON_DPAD_LEFT,
slouken@7191
   250
    SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
slouken@7191
   251
    SDL_CONTROLLER_BUTTON_MAX
icculus@6917
   252
} SDL_GameControllerButton;
slouken@6690
   253
slouken@6690
   254
/**
slouken@6690
   255
 *  turn this string into a button mapping
slouken@6690
   256
 */
icculus@6917
   257
extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString);
slouken@6690
   258
urkle@6964
   259
/**
urkle@6964
   260
 *  turn this button enum into a string mapping
urkle@6964
   261
 */
urkle@6964
   262
extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
slouken@6690
   263
slouken@6690
   264
/**
slouken@6932
   265
 *  Get the SDL joystick layer binding for this controller button mapping
slouken@6690
   266
 */
jorgen@6873
   267
extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
jorgen@6873
   268
SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller,
icculus@6917
   269
                                   SDL_GameControllerButton button);
slouken@6690
   270
slouken@6690
   271
slouken@6690
   272
/**
slouken@6690
   273
 *  Get the current state of a button on a game controller.
jorgen@6873
   274
 *
slouken@6690
   275
 *  The button indices start at index 0.
slouken@6690
   276
 */
jorgen@6873
   277
extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
icculus@6917
   278
                                                          SDL_GameControllerButton button);
slouken@6690
   279
slouken@6690
   280
/**
slouken@6690
   281
 *  Close a controller previously opened with SDL_GameControllerOpen().
slouken@6690
   282
 */
jorgen@6873
   283
extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
slouken@6690
   284
slouken@6690
   285
slouken@6690
   286
/* Ends C function definitions when using C++ */
slouken@6690
   287
#ifdef __cplusplus
slouken@6690
   288
}
slouken@6690
   289
#endif
slouken@6690
   290
#include "close_code.h"
slouken@6690
   291
slouken@6690
   292
#endif /* _SDL_gamecontroller_h */
slouken@6690
   293
slouken@6690
   294
/* vi: set ts=4 sw=4 expandtab: */