include/SDL_gamecontroller.h
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Oct 2018 16:53:14 -0700
changeset 12359 691c32a30fb9
parent 12088 399cc39583cc
child 12503 806492103856
permissions -rw-r--r--
Generalized the XInput user index into a player index
slouken@6690
     1
/*
slouken@6690
     2
  Simple DirectMedia Layer
slouken@11811
     3
  Copyright (C) 1997-2018 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@10638
    28
#ifndef SDL_gamecontroller_h_
slouken@10638
    29
#define SDL_gamecontroller_h_
slouken@6690
    30
slouken@6690
    31
#include "SDL_stdinc.h"
slouken@6690
    32
#include "SDL_error.h"
slouken@8051
    33
#include "SDL_rwops.h"
slouken@6690
    34
#include "SDL_joystick.h"
slouken@6690
    35
slouken@6690
    36
#include "begin_code.h"
slouken@6690
    37
/* Set up for C function definitions, even when using C++ */
slouken@6690
    38
#ifdef __cplusplus
slouken@6690
    39
extern "C" {
slouken@6690
    40
#endif
slouken@6690
    41
slouken@6690
    42
/**
slouken@6690
    43
 *  \file SDL_gamecontroller.h
slouken@6690
    44
 *
slouken@6690
    45
 *  In order to use these functions, SDL_Init() must have been called
philipp@9225
    46
 *  with the ::SDL_INIT_GAMECONTROLLER flag.  This causes SDL to scan the system
slouken@6690
    47
 *  for game controllers, and load appropriate drivers.
slouken@7341
    48
 *
slouken@7341
    49
 *  If you would like to receive controller updates while the application
slouken@7341
    50
 *  is in the background, you should set the following hint before calling
slouken@7341
    51
 *  SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
slouken@6690
    52
 */
slouken@6690
    53
slouken@10636
    54
/**
slouken@10636
    55
 * The gamecontroller structure used to identify an SDL game controller
slouken@10636
    56
 */
slouken@6690
    57
struct _SDL_GameController;
slouken@6690
    58
typedef struct _SDL_GameController SDL_GameController;
slouken@6690
    59
slouken@6690
    60
jorgen@6873
    61
typedef enum
slouken@6690
    62
{
slouken@7191
    63
    SDL_CONTROLLER_BINDTYPE_NONE = 0,
slouken@7191
    64
    SDL_CONTROLLER_BINDTYPE_BUTTON,
slouken@7191
    65
    SDL_CONTROLLER_BINDTYPE_AXIS,
slouken@7191
    66
    SDL_CONTROLLER_BINDTYPE_HAT
icculus@6917
    67
} SDL_GameControllerBindType;
icculus@6917
    68
slouken@6690
    69
/**
slouken@6932
    70
 *  Get the SDL joystick layer binding for this controller button/axis mapping
slouken@6690
    71
 */
slouken@6932
    72
typedef struct SDL_GameControllerButtonBind
slouken@6690
    73
{
slouken@7191
    74
    SDL_GameControllerBindType bindType;
slouken@7191
    75
    union
slouken@7191
    76
    {
slouken@7191
    77
        int button;
slouken@7191
    78
        int axis;
slouken@7191
    79
        struct {
slouken@6932
    80
            int hat;
slouken@6932
    81
            int hat_mask;
slouken@6932
    82
        } hat;
slouken@7191
    83
    } value;
slouken@6690
    84
slouken@6690
    85
} SDL_GameControllerButtonBind;
slouken@6690
    86
slouken@6690
    87
slouken@6690
    88
/**
slouken@6690
    89
 *  To count the number of game controllers in the system for the following:
slouken@7191
    90
 *  int nJoysticks = SDL_NumJoysticks();
slouken@7191
    91
 *  int nGameControllers = 0;
slouken@10595
    92
 *  for (int i = 0; i < nJoysticks; i++) {
slouken@10595
    93
 *      if (SDL_IsGameController(i)) {
slouken@7191
    94
 *          nGameControllers++;
slouken@7191
    95
 *      }
slouken@6690
    96
 *  }
slouken@6690
    97
 *
philipp@10213
    98
 *  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
    99
 *  guid,name,mappings
slouken@6690
   100
 *
slouken@6690
   101
 *  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
   102
 *  Under Windows there is a reserved GUID of "xinput" that covers any XInput devices.
slouken@7191
   103
 *  The mapping format for joystick is:
slouken@7191
   104
 *      bX - a joystick button, index X
slouken@7191
   105
 *      hX.Y - hat X with value Y
slouken@7191
   106
 *      aX - axis X of the joystick
slouken@6690
   107
 *  Buttons can be used as a controller axis and vice versa.
slouken@6690
   108
 *
slouken@6690
   109
 *  This string shows an example of a valid mapping for a controller
slouken@10595
   110
 *  "03000000341a00003608000000000000,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
   111
 *
slouken@6690
   112
 */
slouken@6690
   113
urkle@6964
   114
/**
gabomdq@8046
   115
 *  Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform()
gabomdq@8042
   116
 *  A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt
gabomdq@8042
   117
 *
gabomdq@8046
   118
 *  If \c freerw is non-zero, the stream will be closed after being read.
gabomdq@8046
   119
 * 
gabomdq@8042
   120
 * \return number of mappings added, -1 on error
gabomdq@8042
   121
 */
slouken@10595
   122
extern DECLSPEC int SDLCALL SDL_GameControllerAddMappingsFromRW(SDL_RWops * rw, int freerw);
gabomdq@8046
   123
gabomdq@8046
   124
/**
gabomdq@8046
   125
 *  Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
gabomdq@8046
   126
 *
gabomdq@8046
   127
 *  Convenience macro.
gabomdq@8046
   128
 */
gabomdq@8046
   129
#define SDL_GameControllerAddMappingsFromFile(file)   SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
gabomdq@8042
   130
gabomdq@8042
   131
/**
urkle@6964
   132
 *  Add or update an existing mapping configuration
urkle@6964
   133
 *
urkle@6964
   134
 * \return 1 if mapping is added, 0 if updated, -1 on error
urkle@6964
   135
 */
slouken@10595
   136
extern DECLSPEC int SDLCALL SDL_GameControllerAddMapping(const char* mappingString);
urkle@6964
   137
urkle@6964
   138
/**
slouken@10661
   139
 *  Get the number of mappings installed
slouken@10661
   140
 *
slouken@10661
   141
 *  \return the number of mappings
slouken@10661
   142
 */
slouken@10721
   143
extern DECLSPEC int SDLCALL SDL_GameControllerNumMappings(void);
slouken@10661
   144
slouken@10661
   145
/**
slouken@10661
   146
 *  Get the mapping at a particular index.
slouken@10661
   147
 *
slouken@10661
   148
 *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if the index is out of range.
slouken@10661
   149
 */
slouken@10661
   150
extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForIndex(int mapping_index);
slouken@10661
   151
slouken@10661
   152
/**
urkle@6964
   153
 *  Get a mapping string for a GUID
urkle@6964
   154
 *
philipp@10213
   155
 *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if no mapping is available
urkle@6964
   156
 */
slouken@10595
   157
extern DECLSPEC char * SDLCALL SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid);
urkle@6964
   158
