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

static unsigned char nibble ( char  c)
static

Definition at line 803 of file SDL_joystick.c.

Referenced by SDL_JoystickGetGUIDFromString().

804 {
805  if ((c >= '0') && (c <= '9')) {
806  return (unsigned char)(c - '0');
807  }
808 
809  if ((c >= 'A') && (c <= 'F')) {
810  return (unsigned char)(c - 'A' + 0x0a);
811  }
812 
813  if ((c >= 'a') && (c <= 'f')) {
814  return (unsigned char)(c - 'a' + 0x0a);
815  }
816 
817  /* received an invalid character, and no real way to return an error */
818  /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
819  return 0;
820 }
const GLubyte * c
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
void SDL_JoystickClose ( SDL_Joystick *  joystick)

Close a joystick previously opened with SDL_JoystickOpen().

Definition at line 410 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().

411 {
412  SDL_Joystick *joysticklist;
413  SDL_Joystick *joysticklistprev;
414 
415  if (!joystick) {
416  return;
417  }
418 
419  /* First decrement ref count */
420  if (--joystick->ref_count > 0) {
421  return;
422  }
423 
424  if (joystick == SDL_updating_joystick) {
425  return;
426  }
427 
428  SDL_SYS_JoystickClose(joystick);
429  joystick->hwdata = NULL;
430 
431  joysticklist = SDL_joysticks;
432  joysticklistprev = NULL;
433  while (joysticklist) {
434  if (joystick == joysticklist) {
435  if (joysticklistprev) {
436  /* unlink this entry */
437  joysticklistprev->next = joysticklist->next;
438  } else {
439  SDL_joysticks = joystick->next;
440  }
441  break;
442  }
443  joysticklistprev = joysticklist;
444  joysticklist = joysticklist->next;
445  }
446 
447  SDL_free(joystick->name);
448 
449  /* Free the data associated with this joystick */
450  SDL_free(joystick->axes);
451  SDL_free(joystick->hats);
452  SDL_free(joystick->balls);
453  SDL_free(joystick->buttons);
454  SDL_free(joystick);
455 }
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_JoystickPowerLevel SDL_JoystickCurrentPowerLevel ( SDL_Joystick *  joystick)

Return the battery level of this joystick

Definition at line 854 of file SDL_joystick.c.

References SDL_JOYSTICK_POWER_UNKNOWN, and SDL_PrivateJoystickValid().

