From 3c51e959694f4d6a2beaad48dc99964b0aacf53f Mon Sep 17 00:00:00 2001 From: Guoliang Chen Date: Wed, 16 Sep 2026 02:38:11 -0700 Subject: [PATCH] input-capture: close lane (lead review); brief: input-inject Co-Authored-By: Claude Fable 5.1 --- docs/features/input-capture/README.md | 18 +++++ docs/features/input-inject/README.md | 112 ++++++++++++++++++++++++++ docs/loop.md | 10 ++- 3 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 docs/features/input-inject/README.md diff --git a/docs/features/input-capture/README.md b/docs/features/input-capture/README.md index 85e29eb..bbffc5d 100644 --- a/docs/features/input-capture/README.md +++ b/docs/features/input-capture/README.md @@ -607,3 +607,21 @@ findings need an actual code or prose change, so none were applied here. Verdict: REWORK ## Review + +- 2026-09-16 (lead): inspected per `docs/loop.md` §5 after one rework + round. Gates green run by me; nine files in the brief's list plus TODO + §4, protocol §10.4 and this README. Read the tap callback in full: + injected tag passes through first, tap-loss re-enables and routes + local before anything else, hotkeys matched on press only, escape + flips routing inside the callback, unmapped keys stay local and are + counted, `swallowKeys`/`swallowMouse` decide the return. QA + spot-checked 30+ VK values and the encodings. Verdict: **merge**. + Carry into input-inject: tag every posted event via the source's + `userData`, including `releaseAll()` key-ups; perform the lock + hotkeys by chord; `isExtended(vk:keycode:)` exists because keypad + Enter shares `VK_RETURN`. Carry into app-shell: the open requests in + this README (`EventTap.now()` timestamps, ownership of the swallow + flags, `releaseAll` + `Cursor.recouple` after `.tapLost`/`.overflow`/ + `.escapedToLocal`, `Screens.startObserving()` on the main thread). + Unverified until CP1: horizontal wheel sign, `kVK_Help→VK_INSERT`, + `KeypadClear→VK_NUMLOCK` (protocol §10.4 note 10). diff --git a/docs/features/input-inject/README.md b/docs/features/input-inject/README.md new file mode 100644 index 0000000..10ea8d5 --- /dev/null +++ b/docs/features/input-inject/README.md @@ -0,0 +1,112 @@ +# input-inject + +## Goal + +The Mac's side of `InputSimulation.cs`: turn trusted `Keyboard` and +`Mouse` packets into `CGEvent`s posted to the HID event tap, tagged so +our own `EventTap` passes them through, with the click-count, held-key +and cursor-bounds bookkeeping macOS needs and Windows gets for free from +`SendInput`. Also the local reactions to remote chords +(`InputSimulation.cs:358-475`): Win+L locks this Mac, Ctrl+Alt+Del is +ignored, and `releaseAll()` — the `ReleaseAllKeys` of +`InitAndCleanup.cs:214-243` — that contracts C2 calls on every recovery. + +## Assumptions + +- **One `CGEventSource(stateID: .hidSystemState)` per `Injector`, with + `userData = InputTypes.injectedEventTag`** so every event it creates + carries the tag in `eventSourceUserData` and the tap's `RealData` + check (`InputHook.cs:213-220`) passes it through. Events are posted + with `.post(tap: .cghidEventTap)`, the closest thing to `SendInput`. +- **Keyboard** (`InputSimulation.cs:108-157`): `wVk` → + `KeyMap.keycode(forVK:)`; unknown VKs are dropped and counted (Windows + would `MapVirtualKey` them; we have no keycode to post). `LLKHF.UP` + → key up. Modifier VKs (`LSHIFT`…`RWIN`, `SHIFT`/`CONTROL`/`MENU` + generic forms mapped to the left key) post as key events for the + modifier keycode, which macOS delivers as `flagsChanged`; the injector + keeps a `KeyState` of held modifiers and sets `event.flags` on + **every** posted key and mouse event from it, otherwise ⌘C from a + Windows keyboard arrives as a bare C. `EXTENDED` is informational + here (the keycode already distinguishes keypad Enter etc.). Repeats + arrive as repeated downs and are posted as such with + `keyboardEventAutorepeat = 1` after the first. The 10 ms sleep after + key-up (`Event.cs:268-271`) is the sender's, not ours. +- **Caps Lock**: `VK_CAPITAL` down+up toggles; post the keycode pair + and let the system toggle the LED state. +- **Mouse** (`InputSimulation.cs:160-219`): `dwFlags` is a `WM_*` id. + `WM_MOUSEMOVE` with absolute coordinates → `Coordinates.fromUniversal` + onto the **desktop** bounds (`:160-173`); with the relative bias → + delta applied to the current pointer (`Receiver.cs:147-153`); the + result is clamped to the desktop union. A move while a button is held + posts the matching `*MouseDragged` type (macOS needs it for drags; + Windows does not distinguish). Buttons: L/R/M down/up → the CG button + types with `mouseEventButtonNumber` 0/1/2; `WM_XBUTTON*` with + `WheelDelta` 1/2 → `.otherMouseDown/Up` with button number 3/4. + Windows drops `*DBLCLK` (no `MOUSEEVENTF` case); double clicks arrive + as two downs, so **click count is ours**: consecutive downs of the + same button within `NSEvent.doubleClickInterval` and 4 px set + `mouseEventClickState` 2, 3…, reset otherwise. +- **Wheel**: `WM_MOUSEWHEEL` → `CGEvent(scrollWheelEvent2Source:units: + .line, wheelCount: 1, wheel1: WheelDelta / 120)`; `WM_MOUSEHWHEEL` → + `wheelCount: 2, wheel2:`. Sign: Windows +120 is away from the user, + which is CG line +1. The user's "natural scrolling" preference is + applied by the system to posted events too; whether that matches what + they expect from a remote mouse is a CP1 question, recorded in §10.5. +- **Remote chords** (`InputSimulation.cs:358-475`): the injector tracks + the remote's Win/Ctrl/Alt/Shift from the VKs it receives; Win+L + (`VK_L` with Win held) locks this Mac by running + `/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend` + rather than injecting the chord (which would open Spotlight-like + shortcuts); Ctrl+Alt+Del is swallowed and logged; Ctrl+Alt+End + (service start) is ignored (README D6). +- **`releaseAll()`** posts key-up for every key and button the injector + believes is down — a superset of Windows' fixed modifier list + (`InitAndCleanup.cs:214-243`) because a lost key-up for a letter is as + bad as for Shift — then clears `KeyState`. Rate limit of once per 2 s + (`:218`) is **not** ported: recovery must always work (C2). +- **Threading**: the injector is driven by the Engine actor and posts + from its own serial `DispatchQueue`; posting is thread-safe but the + bookkeeping is not, so all of it lives on that queue. Not `Sendable` + beyond that queue. +- **Cursor bounds** come from `ScreenLayout` passed in with each + packet; the injector never calls `Screens` itself. + +## Handshakes + +### Consumed + +- `input-capture`: `InputTypes.injectedEventTag`, `WindowsMessage`, + `KeyFlag`, `KeyMap.keycode(forVK:)`, `ScreenLayout`, and the promise + that tagged events are passed through untouched. +- `session-matrix`: `Coordinates.fromUniversal`, the relative-bias decode. +- `wire-core`: `Packet.mouse` / `Packet.keyboard`. + +### Promised (to app-shell) + +```swift +public final class Injector { + public init() + public func inject(_ packet: Packet, layout: ScreenLayout) // Keyboard or Mouse; anything else ignored + public func releaseAll() // C2; safe to call any time + public func lockThisMac() // CGSession -suspend + public var droppedUnmappedKeys: UInt64 { get } + public var remoteModifiers: ModifierState { get } // for the menu bar / debugging +} +``` + +## Status / TODO + +- [ ] `Sources/MWBMac/KeyState.swift`: held keys/buttons, modifier flag + word, click-count tracking, remote-chord state + (`InputSimulation.cs:358-475`, `InitAndCleanup.cs:214-243`). +- [ ] `Sources/MWBMac/Injector.swift`: the tagged source, keyboard and + mouse posting per Assumptions, wheel, clamping, `releaseAll`, + `lockThisMac`, Ctrl+Alt+Del handling (`InputSimulation.cs:82-219, + 440-475`, `Receiver.cs:147-153`). +- [ ] `docs/protocol.md`: append §10.5 with anything learned, cited. + +## Work log + +## QA log + +## Review diff --git a/docs/loop.md b/docs/loop.md index 1d7109d..61fd07f 100644 --- a/docs/loop.md +++ b/docs/loop.md @@ -155,8 +155,8 @@ Strictly one at a time, in this order (mirrors `TODO.md`): | 1 | wire-core | merged 2026-09-16 (worktree; the last lane to use one) | | 2 | session-matrix | merged 2026-09-16 (one rework) | | 3 | control-channel | merged 2026-09-16 (two reworks) | -| 4 | input-capture | in progress | -| 5 | input-inject | | +| 4 | input-capture | merged 2026-09-16 (one rework) | +| 5 | input-inject | in progress | | 6 | app-shell | **CP1**: first manual acceptance checkpoint (contracts C5) | | 7 | clipboard | **CP2** | | 8 | extras | **CP3** | @@ -174,6 +174,12 @@ Strictly one at a time, in this order (mirrors `TODO.md`): ## 8. Lane log +- **2026-09-16, lane 4 (input-capture)** — Dev (Opus), QA (Sonnet) + REWORK with four host-side findings (unguarded `thread` field, a + lock taken on the local path, a wrong lock-count claim, switch-to-all + swallow rule), Dev rework, lead inspection. What the brief got wrong: + the modifier mask list omitted right Control (0x2000) — the dev + caught it. Two in-lane bug fixes in the ring's coalescing. - **2026-09-16, lane 3 (control-channel)** — Dev (Opus); QA (Sonnet) REWORK (Name2IP/DNS subnet flow, id collision gated to clients, Awake rule); Dev rework; QA round 2 REWORK (the Awake fix moved the