urkle@6964
   159
/**
urkle@6964
   160
 *  Get a mapping string for an open GameController
urkle@6964
   161
 *
philipp@10213
   162
 *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if no mapping is available
urkle@6964
   163
 */
slouken@10595
   164
extern DECLSPEC char * SDLCALL SDL_GameControllerMapping(SDL_GameController * gamecontroller);
slouken@6690
   165
slouken@6690
   166
/**
slouken@6690
   167
 *  Is the joystick on this index supported by the game controller interface?
slouken@6690
   168
 */
jorgen@6871
   169
extern DECLSPEC SDL_bool SDLCALL SDL_IsGameController(int joystick_index);
slouken@6690
   170
slouken@6690
   171
/**
slouken@6690
   172
 *  Get the implementation dependent name of a game controller.
slouken@6690
   173
 *  This can be called before any controllers are opened.
slouken@6690
   174
 *  If no name can be found, this function returns NULL.
slouken@6690
   175
 */
slouken@6690
   176
extern DECLSPEC const char *SDLCALL SDL_GameControllerNameForIndex(int joystick_index);
slouken@6690
   177
slouken@6690
   178
/**
slouken@11922
   179
 *  Get the mapping of a game controller.
slouken@11922
   180
 *  This can be called before any controllers are opened.
slouken@11922
   181
 *
slouken@11922
   182
 *  \return the mapping string.  Must be freed with SDL_free().  Returns NULL if no mapping is available
slouken@11922
   183
 */
