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 "../video/SDL_sysvideo.h"
+ Include dependency graph for SDL_joystick.c:

Go to the source code of this file.

Functions

void SDL_LockJoysticks (void)
 
void SDL_UnlockJoysticks (void)
 
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)
 
static SDL_bool SDL_JoystickAxesCenteredAtZero (SDL_Joystick *joystick)
 
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)
 
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)
 
SDL_bool SDL_JoystickGetAttached (SDL_Joystick *joystick)
 
SDL_JoystickID SDL_JoystickInstanceID (SDL_Joystick *joystick)
 
SDL_Joystick * SDL_JoystickFromInstanceID (SDL_JoystickID joyid)
 
const char * SDL_JoystickName (SDL_Joystick *joystick)
 
void SDL_JoystickClose (SDL_Joystick *joystick)
 
void SDL_JoystickQuit (void)
 
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent ()
 
void SDL_PrivateJoystickAdded (int device_index)
 
static void UpdateEventsForDeviceRemoval ()
 
void SDL_PrivateJoystickRemoved (SDL_JoystickID device_instance)
 
int SDL_PrivateJoystickAxis (SDL_Joystick *joystick, Uint8 axis, Sint16 value)
 
int SDL_PrivateJoystickHat (SDL_Joystick *joystick, Uint8 hat, Uint8 value)
 
int SDL_PrivateJoystickBall (SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
 
int SDL_PrivateJoystickButton (SDL_Joystick *joystick, Uint8 button, Uint8 state)
 
void SDL_JoystickUpdate (void)
 
int SDL_JoystickEventState (int state)
 
void SDL_GetJoystickGUIDInfo (SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
 
static SDL_bool SDL_IsJoystickProductWheel (Uint32 vidpid)
 
static SDL_bool SDL_IsJoystickProductFlightStick (Uint32 vidpid)
 
static SDL_bool SDL_IsJoystickProductThrottle (Uint32 vidpid)
 
static SDL_JoystickType SDL_GetJoystickGUIDType (SDL_JoystickGUID guid)
 
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_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)
 
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_bool SDL_updating_joystick = SDL_FALSE
 
static SDL_mutexSDL_joystick_lock = NULL
 

Function Documentation

◆ nibble()

static unsigned char nibble ( char  c)
static

Definition at line 1217 of file SDL_joystick.c.

Referenced by SDL_JoystickGetGUIDFromString().

1218 {
1219  if ((c >= '0') && (c <= '9')) {
1220  return (unsigned char)(c - '0');
1221  }
1222 
1223  if ((c >= 'A') && (c <= 'F')) {
1224  return (unsigned char)(c - 'A' + 0x0a);
1225  }
1226 
1227  if ((c >= 'a') && (c <= 'f')) {
1228  return (unsigned char)(c - 'a' + 0x0a);
1229  }
1230 
1231  /* received an invalid character, and no real way to return an error */
1232  /* AssertMsg1(false, "Q_nibble invalid hex character '%c' ", c); */
1233  return 0;
1234 }
const GLubyte * c

◆ SDL_GetJoystickGUIDInfo()

void SDL_GetJoystickGUIDInfo ( SDL_JoystickGUID  guid,
Uint16 vendor,
Uint16 product,
Uint16 version 
)

Definition at line 933 of file SDL_joystick.c.

References SDL_JoystickGUID::data.

Referenced by SDL_GetJoystickGUIDType(), SDL_JoystickGetDeviceProduct(), SDL_JoystickGetDeviceProductVersion(), SDL_JoystickGetDeviceVendor(), SDL_JoystickGetProduct(), SDL_JoystickGetProductVersion(), SDL_JoystickGetVendor(), and SDL_ShouldIgnoreGameController().

934 {
935  Uint16 *guid16 = (Uint16 *)guid.data;
936 
937  /* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */
938  if (/* guid16[0] is device bus type */
939  guid16[1] == 0x0000 &&
940  /* guid16[2] is vendor ID */
941  guid16[3] == 0x0000 &&
942  /* guid16[4] is product ID */
943  guid16[5] == 0x0000
944  /* guid16[6] is product version */
945  ) {
946  if (vendor) {
947  *vendor = guid16[2];
948  }
949  if (product) {
950  *product = guid16[4];
951  }
952  if (version) {
953  *version = guid16[6];
954  }
955  } else {
956  if (vendor) {
957  *vendor = 0;
958  }
959  if (product) {
960  *product = 0;
961  }
962  if (version) {
963  *version = 0;
964  }
965  }
966 }
Uint8 data[16]
Definition: SDL_joystick.h:71
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_GetJoystickGUIDType()

static SDL_JoystickType SDL_GetJoystickGUIDType ( SDL_JoystickGUID  guid)
static

Definition at line 1027 of file SDL_joystick.c.

References SDL_JoystickGUID::data, MAKE_VIDPID, NULL, SDL_GetJoystickGUIDInfo(), SDL_IsJoystickProductFlightStick(), SDL_IsJoystickProductThrottle(), SDL_IsJoystickProductWheel(), SDL_JOYSTICK_TYPE_ARCADE_PAD, SDL_JOYSTICK_TYPE_ARCADE_STICK, SDL_JOYSTICK_TYPE_DANCE_PAD, SDL_JOYSTICK_TYPE_DRUM_KIT, SDL_JOYSTICK_TYPE_FLIGHT_STICK, SDL_JOYSTICK_TYPE_GAMECONTROLLER, SDL_JOYSTICK_TYPE_GUITAR, SDL_JOYSTICK_TYPE_THROTTLE, SDL_JOYSTICK_TYPE_UNKNOWN, and SDL_JOYSTICK_TYPE_WHEEL.

Referenced by SDL_JoystickGetDeviceType(), and SDL_JoystickGetType().

1028 {
1029  Uint16 vendor;
1030  Uint16 product;
1031  Uint32 vidpid;
1032 
1033  if (guid.data[14] == 'x') {
1034  /* XInput GUID, get the type based on the XInput device subtype */
1035  switch (guid.data[15]) {
1036  case 0x01: /* XINPUT_DEVSUBTYPE_GAMEPAD */
1038  case 0x02: /* XINPUT_DEVSUBTYPE_WHEEL */
1039  return SDL_JOYSTICK_TYPE_WHEEL;
1040  case 0x03: /* XINPUT_DEVSUBTYPE_ARCADE_STICK */
1042  case 0x04: /* XINPUT_DEVSUBTYPE_FLIGHT_STICK */
1044  case 0x05: /* XINPUT_DEVSUBTYPE_DANCE_PAD */
1046  case 0x06: /* XINPUT_DEVSUBTYPE_GUITAR */
1047  case 0x07: /* XINPUT_DEVSUBTYPE_GUITAR_ALTERNATE */
1048  case 0x0B: /* XINPUT_DEVSUBTYPE_GUITAR_BASS */
1049  return SDL_JOYSTICK_TYPE_GUITAR;
1050  case 0x08: /* XINPUT_DEVSUBTYPE_DRUM_KIT */
1052  case 0x13: /* XINPUT_DEVSUBTYPE_ARCADE_PAD */
1054  default:
1056  }
1057  }
1058 
1059  SDL_GetJoystickGUIDInfo(guid, &vendor, &product, NULL);
1060  vidpid = MAKE_VIDPID(vendor, product);
1061 
1062  if (SDL_IsJoystickProductWheel(vidpid)) {
1063  return SDL_JOYSTICK_TYPE_WHEEL;
1064  }
1065 
1066  if (SDL_IsJoystickProductFlightStick(vidpid)) {
1068  }
1069 
1070  if (SDL_IsJoystickProductThrottle(vidpid)) {
1072  }
1073 
1075 }
static SDL_bool SDL_IsJoystickProductWheel(Uint32 vidpid)
Definition: SDL_joystick.c:968
#define MAKE_VIDPID(VID, PID)
uint32_t Uint32
Definition: SDL_stdinc.h:181
Uint8 data[16]
Definition: SDL_joystick.h:71
static SDL_bool SDL_IsJoystickProductFlightStick(Uint32 vidpid)
Definition: SDL_joystick.c:995
#define NULL
Definition: begin_code.h:164
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
Definition: SDL_joystick.c:933
uint16_t Uint16
Definition: SDL_stdinc.h:169
static SDL_bool SDL_IsJoystickProductThrottle(Uint32 vidpid)

◆ SDL_IsJoystickProductFlightStick()

static SDL_bool SDL_IsJoystickProductFlightStick ( Uint32  vidpid)
static

Definition at line 995 of file SDL_joystick.c.

References i, MAKE_VIDPID, SDL_arraysize, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_GetJoystickGUIDType().

996 {
997  static Uint32 flightstick_joysticks[] = {
998  MAKE_VIDPID(0x044f, 0x0402), /* HOTAS Warthog Joystick */
999  MAKE_VIDPID(0x0738, 0x2221), /* Saitek Pro Flight X-56 Rhino Stick */
1000  };
1001  int i;
1002 
1003  for (i = 0; i < SDL_arraysize(flightstick_joysticks); ++i) {
1004  if (vidpid == flightstick_joysticks[i]) {
1005  return SDL_TRUE;
1006  }
1007  }
1008  return SDL_FALSE;
1009 }
#define MAKE_VIDPID(VID, PID)
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_arraysize(array)
Definition: SDL_stdinc.h:93

◆ SDL_IsJoystickProductThrottle()

static SDL_bool SDL_IsJoystickProductThrottle ( Uint32  vidpid)
static

Definition at line 1011 of file SDL_joystick.c.

References i, MAKE_VIDPID, SDL_arraysize, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_GetJoystickGUIDType().

1012 {
1013  static Uint32 throttle_joysticks[] = {
1014  MAKE_VIDPID(0x044f, 0x0404), /* HOTAS Warthog Throttle */
1015  MAKE_VIDPID(0x0738, 0xa221), /* Saitek Pro Flight X-56 Rhino Throttle */
1016  };
1017  int i;
1018 
1019  for (i = 0; i < SDL_arraysize(throttle_joysticks); ++i) {
1020  if (vidpid == throttle_joysticks[i]) {
1021  return SDL_TRUE;
1022  }
1023  }
1024  return SDL_FALSE;
1025 }
#define MAKE_VIDPID(VID, PID)
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_arraysize(array)
Definition: SDL_stdinc.h:93

◆ SDL_IsJoystickProductWheel()

static SDL_bool SDL_IsJoystickProductWheel ( Uint32  vidpid)
static

Definition at line 968 of file SDL_joystick.c.

References i, MAKE_VIDPID, SDL_arraysize, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_GetJoystickGUIDType().

969 {
970  static Uint32 wheel_joysticks[] = {
971  MAKE_VIDPID(0x046d, 0xc294), /* Logitech generic wheel */
972  MAKE_VIDPID(0x046d, 0xc295), /* Logitech Momo Force */
973  MAKE_VIDPID(0x046d, 0xc298), /* Logitech Driving Force Pro */
974  MAKE_VIDPID(0x046d, 0xc299), /* Logitech G25 */
975  MAKE_VIDPID(0x046d, 0xc29a), /* Logitech Driving Force GT */
976  MAKE_VIDPID(0x046d, 0xc29b), /* Logitech G27 */
977  MAKE_VIDPID(0x046d, 0xc261), /* Logitech G920 (initial mode) */
978  MAKE_VIDPID(0x046d, 0xc262), /* Logitech G920 (active mode) */
979  MAKE_VIDPID(0x044f, 0xb65d), /* Thrustmaster Wheel FFB */
980  MAKE_VIDPID(0x044f, 0xb66d), /* Thrustmaster Wheel FFB */
981  MAKE_VIDPID(0x044f, 0xb677), /* Thrustmaster T150 */
982  MAKE_VIDPID(0x044f, 0xb664), /* Thrustmaster TX (initial mode) */
983  MAKE_VIDPID(0x044f, 0xb669), /* Thrustmaster TX (active mode) */
984  };
985  int i;
986 
987  for (i = 0; i < SDL_arraysize(wheel_joysticks); ++i) {
988  if (vidpid == wheel_joysticks[i]) {
989  return SDL_TRUE;
990  }
991  }
992  return SDL_FALSE;
993 }
#define MAKE_VIDPID(VID, PID)
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_arraysize(array)
Definition: SDL_stdinc.h:93

◆ SDL_JoystickAllowBackgroundEventsChanged()

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

Definition at line 60 of file SDL_joystick.c.

References SDL_FALSE, SDL_joystick_allows_background_events, and SDL_TRUE.

Referenced by SDL_JoystickInit(), and SDL_JoystickQuit().

61 {
62  if (hint && *hint == '1') {
64  } else {
66  }
67 }
static SDL_bool SDL_joystick_allows_background_events
Definition: SDL_joystick.c:37

◆ SDL_JoystickAxesCenteredAtZero()

static SDL_bool SDL_JoystickAxesCenteredAtZero ( SDL_Joystick *  joystick)
static

Definition at line 126 of file SDL_joystick.c.

References i, MAKE_VIDPID, SDL_arraysize, SDL_FALSE, SDL_JoystickGetProduct(), SDL_JoystickGetVendor(), and SDL_TRUE.

Referenced by SDL_JoystickOpen().

127 {
128  static Uint32 zero_centered_joysticks[] = {
129  MAKE_VIDPID(0x0e8f, 0x3013), /* HuiJia SNES USB adapter */
130  MAKE_VIDPID(0x05a0, 0x3232), /* 8Bitdo Zero Gamepad */
131  };
132 
133  int i;
135  SDL_JoystickGetProduct(joystick));
136 
137 /*printf("JOYSTICK '%s' VID/PID 0x%.4x/0x%.4x AXES: %d\n", joystick->name, vendor, product, joystick->naxes);*/
138 
139  if (joystick->naxes == 2) {
140  /* Assume D-pad or thumbstick style axes are centered at 0 */
141  return SDL_TRUE;
142  }
143 
144  for (i = 0; i < SDL_arraysize(zero_centered_joysticks); ++i) {
145  if (id == zero_centered_joysticks[i]) {
146  return SDL_TRUE;
147  }
148  }
149  return SDL_FALSE;
150 }
Uint16 SDL_JoystickGetVendor(SDL_Joystick *joystick)
#define MAKE_VIDPID(VID, PID)
Uint16 SDL_JoystickGetProduct(SDL_Joystick *joystick)
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_arraysize(array)
Definition: SDL_stdinc.h:93

