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

◆ SDL_GameControllerAddMappingsFromFile

#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

◆ SDL_GameControllerAxis

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.

◆ SDL_GameControllerBindType

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.

◆ SDL_GameControllerButton

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

◆ SDL_GameControllerAddMapping()

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 787 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().

788 {
789  char *pchGUID;
790  SDL_JoystickGUID jGUID;
791  SDL_bool is_xinput_mapping = SDL_FALSE;
792  SDL_bool is_emscripten_mapping = SDL_FALSE;
793  SDL_bool existing = SDL_FALSE;
794  ControllerMapping_t *pControllerMapping;
795 
796  if (!mappingString) {
797  return SDL_InvalidParamError("mappingString");
798  }
799 
800  pchGUID = SDL_PrivateGetControllerGUIDFromMappingString(mappingString);
801  if (!pchGUID) {
802  return SDL_SetError("Couldn't parse GUID from %s", mappingString);
803  }
804  if (!SDL_strcasecmp(pchGUID, "xinput")) {
805  is_xinput_mapping = SDL_TRUE;
806  }
807  if (!SDL_strcasecmp(pchGUID, "emscripten")) {
808  is_emscripten_mapping = SDL_TRUE;
809  }
810  jGUID = SDL_JoystickGetGUIDFromString(pchGUID);
811  SDL_free(pchGUID);
812 
813  pControllerMapping = SDL_PrivateAddMappingForGUID(jGUID, mappingString, &existing);
814  if (!pControllerMapping) {
815  return -1;
816  }
817 
818  if (existing) {
819  return 0;
820  } else {
821  if (is_xinput_mapping) {
822  s_pXInputMapping = pControllerMapping;
823  }
824  if (is_emscripten_mapping) {
825  s_pEmscriptenMapping = pControllerMapping;
826  }
827  return 1;
828  }
829 }
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
void SDL_free(void *mem)
SDL_bool
Definition: SDL_stdinc.h:130
#define SDL_SetError
static ControllerMapping_t * s_pXInputMapping
char * SDL_PrivateGetControllerGUIDFromMappingString(const char *pMapping)
#define SDL_JoystickGetGUIDFromString

◆ SDL_GameControllerAddMappingsFromRW()

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 716 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.

717 {
718  const char *platform = SDL_GetPlatform();
719  int controllers = 0;
720  char *buf, *line, *line_end, *tmp, *comma, line_platform[64];
721  size_t db_size, platform_len;
722 
723  if (rw == NULL) {
724  return SDL_SetError("Invalid RWops");
725  }
726  db_size = (size_t)SDL_RWsize(rw);
727 
728  buf = (char *)SDL_malloc(db_size + 1);
729  if (buf == NULL) {
730  if (freerw) {
731  SDL_RWclose(rw);
732  }
733  return SDL_SetError("Could not allocate space to read DB into memory");
734  }
735 
736  if (SDL_RWread(rw, buf, db_size, 1) != 1) {
737  if (freerw) {
738  SDL_RWclose(rw);
739  }
740  SDL_free(buf);
741  return SDL_SetError("Could not read DB");
742  }
743 
744  if (freerw) {
745  SDL_RWclose(rw);
746  }
747 
748  buf[db_size] = '\0';
749  line = buf;
750 
751  while (line < buf + db_size) {
752  line_end = SDL_strchr(line, '\n');
753  if (line_end != NULL) {
754  *line_end = '\0';
755  } else {
756  line_end = buf + db_size;
757  }
758 
759  /* Extract and verify the platform */
761  if (tmp != NULL) {
763  comma = SDL_strchr(tmp, ',');
764  if (comma != NULL) {
765  platform_len = comma - tmp + 1;
766  if (platform_len + 1 < SDL_arraysize(line_platform)) {
767  SDL_strlcpy(line_platform, tmp, platform_len);
768  if (SDL_strncasecmp(line_platform, platform, platform_len) == 0 &&
769  SDL_GameControllerAddMapping(line) > 0) {
770  controllers++;
771  }
772  }
773  }
774  }
775 
776  line = line_end + 1;
777  }
778 
779  SDL_free(buf);
780  return controllers;
781 }
#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:90
#define SDL_malloc
#define SDL_strstr

