From 21e5fdfc63f0a62b8fbd056a5ee8a6e815c314b0 Mon Sep 17 00:00:00 2001 From: asdf17128 Date: Thu, 15 Oct 2026 11:37:35 +0800 Subject: [PATCH] Release pointer buttons without replaying stale motion (#803) * Fix cursor warping on native touch down * Preserve input cleanup without replaying stale pointer motion --------- Co-authored-by: siyuan.qsy --- app/src/main/cpp/waylandcomp/src/compositor.c | 19 +++- tools/tests/pointer_release_harness.c | 88 +++++++++++++++++++ tools/tests/test_pointer_release.py | 57 ++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 tools/tests/pointer_release_harness.c create mode 100644 tools/tests/test_pointer_release.py diff --git a/app/src/main/cpp/waylandcomp/src/compositor.c b/app/src/main/cpp/waylandcomp/src/compositor.c index 7d74dce39f..3b218ee86f 100644 --- a/app/src/main/cpp/waylandcomp/src/compositor.c +++ b/app/src/main/cpp/waylandcomp/src/compositor.c @@ -2609,7 +2609,24 @@ static void pointer_delta(double dx, double dy) { } static void pointer_button(uint32_t button, int pressed) { - pointer_input(0, 0, 1, button, pressed); + if (pressed) { + pointer_input(0, 0, 1, button, pressed); + return; + } + // Releases also clean up interrupted gestures. Deliver them to the existing + // pointer focus without motion or hit-testing: native touch has independent + // coordinates, so replaying g_ptr_x/y here can warp the game cursor. + uint32_t t = now_ms(); + for (int i = 0; i < g_nptrs; i++) { + struct seat_pointer *sp = &g_ptrs[i]; + if (!sp->focus) continue; + wl_pointer_send_button(sp->ptr, wl_display_next_serial(g_display), t, button, + WL_POINTER_BUTTON_STATE_RELEASED); + if (wl_resource_get_version(sp->ptr) >= WL_POINTER_FRAME_SINCE_VERSION) + wl_pointer_send_frame(sp->ptr); + } + g_grab = NULL; + wl_display_flush_clients(g_display); } static void deliver_pointer(const struct input_msg *m) { diff --git a/tools/tests/pointer_release_harness.c b/tools/tests/pointer_release_harness.c new file mode 100644 index 0000000000..8369eb3771 --- /dev/null +++ b/tools/tests/pointer_release_harness.c @@ -0,0 +1,88 @@ +#include +#include +#include +#include + +#define WL_POINTER_BUTTON_STATE_RELEASED 0 +#define WL_POINTER_FRAME_SINCE_VERSION 5 +struct wl_resource { int version, releases, frames; uint32_t held; }; +struct seat_pointer { struct wl_resource *ptr, *focus; }; +static struct seat_pointer g_ptrs[3]; +static int g_nptrs = 3; +static void *g_grab, *g_display; +static unsigned motion_calls, flushes; +static uint32_t now_ms(void) { return 42; } +static uint32_t wl_display_next_serial(void *d) { (void)d; return 1; } +static int wl_resource_get_version(struct wl_resource *r) { return r->version; } +static void wl_pointer_send_frame(struct wl_resource *r) { r->frames++; } +static void wl_display_flush_clients(void *d) { (void)d; flushes++; } +static void wl_pointer_send_button(struct wl_resource *r, uint32_t serial, + uint32_t t, uint32_t button, uint32_t state) { + (void)serial; (void)t; + assert(state == WL_POINTER_BUTTON_STATE_RELEASED); + r->held &= ~(1u << (button - 0x110)); + r->releases++; +} +/* The old dispatcher calls this even for releases, emitting stale coordinates. + * Recording this boundary detects that unwanted motion/focus routing path. */ +static void pointer_input(double x, double y, int relative, uint32_t button, int pressed) { + assert(x == 0 && y == 0 && relative == 1 && button >= 0x110 && button <= 0x112); + assert(pressed == 1); + motion_calls++; +} +#include "pointer_button.inc" +#define BTN_LEFT 0x110 +#define MAX_TOUCH_POINTS 2 +struct touch_point { int pointer_fallback; }; +static struct touch_point g_touch_points[MAX_TOUCH_POINTS]; +struct seat_touch { struct wl_resource *touch, *focus; }; +static struct seat_touch g_touches[1]; +static int g_ntouches = 1, cancels; +static void wl_touch_send_cancel(struct wl_resource *r) { (void)r; cancels++; } +#include "touch_cancel_all.inc" + +int main(int argc, char **argv) { + assert(argc == 2); + struct wl_resource surface = {0}; + struct wl_resource a = {.version = 5, .held = 7}; + struct wl_resource b = {.version = 4, .held = 7}; + struct wl_resource other = {.version = 5, .held = 7}; + g_ptrs[0] = (struct seat_pointer){&a, &surface}; + g_ptrs[1] = (struct seat_pointer){&b, &surface}; + g_ptrs[2] = (struct seat_pointer){&other, NULL}; + g_grab = &surface; + if (!strcmp(argv[1], "fallback") || !strcmp(argv[1], "native")) { + int fallback = !strcmp(argv[1], "fallback"); + g_touches[0] = (struct seat_touch){&a, &surface}; + g_touch_points[0].pointer_fallback = fallback; + touch_cancel_all(); + assert(!motion_calls && cancels == 1 && !g_touches[0].focus); + assert(!g_touch_points[0].pointer_fallback); + assert(a.releases == fallback && a.held == (fallback ? 6u : 7u)); + for (uint32_t button = 0x110; button <= 0x112; button++) pointer_button(button, 0); + assert(!motion_calls && !a.held && !b.held && !g_grab); + } else if (!strcmp(argv[1], "press")) { + pointer_button(0x110, 1); + assert(motion_calls == 1 && !a.releases && g_grab == &surface); + } else if (!strcmp(argv[1], "unfocused")) { + g_ptrs[0].focus = g_ptrs[1].focus = NULL; + pointer_button(0x110, 0); + assert(!motion_calls && !a.releases && !b.releases && !other.releases); + assert(!g_grab); + } else if (!strcmp(argv[1], "cleanup")) { + for (uint32_t button = 0x110; button <= 0x112; button++) pointer_button(button, 0); + assert(!motion_calls && !a.held && !b.held && other.held == 7); + assert(a.releases == 3 && b.releases == 3 && !other.releases); + assert(!g_grab && flushes == 3); + } else { + assert(!strcmp(argv[1], "focused")); + a.held = b.held = 1; + pointer_button(0x110, 0); + assert(!motion_calls && !a.held && !b.held); + assert(a.releases == 1 && b.releases == 1 && !other.releases); + assert(a.frames == 1 && !b.frames && !other.frames); + assert(g_ptrs[0].focus == &surface && g_ptrs[1].focus == &surface); + assert(!g_grab && flushes == 1); + } + puts("PASS"); +} diff --git a/tools/tests/test_pointer_release.py b/tools/tests/test_pointer_release.py new file mode 100644 index 0000000000..9da5672e3f --- /dev/null +++ b/tools/tests/test_pointer_release.py @@ -0,0 +1,57 @@ +"""Exercise the compositor's real button dispatcher with a recording Wayland sink.""" +import os +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + +REPO = Path(__file__).resolve().parents[2] + + +class PointerReleaseTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + compiler = shutil.which(os.environ.get('CC', 'cc')) + if compiler is None: + raise unittest.SkipTest('A host C compiler is required') + source = (REPO / 'app/src/main/cpp/waylandcomp/src/compositor.c').read_text() + start = source.index('static void pointer_button(uint32_t button, int pressed) {') + end = source.index('\n}\n', start) + 3 + cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-pointer-release-') + cls.addClassCleanup(cls.temp.cleanup) + folder = Path(cls.temp.name) + (folder / 'pointer_button.inc').write_text(source[start:end]) + start = source.index('static void touch_cancel_all(void) {') + end = source.index('\n}\n', start) + 3 + (folder / 'touch_cancel_all.inc').write_text(source[start:end]) + cls.binary = folder / 'pointer-test' + subprocess.run([compiler, '-std=c11', '-Wall', '-I', str(folder), + str(REPO / 'tools/tests/pointer_release_harness.c'), + '-o', str(cls.binary)], check=True, capture_output=True, text=True) + + def check_case(self, case): + result = subprocess.run([str(self.binary), case], capture_output=True, text=True) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + + def test_cleanup_releases_all_buttons_without_motion(self): + self.check_case('cleanup') + + def test_release_without_focus_does_not_select_a_window(self): + self.check_case('unfocused') + + def test_release_reaches_each_focused_pointer_and_clears_grab(self): + self.check_case('focused') + + def test_touch_fallback_cancel_releases_without_motion(self): + self.check_case('fallback') + + def test_native_touch_cancel_then_cleanup_releases_stale_buttons(self): + self.check_case('native') + + def test_press_keeps_existing_routing(self): + self.check_case('press') + + +if __name__ == '__main__': + unittest.main()