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_IPHONE_MAX_GFORCE   5.0
 
#define SDL_JOYSTICK_AXIS_MAX   32767
 
#define SDL_JOYSTICK_AXIS_MIN   -32768
 

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

void SDL_LockJoysticks (void)
 
void SDL_UnlockJoysticks (void)
 
int SDL_NumJoysticks (void)
 
const char * SDL_JoystickNameForIndex (int device_index)
 
int SDL_JoystickGetDevicePlayerIndex (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 instance_id)
 
SDL_Joystick * SDL_JoystickFromPlayerIndex (int player_index)
 
int SDL_JoystickAttachVirtual (SDL_JoystickType type, int naxes, int nbuttons, int nhats)
 
int SDL_JoystickDetachVirtual (int device_index)
 
SDL_bool SDL_JoystickIsVirtual (int device_index)
 
int SDL_JoystickSetVirtualAxis (SDL_Joystick *joystick, int axis, Sint16 value)
 
int SDL_JoystickSetVirtualButton (SDL_Joystick *joystick, int button, Uint8 value)
 
int SDL_JoystickSetVirtualHat (SDL_Joystick *joystick, int hat, Uint8 value)
 
const char * SDL_JoystickName (SDL_Joystick *joystick)
 
int SDL_JoystickGetPlayerIndex (SDL_Joystick *joystick)
 
void SDL_JoystickSetPlayerIndex (SDL_Joystick *joystick, int player_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)
 
const char * SDL_JoystickGetSerial (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)
 

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)
 
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)
 
