Skip to main content

agentmux_srv/server/streamed_browser/screen/
keymap.rs

1// Copyright 2026, AgentMux Corp.
2// SPDX-License-Identifier: Apache-2.0
3
4//! Keys as the X server numbers them. The pane sends a key's DOM `code` (the physical key: `KeyA`, `ArrowLeft`); the X server under
5//! Xvfb numbers keys as Linux does plus 8. Layout is US: a key is the key at that place, not the letter a layout would give it.
6
7/// DOM key codes and the Linux scancodes of those keys.
8const SCANCODES: &[(&str, u8)] = &[
9    ("Escape", 1),
10    ("Digit1", 2),
11    ("Digit2", 3),
12    ("Digit3", 4),
13    ("Digit4", 5),
14    ("Digit5", 6),
15    ("Digit6", 7),
16    ("Digit7", 8),
17    ("Digit8", 9),
18    ("Digit9", 10),
19    ("Digit0", 11),
20    ("Minus", 12),
21    ("Equal", 13),
22    ("Backspace", 14),
23    ("Tab", 15),
24    ("KeyQ", 16),
25    ("KeyW", 17),
26    ("KeyE", 18),
27    ("KeyR", 19),
28    ("KeyT", 20),
29    ("KeyY", 21),
30    ("KeyU", 22),
31    ("KeyI", 23),
32    ("KeyO", 24),
33    ("KeyP", 25),
34    ("BracketLeft", 26),
35    ("BracketRight", 27),
36    ("Enter", 28),
37    ("ControlLeft", 29),
38    ("KeyA", 30),
39    ("KeyS", 31),
40    ("KeyD", 32),
41    ("KeyF", 33),
42    ("KeyG", 34),
43    ("KeyH", 35),
44    ("KeyJ", 36),
45    ("KeyK", 37),
46    ("KeyL", 38),
47    ("Semicolon", 39),
48    ("Quote", 40),
49    ("Backquote", 41),
50    ("ShiftLeft", 42),
51    ("Backslash", 43),
52    ("KeyZ", 44),
53    ("KeyX", 45),
54    ("KeyC", 46),
55    ("KeyV", 47),
56    ("KeyB", 48),
57    ("KeyN", 49),
58    ("KeyM", 50),
59    ("Comma", 51),
60    ("Period", 52),
61    ("Slash", 53),
62    ("ShiftRight", 54),
63    ("NumpadMultiply", 55),
64    ("AltLeft", 56),
65    ("Space", 57),
66    ("CapsLock", 58),
67    ("F1", 59),
68    ("F2", 60),
69    ("F3", 61),
70    ("F4", 62),
71    ("F5", 63),
72    ("F6", 64),
73    ("F7", 65),
74    ("F8", 66),
75    ("F9", 67),
76    ("F10", 68),
77    ("Numpad7", 71),
78    ("Numpad8", 72),
79    ("Numpad9", 73),
80    ("NumpadSubtract", 74),
81    ("Numpad4", 75),
82    ("Numpad5", 76),
83    ("Numpad6", 77),
84    ("NumpadAdd", 78),
85    ("Numpad1", 79),
86    ("Numpad2", 80),
87    ("Numpad3", 81),
88    ("Numpad0", 82),
89    ("NumpadDecimal", 83),
90    ("F11", 87),
91    ("F12", 88),
92    ("NumpadEnter", 96),
93    ("ControlRight", 97),
94    ("NumpadDivide", 98),
95    ("AltRight", 100),
96    ("Home", 102),
97    ("ArrowUp", 103),
98    ("PageUp", 104),
99    ("ArrowLeft", 105),
100    ("ArrowRight", 106),
101    ("End", 107),
102    ("ArrowDown", 108),
103    ("PageDown", 109),
104    ("Insert", 110),
105    ("Delete", 111),
106    ("MetaLeft", 125),
107    ("MetaRight", 126),
108    ("ContextMenu", 127),
109];
110
111/// The X keycode of a DOM key code, or none for a key this prototype does not know.
112pub fn keycode(code: &str) -> Option<u8> {
113    SCANCODES.iter().find(|(name, _)| *name == code).map(|(_, evdev)| evdev + 8)
114}
115
116/// The keycode of Left Shift, and of Left Control and Left Alt, for the keys srv presses itself (an address typed in, a paste).
117pub const SHIFT: u8 = 42 + 8;
118pub const CONTROL: u8 = 29 + 8;
119pub const ALT: u8 = 56 + 8;
120
121/// The key (and whether Shift is held) that types `c` on a US keyboard, for the characters of an address or a short paste.
122pub fn ascii(c: char) -> Option<(u8, bool)> {
123    let dom = |code: &str, shift: bool| keycode(code).map(|k| (k, shift));
124    match c {
125        'a'..='z' => dom(&format!("Key{}", c.to_ascii_uppercase()), false),
126        'A'..='Z' => dom(&format!("Key{c}"), true),
127        '0'..='9' => dom(&format!("Digit{c}"), false),
128        ' ' => dom("Space", false),
129        '\n' | '\r' => dom("Enter", false),
130        '\t' => dom("Tab", false),
131        '-' => dom("Minus", false),
132        '_' => dom("Minus", true),
133        '=' => dom("Equal", false),
134        '+' => dom("Equal", true),
135        '[' => dom("BracketLeft", false),
136        '{' => dom("BracketLeft", true),
137        ']' => dom("BracketRight", false),
138        '}' => dom("BracketRight", true),
139        ';' => dom("Semicolon", false),
140        ':' => dom("Semicolon", true),
141        '\'' => dom("Quote", false),
142        '"' => dom("Quote", true),
143        '`' => dom("Backquote", false),
144        '~' => dom("Backquote", true),
145        '\\' => dom("Backslash", false),
146        '|' => dom("Backslash", true),
147        ',' => dom("Comma", false),
148        '<' => dom("Comma", true),
149        '.' => dom("Period", false),
150        '>' => dom("Period", true),
151        '/' => dom("Slash", false),
152        '?' => dom("Slash", true),
153        '!' => dom("Digit1", true),
154        '@' => dom("Digit2", true),
155        '#' => dom("Digit3", true),
156        '$' => dom("Digit4", true),
157        '%' => dom("Digit5", true),
158        '^' => dom("Digit6", true),
159        '&' => dom("Digit7", true),
160        '*' => dom("Digit8", true),
161        '(' => dom("Digit9", true),
162        ')' => dom("Digit0", true),
163        _ => None,
164    }
165}
166
167#[cfg(test)]
168mod tests {
169    use super::*;
170
171    #[test]
172    fn a_dom_key_code_is_the_linux_scancode_plus_eight() {
173        assert_eq!(keycode("KeyA"), Some(38));
174        assert_eq!(keycode("Enter"), Some(36));
175        assert_eq!(keycode("ArrowLeft"), Some(113));
176        assert_eq!(keycode("ShiftLeft"), Some(SHIFT));
177        assert_eq!(keycode("ControlLeft"), Some(CONTROL));
178        assert_eq!(keycode("AltLeft"), Some(ALT));
179        assert_eq!(keycode("NoSuchKey"), None);
180        assert_eq!(keycode(""), None);
181    }
182
183    #[test]
184    fn no_two_keys_share_a_keycode() {
185        let codes = [
186            "Escape", "Digit1", "Digit0", "Minus", "Equal", "Backspace", "Tab", "KeyQ", "KeyP", "BracketLeft", "BracketRight", "Enter", "ControlLeft", "KeyA", "KeyL",
187            "Semicolon", "Quote", "Backquote", "ShiftLeft", "Backslash", "KeyZ", "KeyM", "Comma", "Period", "Slash", "ShiftRight", "AltLeft", "Space", "F1", "F10", "F11", "F12",
188            "Home", "ArrowUp", "PageUp", "ArrowLeft", "ArrowRight", "End", "ArrowDown", "PageDown", "Insert", "Delete", "ControlRight", "AltRight",
189        ];
190        let mut seen = std::collections::HashSet::new();
191        for c in codes {
192            let k = keycode(c).unwrap_or_else(|| panic!("{c} has no keycode"));
193            assert!(seen.insert(k), "{c} repeats keycode {k}");
194        }
195    }
196
197    #[test]
198    fn an_address_can_be_typed_on_a_us_keyboard() {
199        for c in "https://example.com/a_b-c?d=e&f=1#x%20y:8080~+@!$*(),;'".chars() {
200            assert!(ascii(c).is_some(), "{c:?} has no key");
201        }
202        assert_eq!(ascii('a'), Some((38, false)));
203        assert_eq!(ascii('A'), Some((38, true)));
204        assert_eq!(ascii(':'), Some((keycode("Semicolon").unwrap(), true)));
205        assert_eq!(ascii('\u{e9}'), None);
206    }
207}