SDL  2.0
SDL_gamecontroller.h File Reference
#include "SDL_stdinc.h"
#include "SDL_error.h"
#include "SDL_rwops.h"
#include "SDL_joystick.h"
#include "begin_code.h"
#include "close_code.h"
+ Include dependency graph for SDL_gamecontroller.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_GameControllerButtonBind
 

Macros

#define SDL_GameControllerAddMappingsFromFile(file)   SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)
 

Enumerations

enum  SDL_GameControllerBindType {
  SDL_CONTROLLER_BINDTYPE_NONE = 0,
  SDL_CONTROLLER_BINDTYPE_BUTTON,
  SDL_CONTROLLER_BINDTYPE_AXIS,
  SDL_CONTROLLER_BINDTYPE_HAT
}
 
enum  SDL_GameControllerAxis {
  SDL_CONTROLLER_AXIS_INVALID = -1,
  SDL_CONTROLLER_AXIS_LEFTX,
  SDL_CONTROLLER_AXIS_LEFTY,
  SDL_CONTROLLER_AXIS_RIGHTX,
  SDL_CONTROLLER_AXIS_RIGHTY,
  SDL_CONTROLLER_AXIS_TRIGGERLEFT,
  SDL_CONTROLLER_AXIS_TRIGGERRIGHT,
  SDL_CONTROLLER_AXIS_MAX
}
 
enum  SDL_GameControllerButton {
  SDL_CONTROLLER_BUTTON_INVALID = -1,
  SDL_CONTROLLER_BUTTON_A,
  SDL_CONTROLLER_BUTTON_B,
  SDL_CONTROLLER_BUTTON_X,
  SDL_CONTROLLER_BUTTON_Y,
  SDL_CONTROLLER_BUTTON_BACK,
  SDL_CONTROLLER_BUTTON_GUIDE,
  SDL_CONTROLLER_BUTTON_START,
  SDL_CONTROLLER_BUTTON_LEFTSTICK,
  SDL_CONTROLLER_BUTTON_RIGHTSTICK,
  SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
  SDL_CONTROLLER_BUTTON_RIGHTSHOULDER,
  SDL_CONTROLLER_BUTTON_DPAD_UP,
  SDL_CONTROLLER_BUTTON_DPAD_DOWN,
  SDL_CONTROLLER_BUTTON_DPAD_LEFT,
  SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
  SDL_CONTROLLER_BUTTON_MAX
}
 

Functions

int SDL_GameControllerAddMappingsFromRW (SDL_RWops *rw, int freerw)
 
int SDL_GameControllerAddMapping (const char *mappingString)
 
char * SDL_GameControllerMappingForGUID (SDL_JoystickGUID guid)
 
char * SDL_GameControllerMapping (SDL_GameController *gamecontroller)
 
SDL_bool SDL_IsGameController (int joystick_index)
 
const char * SDL_GameControllerNameForIndex (int joystick_index)
 
SDL_GameController * SDL_GameControllerOpen (int joystick_index)
 
SDL_GameController * SDL_GameControllerFromInstanceID (SDL_JoystickID joyid)
 
const char * SDL_GameControllerName (SDL_GameController *gamecontroller)
 
SDL_bool SDL_GameControllerGetAttached (SDL_GameController *gamecontroller)
 
SDL_Joystick * SDL_GameControllerGetJoystick (SDL_GameController *gamecontroller)
 
int SDL_GameControllerEventState (int state)
 
void SDL_GameControllerUpdate (void)
 
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString (const char *pchString)
 