int SDL_JoystickRumble (SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
 
int SDL_JoystickRumbleTriggers (SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
 
SDL_bool SDL_JoystickHasLED (SDL_Joystick *joystick)
 
int SDL_JoystickSetLED (SDL_Joystick *joystick, Uint8 red, Uint8 green, Uint8 blue)
 
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

#define SDL_HAT_CENTERED   0x00

Definition at line 386 of file SDL_joystick.h.

◆ SDL_HAT_DOWN

#define SDL_HAT_DOWN   0x04

Definition at line 389 of file SDL_joystick.h.

◆ SDL_HAT_LEFT

#define SDL_HAT_LEFT   0x08

Definition at line 390 of file SDL_joystick.h.

◆ SDL_HAT_LEFTDOWN

#define SDL_HAT_LEFTDOWN   (SDL_HAT_LEFT|SDL_HAT_DOWN)

Definition at line 394 of file SDL_joystick.h.

◆ SDL_HAT_LEFTUP

#define SDL_HAT_LEFTUP   (SDL_HAT_LEFT|SDL_HAT_UP)

Definition at line 393 of file SDL_joystick.h.

◆ SDL_HAT_RIGHT

#define SDL_HAT_RIGHT   0x02

Definition at line 388 of file SDL_joystick.h.

◆ SDL_HAT_RIGHTDOWN

#define SDL_HAT_RIGHTDOWN   (SDL_HAT_RIGHT|SDL_HAT_DOWN)

Definition at line 392 of file SDL_joystick.h.

◆ SDL_HAT_RIGHTUP

#define SDL_HAT_RIGHTUP   (SDL_HAT_RIGHT|SDL_HAT_UP)

Definition at line 391 of file SDL_joystick.h.

◆ SDL_HAT_UP

#define SDL_HAT_UP   0x01

Definition at line 387 of file SDL_joystick.h.

◆ SDL_IPHONE_MAX_GFORCE

#define SDL_IPHONE_MAX_GFORCE   5.0

Definition at line 111 of file SDL_joystick.h.

◆ SDL_JOYSTICK_AXIS_MAX

#define SDL_JOYSTICK_AXIS_MAX   32767

Definition at line 358 of file SDL_joystick.h.

◆ SDL_JOYSTICK_AXIS_MIN

#define SDL_JOYSTICK_AXIS_MIN   -32768

Definition at line 359 of file SDL_joystick.h.

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.

98 {
100  SDL_JOYSTICK_POWER_EMPTY, /* <= 5% */
101  SDL_JOYSTICK_POWER_LOW, /* <= 20% */
102  SDL_JOYSTICK_POWER_MEDIUM, /* <= 70% */
103  SDL_JOYSTICK_POWER_FULL, /* <= 100% */
SDL_JoystickPowerLevel
Definition: SDL_joystick.h:98
@ SDL_JOYSTICK_POWER_MAX
Definition: SDL_joystick.h:105
@ SDL_JOYSTICK_POWER_FULL
Definition: SDL_joystick.h:103
@ SDL_JOYSTICK_POWER_MEDIUM
Definition: SDL_joystick.h:102
@ SDL_JOYSTICK_POWER_EMPTY
Definition: SDL_joystick.h:100
@ SDL_JOYSTICK_POWER_UNKNOWN
Definition: SDL_joystick.h:99
@ SDL_JOYSTICK_POWER_WIRED
Definition: SDL_joystick.h:104
@ SDL_JOYSTICK_POWER_LOW
Definition: SDL_joystick.h:101

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

84 {
SDL_JoystickType
Definition: SDL_joystick.h:84
@ SDL_JOYSTICK_TYPE_DANCE_PAD
Definition: SDL_joystick.h:90
@ SDL_JOYSTICK_TYPE_ARCADE_PAD
Definition: SDL_joystick.h:93
@ SDL_JOYSTICK_TYPE_UNKNOWN
Definition: SDL_joystick.h:85
@ SDL_JOYSTICK_TYPE_ARCADE_STICK
Definition: SDL_joystick.h:88
@ SDL_JOYSTICK_TYPE_WHEEL
Definition: SDL_joystick.h:87
@ SDL_JOYSTICK_TYPE_THROTTLE
Definition: SDL_joystick.h:94
@ SDL_JOYSTICK_TYPE_GUITAR
Definition: SDL_joystick.h:91
@ SDL_JOYSTICK_TYPE_FLIGHT_STICK
Definition: SDL_joystick.h:89
@ SDL_JOYSTICK_TYPE_GAMECONTROLLER
Definition: SDL_joystick.h:86
@ SDL_JOYSTICK_TYPE_DRUM_KIT
Definition: SDL_joystick.h:92

Function Documentation

◆ SDL_JoystickAttachVirtual()

int SDL_JoystickAttachVirtual ( SDL_JoystickType  type,
int  naxes,
int  nbuttons,
int  nhats 
)

Attaches a new virtual joystick. Returns the joystick's device index, or -1 if an error occurred.

Definition at line 471 of file SDL_joystick.c.

473 {
474 #if SDL_JOYSTICK_VIRTUAL
475  return SDL_JoystickAttachVirtualInner(type, naxes, nbuttons, nhats);
476 #else
477  return SDL_SetError("SDL not built with virtual-joystick support");
478 #endif
479 }
#define SDL_SetError
GLuint GLuint GLsizei GLenum type
Definition: SDL_opengl.h:1571

References SDL_SetError.

◆ SDL_JoystickClose()

void SDL_JoystickClose ( SDL_Joystick *  joystick)

Close a joystick previously opened with SDL_JoystickOpen().

Definition at line 976 of file SDL_joystick.c.

977 {
978  SDL_Joystick *joysticklist;
979  SDL_Joystick *joysticklistprev;
980  int i;
981 
983  return;
984  }
985 
987 
988  /* First decrement ref count */
989  if (--joystick->ref_count > 0) {
991  return;
992  }
993 
994  if (SDL_updating_joystick) {
996  return;
997  }
998 
999  if (joystick->rumble_expiration) {
1000  SDL_JoystickRumble(joystick, 0, 0, 0);
1001  }
1002  if (joystick->trigger_rumble_expiration) {
1004  }
1005 
1006  joystick->driver->Close(joystick);
1007  joystick->hwdata = NULL;
1008 
1009  joysticklist = SDL_joysticks;
1010  joysticklistprev = NULL;
1011  while (joysticklist) {
1012  if (joystick == joysticklist) {
1013  if (joysticklistprev) {
1014  /* unlink this entry */
1015  joysticklistprev->next = joysticklist->next;
1016  } else {
1017  SDL_joysticks = joystick->next;
1018  }
1019  break;
1020  }
1021  joysticklistprev = joysticklist;
1022  joysticklist = joysticklist->next;
1023  }
1024 
1025  SDL_free(joystick->name);
1026  SDL_free(joystick->serial);
1027 
1028  /* Free the data associated with this joystick */
1029  SDL_free(joystick->axes);
1030  SDL_free(joystick->hats);
1031  SDL_free(joystick->balls);
1032  SDL_free(joystick->buttons);
1033  for (i = 0; i < joystick->ntouchpads; i++) {
1034  SDL_JoystickTouchpadInfo *touchpad = &joystick->touchpads[i];
1035  SDL_free(touchpad->fingers);
1036  }
1037  SDL_free(joystick->touchpads);
1038  SDL_free(joystick->sensors);
1039  SDL_free(joystick);
1040 
1042 }
#define SDL_free
int SDL_JoystickRumbleTriggers(SDL_Joystick *joystick, Uint16 left_rumble, Uint16 right_rumble, Uint32 duration_ms)
Definition: SDL_joystick.c:891
void SDL_LockJoysticks(void)
Definition: SDL_joystick.c:102
static SDL_bool SDL_updating_joystick
Definition: SDL_joystick.c:95
int SDL_JoystickRumble(SDL_Joystick *joystick, Uint16 low_frequency_rumble, Uint16 high_frequency_rumble, Uint32 duration_ms)
Definition: SDL_joystick.c:856
void SDL_UnlockJoysticks(void)
Definition: SDL_joystick.c:110
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:94
SDL_bool SDL_PrivateJoystickValid(SDL_Joystick *joystick)
Definition: SDL_joystick.c:559
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:163
SDL_JoystickTouchpadFingerInfo * fingers
static SDL_Joystick * joystick
Definition: testjoystick.c:37

References SDL_JoystickTouchpadInfo::fingers, i, joystick, NULL, SDL_free, SDL_JoystickRumble(), SDL_JoystickRumbleTriggers(), SDL_joysticks, SDL_LockJoysticks(), SDL_PrivateJoystickValid(), SDL_UnlockJoysticks(), and SDL_updating_joystick.

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

◆ SDL_JoystickCurrentPowerLevel()

SDL_JoystickPowerLevel SDL_JoystickCurrentPowerLevel ( SDL_Joystick *  joystick)

Return the battery level of this joystick

Definition at line 2493 of file SDL_joystick.c.

2494 {
2497  }
2498  return joystick->epowerlevel;
2499 }

References joystick, SDL_JOYSTICK_POWER_UNKNOWN, and SDL_PrivateJoystickValid().

◆ SDL_JoystickDetachVirtual()

int SDL_JoystickDetachVirtual ( int  device_index)

Detaches a virtual joystick Returns 0 on success, or -1 if an error occurred.

Definition at line 482 of file SDL_joystick.c.

483 {
484 #if SDL_JOYSTICK_VIRTUAL
485  SDL_JoystickDriver *driver;
486 
488  if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
489  if (driver == &SDL_VIRTUAL_JoystickDriver) {
490  const int result = SDL_JoystickDetachVirtualInner(device_index);
492  return result;
493  }
494  }
496 
497  return SDL_SetError("Virtual joystick not found at provided index");
498 #else
499  return SDL_SetError("SDL not built with virtual-joystick support");
500 #endif
501 }
SDL_bool SDL_GetDriverAndJoystickIndex(int device_index, SDL_JoystickDriver **driver, int *driver_index)
Definition: SDL_joystick.c:272
GLuint64EXT * result
SDL_JoystickDriver SDL_VIRTUAL_JoystickDriver

References SDL_GetDriverAndJoystickIndex(), SDL_LockJoysticks(), SDL_SetError, SDL_UnlockJoysticks(), and SDL_VIRTUAL_JoystickDriver.

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

1569 {
1570 #if SDL_EVENTS_DISABLED
1571  return SDL_DISABLE;
1572 #else
1573  const Uint32 event_list[] = {
1576  };
1577  unsigned int i;
1578 
1579  switch (state) {
1580  case SDL_QUERY:
1581  state = SDL_DISABLE;
1582  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1583  state = SDL_EventState(event_list[i], SDL_QUERY);
1584  if (state == SDL_ENABLE) {
1585  break;
1586  }
1587  }
1588  break;
1589  default:
1590  for (i = 0; i < SDL_arraysize(event_list); ++i) {
1591  SDL_EventState(event_list[i], state);
1592  }
1593  break;
1594  }
1595  return state;
1596 #endif /* SDL_EVENTS_DISABLED */
1597 }
#define SDL_EventState
@ SDL_JOYDEVICEADDED
Definition: SDL_events.h:118
@ SDL_JOYBUTTONDOWN
Definition: SDL_events.h:116
@ SDL_JOYDEVICEREMOVED
Definition: SDL_events.h:119
@ SDL_JOYBUTTONUP
Definition: SDL_events.h:117
@ SDL_JOYBALLMOTION
Definition: SDL_events.h:114
@ SDL_JOYAXISMOTION
Definition: SDL_events.h:113
@ SDL_JOYHATMOTION
Definition: SDL_events.h:115
#define SDL_QUERY
Definition: SDL_events.h:792
#define SDL_DISABLE
Definition: SDL_events.h:794
#define SDL_ENABLE
Definition: SDL_events.h:795
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:121
uint32_t Uint32
Definition: SDL_stdinc.h:209
struct xkb_state * state

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

