SDL  2.0
SDL_gamecontroller.c File Reference
#include "../SDL_internal.h"
#include "SDL_events.h"
#include "SDL_assert.h"
#include "SDL_sysjoystick.h"
#include "SDL_hints.h"
#include "SDL_gamecontrollerdb.h"
#include "../events/SDL_events_c.h"
+ Include dependency graph for SDL_gamecontroller.c:

Go to the source code of this file.

Data Structures

struct  _SDL_HatMapping
 
struct  _SDL_ControllerMapping
 
struct  ControllerMapping_t
 
struct  SDL_GameController
 

Macros

#define ABS(_x)   ((_x) < 0 ? -(_x) : (_x))
 
#define SDL_CONTROLLER_PLATFORM_FIELD   "platform:"
 
#define k_nMaxReverseEntries   48
 
#define k_nMaxHatEntries   0x3f + 1
 

Functions

int SDL_PrivateGameControllerAxis (SDL_GameController *gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
 
int SDL_PrivateGameControllerButton (SDL_GameController *gamecontroller, SDL_GameControllerButton button, Uint8 state)
 
static void UpdateEventsForDeviceRemoval ()
 
int SDL_GameControllerEventWatcher (void *userdata, SDL_Event *event)
 
ControllerMapping_tSDL_PrivateGetControllerMappingForGUID (SDL_JoystickGUID *guid)
 
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString (const char *pchString)
 
const char * SDL_GameControllerGetStringForAxis (SDL_GameControllerAxis axis)
 
SDL_GameControllerButton SDL_GameControllerGetButtonFromString (const char *pchString)
 
const char * SDL_GameControllerGetStringForButton (SDL_GameControllerButton axis)
 
void SDL_PrivateGameControllerParseButton (const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
 
static void SDL_PrivateGameControllerParseControllerConfigString (struct _SDL_ControllerMapping *pMapping, const char *pchString)
 
void SDL_PrivateLoadButtonMapping (struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
 
char * SDL_PrivateGetControllerGUIDFromMappingString (const char *pMapping)
 
char * SDL_PrivateGetControllerNameFromMappingString (const char *pMapping)
 
char * SDL_PrivateGetControllerMappingFromMappingString (const char *pMapping)
 
void SDL_PrivateGameControllerRefreshMapping (ControllerMapping_t *pControllerMapping)
 
static ControllerMapping_tSDL_PrivateAddMappingForGUID (SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing)
 
ControllerMapping_tSDL_PrivateGetControllerMapping (int device_index)
 
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)
 
static void SDL_GameControllerLoadHints ()
 
int SDL_GameControllerInit (void)
 
const char * SDL_GameControllerNameForIndex (int device_index)
 
SDL_bool SDL_IsGameController (int device_index)
 
SDL_GameController * SDL_GameControllerOpen (int device_index)
 
void SDL_GameControllerUpdate (void)
 
Sint16 SDL_GameControllerGetAxis (SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
 
Uint8 SDL_GameControllerGetButton (SDL_GameController *gamecontroller, SDL_GameControllerButton button)
 
SDL_bool SDL_GameControllerGetAttached (SDL_GameController *gamecontroller)
 
const char * SDL_GameControllerName (SDL_GameController *gamecontroller)
 
SDL_Joystick * SDL_GameControllerGetJoystick (SDL_GameController *gamecontroller)
 
SDL_GameController * SDL_GameControllerFromInstanceID (SDL_JoystickID joyid)
 
SDL_GameControllerButtonBind SDL_GameControllerGetBindForAxis (SDL_GameController *gamecontroller, SDL_GameControllerAxis axis)
 
SDL_GameControllerButtonBind SDL_GameControllerGetBindForButton (SDL_GameController *gamecontroller, SDL_GameControllerButton button)
 
void SDL_GameControllerClose (SDL_GameController *gamecontroller)
 
void SDL_GameControllerQuit (void)
 
int SDL_GameControllerEventState (int state)
 

Variables

static SDL_GameController * SDL_gamecontrollers = NULL
 
static ControllerMapping_ts_pSupportedControllers = NULL
 
static ControllerMapping_ts_pXInputMapping = NULL
 
static ControllerMapping_ts_pEmscriptenMapping = NULL
 
static const char * map_StringForControllerAxis []
 
static const char * map_StringForControllerButton []
 

Macro Definition Documentation

§ ABS

§ k_nMaxHatEntries

#define k_nMaxHatEntries   0x3f + 1

We are encoding the "HAT" as 0xhm. where h == hat ID and m == mask MAX 4 hats supported

Definition at line 55 of file SDL_gamecontroller.c.

Referenced by SDL_PrivateLoadButtonMapping().

§ k_nMaxReverseEntries

#define k_nMaxReverseEntries   48

§ SDL_CONTROLLER_PLATFORM_FIELD

#define SDL_CONTROLLER_PLATFORM_FIELD   "platform:"

Definition at line 36 of file SDL_gamecontroller.c.

Referenced by SDL_GameControllerAddMappingsFromRW().

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:159
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_GameControllerEventWatcher()

int SDL_GameControllerEventWatcher ( void userdata,
SDL_Event event 
)

Definition at line 141 of file SDL_gamecontroller.c.

References ABS, axis, SDL_JoyAxisEvent::axis, SDL_JoyButtonEvent::button, SDL_Event::cdevice, SDL_JoyHatEvent::hat, SDL_Event::jaxis, SDL_Event::jbutton, SDL_Event::jdevice, SDL_Event::jhat, k_nMaxReverseEntries, SDL_CONTROLLER_AXIS_TRIGGERLEFT, SDL_CONTROLLER_AXIS_TRIGGERRIGHT, SDL_CONTROLLERDEVICEADDED, SDL_CONTROLLERDEVICEREMOVED, SDL_gamecontrollers, SDL_HAT_DOWN, SDL_HAT_LEFT, SDL_HAT_RIGHT, SDL_HAT_UP, SDL_IsGameController(), SDL_JOYAXISMOTION, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED, SDL_JOYHATMOTION, SDL_PRESSED, SDL_PrivateGameControllerAxis(), SDL_PrivateGameControllerButton(), SDL_PushEvent, SDL_RELEASED, SDL_SetError, SDL_JoyButtonEvent::state, SDL_Event::type, UpdateEventsForDeviceRemoval(), SDL_JoyAxisEvent::value, SDL_JoyAxisEvent::which, SDL_JoyHatEvent::which, SDL_JoyButtonEvent::which, SDL_JoyDeviceEvent::which, and SDL_ControllerDeviceEvent::which.

Referenced by SDL_GameControllerInit(), and SDL_GameControllerQuit().

142 {
143  switch(event->type) {
144  case SDL_JOYAXISMOTION:
145  {
146  SDL_GameController *controllerlist;
147 
148  if (event->jaxis.axis >= k_nMaxReverseEntries)
149  {
150  SDL_SetError("SDL_GameControllerEventWatcher: Axis index %d too large, ignoring motion", (int)event->jaxis.axis);
151  break;
152  }
153 
154  controllerlist = SDL_gamecontrollers;
155  while (controllerlist) {
156  if (controllerlist->joystick->instance_id == event->jaxis.which) {
157  if (controllerlist->mapping.raxes[event->jaxis.axis] >= 0) /* simple axis to axis, send it through */ {
158  SDL_GameControllerAxis axis = controllerlist->mapping.raxes[event->jaxis.axis];
159  Sint16 value = event->jaxis.value;
160  switch (axis) {
163  value = value / 2 + 16384;
164  break;
165  default:
166  break;
167  }
168  SDL_PrivateGameControllerAxis(controllerlist, axis, value);
169  } else if (controllerlist->mapping.raxesasbutton[event->jaxis.axis] >= 0) { /* simulate an axis as a button */
170  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.raxesasbutton[event->jaxis.axis], ABS(event->jaxis.value) > 32768/2 ? SDL_PRESSED : SDL_RELEASED);
171  }
172  break;
173  }
174  controllerlist = controllerlist->next;
175  }
176  }
177  break;
178  case SDL_JOYBUTTONDOWN:
179  case SDL_JOYBUTTONUP:
180  {
181  SDL_GameController *controllerlist;
182 
183  if (event->jbutton.button >= k_nMaxReverseEntries)
184  {
185  SDL_SetError("SDL_GameControllerEventWatcher: Button index %d too large, ignoring update", (int)event->jbutton.button);
186  break;
187  }
188 
189  controllerlist = SDL_gamecontrollers;
190  while (controllerlist) {
191  if (controllerlist->joystick->instance_id == event->jbutton.which) {
192  if (controllerlist->mapping.rbuttons[event->jbutton.button] >= 0) { /* simple button as button */
193  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rbuttons[event->jbutton.button], event->jbutton.state);
194  } else if (controllerlist->mapping.rbuttonasaxis[event->jbutton.button] >= 0) { /* an button pretending to be an axis */
195  SDL_PrivateGameControllerAxis(controllerlist, controllerlist->mapping.rbuttonasaxis[event->jbutton.button], event->jbutton.state > 0 ? 32767 : 0);
196  }
197  break;
198  }
199  controllerlist = controllerlist->next;
200  }
201  }
202  break;
203  case SDL_JOYHATMOTION:
204  {
205  SDL_GameController *controllerlist;
206 
207  if (event->jhat.hat >= 4) break;
208 
209  controllerlist = SDL_gamecontrollers;
210  while (controllerlist) {
211  if (controllerlist->joystick->instance_id == event->jhat.which) {
212  Uint8 bSame = controllerlist->hatState[event->jhat.hat] & event->jhat.value;
213  /* Get list of removed bits (button release) */
214  Uint8 bChanged = controllerlist->hatState[event->jhat.hat] ^ bSame;
215  /* the hat idx in the high nibble */
216  int bHighHat = event->jhat.hat << 4;
217 
218  if (bChanged & SDL_HAT_DOWN)
219  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_RELEASED);
220  if (bChanged & SDL_HAT_UP)
221  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_RELEASED);
222  if (bChanged & SDL_HAT_LEFT)
223  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_RELEASED);
224  if (bChanged & SDL_HAT_RIGHT)
225  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_RELEASED);
226 
227  /* Get list of added bits (button press) */
228  bChanged = event->jhat.value ^ bSame;
229 
230  if (bChanged & SDL_HAT_DOWN)
231  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_DOWN], SDL_PRESSED);
232  if (bChanged & SDL_HAT_UP)
233  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_UP], SDL_PRESSED);
234  if (bChanged & SDL_HAT_LEFT)
235  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_LEFT], SDL_PRESSED);
236  if (bChanged & SDL_HAT_RIGHT)
237  SDL_PrivateGameControllerButton(controllerlist, controllerlist->mapping.rhatasbutton[bHighHat | SDL_HAT_RIGHT], SDL_PRESSED);
238 
239  /* update our state cache */
240  controllerlist->hatState[event->jhat.hat] = event->jhat.value;
241 
242  break;
243  }
244  controllerlist = controllerlist->next;
245  }
246  }
247  break;
248  case SDL_JOYDEVICEADDED:
249  {
250  if (SDL_IsGameController(event->jdevice.which)) {
251  SDL_Event deviceevent;
252  deviceevent.type = SDL_CONTROLLERDEVICEADDED;
253  deviceevent.cdevice.which = event->jdevice.which;
254  SDL_PushEvent(&deviceevent);
255  }
256  }
257  break;
259  {
260  SDL_GameController *controllerlist = SDL_gamecontrollers;
261  while (controllerlist) {
262  if (controllerlist->joystick->instance_id == event->jdevice.which) {
263  SDL_Event deviceevent;
264 
265  deviceevent.type = SDL_CONTROLLERDEVICEREMOVED;
266  deviceevent.cdevice.which = event->jdevice.which;
267  SDL_PushEvent(&deviceevent);
268 
270  break;
271  }
272  controllerlist = controllerlist->next;
273  }
274  }
275  break;
276  default:
277  break;
278  }
279 
280  return 1;
281 }
SDL_JoystickID which
Definition: SDL_events.h:335
SDL_JoyDeviceEvent jdevice
Definition: SDL_events.h:540
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:543
SDL_JoyButtonEvent jbutton
Definition: SDL_events.h:539
static void UpdateEventsForDeviceRemoval()
SDL_JoystickID which
Definition: SDL_events.h:283
int SDL_PrivateGameControllerButton(SDL_GameController *gamecontroller, SDL_GameControllerButton button, Uint8 state)
#define ABS(_x)
SDL_Texture * axis
int SDL_PrivateGameControllerAxis(SDL_GameController *gamecontroller, SDL_GameControllerAxis axis, Sint16 value)
#define SDL_HAT_RIGHT
Definition: SDL_joystick.h:209
#define SDL_HAT_LEFT
Definition: SDL_joystick.h:211
static SDL_GameController * SDL_gamecontrollers
GLsizei const GLfloat * value
SDL_JoyAxisEvent jaxis
Definition: SDL_events.h:536
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_PushEvent
SDL_bool SDL_IsGameController(int device_index)
#define SDL_SetError
General event structure.
Definition: SDL_events.h:525
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_JoyHatEvent jhat
Definition: SDL_events.h:538
#define SDL_RELEASED
Definition: SDL_events.h:49
#define SDL_HAT_UP
Definition: SDL_joystick.h:208
#define k_nMaxReverseEntries
#define SDL_HAT_DOWN
Definition: SDL_joystick.h:210
SDL_JoystickID which
Definition: SDL_events.h:315
SDL_GameControllerAxis
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147
Uint32 type
Definition: SDL_events.h:527