◆ SDL_GameControllerClose()

void SDL_GameControllerClose ( SDL_GameController *  gamecontroller)

Close a controller previously opened with SDL_GameControllerOpen().

Definition at line 1215 of file SDL_gamecontroller.c.

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

Referenced by SDL_GameControllerQuit().

1216 {
1217  SDL_GameController *gamecontrollerlist, *gamecontrollerlistprev;
1218 
1219  if (!gamecontroller)
1220  return;
1221 
1222  /* First decrement ref count */
1223  if (--gamecontroller->ref_count > 0) {
1224  return;
1225  }
1226 
1227  SDL_JoystickClose(gamecontroller->joystick);
1228 
1229  gamecontrollerlist = SDL_gamecontrollers;
1230  gamecontrollerlistprev = NULL;
1231  while (gamecontrollerlist) {
1232  if (gamecontroller == gamecontrollerlist) {
1233  if (gamecontrollerlistprev) {
1234  /* unlink this entry */
1235  gamecontrollerlistprev->next = gamecontrollerlist->next;
1236  } else {
1237  SDL_gamecontrollers = gamecontroller->next;
1238  }
1239 
1240  break;
1241  }
1242  gamecontrollerlistprev = gamecontrollerlist;
1243  gamecontrollerlist = gamecontrollerlist->next;
1244  }
1245 
1246  SDL_free(gamecontroller);
1247 }
#define SDL_JoystickClose
static SDL_GameController * SDL_gamecontrollers
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerEventState()

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 1341 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.

1342 {
1343 #if SDL_EVENTS_DISABLED
1344  return SDL_IGNORE;
1345 #else
1346  const Uint32 event_list[] = {
1349  };
1350  unsigned int i;
1351 
1352  switch (state) {
1353  case SDL_QUERY:
1354  state = SDL_IGNORE;
1355  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1356  state = SDL_EventState(event_list[i], SDL_QUERY);
1357  if (state == SDL_ENABLE) {
1358  break;
1359  }
1360  }
1361  break;
1362  default:
1363  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1364  SDL_EventState(event_list[i], state);
1365  }
1366  break;
1367  }
1368  return (state);
1369 #endif /* SDL_EVENTS_DISABLED */
1370 }
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:161
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:50
#define SDL_EventState
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
#define SDL_QUERY
Definition: SDL_events.h:719
#define SDL_IGNORE
Definition: SDL_events.h:720

◆ SDL_GameControllerFromInstanceID()

SDL_GameController* SDL_GameControllerFromInstanceID ( SDL_JoystickID  joyid)

Return the SDL_GameController associated with an instance id.

Definition at line 1150 of file SDL_gamecontroller.c.

References NULL, and SDL_gamecontrollers.

1151 {
1152  SDL_GameController *gamecontroller = SDL_gamecontrollers;
1153  while (gamecontroller) {
1154  if (gamecontroller->joystick->instance_id == joyid) {
1155  return gamecontroller;
1156  }
1157  gamecontroller = gamecontroller->next;
1158  }
1159 
1160  return NULL;
1161 }
static SDL_GameController * SDL_gamecontrollers
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerGetAttached()

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 1115 of file SDL_gamecontroller.c.

References SDL_FALSE, and SDL_JoystickGetAttached.

1116 {
1117  if (!gamecontroller)
1118  return SDL_FALSE;
1119 
1120  return SDL_JoystickGetAttached(gamecontroller->joystick);
1121 }
#define SDL_JoystickGetAttached

◆ SDL_GameControllerGetAxis()

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 1054 of file SDL_gamecontroller.c.

References SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_JoystickGetAxis, and SDL_JoystickGetButton.

