SDL_joystick_c.h File Reference
`#include "../SDL_internal.h"`
`#include "SDL_joystick.h"`
Include dependency graph for SDL_joystick_c.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions

int | SDL_JoystickInit (void)
void | SDL_JoystickQuit (void)
int | SDL_GameControllerInit (void)
void | SDL_GameControllerQuit (void)
int | SDL_PrivateJoystickAxis (SDL_Joystick *joystick, Uint8 axis, Sint16 value)
int | SDL_PrivateJoystickBall (SDL_Joystick *joystick, Uint8 ball, Sint16 xrel, Sint16 yrel)
int | SDL_PrivateJoystickHat (SDL_Joystick *joystick, Uint8 hat, Uint8 value)
int | SDL_PrivateJoystickButton (SDL_Joystick *joystick, Uint8 button, Uint8 state)
void | SDL_PrivateJoystickBatteryLevel (SDL_Joystick *joystick, SDL_JoystickPowerLevel ePowerLevel)
int | SDL_PrivateJoystickValid (SDL_Joystick *joystick)

## Function Documentation

int SDL_GameControllerInit | ( | void |  | ) |

Definition at line 860 of file SDL_gamecontroller.c.

References SDL_Event::cdevice, i, NULL, s_ControllerMappings, SDL_AddEventWatch, SDL_CONTROLLERDEVICEADDED, SDL_GameControllerAddMapping(), SDL_GameControllerEventWatcher(), SDL_GameControllerLoadHints(), SDL_IsGameController(), SDL_NumJoysticks, SDL_PushEvent, SDL_Event::type, and SDL_ControllerDeviceEvent::which.

Referenced by SDL_InitSubSystem().

{
int i = 0;
const char *pMappingString = NULL;
s_pSupportedControllers = NULL;
pMappingString = s_ControllerMappings[i];
while (pMappingString) {
SDL_GameControllerAddMapping(pMappingString);
i++;
pMappingString = s_ControllerMappings[i];
}
/* load in any user supplied config */
SDL_GameControllerLoadHints();
/* watch for joy events and fire controller ones if needed */
SDL_AddEventWatch(SDL_GameControllerEventWatcher, NULL);
/* Send added events for controllers currently attached */
for (i = 0; i < SDL_NumJoysticks(); ++i) {
if (SDL_IsGameController(i)) {
SDL_Event deviceevent;
deviceevent.type = SDL_CONTROLLERDEVICEADDED;
deviceevent.cdevice.which = i;
SDL_PushEvent(&deviceevent);
}
}
return (0);
}
SDL_Event::cdevice
SDL_ControllerDeviceEvent cdevice
Definition: SDL_events.h:539
SDL_NumJoysticks
#define SDL_NumJoysticks
Definition: SDL_dynapi_overrides.h:201
SDL_GameControllerLoadHints
static void SDL_GameControllerLoadHints()
Definition: SDL_gamecontroller.c:828
SDL_CONTROLLERDEVICEADDED
Definition: SDL_events.h:120
SDL_ControllerDeviceEvent::which
Sint32 which
Definition: SDL_events.h:389
s_pSupportedControllers
static ControllerMapping_t * s_pSupportedControllers
Definition: SDL_gamecontroller.c:90
SDL_GameControllerAddMapping
int SDL_GameControllerAddMapping(const char *mappingString)
Definition: SDL_gamecontroller.c:746
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
SDL_IsGameController
SDL_bool SDL_IsGameController(int device_index)
Definition: SDL_gamecontroller.c:911
NULL
#define NULL
Definition: begin_code.h:143
s_ControllerMappings
static const char * s_ControllerMappings[]
Definition: SDL_gamecontrollerdb.h:32
SDL_AddEventWatch
#define SDL_AddEventWatch
Definition: SDL_dynapi_overrides.h:134
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_GameControllerEventWatcher
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
Definition: SDL_gamecontroller.c:111
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523