§ 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 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_GameControllerInit()

int SDL_GameControllerInit ( void  )

Definition at line 901 of file SDL_gamecontroller.c.

References SDL_Event::cdevice, i, NULL, s_ControllerMappings, SDL_AddEventWatch, SDL_CONTROLLERDEVICEADDED, SDL_GameControllerAddMapping(), SDL_GameControllerEventWatcher(), SDL_GameControllerLoadHints(), SDL_IsGameController(), SDL_NumJoysticks, SDL_PushEvent, SDL_Event::type, and SDL_ControllerDeviceEvent::which.

Referenced by SDL_InitSubSystem().

902 {
903  int i = 0;
904  const char *pMappingString = NULL;
905  pMappingString = s_ControllerMappings[i];
906  while (pMappingString) {
907  SDL_GameControllerAddMapping(pMappingString);
908 
909  i++;
910  pMappingString = s_ControllerMappings[i];
911  }
912 
913  /* load in any user supplied config */
915 
916  /* watch for joy events and fire controller ones if needed */
918 
919  /* Send added events for controllers currently attached */
920  for (i = 0; i < SDL_NumJoysticks(); ++i) {
921  if (SDL_IsGameController(i)) {
922  SDL_Event deviceevent;
923  deviceevent.type = SDL_CONTROLLERDEVICEADDED;
924  deviceevent.cdevice.which = i;
925  SDL_PushEvent(&deviceevent);
926  }
927  }
928 
929  return (0);
930 }
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:543
#define SDL_NumJoysticks
static void SDL_GameControllerLoadHints()
int SDL_GameControllerAddMapping(const char *mappingString)
#define SDL_PushEvent
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
SDL_bool SDL_IsGameController(int device_index)
#define NULL
Definition: begin_code.h:143
static const char * s_ControllerMappings[]
#define SDL_AddEventWatch
General event structure.
Definition: SDL_events.h:525
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
Uint32 type
Definition: SDL_events.h:527

