Wayland touch: events a touch listener dispatches run before the stand-in pointer event, so a tap can be lost or the left button left held #193

Open
opened 2026-11-17 06:45:38 +00:00 by jorijnvdgraaf · 0 comments

Summary

For the first finger, each touch handler fires the touch event and then the pointer event the finger stands in for, and it updates the bookkeeping (primaryTouchId, mouseLeftHeld) between the two. A listener of the touch event that dispatches Wayland events itself (it makes a Window, or calls Clipboard::GetText) runs the handlers of whatever is queued behind that event inside itself, against the half-updated state. A tap can leave the left button held, a second tap can lose its click, and the stand-in events arrive out of order with the events dispatched inside the listener. Line numbers refer to c006519.

Details

  • touch_down (implementations/Crafter.Graphics-Device.cpp:734-745): onTouchDown.Invoke, and only then primaryTouchId = id, mouseLeftHeld = true and onMouseLeftClick.Invoke.
  • touch_up (:759-765): onTouchUp.Invoke, and only then primaryTouchId = -1, mouseLeftHeld = false and onMouseLeftRelease.Invoke.
  • The dispatches a listener can reach: the Window constructor (implementations/Crafter.Graphics-Window.cpp:392, wl_display_dispatch) and Clipboard::GetText when the selection holds text (implementations/Crafter.Graphics-Clipboard.cpp:341, :368, wl_display_dispatch_pending). Both dispatch the default queue, which is where wl_touch's events are. libwayland takes an event off the queue and releases its lock before it calls the handler, so a dispatch from inside a handler runs everything still queued (wayland 1.26.0, src/wayland-client.c, dispatch_event and dispatch_queue).

Concrete cases

  1. The left button stays held. A tap's down and up are both queued, the loop having been busy. touch_down fires onTouchDown, and a listener dispatches. The nested touch_up finds primaryTouchId == -1, fires no release and clears touchWindow. Back in touch_down: primaryTouchId = id, mouseLeftHeld = true, onMouseLeftClick. No up is left to release it.
  2. A tap is lost. Two taps are queued. In the first tap's touch_up, an onTouchUp listener dispatches. The nested touch_down of the second finger finds primaryTouchId still set and fires no click. The outer touch_up then clears primaryTouchId, so the second finger is never the primary one: no click, no move and no release for it.
  3. Time runs backwards. Since 364d210 the stand-in event is given its own touch's lastInputTime again (:744, :764, :785), but it fires after the events dispatched inside the listener, which carry later times. A consumer that subtracts consecutive times as uint32 gets a value near 2^32. AGENTS.consumer.md says what a dispatching listener does to the time for itself and for the listeners after it; it does not say this.

Found by reading the code, in the pre-push review of 364d210..c006519. The order is older than that range. Not reproduced on a compositor. tests/InputTime shows how to stage it without one: a touch listener that calls the handler a nested dispatch would run.

Suggested fix

Finish the state before any listener runs. In touch_down, set primaryTouchId, focusedWindow, currentMousePos and mouseLeftHeld before onTouchDown.Invoke; in touch_up, clear them before onTouchUp.Invoke; keep in a local whether this finger was the primary one, so that a nested handler meets a consistent state and the stand-in event still fires for the right finger. That cures 1 and 2. For 3, either fire the stand-in event before the touch event, or say in the consumer notes that the two are not ordered against what a listener dispatches.

## Summary For the first finger, each touch handler fires the touch event and then the pointer event the finger stands in for, and it updates the bookkeeping (`primaryTouchId`, `mouseLeftHeld`) between the two. A listener of the touch event that dispatches Wayland events itself (it makes a `Window`, or calls `Clipboard::GetText`) runs the handlers of whatever is queued behind that event inside itself, against the half-updated state. A tap can leave the left button held, a second tap can lose its click, and the stand-in events arrive out of order with the events dispatched inside the listener. Line numbers refer to `c006519`. ## Details - `touch_down` (`implementations/Crafter.Graphics-Device.cpp:734-745`): `onTouchDown.Invoke`, and only then `primaryTouchId = id`, `mouseLeftHeld = true` and `onMouseLeftClick.Invoke`. - `touch_up` (`:759-765`): `onTouchUp.Invoke`, and only then `primaryTouchId = -1`, `mouseLeftHeld = false` and `onMouseLeftRelease.Invoke`. - The dispatches a listener can reach: the `Window` constructor (`implementations/Crafter.Graphics-Window.cpp:392`, `wl_display_dispatch`) and `Clipboard::GetText` when the selection holds text (`implementations/Crafter.Graphics-Clipboard.cpp:341`, `:368`, `wl_display_dispatch_pending`). Both dispatch the default queue, which is where `wl_touch`'s events are. libwayland takes an event off the queue and releases its lock before it calls the handler, so a dispatch from inside a handler runs everything still queued (wayland 1.26.0, `src/wayland-client.c`, `dispatch_event` and `dispatch_queue`). ## Concrete cases 1. The left button stays held. A tap's down and up are both queued, the loop having been busy. `touch_down` fires `onTouchDown`, and a listener dispatches. The nested `touch_up` finds `primaryTouchId == -1`, fires no release and clears `touchWindow`. Back in `touch_down`: `primaryTouchId = id`, `mouseLeftHeld = true`, `onMouseLeftClick`. No up is left to release it. 2. A tap is lost. Two taps are queued. In the first tap's `touch_up`, an `onTouchUp` listener dispatches. The nested `touch_down` of the second finger finds `primaryTouchId` still set and fires no click. The outer `touch_up` then clears `primaryTouchId`, so the second finger is never the primary one: no click, no move and no release for it. 3. Time runs backwards. Since 364d210 the stand-in event is given its own touch's `lastInputTime` again (`:744`, `:764`, `:785`), but it fires after the events dispatched inside the listener, which carry later times. A consumer that subtracts consecutive times as `uint32` gets a value near 2^32. `AGENTS.consumer.md` says what a dispatching listener does to the time for itself and for the listeners after it; it does not say this. Found by reading the code, in the pre-push review of 364d210..c006519. The order is older than that range. Not reproduced on a compositor. `tests/InputTime` shows how to stage it without one: a touch listener that calls the handler a nested dispatch would run. ## Suggested fix Finish the state before any listener runs. In `touch_down`, set `primaryTouchId`, `focusedWindow`, `currentMousePos` and `mouseLeftHeld` before `onTouchDown.Invoke`; in `touch_up`, clear them before `onTouchUp.Invoke`; keep in a local whether this finger was the primary one, so that a nested handler meets a consistent state and the stand-in event still fires for the right finger. That cures 1 and 2. For 3, either fire the stand-in event before the touch event, or say in the consumer notes that the two are not ordered against what a listener dispatches.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Catcrafts/Crafter.Graphics#193
No description provided.