◆ 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_LockJoysticks(), SDL_SYS_JoystickClose(), SDL_UnlockJoysticks(), 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_LockJoysticks(void)
Definition: SDL_joystick.c:43
#define SDL_free
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:51
#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 1268 of file SDL_joystick.c.

References SDL_JOYSTICK_POWER_UNKNOWN, and SDL_PrivateJoystickValid().

1269 {
1270  if (!SDL_PrivateJoystickValid(joystick)) {
1271  return (SDL_JOYSTICK_POWER_UNKNOWN);
1272  }
1273  return joystick->epowerlevel;
1274 }
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 902 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.

903 {
904 #if SDL_EVENTS_DISABLED
905  return SDL_DISABLE;
906 #else
907  const Uint32 event_list[] = {
910  };
911  unsigned int i;
912 
913  switch (state) {
914  case SDL_QUERY:
915  state = SDL_DISABLE;
916  for (i = 0; i < SDL_arraysize(event_list); ++i) {
917  state = SDL_EventState(event_list[i], SDL_QUERY);
918  if (state == SDL_ENABLE) {
919  break;
920  }
921  }
922  break;
923  default:
924  for (i = 0; i < SDL_arraysize(event_list); ++i) {
925  SDL_EventState(event_list[i], state);
926  }
927  break;
928  }
929  return (state);
930 #endif /* SDL_EVENTS_DISABLED */
931 }
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_LockJoysticks(), and SDL_UnlockJoysticks().

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_LockJoysticks(void)
Definition: SDL_joystick.c:43
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:51
#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 1078 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().