§ SDL_GameControllerLoadHints()

static void SDL_GameControllerLoadHints ( )
static

Definition at line 869 of file SDL_gamecontroller.c.

References NULL, SDL_free(), SDL_GameControllerAddMapping(), SDL_GetHint, SDL_HINT_GAMECONTROLLERCONFIG, SDL_malloc, SDL_memcpy, SDL_strchr, and SDL_strlen.

Referenced by SDL_GameControllerInit().

870 {
871  const char *hint = SDL_GetHint(SDL_HINT_GAMECONTROLLERCONFIG);
872  if (hint && hint[0]) {
873  size_t nchHints = SDL_strlen(hint);
874  char *pUserMappings = SDL_malloc(nchHints + 1);
875  char *pTempMappings = pUserMappings;
876  SDL_memcpy(pUserMappings, hint, nchHints);
877  pUserMappings[nchHints] = '\0';
878  while (pUserMappings) {
879  char *pchNewLine = NULL;
880 
881  pchNewLine = SDL_strchr(pUserMappings, '\n');
882  if (pchNewLine)
883  *pchNewLine = '\0';
884 
885  SDL_GameControllerAddMapping(pUserMappings);
886 
887  if (pchNewLine) {
888  pUserMappings = pchNewLine + 1;
889  } else {
890  pUserMappings = NULL;
891  }
892  }
893  SDL_free(pTempMappings);
894  }
895 }
#define SDL_GetHint
#define SDL_strchr
#define SDL_memcpy
int SDL_GameControllerAddMapping(const char *mappingString)
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
#define SDL_strlen
#define SDL_HINT_GAMECONTROLLERCONFIG
A variable that lets you manually hint extra gamecontroller db entries.
Definition: SDL_hints.h:359
#define SDL_malloc