const char * SDL_GameControllerGetStringForAxis (SDL_GameControllerAxis axis)
 
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis (SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
 
Sint16 SDL_GameControllerGetAxis (SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
 
SDL_GameControllerButton SDL_GameControllerGetButtonFromString (const char *pchString)
 
const char * SDL_GameControllerGetStringForButton (SDL_GameControllerButton button)
 
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton (SDL_GameController *gamecontroller, SDL_GameControllerButton button)
 
Uint8 SDL_GameControllerGetButton (SDL_GameController *gamecontroller, SDL_GameControllerButton button)
 
void SDL_GameControllerClose (SDL_GameController *gamecontroller)
 

Detailed Description

Include file for SDL game controller event handling

In order to use these functions, SDL_Init() must have been called with the SDL_INIT_GAMECONTROLLER flag. This causes SDL to scan the system for game controllers, and load appropriate drivers.

If you would like to receive controller updates while the application is in the background, you should set the following hint before calling SDL_Init(): SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS

Definition in file SDL_gamecontroller.h.

Macro Definition Documentation

#define SDL_GameControllerAddMappingsFromFile (   file)    SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(file, "rb"), 1)

Load a set of mappings from a file, filtered by the current SDL_GetPlatform()

Convenience macro.

Definition at line 127 of file SDL_gamecontroller.h.

Referenced by main().

Enumeration Type Documentation

The list of axes available from a controller

Enumerator
SDL_CONTROLLER_AXIS_INVALID 
SDL_CONTROLLER_AXIS_LEFTX 
SDL_CONTROLLER_AXIS_LEFTY 
SDL_CONTROLLER_AXIS_RIGHTX 
SDL_CONTROLLER_AXIS_RIGHTY 
SDL_CONTROLLER_AXIS_TRIGGERLEFT 
SDL_CONTROLLER_AXIS_TRIGGERRIGHT 
SDL_CONTROLLER_AXIS_MAX 

Definition at line 218 of file SDL_gamecontroller.h.

Enumerator
SDL_CONTROLLER_BINDTYPE_NONE 
SDL_CONTROLLER_BINDTYPE_BUTTON 
SDL_CONTROLLER_BINDTYPE_AXIS 
SDL_CONTROLLER_BINDTYPE_HAT 

Definition at line 59 of file SDL_gamecontroller.h.

The list of buttons available from a controller

Enumerator
SDL_CONTROLLER_BUTTON_INVALID 
SDL_CONTROLLER_BUTTON_A 
SDL_CONTROLLER_BUTTON_B 
SDL_CONTROLLER_BUTTON_X 
SDL_CONTROLLER_BUTTON_Y 
SDL_CONTROLLER_BUTTON_BACK 
SDL_CONTROLLER_BUTTON_GUIDE 
SDL_CONTROLLER_BUTTON_START 
SDL_CONTROLLER_BUTTON_LEFTSTICK 
SDL_CONTROLLER_BUTTON_RIGHTSTICK 
SDL_CONTROLLER_BUTTON_LEFTSHOULDER 
SDL_CONTROLLER_BUTTON_RIGHTSHOULDER 
SDL_CONTROLLER_BUTTON_DPAD_UP 
SDL_CONTROLLER_BUTTON_DPAD_DOWN 
SDL_CONTROLLER_BUTTON_DPAD_LEFT 
SDL_CONTROLLER_BUTTON_DPAD_RIGHT 
SDL_CONTROLLER_BUTTON_MAX 

Definition at line 262 of file SDL_gamecontroller.h.

263 {
SDL_GameControllerButton

Function Documentation

int SDL_GameControllerAddMapping ( const char *  mappingString)

Add or update an existing mapping configuration

Returns
1 if mapping is added, 0 if updated, -1 on error

Definition at line 746 of file SDL_gamecontroller.c.

References SDL_FALSE, SDL_free(), SDL_InvalidParamError, SDL_JoystickGetGUIDFromString, SDL_PrivateAddMappingForGUID(), SDL_PrivateGetControllerGUIDFromMappingString(), SDL_SetError, SDL_strcasecmp, and SDL_TRUE.

Referenced by SDL_GameControllerAddMappingsFromRW(), SDL_GameControllerInit(), and SDL_GameControllerLoadHints().

747 {
748  char *pchGUID;
749  SDL_JoystickGUID jGUID;
750  SDL_bool is_xinput_mapping = SDL_FALSE;
751  SDL_bool is_emscripten_mapping = SDL_FALSE;
752  SDL_bool existing = SDL_FALSE;
753  ControllerMapping_t *pControllerMapping;
754 
755  if (!mappingString) {
756  return SDL_InvalidParamError("mappingString");
757  }
758 
759  pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
760  if (!pchGUID) {
761  return SDL_SetError("Couldn't parse GUID from %s", mappingString);
762  }
763  if (!SDL_strcasecmp(pchGUID, "xinput")) {
764  is_xinput_mapping = SDL_TRUE;
765  }
766  if (!SDL_strcasecmp(pchGUID, "emscripten")) {
767  is_emscripten_mapping = SDL_TRUE;
768  }
769  jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
770  SDL_free(pchGUID);
771 
772  pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing);
773  if (!pControllerMapping) {
774  return -1;
775  }
776 
777  if (existing) {
778  return 0;
779  } else {
780  if (is_xinput_mapping) {
781  s_pXInputMapping = pControllerMapping;
782  }
783  if (is_emscripten_mapping) {
784  s_pEmscriptenMapping = pControllerMapping;
785  }
786  return 1;
787  }
788 }
static ControllerMapping_t * SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing)
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
#define SDL_strcasecmp
static ControllerMapping_t * s_pEmscriptenMapping
SDL_bool
Definition: SDL_stdinc.h:126
void SDL_free(void *mem)
#define SDL_SetError
static ControllerMapping_t * s_pXInputMapping
char * SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
#define SDL_JoystickGetGUIDFromString
int SDL_GameControllerAddMappingsFromRW ( SDL_RWops rw,
int  freerw 
)

To count the number of game controllers in the system for the following: int nJoysticks = SDL_NumJoysticks(); int nGameControllers = 0; for ( int i = 0; i < nJoysticks; i++ ) { if ( SDL_IsGameController(i) ) { nGameControllers++; } }

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: guid,name,mappings

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. Under Windows there is a reserved GUID of "xinput" that covers any XInput devices. The mapping format for joystick is: bX - a joystick button, index X hX.Y - hat X with value Y aX - axis X of the joystick Buttons can be used as a controller axis and vice versa.

This string shows an example of a valid mapping for a controller "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", Load a set of mappings from a seekable SDL data stream (memory or file), filtered by the current SDL_GetPlatform() A community sourced database of controllers is available at https://raw.github.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt

If freerw is non-zero, the stream will be closed after being read.

Returns
number of mappings added, -1 on error

Definition at line 675 of file SDL_gamecontroller.c.

References sort_controllers::controllers, NULL, SDL_arraysize, SDL_CONTROLLER_PLATFORM_FIELD, SDL_free(), SDL_GameControllerAddMapping(), SDL_GetPlatform, SDL_malloc, SDL_RWclose, SDL_RWread, SDL_RWsize, SDL_SetError, SDL_strchr, SDL_strlcpy, SDL_strlen, SDL_strncasecmp, and SDL_strstr.

676 {
677  const char *platform = SDL_GetPlatform();
678  int controllers = 0;
679  char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
680  size_t db_size, platform_len;
681 
682  if (rw == NULL) {
683  return SDL_SetError("Invalid RWops");
684  }
685  db_size = (size_t)SDL_RWsize(rw);
686 
687  buf = (char *)SDL_malloc(db_size + 1);
688  if (buf == NULL) {
689  if (freerw) {
690  SDL_RWclose(rw);
691  }
692  return SDL_SetError("Could not allocate space to read DB into memory");
693  }
694 
695  if (SDL_RWread(rw, buf, db_size, 1) != 1) {
696  if (freerw) {
697  SDL_RWclose(rw);
698  }
699  SDL_free(buf);
700  return SDL_SetError("Could not read DB");
701  }
702 
703  if (freerw) {
704  SDL_RWclose(rw);
705  }
706 
707  buf[db_size] = '\0';
708  line = buf;
709 
710  while (line < buf + db_size) {
711  line_end = SDL_strchr(line, '\n');
712  if (line_end != NULL) {
713  *line_end = '\0';
714  } else {
715  line_end = buf + db_size;
716  }
717 
718  /* Extract and verify the platform */
720  if (tmp != NULL) {
722  comma = SDL_strchr(tmp, ',');
723  if (comma != NULL) {
724  platform_len = comma - tmp + 1;
725  if (platform_len + 1 < SDL_arraysize(line_platform)) {
726  SDL_strlcpy(line_platform, tmp, platform_len);
727  if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
728  SDL_GameControllerAddMapping(line) > 0) {
729  controllers++;
730  }
731  }
732  }
733  }
734 
735  line = line_end + 1;
736  }
737 
738  SDL_free(buf);
739  return controllers;
740 }
#define SDL_strlcpy
#define SDL_CONTROLLER_PLATFORM_FIELD
#define SDL_RWsize(ctx)
Definition: SDL_rwops.h:184
#define SDL_RWread(ctx, ptr, size, n)
Definition: SDL_rwops.h:187
#define SDL_strncasecmp
#define SDL_strchr
unsigned int size_t
int SDL_GameControllerAddMapping(const char *mappingString)
void SDL_free(void *mem)
#define SDL_GetPlatform
GLenum GLuint GLenum GLsizei const GLchar * buf
#define NULL
Definition: begin_code.h:143
#define SDL_RWclose(ctx)
Definition: SDL_rwops.h:189
#define SDL_SetError
#define SDL_strlen
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
#define SDL_malloc
#define SDL_strstr
void SDL_GameControllerClose ( SDL_GameController *  gamecontroller)