◆ SDL_JoystickFromInstanceID()

SDL_Joystick* SDL_JoystickFromInstanceID ( SDL_JoystickID  instance_id)

Return the SDL_Joystick associated with an instance id.

Definition at line 774 of file SDL_joystick.c.

775 {
776  SDL_Joystick *joystick;
777 
779  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
780  if (joystick->instance_id == instance_id) {
781  break;
782  }
783  }
785  return joystick;
786 }

References joystick, SDL_joysticks, SDL_LockJoysticks(), and SDL_UnlockJoysticks().

◆ SDL_JoystickFromPlayerIndex()

SDL_Joystick* SDL_JoystickFromPlayerIndex ( int  player_index)

Return the SDL_Joystick associated with a player index.

Definition at line 792 of file SDL_joystick.c.

793 {
794  SDL_JoystickID instance_id;
795  SDL_Joystick *joystick;
796 
798  instance_id = SDL_GetJoystickIDForPlayerIndex(player_index);
799  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
800  if (joystick->instance_id == instance_id) {
801  break;
802  }
803  }
805  return joystick;
806 }
static SDL_JoystickID SDL_GetJoystickIDForPlayerIndex(int player_index)
Definition: SDL_joystick.c:147
Sint32 SDL_JoystickID
Definition: SDL_joystick.h:81

References joystick, SDL_GetJoystickIDForPlayerIndex(), SDL_joysticks, SDL_LockJoysticks(), and SDL_UnlockJoysticks().

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

749 {
751  return SDL_FALSE;
752  }
753 
754  return joystick->attached;
755 }
@ SDL_FALSE
Definition: SDL_stdinc.h:169

References joystick, SDL_FALSE, and SDL_PrivateJoystickValid().

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

641 {
642  Sint16 state;
643 
645  return 0;
646  }
647  if (axis < joystick->naxes) {
648  state = joystick->axes[axis].value;
649  } else {
650  SDL_SetError("Joystick only has %d axes", joystick->naxes);
651  state = 0;
652  }
653  return state;
654 }
int16_t Sint16
Definition: SDL_stdinc.h:191
SDL_Texture * axis

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

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

661 {
663  return SDL_FALSE;
664  }
665  if (axis >= joystick->naxes) {
666  SDL_SetError("Joystick only has %d axes", joystick->naxes);
667  return SDL_FALSE;
668  }
669  if (state) {
670  *state = joystick->axes[axis].initial_value;
671  }
672  return joystick->axes[axis].has_initial_value;
673 }

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

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

700 {
701  int retval;
702 
704  return -1;
705  }
706 
707  retval = 0;
708  if (ball < joystick->nballs) {
709  if (dx) {
710  *dx = joystick->balls[ball].dx;
711  }
712  if (dy) {
713  *dy = joystick->balls[ball].dy;
714  }
715  joystick->balls[ball].dx = 0;
716  joystick->balls[ball].dy = 0;
717  } else {
718  return SDL_SetError("Joystick only has %d balls", joystick->nballs);
719  }
720  return retval;
721 }
SDL_bool retval

References joystick, retval, SDL_PrivateJoystickValid(), and 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 727 of file SDL_joystick.c.

728 {
729  Uint8 state;
730 
732  return 0;
733  }
734  if (button < joystick->nbuttons) {
735  state = joystick->buttons[button];
736  } else {
737  SDL_SetError("Joystick only has %d buttons", joystick->nbuttons);
738  state = 0;
739  }
740  return state;
741 }
uint8_t Uint8
Definition: SDL_stdinc.h:185
SDL_Texture * button

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

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

2273 {
2274  SDL_JoystickDriver *driver;
2275  SDL_JoystickGUID guid;
2276 
2278  if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
2279  guid = driver->GetDeviceGUID(device_index);
2280  } else {
2281  SDL_zero(guid);
2282  }
2284 
2285  return guid;
2286 }
#define SDL_zero(x)
Definition: SDL_stdinc.h:426
SDL_JoystickGUID(* GetDeviceGUID)(int device_index)

