use crate::geometry::Point;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SurfaceInput {
Key {
code: KeyCode,
native: u32,
pressed: bool,
repeat: bool,
modifiers: Modifiers,
},
PointerMoved { position: Point, modifiers: Modifiers },
PointerLeft,
Button { button: MouseButton, pressed: bool, position: Point, modifiers: Modifiers },
Scroll { delta: ScrollDelta, modifiers: Modifiers },
Motion { dx: f32, dy: f32 },
RawMotion { dx: f32, dy: f32 },
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ScrollDelta {
Lines { x: f32, y: f32 },
Points { x: f32, y: f32 },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum MouseButton {
Primary,
Secondary,
Middle,
Back,
Forward,
Other(u16),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Modifiers {
pub shift: bool,
pub control: bool,
pub alt: bool,
pub meta: bool,
}
macro_rules! key_codes {
($($name:ident),* $(,)?) => {
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum KeyCode {
$($name,)*
Unidentified,
}
impl KeyCode {
pub fn name(self) -> &'static str {
match self {
$(KeyCode::$name => stringify!($name),)*
KeyCode::Unidentified => "Unidentified",
}
}
}
};
}
key_codes! {
KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR,
KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ, Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7,
Digit8, Digit9, Backquote, Minus, Equal, BracketLeft, BracketRight, Backslash, Semicolon, Quote, Comma, Period,
Slash, IntlBackslash, IntlRo, IntlYen, Escape, Tab, CapsLock, Backspace, Enter, Space, ShiftLeft, ShiftRight,
ControlLeft, ControlRight, AltLeft, AltRight, MetaLeft, MetaRight, ContextMenu, Insert, Delete, Home, End,
PageUp, PageDown, ArrowUp, ArrowDown, ArrowLeft, ArrowRight, PrintScreen, ScrollLock, Pause, NumLock, Numpad0,
Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, Numpad8, Numpad9, NumpadDivide, NumpadMultiply,
NumpadSubtract, NumpadAdd, NumpadEnter, NumpadDecimal, NumpadEqual, NumpadComma, KanaMode, Lang1, Lang2,
Convert, NonConvert, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20,
F21, F22, F23, F24,
}
use KeyCode::*;
#[rustfmt::skip]
const EVDEV: [KeyCode; 127] = [
Escape, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7, Digit8, Digit9, Digit0, Minus, Equal,
Backspace, Tab, KeyQ, KeyW, KeyE, KeyR, KeyT, KeyY, KeyU, KeyI, KeyO, KeyP, BracketLeft, BracketRight, Enter,
ControlLeft, KeyA, KeyS, KeyD, KeyF, KeyG, KeyH, KeyJ, KeyK, KeyL, Semicolon, Quote, Backquote, ShiftLeft,
Backslash, KeyZ, KeyX, KeyC, KeyV, KeyB, KeyN, KeyM, Comma, Period, Slash, ShiftRight, NumpadMultiply, AltLeft,
Space, CapsLock, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, NumLock, ScrollLock, Numpad7, Numpad8, Numpad9,
NumpadSubtract, Numpad4, Numpad5, Numpad6, NumpadAdd, Numpad1, Numpad2, Numpad3, Numpad0, NumpadDecimal,
Unidentified, Unidentified, IntlBackslash, F11, F12, IntlRo,
Unidentified, Unidentified, Convert, KanaMode, NonConvert, NumpadComma, NumpadEnter, ControlRight,
NumpadDivide, PrintScreen, AltRight,
Unidentified, Home, ArrowUp, PageUp, ArrowLeft, ArrowRight, End, ArrowDown, PageDown, Insert, Delete,
Unidentified, Unidentified, Unidentified, Unidentified, Unidentified, NumpadEqual,
Unidentified, Pause,
Unidentified, NumpadComma, Lang1, Lang2, IntlYen, MetaLeft, MetaRight, ContextMenu,
];
#[rustfmt::skip]
const MAC: [KeyCode; 127] = [
KeyA, KeyS, KeyD, KeyF, KeyH, KeyG, KeyZ, KeyX, KeyC, KeyV, IntlBackslash, KeyB, KeyQ, KeyW, KeyE, KeyR, KeyY,
KeyT, Digit1, Digit2, Digit3, Digit4, Digit6, Digit5, Equal, Digit9, Digit7, Minus, Digit8, Digit0,
BracketRight, KeyO, KeyU, BracketLeft, KeyI, KeyP, Enter, KeyL, KeyJ, Quote, KeyK, Semicolon, Backslash, Comma,
Slash, KeyN, KeyM, Period, Tab, Space, Backquote, Backspace,
Unidentified, Escape, MetaRight, MetaLeft, ShiftLeft, CapsLock, AltLeft, ControlLeft, ShiftRight, AltRight,
ControlRight,
Unidentified, F17, NumpadDecimal, Unidentified, NumpadMultiply, Unidentified, NumpadAdd, Unidentified,
NumLock,
Unidentified, Unidentified, Unidentified, NumpadDivide, NumpadEnter, Unidentified, NumpadSubtract, F18, F19,
NumpadEqual, Numpad0, Numpad1, Numpad2, Numpad3, Numpad4, Numpad5, Numpad6, Numpad7, F20, Numpad8, Numpad9,
IntlYen, IntlRo, NumpadComma, F5, F6, F7, F3, F8, F9, Lang2, F11, Lang1, F13, F16, F14, Unidentified, F10,
ContextMenu, F12, Unidentified, F15,
Insert, Home, PageUp, Delete, F4, End, F2, PageDown, F1, ArrowLeft, ArrowRight, ArrowDown, ArrowUp,
];
impl KeyCode {
pub fn from_evdev(code: u32) -> KeyCode {
match code {
1..=127 => EVDEV[code as usize - 1],
183..=194 => [F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23, F24][code as usize - 183],
_ => Unidentified,
}
}
pub fn from_keysym(keysym: u32) -> Option<KeyCode> {
Some(match keysym {
0xff1b => Escape,
0xff09 | 0xfe20 => Tab,
0xff0d => Enter,
0xff08 => Backspace,
0xffff => Delete,
0xff63 => Insert,
0xff50 => Home,
0xff57 => End,
0xff55 => PageUp,
0xff56 => PageDown,
0xff51 => ArrowLeft,
0xff52 => ArrowUp,
0xff53 => ArrowRight,
0xff54 => ArrowDown,
0xff61 => PrintScreen,
0xff67 => ContextMenu,
0xff13 => Pause,
0xff14 => ScrollLock,
0xff7f => NumLock,
0xffe5 => CapsLock,
0xffe1 => ShiftLeft,
0xffe2 => ShiftRight,
0xffe3 => ControlLeft,
0xffe4 => ControlRight,
0xffe9 => AltLeft,
0xffea | 0xfe03 => AltRight,
0xffeb => MetaLeft,
0xffec => MetaRight,
0xffbe..=0xffd5 => [
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22,
F23, F24,
][keysym as usize - 0xffbe],
_ => return None,
})
}
pub fn from_mac(code: u16) -> KeyCode {
MAC.get(code as usize).copied().unwrap_or(Unidentified)
}
pub fn from_windows_scancode(code: u32) -> KeyCode {
match code {
0x01..=0x53 if code != 0x45 => EVDEV[code as usize - 1],
0x45 => Pause,
0x56 => IntlBackslash,
0x57 => F11,
0x58 => F12,
0x59 => NumpadEqual,
0x64..=0x6E => [F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23][code as usize - 0x64],
0x70 => KanaMode,
0x73 => IntlRo,
0x76 => F24,
0x79 => Convert,
0x7B => NonConvert,
0x7D => IntlYen,
0x7E => NumpadComma,
0x54 => PrintScreen,
0xE01C => NumpadEnter,
0xE01D => ControlRight,
0xE035 => NumpadDivide,
0xE037 => PrintScreen,
0xE038 => AltRight,
0xE045 => NumLock,
0xE047 => Home,
0xE048 => ArrowUp,
0xE049 => PageUp,
0xE04B => ArrowLeft,
0xE04D => ArrowRight,
0xE04F => End,
0xE050 => ArrowDown,
0xE051 => PageDown,
0xE052 => Insert,
0xE053 => Delete,
0xE05B => MetaLeft,
0xE05C => MetaRight,
0xE05D => ContextMenu,
0x72 => Lang1,
0x71 => Lang2,
_ => Unidentified,
}
}
pub fn from_us_char(c: char) -> KeyCode {
const LETTERS: [KeyCode; 26] = [
KeyA, KeyB, KeyC, KeyD, KeyE, KeyF, KeyG, KeyH, KeyI, KeyJ, KeyK, KeyL, KeyM, KeyN, KeyO, KeyP, KeyQ, KeyR,
KeyS, KeyT, KeyU, KeyV, KeyW, KeyX, KeyY, KeyZ,
];
const DIGITS: [KeyCode; 10] = [Digit0, Digit1, Digit2, Digit3, Digit4, Digit5, Digit6, Digit7, Digit8, Digit9];
match c.to_ascii_lowercase() {
c @ 'a'..='z' => LETTERS[c as usize - 'a' as usize],
c @ '0'..='9' => DIGITS[c as usize - '0' as usize],
' ' => Space,
'`' => Backquote,
'-' => Minus,
'=' => Equal,
'[' => BracketLeft,
']' => BracketRight,
'\\' => Backslash,
';' => Semicolon,
'\'' => Quote,
',' => Comma,
'.' => Period,
'/' => Slash,
_ => Unidentified,
}
}
pub fn from_key(key: crate::backend::Key) -> KeyCode {
use crate::backend::Key;
const F: [KeyCode; 24] = [
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, F20, F21, F22, F23,
F24,
];
match key {
Key::Char(c) => KeyCode::from_us_char(c),
Key::Enter => Enter,
Key::Escape => Escape,
Key::Tab => Tab,
Key::Backspace => Backspace,
Key::Delete => Delete,
Key::Up => ArrowUp,
Key::Down => ArrowDown,
Key::Left => ArrowLeft,
Key::Right => ArrowRight,
Key::Home => Home,
Key::End => End,
Key::PageUp => PageUp,
Key::PageDown => PageDown,
Key::F(n) => F.get(usize::from(n).wrapping_sub(1)).copied().unwrap_or(Unidentified),
}
}
}