Skip to content

Commit

Permalink
Added hints to control whether SDL updates joystick and sensor state …
Browse files Browse the repository at this point in the history
…in the main event loop
  • Loading branch information
slouken committed Dec 18, 2020
1 parent 6c4ab48 commit 5f7cd1f
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
26 changes: 26 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -1302,6 +1302,32 @@ extern "C" {
#define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"


/**
* \brief A variable controlling whether SDL updates joystick state when getting input events
*
* This variable can be set to the following values:
*
* "0" - You'll call SDL_JoystickUpdate() manually
* "1" - SDL will automatically call SDL_JoystickUpdate() (default)
*
* This hint can be toggled on and off at runtime.
*/
#define SDL_HINT_AUTO_UPDATE_JOYSTICKS "SDL_AUTO_UPDATE_JOYSTICKS"


/**
* \brief A variable controlling whether SDL updates sensor state when getting input events
*
* This variable can be set to the following values:
*
* "0" - You'll call SDL_SensorUpdate() manually
* "1" - SDL will automatically call SDL_SensorUpdate() (default)
*
* This hint can be toggled on and off at runtime.
*/
#define SDL_HINT_AUTO_UPDATE_SENSORS "SDL_AUTO_UPDATE_SENSORS"


/**
* \brief A variable controlling whether SDL logs all events pushed onto its internal queue.
*
Expand Down
75 changes: 73 additions & 2 deletions src/events/SDL_events.c
Expand Up @@ -92,6 +92,54 @@ static struct
} SDL_EventQ = { NULL, { 1 }, { 0 }, 0, NULL, NULL, NULL, NULL, NULL };


#if !SDL_JOYSTICK_DISABLED

static SDL_bool SDL_update_joysticks = SDL_TRUE;

static void
SDL_CalculateShouldUpdateJoysticks()
{
if (SDL_GetHintBoolean(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_TRUE) &&
(!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] || SDL_JoystickEventState(SDL_QUERY))) {
SDL_update_joysticks = SDL_TRUE;
} else {
SDL_update_joysticks = SDL_FALSE;
}
}

static void SDLCALL
SDL_AutoUpdateJoysticksChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_CalculateShouldUpdateJoysticks();
}

#endif /* !SDL_JOYSTICK_DISABLED */


#if !SDL_SENSOR_DISABLED

static SDL_bool SDL_update_sensors = SDL_TRUE;

static void
SDL_CalculateShouldUpdateSensors()
{
if (SDL_GetHintBoolean(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_TRUE) &&
!SDL_disabled_events[SDL_SENSORUPDATE >> 8]) {
SDL_update_sensors = SDL_TRUE;
} else {
SDL_update_sensors = SDL_FALSE;
}
}

static void SDLCALL
SDL_AutoUpdateSensorsChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_CalculateShouldUpdateSensors();
}

#endif /* !SDL_SENSOR_DISABLED */


/* 0 (default) means no logging, 1 means logging, 2 means logging with mouse and finger motion */
static int SDL_DoEventLogging = 0;

Expand Down Expand Up @@ -688,14 +736,14 @@ SDL_PumpEvents(void)

#if !SDL_JOYSTICK_DISABLED
/* Check for joystick state change */
if ((!SDL_disabled_events[SDL_JOYAXISMOTION >> 8] || SDL_JoystickEventState(SDL_QUERY))) {
if (SDL_update_joysticks) {
SDL_JoystickUpdate();
}
#endif

#if !SDL_SENSOR_DISABLED
/* Check for sensor state change */
if (!SDL_disabled_events[SDL_SENSORUPDATE >> 8]) {
if (SDL_update_sensors) {
SDL_SensorUpdate();
}
#endif
Expand Down Expand Up @@ -947,6 +995,17 @@ SDL_EventState(Uint32 type, int state)
/* Querying state... */
break;
}

#if !SDL_JOYSTICK_DISABLED
if (state == SDL_DISABLE || state == SDL_ENABLE) {
SDL_CalculateShouldUpdateJoysticks();
}
#endif
#if !SDL_SENSOR_DISABLED
if (state == SDL_DISABLE || state == SDL_ENABLE) {
SDL_CalculateShouldUpdateSensors();
}
#endif
}

/* turn off drag'n'drop support if we've disabled the events.
Expand Down Expand Up @@ -1018,6 +1077,12 @@ SDL_SendLocaleChangedEvent(void)
int
SDL_EventsInit(void)
{
#if !SDL_JOYSTICK_DISABLED
SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
#endif
#if !SDL_SENSOR_DISABLED
SDL_AddHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
#endif
SDL_AddHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
if (SDL_StartEventLoop() < 0) {
SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
Expand All @@ -1035,6 +1100,12 @@ SDL_EventsQuit(void)
SDL_QuitQuit();
SDL_StopEventLoop();
SDL_DelHintCallback(SDL_HINT_EVENT_LOGGING, SDL_EventLoggingChanged, NULL);
#if !SDL_JOYSTICK_DISABLED
SDL_DelHintCallback(SDL_HINT_AUTO_UPDATE_JOYSTICKS, SDL_AutoUpdateJoysticksChanged, NULL);
#endif
#if !SDL_SENSOR_DISABLED
SDL_DelHintCallback(SDL_HINT_AUTO_UPDATE_SENSORS, SDL_AutoUpdateSensorsChanged, NULL);
#endif
}

/* vi: set ts=4 sw=4 expandtab: */

0 comments on commit 5f7cd1f

Please sign in to comment.