References SDL_JoystickDriver::GetDeviceGUID, SDL_GetDriverAndJoystickIndex(), SDL_LockJoysticks(), SDL_UnlockJoysticks(), and SDL_zero.

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

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

2330 {
2331  SDL_JoystickDriver *driver;
2332  SDL_JoystickID instance_id = -1;
2333 
2335  if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
2336  instance_id = driver->GetDeviceInstanceID(device_index);
2337  }
2339 
2340  return instance_id;
2341 }
SDL_JoystickID(* GetDeviceInstanceID)(int device_index)

References SDL_JoystickDriver::GetDeviceInstanceID, SDL_GetDriverAndJoystickIndex(), SDL_LockJoysticks(), and SDL_UnlockJoysticks().

Referenced by SDL_JoystickGetDeviceIndexFromInstanceID(), and SDL_JoystickGetDevicePlayerIndex().

◆ SDL_JoystickGetDevicePlayerIndex()

int SDL_JoystickGetDevicePlayerIndex ( int  device_index)

Get the player index of a joystick, or -1 if it's not available This can be called before any joysticks are opened.

Definition at line 316 of file SDL_joystick.c.

317 {
318  int player_index;
319 
323 
324  return player_index;
325 }
SDL_JoystickID SDL_JoystickGetDeviceInstanceID(int device_index)
static int SDL_GetPlayerIndexForJoystickID(SDL_JoystickID instance_id)
Definition: SDL_joystick.c:131

References SDL_GetPlayerIndexForJoystickID(), SDL_JoystickGetDeviceInstanceID(), SDL_LockJoysticks(), and SDL_UnlockJoysticks().

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

2298 {
2299  Uint16 product;
2300  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
2301 
2302  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
2303  return product;
2304 }
SDL_JoystickGUID SDL_JoystickGetDeviceGUID(int device_index)
void SDL_GetJoystickGUIDInfo(SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
uint16_t Uint16
Definition: SDL_stdinc.h:197

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

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

2307 {
2308  Uint16 version;
2309  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
2310 
2311  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
2312  return version;
2313 }

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

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

2316 {
2318  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
2319 
2320  type = SDL_GetJoystickGUIDType(guid);
2322  if (SDL_IsGameController(device_index)) {
2324  }
2325  }
2326  return type;
2327 }
#define SDL_IsGameController
static SDL_JoystickType SDL_GetJoystickGUIDType(SDL_JoystickGUID guid)

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

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

2289 {
2290  Uint16 vendor;
2291  SDL_JoystickGUID guid = SDL_JoystickGetDeviceGUID(device_index);
2292 
2293  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
2294  return vendor;
2295 }

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

◆ SDL_JoystickGetGUID()

SDL_JoystickGUID SDL_JoystickGetGUID ( SDL_Joystick *  joystick)

Return the GUID for this opened joystick

Definition at line 2360 of file SDL_joystick.c.

2361 {
2363  SDL_JoystickGUID emptyGUID;
2364  SDL_zero(emptyGUID);
2365  return emptyGUID;
2366  }
2367  return joystick->guid;
2368 }

References joystick, SDL_PrivateJoystickValid(), and SDL_zero.

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

◆ SDL_JoystickGetGUIDFromString()

SDL_JoystickGUID SDL_JoystickGetGUIDFromString ( const char *  pchGUID)

Convert a string into a joystick guid

Definition at line 2465 of file SDL_joystick.c.

2466 {
2467  SDL_JoystickGUID guid;
2468  int maxoutputbytes= sizeof(guid);
2469  size_t len = SDL_strlen(pchGUID);
2470  Uint8 *p;
2471  size_t i;
2472 
2473  /* Make sure it's even */
2474  len = (len) & ~0x1;
2475 
2476  SDL_memset(&guid, 0x00, sizeof(guid));
2477 
2478  p = (Uint8 *)&guid;
2479  for (i = 0; (i < len) && ((p - (Uint8 *)&guid) < maxoutputbytes); i+=2, p++) {
2480  *p = (nibble(pchGUID[i]) << 4) | nibble(pchGUID[i+1]);
2481  }
2482 
2483  return guid;
2484 }
#define SDL_memset
#define SDL_strlen
static unsigned char nibble(char c)
GLenum GLsizei len
GLfloat GLfloat p

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

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

2421 {
2422  static const char k_rgchHexToASCII[] = "0123456789abcdef";
2423  int i;
2424 
2425  if ((pszGUID == NULL) || (cbGUID <= 0)) {
2426  return;
2427  }
2428 
2429  for (i = 0; i < sizeof(guid.data) && i < (cbGUID-1)/2; i++) {
2430  /* each input byte writes 2 ascii chars, and might write a null byte. */
2431  /* If we don't have room for next input byte, stop */
2432  unsigned char c = guid.data[i];
2433 
2434  *pszGUID++ = k_rgchHexToASCII[c >> 4];
2435  *pszGUID++ = k_rgchHexToASCII[c & 0x0F];
2436  }
2437  *pszGUID = '\0';
2438 }
const GLubyte * c
Uint8 data[16]
Definition: SDL_joystick.h:71

References SDL_JoystickGUID::data, i, and NULL.

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

680 {
681  Uint8 state;
682 
684  return 0;
685  }
686  if (hat < joystick->nhats) {
687  state = joystick->hats[hat];
688  } else {
689  SDL_SetError("Joystick only has %d hats", joystick->nhats);
690  state = 0;
691  }
692  return state;
693 }

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

◆ SDL_JoystickGetPlayerIndex()

int SDL_JoystickGetPlayerIndex ( SDL_Joystick *  joystick)

Get the player index of an opened joystick, or -1 if it's not available