Close a controller previously opened with SDL_GameControllerOpen().

Definition at line 1161 of file SDL_gamecontroller.c.

References NULL, SDL_free(), SDL_gamecontrollers, and SDL_JoystickClose.

Referenced by SDL_GameControllerQuit().

1162 {
1163  SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
1164 
1165  if (!gamecontroller)
1166  return;
1167 
1168  /* First decrement ref count */
1169  if (--gamecontroller->ref_count > 0) {
1170  return;
1171  }
1172 
1173  SDL_JoystickClose(gamecontroller->joystick);
1174 
1175  gamecontrollerlist = SDL_gamecontrollers;
1176  gamecontrollerlistprev = NULL;
1177  while (gamecontrollerlist) {
1178  if (gamecontroller == gamecontrollerlist) {
1179  if (gamecontrollerlistprev) {
1180  /* unlink this entry */
1181  gamecontrollerlistprev->next = gamecontrollerlist->next;
1182  } else {
1183  SDL_gamecontrollers = gamecontroller->next;
1184  }
1185 
1186  break;
1187  }
1188  gamecontrollerlistprev = gamecontrollerlist;
1189  gamecontrollerlist = gamecontrollerlist->next;
1190  }
1191 
1192  SDL_free(gamecontroller);
1193 }
#define SDL_JoystickClose
static SDL_GameController * SDL_gamecontrollers
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
int SDL_GameControllerEventState ( int  state)