§ 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_GameControllerQuit()

void SDL_GameControllerQuit ( void  )

Definition at line 1254 of file SDL_gamecontroller.c.

References ControllerMapping_t::mapping, ControllerMapping_t::name, ControllerMapping_t::next, NULL, s_pSupportedControllers, SDL_DelEventWatch, SDL_free(), SDL_GameControllerClose(), SDL_GameControllerEventWatcher(), and SDL_gamecontrollers.

Referenced by SDL_QuitSubSystem().

1255 {
1256  ControllerMapping_t *pControllerMap;
1257  while (SDL_gamecontrollers) {
1258  SDL_gamecontrollers->ref_count = 1;
1260  }
1261 
1262  while (s_pSupportedControllers) {
1263  pControllerMap = s_pSupportedControllers;
1265  SDL_free(pControllerMap->name);
1266  SDL_free(pControllerMap->mapping);
1267  SDL_free(pControllerMap);
1268  }
1269 
1271 
1272 }
#define SDL_DelEventWatch
struct _ControllerMapping_t * next
void SDL_GameControllerClose(SDL_GameController *gamecontroller)
static SDL_GameController * SDL_gamecontrollers
static ControllerMapping_t * s_pSupportedControllers
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)

§ 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)

§ SDL_PrivateAddMappingForGUID()

static ControllerMapping_t* SDL_PrivateAddMappingForGUID ( SDL_JoystickGUID  jGUID,
const char *  mappingString,
SDL_bool existing 
)
static

Definition at line 620 of file SDL_gamecontroller.c.

References ControllerMapping_t::guid, ControllerMapping_t::mapping, ControllerMapping_t::name, ControllerMapping_t::next, NULL, s_pSupportedControllers, SDL_FALSE, SDL_free(), SDL_malloc, SDL_OutOfMemory, SDL_PrivateGameControllerRefreshMapping(), SDL_PrivateGetControllerMappingForGUID(), SDL_PrivateGetControllerMappingFromMappingString(), SDL_PrivateGetControllerNameFromMappingString(), SDL_SetError, and SDL_TRUE.

Referenced by SDL_GameControllerAddMapping(), and SDL_PrivateGetControllerMapping().

621 {
622  char *pchName;
623  char *pchMapping;
624  ControllerMapping_t *pControllerMapping;
625 
626  pchName = SDL_PrivateGetControllerNameFromMappingString(mappingString);
627  if (!pchName) {
628  SDL_SetError("Couldn't parse name from %s", mappingString);
629  return NULL;
630  }
631 
632  pchMapping = SDL_PrivateGetControllerMappingFromMappingString(mappingString);
633  if (!pchMapping) {
634  SDL_free(pchName);
635  SDL_SetError("Couldn't parse %s", mappingString);
636  return NULL;
637  }
638 
639  pControllerMapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
640  if (pControllerMapping) {
641  /* Update existing mapping */
642  SDL_free(pControllerMapping->name);
643  pControllerMapping->name = pchName;
644  SDL_free(pControllerMapping->mapping);
645  pControllerMapping->mapping = pchMapping;
646  /* refresh open controllers */
647  SDL_PrivateGameControllerRefreshMapping(pControllerMapping);
648  *existing = SDL_TRUE;
649  } else {
650  pControllerMapping = SDL_malloc(sizeof(*pControllerMapping));
651  if (!pControllerMapping) {
652  SDL_free(pchName);
653  SDL_free(pchMapping);
654  SDL_OutOfMemory();
655  return NULL;
656  }
657  pControllerMapping->guid = jGUID;
658  pControllerMapping->name = pchName;
659  pControllerMapping->mapping = pchMapping;
660  pControllerMapping->next = s_pSupportedControllers;
661  s_pSupportedControllers = pControllerMapping;
662  *existing = SDL_FALSE;
663  }
664  return pControllerMapping;
665 }
char * SDL_PrivateGetControllerMappingFromMappingString(const char *pMapping)
struct _ControllerMapping_t * next
ControllerMapping_t * SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
SDL_JoystickGUID guid
static ControllerMapping_t * s_pSupportedControllers
void SDL_free(void *mem)
void SDL_PrivateGameControllerRefreshMapping(ControllerMapping_t *pControllerMapping)
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
char * SDL_PrivateGetControllerNameFromMappingString(const char *pMapping)
#define SDL_malloc

§ SDL_PrivateGameControllerAxis()

int SDL_PrivateGameControllerAxis ( SDL_GameController *  gamecontroller,
SDL_GameControllerAxis  axis,
Sint16  value 
)

Definition at line 1278 of file SDL_gamecontroller.c.

References axis, SDL_CONTROLLERAXISMOTION, SDL_ENABLE, SDL_GetEventState, and SDL_PushEvent.

Referenced by SDL_GameControllerEventWatcher().

1279 {
1280  int posted;
1281 
1282  /* translate the event, if desired */
1283  posted = 0;
1284 #if !SDL_EVENTS_DISABLED
1286  SDL_Event event;
1287  event.type = SDL_CONTROLLERAXISMOTION;
1288  event.caxis.which = gamecontroller->joystick->instance_id;
1289  event.caxis.axis = axis;
1290  event.caxis.value = value;
1291  posted = SDL_PushEvent(&event) == 1;
1292  }
1293 #endif /* !SDL_EVENTS_DISABLED */
1294  return (posted);
1295 }
#define SDL_ENABLE
Definition: SDL_events.h:722
SDL_Texture * axis
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525

§ SDL_PrivateGameControllerButton()

int SDL_PrivateGameControllerButton ( SDL_GameController *  gamecontroller,
SDL_GameControllerButton  button,
Uint8  state 
)

Definition at line 1302 of file SDL_gamecontroller.c.

References button, SDL_CONTROLLER_BUTTON_INVALID, SDL_CONTROLLERBUTTONDOWN, SDL_CONTROLLERBUTTONUP, SDL_ENABLE, SDL_GetEventState, SDL_PRESSED, SDL_PushEvent, SDL_RELEASED, state, and SDL_Event::type.

Referenced by SDL_GameControllerEventWatcher().

1303 {
1304  int posted;
1305 #if !SDL_EVENTS_DISABLED
1306  SDL_Event event;
1307 
1309  return (0);
1310 
1311  switch (state) {
1312  case SDL_PRESSED:
1313  event.type = SDL_CONTROLLERBUTTONDOWN;
1314  break;
1315  case SDL_RELEASED:
1316  event.type = SDL_CONTROLLERBUTTONUP;
1317  break;
1318  default:
1319  /* Invalid state -- bail */
1320  return (0);
1321  }
1322 #endif /* !SDL_EVENTS_DISABLED */
1323 
1324  /* translate the event, if desired */
1325  posted = 0;
1326 #if !SDL_EVENTS_DISABLED
1327  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
1328  event.cbutton.which = gamecontroller->joystick->instance_id;
1329  event.cbutton.button = button;
1330  event.cbutton.state = state;
1331  posted = SDL_PushEvent(&event) == 1;
1332  }
1333 #endif /* !SDL_EVENTS_DISABLED */
1334  return (posted);
1335 }
SDL_Texture * button
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_RELEASED
Definition: SDL_events.h:49
Uint32 type
Definition: SDL_events.h:527

§ SDL_PrivateGameControllerParseButton()

void SDL_PrivateGameControllerParseButton ( const char *  szGameButton,
const char *  szJoystickButton,
struct _SDL_ControllerMapping pMapping 
)

Definition at line 384 of file SDL_gamecontroller.c.

References _SDL_ControllerMapping::axes, _SDL_ControllerMapping::axesasbutton, axis, button, _SDL_ControllerMapping::buttonasaxis, _SDL_ControllerMapping::buttons, _SDL_HatMapping::hat, _SDL_ControllerMapping::hatasbutton, k_nMaxReverseEntries, _SDL_HatMapping::mask, _SDL_ControllerMapping::raxes, _SDL_ControllerMapping::raxesasbutton, _SDL_ControllerMapping::rbuttonasaxis, _SDL_ControllerMapping::rbuttons, _SDL_ControllerMapping::rhatasbutton, SDL_assert, SDL_atoi, SDL_CONTROLLER_AXIS_INVALID, SDL_CONTROLLER_BUTTON_INVALID, SDL_GameControllerGetAxisFromString(), SDL_GameControllerGetButtonFromString(), and SDL_SetError.

Referenced by SDL_PrivateGameControllerParseControllerConfigString().

385 {
386  int iSDLButton = 0;
389  button = SDL_GameControllerGetButtonFromString(szGameButton);
390  axis = SDL_GameControllerGetAxisFromString(szGameButton);
391  iSDLButton = SDL_atoi(&szJoystickButton[1]);
392 
393  if (szJoystickButton[0] == 'a') {
394  if (iSDLButton >= k_nMaxReverseEntries) {
395  SDL_SetError("Axis index too large: %d", iSDLButton);
396  return;
397  }
398  if (axis != SDL_CONTROLLER_AXIS_INVALID) {
399  pMapping->axes[ axis ] = iSDLButton;
400  pMapping->raxes[ iSDLButton ] = axis;
401  } else if (button != SDL_CONTROLLER_BUTTON_INVALID) {
402  pMapping->axesasbutton[ button ] = iSDLButton;
403  pMapping->raxesasbutton[ iSDLButton ] = button;
404  } else {
405  SDL_assert(!"How did we get here?");
406  }
407 
408  } else if (szJoystickButton[0] == 'b') {
409  if (iSDLButton >= k_nMaxReverseEntries) {
410  SDL_SetError("Button index too large: %d", iSDLButton);
411  return;
412  }
413  if (button != SDL_CONTROLLER_BUTTON_INVALID) {
414  pMapping->buttons[ button ] = iSDLButton;
415  pMapping->rbuttons[ iSDLButton ] = button;
416  } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
417  pMapping->buttonasaxis[ axis ] = iSDLButton;
418  pMapping->rbuttonasaxis[ iSDLButton ] = axis;
419  } else {
420  SDL_assert(!"How did we get here?");
421  }
422  } else if (szJoystickButton[0] == 'h') {
423  int hat = SDL_atoi(&szJoystickButton[1]);
424  int mask = SDL_atoi(&szJoystickButton[3]);
425  if (hat >= 4) {
426  SDL_SetError("Hat index too large: %d", iSDLButton);
427  }
428 
429  if (button != SDL_CONTROLLER_BUTTON_INVALID) {
430  int ridx;
431  pMapping->hatasbutton[ button ].hat = hat;
432  pMapping->hatasbutton[ button ].mask = mask;
433  ridx = (hat << 4) | mask;
434  pMapping->rhatasbutton[ ridx ] = button;
435  } else if (axis != SDL_CONTROLLER_AXIS_INVALID) {
436  SDL_assert(!"Support hat as axis");
437  } else {
438  SDL_assert(!"How did we get here?");
439  }
440  }
441 }
struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX]
int buttons[SDL_CONTROLLER_BUTTON_MAX]
SDL_GameControllerButton SDL_GameControllerGetButtonFromString(const char *pchString)
SDL_Texture * button
SDL_GameControllerAxis SDL_GameControllerGetAxisFromString(const char *pchString)
SDL_GameControllerAxis raxes[k_nMaxReverseEntries]
SDL_GameControllerButton rbuttons[k_nMaxReverseEntries]
SDL_Texture * axis
SDL_GameControllerButton
SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries]
int buttonasaxis[SDL_CONTROLLER_AXIS_MAX]
SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries]
SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries]
#define SDL_atoi
GLenum GLint GLuint mask
#define SDL_assert(condition)
Definition: SDL_assert.h:167
#define SDL_SetError
int axes[SDL_CONTROLLER_AXIS_MAX]
int axesasbutton[SDL_CONTROLLER_BUTTON_MAX]
#define k_nMaxReverseEntries
SDL_GameControllerAxis