855 {
856  if (!SDL_PrivateJoystickValid(joystick)) {
858  }
859  return joystick->epowerlevel;
860 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
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 722 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.

723 {
724 #if SDL_EVENTS_DISABLED
725  return SDL_DISABLE;
726 #else
727  const Uint32 event_list[] = {
730  };
731  unsigned int i;
732 
733  switch (state) {
734  case SDL_QUERY:
735  state = SDL_DISABLE;
736  for (i = 0; i < SDL_arraysize(event_list); ++i) {
737  state = SDL_EventState(event_list[i], SDL_QUERY);
738  if (state == SDL_ENABLE) {
739  break;
740  }
741  }
742  break;
743  default:
744  for (i = 0; i < SDL_arraysize(event_list); ++i) {
745  SDL_EventState(event_list[i], state);
746  }
747  break;
748  }
749  return (state);
750 #endif /* SDL_EVENTS_DISABLED */
751 }
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:155
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:718
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
#define SDL_DISABLE
Definition: SDL_events.h:717
#define SDL_EventState
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
#define SDL_QUERY
Definition: SDL_events.h:715
SDL_Joystick* SDL_JoystickFromInstanceID ( SDL_JoystickID  joyid)

Return the SDL_Joystick associated with an instance id.

Definition at line 380 of file SDL_joystick.c.

References NULL, and SDL_joysticks.

381 {
382  SDL_Joystick *joystick = SDL_joysticks;
383  while (joystick) {
384  if (joystick->instance_id == joyid) {
385  return joystick;
386  }
387  joystick = joystick->next;
388  }
389 
390  return NULL;
391 }
#define NULL
Definition: begin_code.h:143
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36
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 354 of file SDL_joystick.c.

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

355 {
356  if (!SDL_PrivateJoystickValid(joystick)) {
357  return SDL_FALSE;
358  }
359 
360  return SDL_SYS_JoystickAttached(joystick);
361 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
SDL_bool SDL_SYS_JoystickAttached(SDL_Joystick *joystick)
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 265 of file SDL_joystick.c.

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

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

References retval, SDL_PrivateJoystickValid(), and SDL_SetError.

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

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

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

Return the GUID for the joystick at this index

Definition at line 754 of file SDL_joystick.c.

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

755 {
756  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
757  SDL_JoystickGUID emptyGUID;
758  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
759  SDL_zero(emptyGUID);
760  return emptyGUID;
761  }
762  return SDL_SYS_JoystickGetDeviceGUID(device_index);
763 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:75
#define SDL_zero(x)
Definition: SDL_stdinc.h:355
#define SDL_SetError
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)
SDL_JoystickGUID SDL_JoystickGetGUID ( SDL_Joystick *  joystick)

Return the GUID for this opened joystick

Definition at line 766 of file SDL_joystick.c.

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

767 {
768  if (!SDL_PrivateJoystickValid(joystick)) {
769  SDL_JoystickGUID emptyGUID;
770  SDL_zero(emptyGUID);
771  return emptyGUID;
772  }
773  return SDL_SYS_JoystickGetGUID(joystick);
774 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
#define SDL_zero(x)
Definition: SDL_stdinc.h:355
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)
SDL_JoystickGUID SDL_JoystickGetGUIDFromString ( const char *  pchGUID)

convert a string into a joystick formatted guid

Definition at line 824 of file SDL_joystick.c.

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

825 {
826  SDL_JoystickGUID guid;
827  int maxoutputbytes= sizeof(guid);
828  size_t len = SDL_strlen(pchGUID);
829  Uint8 *p;
830  size_t i;
831 
832  /* Make sure it's even */
833  len = (len) & ~0x1;
834 
835  SDL_memset(&guid, 0x00, sizeof(guid));
836 
837  p = (Uint8 *)&guid;
838  for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
839  *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
840  }
841 
842  return guid;
843 }
GLuint GLfloat GLfloat GLfloat x1
GLfloat GLfloat p
GLenum GLsizei len
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
static unsigned char nibble(char c)
Definition: SDL_joystick.c:803
#define SDL_strlen
#define SDL_memset
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 777 of file SDL_joystick.c.

References SDL_JoystickGUID::data, i, and NULL.

778 {
779  static const char k_rgchHexToASCII[] = "0123456789abcdef";
780  int i;
781 
782  if ((pszGUID == NULL) || (cbGUID <= 0)) {
783  return;
784  }
785 
786  for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
787  /* each input byte writes 2 ascii chars, and might write a null byte. */
788  /* If we don't have room for next input byte, stop */
789  unsigned char c = guid.data[i];
790 
791  *pszGUID++ = k_rgchHexToASCII[c >> 4];
792  *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
793  }
794  *pszGUID = '\0';
795 }
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:42
#define NULL
Definition: begin_code.h:143
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 285 of file SDL_joystick.c.

References SDL_PrivateJoystickValid(), SDL_SetError, and state.

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

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

Definition at line 367 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

