Preserve Remote key pairs through repeats and failed writes

This commit is contained in:
ak47 committed 2026-11-05 21:05:39 -05:00
1 parent 2d6dba20aa
commit 76993ecbfa
7 files changed
+733 -19

No files matched your search

+74 -10
View File
@@ -1,10 +1,11 @@
# Keyboard conversion prerequisite
`src/keyboard.go` supplies an isolated, bidirectional conversion between XKB
keysym names and NVDA Remote `vk_code`/`extended` identities. It does not capture,
forward or inject keys. The source command still discards incoming keyboard
payloads and requests no keyboard grant. No reader code changed for this
increment.
keysym names and NVDA Remote `vk_code`/`extended` identities. The subsequent
`src/keyboard_state.go` adds outgoing key-pair planning with bounded state and
write acknowledgements. Neither helper captures, forwards or injects keys.
The source command still discards incoming keyboard payloads and requests no
keyboard grant. No reader code changed for either increment.
## Mapping contract
@@ -44,16 +45,70 @@ Unknown profiles, key names and wire identities return one constant error with
no input text or partial key. The maps retain no caller input, held-key state,
desktop handles or consent and can be read concurrently.
## Outgoing key-pair planning
`keyboardEncoder` belongs to one nonzero capture epoch and one serial owner.
It accepts source Xorg keycodes 8–255 and retains at most 32 distinct held
keys. It stores wire identities and source slots, without retaining key names
or mapper references. A fresh down uses the supplied explicit map; repeat and
release retain that down's identity even if the name, layout/profile or Num
Lock state changes. Fresh downs after a completed release use the current map.
Events must come from a future trusted core capture service. Marked repeats
are repeated downs of an already held source. Ordinary keys emit another down;
modifier and lock-key repeats emit nothing. A release for an unowned source is
suppressed. A second unmarked down for a held source, orphan repeat, repeat
marked on an up, invalid source code, unsupported fresh mapping, colliding wire
identity or held-key overflow stops admission and preserves existing ownership
for cleanup. Two source slots cannot independently hold the same VK/extended
identity; main and keypad Insert remain distinct. This detects codec identity
collisions, not physical/injected collisions on the destination.
Each emitted plan has a strictly increasing local sequence. That sequence is
not added to the NVDA frame. The serial writer acknowledges the exact epoch
and sequence only after its write succeeds. There can be only one outstanding
plan; another `apply` returns backpressure without consuming the event or
changing state. The caller must retain that event in its own bounded queue or
stop the stream, rather than dropping it. Zero, duplicate, late, wrong-epoch
and wrong-sequence acknowledgements cannot consume a pending plan. Sequence
exhaustion stops admission instead of wrapping.
A down reserves ownership before returning its plan. An up retains ownership
until acknowledgement, so queue/write failure cannot erase the key from the
cleanup ledger. Successful write acknowledgement is **not** proof that the
relay or destination applied the event. On any uncertain write or admission
failure, retire the original encoder. Retirement stops it permanently and
transfers a bounded release plan to the caller in reverse down order, including
unacknowledged downs and ups. It clears local state, so repeated retirement
emits no duplicate plan; the caller owns retaining and delivering the returned
plan. Stale epoch cleanup changes nothing. Fresh capture requires a new encoder
and separate reader authorization; it cannot resume this instance.
Neither the mapper nor encoder performs network, native or desktop I/O. The
encoder is not safe for concurrent mutation; its future owner must serialize
events, acknowledgements and retirement. It has no automatic retry, consent,
capture acquisition or native recovery mechanism.
## Integration boundaries
This helper has no callers in the live coordinator. Before connecting it to
These helpers have no callers in the live coordinator. Before connecting them to
keyboard services, the reader must provide exclusive capture, trusted routing,
delivery-time lease checks, a core escape and independent injection cleanup.
The input owner must retain the original key mapping from down through repeat
and up. Layout/Num Lock changes while held must not remap the release. Mapping
failure must suspend input and release only owned keys rather than dropping a
key-up or clearing physical modifiers. Lock-state synchronization, physical
collisions and bypass/repeat handling remain separate unfinished work.
The encoder supplies original wire key-pair identity, but the reader still
must own actual capture/injection and the destination key ledger. Mapping
failure must suspend input and release only owned keys. The caller must stop
and join the old writer before delivering a retirement plan; never send old
cleanup through a replacement connection or forget a failed release. A dead
transport cannot deliver cleanup, so independent target cleanup remains
necessary. Lock-state synchronization, physical collisions and bypass handling
remain unfinished.
The encoder requires normalized autorepeat: a repeated down with no synthetic
release between repeats. Classic X11 release/press pairs must be normalized by
the future native capture owner. The current reader XRecord `RepeatTracker`
marks repeat downs but still exposes releases; it cannot be wired directly to
this encoder. Capture entry with already-held keys, source overflow, bypass
chords and device/layout changes need explicit native policy and tests.
Mapping a supported wire key does not establish that the destination's active
keymap contains it. Injection must resolve destination keycodes on its Xlib
@@ -70,6 +125,15 @@ input, independent concurrent maps, and the real JSON codec and membership
direction checks. These are synthetic protocol tests; they neither consume
desktop input nor exercise an X server or Windows keyboard hooks.
Encoder tests additionally cover mapping changes through repeats/releases,
source boundaries, modifier/lock repeat suppression, unowned ups, duplicate
downs, orphan repeats, collision rejection, 32-key overflow, stale epochs,
pending-write backpressure, exact acknowledgement sequences and retirement.
A closed pipe verifies failed release writes retain cleanup ownership. A private
TLS relay receives planned downs, repeats, original-key ups and post-collision
cleanup in order. These checks exercise the production frame codec/transport
without attaching the encoder to the live coordinator or to desktop services.
The VK values and layout dependence were checked against Microsoft's
[virtual-key reference](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes).
Scan codes and extended flags are distinct Windows event metadata; see