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

Go to the source code of this file.

Data Structures

struct  SDL_JoystickGUID
 

Macros

Hat positions
#define SDL_HAT_CENTERED   0x00
 
#define SDL_HAT_UP   0x01
 
#define SDL_HAT_RIGHT   0x02
 
#define SDL_HAT_DOWN   0x04
 
#define SDL_HAT_LEFT   0x08
 
#define SDL_HAT_RIGHTUP   (SDL_HAT_RIGHT|SDL_HAT_UP)
 
#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)
 
#define SDL_HAT_LEFTUP   (SDL_HAT_LEFT|SDL_HAT_UP)
 
#define SDL_HAT_LEFTDOWN   (SDL_HAT_LEFT|SDL_HAT_DOWN)
 

Typedefs

typedef Sint32 SDL_JoystickID
 

Enumerations

enum  SDL_JoystickPowerLevel {
  SDL_JOYSTICK_POWER_UNKNOWN = -1,
  SDL_JOYSTICK_POWER_EMPTY,
  SDL_JOYSTICK_POWER_LOW,
  SDL_JOYSTICK_POWER_MEDIUM,
  SDL_JOYSTICK_POWER_FULL,
  SDL_JOYSTICK_POWER_WIRED,
  SDL_JOYSTICK_POWER_MAX
}
 

Functions

int SDL_NumJoysticks (void)
 
const char * SDL_JoystickNameForIndex (int device_index)
 
SDL_Joystick * SDL_JoystickOpen (int device_index)
 
SDL_Joystick * SDL_JoystickFromInstanceID (SDL_JoystickID joyid)
 
const char * SDL_JoystickName (SDL_Joystick *joystick)
 
SDL_JoystickGUID SDL_JoystickGetDeviceGUID (int device_index)
 
SDL_JoystickGUID SDL_JoystickGetGUID (SDL_Joystick *joystick)
 
void SDL_JoystickGetGUIDString (SDL_JoystickGUID guid, char *pszGUID, int cbGUID)
 
SDL_JoystickGUID SDL_JoystickGetGUIDFromString (const char *pchGUID)
 
SDL_bool SDL_JoystickGetAttached (SDL_Joystick *joystick)
 
SDL_JoystickID SDL_JoystickInstanceID (SDL_Joystick *joystick)
 
int SDL_JoystickNumAxes (SDL_Joystick *joystick)
 
int SDL_JoystickNumBalls (SDL_Joystick *joystick)
 
int SDL_JoystickNumHats (SDL_Joystick *joystick)
 
int SDL_JoystickNumButtons (SDL_Joystick *joystick)
 
void SDL_JoystickUpdate (void)
 
int SDL_JoystickEventState (int state)
 
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)
 
void SDL_JoystickClose (SDL_Joystick *joystick)
 
SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel (SDL_Joystick *joystick)
 

Detailed Description

Include file for SDL joystick event handling

The term "device_index" identifies currently plugged in joystick devices between 0 and SDL_NumJoysticks, with the exact joystick behind a device_index changing as joysticks are plugged and unplugged.

The term "instance_id" is the current instantiation of a joystick device in the system, if the joystick is removed and then re-inserted then it will get a new instance_id, instance_id's are monotonically increasing identifiers of a joystick plugged in.

The term JoystickGUID is a stable 128-bit identifier for a joystick device that does not change over time, it identifies class of the device (a X360 wired controller for example). This identifier is platform dependent.

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

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

Definition in file SDL_joystick.h.

Macro Definition Documentation

#define SDL_HAT_DOWN   0x04
#define SDL_HAT_LEFT   0x08
#define SDL_HAT_LEFTDOWN   (SDL_HAT_LEFT|SDL_HAT_DOWN)

Definition at line 215 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

#define SDL_HAT_LEFTUP   (SDL_HAT_LEFT|SDL_HAT_UP)

Definition at line 214 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

#define SDL_HAT_RIGHT   0x02
#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)

Definition at line 213 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

#define SDL_HAT_RIGHTUP   (SDL_HAT_RIGHT|SDL_HAT_UP)

Definition at line 212 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

#define SDL_HAT_UP   0x01

Typedef Documentation

SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char int SDL_PRINTF_FORMAT_STRING const char const char SDL_SCANF_FORMAT_STRING const char return SDL_ThreadFunction const char void return Uint32 return Uint32 SDL_AssertionHandler void SDL_SpinLock SDL_atomic_t int int return SDL_atomic_t return void void void return void return int return SDL_AudioSpec SDL_AudioSpec return int int return return int SDL_RWops int SDL_AudioSpec Uint8 Uint32 return SDL_AudioCVT SDL_AudioFormat Uint8 int SDL_AudioFormat Uint8 int return Uint8 const Uint8 Uint32 int const char return return return return return return return return Uint32 return Uint32 SDL_Event return SDL_Event int return SDL_EventFilter void SDL_EventFilter void SDL_EventFilter void int return const char const char return SDL_JoystickGUID return int return int return SDL_GameController return int return const char return SDL_GameController SDL_GameControllerAxis return const char return SDL_GameController SDL_GameControllerButton return SDL_GameController SDL_RWops return SDL_TouchID SDL_RWops return int return int return return SDL_Joystick return SDL_Haptic SDL_Haptic return SDL_Haptic return SDL_Haptic SDL_HapticEffect return SDL_Haptic int Uint32 return SDL_Haptic int SDL_Haptic int return SDL_Haptic return SDL_Haptic return SDL_Haptic return SDL_Haptic return const char const char return const char SDL_HintCallback void int return SDL_Joystick return SDL_Joystick return const char return SDL_Joystick return SDL_Joystick return SDL_Joystick return int return SDL_Joystick int return SDL_Joystick int return return return SDL_Scancode return SDL_Scancode return SDL_Keycode return return const char return void int SDL_LogPriority SDL_LogOutputFunction void Uint32 const char const char SDL_Window return int int return SDL_Window int int return SDL_Surface int int return SDL_Cursor return int return SDL_mutex return SDL_mutex return Uint32 return SDL_sem return SDL_sem Uint32 return SDL_sem return SDL_cond SDL_cond return SDL_cond SDL_mutex Uint32 return Uint32 int Uint32 Uint32 Uint32 Uint32 return Uint32 return int return SDL_Palette const SDL_Color int int return const SDL_PixelFormat Uint8 Uint8 Uint8 return Uint32 const SDL_PixelFormat Uint8 Uint8 Uint8 float Uint16 int int return const SDL_Rect const SDL_Rect SDL_Rect return const SDL_Point int const SDL_Rect SDL_Rect return return int int Uint32 SDL_Window SDL_Renderer return SDL_Surface return SDL_Renderer SDL_RendererInfo return SDL_Renderer Uint32 int int int return SDL_Texture Uint32 int int int return SDL_Texture Uint8 Uint8 Uint8 return SDL_Texture Uint8 return SDL_Texture SDL_BlendMode return SDL_Texture const SDL_Rect const Uint8 int const Uint8 int const Uint8 int return SDL_Texture SDL_Renderer SDL_Texture return SDL_Renderer int int return SDL_Renderer const SDL_Rect return SDL_Renderer const SDL_Rect return SDL_Renderer float float return SDL_Renderer Uint8 Uint8 Uint8 Uint8 return SDL_Renderer SDL_BlendMode return SDL_Renderer return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Point int return SDL_Renderer const SDL_Rect int return SDL_Renderer const SDL_Rect int return SDL_Renderer SDL_Texture const SDL_Rect const SDL_Rect const double const SDL_Point const SDL_RendererFlip return SDL_Renderer SDL_Renderer SDL_Texture return void int return return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops return SDL_RWops Uint16 return SDL_RWops Uint32 return SDL_RWops Uint64 return const char unsigned int unsigned int unsigned int unsigned int Uint32 return SDL_Window SDL_Surface SDL_WindowShapeMode return size_t return void size_t return const char return void size_t size_t int(*) a int return int return int size_t return size_t return const wchar_t return const wchar_t size_t return const char size_t return const char size_t return char return char return const char int return int char int return long char int return Sint64 char int return const char return const char char int return const char char int return const char char return const char const char size_t return const char const char size_t return double return double return double return double return double return double return double int return float return const char const char return SDL_iconv_t const char size_t char size_t return Uint32 int int int Uint32 Uint32 Uint32 Uint32 return SDL_Surface SDL_Surface return SDL_RWops int return SDL_Surface int return SDL_Surface Uint32 return SDL_Surface Uint8 Uint8 Uint8 return SDL_Surface Uint8 return SDL_Surface SDL_BlendMode return SDL_Surface SDL_Rect SDL_Surface Uint32 Uint32 return SDL_Surface const SDL_Rect Uint32 return SDL_Surface const SDL_Rect SDL_Surface SDL_Rect return SDL_Surface const SDL_Rect SDL_Surface const SDL_Rect return SDL_Surface SDL_Rect SDL_Surface SDL_Rect return SDL_Thread return SDL_Thread return SDL_Thread int return SDL_TLSID const void void(*) return return Uint32 SDL_TimerID return int return SDL_TouchID int return return return const char return return int return int return int SDL_DisplayMode return int const SDL_DisplayMode SDL_DisplayMode return SDL_Window const SDL_DisplayMode return SDL_Window return const void return Uint32 return SDL_Window const char SDL_Window SDL_Surface SDL_Window const char return SDL_Window int int SDL_Window int int SDL_Window int int SDL_Window int int SDL_Window SDL_Window SDL_Window SDL_Window Uint32 return SDL_Window return SDL_Window SDL_bool SDL_Window float return SDL_Window const Uint16 const Uint16 const Uint16 return SDL_Window const char return SDL_GLattr int return SDL_Window return return SDL_Window int int return SDL_GLContext SDL_RWops int return return void return int int return double return SDL_bool return int int return SDL_AudioDeviceID const void Uint32 return SDL_AudioDeviceID int float float float return SDL_JoystickID

Definition at line 72 of file SDL_joystick.h.

Enumeration Type Documentation

Enumerator
SDL_JOYSTICK_POWER_UNKNOWN 
SDL_JOYSTICK_POWER_EMPTY 
SDL_JOYSTICK_POWER_LOW 
SDL_JOYSTICK_POWER_MEDIUM 
SDL_JOYSTICK_POWER_FULL 
SDL_JOYSTICK_POWER_WIRED 
SDL_JOYSTICK_POWER_MAX 

Definition at line 74 of file SDL_joystick.h.

Function Documentation

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