1079 {
1080  if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
1081  SDL_JoystickGUID emptyGUID;
1082  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
1083  SDL_zero(emptyGUID);
1084  return emptyGUID;
1085  }
1086  return SDL_SYS_JoystickGetDeviceGUID(device_index);
1087 }
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
#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 1130 of file SDL_joystick.c.

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

Referenced by SDL_JoystickOpen().

1131 {
1132  if (device_index < 0 || device_index >= SDL_NumJoysticks()) {
1133  SDL_SetError("There are %d joysticks available", SDL_NumJoysticks());
1134  return -1;
1135  }
1136  return SDL_SYS_GetInstanceIdOfDeviceIndex(device_index);
1137 }
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 1098 of file SDL_joystick.c.

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

1099 {
1100  Uint16 product;
1101  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1102 
1103  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
1104  return product;
1105 }
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:933
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 1107 of file SDL_joystick.c.

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

1108 {
1109  Uint16 version;
1110  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1111 
1112  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
1113  return version;
1114 }
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:933
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 1116 of file SDL_joystick.c.

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

1117 {
1119  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1120 
1121  type = SDL_GetJoystickGUIDType(guid);
1122  if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
1123  if (SDL_IsGameController(device_index)) {
1125  }
1126  }
1127  return type;
1128 }
#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 1089 of file SDL_joystick.c.

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

1090 {
1091  Uint16 vendor;
1092  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
1093 
1094  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
1095  return vendor;
1096 }
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:933
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 1139 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().

1140 {
1141  if (!SDL_PrivateJoystickValid(joystick)) {
1142  SDL_JoystickGUID emptyGUID;
1143  SDL_zero(emptyGUID);
1144  return emptyGUID;
1145  }
1146  return SDL_SYS_JoystickGetGUID(joystick);
1147 }
int SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:259
#define SDL_zero(x)
Definition: SDL_stdinc.h:416
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 1238 of file SDL_joystick.c.

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

1239 {
1240  SDL_JoystickGUID guid;
1241  int maxoutputbytes= sizeof(guid);
1242  size_t len = SDL_strlen(pchGUID);
1243  Uint8 *p;
1244  size_t i;
1245 
1246  /* Make sure it's even */
1247  len = (len) & ~0x1;
1248 
1249  SDL_memset(&guid, 0x00, sizeof(guid));
1250 
1251  p = (Uint8 *)&guid;
1252  for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
1253  *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
1254  }
1255 
1256  return guid;
1257 }
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 1191 of file SDL_joystick.c.

References SDL_JoystickGUID::data, i, and NULL.