slouken@11922
   184
extern DECLSPEC char *SDLCALL SDL_GameControllerMappingForDeviceIndex(int joystick_index);
slouken@11922
   185
slouken@11922
   186
/**
jorgen@6873
   187
 *  Open a game controller for use.
jorgen@6873
   188
 *  The index passed as an argument refers to the N'th game controller on the system.
philipp@9569
   189
 *  This index is not the value which will identify this controller in future
philipp@9569
   190
 *  controller events.  The joystick's instance id (::SDL_JoystickID) will be
philipp@9569
   191
 *  used there instead.
jorgen@6873
   192
 *
slouken@6690
   193
 *  \return A controller identifier, or NULL if an error occurred.
slouken@6690
   194
 */
slouken@6690
   195
extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerOpen(int joystick_index);
slouken@6690
   196
slouken@6690
   197
/**
icculus@9916
   198
 * Return the SDL_GameController associated with an instance id.
icculus@9916
   199
 */
icculus@9916
   200
extern DECLSPEC SDL_GameController *SDLCALL SDL_GameControllerFromInstanceID(SDL_JoystickID joyid);
icculus@9916
   201
icculus@9916
   202
/**
slouken@6690
   203
 *  Return the name for this currently opened controller
slouken@6690
   204
 */
jorgen@6873
   205
extern DECLSPEC const char *SDLCALL SDL_GameControllerName(SDL_GameController *gamecontroller);
jorgen@6873
   206
slouken@6690
   207
/**
slouken@12359
   208
 *  Get the player index of an opened game controller, or -1 if it's not available
slouken@12359
   209
 *
slouken@12359
   210
 *  For XInput controllers this returns the XInput user index.
slouken@12359
   211
 */
slouken@12359
   212
extern DECLSPEC int SDLCALL SDL_GameControllerGetPlayerIndex(SDL_GameController *gamecontroller);
slouken@12359
   213
slouken@12359
   214
/**
slouken@10595
   215
 *  Get the USB vendor ID of an opened controller, if available.
slouken@10595
   216
 *  If the vendor ID isn't available this function returns 0.
slouken@10595
   217
 */
slouken@10595
   218
extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetVendor(SDL_GameController * gamecontroller);
slouken@10595
   219
slouken@10595
   220
/**
slouken@10595
   221
 *  Get the USB product ID of an opened controller, if available.
slouken@10595
   222
 *  If the product ID isn't available this function returns 0.
slouken@10595
   223
 */
slouken@10595
   224
extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProduct(SDL_GameController * gamecontroller);
slouken@10595
   225
slouken@10595
   226
/**
slouken@10595
   227
 *  Get the product version of an opened controller, if available.
slouken@10595
   228
 *  If the product version isn't available this function returns 0.
slouken@10595
   229
 */
slouken@10595
   230
extern DECLSPEC Uint16 SDLCALL SDL_GameControllerGetProductVersion(SDL_GameController * gamecontroller);
slouken@10595
   231
slouken@10595
   232
/**
jorgen@6873
   233
 *  Returns SDL_TRUE if the controller has been opened and currently connected,
jorgen@6873
   234
 *  or SDL_FALSE if it has not.
slouken@6690
   235
 */
jorgen@6873
   236
extern DECLSPEC SDL_bool SDLCALL SDL_GameControllerGetAttached(SDL_GameController *gamecontroller);
slouken@6690
   237
slouken@6690
   238
/**
slouken@6690
   239
 *  Get the underlying joystick object used by a controller
slouken@6690
   240
 */
jorgen@6873
   241
extern DECLSPEC SDL_Joystick *SDLCALL SDL_GameControllerGetJoystick(SDL_GameController *gamecontroller);
slouken@6690
   242
slouken@6690
   243
/**
slouken@6690
   244
 *  Enable/disable controller event polling.
jorgen@6873
   245
 *
slouken@6690
   246
 *  If controller events are disabled, you must call SDL_GameControllerUpdate()
slouken@6690
   247
 *  yourself and check the state of the controller when you want controller
slouken@6690
   248
 *  information.
jorgen@6873
   249
 *
slouken@6690
   250
 *  The state can be one of ::SDL_QUERY, ::SDL_ENABLE or ::SDL_IGNORE.
slouken@6690
   251
 */
slouken@6690
   252
