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

Go to the source code of this file.

Functions

static void SDL_JoystickAllowBackgroundEventsChanged (void *userdata, const char *name, const char *oldValue, const char *hint)
 
int SDL_JoystickInit (void)
 
int SDL_NumJoysticks (void)
 
const char * SDL_JoystickNameForIndex (int device_index)
 
SDL_Joystick * SDL_JoystickOpen (int device_index)
 
int SDL_PrivateJoystickValid (SDL_Joystick *joystick)
 
int SDL_JoystickNumAxes (SDL_Joystick *joystick)
 
int SDL_JoystickNumHats (SDL_Joystick *joystick)
 
int SDL_JoystickNumBalls (SDL_Joystick *joystick)
 
int SDL_JoystickNumButtons (SDL_Joystick *joystick)
 
Sint16 SDL_JoystickGetAxis (SDL_Joystick *joystick, int axis)
 
Uint8 SDL_JoystickGetHat (SDL_Joystick *joystick, int hat)
 
int SDL_JoystickGetBall (SDL_Joystick *joystick, int ball, int *dx, int *dy)
 
Uint8 SDL_JoystickGetButton (SDL_Joystick *joystick, int button)
 
SDL_bool SDL_JoystickGetAttached (SDL_Joystick *joystick)
 
SDL_JoystickID SDL_JoystickInstanceID (SDL_Joystick *joystick)
 
SDL_Joystick * SDL_JoystickFromInstanceID (SDL_JoystickID joyid)
 
const char * SDL_JoystickName (SDL_Joystick *joystick)
 
void SDL_JoystickClose (SDL_Joystick *joystick)
 
void SDL_JoystickQuit (void)
 
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent ()
 
void SDL_PrivateJoystickAdded (int device_index)
 
static void UpdateEventsForDeviceRemoval ()
 
void SDL_PrivateJoystickRemoved (SDL_JoystickID device_instance)
 
int SDL_PrivateJoystickAxis (SDL_Joystick *joystick, Uint8 axis, Sint16 value)
 
int SDL_PrivateJoystickHat (SDL_Joystick *joystick, Uint8 hat, Uint8 value)
 
