cua icon indicating copy to clipboard operation
cua copied to clipboard

macOS window-scoped click duplicates mouse events in background and unmodified foreground modes

Open wolf3c opened this issue 3 months ago • 3 comments

Summary

On macOS, one window-scoped coordinate click can be delivered twice when the target accepts both per-PID routes. This affects both background and ordinary unmodified foreground calls.

In Cua Driver 0.17.0, window-scoped pixel delivery ultimately sends each event through both:

  1. SkyLight SLEventPostToPid
  2. public CGEvent::post_to_pid

A high-level click(count: 1) can therefore be observed as down, down, up, up. Some receivers synthesize only one logical click from those transitions, which can hide the bug; other applications can expose double-click-like behavior such as opening a separate window or toggling window size.

Environment

  • Cua Driver: 0.17.0
  • macOS: 26.6 (25G72)
  • Architecture: Apple Silicon
  • Action: window-scoped coordinate click
  • Count: 1
  • Delivery postures examined: background and ordinary unmodified foreground

Correction to the original report

The earlier foreground result of 3 mouse-downs / 3 mouse-ups used a receiver report whose route was global_input. It did not exercise the same transport as an ordinary unmodified window-scoped foreground click, so it cannot be used to conclude that delivery_mode: "foreground" avoids duplicate per-PID delivery.

For a window-scoped click without modifiers, foreground assistance activates/raises the target, but the click still goes through the window-local per-PID path.

Reproduction evidence

Background native receiver

Three high-level calls with count: 1 produced:

Delivery posture High-level calls mouseDown received mouseUp received
background 3 6 3

Ordinary unmodified foreground, production-path request

Request:

{
  "pid": 720,
  "window_id": 78,
  "x": 783,
  "y": 563,
  "count": 1,
  "delivery_mode": "foreground"
}

A trusted browser receiver observed:

  • pointerdown × 2
  • mousedown × 2
  • pointerup × 2
  • mouseup × 2
  • synthesized click × 1, detail = 1
  • dblclick × 0

The raw transition order was approximately down, down, up, up within 31 ms.

A manual single click on the same receiver with the same listeners produced exactly one down/up/click sequence, ruling out duplicate event listeners in the receiver.

The fact that this browser synthesized only one click explains why the issue can be invisible in some applications. In a custom-drawn desktop application, we separately observed single-click requests followed by independent-window or window-size-toggle behavior consistent with duplicated low-level delivery; that application has not yet been instrumented with an event tap, so this report does not claim its exact receiver-side event count.

Why foreground does not avoid it

The ordinary unmodified window-scoped foreground call graph is:

click
  -> with_foreground_assist(...)
  -> click_at_xy_with_window_local(...)
  -> click_at_xy_inner(...)
  -> post_mouse_event(...)
  -> SkyLight post_to_pid + CGEvent::post_to_pid

Only a different foreground branch—for example the modified-input/global-input path used by the earlier control—uses global HID input. Therefore delivery_mode: "foreground" is primarily an activation posture, not a guarantee of a single global-HID transport.

Relevant implementation

Release tag: cua-driver-rs-v0.17.0

post_mouse_event implements unconditional dual posting:

crate::input::skylight::post_to_pid(pid as libc::pid_t, event_ptr, false);
event.post_to_pid(pid as libc::pid_t);

The event stamps and shared click-group ID do not guarantee receiver-side deduplication when both routes land. The same dual-post behavior is still present on current main. PR #2837 improves exact-target background input but explicitly leaves foreground behavior unchanged, so it does not appear to resolve this defect.

User impact

  • A receiver that accepts only one route appears normal.
  • A browser may collapse duplicate raw transitions into one synthesized click, masking the problem.
  • A receiver that consumes both copies may expose double-click-like behavior from count: 1.
  • Switching the same window-scoped coordinate action from background to ordinary foreground is not a reliable workaround.

Adjacent audit scope

post_mouse_event is also shared by other pointer primitives such as right click, middle click, and drag. Wheel delivery has a similar dual-route pattern. This issue proves duplicate delivery for click; the adjacent pointer families should be audited, but this report does not claim they all produce the same visible behavior before they are tested.

Expected behavior