Enable/disable controller event polling.

If controller events are disabled, you must call SDL_GameControllerUpdate() yourself and check the state of the controller when you want controller information.

The state can be one of SDL_QUERY, SDL_ENABLE or SDL_IGNORE.

Definition at line 1287 of file SDL_gamecontroller.c.

References i, SDL_arraysize, SDL_CONTROLLERAXISMOTION, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMAPPED, SDL_CONTROLLERDEVICEREMOVED, SDL_ENABLE, SDL_EventState, SDL_IGNORE, and SDL_QUERY.

1288 {
1289 #if SDL_EVENTS_DISABLED
1290  return SDL_IGNORE;
1291 #else
1292  const Uint32 event_list[] = {
1295  };
1296  unsigned int i;
1297 
1298  switch (state) {
1299  case SDL_QUERY:
1300  state = SDL_IGNORE;
1301  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1302  state = SDL_EventState(event_list[i], SDL_QUERY);
1303  if (state == SDL_ENABLE) {
1304  break;
1305  }
1306  }
1307  break;
1308  default:
1309  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1310  SDL_EventState(event_list[i], state);
1311  }
1312  break;
1313  }
1314  return (state);
1315 #endif /* SDL_EVENTS_DISABLED */
1316 }
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:718
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define SDL_EventState
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
#define SDL_QUERY
Definition: SDL_events.h:715
#define SDL_IGNORE
Definition: SDL_events.h:716
SDL_GameController* SDL_GameControllerFromInstanceID ( SDL_JoystickID  joyid)