int SDL_PrivateJoystickBall (SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
 
int SDL_PrivateJoystickButton (SDL_Joystick *joystick, Uint8 button, Uint8 state)
 
void SDL_JoystickUpdate (void)
 
int SDL_JoystickEventState (int state)
 
SDL_JoystickGUID SDL_JoystickGetDeviceGUID (int device_index)
 
SDL_JoystickGUID SDL_JoystickGetGUID (SDL_Joystick *joystick)
 
void SDL_JoystickGetGUIDString (SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
 
static unsigned char nibble (char c)
 
SDL_JoystickGUID SDL_JoystickGetGUIDFromString (const char *pchGUID)
 
void SDL_PrivateJoystickBatteryLevel (SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
 
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel (SDL_Joystick *joystick)
 

Variables

static SDL_bool SDL_joystick_allows_background_events = SDL_FALSE
 
static SDL_Joystick * SDL_joysticks = NULL
 
static SDL_Joystick * SDL_updating_joystick = NULL
 

Function Documentation

◆ nibble()

static unsigned char nibble ( char  c)
static

Definition at line 869 of file SDL_joystick.c.

Referenced by SDL_JoystickGetGUIDFromString().

870 {
871  if ((c >= '0') && (c <= '9')) {
872  return (unsigned char)(c - '0');
873  }
874 
875  if ((c >= 'A') && (c <= 'F')) {
876  return (unsigned char)(c - 'A' + 0x0a);
877  }
878 
879  if ((c >= 'a') && (c <= 'f')) {
880  return (unsigned char)(c - 'a' + 0x0a);
881  }
882 
883  /* received an invalid character, and no real way to return an error */
884  /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
885  return 0;
886 }
const GLubyte * c

◆ SDL_JoystickAllowBackgroundEventsChanged()

static void SDL_JoystickAllowBackgroundEventsChanged ( void userdata,
const char *  name,
const char *  oldValue,
const char *  hint 
)
static

Definition at line 40 of file SDL_joystick.c.

References SDL_FALSE, SDL_joystick_allows_background_events, and SDL_TRUE.

Referenced by SDL_JoystickInit().

41 {
42  if (hint && *hint == '1') {
44  } else {
46  }
47 }
static SDL_bool SDL_joystick_allows_background_events
Definition: SDL_joystick.c:35

◆ SDL_JoystickClose()

void SDL_JoystickClose ( SDL_Joystick *  joystick)

Close a joystick previously opened with SDL_JoystickOpen().

Definition at line 411 of file SDL_joystick.c.

References NULL, SDL_free(), SDL_joysticks, SDL_SYS_JoystickClose(), and SDL_updating_joystick.

Referenced by SDL_JoystickOpen(), SDL_JoystickQuit(), and SDL_JoystickUpdate().

412 {
413  SDL_Joystick *joysticklist;
414  SDL_Joystick *joysticklistprev;
415 
416  if (!joystick) {
417  return;
418  }
419 
420  /* First decrement ref count */
421  if (--joystick->ref_count > 0) {
422  return;
423  }
424 
425  if (joystick == SDL_updating_joystick) {
426  return;
427  }
428 
429  SDL_SYS_JoystickClose(joystick);
430  joystick->hwdata = NULL;
431 
432  joysticklist = SDL_joysticks;
433  joysticklistprev = NULL;
434  while (joysticklist) {
435  if (joystick == joysticklist) {
436  if (joysticklistprev) {
437  /* unlink this entry */
438  joysticklistprev->next = joysticklist->next;
439  } else {
440  SDL_joysticks = joystick->next;
441  }
442  break;
443  }
444  joysticklistprev = joysticklist;
445  joysticklist = joysticklist->next;
446  }
447 
448  SDL_free(joystick->name);
449 
450  /* Free the data associated with this joystick */
451  SDL_free(joystick->axes);
452  SDL_free(joystick->hats);
453  SDL_free(joystick->balls);
454  SDL_free(joystick->buttons);
455  SDL_free(joystick);
456 }
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:143
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

◆ SDL_JoystickCurrentPowerLevel()

SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel ( SDL_Joystick *  joystick)

Return the battery level of this joystick

Definition at line 920 of file SDL_joystick.c.

References SDL_JOYSTICK_POWER_UNKNOWN, and SDL_PrivateJoystickValid().

921 {
922  if (!SDL_PrivateJoystickValid(joystick)) {
924  }
925  return joystick->epowerlevel;
926 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickEventState()

int SDL_JoystickEventState ( int  state)

Enable/disable joystick event polling.

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

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

Definition at line 788 of file SDL_joystick.c.

References i, SDL_arraysize, SDL_DISABLE, SDL_ENABLE, SDL_EventState, SDL_JOYAXISMOTION, SDL_JOYBALLMOTION, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_JOYDEVICEADDED, SDL_JOYDEVICEREMOVED, SDL_JOYHATMOTION, and SDL_QUERY.

789 {
790 #if SDL_EVENTS_DISABLED
791  return SDL_DISABLE;
792 #else
793  const Uint32 event_list[] = {
796  };
797  unsigned int i;
798 
799  switch (state) {
800  case SDL_QUERY:
801  state = SDL_DISABLE;
802  for (i = 0; i < SDL_arraysize(event_list); ++i) {
803  state = SDL_EventState(event_list[i], SDL_QUERY);
804  if (state == SDL_ENABLE) {
805  break;
806  }
807  }
808  break;
809  default:
810  for (i = 0; i < SDL_arraysize(event_list); ++i) {
811  SDL_EventState(event_list[i], state);
812  }
813  break;
814  }
815  return (state);
816 #endif /* SDL_EVENTS_DISABLED */
817 }
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_DISABLE
Definition: SDL_events.h:721
#define SDL_EventState
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:90
#define SDL_QUERY
Definition: SDL_events.h:719

◆ SDL_JoystickFromInstanceID()

SDL_Joystick* SDL_JoystickFromInstanceID ( SDL_JoystickID  joyid)

Return the SDL_Joystick associated with an instance id.

Definition at line 381 of file SDL_joystick.c.

References NULL, and SDL_joysticks.

382 {
383  SDL_Joystick *joystick = SDL_joysticks;
384  while (joystick) {
385  if (joystick->instance_id == joyid) {
386  return joystick;
387  }
388  joystick = joystick->next;
389  }
390 
391  return NULL;
392 }
#define NULL
Definition: begin_code.h:143
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

◆ SDL_JoystickGetAttached()

SDL_bool SDL_JoystickGetAttached ( SDL_Joystick *  joystick)

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

Definition at line 355 of file SDL_joystick.c.

References SDL_FALSE, SDL_PrivateJoystickValid(), and SDL_SYS_JoystickAttached().

356 {
357  if (!SDL_PrivateJoystickValid(joystick)) {
358  return SDL_FALSE;
359  }
360 
361  return SDL_SYS_JoystickAttached(joystick);
362 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)

◆ SDL_JoystickGetAxis()

Sint16 SDL_JoystickGetAxis ( SDL_Joystick *  joystick,
int  axis 
)

Get the current state of an axis control on a joystick.

The state is a value ranging from -32768 to 32767.

The axis indices start at index 0.

Definition at line 266 of file SDL_joystick.c.

References axis, SDL_PrivateJoystickValid(), SDL_SetError, and state.

267 {
268  Sint16 state;
269 
270  if (!SDL_PrivateJoystickValid(joystick)) {
271  return (0);
272  }
273  if (axis < joystick->naxes) {
274  state = joystick->axes[axis];
275  } else {
276  SDL_SetError("Joystick only has %d axes", joystick->naxes);
277  state = 0;
278  }
279  return (state);
280 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
struct xkb_state * state
SDL_Texture * axis
#define SDL_SetError
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147

◆ SDL_JoystickGetBall()

int SDL_JoystickGetBall ( SDL_Joystick *  joystick,
int  ball,
int *  dx,
int *  dy 
)

Get the ball axis change since the last poll.

Returns
0, or -1 if you passed it invalid parameters.

The ball indices start at index 0.

Definition at line 306 of file SDL_joystick.c.

References retval, SDL_PrivateJoystickValid(), and SDL_SetError.

307 {
308  int retval;
309 
310  if (!SDL_PrivateJoystickValid(joystick)) {
311  return (-1);
312  }
313 
314  retval = 0;
315  if (ball < joystick->nballs) {
316  if (dx) {
317  *dx = joystick->balls[ball].dx;
318  }
319  if (dy) {
320  *dy = joystick->balls[ball].dy;
321  }
322  joystick->balls[ball].dx = 0;
323  joystick->balls[ball].dy = 0;
324  } else {
325  return SDL_SetError("Joystick only has %d balls", joystick->nballs);
326  }
327  return (retval);
328 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
SDL_bool retval
#define SDL_SetError

◆ SDL_JoystickGetButton()

Uint8 SDL_JoystickGetButton ( SDL_Joystick *  joystick,
int  button 
)

Get the current state of a button on a joystick.

The button indices start at index 0.

Definition at line 334 of file SDL_joystick.c.

References button, SDL_PrivateJoystickValid(), SDL_SetError, and state.

335 {
336  Uint8 state;
337 
338  if (!SDL_PrivateJoystickValid(joystick)) {
339  return (0);
340  }
341  if (button < joystick->nbuttons) {
342  state = joystick->buttons[button];
343  } else {
344  SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
345  state = 0;
346  }
347  return (state);
348 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
SDL_Texture * button
struct xkb_state * state
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_SetError

◆ SDL_JoystickGetDeviceGUID()

SDL_JoystickGUID SDL_JoystickGetDeviceGUID ( int  device_index)

Return the GUID for the joystick at this index

Definition at line 820 of file SDL_joystick.c.

References SDL_NumJoysticks(), SDL_SetError, SDL_SYS_JoystickGetDeviceGUID(), and SDL_zero.

821 {
822  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
823  SDL_JoystickGUID emptyGUID;
824  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
825  SDL_zero(emptyGUID);
826  return emptyGUID;
827  }
828  return SDL_SYS_JoystickGetDeviceGUID(device_index);
829 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:75
#define SDL_zero(x)
Definition: SDL_stdinc.h:361
#define SDL_SetError
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)

◆ SDL_JoystickGetGUID()

SDL_JoystickGUID SDL_JoystickGetGUID ( SDL_Joystick *  joystick)

Return the GUID for this opened joystick

Definition at line 832 of file SDL_joystick.c.

References SDL_PrivateJoystickValid(), SDL_SYS_JoystickGetGUID(), and SDL_zero.

833 {
834  if (!SDL_PrivateJoystickValid(joystick)) {
835  SDL_JoystickGUID emptyGUID;
836  SDL_zero(emptyGUID);
837  return emptyGUID;
838  }
839  return SDL_SYS_JoystickGetGUID(joystick);
840 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
#define SDL_zero(x)
Definition: SDL_stdinc.h:361
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)

◆ SDL_JoystickGetGUIDFromString()

SDL_JoystickGUID SDL_JoystickGetGUIDFromString ( const char *  pchGUID)

convert a string into a joystick formatted guid

Definition at line 890 of file SDL_joystick.c.

References i, nibble(), SDL_memset, and SDL_strlen.

891 {
892  SDL_JoystickGUID guid;
893  int maxoutputbytes= sizeof(guid);
894  size_t len = SDL_strlen(pchGUID);
895  Uint8 *p;
896  size_t i;
897 
898  /* Make sure it's even */
899  len = (len) & ~0x1;
900 
901  SDL_memset(&guid, 0x00, sizeof(guid));
902 
903  p = (Uint8 *)&guid;
904  for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
905  *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
906  }
907 
908  return guid;
909 }
GLuint GLfloat GLfloat GLfloat x1
GLfloat GLfloat p
GLenum GLsizei len
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
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
static unsigned char nibble(char c)
Definition: SDL_joystick.c:869
#define SDL_strlen
#define SDL_memset

◆ SDL_JoystickGetGUIDString()

void SDL_JoystickGetGUIDString ( SDL_JoystickGUID  guid,
char *  pszGUID,
int  cbGUID 
)

Return a string representation for this guid. pszGUID must point to at least 33 bytes (32 for the string plus a NULL terminator).

Definition at line 843 of file SDL_joystick.c.

References SDL_JoystickGUID::data, i, and NULL.

844 {
845  static const char k_rgchHexToASCII[] = "0123456789abcdef";
846  int i;
847 
848  if ((pszGUID == NULL) || (cbGUID <= 0)) {
849  return;
850  }
851 
852  for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
853  /* each input byte writes 2 ascii chars, and might write a null byte. */
854  /* If we don't have room for next input byte, stop */
855  unsigned char c = guid.data[i];
856 
857  *pszGUID++ = k_rgchHexToASCII[c >> 4];
858  *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
859  }
860  *pszGUID = '\0';
861 }
Uint8 data[16]
Definition: SDL_joystick.h:69
const GLubyte * c
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

◆ SDL_JoystickGetHat()

Uint8 SDL_JoystickGetHat ( SDL_Joystick *  joystick,
int  hat 
)

Get the current state of a POV hat on a joystick.

The hat indices start at index 0.

Returns
The return value is one of the following positions:

Definition at line 286 of file SDL_joystick.c.

References SDL_PrivateJoystickValid(), SDL_SetError, and state.

287 {
288  Uint8 state;
289 
290  if (!SDL_PrivateJoystickValid(joystick)) {
291  return (0);
292  }
293  if (hat < joystick->nhats) {
294  state = joystick->hats[hat];
295  } else {
296  SDL_SetError("Joystick only has %d hats", joystick->nhats);
297  state = 0;
298  }
299  return (state);
300 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
struct xkb_state * state
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
#define SDL_SetError

◆ SDL_JoystickInit()

int SDL_JoystickInit ( void  )

Definition at line 50 of file SDL_joystick.c.

References NULL, SDL_AddHintCallback, SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_INIT_EVENTS, SDL_InitSubSystem, SDL_JoystickAllowBackgroundEventsChanged(), and SDL_SYS_JoystickInit().

Referenced by SDL_InitSubSystem().

51 {
52  int status;
53 
54  /* See if we should allow joystick events while in the background */
57 
58 #if !SDL_EVENTS_DISABLED
60  return -1;
61  }
62 #endif /* !SDL_EVENTS_DISABLED */
63 
64  status = SDL_SYS_JoystickInit();
65  if (status >= 0) {
66  status = 0;
67  }
68  return (status);
69 }
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
Definition: SDL_hints.h:373
#define SDL_INIT_EVENTS
Definition: SDL.h:81
#define SDL_InitSubSystem
int SDL_SYS_JoystickInit(void)
#define NULL
Definition: begin_code.h:143
#define SDL_AddHintCallback
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
Definition: SDL_joystick.c:40

◆ SDL_JoystickInstanceID()

SDL_JoystickID SDL_JoystickInstanceID ( SDL_Joystick *  joystick)

Get the instance ID of an opened joystick or -1 if the joystick is invalid.

Definition at line 368 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

369 {
370  if (!SDL_PrivateJoystickValid(joystick)) {
371  return (-1);
372  }
373 
374  return (joystick->instance_id);
375 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickName()

const char* SDL_JoystickName ( SDL_Joystick *  joystick)

Return the name for this currently opened joystick. If no name can be found, this function returns NULL.

Definition at line 398 of file SDL_joystick.c.

References NULL, and SDL_PrivateJoystickValid().

399 {
400  if (!SDL_PrivateJoystickValid(joystick)) {
401  return (NULL);
402  }
403 
404  return (joystick->name);
405 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200
#define NULL
Definition: begin_code.h:143

◆ SDL_JoystickNameForIndex()

const char* SDL_JoystickNameForIndex ( int  device_index)

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

Definition at line 84 of file SDL_joystick.c.

References NULL, SDL_NumJoysticks(), SDL_SetError, and SDL_SYS_JoystickNameForDeviceIndex().

85 {
86  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
87  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
88  return (NULL);
89  }
90  return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
91 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:75
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
#define NULL
Definition: begin_code.h:143
#define SDL_SetError

◆ SDL_JoystickNumAxes()

int SDL_JoystickNumAxes ( SDL_Joystick *  joystick)

Get the number of general axis controls on a joystick.

Definition at line 218 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

219 {
220  if (!SDL_PrivateJoystickValid(joystick)) {
221  return (-1);
222  }
223  return (joystick->naxes);
224 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickNumBalls()

int SDL_JoystickNumBalls ( SDL_Joystick *  joystick)

Get the number of trackballs on a joystick.

Joystick trackballs have only relative motion events associated with them and their state cannot be polled.

Definition at line 242 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

243 {
244  if (!SDL_PrivateJoystickValid(joystick)) {
245  return (-1);
246  }
247  return (joystick->nballs);
248 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickNumButtons()

int SDL_JoystickNumButtons ( SDL_Joystick *  joystick)

Get the number of buttons on a joystick.

Definition at line 254 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

255 {
256  if (!SDL_PrivateJoystickValid(joystick)) {
257  return (-1);
258  }
259  return (joystick->nbuttons);
260 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickNumHats()

int SDL_JoystickNumHats ( SDL_Joystick *  joystick)

Get the number of POV hats on a joystick.

Definition at line 230 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

231 {
232  if (!SDL_PrivateJoystickValid(joystick)) {
233  return (-1);
234  }
235  return (joystick->nhats);
236 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:200

◆ SDL_JoystickOpen()

SDL_Joystick* SDL_JoystickOpen ( int  device_index)

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

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

Definition at line 101 of file SDL_joystick.c.

References SDL_GameController::joystick, NULL, SDL_free(), SDL_JOYSTICK_POWER_UNKNOWN, SDL_JoystickClose(), SDL_joysticks, SDL_malloc, SDL_memset, SDL_NumJoysticks(), SDL_OutOfMemory, SDL_SetError, SDL_strdup, SDL_SYS_GetInstanceIdOfDeviceIndex(), SDL_SYS_JoystickNameForDeviceIndex(), SDL_SYS_JoystickOpen(), and SDL_SYS_JoystickUpdate().

102 {
103  SDL_Joystick *joystick;
104  SDL_Joystick *joysticklist;
105  const char *joystickname = NULL;
106 
107  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
108  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
109  return (NULL);
110  }
111 
112  joysticklist = SDL_joysticks;
113  /* If the joystick is already open, return it
114  * it is important that we have a single joystick * for each instance id
115  */
116  while (joysticklist) {
117  if (SDL_SYS_GetInstanceIdOfDeviceIndex(device_index) == joysticklist->instance_id) {
118  joystick = joysticklist;
119  ++joystick->ref_count;
120  return (joystick);
121  }
122  joysticklist = joysticklist->next;
123  }
124 
125  /* Create and initialize the joystick */
126  joystick = (SDL_Joystick *) SDL_malloc((sizeof *joystick));
127  if (joystick == NULL) {
128  SDL_OutOfMemory();
129  return NULL;
130  }
131 
132  SDL_memset(joystick, 0, (sizeof *joystick));
133  if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
134  SDL_free(joystick);
135  return NULL;
136  }
137 
138  joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index);
139  if (joystickname)
140  joystick->name = SDL_strdup(joystickname);
141  else
142  joystick->name = NULL;
143 
144  if (joystick->naxes > 0) {
145  joystick->axes = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
146  joystick->axes_zero = (Sint16 *) SDL_malloc(joystick->naxes * sizeof(Sint16));
147  }
148  if (joystick->nhats > 0) {
149  joystick->hats = (Uint8 *) SDL_malloc
150  (joystick->nhats * sizeof(Uint8));
151  }
152  if (joystick->nballs > 0) {
153  joystick->balls = (struct balldelta *) SDL_malloc
154  (joystick->nballs * sizeof(*joystick->balls));
155  }
156  if (joystick->nbuttons > 0) {
157  joystick->buttons = (Uint8 *) SDL_malloc
158  (joystick->nbuttons * sizeof(Uint8));
159  }
160  if (((joystick->naxes > 0) && !joystick->axes)
161  || ((joystick->nhats > 0) && !joystick->hats)
162  || ((joystick->nballs > 0) && !joystick->balls)
163  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
164  SDL_OutOfMemory();
165  SDL_JoystickClose(joystick);
166  return NULL;
167  }
168  if (joystick->axes) {
169  SDL_memset(joystick->axes, 0, joystick->naxes * sizeof(Sint16));
170  SDL_memset(joystick->axes_zero, 0, joystick->naxes * sizeof(Sint16));
171  }
172  if (joystick->hats) {
173  SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
174  }
175  if (joystick->balls) {
176  SDL_memset(joystick->balls, 0,
177  joystick->nballs * sizeof(*joystick->balls));
178  }
179  if (joystick->buttons) {
180  SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
181  }
182  joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
183 
184  /* Add joystick to list */
185  ++joystick->ref_count;
186  /* Link the joystick in the list */
187  joystick->next = SDL_joysticks;
188  SDL_joysticks = joystick;
189 
190  SDL_SYS_JoystickUpdate(joystick);
191 
192  return (joystick);
193 }
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:411
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:143
void SDL_free(void *mem)
const char * SDL_SYS_JoystickNameForDeviceIndex(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
#define SDL_strdup
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
#define SDL_malloc
#define SDL_memset
int16_t Sint16
A signed 16-bit integer type.
Definition: SDL_stdinc.h:147
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

◆ SDL_JoystickQuit()

void SDL_JoystickQuit ( void  )

Definition at line 459 of file SDL_joystick.c.

References SDL_assert, SDL_INIT_EVENTS, SDL_JoystickClose(), SDL_joysticks, SDL_QuitSubSystem, SDL_SYS_JoystickQuit(), and SDL_updating_joystick.

Referenced by SDL_QuitSubSystem().

460 {
461  /* Make sure we're not getting called in the middle of updating joysticks */
463 
464  /* Stop the event polling */
465  while (SDL_joysticks) {
466  SDL_joysticks->ref_count = 1;
468  }
469 
470  /* Quit the joystick setup */
472 
473 #if !SDL_EVENTS_DISABLED
475 #endif
476 }
#define SDL_INIT_EVENTS
Definition: SDL.h:81
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:411
#define SDL_QuitSubSystem
void SDL_SYS_JoystickQuit(void)
#define SDL_assert(condition)
Definition: SDL_assert.h:167
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

◆ SDL_JoystickUpdate()

void SDL_JoystickUpdate ( void  )

Update the current state of the open joysticks.

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

Definition at line 736 of file SDL_joystick.c.

References i, NULL, SDL_FALSE, SDL_HAT_CENTERED, SDL_JoystickClose(), SDL_joysticks, SDL_PrivateJoystickAxis(), SDL_PrivateJoystickButton(), SDL_PrivateJoystickHat(), SDL_SYS_JoystickDetect(), SDL_SYS_JoystickUpdate(), and SDL_updating_joystick.

737 {
738  SDL_Joystick *joystick;
739 
740  joystick = SDL_joysticks;
741  while (joystick) {
742  SDL_Joystick *joysticknext;
743  /* save off the next pointer, the Update call may cause a joystick removed event
744  * and cause our joystick pointer to be freed
745  */
746  joysticknext = joystick->next;
747 
748  SDL_updating_joystick = joystick;
749 
750  SDL_SYS_JoystickUpdate(joystick);
751 
752  if (joystick->force_recentering) {
753  int i;
754 
755  /* Tell the app that everything is centered/unpressed... */
756  for (i = 0; i < joystick->naxes; i++) {
757  SDL_PrivateJoystickAxis(joystick, i, joystick->axes_zero[i]);
758  }
759 
760  for (i = 0; i < joystick->nbuttons; i++) {
761  SDL_PrivateJoystickButton(joystick, i, 0);
762  }
763 
764  for (i = 0; i < joystick->nhats; i++) {
766  }
767 
768  joystick->force_recentering = SDL_FALSE;
769  }
770 
772 
773  /* If the joystick was closed while updating, free it here */
774  if (joystick->ref_count <= 0) {
775  SDL_JoystickClose(joystick);
776  }
777 
778  joystick = joysticknext;
779  }
780 
781  /* this needs to happen AFTER walking the joystick list above, so that any
782  dangling hardware data from removed devices can be free'd
783  */
785 }
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:411
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
Definition: SDL_joystick.c:608
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:684
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:567
void SDL_SYS_JoystickDetect()
void SDL_SYS_JoystickUpdate(SDL_Joystick *joystick)
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
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

◆ SDL_NumJoysticks()

int SDL_NumJoysticks ( void  )

Count the number of joysticks attached to the system right now

Definition at line 75 of file SDL_joystick.c.

References SDL_SYS_NumJoysticks().

Referenced by SDL_JoystickGetDeviceGUID(), SDL_JoystickNameForIndex(), and SDL_JoystickOpen().

76 {
77  return SDL_SYS_NumJoysticks();
78 }
int SDL_SYS_NumJoysticks()

◆ SDL_PrivateJoystickAdded()

void SDL_PrivateJoystickAdded ( int  device_index)

Definition at line 501 of file SDL_joystick.c.

References NULL, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_GetEventState, SDL_JOYDEVICEADDED, SDL_PushEvent, and SDL_Event::type.

Referenced by SDL_SYS_AddJoystickDevice().

502 {
503 #if !SDL_EVENTS_DISABLED
505 
506  event.type = SDL_JOYDEVICEADDED;
507 
508  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
509  event.jdevice.which = device_index;
510  if ( (SDL_EventOK == NULL) ||
511  (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
512  SDL_PushEvent(&event);
513  }
514  }
515 #endif /* !SDL_EVENTS_DISABLED */
516 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
#define SDL_PushEvent
void * SDL_EventOKParam
Definition: SDL_events.c:41
#define NULL
Definition: begin_code.h:143
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickAxis()

int SDL_PrivateJoystickAxis ( SDL_Joystick *  joystick,
Uint8  axis,
Sint16  value 
)

Definition at line 567 of file SDL_joystick.c.

References axis, SDL_ENABLE, SDL_GetEventState, SDL_JOYAXISMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

Referenced by SDL_JoystickUpdate(), SDL_SYS_AccelerometerUpdate(), and SDL_SYS_MFIJoystickUpdate().

568 {
569  int posted;
570 
571  /* Make sure we're not getting garbage or duplicate events */
572  if (axis >= joystick->naxes) {
573  return 0;
574  }
575  if (value == joystick->axes[axis]) {
576  return 0;
577  }
578 
579  /* We ignore events if we don't have keyboard focus, except for centering
580  * events.
581  */
583  if ((value > joystick->axes_zero[axis] && value >= joystick->axes[axis]) ||
584  (value < joystick->axes_zero[axis] && value <= joystick->axes[axis])) {
585  return 0;
586  }
587  }
588 
589  /* Update internal joystick state */
590  joystick->axes[axis] = value;
591 
592  /* Post the event, if desired */
593  posted = 0;
594 #if !SDL_EVENTS_DISABLED
597  event.type = SDL_JOYAXISMOTION;
598  event.jaxis.which = joystick->instance_id;
599  event.jaxis.axis = axis;
600  event.jaxis.value = value;
601  posted = SDL_PushEvent(&event) == 1;
602  }
603 #endif /* !SDL_EVENTS_DISABLED */
604  return (posted);
605 }
#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
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBall()

int SDL_PrivateJoystickBall ( SDL_Joystick *  joystick,
Uint8  ball,
Sint16  xrel,
Sint16  yrel 
)

Definition at line 648 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_JOYBALLMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

650 {
651  int posted;
652 
653  /* Make sure we're not getting garbage events */
654  if (ball >= joystick->nballs) {
655  return 0;
656  }
657 
658  /* We ignore events if we don't have keyboard focus. */
660  return 0;
661  }
662 
663  /* Update internal mouse state */
664  joystick->balls[ball].dx += xrel;
665  joystick->balls[ball].dy += yrel;
666 
667  /* Post the event, if desired */
668  posted = 0;
669 #if !SDL_EVENTS_DISABLED
672  event.jball.type = SDL_JOYBALLMOTION;
673  event.jball.which = joystick->instance_id;
674  event.jball.ball = ball;
675  event.jball.xrel = xrel;
676  event.jball.yrel = yrel;
677  posted = SDL_PushEvent(&event) == 1;
678  }
679 #endif /* !SDL_EVENTS_DISABLED */
680  return (posted);
681 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBatteryLevel()

void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 913 of file SDL_joystick.c.

914 {
915  joystick->epowerlevel = ePowerLevel;
916 }

◆ SDL_PrivateJoystickButton()

int SDL_PrivateJoystickButton ( SDL_Joystick *  joystick,
Uint8  button,
Uint8  state 
)

Definition at line 684 of file SDL_joystick.c.

References button, SDL_ENABLE, SDL_GetEventState, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_PRESSED, SDL_PrivateJoystickShouldIgnoreEvent(), SDL_PushEvent, SDL_RELEASED, state, and SDL_Event::type.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

685 {
686  int posted;
687 #if !SDL_EVENTS_DISABLED
689 
690  switch (state) {
691  case SDL_PRESSED:
692  event.type = SDL_JOYBUTTONDOWN;
693  break;
694  case SDL_RELEASED:
695  event.type = SDL_JOYBUTTONUP;
696  break;
697  default:
698  /* Invalid state -- bail */
699  return (0);
700  }
701 #endif /* !SDL_EVENTS_DISABLED */
702 
703  /* Make sure we're not getting garbage or duplicate events */
704  if (button >= joystick->nbuttons) {
705  return 0;
706  }
707  if (state == joystick->buttons[button]) {
708  return 0;
709  }
710 
711  /* We ignore events if we don't have keyboard focus, except for button
712  * release. */
714  if (state == SDL_PRESSED) {
715  return 0;
716  }
717  }
718 
719  /* Update internal joystick state */
720  joystick->buttons[button] = state;
721 
722  /* Post the event, if desired */
723  posted = 0;
724 #if !SDL_EVENTS_DISABLED
725  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
726  event.jbutton.which = joystick->instance_id;
727  event.jbutton.button = button;
728  event.jbutton.state = state;
729  posted = SDL_PushEvent(&event) == 1;
730  }
731 #endif /* !SDL_EVENTS_DISABLED */
732  return (posted);
733 }
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
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
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_PrivateJoystickHat()

int SDL_PrivateJoystickHat ( SDL_Joystick *  joystick,
Uint8  hat,
Uint8  value 
)

Definition at line 608 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_HAT_CENTERED, SDL_JOYHATMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

609 {
610  int posted;
611 
612  /* Make sure we're not getting garbage or duplicate events */
613  if (hat >= joystick->nhats) {
614  return 0;
615  }
616  if (value == joystick->hats[hat]) {
617  return 0;
618  }
619 
620  /* We ignore events if we don't have keyboard focus, except for centering
621  * events.
622  */
624  if (value != SDL_HAT_CENTERED) {
625  return 0;
626  }
627  }
628 
629  /* Update internal joystick state */
630  joystick->hats[hat] = value;
631 
632  /* Post the event, if desired */
633  posted = 0;
634 #if !SDL_EVENTS_DISABLED
637  event.jhat.type = SDL_JOYHATMOTION;
638  event.jhat.which = joystick->instance_id;
639  event.jhat.hat = hat;
640  event.jhat.value = value;
641  posted = SDL_PushEvent(&event) == 1;
642  }
643 #endif /* !SDL_EVENTS_DISABLED */
644  return (posted);
645 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:480
General event structure.
Definition: SDL_events.h:525
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207

◆ SDL_PrivateJoystickRemoved()

void SDL_PrivateJoystickRemoved ( SDL_JoystickID  device_instance)

Definition at line 547 of file SDL_joystick.c.

References NULL, SDL_ENABLE, SDL_EventOK, SDL_EventOKParam, SDL_GetEventState, SDL_JOYDEVICEREMOVED, SDL_PushEvent, SDL_Event::type, and UpdateEventsForDeviceRemoval().

Referenced by SDL_SYS_RemoveJoystickDevice().

548 {
549 #if !SDL_EVENTS_DISABLED
551 
552  event.type = SDL_JOYDEVICEREMOVED;
553 
554  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
555  event.jdevice.which = device_instance;
556  if ( (SDL_EventOK == NULL) ||
557  (*SDL_EventOK) (SDL_EventOKParam, &event) ) {
558  SDL_PushEvent(&event);
559  }
560  }
561 
563 #endif /* !SDL_EVENTS_DISABLED */
564 }
#define SDL_ENABLE
Definition: SDL_events.h:722
static void UpdateEventsForDeviceRemoval()
Definition: SDL_joystick.c:523
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
SDL_EventFilter SDL_EventOK
Definition: SDL_events.c:40
#define SDL_PushEvent
void * SDL_EventOKParam
Definition: SDL_events.c:41
#define NULL
Definition: begin_code.h:143
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickShouldIgnoreEvent()

static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent ( )
static

Definition at line 480 of file SDL_joystick.c.

References NULL, SDL_FALSE, SDL_GetKeyboardFocus, SDL_INIT_VIDEO, SDL_joystick_allows_background_events, SDL_TRUE, and SDL_WasInit.

Referenced by SDL_PrivateJoystickAxis(), SDL_PrivateJoystickBall(), SDL_PrivateJoystickButton(), and SDL_PrivateJoystickHat().

481 {
483  return SDL_FALSE;
484  }
485 
487  if (SDL_GetKeyboardFocus() == NULL) {
488  /* Video is initialized and we don't have focus, ignore the event. */
489  return SDL_TRUE;
490  } else {
491  return SDL_FALSE;
492  }
493  }
494 
495  /* Video subsystem wasn't initialized, always allow the event */
496  return SDL_FALSE;
497 }
#define SDL_GetKeyboardFocus
static SDL_bool SDL_joystick_allows_background_events
Definition: SDL_joystick.c:35
#define NULL
Definition: begin_code.h:143
#define SDL_WasInit
#define SDL_INIT_VIDEO
Definition: SDL.h:77