For XInput controllers this returns the XInput user index.

Get the player index of an opened joystick, or -1 if it's not available

Definition at line 825 of file SDL_joystick.c.

826 {
827  int player_index;
828 
830  return -1;
831  }
832 
834  player_index = SDL_GetPlayerIndexForJoystickID(joystick->instance_id);
836 
837  return player_index;
838 }

References joystick, SDL_GetPlayerIndexForJoystickID(), SDL_LockJoysticks(), SDL_PrivateJoystickValid(), and SDL_UnlockJoysticks().

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

2380 {
2381  Uint16 product;
2383 
2384  SDL_GetJoystickGUIDInfo(guid, NULL, &product, NULL);
2385  return product;
2386 }
SDL_JoystickGUID SDL_JoystickGetGUID(SDL_Joystick *joystick)

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

Referenced by SDL_JoystickAxesCenteredAtZero().

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

2389 {
2390  Uint16 version;
2392 
2393  SDL_GetJoystickGUIDInfo(guid, NULL, NULL, &version);
2394  return version;
2395 }

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

◆ SDL_JoystickGetSerial()

const char* SDL_JoystickGetSerial ( SDL_Joystick *  joystick)

Get the serial number of an opened joystick, if available.

Returns the serial number of the joystick, or NULL if it is not available.

Definition at line 2397 of file SDL_joystick.c.

2398 {
2400  return NULL;
2401  }
2402  return joystick->serial;
2403 }

References joystick, NULL, and SDL_PrivateJoystickValid().

◆ SDL_JoystickGetType()

SDL_JoystickType SDL_JoystickGetType ( SDL_Joystick *  joystick)

Get the type of an opened joystick.

Definition at line 2405 of file SDL_joystick.c.

2406 {
2409 
2410  type = SDL_GetJoystickGUIDType(guid);
2412  if (joystick && joystick->is_game_controller) {
2414  }
2415  }
2416  return type;
2417 }

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

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

2371 {
2372  Uint16 vendor;
2374 
2375  SDL_GetJoystickGUIDInfo(guid, &vendor, NULL, NULL);
2376  return vendor;
2377 }

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

Referenced by SDL_JoystickAxesCenteredAtZero().

◆ SDL_JoystickHasLED()

SDL_bool SDL_JoystickHasLED ( SDL_Joystick *  joystick)

Return whether a joystick has an LED

Parameters
joystickThe joystick to query
Returns
SDL_TRUE, or SDL_FALSE if this joystick does not have a modifiable LED

Definition at line 925 of file SDL_joystick.c.

926 {
928 
930  return SDL_FALSE;
931  }
932 
934 
935  result = joystick->driver->HasLED(joystick);
936 
938 
939  return result;
940 }
SDL_bool
Definition: SDL_stdinc.h:168

References joystick, SDL_FALSE, SDL_LockJoysticks(), SDL_PrivateJoystickValid(), and SDL_UnlockJoysticks().

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

762 {
764  return -1;
765  }
766 
767  return joystick->instance_id;
768 }

References joystick, and SDL_PrivateJoystickValid().

◆ SDL_JoystickIsVirtual()

SDL_bool SDL_JoystickIsVirtual ( int  device_index)

Indicates whether or not a virtual-joystick is at a given device index.

Definition at line 504 of file SDL_joystick.c.

505 {
506 #if SDL_JOYSTICK_VIRTUAL
507  SDL_JoystickDriver *driver;
508  int driver_device_index;
509  SDL_bool is_virtual = SDL_FALSE;
510 
512  if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &driver_device_index)) {
513  if (driver == &SDL_VIRTUAL_JoystickDriver) {
514  is_virtual = SDL_TRUE;
515  }
516  }
518 
519  return is_virtual;
520 #else
521  return SDL_FALSE;
522 #endif
523 }
@ SDL_TRUE
Definition: SDL_stdinc.h:170

References SDL_FALSE, SDL_GetDriverAndJoystickIndex(), SDL_LockJoysticks(), SDL_TRUE, SDL_UnlockJoysticks(), and SDL_VIRTUAL_JoystickDriver.

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

813 {
815  return NULL;
816  }
817 
818  return joystick->name;
819 }

References joystick, NULL, and SDL_PrivateJoystickValid().

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

298 {
299  SDL_JoystickDriver *driver;
300  const char *name = NULL;
301 
303  if (SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
304  name = driver->GetDeviceName(device_index);
305  }
307 
308  /* FIXME: Really we should reference count this name so it doesn't go away after unlock */
309  return name;
310 }
GLuint const GLchar * name
const char *(* GetDeviceName)(int device_index)

References SDL_JoystickDriver::GetDeviceName, NULL, SDL_GetDriverAndJoystickIndex(), SDL_LockJoysticks(), and SDL_UnlockJoysticks().

◆ SDL_JoystickNumAxes()

int SDL_JoystickNumAxes ( SDL_Joystick *  joystick)

Get the number of general axis controls on a joystick.

Definition at line 592 of file SDL_joystick.c.

593 {
595  return -1;
596  }
597  return joystick->naxes;
598 }

References joystick, and SDL_PrivateJoystickValid().

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

617 {
619  return -1;
620  }
621  return joystick->nballs;
622 }

References joystick, and SDL_PrivateJoystickValid().

◆ SDL_JoystickNumButtons()

int SDL_JoystickNumButtons ( SDL_Joystick *  joystick)

Get the number of buttons on a joystick.

Definition at line 628 of file SDL_joystick.c.

629 {
631  return -1;
632  }
633  return joystick->nbuttons;
634 }

References joystick, and SDL_PrivateJoystickValid().

◆ SDL_JoystickNumHats()

int SDL_JoystickNumHats ( SDL_Joystick *  joystick)

Get the number of POV hats on a joystick.

Definition at line 604 of file SDL_joystick.c.