368 {
369  if (!SDL_PrivateJoystickValid(joystick)) {
370  return (-1);
371  }
372 
373  return (joystick->instance_id);
374 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
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 397 of file SDL_joystick.c.

References NULL, and SDL_PrivateJoystickValid().

398 {
399  if (!SDL_PrivateJoystickValid(joystick)) {
400  return (NULL);
401  }
402 
403  return (joystick->name);
404 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
#define NULL
Definition: begin_code.h:143
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
int SDL_JoystickNumAxes ( SDL_Joystick *  joystick)

Get the number of general axis controls on a joystick.

Definition at line 217 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

218 {
219  if (!SDL_PrivateJoystickValid(joystick)) {
220  return (-1);
221  }
222  return (joystick->naxes);
223 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
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 241 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

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

Get the number of buttons on a joystick.

Definition at line 253 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

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

Get the number of POV hats on a joystick.

Definition at line 229 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

230 {
231  if (!SDL_PrivateJoystickValid(joystick)) {
232  return (-1);
233  }
234  return (joystick->nhats);
235 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:199
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
146  (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  }
171  if (joystick->hats) {
172  SDL_memset(joystick->hats, 0, joystick->nhats * sizeof(Uint8));
173  }
174  if (joystick->balls) {
175  SDL_memset(joystick->balls, 0,
176  joystick->nballs * sizeof(*joystick->balls));
177  }
178  if (joystick->buttons) {
179  SDL_memset(joystick->buttons, 0, joystick->nbuttons * sizeof(Uint8));
180  }
181  joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
182 
183  /* Add joystick to list */
184  ++joystick->ref_count;
185  /* Link the joystick in the list */
186  joystick->next = SDL_joysticks;
187  SDL_joysticks = joystick;
188 
189  SDL_SYS_JoystickUpdate(joystick);
190 
191  return (joystick);
192 }
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:410
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:75
uint8_t Uint8
An unsigned 8-bit integer type.
Definition: SDL_stdinc.h:139
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:143
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36
void SDL_JoystickQuit ( void  )

Definition at line 458 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().

459 {
460  /* Make sure we're not getting called in the middle of updating joysticks */
462 
463  /* Stop the event polling */
464  while (SDL_joysticks) {
465  SDL_joysticks->ref_count = 1;
467  }
468 
469  /* Quit the joystick setup */
471 
472 #if !SDL_EVENTS_DISABLED
474 #endif
475 }
#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:410
#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
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 670 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.

671 {
672  SDL_Joystick *joystick;
673 
674  joystick = SDL_joysticks;
675  while (joystick) {
676  SDL_Joystick *joysticknext;
677  /* save off the next pointer, the Update call may cause a joystick removed event
678  * and cause our joystick pointer to be freed
679  */
680  joysticknext = joystick->next;
681 
682  SDL_updating_joystick = joystick;
683 
684  SDL_SYS_JoystickUpdate(joystick);
685 
686  if (joystick->force_recentering) {
687  int i;
688 
689  /* Tell the app that everything is centered/unpressed... */
690  for (i = 0; i < joystick->naxes; i++) {
691  SDL_PrivateJoystickAxis(joystick, i, 0);
692  }
693 
694  for (i = 0; i < joystick->nbuttons; i++) {
695  SDL_PrivateJoystickButton(joystick, i, 0);
696  }
697 
698  for (i = 0; i < joystick->nhats; i++) {
700  }
701 
702  joystick->force_recentering = SDL_FALSE;
703  }
704 
706 
707  /* If the joystick was closed while updating, free it here */
708  if (joystick->ref_count <= 0) {
709  SDL_JoystickClose(joystick);
710  }
711 
712  joystick = joysticknext;
713  }
714 
715  /* this needs to happen AFTER walking the joystick list above, so that any
716  dangling hardware data from removed devices can be free'd
717  */
719 }
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:410
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
Definition: SDL_joystick.c:542
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:618
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:501
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:42
#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
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()
int SDL_PrivateJoystickAxis ( SDL_Joystick *  joystick,
Uint8  axis,
Sint16  value 
)

Definition at line 501 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().

502 {
503  int posted;
504 
505  /* Make sure we're not getting garbage or duplicate events */
506  if (axis >= joystick->naxes) {
507  return 0;
508  }
509  if (value == joystick->axes[axis]) {
510  return 0;
511  }
512 
513  /* We ignore events if we don't have keyboard focus, except for centering
514  * events.
515  */
517  if ((value > 0 && value >= joystick->axes[axis]) ||
518  (value < 0 && value <= joystick->axes[axis])) {
519  return 0;
520  }
521  }
522 
523  /* Update internal joystick state */
524  joystick->axes[axis] = value;
525 
526  /* Post the event, if desired */
527  posted = 0;
528 #if !SDL_EVENTS_DISABLED
531  event.type = SDL_JOYAXISMOTION;
532  event.jaxis.which = joystick->instance_id;
533  event.jaxis.axis = axis;
534  event.jaxis.value = value;
535  posted = SDL_PushEvent(&event) == 1;
536  }
537 #endif /* !SDL_EVENTS_DISABLED */
538  return (posted);
539 }
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_Texture * axis
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
int SDL_PrivateJoystickBall ( SDL_Joystick *  joystick,
Uint8  ball,
Sint16  xrel,
Sint16  yrel 
)

Definition at line 582 of file SDL_joystick.c.

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

584 {
585  int posted;
586 
587  /* Make sure we're not getting garbage events */
588  if (ball >= joystick->nballs) {
589  return 0;
590  }
591 
592  /* We ignore events if we don't have keyboard focus. */
594  return 0;
595  }
596 
597  /* Update internal mouse state */
598  joystick->balls[ball].dx += xrel;
599  joystick->balls[ball].dy += yrel;
600 
601  /* Post the event, if desired */
602  posted = 0;
603 #if !SDL_EVENTS_DISABLED
606  event.jball.type = SDL_JOYBALLMOTION;
607  event.jball.which = joystick->instance_id;
608  event.jball.ball = ball;
609  event.jball.xrel = xrel;
610  event.jball.yrel = yrel;
611  posted = SDL_PushEvent(&event) == 1;
612  }
613 #endif /* !SDL_EVENTS_DISABLED */
614  return (posted);
615 }
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 847 of file SDL_joystick.c.