◆ SDL_PrivateJoystickValid()

int SDL_PrivateJoystickValid ( SDL_Joystick *  joystick)

Definition at line 200 of file SDL_joystick.c.

References NULL, and SDL_SetError.

Referenced by SDL_HapticOpenFromJoystick(), SDL_JoystickCurrentPowerLevel(), SDL_JoystickGetAttached(), SDL_JoystickGetAxis(), SDL_JoystickGetBall(), SDL_JoystickGetButton(), SDL_JoystickGetGUID(), SDL_JoystickGetHat(), SDL_JoystickInstanceID(), SDL_JoystickIsHaptic(), SDL_JoystickName(), SDL_JoystickNumAxes(), SDL_JoystickNumBalls(), SDL_JoystickNumButtons(), and SDL_JoystickNumHats().

201 {
202  int valid;
203 
204  if (joystick == NULL) {
205  SDL_SetError("Joystick hasn't been opened yet");
206  valid = 0;
207  } else {
208  valid = 1;
209  }
210 
211  return valid;
212 }
#define NULL
Definition: begin_code.h:143
#define SDL_SetError

◆ UpdateEventsForDeviceRemoval()

static void UpdateEventsForDeviceRemoval ( )
static

Definition at line 523 of file SDL_joystick.c.

References events, i, SDL_Event::jdevice, NULL, SDL_ADDEVENT, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_PEEKEVENT, SDL_PeepEvents, SDL_stack_alloc, SDL_stack_free, and SDL_JoyDeviceEvent::which.

Referenced by SDL_PrivateJoystickRemoved().

524 {
525  int i, num_events;
526  SDL_Event *events;
527 
529  if (num_events <= 0) {
530  return;
531  }
532 
533  events = SDL_stack_alloc(SDL_Event, num_events);
534  if (!events) {
535  return;
536  }
537 
538  num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
539  for (i = 0; i < num_events; ++i) {
540  --events[i].jdevice.which;
541  }
542  SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
543 
544  SDL_stack_free(events);
545 }
SDL_JoyDeviceEvent jdevice
Definition: SDL_events.h:540
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:36
#define SDL_PeepEvents
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:330
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:331

Variable Documentation

◆ SDL_joystick_allows_background_events

SDL_bool SDL_joystick_allows_background_events = SDL_FALSE
static

◆ SDL_joysticks

SDL_Joystick* SDL_joysticks = NULL
static

◆ SDL_updating_joystick

SDL_Joystick* SDL_updating_joystick = NULL
static

Definition at line 37 of file SDL_joystick.c.

Referenced by SDL_JoystickClose(), SDL_JoystickQuit(), and SDL_JoystickUpdate().