void SDL_GameControllerQuit | ( | void |  | ) |

Definition at line 1200 of file SDL_gamecontroller.c.

References ControllerMapping_t::mapping, ControllerMapping_t::name, ControllerMapping_t::next, NULL, s_pSupportedControllers, SDL_DelEventWatch, SDL_free(), SDL_GameControllerClose(), SDL_GameControllerEventWatcher(), and SDL_gamecontrollers.

Referenced by SDL_QuitSubSystem().

{
ControllerMapping_t *pControllerMap;
while (SDL_gamecontrollers) {
SDL_gamecontrollers->ref_count = 1;
SDL_GameControllerClose(SDL_gamecontrollers);
}
while (s_pSupportedControllers) {
pControllerMap = s_pSupportedControllers;
s_pSupportedControllers = s_pSupportedControllers->next;
SDL_free(pControllerMap->name);
SDL_free(pControllerMap->mapping);
SDL_free(pControllerMap);
}
SDL_DelEventWatch(SDL_GameControllerEventWatcher, NULL);
}
SDL_DelEventWatch
#define SDL_DelEventWatch
Definition: SDL_dynapi_overrides.h:135
ControllerMapping_t::next
struct _ControllerMapping_t * next
Definition: SDL_gamecontroller.c:87
SDL_GameControllerClose
void SDL_GameControllerClose(SDL_GameController *gamecontroller)
Definition: SDL_gamecontroller.c:1161
SDL_gamecontrollers
static SDL_GameController * SDL_gamecontrollers
Definition: SDL_gamecontroller.c:39
s_pSupportedControllers
static ControllerMapping_t * s_pSupportedControllers
Definition: SDL_gamecontroller.c:90
SDL_free
void SDL_free(void *mem)
ControllerMapping_t::name
char * name
Definition: SDL_gamecontroller.c:85
ControllerMapping_t::mapping
char * mapping
Definition: SDL_gamecontroller.c:86
NULL
#define NULL
Definition: begin_code.h:143
ControllerMapping_t
Definition: SDL_gamecontroller.c:82
SDL_GameControllerEventWatcher
int SDL_GameControllerEventWatcher(void *userdata, SDL_Event *event)
Definition: SDL_gamecontroller.c:111

int SDL_JoystickInit | ( | void |  | ) |

Definition at line 50 of file SDL_joystick.c.

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

Referenced by SDL_InitSubSystem().

{
int status;
/* See if we should allow joystick events while in the background */
SDL_AddHintCallback(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS,
SDL_JoystickAllowBackgroundEventsChanged, NULL);
#if !SDL_EVENTS_DISABLED
if (SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) {
return -1;
}
#endif /* !SDL_EVENTS_DISABLED */
status = SDL_SYS_JoystickInit();
if (status >= 0) {
status = 0;
}
return (status);
}
SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
#define SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS
A variable that lets you enable joystick (and gamecontroller) events even when your app is in the bac...
Definition: SDL_hints.h:334
SDL_INIT_EVENTS
#define SDL_INIT_EVENTS
Definition: SDL.h:81
SDL_InitSubSystem
#define SDL_InitSubSystem
Definition: SDL_dynapi_overrides.h:61
SDL_SYS_JoystickInit
int SDL_SYS_JoystickInit(void)
Definition: SDL_sysjoystick.m:260
NULL
#define NULL
Definition: begin_code.h:143
SDL_AddHintCallback
#define SDL_AddHintCallback
Definition: SDL_dynapi_overrides.h:198
SDL_JoystickAllowBackgroundEventsChanged
static void SDL_JoystickAllowBackgroundEventsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
Definition: SDL_joystick.c:40

void SDL_JoystickQuit | ( | void |  | ) |

Definition at line 458 of file SDL_joystick.c.

References SDL_assert, SDL_INIT_EVENTS, SDL_JoystickClose(), SDL_joysticks, SDL_QuitSubSystem, SDL_SYS_JoystickQuit(), and SDL_updating_joystick.