Return the SDL_GameController associated with an instance id.

Definition at line 1096 of file SDL_gamecontroller.c.

References NULL, and SDL_gamecontrollers.

1097 {
1098  SDL_GameController *gamecontroller = SDL_gamecontrollers;
1099  while (gamecontroller) {
1100  if (gamecontroller->joystick->instance_id == joyid) {
1101  return gamecontroller;
1102  }
1103  gamecontroller = gamecontroller->next;
1104  }
1105 
1106  return NULL;
1107 }
static SDL_GameController * SDL_gamecontrollers
#define NULL
Definition: begin_code.h:143
SDL_bool SDL_GameControllerGetAttached ( SDL_GameController *  gamecontroller)

Returns SDL_TRUE if the controller has been opened and currently connected, or SDL_FALSE if it has not.

Definition at line 1061 of file SDL_gamecontroller.c.

References SDL_FALSE, and SDL_JoystickGetAttached.

1062 {
1063  if (!gamecontroller)
1064  return SDL_FALSE;
1065 
1066  return SDL_JoystickGetAttached(gamecontroller->joystick);
1067 }
#define SDL_JoystickGetAttached
Sint16 SDL_GameControllerGetAxis ( SDL_GameController *  gamecontroller,
SDL_GameControllerAxis  axis 
)

Get the current state of an axis control on a game controller.

The state is a value ranging from -32768 to 32767 (except for the triggers, which range from 0 to 32767).

The axis indices start at index 0.

Definition at line 1000 of file SDL_gamecontroller.c.

References SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_JoystickGetAxis, and SDL_JoystickGetButton.

1001 {
1002  if (!gamecontroller)
1003  return 0;
1004 
1005  if (gamecontroller->mapping.axes[axis] >= 0) {
1006  Sint16 value = (SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axes[axis]));
1007  switch (axis) {
1010  /* Shift it to be 0 - 32767. */
1011  value = value / 2 + 16384;
1012  default:
1013  break;
1014  }
1015  return value;
1016  } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
1017  Uint8 value;
1018  value = SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis]);
1019  if (value > 0)
1020  return 32767;
1021  return 0;
1022  }
1023  return 0;
1024 }
#define SDL_JoystickGetButton
SDL_Texture * axis
GLsizei const GLfloat * value
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
#define SDL_JoystickGetAxis
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:143
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString ( const char *  pchString)

turn this string into a axis mapping

Definition at line 270 of file SDL_gamecontroller.c.

References map_StringForControllerAxis, SDL_CONTROLLER_AXIS_INVALID, and SDL_strcasecmp.

Referenced by SDL_PrivateGameControllerParseButton().

271 {
272  int entry;
273  if (!pchString || !pchString[0])
275 
276  for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
277  if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
278  return entry;
279  }
281 }
static const char * map_StringForControllerAxis[]
#define SDL_strcasecmp
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis ( SDL_GameController *  gamecontroller,
SDL_GameControllerAxis  axis 
)

Get the SDL joystick layer binding for this controller button mapping