§ SDL_PrivateGameControllerParseControllerConfigString()

static void SDL_PrivateGameControllerParseControllerConfigString ( struct _SDL_ControllerMapping pMapping,
const char *  pchString 
)
static

Definition at line 448 of file SDL_gamecontroller.c.

References i, SDL_FALSE, SDL_memset, SDL_PrivateGameControllerParseButton(), SDL_SetError, and SDL_TRUE.

Referenced by SDL_PrivateLoadButtonMapping().

449 {
450  char szGameButton[20];
451  char szJoystickButton[20];
452  SDL_bool bGameButton = SDL_TRUE;
453  int i = 0;
454  const char *pchPos = pchString;
455 
456  SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
457  SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
458 
459  while (pchPos && *pchPos) {
460  if (*pchPos == ':') {
461  i = 0;
462  bGameButton = SDL_FALSE;
463  } else if (*pchPos == ' ') {
464 
465  } else if (*pchPos == ',') {
466  i = 0;
467  bGameButton = SDL_TRUE;
468  SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
469  SDL_memset(szGameButton, 0x0, sizeof(szGameButton));
470  SDL_memset(szJoystickButton, 0x0, sizeof(szJoystickButton));
471 
472  } else if (bGameButton) {
473  if (i >= sizeof(szGameButton)) {
474  SDL_SetError("Button name too large: %s", szGameButton);
475  return;
476  }
477  szGameButton[i] = *pchPos;
478  i++;
479  } else {
480  if (i >= sizeof(szJoystickButton)) {
481  SDL_SetError("Joystick button name too large: %s", szJoystickButton);
482  return;
483  }
484  szJoystickButton[i] = *pchPos;
485  i++;
486  }
487  pchPos++;
488  }
489 
490  SDL_PrivateGameControllerParseButton(szGameButton, szJoystickButton, pMapping);
491 
492 }
void SDL_PrivateGameControllerParseButton(const char *szGameButton, const char *szJoystickButton, struct _SDL_ControllerMapping *pMapping)
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
SDL_bool
Definition: SDL_stdinc.h:130
#define SDL_SetError
GLuint GLfloat x0
#define SDL_memset

§ SDL_PrivateGameControllerRefreshMapping()

void SDL_PrivateGameControllerRefreshMapping ( ControllerMapping_t pControllerMapping)

Definition at line 598 of file SDL_gamecontroller.c.

References ControllerMapping_t::guid, ControllerMapping_t::mapping, ControllerMapping_t::name, SDL_CONTROLLERDEVICEREMAPPED, SDL_gamecontrollers, SDL_memcmp, SDL_PrivateLoadButtonMapping(), and SDL_PushEvent.

Referenced by SDL_PrivateAddMappingForGUID().

599 {
600  SDL_GameController *gamecontrollerlist = SDL_gamecontrollers;
601  while (gamecontrollerlist) {
602  if (!SDL_memcmp(&gamecontrollerlist->mapping.guid, &pControllerMapping->guid, sizeof(pControllerMapping->guid))) {
604  event.type = SDL_CONTROLLERDEVICEREMAPPED;
605  event.cdevice.which = gamecontrollerlist->joystick->instance_id;
606  SDL_PushEvent(&event);
607 
608  /* Not really threadsafe. Should this lock access within SDL_GameControllerEventWatcher? */
609  SDL_PrivateLoadButtonMapping(&gamecontrollerlist->mapping, pControllerMapping->guid, pControllerMapping->name, pControllerMapping->mapping);
610  }
611 
612  gamecontrollerlist = gamecontrollerlist->next;
613  }
614 }
SDL_JoystickGUID guid
static SDL_GameController * SDL_gamecontrollers
struct _cl_event * event
#define SDL_PushEvent
#define SDL_memcmp
void SDL_PrivateLoadButtonMapping(struct _SDL_ControllerMapping *pMapping, SDL_JoystickGUID guid, const char *pchName, const char *pchMapping)
General event structure.
Definition: SDL_events.h:525

§ SDL_PrivateGetControllerGUIDFromMappingString()

char* SDL_PrivateGetControllerGUIDFromMappingString ( const char *  pMapping)

Definition at line 533 of file SDL_gamecontroller.c.

References NULL, SDL_malloc, SDL_memcpy, SDL_OutOfMemory, and SDL_strchr.

Referenced by SDL_GameControllerAddMapping().

534 {
535  const char *pFirstComma = SDL_strchr(pMapping, ',');
536  if (pFirstComma) {
537  char *pchGUID = SDL_malloc(pFirstComma - pMapping + 1);
538  if (!pchGUID) {
539  SDL_OutOfMemory();
540  return NULL;
541  }
542  SDL_memcpy(pchGUID, pMapping, pFirstComma - pMapping);
543  pchGUID[ pFirstComma - pMapping ] = 0;
544  return pchGUID;
545  }
546  return NULL;
547 }
#define SDL_strchr
#define SDL_memcpy
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_malloc

§ SDL_PrivateGetControllerMapping()

ControllerMapping_t* SDL_PrivateGetControllerMapping ( int  device_index)

Definition at line 670 of file SDL_gamecontroller.c.

References s_pEmscriptenMapping, s_pXInputMapping, SDL_JoystickGetDeviceGUID, SDL_JoystickNameForIndex, SDL_PrivateAddMappingForGUID(), SDL_PrivateGetControllerMappingForGUID(), and SDL_strstr.

Referenced by SDL_GameControllerNameForIndex(), SDL_GameControllerOpen(), and SDL_IsGameController().

