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()