From 9a71b4c237c0885a6cfc8be4db84f192e1c58b08 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Tue, 4 Sep 2018 00:09:35 -0700 Subject: [PATCH] Improve mouse batching to only batch if the last event was less than 1 millisecond ago --- app/streaming/input.cpp | 17 +++++++++++++---- app/streaming/input.hpp | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/app/streaming/input.cpp b/app/streaming/input.cpp index 9977041b..877858a0 100644 --- a/app/streaming/input.cpp +++ b/app/streaming/input.cpp @@ -23,7 +23,8 @@ const int SdlInputHandler::k_ButtonMap[] = { }; SdlInputHandler::SdlInputHandler(bool multiController) - : m_MultiController(multiController) + : m_LastMouseMotionTime(0), + m_MultiController(multiController) { // Allow gamepad input when the app doesn't have focus SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); @@ -391,11 +392,19 @@ void SdlInputHandler::handleMouseMotionEvent(SDL_MouseMotionEvent* event) short xdelta = (short)event->xrel; short ydelta = (short)event->yrel; - // Delay for 1 ms to allow batching of mouse move - // events from high DPI mice. - SDL_Delay(1); + // If we're sending more than one motion event per millisecond, + // delay for 1 ms to allow batching of mouse move events. + Uint32 currentTime = SDL_GetTicks(); + if (!SDL_TICKS_PASSED(currentTime, m_LastMouseMotionTime + 1)) { + SDL_Delay(1); + currentTime = SDL_GetTicks(); + } + m_LastMouseMotionTime = currentTime; + + // Pump even if we didn't delay since we might get some extra events SDL_PumpEvents(); + // Batch all of the pending mouse motion events SDL_Event nextEvent; while (SDL_PeepEvents(&nextEvent, 1, diff --git a/app/streaming/input.hpp b/app/streaming/input.hpp index 5a9991df..db040a02 100644 --- a/app/streaming/input.hpp +++ b/app/streaming/input.hpp @@ -44,6 +44,7 @@ private: void sendGamepadState(GamepadState* state); + Uint32 m_LastMouseMotionTime; bool m_MultiController; int m_GamepadMask; GamepadState m_GamepadState[MAX_GAMEPADS];