Skip to main content

sg_abi/
input.rs

1//! Input event ABI shared by host and guest.
2//!
3//! Keys are numbered by position in [`KEY_NAMES`] (1-based; 0 = unknown). The names
4//! are DOM `KeyboardEvent.code` values, so web game code ports unchanged.
5
6pub const KEY_NAMES: &[&str] = &[
7    "Backquote",
8    "Backslash",
9    "BracketLeft",
10    "BracketRight",
11    "Comma",
12    "Digit0",
13    "Digit1",
14    "Digit2",
15    "Digit3",
16    "Digit4",
17    "Digit5",
18    "Digit6",
19    "Digit7",
20    "Digit8",
21    "Digit9",
22    "Equal",
23    "IntlBackslash",
24    "IntlRo",
25    "IntlYen",
26    "KeyA",
27    "KeyB",
28    "KeyC",
29    "KeyD",
30    "KeyE",
31    "KeyF",
32    "KeyG",
33    "KeyH",
34    "KeyI",
35    "KeyJ",
36    "KeyK",
37    "KeyL",
38    "KeyM",
39    "KeyN",
40    "KeyO",
41    "KeyP",
42    "KeyQ",
43    "KeyR",
44    "KeyS",
45    "KeyT",
46    "KeyU",
47    "KeyV",
48    "KeyW",
49    "KeyX",
50    "KeyY",
51    "KeyZ",
52    "Minus",
53    "Period",
54    "Quote",
55    "Semicolon",
56    "Slash",
57    "AltLeft",
58    "AltRight",
59    "Backspace",
60    "CapsLock",
61    "ContextMenu",
62    "ControlLeft",
63    "ControlRight",
64    "Enter",
65    "MetaLeft",
66    "MetaRight",
67    "ShiftLeft",
68    "ShiftRight",
69    "Space",
70    "Tab",
71    "Convert",
72    "KanaMode",
73    "Lang1",
74    "Lang2",
75    "Lang3",
76    "Lang4",
77    "Lang5",
78    "NonConvert",
79    "Delete",
80    "End",
81    "Help",
82    "Home",
83    "Insert",
84    "PageDown",
85    "PageUp",
86    "ArrowDown",
87    "ArrowLeft",
88    "ArrowRight",
89    "ArrowUp",
90    "NumLock",
91    "Numpad0",
92    "Numpad1",
93    "Numpad2",
94    "Numpad3",
95    "Numpad4",
96    "Numpad5",
97    "Numpad6",
98    "Numpad7",
99    "Numpad8",
100    "Numpad9",
101    "NumpadAdd",
102    "NumpadBackspace",
103    "NumpadClear",
104    "NumpadClearEntry",
105    "NumpadComma",
106    "NumpadDecimal",
107    "NumpadDivide",
108    "NumpadEnter",
109    "NumpadEqual",
110    "NumpadHash",
111    "NumpadMemoryAdd",
112    "NumpadMemoryClear",
113    "NumpadMemoryRecall",
114    "NumpadMemoryStore",
115    "NumpadMemorySubtract",
116    "NumpadMultiply",
117    "NumpadParenLeft",
118    "NumpadParenRight",
119    "NumpadStar",
120    "NumpadSubtract",
121    "Escape",
122    "Fn",
123    "FnLock",
124    "PrintScreen",
125    "ScrollLock",
126    "Pause",
127    "BrowserBack",
128    "BrowserFavorites",
129    "BrowserForward",
130    "BrowserHome",
131    "BrowserRefresh",
132    "BrowserSearch",
133    "BrowserStop",
134    "Eject",
135    "LaunchApp1",
136    "LaunchApp2",
137    "LaunchMail",
138    "MediaPlayPause",
139    "MediaSelect",
140    "MediaStop",
141    "MediaTrackNext",
142    "MediaTrackPrevious",
143    "Power",
144    "Sleep",
145    "AudioVolumeDown",
146    "AudioVolumeMute",
147    "AudioVolumeUp",
148    "WakeUp",
149    "Meta",
150    "Hyper",
151    "Turbo",
152    "Abort",
153    "Resume",
154    "Suspend",
155    "Again",
156    "Copy",
157    "Cut",
158    "Find",
159    "Open",
160    "Paste",
161    "Props",
162    "Select",
163    "Undo",
164    "Hiragana",
165    "Katakana",
166    "F1",
167    "F2",
168    "F3",
169    "F4",
170    "F5",
171    "F6",
172    "F7",
173    "F8",
174    "F9",
175    "F10",
176    "F11",
177    "F12",
178    "F13",
179    "F14",
180    "F15",
181    "F16",
182    "F17",
183    "F18",
184    "F19",
185    "F20",
186    "F21",
187    "F22",
188    "F23",
189    "F24",
190    "F25",
191    "F26",
192    "F27",
193    "F28",
194    "F29",
195    "F30",
196    "F31",
197    "F32",
198    "F33",
199    "F34",
200    "F35",
201];
202
203/// Returns the key number for a DOM code name.
204pub fn key_code(name: &str) -> u32 {
205    KEY_NAMES.iter().position(|n| *n == name).map_or(0, |i| i as u32 + 1)
206}
207
208/// Returns the DOM code name for a key number ("" if unknown).
209pub fn key_name(code: u32) -> &'static str {
210    (code as usize).checked_sub(1).and_then(|i| KEY_NAMES.get(i)).copied().unwrap_or("")
211}
212
213/// One input event, as written by `sg.input_poll` (24 bytes, little endian).
214#[repr(C)]
215#[derive(Clone, Copy, Debug, Default, PartialEq)]
216pub struct RawEvent {
217    pub kind: u32,
218    /// key number, mouse button, text char, or 0/1 for focus and pointer lock
219    pub code: u32,
220    pub x: f32,
221    pub y: f32,
222    /// modifier bits (MOD_*)
223    pub mods: u32,
224    /// FLAG_REPEAT for auto-repeated keys
225    pub flags: u32,
226}
227
228pub const EVENT_SIZE: usize = 24;
229
230pub mod kind {
231    pub const KEY_DOWN: u32 = 1;
232    pub const KEY_UP: u32 = 2;
233    /// `code` is a Unicode scalar value typed by the user.
234    pub const TEXT: u32 = 3;
235    /// Raw relative motion in (x, y); delivered whether or not the pointer is locked.
236    pub const MOUSE_MOVE: u32 = 4;
237    /// Cursor position in physical pixels (x, y).
238    pub const CURSOR: u32 = 5;
239    /// `code`: 0 left, 1 middle, 2 right, 3 back, 4 forward (DOM `button` numbering).
240    pub const BUTTON_DOWN: u32 = 6;
241    pub const BUTTON_UP: u32 = 7;
242    /// Scroll in lines (x, y); positive y = away from the user (DOM deltaY sign inverted).
243    pub const WHEEL: u32 = 8;
244    pub const FOCUS: u32 = 9;
245    /// New surface size (x, y) in pixels.
246    pub const RESIZE: u32 = 10;
247    pub const POINTER_LOCK: u32 = 11;
248}
249
250pub const MOD_SHIFT: u32 = 1;
251pub const MOD_CTRL: u32 = 2;
252pub const MOD_ALT: u32 = 4;
253pub const MOD_META: u32 = 8;
254pub const FLAG_REPEAT: u32 = 1;
255
256impl RawEvent {
257    pub fn to_bytes(&self) -> [u8; EVENT_SIZE] {
258        let mut b = [0u8; EVENT_SIZE];
259        b[0..4].copy_from_slice(&self.kind.to_le_bytes());
260        b[4..8].copy_from_slice(&self.code.to_le_bytes());
261        b[8..12].copy_from_slice(&self.x.to_le_bytes());
262        b[12..16].copy_from_slice(&self.y.to_le_bytes());
263        b[16..20].copy_from_slice(&self.mods.to_le_bytes());
264        b[20..24].copy_from_slice(&self.flags.to_le_bytes());
265        b
266    }
267
268    pub fn from_bytes(b: &[u8]) -> Self {
269        let w = |i: usize| u32::from_le_bytes(b[i..i + 4].try_into().unwrap());
270        RawEvent { kind: w(0), code: w(4), x: f32::from_bits(w(8)), y: f32::from_bits(w(12)), mods: w(16), flags: w(20) }
271    }
272}