Get the SDL joystick layer binding for this controller axis mapping

Definition at line 1113 of file SDL_gamecontroller.c.

References axis, SDL_GameControllerButtonBind::bindType, SDL_GameControllerButtonBind::button, SDL_CONTROLLER_AXIS_INVALID, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_CONTROLLER_BINDTYPE_BUTTON, SDL_memset, and SDL_GameControllerButtonBind::value.

1114 {
1116  SDL_memset(&bind, 0x0, sizeof(bind));
1117 
1118  if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
1119  return bind;
1120 
1121  if (gamecontroller->mapping.axes[axis] >= 0) {
1123  bind.value.button = gamecontroller->mapping.axes[axis];
1124  } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
1126  bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
1127  }
1128 
1129  return bind;
1130 }
SDL_Texture * axis
SDL_GameControllerBindType bindType
union SDL_GameControllerButtonBind::@0 value
GLuint GLfloat x0
#define SDL_memset
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton ( SDL_GameController *  gamecontroller,
SDL_GameControllerButton  button 
)

Get the SDL joystick layer binding for this controller button mapping

Definition at line 1136 of file SDL_gamecontroller.c.

References SDL_GameControllerButtonBind::axis, SDL_GameControllerButtonBind::bindType, button, SDL_GameControllerButtonBind::button, SDL_GameControllerButtonBind::hat, SDL_CONTROLLER_BINDTYPE_AXIS, SDL_CONTROLLER_BINDTYPE_BUTTON, SDL_CONTROLLER_BINDTYPE_HAT, SDL_CONTROLLER_BUTTON_INVALID, SDL_memset, and SDL_GameControllerButtonBind::value.

1137 {
1139  SDL_memset(&bind, 0x0, sizeof(bind));
1140 
1141  if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
1142  return bind;
1143 
1144  if (gamecontroller->mapping.buttons[button] >= 0) {
1146  bind.value.button = gamecontroller->mapping.buttons[button];
1147  } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
1149  bind.value.axis = gamecontroller->mapping.axesasbutton[button];
1150  } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
1152  bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
1153  bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
1154  }
1155 
1156  return bind;
1157 }
SDL_Texture * button
SDL_GameControllerBindType bindType
union SDL_GameControllerButtonBind::@0 value
GLuint GLfloat x0
#define SDL_memset
Uint8 SDL_GameControllerGetButton ( SDL_GameController *  gamecontroller,
SDL_GameControllerButton  button 
)

Get the current state of a button on a game controller.

The button indices start at index 0.

Definition at line 1031 of file SDL_gamecontroller.c.

References ABS, SDL_JoystickGetAxis, SDL_JoystickGetButton, and SDL_JoystickGetHat.

1032 {
1033  if (!gamecontroller)
1034  return 0;
1035 
1036  if (gamecontroller->mapping.buttons[button] >= 0) {
1037  return (SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttons[button]));
1038  } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
1039  Sint16 value;
1040  value = SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button]);
1041  if (ABS(value) > 32768/2)
1042  return 1;
1043  return 0;
1044  } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
1045  Uint8 value;
1046  value = SDL_JoystickGetHat(gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat);
1047 
1048  if (value & gamecontroller->mapping.hatasbutton[button].mask)
1049  return 1;
1050  return 0;
1051  }
1052 
1053  return 0;
1054 }
#define SDL_JoystickGetButton
SDL_Texture * button
#define ABS(_x)
#define SDL_JoystickGetHat
GLsizei const GLfloat * value
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
#define SDL_JoystickGetAxis
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:143
SDL_GameControllerButton SDL_GameControllerGetButtonFromString ( const char *  pchString)

turn this string into a button mapping

Definition at line 316 of file SDL_gamecontroller.c.

References map_StringForControllerButton, SDL_CONTROLLER_BUTTON_INVALID, and SDL_strcasecmp.

Referenced by SDL_PrivateGameControllerParseButton().