extern DECLSPEC int SDLCALL SDL_GameControllerEventState(int state);
slouken@6690
   253
slouken@6690
   254
/**
icculus@6919
   255
 *  Update the current state of the open game controllers.
slouken@7191
   256
 *
icculus@6919
   257
 *  This is called automatically by the event loop if any game controller
icculus@6919
   258
 *  events are enabled.
icculus@6919
   259
 */
icculus@6919
   260
extern DECLSPEC void SDLCALL SDL_GameControllerUpdate(void);
icculus@6919
   261
icculus@6919
   262
icculus@6919
   263
/**
philipp@7174
   264
 *  The list of axes available from a controller
slouken@10727
   265
 *
slouken@10727
   266
 *  Thumbstick axis values range from SDL_JOYSTICK_AXIS_MIN to SDL_JOYSTICK_AXIS_MAX,
slouken@10727
   267
 *  and are centered within ~8000 of zero, though advanced UI will allow users to set
slouken@10727
   268
 *  or autodetect the dead zone, which varies between controllers.
slouken@10727
   269
 *
slouken@10727
   270
 *  Trigger axis values range from 0 to SDL_JOYSTICK_AXIS_MAX.
slouken@6690
   271
 */
jorgen@6873
   272
typedef enum
slouken@6690
   273
{
slouken@7191
   274
    SDL_CONTROLLER_AXIS_INVALID = -1,
slouken@7191
   275
    SDL_CONTROLLER_AXIS_LEFTX,
slouken@7191
   276
    SDL_CONTROLLER_AXIS_LEFTY,
slouken@7191
   277
    SDL_CONTROLLER_AXIS_RIGHTX,
slouken@7191
   278
    SDL_CONTROLLER_AXIS_RIGHTY,
slouken@7191
   279
    SDL_CONTROLLER_AXIS_TRIGGERLEFT,
slouken@7191
   280
    SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
slouken@7191
   281
    SDL_CONTROLLER_AXIS_MAX
icculus@6917
   282
} SDL_GameControllerAxis;
slouken@6690
   283
slouken@6690
   284
/**
slouken@6690
   285
 *  turn this string into a axis mapping
slouken@6690
   286
 */
icculus@6917
   287
extern DECLSPEC SDL_GameControllerAxis SDLCALL SDL_GameControllerGetAxisFromString(const char *pchString);
slouken@6690
   288
slouken@6690
   289
/**
urkle@6964
   290
 *  turn this axis enum into a string mapping
urkle@6964
   291
 */
urkle@6964
   292
extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForAxis(SDL_GameControllerAxis axis);
urkle@6964
   293
urkle@6964
   294
/**
slouken@6932
   295
 *  Get the SDL joystick layer binding for this controller button mapping
slouken@6690
   296
 */
jorgen@6873
   297
extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
jorgen@6873
   298
SDL_GameControllerGetBindForAxis(SDL_GameController *gamecontroller,
icculus@6917
   299
                                 SDL_GameControllerAxis axis);
slouken@6690
   300
slouken@6690
   301
/**
slouken@6690
   302
 *  Get the current state of an axis control on a game controller.
jorgen@6873
   303
 *
icculus@9359
   304
 *  The state is a value ranging from -32768 to 32767 (except for the triggers,
icculus@9359
   305
 *  which range from 0 to 32767).
jorgen@6873
   306
 *
slouken@6690
   307
 *  The axis indices start at index 0.
slouken@6690
   308
 */
jorgen@6873
   309
extern DECLSPEC Sint16 SDLCALL
jorgen@6873
   310
SDL_GameControllerGetAxis(SDL_GameController *gamecontroller,
icculus@6917
   311
                          SDL_GameControllerAxis axis);
slouken@6690
   312
slouken@6690
   313
/**
slouken@6690
   314
 *  The list of buttons available from a controller
slouken@6690
   315
 */
slouken@6690
   316