1192 {
1193  static const char k_rgchHexToASCII[] = "0123456789abcdef";
1194  int i;
1195 
1196  if ((pszGUID == NULL) || (cbGUID <= 0)) {
1197  return;
1198  }
1199 
1200  for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
1201  /* each input byte writes 2 ascii chars, and might write a null byte. */
1202  /* If we don't have room for next input byte, stop */
1203  unsigned char c = guid.data[i];
1204 
1205  *pszGUID++ = k_rgchHexToASCII[c >> 4];
1206  *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
1207  }
1208  *pszGUID = '\0';
1209 }
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 1158 of file SDL_joystick.c.

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

Referenced by SDL_JoystickAxesCenteredAtZero().

1159 {
1160  Uint16 product;
1161  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1162 
1163  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
1164  return product;
1165 }
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:933
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 1167 of file SDL_joystick.c.

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

1168 {
1169  Uint16 version;
1170  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1171 
1172  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
1173  return version;
1174 }
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:933
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 1176 of file SDL_joystick.c.

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

1177 {
1179  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1180 
1181  type = SDL_GetJoystickGUIDType(guid);
1182  if (type == SDL_JOYSTICK_TYPE_UNKNOWN) {
1183  if (joystick && joystick->is_game_controller) {
1185  }
1186  }
1187  return type;
1188 }
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 1149 of file SDL_joystick.c.

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

Referenced by SDL_JoystickAxesCenteredAtZero().

1150 {
1151  Uint16 vendor;
1152  SDL_JoystickGUID guid = SDL_JoystickGetGUID(joystick);
1153 
1154  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
1155  return vendor;
1156 }
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:933
uint16_t Uint16
Definition: SDL_stdinc.h:169

◆ SDL_JoystickInit()

int SDL_JoystickInit ( void  )

Definition at line 70 of file SDL_joystick.c.

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

Referenced by SDL_InitSubSystem().

71 {
72  int status;
73 
75 
76  /* Create the joystick list lock */
77  if (!SDL_joystick_lock) {
79  }
80 
81  /* See if we should allow joystick events while in the background */
84 
85 #if !SDL_EVENTS_DISABLED
87  return -1;
88  }
89 #endif /* !SDL_EVENTS_DISABLED */
90 
91  status = SDL_SYS_JoystickInit();
92  if (status >= 0) {
93  status = 0;
94  }
95  return (status);
96 }
#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:466
#define SDL_INIT_EVENTS
Definition: SDL.h:82
int SDL_GameControllerInitMappings(void)
static SDL_mutex * SDL_joystick_lock
Definition: SDL_joystick.c:40
#define SDL_CreateMutex
#define SDL_InitSubSystem
int SDL_SYS_JoystickInit(void)
#define NULL
Definition: begin_code.h:164
#define SDL_AddHintCallback
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
Definition: SDL_joystick.c:60

◆ 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_LockJoysticks(), SDL_NumJoysticks(), SDL_OutOfMemory, SDL_SetError, SDL_strdup, SDL_SYS_JoystickNameForDeviceIndex(), SDL_SYS_JoystickOpen(), SDL_SYS_JoystickUpdate(), SDL_TRUE, and SDL_UnlockJoysticks().

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_LockJoysticks(void)
Definition: SDL_joystick.c:43
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:491
#define SDL_IsGameController
int SDL_NumJoysticks(void)
Definition: SDL_joystick.c:102
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_free
const char * SDL_SYS_JoystickNameForDeviceIndex(int device_index)
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:51
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_calloc
#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_JoystickQuit()

void SDL_JoystickQuit ( void  )

Definition at line 545 of file SDL_joystick.c.

References NULL, SDL_assert, SDL_DelHintCallback, SDL_DestroyMutex, SDL_GameControllerQuitMappings(), SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, SDL_INIT_EVENTS, SDL_JoystickAllowBackgroundEventsChanged(), SDL_JoystickClose(), SDL_joysticks, SDL_LockJoysticks(), SDL_QuitSubSystem, SDL_SYS_JoystickQuit(), SDL_UnlockJoysticks(), and SDL_updating_joystick.

Referenced by SDL_QuitSubSystem().

546 {
547  /* Make sure we're not getting called in the middle of updating joysticks */
549 
551 
552  /* Stop the event polling */
553  while (SDL_joysticks) {
554  SDL_joysticks->ref_count = 1;
556  }
557 
558  /* Quit the joystick setup */
560 
562 
563 #if !SDL_EVENTS_DISABLED
565 #endif
566 
569 
570  if (SDL_joystick_lock) {
573  }
574 
576 }
#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:466
#define SDL_INIT_EVENTS
Definition: SDL.h:82
void SDL_LockJoysticks(void)
Definition: SDL_joystick.c:43
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:491
void SDL_GameControllerQuitMappings(void)
static SDL_mutex * SDL_joystick_lock
Definition: SDL_joystick.c:40
#define SDL_QuitSubSystem
void SDL_SYS_JoystickQuit(void)
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:51
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164
#define SDL_DestroyMutex
#define SDL_DelHintCallback
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
Definition: SDL_joystick.c:60
static SDL_bool SDL_updating_joystick
Definition: SDL_joystick.c:39
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 840 of file SDL_joystick.c.

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