605 {
607  return -1;
608  }
609  return joystick->nhats;
610 }

References joystick, and SDL_PrivateJoystickValid().

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

368 {
369  SDL_JoystickDriver *driver;
370  SDL_JoystickID instance_id;
371  SDL_Joystick *joystick;
372  SDL_Joystick *joysticklist;
373  const char *joystickname = NULL;
374 
376 
377  if (!SDL_GetDriverAndJoystickIndex(device_index, &driver, &device_index)) {
379  return NULL;
380  }
381 
382  joysticklist = SDL_joysticks;
383  /* If the joystick is already open, return it
384  * it is important that we have a single joystick * for each instance id
385  */
386  instance_id = driver->GetDeviceInstanceID(device_index);
387  while (joysticklist) {
388  if (instance_id == joysticklist->instance_id) {
389  joystick = joysticklist;
390  ++joystick->ref_count;
392  return joystick;
393  }
394  joysticklist = joysticklist->next;
395  }
396 
397  /* Create and initialize the joystick */
398  joystick = (SDL_Joystick *) SDL_calloc(sizeof(*joystick), 1);
399  if (joystick == NULL) {
400  SDL_OutOfMemory();
402  return NULL;
403  }
404  joystick->driver = driver;
405  joystick->instance_id = instance_id;
406  joystick->attached = SDL_TRUE;
407  joystick->epowerlevel = SDL_JOYSTICK_POWER_UNKNOWN;
408 
409  if (driver->Open(joystick, device_index) < 0) {
412  return NULL;
413  }
414 
415  joystickname = driver->GetDeviceName(device_index);
416  if (joystickname) {
417  joystick->name = SDL_strdup(joystickname);
418  } else {
419  joystick->name = NULL;
420  }
421 
422  joystick->guid = driver->GetDeviceGUID(device_index);
423 
424  if (joystick->naxes > 0) {
426  }
427  if (joystick->nhats > 0) {
428  joystick->hats = (Uint8 *) SDL_calloc(joystick->nhats, sizeof(Uint8));
429  }
430  if (joystick->nballs > 0) {
431  joystick->balls = (struct balldelta *) SDL_calloc(joystick->nballs, sizeof(*joystick->balls));
432  }
433  if (joystick->nbuttons > 0) {
434  joystick->buttons = (Uint8 *) SDL_calloc(joystick->nbuttons, sizeof(Uint8));
435  }
436  if (((joystick->naxes > 0) && !joystick->axes)
437  || ((joystick->nhats > 0) && !joystick->hats)
438  || ((joystick->nballs > 0) && !joystick->balls)
439  || ((joystick->nbuttons > 0) && !joystick->buttons)) {
440  SDL_OutOfMemory();
443  return NULL;
444  }
445 
446  /* If this joystick is known to have all zero centered axes, skip the auto-centering code */
448  int i;
449 
450  for (i = 0; i < joystick->naxes; ++i) {
451  joystick->axes[i].has_initial_value = SDL_TRUE;
452  }
453  }
454 
455  joystick->is_game_controller = SDL_IsGameController(device_index);
456 
457  /* Add joystick to list */
458  ++joystick->ref_count;
459  /* Link the joystick in the list */
460  joystick->next = SDL_joysticks;
462 
464 
465  driver->Update(joystick);
466 
467  return joystick;
468 }
#define SDL_strdup
#define SDL_calloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
static SDL_bool SDL_JoystickAxesCenteredAtZero(SDL_Joystick *joystick)
Definition: SDL_joystick.c:333
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:976
void(* Update)(SDL_Joystick *joystick)
int(* Open)(SDL_Joystick *joystick, int device_index)

References SDL_JoystickDriver::GetDeviceGUID, SDL_JoystickDriver::GetDeviceInstanceID, SDL_JoystickDriver::GetDeviceName, i, joystick, NULL, SDL_JoystickDriver::Open, SDL_calloc, SDL_free, SDL_GetDriverAndJoystickIndex(), SDL_IsGameController, SDL_JOYSTICK_POWER_UNKNOWN, SDL_JoystickAxesCenteredAtZero(), SDL_JoystickClose(), SDL_joysticks, SDL_LockJoysticks(), SDL_OutOfMemory, SDL_strdup, SDL_TRUE, SDL_UnlockJoysticks(), and SDL_JoystickDriver::Update.

◆ SDL_JoystickRumble()

int SDL_JoystickRumble ( SDL_Joystick *  joystick,
Uint16  low_frequency_rumble,
Uint16  high_frequency_rumble,
Uint32  duration_ms 
)

Start a rumble effect Each call to this function cancels any previous rumble effect, and calling it with 0 intensity stops any rumbling.

Parameters
joystickThe joystick to vibrate
low_frequency_rumbleThe intensity of the low frequency (left) rumble motor, from 0 to 0xFFFF
high_frequency_rumbleThe intensity of the high frequency (right) rumble motor, from 0 to 0xFFFF
duration_msThe duration of the rumble effect, in milliseconds
Returns
0, or -1 if rumble isn't supported on this joystick

Definition at line 856 of file SDL_joystick.c.

857 {
858  int result;
859 
861  return -1;
862  }
863 
865  if (low_frequency_rumble == joystick->low_frequency_rumble &&
866  high_frequency_rumble == joystick->high_frequency_rumble) {
867  /* Just update the expiration */
868  result = 0;
869  } else {
870  result = joystick->driver->Rumble(joystick, low_frequency_rumble, high_frequency_rumble);
871  }
872 
873  /* Save the rumble value regardless of success, so we don't spam the driver */
874  joystick->low_frequency_rumble = low_frequency_rumble;
875  joystick->high_frequency_rumble = high_frequency_rumble;
876 
877  if ((low_frequency_rumble || high_frequency_rumble) && duration_ms) {
878  joystick->rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
879  if (!joystick->rumble_expiration) {
880  joystick->rumble_expiration = 1;
881  }
882  } else {
883  joystick->rumble_expiration = 0;
884  }
886 
887  return result;
888 }
#define SDL_min(x, y)
Definition: SDL_stdinc.h:412
#define SDL_MAX_RUMBLE_DURATION_MS
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.