1055 {
1056  if (!gamecontroller)
1057  return 0;
1058 
1059  if (gamecontroller->mapping.axes[axis] >= 0) {
1060  Sint16 value = (SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axes[axis]));
1061  switch (axis) {
1064  /* Shift it to be 0 - 32767 */
1065  value = value / 2 + 16384;
1066  default:
1067  break;
1068  }
1069  return value;
1070  } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
1071  Uint8 value;
1072  value = SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttonasaxis[axis]);
1073  if (value > 0)
1074  return 32767;
1075  return 0;
1076  }
1077  return 0;
1078 }
#define SDL_JoystickGetButton
SDL_Texture * axis
GLsizei const GLfloat * value
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_JoystickGetAxis
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147

◆ SDL_GameControllerGetAxisFromString()

SDL_GameControllerAxis SDL_GameControllerGetAxisFromString ( const char *  pchString)

turn this string into a axis mapping

Definition at line 311 of file SDL_gamecontroller.c.

References map_StringForControllerAxis, SDL_CONTROLLER_AXIS_INVALID, and SDL_strcasecmp.

Referenced by SDL_PrivateGameControllerParseButton().

312 {
313  int entry;
314  if (!pchString || !pchString[0])
316 
317  for (entry = 0; map_StringForControllerAxis[entry]; ++entry) {
318  if (!SDL_strcasecmp(pchString, map_StringForControllerAxis[entry]))
319  return entry;
320  }
322 }
static const char * map_StringForControllerAxis[]
#define SDL_strcasecmp

◆ SDL_GameControllerGetBindForAxis()

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 1167 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.

1168 {
1170  SDL_memset(&bind, 0x0, sizeof(bind));
1171 
1172  if (!gamecontroller || axis == SDL_CONTROLLER_AXIS_INVALID)
1173  return bind;
1174 
1175  if (gamecontroller->mapping.axes[axis] >= 0) {
1177  bind.value.button = gamecontroller->mapping.axes[axis];
1178  } else if (gamecontroller->mapping.buttonasaxis[axis] >= 0) {
1180  bind.value.button = gamecontroller->mapping.buttonasaxis[axis];
1181  }
1182 
1183  return bind;
1184 }
SDL_Texture * axis
SDL_GameControllerBindType bindType
union SDL_GameControllerButtonBind::@0 value
GLuint GLfloat x0
#define SDL_memset

◆ SDL_GameControllerGetBindForButton()

SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton ( SDL_GameController *  gamecontroller,
SDL_GameControllerButton  button 
)

Get the SDL joystick layer binding for this controller button mapping

Definition at line 1190 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.

1191 {
1193  SDL_memset(&bind, 0x0, sizeof(bind));
1194 
1195  if (!gamecontroller || button == SDL_CONTROLLER_BUTTON_INVALID)
1196  return bind;
1197 
1198  if (gamecontroller->mapping.buttons[button] >= 0) {
1200  bind.value.button = gamecontroller->mapping.buttons[button];
1201  } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
1203  bind.value.axis = gamecontroller->mapping.axesasbutton[button];
1204  } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
1206  bind.value.hat.hat = gamecontroller->mapping.hatasbutton[button].hat;
1207  bind.value.hat.hat_mask = gamecontroller->mapping.hatasbutton[button].mask;
1208  }
1209 
1210  return bind;
1211 }
SDL_Texture * button
SDL_GameControllerBindType bindType
union SDL_GameControllerButtonBind::@0 value
GLuint GLfloat x0
#define SDL_memset

◆ SDL_GameControllerGetButton()

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 1085 of file SDL_gamecontroller.c.

References ABS, SDL_JoystickGetAxis, SDL_JoystickGetButton, and SDL_JoystickGetHat.

1086 {
1087  if (!gamecontroller)
1088  return 0;
1089 
1090  if (gamecontroller->mapping.buttons[button] >= 0) {
1091  return (SDL_JoystickGetButton(gamecontroller->joystick, gamecontroller->mapping.buttons[button]));
1092  } else if (gamecontroller->mapping.axesasbutton[button] >= 0) {
1093  Sint16 value;
1094  value = SDL_JoystickGetAxis(gamecontroller->joystick, gamecontroller->mapping.axesasbutton[button]);
1095  if (ABS(value) > 32768/2)
1096  return 1;
1097  return 0;
1098  } else if (gamecontroller->mapping.hatasbutton[button].hat >= 0) {
1099  Uint8 value;
1100  value = SDL_JoystickGetHat(gamecontroller->joystick, gamecontroller->mapping.hatasbutton[button].hat);
1101 
1102  if (value & gamecontroller->mapping.hatasbutton[button].mask)
1103  return 1;
1104  return 0;
1105  }
1106 
1107  return 0;
1108 }
#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:143
#define SDL_JoystickGetAxis
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147

