1use crate::geometry::Point;
5
6#[derive(Clone, Copy, Debug, PartialEq)]
9pub enum SurfaceInput {
10 Key {
13 code: KeyCode,
14 native: u32,
18 pressed: bool,
19 repeat: bool,
20 modifiers: Modifiers,
21 },
22 PointerMoved { position: Point, modifiers: Modifiers },
26 PointerLeft,
28 Button { button: MouseButton, pressed: bool, position: Point, modifiers: Modifiers },
31 Scroll { delta: ScrollDelta, modifiers: Modifiers },
34 Motion { dx: f32, dy: f32 },
37 RawMotion { dx: f32, dy: f32 },
42}
43
44#[derive(Clone, Copy, Debug, PartialEq)]
46pub enum ScrollDelta {
47 Lines { x: f32, y: f32 },
49 Points { x: f32, y: f32 },
51}
52
53#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
56pub enum MouseButton {
57 Primary,
58 Secondary,
59 Middle,
60 Back,
61 Forward,
62 Other(u16),
64}
65
66#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
69pub struct Modifiers {
70 pub shift: bool,
71 pub control: bool,
72 pub alt: bool,
73 pub meta: bool,
74}
75
76macro_rules! key_codes {
77 ($($name:ident),* $(,)?) => {
78 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
82 pub enum KeyCode {
83 $($name,)*
84 Unidentified,
86 }
87
88 impl KeyCode {
89 pub fn name(self) -> &'static str {
91 match self {
92 $(KeyCode::$name => stringify!($name),)*
93 KeyCode::Unidentified => "Unidentified",
94 }
95 }
96 }
97 };
98}
99
100key_codes! {
101 KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR,
102 KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ, Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7,
103 Digit8, Digit9, Backquote, Minus, Equal, BracketLeft, BracketRight, Backslash, Semicolon, Quote, Comma, Period,
104 Slash, IntlBackslash, IntlRo, IntlYen, Escape, Tab, CapsLock, Backspace, Enter, Space, ShiftLeft, ShiftRight,
105 ControlLeft, ControlRight, AltLeft, AltRight, MetaLeft, MetaRight, ContextMenu, Insert, Delete, Home, End,
106 PageUp, PageDown, ArrowUp, ArrowDown, ArrowLeft, ArrowRight, PrintScreen, ScrollLock, Pause, NumLock, Numpad0,
107 Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, NumpadDivide, NumpadMultiply,
108 NumpadSubtract, NumpadAdd, NumpadEnter, NumpadDecimal, NumpadEqual, NumpadComma, KanaMode, Lang1, Lang2,
109 Convert, NonConvert, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20,
110 F21, F22, F23, F24,
111}
112
113use KeyCode::*;
114
115#[rustfmt::skip]
118const EVDEV: [KeyCode; 127] = [
119 Escape, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7, Digit8, Digit9, Digit0, Minus, Equal,
120 Backspace, Tab, KeyQ, KeyW, KeyE, KeyR, KeyT, KeyY, KeyU, KeyI, KeyO, KeyP, BracketLeft, BracketRight, Enter,
121 ControlLeft, KeyA, KeyS, KeyD, KeyF, KeyG, KeyH, KeyJ, KeyK, KeyL, Semicolon, Quote, Backquote, ShiftLeft,
122 Backslash, KeyZ, KeyX, KeyC, KeyV, KeyB, KeyN, KeyM, Comma, Period, Slash, ShiftRight, NumpadMultiply, AltLeft,
123 Space, CapsLock, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, NumLock, ScrollLock, Numpad7, Numpad8, Numpad9,
124 NumpadSubtract, Numpad4, Numpad5, Numpad6, NumpadAdd, Numpad1, Numpad2, Numpad3, Numpad0, NumpadDecimal,
125 Unidentified, Unidentified, IntlBackslash, F11, F12, IntlRo,
127 Unidentified, Unidentified, Convert, KanaMode, NonConvert, NumpadComma, NumpadEnter, ControlRight,
130 NumpadDivide, PrintScreen, AltRight,
131 Unidentified, Home, ArrowUp, PageUp, ArrowLeft, ArrowRight, End, ArrowDown, PageDown, Insert, Delete,
133 Unidentified, Unidentified, Unidentified, Unidentified, Unidentified, NumpadEqual,
135 Unidentified, Pause,
137 Unidentified, NumpadComma, Lang1, Lang2, IntlYen, MetaLeft, MetaRight, ContextMenu,
139];
140
141#[rustfmt::skip]
143const MAC: [KeyCode; 127] = [
144 KeyA, KeyS, KeyD, KeyF, KeyH, KeyG, KeyZ, KeyX, KeyC, KeyV, IntlBackslash, KeyB, KeyQ, KeyW, KeyE, KeyR, KeyY,
145 KeyT, Digit1, Digit2, Digit3, Digit4, Digit6, Digit5, Equal, Digit9, Digit7, Minus, Digit8, Digit0,
146 BracketRight, KeyO, KeyU, BracketLeft, KeyI, KeyP, Enter, KeyL, KeyJ, Quote, KeyK, Semicolon, Backslash, Comma,
147 Slash, KeyN, KeyM, Period, Tab, Space, Backquote, Backspace,
148 Unidentified, Escape, MetaRight, MetaLeft, ShiftLeft, CapsLock, AltLeft, ControlLeft, ShiftRight, AltRight,
150 ControlRight,
151 Unidentified, F17, NumpadDecimal, Unidentified, NumpadMultiply, Unidentified, NumpadAdd, Unidentified,
153 NumLock,
155 Unidentified, Unidentified, Unidentified, NumpadDivide, NumpadEnter, Unidentified, NumpadSubtract, F18, F19,
157 NumpadEqual, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, F20, Numpad8, Numpad9,
158 IntlYen, IntlRo, NumpadComma, F5, F6, F7, F3, F8, F9, Lang2, F11, Lang1, F13, F16, F14, Unidentified, F10,
159 ContextMenu, F12, Unidentified, F15,
160 Insert, Home, PageUp, Delete, F4, End, F2, PageDown, F1, ArrowLeft, ArrowRight, ArrowDown, ArrowUp,
162];
163
164impl KeyCode {
165 pub fn from_evdev(code: u32) -> KeyCode {
168 match code {
169 1..=127 => EVDEV[code as usize - 1],
170 183..=194 => [F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24][code as usize - 183],
171 _ => Unidentified,
172 }
173 }
174
175 pub fn from_keysym(keysym: u32) -> Option<KeyCode> {
182 Some(match keysym {
183 0xff1b => Escape,
184 0xff09 | 0xfe20 => Tab,
185 0xff0d => Enter,
186 0xff08 => Backspace,
187 0xffff => Delete,
188 0xff63 => Insert,
189 0xff50 => Home,
190 0xff57 => End,
191 0xff55 => PageUp,
192 0xff56 => PageDown,
193 0xff51 => ArrowLeft,
194 0xff52 => ArrowUp,
195 0xff53 => ArrowRight,
196 0xff54 => ArrowDown,
197 0xff61 => PrintScreen,
198 0xff67 => ContextMenu,
199 0xff13 => Pause,
200 0xff14 => ScrollLock,
201 0xff7f => NumLock,
202 0xffe5 => CapsLock,
203 0xffe1 => ShiftLeft,
204 0xffe2 => ShiftRight,
205 0xffe3 => ControlLeft,
206 0xffe4 => ControlRight,
207 0xffe9 => AltLeft,
208 0xffea | 0xfe03 => AltRight,
210 0xffeb => MetaLeft,
211 0xffec => MetaRight,
212 0xffbe..=0xffd5 => [
213 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
214 F23, F24,
215 ][keysym as usize - 0xffbe],
216 _ => return None,
217 })
218 }
219
220 pub fn from_mac(code: u16) -> KeyCode {
222 MAC.get(code as usize).copied().unwrap_or(Unidentified)
223 }
224
225 pub fn from_windows_scancode(code: u32) -> KeyCode {
228 match code {
229 0x01..=0x53 if code != 0x45 => EVDEV[code as usize - 1],
231 0x45 => Pause,
233 0x56 => IntlBackslash,
234 0x57 => F11,
235 0x58 => F12,
236 0x59 => NumpadEqual,
237 0x64..=0x6E => [F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23][code as usize - 0x64],
238 0x70 => KanaMode,
239 0x73 => IntlRo,
240 0x76 => F24,
241 0x79 => Convert,
242 0x7B => NonConvert,
243 0x7D => IntlYen,
244 0x7E => NumpadComma,
245 0x54 => PrintScreen,
246 0xE01C => NumpadEnter,
247 0xE01D => ControlRight,
248 0xE035 => NumpadDivide,
249 0xE037 => PrintScreen,
250 0xE038 => AltRight,
251 0xE045 => NumLock,
252 0xE047 => Home,
253 0xE048 => ArrowUp,
254 0xE049 => PageUp,
255 0xE04B => ArrowLeft,
256 0xE04D => ArrowRight,
257 0xE04F => End,
258 0xE050 => ArrowDown,
259 0xE051 => PageDown,
260 0xE052 => Insert,
261 0xE053 => Delete,
262 0xE05B => MetaLeft,
263 0xE05C => MetaRight,
264 0xE05D => ContextMenu,
265 0x72 => Lang1,
266 0x71 => Lang2,
267 _ => Unidentified,
268 }
269 }
270
271 pub fn from_us_char(c: char) -> KeyCode {
274 const LETTERS: [KeyCode; 26] = [
275 KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR,
276 KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ,
277 ];
278 const DIGITS: [KeyCode; 10] = [Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7, Digit8, Digit9];
279 match c.to_ascii_lowercase() {
280 c @ 'a'..='z' => LETTERS[c as usize - 'a' as usize],
281 c @ '0'..='9' => DIGITS[c as usize - '0' as usize],
282 ' ' => Space,
283 '`' => Backquote,
284 '-' => Minus,
285 '=' => Equal,
286 '[' => BracketLeft,
287 ']' => BracketRight,
288 '\\' => Backslash,
289 ';' => Semicolon,
290 '\'' => Quote,
291 ',' => Comma,
292 '.' => Period,
293 '/' => Slash,
294 _ => Unidentified,
295 }
296 }
297
298 pub fn from_key(key: crate::backend::Key) -> KeyCode {
300 use crate::backend::Key;
301 const F: [KeyCode; 24] = [
302 F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23,
303 F24,
304 ];
305 match key {
306 Key::Char(c) => KeyCode::from_us_char(c),
307 Key::Enter => Enter,
308 Key::Escape => Escape,
309 Key::Tab => Tab,
310 Key::Backspace => Backspace,
311 Key::Delete => Delete,
312 Key::Up => ArrowUp,
313 Key::Down => ArrowDown,
314 Key::Left => ArrowLeft,
315 Key::Right => ArrowRight,
316 Key::Home => Home,
317 Key::End => End,
318 Key::PageUp => PageUp,
319 Key::PageDown => PageDown,
320 Key::F(n) => F.get(usize::from(n).wrapping_sub(1)).copied().unwrap_or(Unidentified),
321 }
322 }
323}