References joystick, SDL_GetTicks(), SDL_LockJoysticks(), SDL_MAX_RUMBLE_DURATION_MS, SDL_min, SDL_PrivateJoystickValid(), and SDL_UnlockJoysticks().

Referenced by SDL_JoystickClose(), and SDL_JoystickUpdate().

◆ SDL_JoystickRumbleTriggers()

int SDL_JoystickRumbleTriggers ( SDL_Joystick *  joystick,
Uint16  left_rumble,
Uint16  right_rumble,
Uint32  duration_ms 
)

Start a rumble effect in the joystick's triggers Each call to this function cancels any previous trigger rumble effect, and calling it with 0 intensity stops any rumbling.

Parameters
joystickThe joystick to vibrate
left_rumbleThe intensity of the left trigger rumble motor, from 0 to 0xFFFF
right_rumbleThe intensity of the right trigger rumble motor, from 0 to 0xFFFF
duration_msThe duration of the rumble effect, in milliseconds
Returns
0, or -1 if trigger rumble isn't supported on this joystick

Definition at line 891 of file SDL_joystick.c.

892 {
893  int result;
894 
896  return -1;
897  }
898 
900  if (left_rumble == joystick->left_trigger_rumble && right_rumble == joystick->right_trigger_rumble) {
901  /* Just update the expiration */
902  result = 0;
903  } else {
904  result = joystick->driver->RumbleTriggers(joystick, left_rumble, right_rumble);
905  }
906 
907  /* Save the rumble value regardless of success, so we don't spam the driver */
908  joystick->left_trigger_rumble = left_rumble;
909  joystick->right_trigger_rumble = right_rumble;
910 
911  if ((left_rumble || right_rumble) && duration_ms) {
912  joystick->trigger_rumble_expiration = SDL_GetTicks() + SDL_min(duration_ms, SDL_MAX_RUMBLE_DURATION_MS);
913  if (!joystick->trigger_rumble_expiration) {
914  joystick->trigger_rumble_expiration = 1;
915  }
916  } else {
917  joystick->trigger_rumble_expiration = 0;
918  }
920 
921  return result;
922 }

References joystick, SDL_GetTicks(), SDL_LockJoysticks(), SDL_MAX_RUMBLE_DURATION_MS, SDL_min, SDL_PrivateJoystickValid(), and SDL_UnlockJoysticks().

Referenced by SDL_JoystickClose(), and SDL_JoystickUpdate().

◆ SDL_JoystickSetLED()

int SDL_JoystickSetLED ( SDL_Joystick *  joystick,
Uint8  red,
Uint8  green,
Uint8  blue 
)

Update a joystick's LED color.

Parameters
joystickThe joystick to update
redThe intensity of the red LED
greenThe intensity of the green LED
blueThe intensity of the blue LED
Returns
0, or -1 if this joystick does not have a modifiable LED

Definition at line 943 of file SDL_joystick.c.

944 {
945  int result;
946 
948  return -1;
949  }
950 
952 
953  if (red == joystick->led_red &&
954  green == joystick->led_green &&
955  blue == joystick->led_blue) {
956  /* Avoid spamming the driver */
957  result = 0;
958  } else {
959  result = joystick->driver->SetLED(joystick, red, green, blue);
960  }
961 
962  /* Save the LED value regardless of success, so we don't spam the driver */
963  joystick->led_red = red;
964  joystick->led_green = green;
965  joystick->led_blue = blue;
966 
968 
969  return result;
970 }
const GLubyte GLuint red
Definition: SDL_glfuncs.h:80
GLbyte GLbyte blue
GLbyte green

References joystick, red, SDL_LockJoysticks(), SDL_PrivateJoystickValid(), and SDL_UnlockJoysticks().

◆ SDL_JoystickSetPlayerIndex()

void SDL_JoystickSetPlayerIndex ( SDL_Joystick *  joystick,
int  player_index 
)

Set the player index of an opened joystick

Definition at line 844 of file SDL_joystick.c.

845 {
847  return;
848  }
849 
851  SDL_SetJoystickIDForPlayerIndex(player_index, joystick->instance_id);
853 }
static SDL_bool SDL_SetJoystickIDForPlayerIndex(int player_index, SDL_JoystickID instance_id)
Definition: SDL_joystick.c:156

References joystick, SDL_LockJoysticks(), SDL_PrivateJoystickValid(), SDL_SetJoystickIDForPlayerIndex(), and SDL_UnlockJoysticks().

◆ SDL_JoystickSetVirtualAxis()

int SDL_JoystickSetVirtualAxis ( SDL_Joystick *  joystick,
int  axis,
Sint16  value 
)

Set values on an opened, virtual-joystick's controls. Please note that values set here will not be applied until the next call to SDL_JoystickUpdate, which can either be called directly, or can be called indirectly through various other SDL APIS, including, but not limited to the following: SDL_PollEvent, SDL_PumpEvents, SDL_WaitEventTimeout, SDL_WaitEvent.

Returns 0 on success, -1 on error.

Definition at line 526 of file SDL_joystick.c.

527 {
528 #if SDL_JOYSTICK_VIRTUAL
529  return SDL_JoystickSetVirtualAxisInner(joystick, axis, value);
530 #else
531  return SDL_SetError("SDL not built with virtual-joystick support");
532 #endif
533 }
GLsizei const GLfloat * value

References axis, joystick, and SDL_SetError.

◆ SDL_JoystickSetVirtualButton()

int SDL_JoystickSetVirtualButton ( SDL_Joystick *  joystick,
int  button,
Uint8  value 
)

Definition at line 536 of file SDL_joystick.c.

537 {
538 #if SDL_JOYSTICK_VIRTUAL
539  return SDL_JoystickSetVirtualButtonInner(joystick, button, value);
540 #else
541  return SDL_SetError("SDL not built with virtual-joystick support");
542 #endif
543 }

References button, joystick, and SDL_SetError.

◆ SDL_JoystickSetVirtualHat()

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

Definition at line 546 of file SDL_joystick.c.

547 {
548 #if SDL_JOYSTICK_VIRTUAL
549  return SDL_JoystickSetVirtualHatInner(joystick, hat, value);
550 #else
551  return SDL_SetError("SDL not built with virtual-joystick support");
552 #endif
553 }

References joystick, and SDL_SetError.

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

1486 {
1487  int i;
1488  SDL_Joystick *joystick, *next;
1489 
1491  return;
1492  }
1493 
1495 
1496  if (SDL_updating_joystick) {
1497  /* The joysticks are already being updated */
1499  return;
1500  }
1501 
1503 
1504  /* Make sure the list is unlocked while dispatching events to prevent application deadlocks */
1506 
1507 #ifdef SDL_JOYSTICK_HIDAPI
1508  /* Special function for HIDAPI devices, as a single device can provide multiple SDL_Joysticks */
1510 #endif /* SDL_JOYSTICK_HIDAPI */
1511 
1512  for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
1513  if (joystick->attached) {
1514  /* This should always be true, but seeing a crash in the wild...? */
1515  if (joystick->driver) {
1516  joystick->driver->Update(joystick);
1517  }
1518 
1519  if (joystick->delayed_guide_button) {
1521  }
1522  }
1523 
1524  if (joystick->rumble_expiration) {
1526  /* Double check now that the lock is held */
1527  if (joystick->rumble_expiration &&
1528  SDL_TICKS_PASSED(SDL_GetTicks(), joystick->rumble_expiration)) {
1529  SDL_JoystickRumble(joystick, 0, 0, 0);
1530  }
1532  }
1533 
1534  if (joystick->trigger_rumble_expiration) {
1536  /* Double check now that the lock is held */
1537  if (joystick->trigger_rumble_expiration &&
1538  SDL_TICKS_PASSED(SDL_GetTicks(), joystick->trigger_rumble_expiration)) {
1540  }
1542  }
1543  }
1544 
1546 
1548 
1549  /* If any joysticks were closed while updating, free them here */
1550  for (joystick = SDL_joysticks; joystick; joystick = next) {
1551  next = joystick->next;
1552  if (joystick->ref_count <= 0) {
1554  }
1555  }
1556 
1557  /* this needs to happen AFTER walking the joystick list above, so that any
1558  dangling hardware data from removed devices can be free'd
1559  */
1560  for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
1562  }
1563 
1565 }
#define SDL_INIT_JOYSTICK
Definition: SDL.h:83
#define SDL_WasInit
void SDL_GameControllerHandleDelayedGuideButton(SDL_Joystick *joystick)
void HIDAPI_UpdateDevices(void)
static SDL_JoystickDriver * SDL_joystick_drivers[]
Definition: SDL_joystick.c:52
#define SDL_TICKS_PASSED(A, B)
Compare SDL ticks values, and return true if A has passed B.
Definition: SDL_timer.h:56
void(* Detect)(void)

References SDL_JoystickDriver::Detect, HIDAPI_UpdateDevices(), i, joystick, SDL_arraysize, SDL_FALSE, SDL_GameControllerHandleDelayedGuideButton(), SDL_GetTicks(), SDL_INIT_JOYSTICK, SDL_joystick_drivers, SDL_JoystickClose(), SDL_JoystickRumble(), SDL_JoystickRumbleTriggers(), SDL_joysticks, SDL_LockJoysticks(), SDL_TICKS_PASSED, SDL_TRUE, SDL_UnlockJoysticks(), SDL_updating_joystick, and SDL_WasInit.

◆ 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 672 of file SDL_dynapi_procs.h.

References SDL_joystick_lock, and SDL_LockMutex.

Referenced by SDL_JoystickClose(), SDL_JoystickDetachVirtual(), SDL_JoystickFromInstanceID(), SDL_JoystickFromPlayerIndex(), SDL_JoystickGetDeviceGUID(), SDL_JoystickGetDeviceIndexFromInstanceID(), SDL_JoystickGetDeviceInstanceID(), SDL_JoystickGetDevicePlayerIndex(), SDL_JoystickGetPlayerIndex(), SDL_JoystickHasLED(), SDL_JoystickIsVirtual(), SDL_JoystickNameForIndex(), SDL_JoystickOpen(), SDL_JoystickQuit(), SDL_JoystickRumble(), SDL_JoystickRumbleTriggers(), SDL_JoystickSetLED(), SDL_JoystickSetPlayerIndex(), SDL_JoystickUpdate(), SDL_NumJoysticks(), SDL_PrivateJoystickAdded(), SDL_PrivateJoystickGetAutoGamepadMapping(), and SDL_PrivateJoystickRemoved().

◆ SDL_NumJoysticks()

int SDL_NumJoysticks ( void  )

Count the number of joysticks attached to the system right now

Definition at line 247 of file SDL_joystick.c.

248 {
249  int i, total_joysticks = 0;
251  for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
252  total_joysticks += SDL_joystick_drivers[i]->GetCount();
253  }
255  return total_joysticks;
256 }
int(* GetCount)(void)

References SDL_JoystickDriver::GetCount, i, SDL_arraysize, SDL_joystick_drivers, SDL_LockJoysticks(), and SDL_UnlockJoysticks().

Referenced by SDL_JoystickGetDeviceIndexFromInstanceID().

◆ SDL_UnlockJoysticks()