diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 8ddddcca22..4170ce5c24 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -444,3 +444,14 @@ starts there as before, but no motion is sent for it. The fake pointer position With the patch, the Duo starts with only the bottom bar under the parked pointer hovered and controller focus on the first tile (4 of 4 starts), and returns from a game the same way (3 of 3). Making 0100 ignore the X server's initial pointer instead was tried and rejected: the centre motion still arrived, and the header or search bar ended up hovered. + +## 0143 wlserver: only the first finger drives the emulated mouse + +In the Hover, Left, Right, Middle and Trackpad touch click modes every finger warped the X pointer and any finger lifting +released the click. With a second finger down the pointer jumped between both fingers on every frame while the button +stayed held, so a dragged item followed whichever finger moved last, and lifting the second finger dropped it under the +first. The first finger of a gesture now owns the emulated mouse: only it moves the pointer and only its lift releases the +click. Fingers that land while it is down are ignored, and so are the others once it lifts, until every finger is up. +Touches the click modes never claimed (Passthrough, Disabled, no focused surface) still release a held click when they +lift, and the nested pointer (`bAlwaysWarpCursor`) moves the pointer as before. `tools/tests/test_gamescope_touch_click_owner.py` +compiles the rule from this patch. diff --git a/tools/gamescope/patches/0143-touch-click-first-finger-drives.patch b/tools/gamescope/patches/0143-touch-click-first-finger-drives.patch new file mode 100644 index 0000000000..2eb9e66487 --- /dev/null +++ b/tools/gamescope/patches/0143-touch-click-first-finger-drives.patch @@ -0,0 +1,75 @@ +diff --git a/src/wlserver.cpp b/src/wlserver.cpp +index 9740875..cb72051 100644 +--- a/src/wlserver.cpp ++++ b/src/wlserver.cpp +@@ -3283,6 +3283,38 @@ static void apply_touchscreen_orientation(GamescopePanelOrientation orientation, + *y = ty; + } + ++struct TouchClickState ++{ ++ std::set ids; ++ int nOwner = -1; ++}; ++ ++static TouchClickState s_TouchClick; ++ ++static bool touch_click_claim( TouchClickState &state, int nTouchId ) ++{ ++ if ( state.ids.empty() ) ++ state.nOwner = nTouchId; ++ state.ids.insert( nTouchId ); ++ return state.nOwner == nTouchId; ++} ++ ++static bool touch_click_drives( const TouchClickState &state, int nTouchId ) ++{ ++ return state.nOwner == nTouchId; ++} ++ ++static bool touch_click_lift( TouchClickState &state, int nTouchId ) ++{ ++ const bool bTracked = state.ids.erase( nTouchId ) > 0; ++ if ( state.nOwner == nTouchId ) ++ { ++ state.nOwner = -1; ++ return true; ++ } ++ return !bTracked && state.nOwner < 0; ++} ++ + void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool bAlwaysWarpCursor, gamescope::IBackendConnector* connector ) + { + assert( wlserver_is_lock_held() ); +@@ -3333,9 +3365,10 @@ void wlserver_touchmotion( double x, double y, int touch_id, uint32_t time, bool + } + else if ( eMode == gamescope::TouchClickModes::Trackpad ) + { +- wlserver_mousemotion( trackpad_dx, trackpad_dy, time ); ++ if ( touch_click_drives( s_TouchClick, touch_id ) ) ++ wlserver_mousemotion( trackpad_dx, trackpad_dy, time ); + } +- else ++ else if ( bAlwaysWarpCursor || touch_click_drives( s_TouchClick, touch_id ) ) + { + g_bPendingTouchMovement = true; + +@@ -3389,7 +3422,7 @@ void wlserver_touchdown( double x, double y, int touch_id, uint32_t time, gamesc + { + return; + } +- else ++ else if ( touch_click_claim( s_TouchClick, touch_id ) ) + { + g_bPendingTouchMovement = true; + +@@ -3417,7 +3450,8 @@ void wlserver_touchup( int touch_id, uint32_t time ) + + // Release whatever the touch pressed even if its surface is gone, or the seat keeps the button down. + bool bReleasedAny = false; +- for ( int i = 0; i < WLSERVER_BUTTON_COUNT; i++ ) ++ 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 ) + { diff --git a/tools/tests/test_gamescope_touch_click_owner.py b/tools/tests/test_gamescope_touch_click_owner.py new file mode 100644 index 0000000000..1063838a35 --- /dev/null +++ b/tools/tests/test_gamescope_touch_click_owner.py @@ -0,0 +1,121 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +PATCH = ROOT / "tools/gamescope/patches/0143-touch-click-first-finger-drives.patch" + + +def added_source(): + return "\n".join(line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")) + + +def block(text, start): + begin = text.index(start) + return text[begin:text.index("\n}", begin) + 3] + + +PREAMBLE = r''' +#include +#include +''' + +MAIN = r''' +struct Pointer { + TouchClickState state; + int warps = 0; + int presses = 0; + int releases = 0; + void down(int id) { if (touch_click_claim(state, id)) { warps++; presses++; } } + void move(int id, bool nested = false) { if (nested || touch_click_drives(state, id)) warps++; } + void up(int id) { if (touch_click_lift(state, id)) releases++; } +}; + +int main() { + Pointer tap; + tap.down(1); + tap.move(1); + tap.up(1); + assert(tap.presses == 1 && tap.releases == 1 && tap.warps == 2); + + Pointer drag; + drag.down(1); + drag.move(1); + drag.down(2); + drag.move(2); + drag.move(1); + drag.up(2); + assert(drag.presses == 1 && drag.releases == 0 && drag.warps == 3); + drag.move(1); + drag.up(1); + assert(drag.releases == 1 && drag.warps == 4); + + Pointer handover; + handover.down(1); + handover.down(2); + handover.up(1); + assert(handover.releases == 1); + handover.move(2); + handover.down(3); + handover.move(3); + assert(handover.warps == 1 && handover.presses == 1); + handover.up(3); + handover.up(2); + assert(handover.releases == 1); + handover.down(4); + handover.move(4); + handover.up(4); + assert(handover.presses == 2 && handover.releases == 2 && handover.warps == 3); + + Pointer nested; + nested.move(0, true); + nested.down(1); + nested.move(0, true); + assert(nested.warps == 3); + + Pointer stray; + stray.up(7); + assert(stray.releases == 1); + stray.down(1); + stray.up(7); + assert(stray.releases == 1); + stray.up(1); + assert(stray.releases == 2); + return 0; +} +''' + + +class GamescopeTouchClickOwnerTest(unittest.TestCase): + def test_only_the_first_finger_drives_the_emulated_mouse(self): + compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++") + if not compiler: + self.skipTest("no C++ compiler") + added = added_source() + helpers = "\n".join(block(added, start) for start in ( + "struct TouchClickState", + "static bool touch_click_claim", + "static bool touch_click_drives", + "static bool touch_click_lift", + )) + source = PREAMBLE + helpers + MAIN + with tempfile.TemporaryDirectory() as tmp: + src = Path(tmp) / "touch_click_owner.cpp" + exe = Path(tmp) / "touch_click_owner" + src.write_text(source) + subprocess.run([compiler, "-std=c++17", "-Wall", "-Werror", "-UNDEBUG", str(src), "-o", str(exe)], check=True) + subprocess.run([str(exe)], check=True) + + def test_the_patch_gates_every_finger_path(self): + added = added_source() + self.assertIn("else if ( bAlwaysWarpCursor || touch_click_drives( s_TouchClick, touch_id ) )", added) + self.assertIn("if ( touch_click_drives( s_TouchClick, touch_id ) )", added) + self.assertIn("else if ( touch_click_claim( s_TouchClick, touch_id ) )", added) + self.assertIn("bClickTouch && i < WLSERVER_BUTTON_COUNT", added) + + +if __name__ == "__main__": + unittest.main()