841 {
842  SDL_Joystick *joystick;
843 
845 
846  if (SDL_updating_joystick) {
847  /* The joysticks are already being updated */
849  return;
850  }
851 
853 
854  /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
856 
857  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
858  SDL_SYS_JoystickUpdate(joystick);
859 
860  if (joystick->force_recentering) {
861  int i;
862 
863  /* Tell the app that everything is centered/unpressed... */
864  for (i = 0; i < joystick->naxes; i++) {
865  if (joystick->axes[i].has_initial_value) {
866  SDL_PrivateJoystickAxis(joystick, i, joystick->axes[i].zero);
867  }
868  }
869 
870  for (i = 0; i < joystick->nbuttons; i++) {
871  SDL_PrivateJoystickButton(joystick, i, 0);
872  }
873 
874  for (i = 0; i < joystick->nhats; i++) {
876  }
877 
878  joystick->force_recentering = SDL_FALSE;
879  }
880  }
881 
883 
885 
886  /* If any joysticks were closed while updating, free them here */
887  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
888  if (joystick->ref_count <= 0) {
889  SDL_JoystickClose(joystick);
890  }
891  }
892 
893  /* this needs to happen AFTER walking the joystick list above, so that any
894  dangling hardware data from removed devices can be free'd
895  */
897 
899 }
void SDL_LockJoysticks(void)
Definition: SDL_joystick.c:43
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:712
int SDL_PrivateJoystickButton(SDL_Joystick *joystick, Uint8 button, Uint8 state)
Definition: SDL_joystick.c:788
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:655
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:51
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:316
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_LockJoysticks()

void SDL_LockJoysticks ( void  )

Locking for multi-threaded access to the joystick API

If you are using the joystick API or handling events from multiple threads you should use these locking functions to protect access to the joysticks.

In particular, you are guaranteed that the joystick list won't change, so the API functions that take a joystick index will be valid, and joystick and game controller events will not be delivered.

Definition at line 43 of file SDL_joystick.c.

References SDL_LockMutex.

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

44 {
45  if (SDL_joystick_lock) {
47  }
48 }
#define SDL_LockMutex
static SDL_mutex * SDL_joystick_lock
Definition: SDL_joystick.c:40

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

◆ SDL_PrivateJoystickAdded()

void SDL_PrivateJoystickAdded ( int  device_index)

Definition at line 595 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_JOYDEVICEADDED, SDL_PushEvent, and SDL_Event::type.

Referenced by SDL_SYS_AddJoystickDevice(), and SteamControllerConnectedCallback().

596 {
597 #if !SDL_EVENTS_DISABLED
599 
600  event.type = SDL_JOYDEVICEADDED;
601 
602  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
603  event.jdevice.which = device_index;
604  SDL_PushEvent(&event);
605  }
606 #endif /* !SDL_EVENTS_DISABLED */
607 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickAxis()

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

Definition at line 655 of file SDL_joystick.c.

References axis, SDL_abs, SDL_ENABLE, SDL_GetEventState, SDL_JOYAXISMOTION, SDL_JOYSTICK_AXIS_MAX, SDL_PrivateJoystickAxis(), SDL_PrivateJoystickShouldIgnoreEvent(), SDL_PushEvent, and SDL_TRUE.

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

656 {
657  int posted;
658 
659  /* Make sure we're not getting garbage or duplicate events */
660  if (axis >= joystick->naxes) {
661  return 0;
662  }
663  if (!joystick->axes[axis].has_initial_value) {
664  joystick->axes[axis].initial_value = value;
665  joystick->axes[axis].value = value;
666  joystick->axes[axis].zero = value;
667  joystick->axes[axis].has_initial_value = SDL_TRUE;
668  }
669  if (value == joystick->axes[axis].value) {
670  return 0;
671  }
672  if (!joystick->axes[axis].sent_initial_value) {
673  /* Make sure we don't send motion until there's real activity on this axis */
674  const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */
675  if (SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
676  return 0;
677  }
678  joystick->axes[axis].sent_initial_value = SDL_TRUE;
679  joystick->axes[axis].value = value; /* Just so we pass the check above */
680  SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value);
681  }
682 
683  /* We ignore events if we don't have keyboard focus, except for centering
684  * events.
685  */
687  if ((value > joystick->axes[axis].zero && value >= joystick->axes[axis].value) ||
688  (value < joystick->axes[axis].zero && value <= joystick->axes[axis].value)) {
689  return 0;
690  }
691  }
692 
693  /* Update internal joystick state */
694  joystick->axes[axis].value = value;
695 
696  /* Post the event, if desired */
697  posted = 0;
698 #if !SDL_EVENTS_DISABLED
701  event.type = SDL_JOYAXISMOTION;
702  event.jaxis.which = joystick->instance_id;
703  event.jaxis.axis = axis;
704  event.jaxis.value = value;
705  posted = SDL_PushEvent(&event) == 1;
706  }
707 #endif /* !SDL_EVENTS_DISABLED */
708  return (posted);
709 }
#define SDL_abs
#define SDL_ENABLE
Definition: SDL_events.h:722
SDL_Texture * axis
int SDL_PrivateJoystickAxis(SDL_Joystick *joystick, Uint8 axis, Sint16 value)
Definition: SDL_joystick.c:655
#define SDL_JOYSTICK_AXIS_MAX
Definition: SDL_joystick.h:288
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
GLsizei const GLfloat * value
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:580
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBall()

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

Definition at line 752 of file SDL_joystick.c.

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

754 {
755  int posted;
756 
757  /* Make sure we're not getting garbage events */
758  if (ball >= joystick->nballs) {
759  return 0;
760  }
761 
762  /* We ignore events if we don't have keyboard focus. */
764  return 0;
765  }
766 
767  /* Update internal mouse state */
768  joystick->balls[ball].dx += xrel;
769  joystick->balls[ball].dy += yrel;
770 
771  /* Post the event, if desired */
772  posted = 0;
773 #if !SDL_EVENTS_DISABLED
776  event.jball.type = SDL_JOYBALLMOTION;
777  event.jball.which = joystick->instance_id;
778  event.jball.ball = ball;
779  event.jball.xrel = xrel;
780  event.jball.yrel = yrel;
781  posted = SDL_PushEvent(&event) == 1;
782  }
783 #endif /* !SDL_EVENTS_DISABLED */
784  return (posted);
785 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:580
General event structure.
Definition: SDL_events.h:525

◆ SDL_PrivateJoystickBatteryLevel()

void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 1261 of file SDL_joystick.c.

1262 {
1263  joystick->epowerlevel = ePowerLevel;
1264 }

◆ SDL_PrivateJoystickButton()

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

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

789 {
790  int posted;
791 #if !SDL_EVENTS_DISABLED
793 
794  switch (state) {
795  case SDL_PRESSED:
796  event.type = SDL_JOYBUTTONDOWN;
797  break;
798  case SDL_RELEASED:
799  event.type = SDL_JOYBUTTONUP;
800  break;
801  default:
802  /* Invalid state -- bail */
803  return (0);
804  }
805 #endif /* !SDL_EVENTS_DISABLED */
806 
807  /* Make sure we're not getting garbage or duplicate events */
808  if (button >= joystick->nbuttons) {
809  return 0;
810  }
811  if (state == joystick->buttons[button]) {
812  return 0;
813  }
814 
815  /* We ignore events if we don't have keyboard focus, except for button
816  * release. */
818  if (state == SDL_PRESSED) {
819  return 0;
820  }
821  }
822 
823  /* Update internal joystick state */
824  joystick->buttons[button] = state;
825 
826  /* Post the event, if desired */
827  posted = 0;
828 #if !SDL_EVENTS_DISABLED
829  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
830  event.jbutton.which = joystick->instance_id;
831  event.jbutton.button = button;
832  event.jbutton.state = state;
833  posted = SDL_PushEvent(&event) == 1;
834  }
835 #endif /* !SDL_EVENTS_DISABLED */
836  return (posted);
837 }
SDL_Texture * button
struct xkb_state * state
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:580
General event structure.
Definition: SDL_events.h:525
#define SDL_PRESSED
Definition: SDL_events.h:50
#define SDL_RELEASED
Definition: SDL_events.h:49
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickHat()

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

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

713 {
714  int posted;
715 
716  /* Make sure we're not getting garbage or duplicate events */
717  if (hat >= joystick->nhats) {
718  return 0;
719  }
720  if (value == joystick->hats[hat]) {
721  return 0;
722  }
723 
724  /* We ignore events if we don't have keyboard focus, except for centering
725  * events.
726  */
728  if (value != SDL_HAT_CENTERED) {
729  return 0;
730  }
731  }
732 
733  /* Update internal joystick state */
734  joystick->hats[hat] = value;
735 
736  /* Post the event, if desired */
737  posted = 0;
738 #if !SDL_EVENTS_DISABLED
741  event.jhat.type = SDL_JOYHATMOTION;
742  event.jhat.which = joystick->instance_id;
743  event.jhat.hat = hat;
744  event.jhat.value = value;
745  posted = SDL_PushEvent(&event) == 1;
746  }
747 #endif /* !SDL_EVENTS_DISABLED */
748  return (posted);
749 }
#define SDL_ENABLE
Definition: SDL_events.h:722
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
GLsizei const GLfloat * value
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:580
General event structure.
Definition: SDL_events.h:525
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:316

