Prevent clicks after pointer drags and X11 foreign grabs

This commit is contained in:
waozi committed 2026-11-21 16:20:26 -03:00
1 parent 609be37059
commit 361fb0197f
8 files changed
+237 -12

No files matched your search

+3
View File
@@ -1,6 +1,9 @@
# Changelog
## Unreleased
- Pointer drags no longer activate controls on release, including after moving
away and back. X11 window-manager grabs no longer become application clicks.
- Portable ZX players forward each frame-resolved cursor to the host so
bundled apps get pointer feedback without app-specific host code.
+5
View File
@@ -850,6 +850,11 @@ window-focus-test:
env -u DISPLAY -u WAYLAND_DISPLAY -u XAUTHORITY -u DBUS_SESSION_BUS_ADDRESS -u SESSION_MANAGER YUE_DESKTOP_RECOVERY=0 python3 tests/window_focus_test.py
test: window-focus-test
.PHONY: desktop-pointer-test
desktop-pointer-test:
sh tests/ziran_desktop_pointer_test.sh
test: desktop-pointer-test
# Asset audio uses actual decoders on a private null device and a muted private
# browser. It never connects to the owner's audio session or desktop.
.PHONY: asset-audio-test
+2
View File
@@ -1668,6 +1668,8 @@ uppercase ASCII, named keys from 256 (Escape), as `desktop_keys` maps SDL's.
`DesktopWheelMoveX()` gives the poll's sideways wheel steps (positive to
the right) beside `DesktopWheelMove()`. Over another window of the program
the main window's pointer is at -1, -1 and presses nothing.
On X11 a button held by another client's grab, such as a window-manager
move, produces no application press or release.
`OpenDesktopWindow(width, height, title, options)` opens the window with
any of `DesktopWindowResizable`, `DesktopWindowUndecorated`,
`DesktopWindowTopmost` and `DesktopWindowAlwaysRun` (keep the frame cadence
+70 -11
View File
@@ -169,6 +169,8 @@ texture: *void;
pointer: PointerFrame;
desktop_physical_buttons: u32;
desktop_physical_sampled: bool;
// The physical buttons held by a press this window never received.
desktop_foreign_buttons: u32;
secondary: SecondaryPointerFrame;
// The middle button, sampled like the right one.
middle: SecondaryPointerFrame;
@@ -498,7 +500,7 @@ OpenDesktopWindow :: (width: s32, height: s32, title_text: string, options: u32)
if Init(cast(u32)0x2020) != 0 { return DesktopOpenFailed("start SDL video", true) }
RasterUseWake(DesktopLanczosWake)
FrameInputProviderReset(*desktop_frame_input)
desktop_physical_buttons=0; desktop_physical_sampled=false
desktop_physical_buttons=0; desktop_physical_sampled=false; desktop_foreign_buttons=0
desktop_frame_input_pending_carry = false
desktop_quit = false
desktop_intercept_close = false
@@ -689,7 +691,7 @@ DesktopApplyResize :: () -> bool {
#program_export
CloseDesktop :: () {
FrameInputProviderReset(*desktop_frame_input)
desktop_physical_buttons=0; desktop_physical_sampled=false
desktop_physical_buttons=0; desktop_physical_sampled=false; desktop_foreign_buttons=0
desktop_frame_input_pending_carry = false
desktop_quit = false
desktop_intercept_close = false
@@ -726,6 +728,50 @@ CloseDesktop :: () {
desktop_drop_count = 0
}
// SDL_BUTTON_LMASK, SDL_BUTTON_MMASK and SDL_BUTTON_RMASK.
DesktopButtonLeft :: 1;
DesktopButtonMiddle :: 2;
DesktopButtonRight :: 4;
DesktopButtonPoll :: struct {
// The physical buttons this window may act on.
down: u32
pressed: u32
released: u32
// The physical buttons held by a press this window never received.
foreign: u32
}
// One X11 poll's buttons. physical is the server's state and previous the last
// poll's; cached is what SDL was told by events and events_pressed and
// events_released are the edges those events carried; foreign was held back by
// the last poll. A press the server shows that no event reported and SDL's
// cache does not hold reaches only another client, such as a window manager
// moving the window under Meta: this window sees neither it nor its release,
// and a later event for it takes it back. A press SDL does hold is a missed
// event: it is pressed here.
DesktopButtonSample :: (physical: u32, previous: u32, sampled: bool, cached: u32,
foreign: u32, events_pressed: u32, events_released: u32) -> DesktopButtonPoll {
poll: DesktopButtonPoll
poll.pressed = events_pressed
kept: u32 = foreign & ~events_pressed
poll.released = events_released & ~kept
foreign_now: u32 = kept
if sampled {
began: u32 = physical & ~previous & ~events_pressed
ended: u32 = previous & ~physical
foreign_now |= began & ~cached
poll.pressed |= began & cached
poll.released |= ended & ~kept
} else {
// Starting while another client holds a button must not adopt it.
foreign_now |= physical & ~cached & ~events_pressed
}
poll.foreign = foreign_now & physical
poll.down = physical & ~poll.foreign
return poll
}
#program_export
SetPointerFrame :: (x: float32, y: float32, down: bool,
pressed: bool, released: bool) {
@@ -1022,20 +1068,33 @@ PollDesktop :: () -> bool {
// X11's SDL event cache can miss a release while another window has
// keyboard focus, then suppress the next press as already held. Query
// actual button state there and retain event edges for taps between polls.
// Other drivers keep their existing event-backed sampling.
// A press SDL never reported and its cache does not hold is another
// client's, such as a window manager's Meta drag or title bar, and acts
// on nothing here. Other drivers keep their existing event-backed sampling.
driver:=GetCurrentVideoDriver()
if driver!=null && TextFromCString(driver)=="x11" {
cached := buttons
buttons=GetGlobalMouseState(null,null)
if elsewhere { buttons = 0 }
if desktop_physical_sampled {
if (buttons&cast(u32)1)!=0 && (desktop_physical_buttons&cast(u32)1)==0 { pressed=true }
if (buttons&cast(u32)1)==0 && (desktop_physical_buttons&cast(u32)1)!=0 { released=true }
if (buttons&cast(u32)4)!=0 && (desktop_physical_buttons&cast(u32)4)==0 { right_pressed=true }
if (buttons&cast(u32)4)==0 && (desktop_physical_buttons&cast(u32)4)!=0 { right_released=true }
if (buttons&cast(u32)2)!=0 && (desktop_physical_buttons&cast(u32)2)==0 { middle_pressed=true }
if (buttons&cast(u32)2)==0 && (desktop_physical_buttons&cast(u32)2)!=0 { middle_released=true }
}
events_pressed: u32 = 0
if pressed { events_pressed |= DesktopButtonLeft }
if middle_pressed { events_pressed |= DesktopButtonMiddle }
if right_pressed { events_pressed |= DesktopButtonRight }
events_released: u32 = 0
if released { events_released |= DesktopButtonLeft }
if middle_released { events_released |= DesktopButtonMiddle }
if right_released { events_released |= DesktopButtonRight }
poll := DesktopButtonSample(buttons, desktop_physical_buttons, desktop_physical_sampled,
cached, desktop_foreign_buttons, events_pressed, events_released)
pressed = (poll.pressed & DesktopButtonLeft) != 0
middle_pressed = (poll.pressed & DesktopButtonMiddle) != 0
right_pressed = (poll.pressed & DesktopButtonRight) != 0
released = (poll.released & DesktopButtonLeft) != 0
middle_released = (poll.released & DesktopButtonMiddle) != 0
right_released = (poll.released & DesktopButtonRight) != 0
desktop_physical_buttons=buttons; desktop_physical_sampled=true
desktop_foreign_buttons = poll.foreign
buttons = poll.down
}
down: bool = (buttons & cast(u32)1) != cast(u32)0
// A press and release inside one poll (a tap) still counts as held for
+11 -1
View File
@@ -27,6 +27,8 @@ PointerState :: struct {
released_x: float32
released_y: float32
captures_drag: bool
// Once a press moves beyond tap slop it stays a drag, even if it returns.
dragged: bool
// The control a press focuses once it becomes a click; see
// TreePointerUpdate.
focus_generation: u64
@@ -465,6 +467,8 @@ TreeTextFocused :: (session: Session) -> bool {
return false
}
// Moving more than eight logical pixels from a press cancels its click,
// including if it returns before release. Drag delivery itself stays intact.
// A press focuses the control under it. While a text field has the focus, a
// press on another control waits until it becomes a click, so a drag that a
// scroll takes over leaves the text focused and the soft keyboard open, as
@@ -495,6 +499,7 @@ TreePointerUpdate :: (session: Session, frame: PointerFrame) -> s32 {
}
if frame.pressed {
active_pointer[input_slot].captures_drag = false
active_pointer[input_slot].dragged = false
active_pointer[input_slot].held_generation = 0
active_pointer[input_slot].pressed_generation = 0
active_pointer[input_slot].released_generation = 0
@@ -512,6 +517,11 @@ TreePointerUpdate :: (session: Session, frame: PointerFrame) -> s32 {
entry.captures_drag
}
}
if (frame.down || frame.released) && active_pointer[input_slot].held_generation != 0 {
dx := frame.x - active_pointer[input_slot].press.x
dy := frame.y - active_pointer[input_slot].press.y
if dx * dx + dy * dy > 64.0 { active_pointer[input_slot].dragged = true }
}
if frame.down && active_pointer[input_slot].held_generation != 0 {
active_pointer[input_slot].held_x = frame.x
active_pointer[input_slot].held_y = frame.y
@@ -522,7 +532,7 @@ TreePointerUpdate :: (session: Session, frame: PointerFrame) -> s32 {
active_pointer[input_slot].held_generation
active_pointer[input_slot].released_x = frame.x
active_pointer[input_slot].released_y = frame.y
if hit >= 0 && active_pointer[input_slot].held_generation ==
if !active_pointer[input_slot].dragged && hit >= 0 && active_pointer[input_slot].held_generation ==
trees[input_slot].committed[hit].identity_generation {
active_pointer[input_slot].activation_generation =
active_pointer[input_slot].held_generation
+95
View File
@@ -0,0 +1,95 @@
Desktop :: #import "desktop_runtime";
sdl :: #system_library "SDL2";
Delay :: (ms: u32) #foreign sdl "SDL_Delay";
WindowPosition :: (window: *void, x: *s32, y: *s32) #foreign sdl "SDL_GetWindowPosition";
x11 :: #system_library "X11";
OpenDisplay :: (name: *u8) -> *void #foreign x11 "XOpenDisplay";
CloseDisplay :: (display: *void) -> s32 #foreign x11 "XCloseDisplay";
Root :: (display: *void) -> u64 #foreign x11 "XDefaultRootWindow";
Grab :: (display: *void, window: u64, owner_events: s32, mask: u32,
pointer_mode: s32, keyboard_mode: s32, confine: u64, cursor: u64, time: u64) -> s32 #foreign x11 "XGrabPointer";
Ungrab :: (display: *void, time: u64) -> s32 #foreign x11 "XUngrabPointer";
Sync :: (display: *void, discard: s32) -> s32 #foreign x11 "XSync";
xtst :: #system_library "Xtst";
Move :: (display: *void, screen: s32, x: s32, y: s32, delay: u64) -> s32 #foreign xtst "XTestFakeMotionEvent";
Button :: (display: *void, button: u32, down: s32, delay: u64) -> s32 #foreign xtst "XTestFakeButtonEvent";
SampleCases :: () -> s32 {
// A WM-owned press and its release remain foreign across polls.
held := Desktop.DesktopButtonSample(1, 0, true, 0, 0, 0, 0)
if held.down != 0 || held.pressed != 0 || held.foreign != 1 { return 1 }
still := Desktop.DesktopButtonSample(1, 1, true, 0, held.foreign, 0, 0)
if still.down != 0 || still.pressed != 0 || still.released != 0 { return 2 }
ended := Desktop.DesktopButtonSample(0, 1, true, 0, still.foreign, 0, 1)
if ended.down != 0 || ended.released != 0 || ended.foreign != 0 { return 3 }
// Opening mid-grab must not adopt the already held button.
initial := Desktop.DesktopButtonSample(1, 0, false, 0, 0, 0, 0)
if initial.down != 0 || initial.foreign != 1 { return 4 }
// An SDL-cached press recovers a missed event; its release is kept.
recovered := Desktop.DesktopButtonSample(1, 0, true, 1, 0, 0, 0)
if recovered.down != 1 || recovered.pressed != 1 { return 5 }
released := Desktop.DesktopButtonSample(0, 1, true, 0, 0, 0, 0)
if released.released != 1 { return 6 }
// A complete tap between polls keeps both event edges.
tap := Desktop.DesktopButtonSample(0, 0, true, 0, 0, 1, 1)
if tap.pressed != 1 || tap.released != 1 { return 7 }
// A real window event takes ownership back. Other buttons are independent.
owned := Desktop.DesktopButtonSample(5, 1, true, 5, 1, 5, 0)
if owned.down != 5 || owned.pressed != 5 || owned.foreign != 0 { return 8 }
return 0
}
// All synthetic input goes to this test's private Xvfb. A grab on its root
// reproduces a window manager moving the app without sending it button events.
GrabCase :: () -> s32 {
if !Desktop.OpenDesktopSized(160, 120) { return 10 }
defer Desktop.CloseDesktop()
Desktop.BeginDesktopFrame()
if !Desktop.EndDesktopFrame() { return 11 }
display := OpenDisplay(null)
if display == null { return 12 }
defer { unused CloseDisplay(display) }
x: s32; y: s32
WindowPosition(Desktop.DesktopWindow(), *x, *y)
unused Move(display, -1, x + 50, y + 50, 0)
unused Sync(display, 0)
Delay(30)
if !Desktop.PollDesktop() { return 13 }
if Grab(display, Root(display), 0, 12, 1, 1, 0, 0, 0) != 0 { return 14 }
defer { unused Ungrab(display, 0); unused Sync(display, 0) }
unused Button(display, 1, 1, 0)
unused Sync(display, 0)
Delay(20)
if !Desktop.PollDesktop() { return 15 }
pointer := Desktop.PollPointer()
if pointer.down || pointer.pressed || pointer.released { return 16 }
unused Button(display, 1, 0, 0)
unused Sync(display, 0)
Delay(20)
if !Desktop.PollDesktop() { return 17 }
pointer = Desktop.PollPointer()
if pointer.down || pointer.pressed || pointer.released { return 18 }
unused Ungrab(display, 0)
unused Sync(display, 0)
unused Button(display, 1, 1, 0)
unused Sync(display, 0)
Delay(20)
if !Desktop.PollDesktop() { return 19 }
pointer = Desktop.PollPointer()
if !pointer.down || !pointer.pressed { return 20 }
unused Button(display, 1, 0, 0)
unused Sync(display, 0)
Delay(20)
if !Desktop.PollDesktop() { return 21 }
pointer = Desktop.PollPointer()
if pointer.down || !pointer.released { return 22 }
return 0
}
#program_export
main :: () -> s32 {
sampled := SampleCases()
if sampled != 0 { return sampled }
return GrabCase()
}
+22
View File
@@ -0,0 +1,22 @@
#!/bin/sh
set -eu
repo=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
. "$repo/tests/toolchain.sh"
work="$repo/build/scratch/desktop-pointer"
mkdir -p "$work/runtime"
unset DISPLAY WAYLAND_DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS
unset SESSION_MANAGER DESKTOP_SESSION XDG_SESSION_ID XDG_SESSION_TYPE
unset SDL_WINDOWID WAYLAND_SOCKET
export HARMONY_DESKTOP_RECOVERY=0 YUE_DESKTOP_RECOVERY=0
export TMPDIR="$work" XDG_RUNTIME_DIR="$work/runtime"
export SDL_VIDEODRIVER=x11 SDL_RENDER_DRIVER=software
libs="$(pkg-config --libs sdl2 cairo freetype2 pangocairo fontconfig glib-2.0) -lX11 -lXtst -lm -lpthread"
source="$repo/tests/desktop_pointer_test.zi"
LDLIBS="$libs" "$ziran" build --project --root "$repo/tests" \
--target=c --entry desktop_pointer_test:main --exe -o "$work/c" "$source"
xvfb-run -a -s "-noreset -screen 0 640x480x24" "$work/c/desktop_pointer_test"
"$ziran" ir --project --root "$repo/tests" -o "$work/ir" "$source"
LDLIBS="$libs" "$ziran" build --target=c --root "$work/ir" \
--entry desktop_pointer_test:main --exe -o "$work/saved" "$work/ir/desktop_pointer_test.zir"
xvfb-run -a -s "-noreset -screen 0 640x480x24" "$work/saved/desktop_pointer_test"
echo "Desktop pointer: source and saved-IR native grab and click local tests passed"
+29
View File
@@ -49,6 +49,35 @@ Answer :: () -> s32 {
if !Frame(0.2) || !result.clicked || !result.released || result.held ||
result.x != 20.0 || result.y != 20.0 { return 3 }
// Small hand jitter is still a tap.
Pointer(20.0, 20.0, true, true, false)
if !Frame(0.21) { return 43 }
Pointer(24.0, 24.0, false, false, true)
if !Frame(0.22) || !result.clicked { return 44 }
// A drag inside the same large control must not activate it.
Pointer(20.0, 20.0, true, true, false)
if !Frame(0.23) { return 45 }
Pointer(60.0, 20.0, true, false, false)
if !Frame(0.24) || !result.down { return 46 }
Pointer(60.0, 20.0, false, false, true)
if !Frame(0.25) || result.clicked || !result.released { return 47 }
// Returning to the start does not turn that drag back into a tap.
Pointer(20.0, 20.0, true, true, false)
if !Frame(0.26) { return 48 }
Pointer(200.0, 20.0, true, false, false)
if !Frame(0.27) { return 49 }
Pointer(20.0, 20.0, false, false, true)
if !Frame(0.28) || result.clicked || !result.released { return 50 }
// A slow frame may first see movement on release.
Pointer(20.0, 20.0, true, true, false)
if !Frame(0.29) { return 51 }
Pointer(70.0, 20.0, false, false, true)
if !Frame(0.30) || result.clicked { return 52 }
Pointer(20.0, 20.0, true, true, false)
if !Frame(0.31) { return 53 }
Pointer(20.0, 20.0, false, false, true)
if !Frame(0.32) || !result.clicked { return 54 }
// A long press holds once and does not click when it lifts.
Pointer(20.0, 20.0, true, true, false)
if !Frame(1.0) || !result.down || result.held { return 4 }