typedef enum
slouken@6690
   317
{
slouken@7191
   318
    SDL_CONTROLLER_BUTTON_INVALID = -1,
slouken@7191
   319
    SDL_CONTROLLER_BUTTON_A,
slouken@7191
   320
    SDL_CONTROLLER_BUTTON_B,
slouken@7191
   321
    SDL_CONTROLLER_BUTTON_X,
slouken@7191
   322
    SDL_CONTROLLER_BUTTON_Y,
slouken@7191
   323
    SDL_CONTROLLER_BUTTON_BACK,
slouken@7191
   324
    SDL_CONTROLLER_BUTTON_GUIDE,
slouken@7191
   325
    SDL_CONTROLLER_BUTTON_START,
slouken@7191
   326
    SDL_CONTROLLER_BUTTON_LEFTSTICK,
slouken@7191
   327
    SDL_CONTROLLER_BUTTON_RIGHTSTICK,
slouken@7191
   328
    SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
slouken@7191
   329
    SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
slouken@7191
   330
    SDL_CONTROLLER_BUTTON_DPAD_UP,
slouken@7191
   331
    SDL_CONTROLLER_BUTTON_DPAD_DOWN,
slouken@7191
   332
    SDL_CONTROLLER_BUTTON_DPAD_LEFT,
slouken@7191
   333
    SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
slouken@7191
   334
    SDL_CONTROLLER_BUTTON_MAX
icculus@6917
   335
} SDL_GameControllerButton;
slouken@6690
   336
slouken@6690
   337
/**
slouken@6690
   338
 *  turn this string into a button mapping
slouken@6690
   339
 */
icculus@6917
   340
extern DECLSPEC SDL_GameControllerButton SDLCALL SDL_GameControllerGetButtonFromString(const char *pchString);
slouken@6690
   341
urkle@6964
   342
/**
urkle@6964
   343
 *  turn this button enum into a string mapping
urkle@6964
   344
 */
urkle@6964
   345
extern DECLSPEC const char* SDLCALL SDL_GameControllerGetStringForButton(SDL_GameControllerButton button);
slouken@6690
   346
slouken@6690
   347
/**
slouken@6932
   348
 *  Get the SDL joystick layer binding for this controller button mapping
slouken@6690
   349
 */
jorgen@6873
   350
extern DECLSPEC SDL_GameControllerButtonBind SDLCALL
jorgen@6873
   351
SDL_GameControllerGetBindForButton(SDL_GameController *gamecontroller,
icculus@6917
   352
                                   SDL_GameControllerButton button);
slouken@6690
   353
slouken@6690
   354
slouken@6690
   355
/**
slouken@6690
   356
 *  Get the current state of a button on a game controller.
jorgen@6873
   357
 *
slouken@6690
   358
 *  The button indices start at index 0.
slouken@6690
   359
 */
jorgen@6873
   360
extern DECLSPEC Uint8 SDLCALL SDL_GameControllerGetButton(SDL_GameController *gamecontroller,
icculus@6917
   361
                                                          SDL_GameControllerButton button);
slouken@6690
   362
slouken@6690
   363
/**
slouken@12088
   364
 *  Trigger a rumble effect
slouken@12088
   365
 *  Each call to this function cancels any previous rumble effect, and calling it with 0 intensity stops any rumbling.
slouken@12088
   366
 *
slouken@12088
   367
 *  \param gamecontroller The controller to vibrate
slouken@12088
   368
 *  \param low_frequency_rumble The intensity of the low frequency (left) rumble motor, from 0 to 0xFFFF
slouken@12088
   369
 *  \param high_frequency_rumble The intensity of the high frequency (right) rumble motor, from 0 to 0xFFFF
slouken@12088
   370
 *  \param duration_ms The duration of the rumble effect, in milliseconds
slouken@12088
   371
 *
slouken@12088
   372
 *  \return 0, or -1 if rumble isn't supported on this joystick
slouken@12088
   373
 */
slouken@12088
   374
extern DECLSPEC int SDLCALL SDL_GameControllerRumble(SDL_GameController *gamecontroller, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms);
slouken@12088
   375
slouken@12088
   376
/**
slouken@6690
   377
 *  Close a controller previously opened with SDL_GameControllerOpen().
slouken@6690
   378
 */
jorgen@6873
   379
extern DECLSPEC void SDLCALL SDL_GameControllerClose(SDL_GameController *gamecontroller);
slouken@6690
   380
slouken@6690
   381
slouken@6690
   382
/* Ends C function definitions when using C++ */
slouken@6690
   383
#ifdef __cplusplus
slouken@6690
   384
}
slouken@6690
   385
#endif
slouken@6690
   386
#include "close_code.h"
slouken@6690
   387
slouken@10638
   388
#endif /* SDL_gamecontroller_h_ */
slouken@6690
   389
slouken@6690
   390
/* vi: set ts=4 sw=4 expandtab: */