◆ SDL_PrivateJoystickRemoved()

void SDL_PrivateJoystickRemoved ( SDL_JoystickID  device_instance)

Definition at line 638 of file SDL_joystick.c.

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

Referenced by SDL_SYS_RemoveJoystickDevice().

639 {
640 #if !SDL_EVENTS_DISABLED
642 
643  event.type = SDL_JOYDEVICEREMOVED;
644 
645  if (SDL_GetEventState(event.type) == SDL_ENABLE) {
646  event.jdevice.which = device_instance;
647  SDL_PushEvent(&event);
648  }
649 
651 #endif /* !SDL_EVENTS_DISABLED */
652 }
#define SDL_ENABLE
Definition: SDL_events.h:722
static void UpdateEventsForDeviceRemoval()
Definition: SDL_joystick.c:614
#define SDL_GetEventState(type)
Definition: SDL_events.h:735
struct _cl_event * event
#define SDL_PushEvent
General event structure.
Definition: SDL_events.h:525
Uint32 type
Definition: SDL_events.h:527

◆ SDL_PrivateJoystickShouldIgnoreEvent()

static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent ( )
static

Definition at line 580 of file SDL_joystick.c.

References NULL, SDL_FALSE, SDL_GetKeyboardFocus, SDL_HasWindows(), SDL_joystick_allows_background_events, and SDL_TRUE.

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

581 {
583  return SDL_FALSE;
584  }
585 
586  if (SDL_HasWindows() && SDL_GetKeyboardFocus() == NULL) {
587  /* We have windows but we don't have focus, ignore the event. */
588  return SDL_TRUE;
589  }
590  return SDL_FALSE;
591 }
#define SDL_GetKeyboardFocus
static SDL_bool SDL_joystick_allows_background_events
Definition: SDL_joystick.c:37
#define NULL
Definition: begin_code.h:164
SDL_bool SDL_HasWindows(void)
Definition: SDL_video.c:1630

◆ SDL_PrivateJoystickValid()

int SDL_PrivateJoystickValid ( SDL_Joystick *  joystick)

Definition at line 259 of file SDL_joystick.c.

References NULL, and SDL_SetError.

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

260 {
261  int valid;
262 
263  if (joystick == NULL) {
264  SDL_SetError("Joystick hasn't been opened yet");
265  valid = 0;
266  } else {
267  valid = 1;
268  }
269 
270  return valid;
271 }
#define NULL
Definition: begin_code.h:164
#define SDL_SetError

◆ SDL_UnlockJoysticks()

void SDL_UnlockJoysticks ( void  )

Definition at line 51 of file SDL_joystick.c.

References SDL_UnlockMutex, and SDLCALL.

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

52 {
53  if (SDL_joystick_lock) {
55  }
56 }
static SDL_mutex * SDL_joystick_lock
Definition: SDL_joystick.c:40
#define SDL_UnlockMutex

◆ UpdateEventsForDeviceRemoval()

static void UpdateEventsForDeviceRemoval ( )
static

Definition at line 614 of file SDL_joystick.c.

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

Referenced by SDL_PrivateJoystickRemoved().

615 {
616  int i, num_events;
617  SDL_Event *events;
618 
620  if (num_events <= 0) {
621  return;
622  }
623 
624  events = SDL_stack_alloc(SDL_Event, num_events);
625  if (!events) {
626  return;
627  }
628 
629  num_events = SDL_PeepEvents(events, num_events, SDL_GETEVENT, SDL_JOYDEVICEADDED, SDL_JOYDEVICEADDED);
630  for (i = 0; i < num_events; ++i) {
631  --events[i].jdevice.which;
632  }
633  SDL_PeepEvents(events, num_events, SDL_ADDEVENT, 0, 0);
634 
635  SDL_stack_free(events);
636 }
SDL_JoyDeviceEvent jdevice
Definition: SDL_events.h:540
static SDL_Event events[EVENT_BUF_SIZE]
Definition: testgesture.c:35
#define SDL_PeepEvents
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:354
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
General event structure.
Definition: SDL_events.h:525
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:355

Variable Documentation

◆ SDL_joystick_allows_background_events

SDL_bool SDL_joystick_allows_background_events = SDL_FALSE
static

◆ SDL_joystick_lock

SDL_mutex* SDL_joystick_lock = NULL
static

Definition at line 40 of file SDL_joystick.c.

◆ SDL_joysticks

SDL_Joystick* SDL_joysticks = NULL
static

◆ SDL_updating_joystick

SDL_bool SDL_updating_joystick = SDL_FALSE
static

Definition at line 39 of file SDL_joystick.c.

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