◆ SDL_GameControllerGetButtonFromString()

SDL_GameControllerButton SDL_GameControllerGetButtonFromString ( const char *  pchString)

turn this string into a button mapping

Definition at line 357 of file SDL_gamecontroller.c.

References map_StringForControllerButton, SDL_CONTROLLER_BUTTON_INVALID, and SDL_strcasecmp.

Referenced by SDL_PrivateGameControllerParseButton().

358 {
359  int entry;
360  if (!pchString || !pchString[0])
362 
363  for (entry = 0; map_StringForControllerButton[entry]; ++entry) {
364  if (SDL_strcasecmp(pchString, map_StringForControllerButton[entry]) == 0)
365  return entry;
366  }
368 }
#define SDL_strcasecmp
static const char * map_StringForControllerButton[]

◆ SDL_GameControllerGetJoystick()

SDL_Joystick* SDL_GameControllerGetJoystick ( SDL_GameController *  gamecontroller)

Get the underlying joystick object used by a controller

Definition at line 1137 of file SDL_gamecontroller.c.

References NULL.

1138 {
1139  if (!gamecontroller)
1140  return NULL;
1141 
1142  return gamecontroller->joystick;
1143 }
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerGetStringForAxis()

const char* SDL_GameControllerGetStringForAxis ( SDL_GameControllerAxis  axis)

turn this axis enum into a string mapping

Definition at line 327 of file SDL_gamecontroller.c.

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

328 {
331  }
332  return NULL;
333 }
static const char * map_StringForControllerAxis[]
SDL_Texture * axis
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerGetStringForButton()

const char* SDL_GameControllerGetStringForButton ( SDL_GameControllerButton  button)

turn this button enum into a string mapping

Definition at line 373 of file SDL_gamecontroller.c.

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

374 {
377  }
378  return NULL;
379 }
SDL_Texture * axis
static const char * map_StringForControllerButton[]
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerMapping()

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 859 of file SDL_gamecontroller.c.

References NULL, and SDL_GameControllerMappingForGUID().

860 {
861  if (!gamecontroller) {
862  return NULL;
863  }
864 
865  return SDL_GameControllerMappingForGUID(gamecontroller->mapping.guid);
866 }
#define NULL
Definition: begin_code.h:143
char * SDL_GameControllerMappingForGUID(SDL_JoystickGUID guid)

◆ SDL_GameControllerMappingForGUID()

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 835 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().

836 {
837  char *pMappingString = NULL;
839  if (mapping) {
840  char pchGUID[33];
841  size_t needed;
842  SDL_JoystickGetGUIDString(guid, pchGUID, sizeof(pchGUID));
843  /* allocate enough memory for GUID + ',' + name + ',' + mapping + \0 */
844  needed = SDL_strlen(pchGUID) + 1 + SDL_strlen(mapping->name) + 1 + SDL_strlen(mapping->mapping) + 1;
845  pMappingString = SDL_malloc(needed);
846  if (!pMappingString) {
847  SDL_OutOfMemory();
848  return NULL;
849  }
850  SDL_snprintf(pMappingString, needed, "%s,%s,%s", pchGUID, mapping->name, mapping->mapping);
851  }
852  return pMappingString;
853 }
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

◆ SDL_GameControllerName()

const char* SDL_GameControllerName ( SDL_GameController *  gamecontroller)

Return the name for this currently opened controller

Definition at line 1125 of file SDL_gamecontroller.c.

References NULL.

1126 {
1127  if (!gamecontroller)
1128  return NULL;
1129 
1130  return (gamecontroller->mapping.name);
1131 }
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerNameForIndex()

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 937 of file SDL_gamecontroller.c.

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

938 {
939  ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
940  if (pSupportedController) {
941  return pSupportedController->name;
942  }
943  return NULL;
944 }
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)
#define NULL
Definition: begin_code.h:143