Referenced by SDL_QuitSubSystem().

{
/* Make sure we're not getting called in the middle of updating joysticks */
SDL_assert(!SDL_updating_joystick);
/* Stop the event polling */
while (SDL_joysticks) {
SDL_joysticks->ref_count = 1;
SDL_JoystickClose(SDL_joysticks);
}
/* Quit the joystick setup */
SDL_SYS_JoystickQuit();
#if !SDL_EVENTS_DISABLED
SDL_QuitSubSystem(SDL_INIT_EVENTS);
#endif
}
SDL_INIT_EVENTS
#define SDL_INIT_EVENTS
Definition: SDL.h:81
SDL_updating_joystick
static SDL_Joystick * SDL_updating_joystick
Definition: SDL_joystick.c:37
SDL_JoystickClose
void SDL_JoystickClose(SDL_Joystick *joystick)
Definition: SDL_joystick.c:410
SDL_QuitSubSystem
#define SDL_QuitSubSystem
Definition: SDL_dynapi_overrides.h:62
SDL_SYS_JoystickQuit
void SDL_SYS_JoystickQuit(void)
Definition: SDL_sysjoystick.m:611
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:167
SDL_joysticks
static SDL_Joystick * SDL_joysticks
Definition: SDL_joystick.c:36

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

Definition at line 501 of file SDL_joystick.c.

References axis, SDL_ENABLE, SDL_GetEventState, SDL_JOYAXISMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

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

{
int posted;
/* Make sure we're not getting garbage or duplicate events */
if (axis >= joystick->naxes) {
return 0;
}
if (value == joystick->axes[axis]) {
return 0;
}
/* We ignore events if we don't have keyboard focus, except for centering
* events.
*/
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
if ((value > 0 && value >= joystick->axes[axis]) ||
(value < 0 && value <= joystick->axes[axis])) {
return 0;
}
}
/* Update internal joystick state */
joystick->axes[axis] = value;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_GetEventState(SDL_JOYAXISMOTION) == SDL_ENABLE) {
SDL_Event event;
event.type = SDL_JOYAXISMOTION;
event.jaxis.which = joystick->instance_id;
event.jaxis.axis = axis;
event.jaxis.value = value;
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
axis
SDL_Texture * axis
Definition: testgamecontroller.c:67
SDL_JOYAXISMOTION
Definition: SDL_events.h:108
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_PrivateJoystickShouldIgnoreEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
SDL_Event
General event structure.
Definition: SDL_events.h:521

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

Definition at line 582 of file SDL_joystick.c.

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

{
int posted;
/* Make sure we're not getting garbage events */
if (ball >= joystick->nballs) {
return 0;
}
/* We ignore events if we don't have keyboard focus. */
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
return 0;
}
/* Update internal mouse state */
joystick->balls[ball].dx += xrel;
joystick->balls[ball].dy += yrel;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_GetEventState(SDL_JOYBALLMOTION) == SDL_ENABLE) {
SDL_Event event;
event.jball.type = SDL_JOYBALLMOTION;
event.jball.which = joystick->instance_id;
event.jball.ball = ball;
event.jball.xrel = xrel;
event.jball.yrel = yrel;
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_JOYBALLMOTION
Definition: SDL_events.h:109
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_PrivateJoystickShouldIgnoreEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
SDL_Event
General event structure.
Definition: SDL_events.h:521

void SDL_PrivateJoystickBatteryLevel | ( | SDL_Joystick * | joystick,
|  | SDL_JoystickPowerLevel | ePowerLevel
| ) |  |

Definition at line 847 of file SDL_joystick.c.

{
joystick->epowerlevel = ePowerLevel;
}

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

Definition at line 618 of file SDL_joystick.c.

References button, SDL_ENABLE, SDL_GetEventState, SDL_JOYBUTTONDOWN, SDL_JOYBUTTONUP, SDL_PRESSED, SDL_PrivateJoystickShouldIgnoreEvent(), SDL_PushEvent, SDL_RELEASED, state, and SDL_Event::type.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

{
int posted;
#if !SDL_EVENTS_DISABLED
SDL_Event event;
switch (state) {
case SDL_PRESSED:
event.type = SDL_JOYBUTTONDOWN;
break;
case SDL_RELEASED:
event.type = SDL_JOYBUTTONUP;
break;
default:
/* Invalid state -- bail */
return (0);
}
#endif /* !SDL_EVENTS_DISABLED */
/* Make sure we're not getting garbage or duplicate events */
if (button >= joystick->nbuttons) {
return 0;
}
if (state == joystick->buttons[button]) {
return 0;
}
/* We ignore events if we don't have keyboard focus, except for button
* release. */
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
if (state == SDL_PRESSED) {
return 0;
}
}
/* Update internal joystick state */
joystick->buttons[button] = state;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_GetEventState(event.type) == SDL_ENABLE) {
event.jbutton.which = joystick->instance_id;
event.jbutton.button = button;
event.jbutton.state = state;
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
SDL_JOYBUTTONUP
Definition: SDL_events.h:112
button
SDL_Texture * button
Definition: testgamecontroller.c:67
state
struct xkb_state * state
Definition: SDL_waylandsym.h:97
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
SDL_JOYBUTTONDOWN
Definition: SDL_events.h:111
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_PrivateJoystickShouldIgnoreEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_PRESSED
#define SDL_PRESSED
Definition: SDL_events.h:50
SDL_RELEASED
#define SDL_RELEASED
Definition: SDL_events.h:49
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523

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

Definition at line 542 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_HAT_CENTERED, SDL_JOYHATMOTION, SDL_PrivateJoystickShouldIgnoreEvent(), and SDL_PushEvent.

Referenced by SDL_JoystickUpdate(), and SDL_SYS_MFIJoystickUpdate().

{
int posted;
/* Make sure we're not getting garbage or duplicate events */
if (hat >= joystick->nhats) {
return 0;
}
if (value == joystick->hats[hat]) {
return 0;
}
/* We ignore events if we don't have keyboard focus, except for centering
* events.
*/
if (SDL_PrivateJoystickShouldIgnoreEvent()) {
if (value != SDL_HAT_CENTERED) {
return 0;
}
}
/* Update internal joystick state */
joystick->hats[hat] = value;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
if (SDL_GetEventState(SDL_JOYHATMOTION) == SDL_ENABLE) {
SDL_Event event;
event.jhat.type = SDL_JOYHATMOTION;
event.jhat.which = joystick->instance_id;
event.jhat.hat = hat;
event.jhat.value = value;
posted = SDL_PushEvent(&event) == 1;
}
#endif /* !SDL_EVENTS_DISABLED */
return (posted);
}
SDL_JOYHATMOTION
Definition: SDL_events.h:110
SDL_ENABLE
#define SDL_ENABLE
Definition: SDL_events.h:718
SDL_GetEventState
#define SDL_GetEventState(type)
Definition: SDL_events.h:731
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_PushEvent
#define SDL_PushEvent
Definition: SDL_dynapi_overrides.h:131
SDL_PrivateJoystickShouldIgnoreEvent
static SDL_bool SDL_PrivateJoystickShouldIgnoreEvent()
Definition: SDL_joystick.c:479
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_HAT_CENTERED
#define SDL_HAT_CENTERED
Definition: SDL_joystick.h:207

int SDL_PrivateJoystickValid | ( | SDL_Joystick * | joystick | ) |

Definition at line 199 of file SDL_joystick.c.

References NULL, and SDL_SetError.

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

{
int valid;
if (joystick == NULL) {
SDL_SetError("Joystick hasn't been opened yet");
valid = 0;
} else {
valid = 1;
}
return valid;
}
NULL
#define NULL
Definition: begin_code.h:143
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36