From 60c24a9e41990bb23770c2aced8dd2afc7966305 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 5 Aug 2018 14:28:29 -0700 Subject: [PATCH] consolidate on one set of input keycodes and modifiers --- src/xwindows/xkeysyms.rs | 40 +++++++++++----------- src/xwindows/xwin.rs | 42 ++++++++++++----------- term/src/input.rs | 36 ++------------------ term/src/lib.rs | 2 +- term/src/terminalstate.rs | 70 ++++++++++++++++++++++++++++++--------- termwiz/src/input.rs | 9 ++++- 6 files changed, 108 insertions(+), 91 deletions(-) diff --git a/src/xwindows/xkeysyms.rs b/src/xwindows/xkeysyms.rs index 99ef8e571..0930694a5 100644 --- a/src/xwindows/xkeysyms.rs +++ b/src/xwindows/xkeysyms.rs @@ -196,8 +196,8 @@ pub const XK_asciitilde: xcb_keysym_t = 0x007e; use term::KeyCode; use term::KeyModifiers; -pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode { - match k { +pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> Option { + Some(match k { XK_space...XK_asciitilde => { // This range overlaps with ascii KeyCode::Char(k as u8 as char) @@ -205,18 +205,18 @@ pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode { XK_BackSpace | XK_Tab | XK_Linefeed | XK_Return | XK_Escape => { KeyCode::Char((k & 0xff) as u8 as char) } - XK_F1 => KeyCode::F(1), - XK_F2 => KeyCode::F(2), - XK_F3 => KeyCode::F(3), - XK_F4 => KeyCode::F(4), - XK_F5 => KeyCode::F(5), - XK_F6 => KeyCode::F(6), - XK_F7 => KeyCode::F(7), - XK_F8 => KeyCode::F(8), - XK_F9 => KeyCode::F(9), - XK_F10 => KeyCode::F(10), - XK_F11 => KeyCode::F(11), - XK_F12 => KeyCode::F(12), + XK_F1 => KeyCode::Function(1), + XK_F2 => KeyCode::Function(2), + XK_F3 => KeyCode::Function(3), + XK_F4 => KeyCode::Function(4), + XK_F5 => KeyCode::Function(5), + XK_F6 => KeyCode::Function(6), + XK_F7 => KeyCode::Function(7), + XK_F8 => KeyCode::Function(8), + XK_F9 => KeyCode::Function(9), + XK_F10 => KeyCode::Function(10), + XK_F11 => KeyCode::Function(11), + XK_F12 => KeyCode::Function(12), XK_Control_L | XK_Control_R => KeyCode::Control, XK_Alt_L | XK_Alt_R => KeyCode::Alt, XK_Meta_L | XK_Meta_R => KeyCode::Meta, @@ -224,16 +224,16 @@ pub fn xcb_keysym_to_keycode(k: xcb_keysym_t) -> KeyCode { XK_Hyper_L | XK_Hyper_R => KeyCode::Hyper, XK_Shift_L | XK_Shift_R => KeyCode::Shift, XK_Home => KeyCode::Home, - XK_Left => KeyCode::Left, - XK_Up => KeyCode::Up, - XK_Right => KeyCode::Right, - XK_Down => KeyCode::Down, + XK_Left => KeyCode::LeftArrow, + XK_Up => KeyCode::UpArrow, + XK_Right => KeyCode::RightArrow, + XK_Down => KeyCode::DownArrow, XK_Page_Up => KeyCode::PageUp, XK_Page_Down => KeyCode::PageDown, XK_End => KeyCode::End, XK_KP_Insert | XK_Insert => KeyCode::Insert, - _ => KeyCode::Unknown, - } + _ => return None, + }) } pub fn modifiers_from_state(state: u16) -> KeyModifiers { diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 97c3bf07d..b54dc0dde 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -420,12 +420,12 @@ impl TerminalWindow { Ok(()) } - fn decode_key(&self, event: &xcb::KeyPressEvent) -> (KeyCode, KeyModifiers) { + fn decode_key(&self, event: &xcb::KeyPressEvent) -> Option<(KeyCode, KeyModifiers)> { let mods = xkeysyms::modifiers(event); let sym = self .conn .lookup_keysym(event, mods.contains(KeyModifiers::SHIFT)); - (xkeysyms::xcb_keysym_to_keycode(sym), mods) + xkeysyms::xcb_keysym_to_keycode(sym).map(|code| (code, mods)) } fn mouse_event(&mut self, event: MouseEvent) -> Result<(), Error> { @@ -452,27 +452,29 @@ impl TerminalWindow { } xcb::KEY_PRESS => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(event) }; - let (code, mods) = self.decode_key(key_press); - self.tabs.get_active().terminal.borrow_mut().key_down( - code, - mods, - &mut TabHost { - pty: &mut *self.tabs.get_active().pty.borrow_mut(), - host: &mut self.host, - }, - )?; + if let Some((code, mods)) = self.decode_key(key_press) { + self.tabs.get_active().terminal.borrow_mut().key_down( + code, + mods, + &mut TabHost { + pty: &mut *self.tabs.get_active().pty.borrow_mut(), + host: &mut self.host, + }, + )?; + } } xcb::KEY_RELEASE => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(event) }; - let (code, mods) = self.decode_key(key_press); - self.tabs.get_active().terminal.borrow_mut().key_up( - code, - mods, - &mut TabHost { - pty: &mut *self.tabs.get_active().pty.borrow_mut(), - host: &mut self.host, - }, - )?; + if let Some((code, mods)) = self.decode_key(key_press) { + self.tabs.get_active().terminal.borrow_mut().key_up( + code, + mods, + &mut TabHost { + pty: &mut *self.tabs.get_active().pty.borrow_mut(), + host: &mut self.host, + }, + )?; + } } xcb::MOTION_NOTIFY => { let motion: &xcb::MotionNotifyEvent = unsafe { xcb::cast_event(event) }; diff --git a/term/src/input.rs b/term/src/input.rs index 4a0f759b3..d11a9bebe 100644 --- a/term/src/input.rs +++ b/term/src/input.rs @@ -5,40 +5,8 @@ use std::time::{Duration, Instant}; use super::VisibleRowIndex; -bitflags! { - #[derive(Default)] - pub struct KeyModifiers :u8{ - const CTRL = 1; - const ALT = 2; - const META = 4; - const SUPER = 8; - const SHIFT = 16; - const NONE = 0; - } -} - -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub enum KeyCode { - Char(char), - Unknown, - Control, - Alt, - Meta, - Super, - Hyper, - Shift, - Left, - Up, - Right, - Down, - PageUp, - PageDown, - Home, - End, - Insert, - /// A numbered F-key - F(u8), -} +pub use termwiz::input::KeyCode; +pub use termwiz::input::Modifiers as KeyModifiers; #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum MouseButton { diff --git a/term/src/lib.rs b/term/src/lib.rs index 9f5401cbd..6c5d5b873 100644 --- a/term/src/lib.rs +++ b/term/src/lib.rs @@ -1,5 +1,5 @@ //! Terminal model -#[macro_use] +#[cfg_attr(test, macro_use)] extern crate bitflags; #[macro_use] extern crate failure; diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index 49af1f65c..1ed91b336 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -571,9 +571,9 @@ impl TerminalState { .. } => { let (report_button, scroll_delta, key) = if event.button == MouseButton::WheelUp { - (64, -1, KeyCode::Up) + (64, -1, KeyCode::UpArrow) } else { - (65, 1, KeyCode::Down) + (65, 1, KeyCode::DownArrow) }; if self.sgr_mouse { @@ -718,8 +718,12 @@ impl TerminalState { host.toggle_full_screen(); return Ok(()); } + (Tab, ..) => "\t", + (Enter, ..) => "\n", + (Backspace, ..) => "\x08", + (Escape, ..) => "\x1b", // Delete - (Char('\x7f'), ..) => "\x1b[3~", + (Char('\x7f'), ..) | (Delete, ..) => "\x1b[3~", (Char(c), CTRL, _, SHIFT, _) if c <= 0xff as char && c > 0x40 as char => { // If shift is held we have C == 0x43 and want to translate // that into 0x03 @@ -751,17 +755,17 @@ impl TerminalState { buf.as_str() } - (Up, _, _, _, APPCURSOR) => "\x1bOA", - (Down, _, _, _, APPCURSOR) => "\x1bOB", - (Right, _, _, _, APPCURSOR) => "\x1bOC", - (Left, _, _, _, APPCURSOR) => "\x1bOD", + (UpArrow, _, _, _, APPCURSOR) => "\x1bOA", + (DownArrow, _, _, _, APPCURSOR) => "\x1bOB", + (RightArrow, _, _, _, APPCURSOR) => "\x1bOC", + (LeftArrow, _, _, _, APPCURSOR) => "\x1bOD", (Home, _, _, _, APPCURSOR) => "\x1bOH", (End, _, _, _, APPCURSOR) => "\x1bOF", - (Up, ..) => "\x1b[A", - (Down, ..) => "\x1b[B", - (Right, ..) => "\x1b[C", - (Left, ..) => "\x1b[D", + (UpArrow, ..) => "\x1b[A", + (DownArrow, ..) => "\x1b[B", + (RightArrow, ..) => "\x1b[C", + (LeftArrow, ..) => "\x1b[D", (PageUp, _, _, SHIFT, _) => { let rows = self.screen().physical_rows as i64; self.scroll_viewport(-rows); @@ -778,7 +782,7 @@ impl TerminalState { (End, ..) => "\x1b[F", (Insert, ..) => "\x1b[2~", - (F(n), ..) => { + (Function(n), ..) => { let modifier = match (ctrl, alt, shift) { (NO, NO, NO) => "", (NO, NO, SHIFT) => ";2", @@ -823,9 +827,45 @@ impl TerminalState { } } - // Modifier keys pressed on their own and unmappable keys don't expand to anything - (Control, ..) | (Alt, ..) | (Meta, ..) | (Super, ..) | (Hyper, ..) | (Shift, ..) - | (Unknown, ..) => "", + // TODO: emit numpad sequences + (Numpad0, ..) | (Numpad1, ..) | (Numpad2, ..) | (Numpad3, ..) | (Numpad4, ..) + | (Numpad5, ..) | (Numpad6, ..) | (Numpad7, ..) | (Numpad8, ..) | (Numpad9, ..) + | (Multiply, ..) | (Add, ..) | (Separator, ..) | (Subtract, ..) | (Decimal, ..) + | (Divide, ..) => "", + + // Modifier keys pressed on their own don't expand to anything + (Control, ..) | (LeftControl, ..) | (RightControl, ..) | (Alt, ..) | (LeftAlt, ..) + | (RightAlt, ..) | (Menu, ..) | (LeftMenu, ..) | (RightMenu, ..) | (Super, ..) + | (Hyper, ..) | (Shift, ..) | (LeftShift, ..) | (RightShift, ..) | (Meta, ..) + | (LeftWindows, ..) | (RightWindows, ..) | (NumLock, ..) | (ScrollLock, ..) => "", + + (Cancel, ..) + | (Clear, ..) + | (Pause, ..) + | (CapsLock, ..) + | (Select, ..) + | (Print, ..) + | (PrintScreen, ..) + | (Execute, ..) + | (Help, ..) + | (Applications, ..) + | (Sleep, ..) + | (BrowserBack, ..) + | (BrowserForward, ..) + | (BrowserRefresh, ..) + | (BrowserStop, ..) + | (BrowserSearch, ..) + | (BrowserFavorites, ..) + | (BrowserHome, ..) + | (VolumeMute, ..) + | (VolumeDown, ..) + | (VolumeUp, ..) + | (MediaNextTrack, ..) + | (MediaPrevTrack, ..) + | (MediaStop, ..) + | (MediaPlayPause, ..) + | (InternalPasteStart, ..) + | (InternalPasteEnd, ..) => "", }; write_all(host.writer(), to_send.as_bytes())?; diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index 4204e1b4f..365723263 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -13,14 +13,17 @@ use winapi::um::wincon::{ }; bitflags! { + #[derive(Default)] pub struct Modifiers: u8 { const NONE = 0; const SHIFT = 1<<1; const ALT = 1<<2; const CTRL = 1<<3; + const SUPER = 1<<4; } } bitflags! { + #[derive(Default)] pub struct MouseButtons: u8 { const NONE = 0; const LEFT = 1<<1; @@ -68,11 +71,15 @@ pub struct KeyEvent { /// Which key is pressed. Not all of these are probable to appear /// on most systems. A lot of this list is @wez trawling docs and /// making an entry for things that might be possible in this first pass. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum KeyCode { /// The decoded unicode character Char(char), + Hyper, + Super, + Meta, + /// Ctrl-break on windows Cancel, Backspace,