317 {
318  int entry;
319  if (!pchString || !pchString[0])
321 
322  for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
323  if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
324  return entry;
325  }
327 }
#define SDL_strcasecmp
static const char * map_StringForControllerButton[]
SDL_Joystick* SDL_GameControllerGetJoystick ( SDL_GameController *  gamecontroller)

Get the underlying joystick object used by a controller

Definition at line 1083 of file SDL_gamecontroller.c.

References NULL.

1084 {
1085  if (!gamecontroller)
1086  return NULL;
1087 
1088  return gamecontroller->joystick;
1089 }
#define NULL
Definition: begin_code.h:143
const char* SDL_GameControllerGetStringForAxis ( SDL_GameControllerAxis  axis)

turn this axis enum into a string mapping

Definition at line 286 of file SDL_gamecontroller.c.

References axis, map_StringForControllerAxis, NULL, SDL_CONTROLLER_AXIS_INVALID, and SDL_CONTROLLER_AXIS_MAX.

287 {
290  }
291  return NULL;
292 }
static const char * map_StringForControllerAxis[]
SDL_Texture * axis
#define NULL
Definition: begin_code.h:143
const char* SDL_GameControllerGetStringForButton ( SDL_GameControllerButton  button)

turn this button enum into a string mapping

Definition at line 332 of file SDL_gamecontroller.c.

References axis, map_StringForControllerButton, NULL, SDL_CONTROLLER_BUTTON_INVALID, and SDL_CONTROLLER_BUTTON_MAX.

333 {
336  }
337  return NULL;
338 }
SDL_Texture * axis
static const char * map_StringForControllerButton[]
#define NULL
Definition: begin_code.h:143
char* SDL_GameControllerMapping ( SDL_GameController *  gamecontroller)

Get a mapping string for an open GameController

Returns
the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available

Definition at line 818 of file SDL_gamecontroller.c.

References NULL, and SDL_GameControllerMappingForGUID().

819 {
820  if (!gamecontroller) {
821  return NULL;
822  }
823 
824  return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
825 }
#define NULL
Definition: begin_code.h:143
char * SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)
char* SDL_GameControllerMappingForGUID ( SDL_JoystickGUID  guid)

Get a mapping string for a GUID

Returns
the mapping string. Must be freed with SDL_free. Returns NULL if no mapping is available

Definition at line 794 of file SDL_gamecontroller.c.

References ControllerMapping_t::mapping, ControllerMapping_t::name, NULL, SDL_JoystickGetGUIDString, SDL_malloc, SDL_OutOfMemory, SDL_PrivateGetControllerMappingForGUID(), SDL_snprintf, and SDL_strlen.

Referenced by SDL_GameControllerMapping().

795 {
796  char *pMappingString = NULL;
798  if (mapping) {
799  char pchGUID[33];
800  size_t needed;
801  SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
802  /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
803  needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
804  pMappingString = SDL_malloc(needed);
805  if (!pMappingString) {
806  SDL_OutOfMemory();
807  return NULL;
808  }
809  SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
810  }
811  return pMappingString;
812 }
ControllerMapping_t * SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
#define SDL_JoystickGetGUIDString
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_strlen
#define SDL_snprintf
GLenum GLenum GLenum GLenum mapping
#define SDL_malloc
const char* SDL_GameControllerName ( SDL_GameController *  gamecontroller)

Return the name for this currently opened controller

Definition at line 1071 of file SDL_gamecontroller.c.

References NULL.

1072 {
1073  if (!gamecontroller)
1074  return NULL;
1075 
1076  return (gamecontroller->mapping.name);
1077 }
#define NULL
Definition: begin_code.h:143
const char* SDL_GameControllerNameForIndex ( int  joystick_index)

Get the implementation dependent name of a game controller. This can be called before any controllers are opened. If no name can be found, this function returns NULL.

Definition at line 897 of file SDL_gamecontroller.c.

