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

#define SDL_JOYSTICK_AXIS_MAX   32767
 
#define SDL_JOYSTICK_AXIS_MIN   -32768
 
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_JoystickType {
  SDL_JOYSTICK_TYPE_UNKNOWN,
  SDL_JOYSTICK_TYPE_GAMECONTROLLER,
  SDL_JOYSTICK_TYPE_WHEEL,
  SDL_JOYSTICK_TYPE_ARCADE_STICK,
  SDL_JOYSTICK_TYPE_FLIGHT_STICK,
  SDL_JOYSTICK_TYPE_DANCE_PAD,
  SDL_JOYSTICK_TYPE_GUITAR,
  SDL_JOYSTICK_TYPE_DRUM_KIT,
  SDL_JOYSTICK_TYPE_ARCADE_PAD,
  SDL_JOYSTICK_TYPE_THROTTLE
}
 
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_JoystickGUID SDL_JoystickGetDeviceGUID (int device_index)
 
Uint16 SDL_JoystickGetDeviceVendor (int device_index)
 
Uint16 SDL_JoystickGetDeviceProduct (int device_index)
 
Uint16 SDL_JoystickGetDeviceProductVersion (int device_index)
 
SDL_JoystickType SDL_JoystickGetDeviceType (int device_index)
 
SDL_JoystickID SDL_JoystickGetDeviceInstanceID (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_JoystickGetGUID (SDL_Joystick *joystick)
 
Uint16 SDL_JoystickGetVendor (SDL_Joystick *joystick)
 
Uint16 SDL_JoystickGetProduct (SDL_Joystick *joystick)
 
Uint16 SDL_JoystickGetProductVersion (SDL_Joystick *joystick)
 
SDL_JoystickType SDL_JoystickGetType (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)
 
SDL_bool SDL_JoystickGetAxisInitialState (SDL_Joystick *joystick, int axis, Sint16 *state)
 
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

◆ SDL_HAT_CENTERED

◆ SDL_HAT_DOWN

#define SDL_HAT_DOWN   0x04

Definition at line 305 of file SDL_joystick.h.

Referenced by loop(), SDL_SYS_AccelerometerUpdate(), and SDLTest_PrintEvent().

◆ SDL_HAT_LEFT

#define SDL_HAT_LEFT   0x08

Definition at line 306 of file SDL_joystick.h.

Referenced by loop(), SDL_SYS_AccelerometerUpdate(), and SDLTest_PrintEvent().

◆ SDL_HAT_LEFTDOWN

#define SDL_HAT_LEFTDOWN   (SDL_HAT_LEFT|SDL_HAT_DOWN)

Definition at line 310 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

◆ SDL_HAT_LEFTUP

#define SDL_HAT_LEFTUP   (SDL_HAT_LEFT|SDL_HAT_UP)

Definition at line 309 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

◆ SDL_HAT_RIGHT

#define SDL_HAT_RIGHT   0x02

Definition at line 304 of file SDL_joystick.h.

Referenced by loop(), SDL_SYS_AccelerometerUpdate(), and SDLTest_PrintEvent().

◆ SDL_HAT_RIGHTDOWN

#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)

Definition at line 308 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

◆ SDL_HAT_RIGHTUP

#define SDL_HAT_RIGHTUP   (SDL_HAT_RIGHT|SDL_HAT_UP)

Definition at line 307 of file SDL_joystick.h.

Referenced by SDLTest_PrintEvent().

◆ SDL_HAT_UP

#define SDL_HAT_UP   0x01

Definition at line 303 of file SDL_joystick.h.

Referenced by loop(), SDL_SYS_AccelerometerUpdate(), and SDLTest_PrintEvent().

◆ SDL_JOYSTICK_AXIS_MAX

#define SDL_JOYSTICK_AXIS_MAX   32767

◆ SDL_JOYSTICK_AXIS_MIN

#define SDL_JOYSTICK_AXIS_MIN   -32768

Typedef Documentation

◆ SDL_JoystickID

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

This is a unique ID for a joystick for the time it is connected to the system, and is never reused for the lifetime of the application. If the joystick is disconnected and reconnected, it will get a new ID.

The ID value starts at 0 and increments from there. The value -1 is an invalid ID.

Definition at line 81 of file SDL_joystick.h.

Enumeration Type Documentation

◆ SDL_JoystickPowerLevel

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 97 of file SDL_joystick.h.

◆ SDL_JoystickType

Enumerator
SDL_JOYSTICK_TYPE_UNKNOWN 
SDL_JOYSTICK_TYPE_GAMECONTROLLER 
SDL_JOYSTICK_TYPE_WHEEL 
SDL_JOYSTICK_TYPE_ARCADE_STICK 
SDL_JOYSTICK_TYPE_FLIGHT_STICK 
SDL_JOYSTICK_TYPE_DANCE_PAD 
SDL_JOYSTICK_TYPE_GUITAR 
SDL_JOYSTICK_TYPE_DRUM_KIT 
SDL_JOYSTICK_TYPE_ARCADE_PAD 
SDL_JOYSTICK_TYPE_THROTTLE 

Definition at line 83 of file SDL_joystick.h.

Function Documentation

◆ SDL_JoystickClose()

void SDL_JoystickClose ( SDL_Joystick *  joystick)

Close a joystick previously opened with SDL_JoystickOpen().

Definition at line 491 of file SDL_joystick.c.

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

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

492 {
493  SDL_Joystick *joysticklist;
494  SDL_Joystick *joysticklistprev;
495 
496  if (!joystick) {
497  return;
498  }
499 
501 
502  /* First decrement ref count */
503  if (--joystick->ref_count > 0) {
505  return;
506  }
507 
508  if (SDL_updating_joystick) {
510  return;
511  }
512 
513  SDL_SYS_JoystickClose(joystick);
514  joystick->hwdata = NULL;
515 
516  joysticklist = SDL_joysticks;
517  joysticklistprev = NULL;
518  while (joysticklist) {
519  if (joystick == joysticklist) {
520  if (joysticklistprev) {
521  /* unlink this entry */
522  joysticklistprev->next = joysticklist->next;
523  } else {
524  SDL_joysticks = joystick->next;
525  }
526  break;
527  }
528  joysticklistprev = joysticklist;
529  joysticklist = joysticklist->next;
530  }
531 
532  SDL_free(joystick->name);
533 
534  /* Free the data associated with this joystick */
535  SDL_free(joystick->axes);
536  SDL_free(joystick->hats);
537  SDL_free(joystick->balls);
538  SDL_free(joystick->buttons);
539  SDL_free(joystick);
540 
542 }
void SDL_UnlockJoystickList(void)
Definition: SDL_joystick.c:51
void SDL_LockJoystickList(void)
Definition: SDL_joystick.c:43
void SDL_free(void *mem)
#define NULL
Definition: begin_code.h:164
void SDL_SYS_JoystickClose(SDL_Joystick *joystick)
static SDL_bool SDL_updating_joystick
Definition: SDL_joystick.c:39
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:38

◆ SDL_JoystickCurrentPowerLevel()

SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel ( SDL_Joystick *  joystick)

Return the battery level of this joystick

Definition at line 1274 of file SDL_joystick.c.

References SDL_JOYSTICK_POWER_UNKNOWN, and SDL_PrivateJoystickValid().

1275 {
1276  if (!SDL_PrivateJoystickValid(joystick)) {
1277  return (SDL_JOYSTICK_POWER_UNKNOWN);
1278  }
1279  return joystick->epowerlevel;
1280 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ 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 908 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.

909 {
910 #if SDL_EVENTS_DISABLED
911  return SDL_DISABLE;
912 #else
913  const Uint32 event_list[] = {
916  };
917  unsigned int i;
918 
919  switch (state) {
920  case SDL_QUERY:
921  state = SDL_DISABLE;
922  for (i = 0; i < SDL_arraysize(event_list); ++i) {
923  state = SDL_EventState(event_list[i], SDL_QUERY);
924  if (state == SDL_ENABLE) {
925  break;
926  }
927  }
928  break;
929  default:
930  for (i = 0; i < SDL_arraysize(event_list); ++i) {
931  SDL_EventState(event_list[i], state);
932  }
933  break;
934  }
935  return (state);
936 #endif /* SDL_EVENTS_DISABLED */
937 }
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
uint32_t Uint32
Definition: SDL_stdinc.h:181
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:93
#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 459 of file SDL_joystick.c.

References NULL, SDL_joysticks, SDL_LockJoystickList(), and SDL_UnlockJoystickList().

460 {
461  SDL_Joystick *joystick;
462 
464  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
465  if (joystick->instance_id == joyid) {
467  return joystick;
468  }
469  }
471  return NULL;
472 }
void SDL_UnlockJoystickList(void)
Definition: SDL_joystick.c:51
void SDL_LockJoystickList(void)
Definition: SDL_joystick.c:43
#define NULL
Definition: begin_code.h:164
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:38

◆ 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 433 of file SDL_joystick.c.

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

434 {
435  if (!SDL_PrivateJoystickValid(joystick)) {
436  return SDL_FALSE;
437  }
438 
439  return SDL_SYS_JoystickAttached(joystick);
440 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
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 325 of file SDL_joystick.c.

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

326 {
327  Sint16 state;
328 
329  if (!SDL_PrivateJoystickValid(joystick)) {
330  return (0);
331  }
332  if (axis < joystick->naxes) {
333  state = joystick->axes[axis].value;
334  } else {
335  SDL_SetError("Joystick only has %d axes", joystick->naxes);
336  state = 0;
337  }
338  return (state);
339 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
struct xkb_state * state
SDL_Texture * axis
#define SDL_SetError
int16_t Sint16
Definition: SDL_stdinc.h:163

◆ SDL_JoystickGetAxisInitialState()

SDL_bool SDL_JoystickGetAxisInitialState ( SDL_Joystick *  joystick,
int  axis,
Sint16 state 
)

Get the initial 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.

Returns
SDL_TRUE if this axis has any initial value, or SDL_FALSE if not.

Definition at line 345 of file SDL_joystick.c.

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

346 {
347  if (!SDL_PrivateJoystickValid(joystick)) {
348  return SDL_FALSE;
349  }
350  if (axis >= joystick->naxes) {
351  SDL_SetError("Joystick only has %d axes", joystick->naxes);
352  return SDL_FALSE;
353  }
354  if (state) {
355  *state = joystick->axes[axis].initial_value;
356  }
357  return joystick->axes[axis].has_initial_value;
358 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
struct xkb_state * state
SDL_Texture * axis
#define SDL_SetError

◆ 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 384 of file SDL_joystick.c.

References retval, SDL_PrivateJoystickValid(), and SDL_SetError.

385 {
386  int retval;
387 
388  if (!SDL_PrivateJoystickValid(joystick)) {
389  return (-1);
390  }
391 
392  retval = 0;
393  if (ball < joystick->nballs) {
394  if (dx) {
395  *dx = joystick->balls[ball].dx;
396  }
397  if (dy) {
398  *dy = joystick->balls[ball].dy;
399  }
400  joystick->balls[ball].dx = 0;
401  joystick->balls[ball].dy = 0;
402  } else {
403  return SDL_SetError("Joystick only has %d balls", joystick->nballs);
404  }
405  return (retval);
406 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
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 412 of file SDL_joystick.c.

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

413 {
414  Uint8 state;
415 
416  if (!SDL_PrivateJoystickValid(joystick)) {
417  return (0);
418  }
419  if (button < joystick->nbuttons) {
420  state = joystick->buttons[button];
421  } else {
422  SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
423  state = 0;
424  }
425  return (state);
426 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
SDL_Texture * button
struct xkb_state * state
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_SetError

◆ SDL_JoystickGetDeviceGUID()

SDL_JoystickGUID SDL_JoystickGetDeviceGUID ( int  device_index)

Return the GUID for the joystick at this index This can be called before any joysticks are opened.

Definition at line 1084 of file SDL_joystick.c.

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

Referenced by SDL_JoystickGetDeviceProduct(), SDL_JoystickGetDeviceProductVersion(), SDL_JoystickGetDeviceType(), and SDL_JoystickGetDeviceVendor().

1085 {
1086  if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
1087  SDL_JoystickGUID emptyGUID;
1088  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
1089  SDL_zero(emptyGUID);
1090  return emptyGUID;
1091  }
1092  return SDL_SYS_JoystickGetDeviceGUID(device_index);
1093 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
#define SDL_zero(x)
Definition: SDL_stdinc.h:385
#define SDL_SetError
SDL_JoystickGUID SDL_SYS_JoystickGetDeviceGUID(int device_index)

◆ SDL_JoystickGetDeviceInstanceID()

SDL_JoystickID SDL_JoystickGetDeviceInstanceID ( int  device_index)

Get the instance ID of a joystick. This can be called before any joysticks are opened. If the index is out of range, this function will return -1.

Definition at line 1136 of file SDL_joystick.c.

References SDL_NumJoysticks(), SDL_SetError, and SDL_SYS_GetInstanceIdOfDeviceIndex().

Referenced by SDL_JoystickOpen().

1137 {
1138  if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
1139  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
1140  return -1;
1141  }
1142  return SDL_SYS_GetInstanceIdOfDeviceIndex(device_index);
1143 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
#define SDL_SetError
SDL_JoystickID SDL_SYS_GetInstanceIdOfDeviceIndex(int device_index)

◆ SDL_JoystickGetDeviceProduct()

Uint16 SDL_JoystickGetDeviceProduct ( int  device_index)

Get the USB product ID of a joystick, if available. This can be called before any joysticks are opened. If the product ID isn't available this function returns 0.

Definition at line 1104 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetDeviceGUID().

1105 {
1106  Uint16 product;
1107  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1108 
1109  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
1110  return product;
1111 }
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickGetDeviceProductVersion()

Uint16 SDL_JoystickGetDeviceProductVersion ( int  device_index)

Get the product version of a joystick, if available. This can be called before any joysticks are opened. If the product version isn't available this function returns 0.

Definition at line 1113 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetDeviceGUID().

1114 {
1115  Uint16 version;
1116  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1117 
1118  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
1119  return version;
1120 }
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickGetDeviceType()

SDL_JoystickType SDL_JoystickGetDeviceType ( int  device_index)

Get the type of a joystick, if available. This can be called before any joysticks are opened.

Definition at line 1122 of file SDL_joystick.c.

References SDL_GetJoystickGUIDType(), SDL_IsGameController, SDL_JOYSTICK_TYPE_GAMECONTROLLER, SDL_JOYSTICK_TYPE_UNKNOWN, and SDL_JoystickGetDeviceGUID().

1123 {
1125  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1126 
1127  type = SDL_GetJoystickGUIDType(guid);
1128  if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
1129  if (SDL_IsGameController(device_index)) {
1131  }
1132  }
1133  return type;
1134 }
#define SDL_IsGameController
SDL_JoystickType
Definition: SDL_joystick.h:83
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)

◆ SDL_JoystickGetDeviceVendor()

Uint16 SDL_JoystickGetDeviceVendor ( int  device_index)

Get the USB vendor ID of a joystick, if available. This can be called before any joysticks are opened. If the vendor ID isn't available this function returns 0.

Definition at line 1095 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetDeviceGUID().

1096 {
1097  Uint16 vendor;
1098  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1099 
1100  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
1101  return vendor;
1102 }
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickGetGUID()

SDL_JoystickGUID SDL_JoystickGetGUID ( SDL_Joystick *  joystick)

Return the GUID for this opened joystick

Definition at line 1145 of file SDL_joystick.c.

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

Referenced by SDL_JoystickGetProduct(), SDL_JoystickGetProductVersion(), SDL_JoystickGetType(), and SDL_JoystickGetVendor().

1146 {
1147  if (!SDL_PrivateJoystickValid(joystick)) {
1148  SDL_JoystickGUID emptyGUID;
1149  SDL_zero(emptyGUID);
1150  return emptyGUID;
1151  }
1152  return SDL_SYS_JoystickGetGUID(joystick);
1153 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
#define SDL_zero(x)
Definition: SDL_stdinc.h:385
SDL_JoystickGUID SDL_SYS_JoystickGetGUID(SDL_Joystick *joystick)

◆ SDL_JoystickGetGUIDFromString()

SDL_JoystickGUID SDL_JoystickGetGUIDFromString ( const char *  pchGUID)

Convert a string into a joystick guid

Definition at line 1244 of file SDL_joystick.c.

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

1245 {
1246  SDL_JoystickGUID guid;
1247  int maxoutputbytes= sizeof(guid);
1248  size_t len = SDL_strlen(pchGUID);
1249  Uint8 *p;
1250  size_t i;
1251 
1252  /* Make sure it's even */
1253  len = (len) & ~0x1;
1254 
1255  SDL_memset(&guid, 0x00, sizeof(guid));
1256 
1257  p = (Uint8 *)&guid;
1258  for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
1259  *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
1260  }
1261 
1262  return guid;
1263 }
GLuint GLfloat GLfloat GLfloat x1
GLfloat GLfloat p
GLenum GLsizei len
uint8_t Uint8
Definition: SDL_stdinc.h:157
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)
#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 1197 of file SDL_joystick.c.

References SDL_JoystickGUID::data, i, and NULL.

1198 {
1199  static const char k_rgchHexToASCII[] = "0123456789abcdef";
1200  int i;
1201 
1202  if ((pszGUID == NULL) || (cbGUID <= 0)) {
1203  return;
1204  }
1205 
1206  for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
1207  /* each input byte writes 2 ascii chars, and might write a null byte. */
1208  /* If we don't have room for next input byte, stop */
1209  unsigned char c = guid.data[i];
1210 
1211  *pszGUID++ = k_rgchHexToASCII[c >> 4];
1212  *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
1213  }
1214  *pszGUID = '\0';
1215 }
Uint8 data[16]
Definition: SDL_joystick.h:71
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:164

◆ 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 364 of file SDL_joystick.c.

References SDL_PrivateJoystickValid(), SDL_SetError, and state.

365 {
366  Uint8 state;
367 
368  if (!SDL_PrivateJoystickValid(joystick)) {
369  return (0);
370  }
371  if (hat < joystick->nhats) {
372  state = joystick->hats[hat];
373  } else {
374  SDL_SetError("Joystick only has %d hats", joystick->nhats);
375  state = 0;
376  }
377  return (state);
378 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
struct xkb_state * state
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_SetError

◆ SDL_JoystickGetProduct()

Uint16 SDL_JoystickGetProduct ( SDL_Joystick *  joystick)

Get the USB product ID of an opened joystick, if available. If the product ID isn't available this function returns 0.

Definition at line 1164 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetGUID().

Referenced by SDL_JoystickAxesCenteredAtZero().

1165 {
1166  Uint16 product;
1167  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1168 
1169  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
1170  return product;
1171 }
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickGetProductVersion()

Uint16 SDL_JoystickGetProductVersion ( SDL_Joystick *  joystick)

Get the product version of an opened joystick, if available. If the product version isn't available this function returns 0.

Definition at line 1173 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetGUID().

1174 {
1175  Uint16 version;
1176  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1177 
1178  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
1179  return version;
1180 }
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickGetType()

SDL_JoystickType SDL_JoystickGetType ( SDL_Joystick *  joystick)

Get the type of an opened joystick.

Definition at line 1182 of file SDL_joystick.c.

References SDL_GetJoystickGUIDType(), SDL_JOYSTICK_TYPE_GAMECONTROLLER, SDL_JOYSTICK_TYPE_UNKNOWN, and SDL_JoystickGetGUID().

1183 {
1185  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1186 
1187  type = SDL_GetJoystickGUIDType(guid);
1188  if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
1189  if (joystick && joystick->is_game_controller) {
1191  }
1192  }
1193  return type;
1194 }
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
SDL_JoystickType
Definition: SDL_joystick.h:83
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571
static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)

◆ SDL_JoystickGetVendor()

Uint16 SDL_JoystickGetVendor ( SDL_Joystick *  joystick)

Get the USB vendor ID of an opened joystick, if available. If the vendor ID isn't available this function returns 0.

Definition at line 1155 of file SDL_joystick.c.

References NULL, SDL_GetJoystickGUIDInfo(), and SDL_JoystickGetGUID().

Referenced by SDL_JoystickAxesCenteredAtZero().

1156 {
1157  Uint16 vendor;
1158  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1159 
1160  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
1161  return vendor;
1162 }
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:939
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ 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 446 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

447 {
448  if (!SDL_PrivateJoystickValid(joystick)) {
449  return (-1);
450  }
451 
452  return (joystick->instance_id);
453 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ 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 478 of file SDL_joystick.c.

References NULL, and SDL_PrivateJoystickValid().

479 {
480  if (!SDL_PrivateJoystickValid(joystick)) {
481  return (NULL);
482  }
483 
484  return (joystick->name);
485 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
#define NULL
Definition: begin_code.h:164

◆ 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 111 of file SDL_joystick.c.

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

112 {
113  if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
114  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
115  return (NULL);
116  }
117  return (SDL_SYS_JoystickNameForDeviceIndex(device_index));
118 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
#define NULL
Definition: begin_code.h:164
#define SDL_SetError

◆ SDL_JoystickNumAxes()

int SDL_JoystickNumAxes ( SDL_Joystick *  joystick)

Get the number of general axis controls on a joystick.

Definition at line 277 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

278 {
279  if (!SDL_PrivateJoystickValid(joystick)) {
280  return (-1);
281  }
282  return (joystick->naxes);
283 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ 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 301 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

302 {
303  if (!SDL_PrivateJoystickValid(joystick)) {
304  return (-1);
305  }
306  return (joystick->nballs);
307 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ SDL_JoystickNumButtons()

int SDL_JoystickNumButtons ( SDL_Joystick *  joystick)

Get the number of buttons on a joystick.

Definition at line 313 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

314 {
315  if (!SDL_PrivateJoystickValid(joystick)) {
316  return (-1);
317  }
318  return (joystick->nbuttons);
319 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ SDL_JoystickNumHats()

int SDL_JoystickNumHats ( SDL_Joystick *  joystick)

Get the number of POV hats on a joystick.

Definition at line 289 of file SDL_joystick.c.

References SDL_PrivateJoystickValid().

290 {
291  if (!SDL_PrivateJoystickValid(joystick)) {
292  return (-1);
293  }
294  return (joystick->nhats);
295 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259

◆ 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 160 of file SDL_joystick.c.

References i, SDL_GameController::joystick, NULL, SDL_calloc(), SDL_free(), SDL_IsGameController, SDL_JOYSTICK_POWER_UNKNOWN, SDL_JoystickAxesCenteredAtZero(), SDL_JoystickClose(), SDL_JoystickGetDeviceInstanceID(), SDL_joysticks, SDL_LockJoystickList(), SDL_NumJoysticks(), SDL_OutOfMemory, SDL_SetError, SDL_strdup, SDL_SYS_JoystickNameForDeviceIndex(), SDL_SYS_JoystickOpen(), SDL_SYS_JoystickUpdate(), SDL_TRUE, and SDL_UnlockJoystickList().

161 {
162  SDL_Joystick *joystick;
163  SDL_Joystick *joysticklist;
164  const char *joystickname = NULL;
165 
166  if ((device_index < 0) || (device_index >= SDL_NumJoysticks())) {
167  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
168  return (NULL);
169  }
170 
172 
173  joysticklist = SDL_joysticks;
174  /* If the joystick is already open, return it
175  * it is important that we have a single joystick * for each instance id
176  */
177  while (joysticklist) {
178  if (SDL_JoystickGetDeviceInstanceID(device_index) == joysticklist->instance_id) {
179  joystick = joysticklist;
180  ++joystick->ref_count;
182  return (joystick);
183  }
184  joysticklist = joysticklist->next;
185  }
186 
187  /* Create and initialize the joystick */
188  joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
189  if (joystick == NULL) {
190  SDL_OutOfMemory();
192  return NULL;
193  }
194 
195  if (SDL_SYS_JoystickOpen(joystick, device_index) < 0) {
196  SDL_free(joystick);
198  return NULL;
199  }
200 
201  joystickname = SDL_SYS_JoystickNameForDeviceIndex(device_index);
202  if (joystickname)
203  joystick->name = SDL_strdup(joystickname);
204  else
205  joystick->name = NULL;
206 
207  if (joystick->naxes > 0) {
208  joystick->axes = (SDL_JoystickAxisInfo *) SDL_calloc(joystick->naxes, sizeof(SDL_JoystickAxisInfo));
209  }
210  if (joystick->nhats > 0) {
211  joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
212  }
213  if (joystick->nballs > 0) {
214  joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
215  }
216  if (joystick->nbuttons > 0) {
217  joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
218  }
219  if (((joystick->naxes > 0) && !joystick->axes)
220  || ((joystick->nhats > 0) && !joystick->hats)
221  || ((joystick->nballs > 0) && !joystick->balls)
222  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
223  SDL_OutOfMemory();
224  SDL_JoystickClose(joystick);
226  return NULL;
227  }
228  joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
229 
230  /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
231  if (SDL_JoystickAxesCenteredAtZero(joystick)) {
232  int i;
233 
234  for (i = 0; i < joystick->naxes; ++i) {
235  joystick->axes[i].has_initial_value = SDL_TRUE;
236  }
237  }
238 
239  joystick->is_game_controller = SDL_IsGameController(device_index);
240 
241  /* Add joystick to list */
242  ++joystick->ref_count;
243  /* Link the joystick in the list */
244  joystick->next = SDL_joysticks;
245  SDL_joysticks = joystick;
246 
248 
249  SDL_SYS_JoystickUpdate(joystick);
250 
251  return (joystick);
252 }
void SDL_UnlockJoystickList(void)
Definition: SDL_joystick.c:51
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:491
#define SDL_IsGameController
void SDL_LockJoystickList(void)
Definition: SDL_joystick.c:43
void * SDL_calloc(size_t nmemb, size_t size)
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
uint8_t Uint8
Definition: SDL_stdinc.h:157
void SDL_free(void *mem)
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
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
SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
#define NULL
Definition: begin_code.h:164
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define SDL_SetError
#define SDL_strdup
static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
Definition: SDL_joystick.c:126
int SDL_SYS_JoystickOpen(SDL_Joystick *joystick, int device_index)
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:38

◆ 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 846 of file SDL_joystick.c.

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

847 {
848  SDL_Joystick *joystick;
849 
851 
852  if (SDL_updating_joystick) {
853  /* The joysticks are already being updated */
855  return;
856  }
857 
859 
860  /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
862 
863  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
864  SDL_SYS_JoystickUpdate(joystick);
865 
866  if (joystick->force_recentering) {
867  int i;
868 
869  /* Tell the app that everything is centered/unpressed... */
870  for (i = 0; i < joystick->naxes; i++) {
871  if (joystick->axes[i].has_initial_value) {
872  SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
873  }
874  }
875 
876  for (i = 0; i < joystick->nbuttons; i++) {
877  SDL_PrivateJoystickButton(joystick, i, 0);
878  }
879 
880  for (i = 0; i < joystick->nhats; i++) {
882  }
883 
884  joystick->force_recentering = SDL_FALSE;
885  }
886  }
887 
889 
891 
892  /* If any joysticks were closed while updating, free them here */
893  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
894  if (joystick->ref_count <= 0) {
895  SDL_JoystickClose(joystick);
896  }
897  }
898 
899  /* this needs to happen AFTER walking the joystick list above, so that any
900  dangling hardware data from removed devices can be free'd
901  */
903 
905 }
void SDL_UnlockJoystickList(void)
Definition: SDL_joystick.c:51
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:491
int SDL_PrivateJoystickHat(SDL_Joystick *joystick, Uint8 hat, Uint8 value)
Definition: SDL_joystick.c:718
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:794
void SDL_LockJoystickList(void)
Definition: SDL_joystick.c:43
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:661
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 SDL_HAT_CENTERED
Definition: SDL_joystick.h:302
static SDL_bool SDL_updating_joystick
Definition: SDL_joystick.c:39
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:38
void SDL_SYS_JoystickDetect(void)

◆ SDL_NumJoysticks()

int SDL_NumJoysticks ( void  )

Count the number of joysticks attached to the system right now

Definition at line 102 of file SDL_joystick.c.

References SDL_SYS_NumJoysticks().

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

103 {
104  return SDL_SYS_NumJoysticks();
105 }
int SDL_SYS_NumJoysticks(void)