diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 974ba58ab6..4a4b57cfa8 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -484,3 +484,14 @@ not wait on fences attached to a dma-buf, so the composite now checks each outpu It picks the next image that is neither held by the host nor still being scanned out. Only when every free image is still on the display does it wait for one, up to 100 ms. On kernels that cannot attach the fence, the host keeps waiting itself and nothing changes. `tools/gamescope/tests/nested-swapchain.cpp` covers the choice and the busy check. + +## 0147 wlserver: a touch click presses after the pointer has moved to the finger + +In the Left, Right and Middle touch click modes a touch-down warped the pointer to the finger and pressed the button in +the same instant. A game that handles the press before it updates the hovered item on that frame acted on the item +under the previous tap. When the touch-down moves the pointer, the press now waits 34 ms, about one frame at 30 fps, so +the game sees the hover first. A tap that lifts sooner, or within 50 ms of the press, keeps the button down for 50 ms, +so the game still sees a press and a release on separate frames. A finger that moves more than 16 pixels before the +press presses at once where it touched, so drags start under the finger. A new touch first finishes the previous tap. +A touch that does not move the pointer, and trackpad mode, press at once as before. +`tools/tests/test_gamescope_touch_click_hover.py` compiles the rule from this patch. diff --git a/tools/gamescope/patches/0147-touch-click-press-after-hover.patch b/tools/gamescope/patches/0147-touch-click-press-after-hover.patch new file mode 100644 index 0000000000..e59d4c09ad --- /dev/null +++ b/tools/gamescope/patches/0147-touch-click-press-after-hover.patch @@ -0,0 +1,248 @@ +diff --git a/src/wlserver.cpp b/src/wlserver.cpp +--- a/src/wlserver.cpp ++++ b/src/wlserver.cpp +@@ -3315,6 +3315,167 @@ static bool touch_click_lift( TouchClickState &state, int nTouchId ) + return !bTracked && state.nOwner < 0; + } + ++static bool touch_click_release_held( uint32_t time ) ++{ ++ bool bReleasedAny = false; ++ for ( int i = 0; i < WLSERVER_BUTTON_COUNT; i++ ) ++ { ++ if ( wlserver.button_held[ i ] == true ) ++ { ++ uint32_t button = TouchClickModeToLinuxButton( (gamescope::TouchClickMode) i ); ++ ++ if ( button != 0 ) ++ { ++ wlr_seat_pointer_notify_button( wlserver.wlr.seat, time, button, WL_POINTER_BUTTON_STATE_RELEASED ); ++ bReleasedAny = true; ++ } ++ ++ wlserver.button_held[ i ] = false; ++ } ++ } ++ ++ if ( bReleasedAny == true ) ++ { ++ wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); ++ } ++ return bReleasedAny; ++} ++ ++static constexpr uint64_t k_ulTouchClickHoverNs = 34'000'000ul; ++static constexpr uint64_t k_ulTouchClickHoldNs = 50'000'000ul; ++static constexpr double k_flTouchClickSlop = 16.0; ++ ++enum class TouchClickStep { None, Press, Release }; ++ ++struct TouchClickTap ++{ ++ uint32_t uButton = 0; ++ uint32_t uTime = 0; ++ uint64_t ulEventAt = 0; ++ uint64_t ulPressedAt = 0; ++ double flDownX = 0.0; ++ double flDownY = 0.0; ++ bool bPressPending = false; ++ bool bLifted = false; ++}; ++ ++static uint64_t touch_click_tap_down( TouchClickTap &tap, uint32_t uButton, uint32_t uTime, uint64_t ulNow, double flX, double flY, bool bMoved ) ++{ ++ tap = TouchClickTap{}; ++ tap.uButton = uButton; ++ tap.uTime = uTime; ++ tap.ulEventAt = ulNow; ++ tap.flDownX = flX; ++ tap.flDownY = flY; ++ tap.bPressPending = bMoved; ++ if ( !bMoved ) ++ tap.ulPressedAt = ulNow; ++ return bMoved ? k_ulTouchClickHoverNs : 0; ++} ++ ++static bool touch_click_tap_motion( const TouchClickTap &tap, double flX, double flY ) ++{ ++ return tap.bPressPending && std::hypot( flX - tap.flDownX, flY - tap.flDownY ) > k_flTouchClickSlop; ++} ++ ++static void touch_click_tap_pressed( TouchClickTap &tap, uint64_t ulNow ) ++{ ++ tap.bPressPending = false; ++ tap.ulPressedAt = ulNow; ++} ++ ++static bool touch_click_tap_up( TouchClickTap &tap, uint32_t uTime, uint64_t ulNow, uint64_t *pDelay ) ++{ ++ tap.bLifted = true; ++ tap.uTime = uTime; ++ tap.ulEventAt = ulNow; ++ *pDelay = 0; ++ if ( tap.bPressPending ) ++ return true; ++ const uint64_t ulHeld = ulNow - tap.ulPressedAt; ++ if ( ulHeld >= k_ulTouchClickHoldNs ) ++ { ++ tap = TouchClickTap{}; ++ return false; ++ } ++ *pDelay = k_ulTouchClickHoldNs - ulHeld; ++ return true; ++} ++ ++static TouchClickStep touch_click_tap_fire( TouchClickTap &tap, uint64_t ulNow, uint64_t *pDelay ) ++{ ++ *pDelay = 0; ++ if ( tap.bPressPending ) ++ { ++ touch_click_tap_pressed( tap, ulNow ); ++ if ( tap.bLifted ) ++ *pDelay = k_ulTouchClickHoldNs; ++ return TouchClickStep::Press; ++ } ++ if ( tap.bLifted ) ++ { ++ tap = TouchClickTap{}; ++ return TouchClickStep::Release; ++ } ++ return TouchClickStep::None; ++} ++ ++static uint32_t touch_click_tap_time( const TouchClickTap &tap, uint64_t ulNow ) ++{ ++ return tap.uTime + uint32_t( ( ulNow - tap.ulEventAt ) / 1'000'000ul ); ++} ++ ++static TouchClickTap s_TouchTap; ++static struct wl_event_source *s_pTouchTapTimer = nullptr; ++ ++static bool touch_click_tap_arm( uint64_t ulDelay ) ++{ ++ if ( !s_pTouchTapTimer ) ++ s_pTouchTapTimer = wl_event_loop_add_timer( wlserver.event_loop, []( void * ) -> int ++ { ++ uint64_t ulDelay = 0; ++ const uint64_t ulNow = get_time_in_nanos(); ++ const uint32_t uTime = touch_click_tap_time( s_TouchTap, ulNow ); ++ const uint32_t uButton = s_TouchTap.uButton; ++ const TouchClickStep eStep = touch_click_tap_fire( s_TouchTap, ulNow, &ulDelay ); ++ if ( eStep == TouchClickStep::Press ) ++ { ++ wlr_seat_pointer_notify_button( wlserver.wlr.seat, uTime, uButton, WL_POINTER_BUTTON_STATE_PRESSED ); ++ wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); ++ if ( ulDelay ) ++ touch_click_tap_arm( ulDelay ); ++ } ++ else if ( eStep == TouchClickStep::Release ) ++ { ++ touch_click_release_held( uTime ); ++ } ++ bump_input_counter(); ++ return 0; ++ }, nullptr ); ++ if ( !s_pTouchTapTimer ) ++ return false; ++ wl_event_source_timer_update( s_pTouchTapTimer, int( std::max( 1, ( ulDelay + 999'999ul ) / 1'000'000ul ) ) ); ++ return true; ++} ++ ++static void touch_click_tap_settle( uint64_t ulNow ) ++{ ++ if ( s_pTouchTapTimer ) ++ wl_event_source_timer_update( s_pTouchTapTimer, 0 ); ++ uint64_t ulDelay = 0; ++ const uint32_t uTime = touch_click_tap_time( s_TouchTap, ulNow ); ++ const uint32_t uButton = s_TouchTap.uButton; ++ TouchClickStep eStep = touch_click_tap_fire( s_TouchTap, ulNow, &ulDelay ); ++ if ( eStep == TouchClickStep::Press ) ++ { ++ wlr_seat_pointer_notify_button( wlserver.wlr.seat, uTime, uButton, WL_POINTER_BUTTON_STATE_PRESSED ); ++ wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); ++ eStep = touch_click_tap_fire( s_TouchTap, ulNow, &ulDelay ); ++ } ++ if ( eStep == TouchClickStep::Release ) ++ touch_click_release_held( uTime ); ++} ++ + void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool bAlwaysWarpCursor, gamescope::IBackendConnector* connector ) + { + assert( wlserver_is_lock_held() ); +@@ -3372,6 +3533,9 @@ void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool + { + g_bPendingTouchMovement = true; + ++ if ( touch_click_drives( s_TouchClick, touch_id ) && touch_click_tap_motion( s_TouchTap, tx, ty ) ) ++ touch_click_tap_settle( get_time_in_nanos() ); ++ + wlserver_mousewarp( tx, ty, time, false, !bAlwaysWarpCursor ); + } + } +@@ -3426,18 +3590,30 @@ void wlserver_touchdown( double x, double y, int touch_id, uint32_t time, gamesc + { + g_bPendingTouchMovement = true; + ++ const uint64_t ulNow = get_time_in_nanos(); ++ touch_click_tap_settle( ulNow ); ++ ++ bool bMoved = false; + if ( eMode != gamescope::TouchClickModes::Trackpad ) + { ++ const double flFromX = wlserver.mouse_surface_cursorx; ++ const double flFromY = wlserver.mouse_surface_cursory; + wlserver_mousewarp( tx, ty, time, false, true ); + wlserver.ulTouchDownWarps++; ++ bMoved = flFromX != wlserver.mouse_surface_cursorx || flFromY != wlserver.mouse_surface_cursory; + } + + uint32_t button = TouchClickModeToLinuxButton( eMode ); + + if ( touch_click_press( wlserver.button_held, eMode, button ) ) + { +- wlr_seat_pointer_notify_button( wlserver.wlr.seat, time, button, WL_POINTER_BUTTON_STATE_PRESSED ); +- wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); ++ const uint64_t ulDelay = touch_click_tap_down( s_TouchTap, button, time, ulNow, tx, ty, bMoved ); ++ if ( !ulDelay || !touch_click_tap_arm( ulDelay ) ) ++ { ++ touch_click_tap_pressed( s_TouchTap, ulNow ); ++ wlr_seat_pointer_notify_button( wlserver.wlr.seat, time, button, WL_POINTER_BUTTON_STATE_PRESSED ); ++ wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); ++ } + } + } + } +@@ -3450,28 +3626,12 @@ void wlserver_touchup( int touch_id, uint32_t time ) + assert( wlserver_is_lock_held() ); + + // Release whatever the touch pressed even if its surface is gone, or the seat keeps the button down. +- bool bReleasedAny = false; + const bool bClickTouch = touch_click_lift( s_TouchClick, touch_id ); +- for ( int i = 0; bClickTouch && i < WLSERVER_BUTTON_COUNT; i++ ) +- { +- if ( wlserver.button_held[ i ] == true ) +- { +- uint32_t button = TouchClickModeToLinuxButton( (gamescope::TouchClickMode) i ); +- +- if ( button != 0 ) +- { +- wlr_seat_pointer_notify_button( wlserver.wlr.seat, time, button, WL_POINTER_BUTTON_STATE_RELEASED ); +- bReleasedAny = true; +- } +- +- wlserver.button_held[ i ] = false; +- } +- } +- +- if ( bReleasedAny == true ) +- { +- wlr_seat_pointer_notify_frame( wlserver.wlr.seat ); +- } ++ uint64_t ulDelay = 0; ++ if ( bClickTouch && !touch_click_tap_up( s_TouchTap, time, get_time_in_nanos(), &ulDelay ) ) ++ touch_click_release_held( time ); ++ else if ( bClickTouch && ulDelay && !touch_click_tap_arm( ulDelay ) ) ++ touch_click_tap_settle( get_time_in_nanos() ); + + if ( wlserver.touch_down_ids.count( touch_id ) > 0 ) + { diff --git a/tools/tests/test_gamescope_touch_click_hover.py b/tools/tests/test_gamescope_touch_click_hover.py new file mode 100644 index 0000000000..f5d4542695 --- /dev/null +++ b/tools/tests/test_gamescope_touch_click_hover.py @@ -0,0 +1,91 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PATCH = ROOT / "tools/gamescope/patches/0147-touch-click-press-after-hover.patch" + + +def added_source(patch): + return "\n".join(line[1:] for line in patch.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")) + + +def rules(text): + begin = text.index("static constexpr uint64_t k_ulTouchClickHoverNs") + end = text.index("static TouchClickTap s_TouchTap;") + return text[begin:end] + + +PREAMBLE = r''' +#include +#include +#include +#include +''' + +MAIN = r''' +static const uint64_t MS = 1'000'000ul; + +int main() { + TouchClickTap tap; + uint64_t delay = 0; + + assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, true) == k_ulTouchClickHoverNs); + assert(tap.bPressPending); + assert(!touch_click_tap_motion(tap, 55.0, 64.0)); + assert(touch_click_tap_motion(tap, 80.0, 60.0)); + assert(touch_click_tap_fire(tap, 134 * MS, &delay) == TouchClickStep::Press); + assert(delay == 0); + assert(!touch_click_tap_motion(tap, 300.0, 300.0)); + assert(!touch_click_tap_up(tap, 400, 500 * MS, &delay)); + + assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, true) > 0); + assert(touch_click_tap_up(tap, 15, 105 * MS, &delay)); + assert(delay == 0); + assert(touch_click_tap_fire(tap, 134 * MS, &delay) == TouchClickStep::Press); + assert(delay == k_ulTouchClickHoldNs); + assert(touch_click_tap_time(tap, 134 * MS) == 44); + assert(touch_click_tap_fire(tap, 184 * MS, &delay) == TouchClickStep::Release); + assert(touch_click_tap_fire(tap, 200 * MS, &delay) == TouchClickStep::None); + + assert(touch_click_tap_down(tap, 272, 10, 100 * MS, 50.0, 60.0, false) == 0); + assert(!tap.bPressPending); + assert(touch_click_tap_up(tap, 30, 120 * MS, &delay)); + assert(delay == 30 * MS); + assert(touch_click_tap_fire(tap, 150 * MS, &delay) == TouchClickStep::Release); + + assert(touch_click_tap_down(tap, 273, 10, 100 * MS, 0.0, 0.0, false) == 0); + assert(!touch_click_tap_up(tap, 90, 180 * MS, &delay)); + assert(touch_click_tap_fire(tap, 190 * MS, &delay) == TouchClickStep::None); + return 0; +} +''' + + +class GamescopeTouchClickHoverTest(unittest.TestCase): + def test_press_waits_for_the_hover_and_a_tap_is_held(self): + compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++") + if not compiler: + self.skipTest("no C++ compiler") + source = PREAMBLE + rules(added_source(PATCH)) + MAIN + with tempfile.TemporaryDirectory() as tmp: + src = Path(tmp) / "touch_click_hover.cpp" + exe = Path(tmp) / "touch_click_hover" + src.write_text(source) + subprocess.run([compiler, "-std=c++17", "-Wall", "-Wextra", "-Werror", "-UNDEBUG", str(src), "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) + + def test_touch_down_defers_only_a_moved_press(self): + added = added_source(PATCH) + self.assertIn("bMoved = flFromX != wlserver.mouse_surface_cursorx || flFromY != wlserver.mouse_surface_cursory;", added) + self.assertIn("if ( !ulDelay || !touch_click_tap_arm( ulDelay ) )", added) + self.assertIn("touch_click_tap_settle( ulNow );", added) + self.assertEqual(1, added.count("touch_click_tap_motion( s_TouchTap, tx, ty )")) + self.assertIn("if ( bClickTouch && !touch_click_tap_up( s_TouchTap, time, get_time_in_nanos(), &ulDelay ) )", added) + + +if __name__ == "__main__": + unittest.main()