One high-level click(count: 1) should produce exactly one logical ordered down/up pair, regardless of background or foreground activation posture.

Suggested direction

Select exactly one transport for each high-level attempt.

Possible designs:

  • choose a deterministic route from target capability/application class;
  • expose or internally own a single-route policy;
  • if an alternate route is needed, try it only as a separate attempt after explicit failure evidence and fresh observation;
  • document foreground activation posture separately from the transport route.

The critical requirement is that one high-level attempt must not silently post two independently consumable copies.

Acceptance criteria

  • [ ] Window-scoped background + count: 1 produces exactly one down/up pair on a receiver that accepts both routes.
  • [ ] Ordinary unmodified window-scoped foreground + count: 1 also produces exactly one down/up pair.
  • [ ] Event order is down, up, never down, down, up, up.
  • [ ] Modified/global-input foreground remains exactly one down/up pair.
  • [ ] count: 2 produces exactly two ordered pairs with correct click state.
  • [ ] Add event-count fixtures for representative AppKit, Catalyst, Chromium, and a receiver that accepts both per-PID routes.
  • [ ] Audit right click, middle click, drag, and wheel for the same route-duplication class.
  • [ ] Documentation states that foreground controls activation posture and does not necessarily select global HID.
  • [ ] No silent dual-post fallback occurs within one high-level action attempt.

wolf3c avatar Aug 05 '26 06:08 wolf3c

Correction/update: an ordinary unmodified window-scoped foreground production-path probe also showed duplicate raw down/up transitions. The earlier 3-down/3-up foreground control used route: "global_input", so it did not exercise the same per-PID path and was not a valid foreground workaround control. I updated the title and body with the corrected call path, event sequence, manual-click baseline, user impact, adjacent audit scope, and expanded acceptance criteria.

wolf3c avatar Aug 05 '26 10:08 wolf3c

maintainer-selected next step in #2907: one invariant regression only. through the existing public MCP click request, verify that an explicit-window unmodified foreground count:1 produces exactly one ordered native down/up pair at the target, both with clickCount=1. establish baseline failure, then run the same test against the existing PR if native permissions permit. no production change or transport architecture is selected.

injaneity avatar Sep 08 '26 01:09 injaneity

Scope I intend to implement, for maintainer selection: extend the single-transport rule that #2907 established for explicit-window left clicks to the remaining pointer primitives in platform-macos/src/input/mouse.rs that still post every event twice.

At current main, post_mouse_event defaults to MousePostMode::Both (SkyLight post_to_pid followed by CGEvent::post_to_pid) for: the pid-routed click_at_xy, right click and middle click (including their mouseMoved primer), drag, and the wheel path's primer. Only the foreground left click uses PublicOnly; the background left click in click_at_xy_chromium already posts through SkyLight and falls back to the public API only when the SPI call is refused. This is the "adjacent audit scope" the acceptance criteria list (right click, middle click, drag, wheel).

Proposed change: one transport per event for every primitive. SkyLight first; the public post_to_pid only when the SPI reports it was not called (true means the SPI was invoked, not that the app consumed the event, so a retry after true is exactly the duplicate this issue describes). MousePostMode goes away; foreground left click keeps its current public-only route unless the maintainer prefers uniformity. No retries, no route switching within one attempt, which matches "no silent dual-post fallback occurs within one high-level action attempt".

Acceptance evidence I will produce:

  • Unit tests on the dispatch policy with a journaling receiver: skylight_mouse_dispatch_does_not_duplicate_receiver_events (move, down:1, up:1, down:2, up:2, drag, wheel each observed once) and unavailable_skylight_preserves_event_order_through_public_fallback.
  • Native event-journal rows in the AppKit harness for right click and drag in both delivery modes, extending harness_appkit_foreground_single_click_has_one_ordered_native_pair to assert one ordered pair per event for harness_appkit_right_click_px_{foreground,background} and harness_appkit_slider_drag_px_{foreground,background}.
  • Baseline failure recorded against main first, then the same rows against the candidate.

Excluded: keyboard, wheel delta semantics, snapshot ownership, and the documentation item about foreground being an activation posture (happy to add a sentence to the tool description if wanted).

will-bogusz avatar Sep 13 '26 10:09 will-bogusz