diff --git a/remote.md b/remote.md index 0ba3058..842414b 100644 --- a/remote.md +++ b/remote.md @@ -563,6 +563,27 @@ physical Braille, real Speech Dispatcher scheduling, NVDA/Orca interoperability and the remaining desktop services stay open; no release gate is complete. See [the live context steps](remote/command.md#physical-keyboard-acceptance-still-pending). +Twenty-ninth implementation increment (2026-11-05): based on reader `9ac3356` +(`testing`) and Tentacles `affe313` (`master`), synchronized with their upstreams. +An isolated bidirectional VK/XKB converter now preserves left/right modifiers, +main/keypad Enter, navigation/keypad distinctions, reader modifier keys and +F1–F24. Explicit portable/US profiles avoid guessing OEM punctuation on other +layouts. Generic Shift requires distinguishing scan information; unsupported +keys, profiles and ambiguous extended combinations return constant errors. +No X keycode or scan code is fabricated, and conversion grants no desktop +authority. See [the keyboard prerequisite and integration boundaries](remote/keyboard.md). + +Fixed wire-identity tests cover both transitions, all letter/digit/function +ranges, modifier variants, aliases, OEM profiles, unknown input and concurrent +maps. Codec/session tests verify press-state preservation and controller-only +keyboard direction. Focused keyboard race tests, the full Remote race suite and +Tentacles `make check` passed, including existing source-command/private-relay +consent and speech fixtures. No reader code changed; no full reader suite or +native/hardware checks were performed. Capture, injection, held-key/repeat and +layout-change ownership, AltGr, physical recovery, clipboard and Braille remain +pending. The source command still discards keyboard payloads. No installed or +active reader was replaced; no release gate is complete. + ## 1. Intended result and recommended approach Build a native Go Tentacle named **Remote**, with package ID `remote`, in this @@ -1006,6 +1027,10 @@ controller or `slave` for controlled. Present plain-language role labels in UI. keys, OEM punctuation, AltGr and keyboard-layout changes. Unknown keys produce bounded diagnostics instead of a guessed character. Never assume X keycodes equal Windows virtual-key or scan codes. + The [isolated converter](remote/keyboard.md) now covers explicit portable/US + subsets, sides and keypad distinctions. AltGr, other layouts, active-layout + resolution and holding the original mapping through key release remain open; + this checklist item is not complete. - [ ] Separate duplicate-down repeat from a fresh press; verify autorepeat behavior on injection. Handle lock-key state without surprising local toggles. Do not depend on temporarily rewriting global keymaps for ordinary input. diff --git a/remote/README.md b/remote/README.md index ed444d0..9c91639 100644 --- a/remote/README.md +++ b/remote/README.md @@ -34,6 +34,11 @@ Implemented: spelling behavior are preserved. See [speech.md](speech.md). - Bounded newline JSON framing, duplicate-key and collection validation, and typed speech, keyboard, membership, clipboard and Braille decoding. +- An isolated bidirectional VK/XKB keyboard mapper with explicit portable/US + profiles, modifier-side and keypad distinctions, and constant errors for + unsupported or ambiguous keys. Capture, injection, AltGr and layout-change + ownership remain pending; this is a library prerequisite, not keyboard + control. See [keyboard.md](keyboard.md). - TLS with normal certificate/hostname validation or an already-approved, endpoint-bound SHA-256 certificate pin. Authentication sends no channel key before verification. Channel keys can be generated with 256 bits of entropy. diff --git a/remote/implementation.md b/remote/implementation.md index e814368..39017b9 100644 --- a/remote/implementation.md +++ b/remote/implementation.md @@ -85,9 +85,13 @@ must never mint consent. original-lease tickets and delivery-time policy/generation checks. Browse-mode current-line reports, spelling and phonetic spelling now retain captured source ancestry and inspect joined fragments before export. Native - current-line output and flat/object review remain private pending source review. + current-line, object/flat review, Where Am I and reading-context output now + also have source checks; key-name echo and unreviewed producers stay private. 5. Complete keyboard recovery, explicit clipboard transfer and virtual/raw - Braille workflows. Record physical keyboard, audible speech and physical + Braille workflows. An isolated [VK/XKB conversion prerequisite](keyboard.md) + now preserves modifier sides and keypad distinctions with explicit layout + profiles. It does not supply capture/injection, AltGr, layout-change or + held-key ownership. Record physical keyboard, audible speech and physical display results separately from private automated checks. 6. Add the manifest, catalog and package tooling only after required reader APIs and acceptance gates exist. Installation and release publication remain diff --git a/remote/keyboard.md b/remote/keyboard.md new file mode 100644 index 0000000..7ac1d24 --- /dev/null +++ b/remote/keyboard.md @@ -0,0 +1,84 @@ +# 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. + +## Mapping contract + +Construct an immutable map with an explicit profile: + +- `portable`: lowercase Latin letter key names, top-row digits, F1–F24, + left/right Shift, Control, Alt and Super, Menu, Caps Lock, Num Lock, Scroll + Lock, Pause, Print, Backspace, Tab, Escape, Space, Return, and keypad and + navigation keys. `Prior`/`Next` and their keypad forms are accepted aliases. +- `us`: the same keys plus the eleven US OEM punctuation key identities. + This requires an explicit US-layout agreement; it is never an automatic + fallback. OEM meanings on other layouts remain unsupported. + +Input names must identify the unshifted key under the agreed layout, rather +than the resulting text. `a` names the letter key; `A`, `exclam`, `ISO_Left_Tab`, +dead keys and Unicode text are not normalized into guessed keys. A future Xorg +owner must resolve the active layout and modifier/lock state. `portable` means +a limited set of logical identities, not identical text on arbitrary layouts. + +Main Return and keypad Enter use different extended flags. Navigation keys, +Insert and Delete also retain their main/keypad distinction, including keypad +digits versus keypad navigation under Num Lock. Left/right modifier VKs are +emitted explicitly; right Shift is **not** an extended key. Scan codes are never +fabricated from X keycodes or keysyms. + +Incoming generic Control/Alt use the extended flag for side selection. Generic +Shift requires scan `0x2a` or `0x36`; absent or unknown scan information is +rejected. Side-specific VKs determine identity directly; other scan information +is range-checked but does not override the identity or become an X keycode. +Unsupported extended/VK combinations are rejected. The same conversion applies +to both press and release, and outgoing press state survives the JSON codec. + +AltGr (`ISO_Level3_Shift`), level-five modifiers, IME/dead keys, other OEM +profiles, OEM102, multimedia keys and unsupported VKs remain unavailable. +In particular, no implicit Control press is generated to emulate Windows AltGr. +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. + +## Integration boundaries + +This helper has no callers in the live coordinator. Before connecting it 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. + +Mapping a supported wire key does not establish that the destination's active +keymap contains it. Injection must resolve destination keycodes on its Xlib +owner, reject unavailable keys, and never rewrite the global keymap as a +fallback. A new mapper can describe a changed layout, but this library does +not detect changes, manage transitions or select a profile from relay data. + +## Evidence and references + +Fixed expectations cover both transitions for modifiers, reader modifier keys, +keypad/navigation, lock keys and all letter/digit/function ranges. Tests also +cover generic modifiers, explicit OEM profiles, aliases, invalid/ambiguous +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. + +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 +[keyboard input](https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input) +and [low-level events](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-kbdllhookstruct). +The existing reader XKB bindings and system X11 keysym definitions supplied the +symbol names. The local NVDA reference at +`2843e703ce19757711e976ebb6e9c5b689fc2017`, +[`input.sendKey`](https://github.com/nvaccess/nvda/blob/2843e703ce19757711e976ebb6e9c5b689fc2017/source/_remoteClient/input.py), +was inspected for wire-field use. No mapping table or implementation was copied +from NVDA or Orca. No actual NVDA frame capture, physical keyboard acceptance, +desktop recovery, audible speech or Braille acceptance is claimed. diff --git a/remote/protocol.md b/remote/protocol.md index 3d691cc..893b3f2 100644 --- a/remote/protocol.md +++ b/remote/protocol.md @@ -55,7 +55,7 @@ messages; they never include channel keys, remote output or server text. | `client_joined`, `client_left` | Typed `client` requires positive `id` and known `connection_type`. Optional `user_id` must agree. ID-only legacy membership is unsupported. | | `speak` | Up to 1,024 sequence entries. Strings preserve whitespace and order. Numeric priority 0, 1 or 2; absence defaults to 0. Null and string priorities fail. | | `cancel`, `pause_speech` | Typed cancel or required boolean `switch`. No output backend is called. | -| `key` | Required integer `vk_code` 1–255 and booleans `extended`, `pressed`. Optional integer `scan_code` 0–65,535. No VK/XKB mapping or injection yet. | +| `key` | Required integer `vk_code` 1–255 and booleans `extended`, `pressed`. Optional integer `scan_code` 0–65,535. An isolated [VK/XKB mapper](keyboard.md) covers an explicit subset; no keyboard capture, forwarding or injection yet. | | `set_braille_info` | Name at most 256 UTF-8 bytes; width 0–4,096. Absent fields permit the controlled peer's geometry request. Zero means no display. | | `set_display_size` | Required `sizes` array, at most 16 widths, each 0–4,096. | | `display` | Required array of at most 4,096 integer cells, each 0–255. Null cells and base64 strings fail. | diff --git a/remote/src/keyboard.go b/remote/src/keyboard.go new file mode 100644 index 0000000..88895a9 --- /dev/null +++ b/remote/src/keyboard.go @@ -0,0 +1,167 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package remote + +import ( + "errors" + "strconv" +) + +// These are protocol identities, never X keycodes or desktop authority. +type wireKey struct { + vk int + extended bool +} + +var errKeyboard = errors.New("unsupported remote keyboard mapping") + +// keyboardMap is immutable after construction. The caller must supply the +// unshifted, active-layout keysym name, not text generated by a keystroke. +// No profile is inferred from the environment or a relay message. +type keyboardMap struct { + encode map[string]wireKey + decode map[wireKey]string +} + +func newKeyboardMap(profile string) (*keyboardMap, error) { + if profile != "portable" && profile != "us" { + return nil, errKeyboard + } + m := &keyboardMap{encode: make(map[string]wireKey), decode: make(map[wireKey]string)} + add := func(vk int, extended bool, names ...string) { + key := wireKey{vk, extended} + m.decode[key] = names[0] + for _, name := range names { + m.encode[name] = key + } + } + add(0x08, false, "BackSpace") + add(0x09, false, "Tab") + add(0x0c, false, "KP_Begin") + add(0x0d, false, "Return") + add(0x0d, true, "KP_Enter") + add(0x13, false, "Pause") + add(0x14, false, "Caps_Lock") + add(0x1b, false, "Escape") + add(0x20, false, "space") + for _, entry := range []struct { + vk int + ordinary string + keypad string + }{ + {0x21, "Page_Up", "KP_Page_Up"}, + {0x22, "Page_Down", "KP_Page_Down"}, + {0x23, "End", "KP_End"}, + {0x24, "Home", "KP_Home"}, + {0x25, "Left", "KP_Left"}, + {0x26, "Up", "KP_Up"}, + {0x27, "Right", "KP_Right"}, + {0x28, "Down", "KP_Down"}, + {0x2d, "Insert", "KP_Insert"}, + {0x2e, "Delete", "KP_Delete"}, + } { + add(entry.vk, true, entry.ordinary) + add(entry.vk, false, entry.keypad) + } + // Standard X keysym aliases do not change the wire identity. + for alias, canonical := range map[string]string{ + "Prior": "Page_Up", "Next": "Page_Down", + "KP_Prior": "KP_Page_Up", "KP_Next": "KP_Page_Down", + } { + m.encode[alias] = m.encode[canonical] + } + add(0x2c, true, "Print") + for code := '0'; code <= '9'; code++ { + add(int(code), false, string(code)) + add(0x60+int(code-'0'), false, "KP_"+string(code)) + } + for code := 'a'; code <= 'z'; code++ { + add(0x41+int(code-'a'), false, string(code)) + } + add(0x5b, true, "Super_L") + add(0x5c, true, "Super_R") + add(0x5d, true, "Menu") + add(0x6a, false, "KP_Multiply") + add(0x6b, false, "KP_Add") + add(0x6c, false, "KP_Separator") + add(0x6d, false, "KP_Subtract") + add(0x6e, false, "KP_Decimal") + add(0x6f, true, "KP_Divide") + for number := 1; number <= 24; number++ { + add(0x6f+number, false, "F"+strconv.Itoa(number)) + } + add(0x90, true, "Num_Lock") + add(0x91, false, "Scroll_Lock") + add(0xa0, false, "Shift_L") + add(0xa1, false, "Shift_R") + add(0xa2, false, "Control_L") + add(0xa3, true, "Control_R") + add(0xa4, false, "Alt_L") + add(0xa5, true, "Alt_R") + if profile == "us" { + // OEM VK meanings depend on the Windows layout. This profile is an + // explicit US-layout contract, not a fallback for other layouts. + for _, entry := range []struct { + vk int + name string + }{ + {0xba, "semicolon"}, {0xbb, "equal"}, {0xbc, "comma"}, + {0xbd, "minus"}, {0xbe, "period"}, {0xbf, "slash"}, + {0xc0, "grave"}, {0xdb, "bracketleft"}, {0xdc, "backslash"}, + {0xdd, "bracketright"}, {0xde, "apostrophe"}, + } { + add(entry.vk, false, entry.name) + } + } + return m, nil +} + +func (m *keyboardMap) toWire(name string, pressed bool) (keyTransition, error) { + if m == nil { + return keyTransition{}, errKeyboard + } + key, ok := m.encode[name] + if !ok { + return keyTransition{}, errKeyboard + } + // No scan code is fabricated from an X keycode or keysym. + return keyTransition{VK: key.vk, Extended: key.extended, Pressed: pressed}, nil +} + +func (m *keyboardMap) fromWire(key keyTransition) (string, error) { + if m == nil || key.VK < 1 || key.VK > 255 || key.Scan != nil && (*key.Scan < 0 || *key.Scan > 65535) { + return "", errKeyboard + } + identity := wireKey{key.VK, key.Extended} + // Some peers use generic modifier VKs. Control/Alt sides are specified + // by extended; Shift requires an explicit PC scan code to disambiguate. + switch key.VK { + case 0x10: + if key.Extended || key.Scan == nil { + return "", errKeyboard + } + switch *key.Scan { + case 0x2a: + identity = wireKey{0xa0, false} + case 0x36: + identity = wireKey{0xa1, false} + default: + return "", errKeyboard + } + case 0x11: + identity.vk = 0xa2 + if key.Extended { + identity.vk = 0xa3 + } + case 0x12: + identity.vk = 0xa4 + if key.Extended { + identity.vk = 0xa5 + } + } + name, ok := m.decode[identity] + if !ok { + return "", errKeyboard + } + return name, nil +} diff --git a/remote/src/keyboard_test.go b/remote/src/keyboard_test.go new file mode 100644 index 0000000..aef8409 --- /dev/null +++ b/remote/src/keyboard_test.go @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package remote + +import ( + "errors" + "fmt" + "strings" + "sync" + "testing" +) + +func keyboardFixture(t *testing.T, profile string) *keyboardMap { + t.Helper() + m, err := newKeyboardMap(profile) + if err != nil { + t.Fatal(err) + } + return m +} + +func TestKeyboardWireIdentities(t *testing.T) { + m := keyboardFixture(t, "portable") + // Explicit expectations distinguish easily confused physical keys. These + // test both directions against wire values, rather than only round trips. + for _, test := range []struct { + name string + vk int + extended bool + }{ + {"Return", 0x0d, false}, {"KP_Enter", 0x0d, true}, + {"Insert", 0x2d, true}, {"KP_Insert", 0x2d, false}, + {"Delete", 0x2e, true}, {"KP_Delete", 0x2e, false}, + {"Home", 0x24, true}, {"KP_Home", 0x24, false}, + {"Left", 0x25, true}, {"KP_Left", 0x25, false}, + {"Up", 0x26, true}, {"KP_Up", 0x26, false}, + {"Right", 0x27, true}, {"KP_Right", 0x27, false}, + {"Down", 0x28, true}, {"KP_Down", 0x28, false}, + {"Page_Up", 0x21, true}, {"KP_Page_Up", 0x21, false}, + {"Page_Down", 0x22, true}, {"KP_Page_Down", 0x22, false}, + {"End", 0x23, true}, {"KP_End", 0x23, false}, + {"KP_Begin", 0x0c, false}, {"KP_0", 0x60, false}, + {"KP_9", 0x69, false}, {"KP_Decimal", 0x6e, false}, + {"KP_Multiply", 0x6a, false}, {"KP_Add", 0x6b, false}, + {"KP_Separator", 0x6c, false}, {"KP_Subtract", 0x6d, false}, + {"KP_Divide", 0x6f, true}, + {"Shift_L", 0xa0, false}, {"Shift_R", 0xa1, false}, + {"Control_L", 0xa2, false}, {"Control_R", 0xa3, true}, + {"Alt_L", 0xa4, false}, {"Alt_R", 0xa5, true}, + {"Super_L", 0x5b, true}, {"Super_R", 0x5c, true}, + {"Menu", 0x5d, true}, {"Caps_Lock", 0x14, false}, + {"Num_Lock", 0x90, true}, {"Scroll_Lock", 0x91, false}, + {"Print", 0x2c, true}, {"Pause", 0x13, false}, + {"BackSpace", 0x08, false}, {"Tab", 0x09, false}, + {"Escape", 0x1b, false}, {"space", 0x20, false}, + } { + t.Run(test.name, func(t *testing.T) { + for _, pressed := range []bool{true, false} { + key, err := m.toWire(test.name, pressed) + if err != nil || key.VK != test.vk || key.Extended != test.extended || key.Pressed != pressed || key.Scan != nil { + t.Fatalf("wire identity: %+v, %v", key, err) + } + name, err := m.fromWire(keyTransition{VK: test.vk, Extended: test.extended, Pressed: pressed}) + if err != nil || name != test.name { + t.Fatalf("XKB identity = %q, %v", name, err) + } + } + }) + } +} + +func TestKeyboardLettersDigitsAndFunctions(t *testing.T) { + m := keyboardFixture(t, "portable") + for _, group := range []struct { + first, last int + name func(int) string + }{ + {0x30, 0x39, func(vk int) string { return string(rune(vk)) }}, + {0x41, 0x5a, func(vk int) string { return string(rune(vk + 'a' - 'A')) }}, + {0x60, 0x69, func(vk int) string { return fmt.Sprintf("KP_%d", vk-0x60) }}, + {0x70, 0x87, func(vk int) string { return fmt.Sprintf("F%d", vk-0x6f) }}, + } { + for vk := group.first; vk <= group.last; vk++ { + name := group.name(vk) + key, err := m.toWire(name, true) + if err != nil || key.VK != vk || key.Extended { + t.Fatalf("%s: %+v, %v", name, key, err) + } + if got, err := m.fromWire(keyTransition{VK: vk}); err != nil || got != name { + t.Fatalf("VK %x: %q, %v", vk, got, err) + } + } + } +} + +func TestKeyboardGenericModifiers(t *testing.T) { + m := keyboardFixture(t, "portable") + left, right, unknown := 0x2a, 0x36, 0x01 + for _, test := range []struct { + key keyTransition + name string + }{ + {keyTransition{VK: 0x10, Scan: &left}, "Shift_L"}, + {keyTransition{VK: 0x10, Scan: &right}, "Shift_R"}, + {keyTransition{VK: 0x11}, "Control_L"}, + {keyTransition{VK: 0x11, Extended: true}, "Control_R"}, + {keyTransition{VK: 0x12}, "Alt_L"}, + {keyTransition{VK: 0x12, Extended: true}, "Alt_R"}, + {keyTransition{VK: 0x10}, ""}, + {keyTransition{VK: 0x10, Scan: &unknown}, ""}, + {keyTransition{VK: 0x10, Scan: &left, Extended: true}, ""}, + } { + name, err := m.fromWire(test.key) + if name != test.name || (err != nil) != (test.name == "") { + t.Fatalf("%+v mapped to %q, %v", test.key, name, err) + } + } +} + +func TestKeyboardExplicitLayoutAndAliases(t *testing.T) { + portable, us := keyboardFixture(t, "portable"), keyboardFixture(t, "us") + for name, vk := range map[string]int{ + "semicolon": 0xba, "equal": 0xbb, "comma": 0xbc, "minus": 0xbd, + "period": 0xbe, "slash": 0xbf, "grave": 0xc0, "bracketleft": 0xdb, + "backslash": 0xdc, "bracketright": 0xdd, "apostrophe": 0xde, + } { + if _, err := portable.toWire(name, true); !errors.Is(err, errKeyboard) { + t.Fatal("portable profile guessed an OEM mapping") + } + if _, err := portable.fromWire(keyTransition{VK: vk}); !errors.Is(err, errKeyboard) { + t.Fatal("portable profile guessed an incoming OEM mapping") + } + key, err := us.toWire(name, false) + if err != nil || key.VK != vk || key.Extended || key.Pressed { + t.Fatalf("US mapping: %+v, %v", key, err) + } + if got, err := us.fromWire(keyTransition{VK: vk}); err != nil || got != name { + t.Fatalf("US VK %x: %q, %v", vk, got, err) + } + } + for alias, canonical := range map[string]string{ + "Prior": "Page_Up", "Next": "Page_Down", "KP_Prior": "KP_Page_Up", "KP_Next": "KP_Page_Down", + } { + key, err := portable.toWire(alias, true) + if name, decodeErr := portable.fromWire(key); err != nil || decodeErr != nil || name != canonical { + t.Fatalf("alias %s lost canonical identity", alias) + } + } +} + +func TestKeyboardRejectsUnknownAndAmbiguousInput(t *testing.T) { + for _, profile := range []string{"", "auto", "US", "de", "us\n"} { + if _, err := newKeyboardMap(profile); !errors.Is(err, errKeyboard) { + t.Fatal("accepted unsupported profile") + } + } + for _, m := range []*keyboardMap{nil, keyboardFixture(t, "portable"), keyboardFixture(t, "us")} { + for _, name := range []string{"", "38", "A", "exclam", "ISO_Left_Tab", "ISO_Level3_Shift", "ISO_Level5_Shift", "dead_acute", "é", "U20AC", "F25", "KP_F1", "XF86AudioMute", strings.Repeat("private", 100)} { + if key, err := m.toWire(name, true); !errors.Is(err, errKeyboard) || key != (keyTransition{}) { + t.Fatal("unknown input did not fail without a partial key") + } + } + for _, key := range []keyTransition{ + {VK: 0}, {VK: -1}, {VK: 256}, {VK: 0xff}, {VK: 0xe2}, + {VK: 0x41, Extended: true}, {VK: 0xa0, Extended: true}, + {VK: 0xa3}, {VK: 0xa5}, {VK: 0xa2, Extended: true}, + } { + if name, err := m.fromWire(key); !errors.Is(err, errKeyboard) || name != "" { + t.Fatal("ambiguous input produced a key") + } + } + } + m := keyboardFixture(t, "portable") + for _, scan := range []int{-1, 65536} { + if _, err := m.fromWire(keyTransition{VK: 0x41, Scan: &scan}); !errors.Is(err, errKeyboard) { + t.Fatal("accepted invalid scan code") + } + } +} + +func TestKeyboardWireCodecAndSessionDirection(t *testing.T) { + m := keyboardFixture(t, "us") + s, err := newSession(controlled, 7) + if err != nil { + t.Fatal(err) + } + if _, err := s.accept(7, message{kind: "channel_joined", payload: membership{peers: []peer{{ID: 3, Role: controller}}}}); err != nil { + t.Fatal(err) + } + for _, name := range []string{"Shift_R", "Tab", "Insert", "KP_Insert", "Caps_Lock", "KP_Enter", "backslash"} { + for _, pressed := range []bool{true, false} { + key, _ := m.toWire(name, pressed) + frame, err := encodeMessage("key", key) + if err != nil { + t.Fatal(err) + } + decoded, err := decodeMessage(frame) + if err != nil { + t.Fatal(err) + } + decoded.origin = 3 // Assigned by a relay, never by the encoder. + if effect, err := s.accept(7, decoded); err != nil || !effect { + t.Fatal("valid controller key rejected", err) + } + got := decoded.payload.(keyTransition) + if canonical, err := m.fromWire(got); err != nil || canonical != name || got.Pressed != pressed || got.Scan != nil { + t.Fatal("codec changed key identity or transition") + } + } + } + if !errors.Is(s.allowSend(7, "key"), errProtocol) { + t.Fatal("controlled peer could send keyboard input") + } +} + +func TestKeyboardMapsAreIndependentAndConcurrent(t *testing.T) { + portable, us := keyboardFixture(t, "portable"), keyboardFixture(t, "us") + var workers sync.WaitGroup + for range 8 { + workers.Add(1) + go func() { + defer workers.Done() + for range 100 { + if _, err := us.toWire("semicolon", true); err != nil { + t.Error(err) + } + if _, err := portable.fromWire(keyTransition{VK: 0xba}); !errors.Is(err, errKeyboard) { + t.Error("US profile leaked into portable profile") + } + } + }() + } + workers.Wait() +}