diff --git a/tools/gamescope/PATCHES.md b/tools/gamescope/PATCHES.md index 8ddddcca22..c416b6aea7 100644 --- a/tools/gamescope/PATCHES.md +++ b/tools/gamescope/PATCHES.md @@ -444,3 +444,33 @@ 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. + +## 0144 steamcompmgr: motion into the screen edge does not decide the pointer hold + +0133 compares the motion gamescope sends with how the X pointer moves. At the edge of the X screen the pointer cannot move +further, so a drawn-cursor game whose pointer sat at an edge while touch or mouse motion pushed outward counted as +holding the pointer: the hold stayed on, every such frame restarted the 30 frames needed to release it, and touch drags +kept turning into relative motion with no button held. The sent motion is now clamped to what the pointer can reach +inside the X screen before it is compared, so motion into an edge counts as no motion. A recentring game is detected as +before, since its pointer stays away from the edges. `tools/tests/test_gamescope_pointer_hold_edge.py` compiles the rule +from this patch with the 0133 rule. + +## 0145 steamcompmgr: touch-down warps do not decide the pointer hold + +In click mode every touch-down warps the X pointer to the finger. 0133 read that jump as the pointer not following the +sent motion, so each new stroke on a drawn-cursor game restarted the 30 following frames needed to release the hold. +Short strokes never released it, and taps kept landing at the relative pointer instead of under the finger. wlserver now +counts its touch-down warps, and a frame that contains one is left out of the hold decision. Touch drags, mouse motion +and mouse warps still count, so a drag on a game that follows the pointer releases the hold and a recentring game is +still held. `tools/tests/test_gamescope_pointer_hold_touch_down.py` compiles the rule with the 0133 rule. 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/gamescope/patches/0144-pointer-hold-ignores-motion-into-the-edge.patch b/tools/gamescope/patches/0144-pointer-hold-ignores-motion-into-the-edge.patch new file mode 100644 index 0000000000..a7db8bacd0 --- /dev/null +++ b/tools/gamescope/patches/0144-pointer-hold-ignores-motion-into-the-edge.patch @@ -0,0 +1,28 @@ +diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp +index e98d320..85af598 100644 +--- a/src/steamcompmgr.cpp ++++ b/src/steamcompmgr.cpp +@@ -3245,6 +3245,11 @@ static bool pointer_hold_step( PointerHold &state, bool bConstrained, double flS + return state.bHeld; + } + ++static double pointer_reach( int nFrom, double flDelta, int nSize ) ++{ ++ return std::clamp( nFrom + flDelta, 0.0, double( std::max( nSize - 1, 0 ) ) ) - nFrom; ++} ++ + static bool s_bPointerHeld = false; + + static void update_pointer_hold( global_focus_t *pFocus, bool bConstrained, bool bHinted ) +@@ -3311,7 +3316,10 @@ static void update_pointer_hold( global_focus_t *pFocus, bool bConstrained, bool + + if ( s_bTracking ) + { +- s_bPointerHeld = pointer_hold_step( s_Hold, true, flDX, flDY, nRootX - s_nRootX, nRootY - s_nRootY, get_time_in_nanos() ); ++ const xwayland_ctx_t *pCtx = pFocus->cursor->getCtx(); ++ const double flReachX = pointer_reach( s_nRootX, flDX, pCtx->root_width ); ++ const double flReachY = pointer_reach( s_nRootY, flDY, pCtx->root_height ); ++ s_bPointerHeld = pointer_hold_step( s_Hold, true, flReachX, flReachY, nRootX - s_nRootX, nRootY - s_nRootY, get_time_in_nanos() ); + } + else + { diff --git a/tools/gamescope/patches/0145-pointer-hold-skips-touch-down-warps.patch b/tools/gamescope/patches/0145-pointer-hold-skips-touch-down-warps.patch new file mode 100644 index 0000000000..3a71f219dd --- /dev/null +++ b/tools/gamescope/patches/0145-pointer-hold-skips-touch-down-warps.patch @@ -0,0 +1,73 @@ +diff --git a/src/wlserver.hpp b/src/wlserver.hpp +--- a/src/wlserver.hpp ++++ b/src/wlserver.hpp +@@ -173,6 +173,7 @@ + double flFollowDY = 0.0; + double flSentDX = 0.0; + double flSentDY = 0.0; ++ uint64_t ulTouchDownWarps = 0; + + bool button_held[ WLSERVER_BUTTON_COUNT ]; + std::set touch_down_ids; +diff --git a/src/wlserver.cpp b/src/wlserver.cpp +--- a/src/wlserver.cpp ++++ b/src/wlserver.cpp +@@ -3429,6 +3429,7 @@ + if ( eMode != gamescope::TouchClickModes::Trackpad ) + { + wlserver_mousewarp( tx, ty, time, false, true ); ++ wlserver.ulTouchDownWarps++; + } + + uint32_t button = TouchClickModeToLinuxButton( eMode ); +diff --git a/src/steamcompmgr.cpp b/src/steamcompmgr.cpp +--- a/src/steamcompmgr.cpp ++++ b/src/steamcompmgr.cpp +@@ -3263,6 +3263,7 @@ + static bool s_bHinted = false; + static const steamcompmgr_win_t *s_pWindow = nullptr; + static const struct wlr_pointer_constraint_v1 *s_pConstraint = nullptr; ++ static uint64_t s_ulTouchDownWarps = 0; + + wlserver_lock(); + const double flX = wlserver.mouse_surface_cursorx; +@@ -3272,8 +3273,12 @@ + const struct wlr_pointer_constraint_v1 *pConstraint = wlserver.GetCursorConstraint(); + wlserver.flSentDX = 0.0; + wlserver.flSentDY = 0.0; ++ const uint64_t ulTouchDownWarps = wlserver.ulTouchDownWarps; + wlserver_unlock(); + ++ const bool bTouchDown = ulTouchDownWarps != s_ulTouchDownWarps; ++ s_ulTouchDownWarps = ulTouchDownWarps; ++ + if ( !bConstrained || !pFocus->cursor ) + { + s_bTracking = false; +@@ -3314,19 +3319,19 @@ + int nRootY = 0; + pFocus->cursor->QueryRootPosition( &nRootX, &nRootY ); + +- if ( s_bTracking ) ++ if ( !s_bTracking ) ++ { ++ s_Hold = PointerHold{}; ++ s_Hold.bHeld = bHinted; ++ s_bPointerHeld = bHinted; ++ } ++ else if ( !bTouchDown ) + { + const xwayland_ctx_t *pCtx = pFocus->cursor->getCtx(); + const double flReachX = pointer_reach( s_nRootX, flDX, pCtx->root_width ); + const double flReachY = pointer_reach( s_nRootY, flDY, pCtx->root_height ); + s_bPointerHeld = pointer_hold_step( s_Hold, true, flReachX, flReachY, nRootX - s_nRootX, nRootY - s_nRootY, get_time_in_nanos() ); + } +- else +- { +- s_Hold = PointerHold{}; +- s_Hold.bHeld = bHinted; +- s_bPointerHeld = bHinted; +- } + + s_bTracking = true; + s_flSeatX = flX; diff --git a/tools/tests/test_gamescope_pointer_hold_edge.py b/tools/tests/test_gamescope_pointer_hold_edge.py new file mode 100644 index 0000000000..e405474e88 --- /dev/null +++ b/tools/tests/test_gamescope_pointer_hold_edge.py @@ -0,0 +1,102 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +HOLD = ROOT / "tools/gamescope/patches/0133-constraint-relative-when-held.patch" +EDGE = ROOT / "tools/gamescope/patches/0144-pointer-hold-ignores-motion-into-the-edge.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 block(text, start): + begin = text.index(start) + return text[begin:text.index("\n}", begin) + 3] + + +PREAMBLE = r''' +#include +#include +#include +#include +''' + +MAIN = r''' +static const uint64_t FRAME = 16'666'667ul; + +struct Pointer { + PointerHold hold; + int x; + int width; + uint64_t now = 1'000'000'000ul; + bool step(double seatDX, int pointerDX) { + now += FRAME; + const double reach = pointer_reach(x, seatDX, width); + x += pointerDX; + return pointer_hold_step(hold, true, reach, 0.0, pointerDX, 0.0, now); + } +}; + +int main() { + assert(pointer_reach(1279, 26.0, 1280) == 0.0); + assert(pointer_reach(1270, 26.0, 1280) == 9.0); + assert(pointer_reach(0, -13.0, 1280) == 0.0); + assert(pointer_reach(640, -13.0, 1280) == -13.0); + assert(pointer_reach(5, 3.0, 0) == -5.0); + + Pointer pinned{{}, 1279, 1280}; + for (int i = 0; i < 20; i++) assert(!pinned.step(26.0, 0)); + + Pointer release{{}, 1279, 1280}; + release.hold.bHeld = true; + for (int i = 0; i < 20; i++) assert(release.step(26.0, 0)); + for (int i = 0; i < 10; i++) assert(release.step(-13.0, -13)); + for (int i = 0; i < 10; i++) assert(release.step(13.0, 13)); + assert(release.x == 1279); + for (int i = 0; i < 20; i++) assert(release.step(26.0, 0)); + for (int i = 0; i < 9; i++) assert(release.step(-13.0, -13)); + assert(!release.step(-13.0, -13)); + bool held = false; + + Pointer recentre{{}, 640, 1280}; + held = false; + for (int i = 0; i < 3; i++) held = recentre.step(26.0, 0); + assert(held); + return 0; +} +''' + + +class GamescopePointerHoldEdgeTest(unittest.TestCase): + def test_motion_into_the_screen_edge_neither_holds_nor_resets_the_release(self): + compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++") + if not compiler: + self.skipTest("no C++ compiler") + hold = added_source(HOLD) + edge = added_source(EDGE) + source = PREAMBLE + "\n".join([ + block(hold, "struct PointerHold"), + block(hold, "static bool pointer_hold_step"), + block(edge, "static double pointer_reach"), + ]) + MAIN + with tempfile.TemporaryDirectory() as tmp: + src = Path(tmp) / "pointer_hold_edge.cpp" + exe = Path(tmp) / "pointer_hold_edge" + 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_hold_rule_compares_against_the_reachable_motion(self): + edge = added_source(EDGE) + self.assertIn("pointer_reach( s_nRootX, flDX, pCtx->root_width )", edge) + self.assertIn("pointer_reach( s_nRootY, flDY, pCtx->root_height )", edge) + self.assertIn("pointer_hold_step( s_Hold, true, flReachX, flReachY,", edge) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_gamescope_pointer_hold_touch_down.py b/tools/tests/test_gamescope_pointer_hold_touch_down.py new file mode 100644 index 0000000000..7f8f2b01d7 --- /dev/null +++ b/tools/tests/test_gamescope_pointer_hold_touch_down.py @@ -0,0 +1,109 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[2] +HOLD = ROOT / "tools/gamescope/patches/0133-constraint-relative-when-held.patch" +DOWN = ROOT / "tools/gamescope/patches/0145-pointer-hold-skips-touch-down-warps.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 block(text, start): + begin = text.index(start) + return text[begin:text.index("\n}", begin) + 3] + + +PREAMBLE = r''' +#include +#include +#include +#include +''' + +MAIN = r''' +static const uint64_t FRAME = 16'666'667ul; + +struct Session { + PointerHold hold; + uint64_t now = 1'000'000'000ul; + bool skipTouchDown; + bool frame(double seatDX, double pointerDX, bool touchDown) { + now += FRAME; + if (touchDown && skipTouchDown) return hold.bHeld; + return pointer_hold_step(hold, true, seatDX, 0.0, pointerDX, 0.0, now); + } + void held() { + hold = PointerHold{}; + hold.bHeld = true; + } + bool strokes() { + held(); + bool result = true; + for (int i = 0; i < 17; i++) result = frame(80.0, 80.0, false); + result = frame(41.0, -1106.0, true); + for (int i = 0; i < 15; i++) result = frame(73.3, 73.0, false); + return result; + } +}; + +int main() { + Session counted{{}, 1'000'000'000ul, false}; + assert(counted.strokes()); + + Session skipped{{}, 1'000'000'000ul, true}; + assert(!skipped.strokes()); + + Session drag{{}, 1'000'000'000ul, true}; + drag.held(); + bool result = drag.frame(41.0, -1106.0, true); + assert(result); + for (int i = 0; i < 29; i++) result = drag.frame(12.0, 12.0, false); + assert(result); + result = drag.frame(12.0, 12.0, false); + assert(!result); + + Session recentre{{}, 1'000'000'000ul, true}; + for (int i = 0; i < 3; i++) result = recentre.frame(26.0, 0.0, false); + assert(result); + result = recentre.frame(-400.0, 300.0, true); + assert(result); + for (int i = 0; i < 40; i++) result = recentre.frame(12.0, 0.0, false); + assert(result); + return 0; +} +''' + + +class GamescopePointerHoldTouchDownTest(unittest.TestCase): + def test_touch_down_is_skipped_and_touch_drags_release_the_hold(self): + compiler = shutil.which("c++") or shutil.which("g++") or shutil.which("clang++") + if not compiler: + self.skipTest("no C++ compiler") + hold = added_source(HOLD) + source = PREAMBLE + "\n".join([ + block(hold, "struct PointerHold"), + block(hold, "static bool pointer_hold_step"), + ]) + MAIN + with tempfile.TemporaryDirectory() as tmp: + src = Path(tmp) / "pointer_hold_touch_down.cpp" + exe = Path(tmp) / "pointer_hold_touch_down" + 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_only_touch_down_warps_are_skipped(self): + added = added_source(DOWN) + self.assertIn(" \t\t\t\twlserver_mousewarp( tx, ty, time, false, true );\n+\t\t\t\twlserver.ulTouchDownWarps++;", DOWN.read_text()) + self.assertEqual(1, added.count("wlserver.ulTouchDownWarps++")) + self.assertIn("const bool bTouchDown = ulTouchDownWarps != s_ulTouchDownWarps;", added) + self.assertIn("else if ( !bTouchDown )", added) + self.assertNotIn("bFromTouch )\n\t\twlserver.ulTouchDownWarps++", added) + +if __name__ == "__main__": + unittest.main() 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()