SDL  2.0
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)
SDL_JoystickID SDL_GetNextJoystickInstanceID (void)
int SDL_GameControllerInitMappings (void)
void SDL_GameControllerQuitMappings (void)
int SDL_GameControllerInit (void)
void SDL_GameControllerQuit (void)
SDL_bool SDL_GetDriverAndJoystickIndex (int device_index, struct _SDL_JoystickDriver **driver, int *driver_index)
int SDL_JoystickGetDeviceIndexFromInstanceID (SDL_JoystickID instance_id)
void SDL_GetJoystickGUIDInfo (SDL_JoystickGUID guid, Uint16 *vendor, Uint16 *product, Uint16 *version)
SDL_bool SDL_IsJoystickPS4 (Uint16 vendor_id, Uint16 product_id)
SDL_bool SDL_IsJoystickNintendoSwitchPro (Uint16 vendor_id, Uint16 product_id)
SDL_bool SDL_IsJoystickSteamController (Uint16 vendor_id, Uint16 product_id)
SDL_bool SDL_IsJoystickXbox360 (Uint16 vendor_id, Uint16 product_id)
SDL_bool SDL_IsJoystickXboxOne (Uint16 vendor_id, Uint16 product_id)
SDL_bool SDL_IsJoystickXInput (SDL_JoystickGUID guid)
SDL_bool SDL_IsJoystickHIDAPI (SDL_JoystickGUID guid)
SDL_bool SDL_ShouldIgnoreJoystick (const char *name, SDL_JoystickGUID guid)
SDL_bool SDL_IsGameControllerNameAndGUID (const char *name, SDL_JoystickGUID guid)
SDL_bool SDL_ShouldIgnoreGameController (const char *name, SDL_JoystickGUID guid)
void SDL_GameControllerHandleDelayedGuideButton (SDL_Joystick *joystick)
void SDL_PrivateJoystickAdded (SDL_JoystickID device_instance)
void SDL_PrivateJoystickRemoved (SDL_JoystickID device_instance)
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

void SDL_GameControllerHandleDelayedGuideButton ( SDL_Joystick *  joystick)

Definition at line 2063 of file SDL_gamecontroller.c.

References SDL_CONTROLLER_BUTTON_GUIDE, SDL_gamecontrollers, SDL_PrivateGameControllerButton(), and SDL_RELEASED.

Referenced by SDL_JoystickUpdate().

{
SDL_GameController *controllerlist = SDL_gamecontrollers;
while (controllerlist) {
if (controllerlist->joystick == joystick) {
break;
}
controllerlist = controllerlist->next;
}
}
int SDL_GameControllerInit ( void  )

Definition at line 1358 of file SDL_gamecontroller.c.

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

Referenced by SDL_InitSubSystem().

{
int i;
/* watch for joy events and fire controller ones if needed */
/* Send added events for controllers currently attached */
for (i = 0; i < SDL_NumJoysticks(); ++i) {
SDL_Event deviceevent;
deviceevent.cdevice.which = i;
SDL_PushEvent(&deviceevent);
}
}
return (0);
}
SDL_bool SDL_GetDriverAndJoystickIndex ( int  device_index,
struct _SDL_JoystickDriver **  driver,
int *  driver_index 
)
void SDL_GetJoystickGUIDInfo ( SDL_JoystickGUID  guid,
Uint16 vendor,
Uint16 product,
Uint16 version 
)

Definition at line 1113 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(), SDL_ShouldIgnoreGameController(), and SDL_ShouldIgnoreJoystick().

