Skip to content

gtk: capture the mouse for real, on Wayland and on X11 - #949

Open
danifunker wants to merge 1 commit into
mainfrom
gtk-pointer-capture
Open

danifunker wants to merge 1 commit into
mainfrom
gtk-pointer-capture

Conversation

@danifunker

@danifunker danifunker commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

On Linux the mouse did not reach the guest at all on a fresh install, and it
looked like a missing feature rather than a bug. Three correct-looking pieces:

  • the GTK shell only ever sent DisplayAction::PointerAt, an absolute
    position, because GDK 4 exposes no way to grab, lock, confine or warp a
    pointer (GTK 4.22's headers have only the read-only
    gdk_display_device_is_grabbed);
  • DisplayController::apply_with drops PointerAt unless the pointer mode
    is Follow;
  • PointerMode::Capture is #[default].

The GTK shell was also the only shell that never consulted follows_pointer,
which is what let the mismatch go unnoticed. A clanker/ note had recorded
"GTK 4 has no pointer lock, so use Follow" as a design decision; this PR
supersedes it, because Follow being the only mode the shell could drive left
Capture — the default — silently dead.

What capture does now

Below GTK, through GDK's own connection, so no second display is opened and no
second event loop runs.

Wayland — zwp_pointer_constraints_v1 locks the pointer and
zwp_relative_pointer_v1 reports deltas. Both managers are bound on an event
queue of our own; because a new Wayland object inherits the queue of the object
that created it, their children's events arrive there and never in GDK's
queue. GDK reads the socket as part of its main loop, so a tick callback only
has to dispatch_pending what is already ours — never a blocking dispatch that
would fight GDK. Under a lock the compositor sends no position at all, which is
why the deltas cannot come from GTK; that is the bug QEMU's GTK backend has.

X11 — a confining XGrabPointer with owner_events: true, so GTK's own
handlers keep working, and the pointer warped back to the middle of the view
after each motion event. The delta is how far it got before the warp; the
warp's own event reports the middle, so a zero delta is the echo and is
dropped. Device pixels, not logical ones, or capture drifts on HiDPI.

Buttons and the wheel need none of this — both servers keep delivering them to
the surface while the pointer is held. Only motion and the cursor change.

Neither available — the capture is given up rather than silently doing
nothing: the pointer mode falls back to Follow so the mouse still reaches the
guest, and a message says which it is. The release chord (Ctrl+Alt) is shown
once per run, since the Mac shows that hint beside the display and Windows
inside it.

This is the same mechanism spice-gtk adopted for the same reason
(rhbz#1674311), and the
two protocols are what the Arch Wiki lists for input grabbing in VM windows.
VirtualBox and VMware avoid the problem by being X11 apps under XWayland,
which a native GTK 4 app cannot do without giving up native scaling.

Wording

Preferences > Mouse described itself in macOS terms on every platform — "Follow
the Mac pointer", "Command shortcuts stay with the Mac" — because the Fluent
catalog is shared by all four shells. Those are now host-neutral, and
prefs-pointer-no-lock is new.

Dependencies

wayland-client, wayland-backend, wayland-protocols and x11-dl, in the
Linux-only target section, at the versions third_party/winit-0.30.13 already
brings into the graph — so nothing resolves a second copy.
wayland-backend/client_system is required rather than preferred: only the
libwayland backend can wrap GDK's wl_display and its existing proxies.
x11-dl loads Xlib at run time, which keeps X11 headers out of the build; it
does not save the host a libX11 install, as the commit message claims -- GTK
links libX11 itself for its own X11 backend, confirmed with ldd on the built
binary.

Verification

  • cargo check -p iris-gui-gtk — clean, no warnings.
  • cargo check --tests -p iris-gui-core -p iris-gui-gtk — clean.
  • iris-gui/scripts/check-boundaries.sh — 0, the shell still names neither the
    emulator nor the model.
  • Every t! id in the shell (127) resolves against the catalogs.

Not verified here, and this needs saying: the capture itself wants a human
at a Wayland session and at an X11 one — a lock is not something a test
asserts. And cargo test -p iris-gui-core could not be built on the machine I
worked on (the iris test binary exceeds the memory I could safely give it),
so the core's display and i18n tests need CI.

🤖 Generated with Claude Code

A fresh Linux install had no working mouse in the guest. The GTK shell could
only send DisplayAction::PointerAt, an absolute position, because GDK 4
exposes no way to grab, lock, confine or warp a pointer; the core drops
PointerAt unless the pointer mode is Follow; and PointerMode::Capture is the
default. Three correct-looking pieces, no mouse. The GTK shell was also the
only shell that never consulted follows_pointer, which is what let the
mismatch go unnoticed.

Capture now happens below GTK, through GDK's own connection, so no second
display is opened and no second event loop runs:

  - Wayland: zwp_pointer_constraints_v1 locks the pointer and
    zwp_relative_pointer_v1 reports deltas. Both managers are bound on an
    event queue of our own, so their children's events arrive there and not
    in GDK's queue. GDK reads the socket as part of its main loop, so a tick
    callback only has to dispatch what is already ours. Under a lock the
    compositor sends no position at all, which is why the deltas cannot come
    from GTK -- the bug QEMU's GTK backend has.
  - X11: a confining XGrabPointer, with the pointer warped back to the middle
    of the view after each motion event, so the delta is how far it got
    before the warp. Device pixels, not logical ones, or it drifts on HiDPI.

Buttons and the wheel need none of this; both servers keep delivering them to
the surface while the pointer is held. Only motion and the cursor change.

A desktop that allows neither mechanism is not left without a mouse: the
capture is given up, the pointer mode falls back to Follow, and a message
says which it is. The release chord is shown once per run, since the Mac
shows the hint beside the display and Windows inside it.

Preferences > Mouse no longer describes itself in macOS terms: the catalog is
shared by all four shells, so "the Mac pointer" and "Command shortcuts stay
with the Mac" are now host-neutral.

The four crates this needs are already in the graph through
third_party/winit-0.30.13, at the versions used there, so nothing resolves a
second copy. wayland-backend/client_system is required rather than preferred:
only the libwayland backend can wrap GDK's display and its existing proxies.

Verified: cargo check -p iris-gui-gtk clean, cargo check --tests clean for
iris-gui-core and iris-gui-gtk, iris-gui/scripts/check-boundaries.sh at 0,
every t! id in the shell resolves against the catalogs. The capture itself
needs a human at a Wayland and an X11 session; the core's own display tests
could not be run here for want of memory and need CI.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant