From 4be17068762b69a27477c13f2a73cc552367c000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Nov 2023 14:24:10 +0100 Subject: [PATCH] SDL: Add axis event deduplication We now do this in the backedns instead of centrally since on some backends this is more efficient. --- SDL/SDLJoystick.cpp | 29 +++++++++++++++++++++-------- SDL/SDLJoystick.h | 4 ++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/SDL/SDLJoystick.cpp b/SDL/SDLJoystick.cpp index bcaf616c7d..d9d0644340 100644 --- a/SDL/SDLJoystick.cpp +++ b/SDL/SDLJoystick.cpp @@ -182,15 +182,28 @@ void SDLJoystick::ProcessInput(const SDL_Event &event){ } break; case SDL_CONTROLLERAXISMOTION: - AxisInput axis; - // TODO: Can we really cast axis events like that? Do they match? - axis.axisId = (InputAxis)event.caxis.axis; - axis.value = event.caxis.value / 32767.0f; - if (axis.value > 1.0f) axis.value = 1.0f; - if (axis.value < -1.0f) axis.value = -1.0f; - axis.deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which); - NativeAxis(&axis, 1); + { + InputDeviceID deviceId = DEVICE_ID_PAD_0 + getDeviceIndex(event.caxis.which); + // TODO: Can we really cast axis IDs like that? Do they match? + InputAxis axisId = (InputAxis)event.caxis.axis; + float value = event.caxis.value * (1.f / 32767.f); + if (value > 1.0f) value = 1.0f; + if (value < -1.0f) value = -1.0f; + // Filter duplicate axis values. + auto key = std::pair(deviceId, axisId); + auto iter = prevAxisValue_.find(key); + if (iter == prevAxisValue_.end()) { + prevAxisValue_[key] = value; + } else if (iter->second != value) { + iter->second = value; + AxisInput axis; + axis.axisId = axisId; + axis.value = value; + axis.deviceId = deviceId; + NativeAxis(&axis, 1); + } // else ignore event. break; + } case SDL_CONTROLLERDEVICEREMOVED: // for removal events, "which" is the instance ID for SDL_CONTROLLERDEVICEREMOVED for (auto it = controllers.begin(); it != controllers.end(); ++it) { diff --git a/SDL/SDLJoystick.h b/SDL/SDLJoystick.h index e91bfd0b5c..50cc43c4c7 100644 --- a/SDL/SDLJoystick.h +++ b/SDL/SDLJoystick.h @@ -27,7 +27,11 @@ private: void setUpControllers(); InputKeyCode getKeycodeForButton(SDL_GameControllerButton button); int getDeviceIndex(int instanceId); + bool registeredAsEventHandler; std::vector controllers; std::map controllerDeviceMap; + + // Deduplicate axis events. Pair is device, axis. + std::map, float> prevAxisValue_; };