848 {
849  joystick->epowerlevel = ePowerLevel;
850 }
int SDL_PrivateJoystickButton ( SDL_Joystick *  joystick,
Uint8  button,
Uint8  state 
)

Definition at line 618 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().

619 {
620  int posted;
621 #if !SDL_EVENTS_DISABLED
623 
624  switch (state) {
625  case SDL_PRESSED:
626  event.type = SDL_JOYBUTTONDOWN;
627  break;
628  case SDL_RELEASED:
629  event.type = SDL_JOYBUTTONUP;
630  break;
631  default:
632  /* Invalid state -- bail */
633  return (0);
634  }
635 #endif /* !SDL_EVENTS_DISABLED */
636 
637  /* Make sure we're not getting garbage or duplicate events */
638  if (button >= joystick->nbuttons) {
639  return 0;
640  }
641  if (state == joystick->buttons[button]) {
642  return 0;
643  }
644 
645  /* We ignore events if we don't have keyboard focus, except for button
646  * release. */
648  if (state == SDL_PRESSED) {
649  return 0;
650  }
651  }
652 
653  /* Update internal joystick state */
654  joystick->buttons[button] = state;
655 
656  /* Post the event, if desired */
657  posted = 0;
658 #if !SDL_EVENTS_DISABLED
659  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
660  event.jbutton.which = joystick->instance_id;
661  event.jbutton.button = button;
662  event.jbutton.state = state;
663  posted = SDL_PushEvent(&event) == 1;
664  }
665 #endif /* !SDL_EVENTS_DISABLED */
666  return (posted);
667 }
SDL_Texture * button
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_RELEASED
Definition: SDL_events.h:49
Uint32 type
Definition: SDL_events.h:523
int SDL_PrivateJoystickHat ( SDL_Joystick *  joystick,
Uint8  hat,
Uint8  value 
)

Definition at line 542 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().

543 {
544  int posted;
545 
546  /* Make sure we're not getting garbage or duplicate events */
547  if (hat >= joystick->nhats) {
548  return 0;
549  }
550  if (value == joystick->hats[hat]) {
551  return 0;
552  }
553 
554  /* We ignore events if we don't have keyboard focus, except for centering
555  * events.
556  */
558  if (value != SDL_HAT_CENTERED) {
559  return 0;
560  }
561  }
562 
563  /* Update internal joystick state */
564  joystick->hats[hat] = value;
565 
566  /* Post the event, if desired */
567  posted = 0;
568 #if !SDL_EVENTS_DISABLED
571  event.jhat.type = SDL_JOYHATMOTION;
572  event.jhat.which = joystick->instance_id;
573  event.jhat.hat = hat;
574  event.jhat.value = value;
575  posted = SDL_PushEvent(&event) == 1;
576  }
577 #endif /* !SDL_EVENTS_DISABLED */
578  return (posted);
579 }
#define SDL_ENABLE
Definition: SDL_events.h:718
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
GLsizei const GLfloat * value
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
General event structure.
Definition: SDL_events.h:521
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent ( )
static

Definition at line 479 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().

480 {
482  return SDL_FALSE;
483  }
484 
486  if (SDL_GetKeyboardFocus() == NULL) {
487  /* Video is initialized and we don't have focus, ignore the event. */
488  return SDL_TRUE;
489  } else {
490  return SDL_FALSE;
491  }
492  }
493 
494  /* Video subsystem wasn't initialized, always allow the event */
495  return SDL_FALSE;
496 }
#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
int SDL_PrivateJoystickValid ( SDL_Joystick *  joystick)

Definition at line 199 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().

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

Variable Documentation

SDL_bool SDL_joystick_allows_background_events = SDL_FALSE
static
SDL_Joystick* SDL_joysticks = NULL
static
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().