◆ SDL_GameControllerOpen()

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 969 of file SDL_gamecontroller.c.

References ControllerMapping_t::guid, ControllerMapping_t::mapping, ControllerMapping_t::name, NULL, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 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().

970 {
971  SDL_GameController *gamecontroller;
972  SDL_GameController *gamecontrollerlist;
973  ControllerMapping_t *pSupportedController = NULL;
974 
975  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
976  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
977  return (NULL);
978  }
979 
980  gamecontrollerlist = SDL_gamecontrollers;
981  /* If the controller is already open, return it */
982  while (gamecontrollerlist) {
983  if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == gamecontrollerlist->joystick->instance_id) {
984  gamecontroller = gamecontrollerlist;
985  ++gamecontroller->ref_count;
986  return (gamecontroller);
987  }
988  gamecontrollerlist = gamecontrollerlist->next;
989  }
990 
991  /* Find a controller mapping */
992  pSupportedController = SDL_PrivateGetControllerMapping(device_index);
993  if (!pSupportedController) {
994  SDL_SetError("Couldn't find mapping for device (%d)", device_index);
995  return (NULL);
996  }
997 
998  /* Create and initialize the joystick */
999  gamecontroller = (SDL_GameController *) SDL_malloc((sizeof *gamecontroller));
1000  if (gamecontroller == NULL) {
1001  SDL_OutOfMemory();
1002  return NULL;
1003  }
1004 
1005  SDL_memset(gamecontroller, 0, (sizeof *gamecontroller));
1006  gamecontroller->joystick = SDL_JoystickOpen(device_index);
1007  if (!gamecontroller->joystick) {
1008  SDL_free(gamecontroller);
1009  return NULL;
1010  }
1011 
1012  SDL_PrivateLoadButtonMapping(&gamecontroller->mapping, pSupportedController->guid, pSupportedController->name, pSupportedController->mapping);
1013 
1014  /* The triggers are mapped from -32768 to 32767, where -32768 is the 'unpressed' value */
1015  {
1016  int leftTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERLEFT];
1017  int rightTriggerMapping = gamecontroller->mapping.axes[SDL_CONTROLLER_AXIS_TRIGGERRIGHT];
1018  if (leftTriggerMapping >= 0) {
1019  gamecontroller->joystick->axes[leftTriggerMapping] =
1020  gamecontroller->joystick->axes_zero[leftTriggerMapping] = (Sint16)-32768;
1021  }
1022  if (rightTriggerMapping >= 0) {
1023  gamecontroller->joystick->axes[rightTriggerMapping] =
1024  gamecontroller->joystick->axes_zero[rightTriggerMapping] = (Sint16)-32768;
1025  }
1026  }
1027 
1028  /* Add joystick to list */
1029  ++gamecontroller->ref_count;
1030  /* Link the joystick in the list */
1031  gamecontroller->next = SDL_gamecontrollers;
1032  SDL_gamecontrollers = gamecontroller;
1033 
1034  SDL_SYS_JoystickUpdate(gamecontroller->joystick);
1035 
1036  return (gamecontroller);
1037 }
#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
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147

◆ SDL_GameControllerUpdate()

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 1043 of file SDL_gamecontroller.c.

References SDL_JoystickUpdate.

1044 {
1045  /* Just for API completeness; the joystick API does all the work. */
1047 }
#define SDL_JoystickUpdate

◆ SDL_IsGameController()

SDL_bool SDL_IsGameController ( int  joystick_index)

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

Definition at line 951 of file SDL_gamecontroller.c.

References SDL_FALSE, SDL_PrivateGetControllerMapping(), and SDL_TRUE.

Referenced by SDL_GameControllerEventWatcher(), and SDL_GameControllerInit().

952 {
953  ControllerMapping_t *pSupportedController = SDL_PrivateGetControllerMapping(device_index);
954  if (pSupportedController) {
955  return SDL_TRUE;
956  }
957 
958  return SDL_FALSE;
959 }
ControllerMapping_t * SDL_PrivateGetControllerMapping(int device_index)