{
Uint16 *guid16 = (Uint16 *)guid.data;
/* If the GUID fits the form of BUS 0000 VENDOR 0000 PRODUCT 0000, return the data */
if (/* guid16[0] is device bus type */
guid16[1] == 0x0000 &&
/* guid16[2] is vendor ID */
guid16[3] == 0x0000 &&
/* guid16[4] is product ID */
guid16[5] == 0x0000
/* guid16[6] is product version */
) {
if (vendor) {
*vendor = guid16[2];
}
if (product) {
*product = guid16[4];
}
if (version) {
*version = guid16[6];
}
} else {
if (vendor) {
*vendor = 0;
}
if (product) {
*product = 0;
}
if (version) {
*version = 0;
}
}
}
SDL_JoystickID SDL_GetNextJoystickInstanceID ( void  )

Definition at line 163 of file SDL_joystick.c.

References SDL_AtomicIncRef.

Referenced by IOS_AddJoystickDevice().

SDL_bool SDL_IsGameControllerNameAndGUID ( const char *  name,
SDL_JoystickGUID  guid 
)

Definition at line 1435 of file SDL_gamecontroller.c.

References SDL_FALSE, SDL_PrivateGetControllerMappingForNameAndGUID(), and SDL_TRUE.

Referenced by SDL_ShouldIgnoreJoystick().

{
if (pSupportedController) {
return SDL_TRUE;
}
return SDL_FALSE;
}
SDL_bool SDL_IsJoystickHIDAPI ( SDL_JoystickGUID  guid)

Definition at line 1192 of file SDL_joystick.c.

References SDL_JoystickGUID::data, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_PrivateGetControllerMappingForGUID(), and SDL_PrivateGetControllerMappingForNameAndGUID().

{
return (guid.data[14] == 'h') ? SDL_TRUE : SDL_FALSE;
}
SDL_bool SDL_IsJoystickNintendoSwitchPro ( Uint16  vendor_id,
Uint16  product_id 
)
SDL_bool SDL_IsJoystickPS4 ( Uint16  vendor_id,
Uint16  product_id 
)

Definition at line 1149 of file SDL_joystick.c.

References GuessControllerType(), and k_eControllerType_PS4Controller.

Referenced by SDL_ShouldIgnoreJoystick().

{
}
SDL_bool SDL_IsJoystickSteamController ( Uint16  vendor_id,
Uint16  product_id 
)

Definition at line 1161 of file SDL_joystick.c.

References BIsSteamController(), and GuessControllerType().

{
return BIsSteamController(GuessControllerType(vendor, product));
}
SDL_bool SDL_IsJoystickXbox360 ( Uint16  vendor_id,
Uint16  product_id 
)

Definition at line 1167 of file SDL_joystick.c.

References GuessControllerType(), k_eControllerType_XBox360Controller, and SDL_FALSE.

{
/* Filter out some bogus values here */
if (vendor == 0x0000 && product == 0x0000) {
return SDL_FALSE;
}
if (vendor == 0x0001 && product == 0x0001) {
return SDL_FALSE;
}
}
SDL_bool SDL_IsJoystickXboxOne ( Uint16  vendor_id,
Uint16  product_id 
)

Definition at line 1180 of file SDL_joystick.c.

References GuessControllerType(), and k_eControllerType_XBoxOneController.

{
}
SDL_bool SDL_IsJoystickXInput ( SDL_JoystickGUID  guid)

Definition at line 1186 of file SDL_joystick.c.

References SDL_JoystickGUID::data, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_GetJoystickGUIDType(), and SDL_PrivateGetControllerMappingForGUID().

{
return (guid.data[14] == 'x') ? SDL_TRUE : SDL_FALSE;
}
int SDL_JoystickGetDeviceIndexFromInstanceID ( SDL_JoystickID  instance_id)

Definition at line 1434 of file SDL_joystick.c.

References i, SDL_JoystickGetDeviceInstanceID, SDL_LockJoysticks, SDL_NumJoysticks, and SDL_UnlockJoysticks.

Referenced by SDL_PrivateJoystickAdded().