References ControllerMapping_t::name, NULL, and SDL_PrivateGetControllerMapping().

898 {
899  ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
900  if (pSupportedController) {
901  return pSupportedController->name;
902  }
903  return NULL;
904 }
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)
#define NULL
Definition: begin_code.h:143
SDL_GameController* SDL_GameControllerOpen ( int  joystick_index)

Open a game controller for use. The index passed as an argument refers to the N'th game controller on the system. This index is not the value which will identify this controller in future controller events. The joystick's instance id (SDL_JoystickID) will be used there instead.

Returns
A controller identifier, or NULL if an error occurred.

Definition at line 929 of file SDL_gamecontroller.c.

References ControllerMapping_t::guid, ControllerMapping_t::mapping, ControllerMapping_t::name, NULL, SDL_free(), SDL_gamecontrollers, SDL_JoystickOpen, SDL_malloc, SDL_memset, SDL_NumJoysticks, SDL_OutOfMemory, SDL_PrivateGetControllerMapping(), SDL_PrivateLoadButtonMapping(), SDL_SetError, SDL_SYS_GetInstanceIdOfDeviceIndex(), and SDL_SYS_JoystickUpdate().

930 {
931  SDL_GameController *gamecontroller;
932  SDL_GameController *gamecontrollerlist;
933  ControllerMapping_t *pSupportedController = NULL;
934 
935  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
936  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
937  return (NULL);
938  }
939 
940  gamecontrollerlist = SDL_gamecontrollers;
941  /* If the controller is already open, return it */
942  while (gamecontrollerlist) {
943  if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
944  gamecontroller = gamecontrollerlist;
945  ++gamecontroller->ref_count;
946  return (gamecontroller);
947  }
948  gamecontrollerlist = gamecontrollerlist->next;
949  }
950 
951  /* Find a controller mapping */
952  pSupportedController = SDL_PrivateGetControllerMapping(device_index);
953  if (!pSupportedController) {
954  SDL_SetError("Couldn't find mapping for device (%d)", device_index);
955  return (NULL);
956  }
957 
958  /* Create and initialize the joystick */
959  gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
960  if (gamecontroller == NULL) {
961  SDL_OutOfMemory();
962  return NULL;
963  }
964 
965  SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
966  gamecontroller->joystick = SDL_JoystickOpen(device_index);
967  if (!gamecontroller->joystick) {
968  SDL_free(gamecontroller);
969  return NULL;
970  }
971 
972  SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
973 
974  /* Add joystick to list */
975  ++gamecontroller->ref_count;
976  /* Link the joystick in the list */
977  gamecontroller->next = SDL_gamecontrollers;
978  SDL_gamecontrollers = gamecontroller;
979 
980  SDL_SYS_JoystickUpdate(gamecontroller->joystick);
981 
982  return (gamecontroller);
983 }
#define SDL_JoystickOpen
#define SDL_NumJoysticks
SDL_JoystickGUID guid
static SDL_GameController * SDL_gamecontrollers
void SDL_free(void *mem)
void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
#define SDL_malloc
#define SDL_memset
void SDL_GameControllerUpdate ( void  )

Update the current state of the open game controllers.

This is called automatically by the event loop if any game controller events are enabled.

Definition at line 989 of file SDL_gamecontroller.c.

References SDL_JoystickUpdate.

990 {
991  /* Just for API completeness; the joystick API does all the work. */
993 }
#define SDL_JoystickUpdate
SDL_bool SDL_IsGameController ( int  joystick_index)

Is the joystick on this index supported by the game controller interface?

Definition at line 911 of file SDL_gamecontroller.c.

References SDL_FALSE, SDL_PrivateGetControllerMapping(), and SDL_TRUE.

Referenced by SDL_GameControllerEventWatcher(), and SDL_GameControllerInit().

912 {
913  ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
914  if (pSupportedController) {
915  return SDL_TRUE;
916  }
917 
918  return SDL_FALSE;
919 }
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)