Added hints to control whether SDL updates joystick and sensor state in the main event loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 100ecbb..50a5a08 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -1303,6 +1303,32 @@ extern "C" {
/**
+ * \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.
*
* This variable can be set to the following values:
diff --git a/src/events/SDL_events.c b/src/events/SDL_events.c
index ec9a181..a57bdd5 100644
--- a/src/events/SDL_events.c
+++ b/src/events/SDL_events.c
@@ -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;
@@ -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
@@ -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.
@@ -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);
@@ -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: */