{
int i, num_joysticks, device_index = -1;
num_joysticks = SDL_NumJoysticks();
for (i = 0; i < num_joysticks; ++i) {
if (SDL_JoystickGetDeviceInstanceID(i) == instance_id) {
device_index = i;
break;
}
}
return device_index;
}
int SDL_JoystickInit ( void  )

Definition at line 114 of file SDL_joystick.c.

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

Referenced by SDL_InitSubSystem().

{
int i, status;
/* Create the joystick list lock */
}
/* See if we should allow joystick events while in the background */
#if !SDL_EVENTS_DISABLED
return -1;
}
#endif /* !SDL_EVENTS_DISABLED */
status = -1;
for (i = 0; i < SDL_arraysize(SDL_joystick_drivers); ++i) {
if (SDL_joystick_drivers[i]->Init() >= 0) {
status = 0;
}
}
return status;
}
void SDL_PrivateJoystickAdded ( SDL_JoystickID  device_instance)

Definition at line 751 of file SDL_joystick.c.

References SDL_ENABLE, SDL_GetEventState, SDL_JOYDEVICEADDED, SDL_JoystickGetDeviceIndexFromInstanceID(), SDL_PushEvent, and SDL_Event::type.

Referenced by IOS_AddJoystickDevice().

{
#if !SDL_EVENTS_DISABLED
int device_index;
device_index = SDL_JoystickGetDeviceIndexFromInstanceID(device_instance);
if (device_index < 0) {
return;
}
event.type = SDL_JOYDEVICEADDED;
event.jdevice.which = device_index;
SDL_PushEvent(&event);
}
#endif /* !SDL_EVENTS_DISABLED */
}
int SDL_PrivateJoystickAxis ( SDL_Joystick *  joystick,
Uint8  axis,
Sint16  value 
)

Definition at line 828 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 IOS_AccelerometerUpdate(), IOS_MFIJoystickUpdate(), SDL_JoystickUpdate(), and SDL_PrivateJoystickAxis().

{
int posted;
/* Make sure we're not getting garbage or duplicate events */
if (axis >= joystick->naxes) {
return 0;
}
if (!joystick->axes[axis].has_initial_value) {
joystick->axes[axis].initial_value = value;
joystick->axes[axis].value = value;
joystick->axes[axis].zero = value;
joystick->axes[axis].has_initial_value = SDL_TRUE;
}
if (value == joystick->axes[axis].value) {
return 0;
}
if (!joystick->axes[axis].sent_initial_value) {
/* Make sure we don't send motion until there's real activity on this axis */
const int MAX_ALLOWED_JITTER = SDL_JOYSTICK_AXIS_MAX / 80; /* ShanWan PS3 controller needed 96 */
if (SDL_abs(value - joystick->axes[axis].value) <= MAX_ALLOWED_JITTER) {
return 0;
}
joystick->axes[axis].sent_initial_value = SDL_TRUE;
joystick->axes[axis].value = value; /* Just so we pass the check above */
SDL_PrivateJoystickAxis(joystick, axis, joystick->axes[axis].initial_value);
}
/* We ignore events if we don't have keyboard focus, except for centering
* events.
*/
if ((value > joystick->axes[axis].zero && value >= joystick->axes[axis].value) ||
(value < joystick->axes[axis].zero && value <= joystick->axes[axis].value)) {
return 0;
}
}
/* Update internal joystick state */
joystick->axes[axis].value = value;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
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;
}
int SDL_PrivateJoystickBall ( SDL_Joystick *  joystick,
Uint8  ball,
Sint16  xrel,
Sint16  yrel 
)

Definition at line 925 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. */
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
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;
}
void SDL_PrivateJoystickBatteryLevel ( SDL_Joystick *  joystick,
SDL_JoystickPowerLevel  ePowerLevel 
)

Definition at line 1570 of file SDL_joystick.c.

{
joystick->epowerlevel = ePowerLevel;
}
int SDL_PrivateJoystickButton ( SDL_Joystick *  joystick,
Uint8  button,
Uint8  state 
)