671 {
672  SDL_JoystickGUID jGUID = SDL_JoystickGetDeviceGUID(device_index);
674 
675  mapping = SDL_PrivateGetControllerMappingForGUID(&jGUID);
676 #if SDL_JOYSTICK_XINPUT
677  if (!mapping && SDL_SYS_IsXInputGamepad_DeviceIndex(device_index)) {
678  mapping = s_pXInputMapping;
679  }
680 #endif
681 #if defined(SDL_JOYSTICK_EMSCRIPTEN)
682  if (!mapping && s_pEmscriptenMapping) {
683  mapping = s_pEmscriptenMapping;
684  }
685 #endif
686 #ifdef __LINUX__
687  if (!mapping) {
688  const char *name = SDL_JoystickNameForIndex(device_index);
689  if (name) {
690  if (SDL_strstr(name, "Xbox 360 Wireless Receiver")) {
691  /* The Linux driver xpad.c maps the wireless dpad to buttons */
692  SDL_bool existing;
693  mapping = SDL_PrivateAddMappingForGUID(jGUID,
694 "none,X360 Wireless Controller,a:b0,b:b1,back:b6,dpdown:b14,dpleft:b11,dpright:b12,dpup:b13,guide:b8,leftshoulder:b4,leftstick:b9,lefttrigger:a2,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b10,righttrigger:a5,rightx:a3,righty:a4,start:b7,x:b2,y:b3,",
695  &existing);
696  }
697  }
698  }
699 #endif /* __LINUX__ */
700 
701  if (!mapping) {
702  const char *name = SDL_JoystickNameForIndex(device_index);
703  if (name) {
704  if (SDL_strstr(name, "Xbox") || SDL_strstr(name, "X-Box")) {
705  mapping = s_pXInputMapping;
706  }
707  }
708  }
709  return mapping;
710 }
static ControllerMapping_t * SDL_PrivateAddMappingForGUID(SDL_JoystickGUID jGUID, const char *mappingString, SDL_bool *existing)
ControllerMapping_t * SDL_PrivateGetControllerMappingForGUID(SDL_JoystickGUID *guid)
#define SDL_JoystickNameForIndex
GLuint const GLchar * name
static ControllerMapping_t * s_pEmscriptenMapping
#define SDL_JoystickGetDeviceGUID
SDL_bool
Definition: SDL_stdinc.h:130
static ControllerMapping_t * s_pXInputMapping
GLenum GLenum GLenum GLenum mapping
#define SDL_strstr

§ SDL_PrivateGetControllerMappingForGUID()

ControllerMapping_t* SDL_PrivateGetControllerMappingForGUID ( SDL_JoystickGUID guid)

Definition at line 286 of file SDL_gamecontroller.c.

References ControllerMapping_t::guid, ControllerMapping_t::next, NULL, s_pSupportedControllers, and SDL_memcmp.

Referenced by SDL_GameControllerMappingForGUID(), SDL_PrivateAddMappingForGUID(), and SDL_PrivateGetControllerMapping().

287 {
288  ControllerMapping_t *pSupportedController = s_pSupportedControllers;
289  while (pSupportedController) {
290  if (SDL_memcmp(guid, &pSupportedController->guid, sizeof(*guid)) == 0) {
291  return pSupportedController;
292  }
293  pSupportedController = pSupportedController->next;
294  }
295  return NULL;
296 }
struct _ControllerMapping_t * next
SDL_JoystickGUID guid
static ControllerMapping_t * s_pSupportedControllers
#define SDL_memcmp
#define NULL
Definition: begin_code.h:143

§ SDL_PrivateGetControllerMappingFromMappingString()

char* SDL_PrivateGetControllerMappingFromMappingString ( const char *  pMapping)

Definition at line 580 of file SDL_gamecontroller.c.

References NULL, SDL_strchr, and SDL_strdup.

Referenced by SDL_PrivateAddMappingForGUID().

581 {
582  const char *pFirstComma, *pSecondComma;
583 
584  pFirstComma = SDL_strchr(pMapping, ',');
585  if (!pFirstComma)
586  return NULL;
587 
588  pSecondComma = SDL_strchr(pFirstComma + 1, ',');
589  if (!pSecondComma)
590  return NULL;
591 
592  return SDL_strdup(pSecondComma + 1); /* mapping is everything after the 3rd comma */
593 }
#define SDL_strchr
#define NULL
Definition: begin_code.h:143
#define SDL_strdup

§ SDL_PrivateGetControllerNameFromMappingString()

char* SDL_PrivateGetControllerNameFromMappingString ( const char *  pMapping)

Definition at line 553 of file SDL_gamecontroller.c.

References NULL, SDL_malloc, SDL_memcpy, SDL_OutOfMemory, and SDL_strchr.

Referenced by SDL_PrivateAddMappingForGUID().

554 {
555  const char *pFirstComma, *pSecondComma;
556  char *pchName;
557 
558  pFirstComma = SDL_strchr(pMapping, ',');
559  if (!pFirstComma)
560  return NULL;
561 
562  pSecondComma = SDL_strchr(pFirstComma + 1, ',');
563  if (!pSecondComma)
564  return NULL;
565 
566  pchName = SDL_malloc(pSecondComma - pFirstComma);
567  if (!pchName) {
568  SDL_OutOfMemory();
569  return NULL;
570  }
571  SDL_memcpy(pchName, pFirstComma + 1, pSecondComma - pFirstComma);
572  pchName[ pSecondComma - pFirstComma - 1 ] = 0;
573  return pchName;
574 }
#define SDL_strchr
#define SDL_memcpy
#define NULL
Definition: begin_code.h:143
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_malloc

§ SDL_PrivateLoadButtonMapping()

