Files
DroidDeck/tools/tests/test_gamescope_pointer_hold_edge.py
MaxsTechReview 5d276ab461 fix(gamescope): direct touch keeps the pointer under the first finger (#3382)
* fix(gamescope): only the first finger drives the emulated mouse

In the click touch modes every finger warped the X pointer and any
finger lifting released the click, so a second finger made the pointer
jump between both contacts and dropped whatever the first was dragging.
The first finger of a gesture now owns the emulated mouse until it lifts,
and later fingers are ignored until every finger is up.

* fix(gamescope): motion into the screen edge does not decide the pointer hold

A drawn-cursor game whose X pointer sat at the screen edge looked like it
held the pointer whenever motion pushed outward, which kept the hold on
and turned touch drags into relative motion. The sent motion is clamped
to what the pointer can reach before the comparison.

* fix(gamescope): touch warps do not decide the pointer hold

* fix(gamescope): only touch-down warps are left out of the pointer hold

Touch drags are real pointer motion, so they count toward releasing the
hold again. Only the jump a touch-down makes is skipped.
2026-11-28 16:09:17 -05:00

103 lines
3.4 KiB
Python

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 <algorithm>
#include <cassert>
#include <cmath>
#include <cstdint>
'''
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()