Definition at line 961 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 IOS_MFIJoystickUpdate(), and SDL_JoystickUpdate().

{
int posted;
#if !SDL_EVENTS_DISABLED
switch (state) {
event.type = SDL_JOYBUTTONDOWN;
break;
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 (state == SDL_PRESSED) {
return 0;
}
}
/* Update internal joystick state */
joystick->buttons[button] = state;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
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;
}
int SDL_PrivateJoystickHat ( SDL_Joystick *  joystick,
Uint8  hat,
Uint8  value 
)

Definition at line 885 of file SDL_joystick.c.

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

Referenced by IOS_MFIJoystickUpdate(), and SDL_JoystickUpdate().

{
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.
*/
return 0;
}
}
/* Update internal joystick state */
joystick->hats[hat] = value;
/* Post the event, if desired */
posted = 0;
#if !SDL_EVENTS_DISABLED
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;
}
void SDL_PrivateJoystickRemoved ( SDL_JoystickID  device_instance)

Definition at line 800 of file SDL_joystick.c.

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

Referenced by IOS_RemoveJoystickDevice().

{
SDL_Joystick *joystick;
#if !SDL_EVENTS_DISABLED
event.type = SDL_JOYDEVICEREMOVED;
event.jdevice.which = device_instance;
SDL_PushEvent(&event);
}
#endif /* !SDL_EVENTS_DISABLED */
/* Mark this joystick as no longer attached */
for (joystick = SDL_joysticks; joystick; joystick = joystick->next) {
if (joystick->instance_id == device_instance) {
joystick->attached = SDL_FALSE;
joystick->force_recentering = SDL_TRUE;
break;
}
}
}
SDL_bool SDL_ShouldIgnoreGameController ( const char *  name,
SDL_JoystickGUID  guid 
)

Definition at line 1460 of file SDL_gamecontroller.c.

References SDL_vidpid_list::entries, i, MAKE_VIDPID, SDL_vidpid_list::num_entries, SDL_FALSE, SDL_GetHintBoolean, SDL_GetJoystickGUIDInfo(), and SDL_TRUE.

Referenced by SDL_ShouldIgnoreJoystick().

{
int i;
Uint16 vendor;
Uint16 product;
Uint16 version;
Uint32 vidpid;
return SDL_FALSE;
}
SDL_GetJoystickGUIDInfo(guid, &vendor, &product, &version);
if (SDL_GetHintBoolean("SDL_GAMECONTROLLER_ALLOW_STEAM_VIRTUAL_GAMEPAD", SDL_FALSE)) {
/* We shouldn't ignore Steam's virtual gamepad since it's using the hints to filter out the real controllers so it can remap input for the virtual controller */
SDL_bool bSteamVirtualGamepad = SDL_FALSE;
#if defined(__LINUX__)
bSteamVirtualGamepad = (vendor == 0x28DE && product == 0x11FF);
#elif defined(__MACOSX__)
bSteamVirtualGamepad = (vendor == 0x045E && product == 0x028E && version == 1);
#elif defined(__WIN32__)
/* We can't tell on Windows, but Steam will block others in input hooks */
bSteamVirtualGamepad = SDL_TRUE;
#endif
if (bSteamVirtualGamepad) {
return SDL_FALSE;
}
}
vidpid = MAKE_VIDPID(vendor, product);
for (i = 0; i < SDL_allowed_controllers.num_entries; ++i) {
if (vidpid == SDL_allowed_controllers.entries[i]) {
return SDL_FALSE;
}
}
return SDL_TRUE;
} else {
for (i = 0; i < SDL_ignored_controllers.num_entries; ++i) {
if (vidpid == SDL_ignored_controllers.entries[i]) {
return SDL_TRUE;
}
}
return SDL_FALSE;
}
}
SDL_bool SDL_ShouldIgnoreJoystick ( const char *  name,
SDL_JoystickGUID  guid 
)