void SDL_PrivateLoadButtonMapping ( struct _SDL_ControllerMapping pMapping,
SDL_JoystickGUID  guid,
const char *  pchName,
const char *  pchMapping 
)

Definition at line 497 of file SDL_gamecontroller.c.

References _SDL_ControllerMapping::axes, _SDL_ControllerMapping::axesasbutton, _SDL_ControllerMapping::buttonasaxis, _SDL_ControllerMapping::buttons, _SDL_ControllerMapping::guid, _SDL_HatMapping::hat, _SDL_ControllerMapping::hatasbutton, j, k_nMaxHatEntries, k_nMaxReverseEntries, _SDL_ControllerMapping::name, _SDL_ControllerMapping::raxes, _SDL_ControllerMapping::raxesasbutton, _SDL_ControllerMapping::rbuttonasaxis, _SDL_ControllerMapping::rbuttons, _SDL_ControllerMapping::rhatasbutton, SDL_CONTROLLER_AXIS_INVALID, SDL_CONTROLLER_AXIS_MAX, SDL_CONTROLLER_BUTTON_INVALID, SDL_CONTROLLER_BUTTON_MAX, and SDL_PrivateGameControllerParseControllerConfigString().

Referenced by SDL_GameControllerOpen(), and SDL_PrivateGameControllerRefreshMapping().

498 {
499  int j;
500 
501  pMapping->guid = guid;
502  pMapping->name = pchName;
503 
504  /* set all the button mappings to non defaults */
505  for (j = 0; j < SDL_CONTROLLER_AXIS_MAX; j++) {
506  pMapping->axes[j] = -1;
507  pMapping->buttonasaxis[j] = -1;
508  }
509  for (j = 0; j < SDL_CONTROLLER_BUTTON_MAX; j++) {
510  pMapping->buttons[j] = -1;
511  pMapping->axesasbutton[j] = -1;
512  pMapping->hatasbutton[j].hat = -1;
513  }
514 
515  for (j = 0; j < k_nMaxReverseEntries; j++) {
516  pMapping->raxes[j] = SDL_CONTROLLER_AXIS_INVALID;
520  }
521 
522  for (j = 0; j < k_nMaxHatEntries; j++) {
524  }
525 
527 }
struct _SDL_HatMapping hatasbutton[SDL_CONTROLLER_BUTTON_MAX]
int buttons[SDL_CONTROLLER_BUTTON_MAX]
SDL_GameControllerAxis raxes[k_nMaxReverseEntries]
SDL_GameControllerButton rbuttons[k_nMaxReverseEntries]
#define k_nMaxHatEntries
SDL_GameControllerButton rhatasbutton[k_nMaxHatEntries]
int buttonasaxis[SDL_CONTROLLER_AXIS_MAX]
SDL_GameControllerButton raxesasbutton[k_nMaxReverseEntries]
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 int in j)
Definition: SDL_x11sym.h:50
SDL_GameControllerAxis rbuttonasaxis[k_nMaxReverseEntries]
int axes[SDL_CONTROLLER_AXIS_MAX]
int axesasbutton[SDL_CONTROLLER_BUTTON_MAX]
#define k_nMaxReverseEntries
static void SDL_PrivateGameControllerParseControllerConfigString(struct _SDL_ControllerMapping *pMapping, const char *pchString)

§ UpdateEventsForDeviceRemoval()

static void UpdateEventsForDeviceRemoval ( )
static

Definition at line 114 of file SDL_gamecontroller.c.

References SDL_Event::cdevice, events, i, NULL, SDL_ADDEVENT, SDL_CONTROLLERDEVICEADDED, SDL_GETEVENT, SDL_PEEKEVENT, SDL_PeepEvents, SDL_stack_alloc, SDL_stack_free, and SDL_ControllerDeviceEvent::which.

Referenced by SDL_GameControllerEventWatcher().

115 {
116  int i, num_events;
117  SDL_Event *events;
118 
120  if (num_events <= 0) {
121  return;
122  }
123 
124  events = SDL_stack_alloc(SDL_Event, num_events);
125  if (!events) {
126  return;
127  }
128 
130  for (i = 0; i < num_events; ++i) {
131  --events[i].cdevice.which;
132  }
133  SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
134 
135  SDL_stack_free(events);
136 }
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:543
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:36
#define SDL_PeepEvents
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:328
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 NULL
Definition: begin_code.h:143
General event structure.
Definition: SDL_events.h:525
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:329

Variable Documentation

§ map_StringForControllerAxis

const char* map_StringForControllerAxis[]
static
Initial value:
= {
"leftx",
"lefty",
"rightx",
"righty",
"lefttrigger",
"righttrigger",
}
#define NULL
Definition: begin_code.h:143

Definition at line 298 of file SDL_gamecontroller.c.

Referenced by SDL_GameControllerGetAxisFromString(), and SDL_GameControllerGetStringForAxis().

§ map_StringForControllerButton

const char* map_StringForControllerButton[]
static
Initial value:
= {
"a",
"b",
"x",
"y",
"back",
"guide",
"start",
"leftstick",
"rightstick",
"leftshoulder",
"rightshoulder",
"dpup",
"dpdown",
"dpleft",
"dpright",
}
#define NULL
Definition: begin_code.h:143

Definition at line 335 of file SDL_gamecontroller.c.

Referenced by SDL_GameControllerGetButtonFromString(), and SDL_GameControllerGetStringForButton().

§ s_pEmscriptenMapping

ControllerMapping_t* s_pEmscriptenMapping = NULL
static

Definition at line 93 of file SDL_gamecontroller.c.

Referenced by SDL_PrivateGetControllerMapping().

§ s_pSupportedControllers

§ s_pXInputMapping

ControllerMapping_t* s_pXInputMapping = NULL
static

Definition at line 92 of file SDL_gamecontroller.c.

Referenced by SDL_PrivateGetControllerMapping().

§ SDL_gamecontrollers