Don't send controller gyro and accelerometer updates if the values haven't changed. This keeps SDL_PollEvent() from returning data continuously when polling controller state.
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
diff --git a/src/joystick/SDL_joystick.c b/src/joystick/SDL_joystick.c
index 4c5623e..8516dd9 100644
--- a/src/joystick/SDL_joystick.c
+++ b/src/joystick/SDL_joystick.c
@@ -2569,26 +2569,26 @@ int SDL_PrivateJoystickSensor(SDL_Joystick *joystick, SDL_SensorType type, const
if (sensor->type == type) {
if (sensor->enabled) {
- /* Allow duplicate events, for things like gyro updates */
-
- /* Update internal sensor state */
num_values = SDL_min(num_values, SDL_arraysize(sensor->data));
- SDL_memcpy(sensor->data, data, num_values*sizeof(*data));
+ if (SDL_memcmp(data, sensor->data, num_values*sizeof(*data)) != 0) {
+
+ /* Update internal sensor state */
+ SDL_memcpy(sensor->data, data, num_values*sizeof(*data));
- /* Post the event, if desired */
- posted = 0;
+ /* Post the event, if desired */
#if !SDL_EVENTS_DISABLED
- if (SDL_GetEventState(SDL_CONTROLLERSENSORUPDATE) == SDL_ENABLE) {
- SDL_Event event;
- event.type = SDL_CONTROLLERSENSORUPDATE;
- event.csensor.which = joystick->instance_id;
- event.csensor.sensor = type;
- num_values = SDL_min(num_values, SDL_arraysize(event.csensor.data));
- SDL_memset(event.csensor.data, 0, sizeof(event.csensor.data));
- SDL_memcpy(event.csensor.data, data, num_values*sizeof(*data));
- posted = SDL_PushEvent(&event) == 1;
- }
+ if (SDL_GetEventState(SDL_CONTROLLERSENSORUPDATE) == SDL_ENABLE) {
+ SDL_Event event;
+ event.type = SDL_CONTROLLERSENSORUPDATE;
+ event.csensor.which = joystick->instance_id;
+ event.csensor.sensor = type;
+ num_values = SDL_min(num_values, SDL_arraysize(event.csensor.data));
+ SDL_memset(event.csensor.data, 0, sizeof(event.csensor.data));
+ SDL_memcpy(event.csensor.data, data, num_values*sizeof(*data));
+ posted = SDL_PushEvent(&event) == 1;
+ }
#endif /* !SDL_EVENTS_DISABLED */
+ }
}
break;
}