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