From e7b8cc2785ea3a8ad7b905a3e516febf0e80f290 Mon Sep 17 00:00:00 2001 From: Gareth Francis Date: Fri, 8 Jun 2018 17:19:51 +0100 Subject: [PATCH] Preserve event timestamps in X11 input driver The X11 XKeyEvent structure (and others) include a timestamp field, with 1ms resolution. This was previously ignored causing the input timestamps to be set to the middle of each frame. If the timestamps are passed through then the event timestamps are not coupled to the framerate and the polling issues are resolved. --- src/arch/InputHandler/InputHandler_X11.cpp | 31 +++++++++++++++++++--- src/arch/InputHandler/InputHandler_X11.h | 3 +++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/arch/InputHandler/InputHandler_X11.cpp b/src/arch/InputHandler/InputHandler_X11.cpp index cd285b526c..61692e7eca 100644 --- a/src/arch/InputHandler/InputHandler_X11.cpp +++ b/src/arch/InputHandler/InputHandler_X11.cpp @@ -203,9 +203,9 @@ void InputHandler_X11::Update() lastEvent.type = 0; continue; } + // This is a new event so the last release was not a repeat. - ButtonPressed( DeviceInput(DEVICE_KEYBOARD, lastDB, 0) ); - lastEvent.type = 0; + RegisterKeyEvent( event.xkey.time, false, lastDB ); } if( event.type == FocusOut ) @@ -221,7 +221,9 @@ void InputHandler_X11::Update() continue; if( bKeyPress ) - ButtonPressed( DeviceInput(DEVICE_KEYBOARD, lastDB, 1) ); + { + RegisterKeyEvent( event.xkey.time, true, lastDB ); + } /* else if( bMousePress ) ButtonPressed( DeviceInput(DEVICE_MOUSE, lastDB, 1) ); @@ -234,7 +236,9 @@ void InputHandler_X11::Update() if( lastEvent.type != 0 ) { if( lastEvent.type == (KeyPress|KeyRelease) ) - ButtonPressed( DeviceInput(DEVICE_KEYBOARD, lastDB, 0) ); + { + RegisterKeyEvent( event.xkey.time, false, lastDB ); + } /* if( lastEvent.type == (ButtonPress|ButtonRelease) ) ButtonPressed( DeviceInput(DEVICE_MOUSE, lastDB, 0) ); @@ -254,6 +258,25 @@ void InputHandler_X11::GetDevicesAndDescriptions( vector& vDevi } } +void InputHandler_X11::RegisterKeyEvent( unsigned long timestamp, bool keyDown, DeviceButton button ) +{ + // https://linux.die.net/man/3/xkeyevent + // Event timestamp is in milliseconds + + // seconds, microseconds + RageTimer timer( + timestamp / 1000, + (timestamp % 1000) * 1000 ); + + DeviceInput di( + DEVICE_KEYBOARD, + button, + keyDown ? 1.0f:0.0f, + timer); + + ButtonPressed( di ); +} + /* * (c) 2005, 2006 Sean Burke, Ben Anderson, Steve Checkoway * All rights reserved. diff --git a/src/arch/InputHandler/InputHandler_X11.h b/src/arch/InputHandler/InputHandler_X11.h index bb53c4e4d7..49905546e4 100644 --- a/src/arch/InputHandler/InputHandler_X11.h +++ b/src/arch/InputHandler/InputHandler_X11.h @@ -12,6 +12,9 @@ public: ~InputHandler_X11(); void Update(); void GetDevicesAndDescriptions( vector& vDevicesOut ); +private: + // timestamp is unsigned long to match X11 Time type + void RegisterKeyEvent( unsigned long timestamp, bool keyDown, DeviceButton button ); }; #endif