From 3c40846d95becbc69e6c8c731aa759bb118624fc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 15 Apr 2023 14:52:54 -0700 Subject: [PATCH] keyboard: move led status to separate enum --- termwiz/src/input.rs | 2 - wezterm-gui/src/inputmap.rs | 11 +--- wezterm-gui/src/termwindow/keyevent.rs | 3 - wezterm-gui/src/termwindow/mouseevent.rs | 4 +- wezterm-input-types/src/lib.rs | 80 +++++++++++++----------- window/src/os/macos/window.rs | 14 +++-- window/src/os/wayland/window.rs | 3 + window/src/os/x11/keyboard.rs | 22 +++++-- 8 files changed, 78 insertions(+), 61 deletions(-) diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index faef8980a..44cf1a18a 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -278,8 +278,6 @@ impl KeyCode { _ => mods, }; - let mods = mods.remove_keyboard_status_mods(); - // Normalize Backspace and Delete let key = match key { Char('\x7f') => Delete, diff --git a/wezterm-gui/src/inputmap.rs b/wezterm-gui/src/inputmap.rs index 604661694..3b41b6d1f 100644 --- a/wezterm-gui/src/inputmap.rs +++ b/wezterm-gui/src/inputmap.rs @@ -427,9 +427,7 @@ impl InputMap { pub fn is_leader(&self, key: &KeyCode, mods: Modifiers) -> Option { if let Some((leader_key, leader_mods, timeout)) = self.leader.as_ref() { - if *leader_key == *key - && *leader_mods == mods.remove_positional_mods().remove_keyboard_status_mods() - { + if *leader_key == *key && *leader_mods == mods.remove_positional_mods() { return Some(timeout.clone()); } } @@ -452,7 +450,7 @@ impl InputMap { }; table - .get(&key.normalize_shift(mods.remove_positional_mods().remove_keyboard_status_mods())) + .get(&key.normalize_shift(mods.remove_positional_mods())) .cloned() } @@ -461,10 +459,7 @@ impl InputMap { event: MouseEventTrigger, mut mods: MouseEventTriggerMods, ) -> Option { - mods.mods = mods - .mods - .remove_positional_mods() - .remove_keyboard_status_mods(); + mods.mods = mods.mods.remove_positional_mods(); self.mouse.get(&(event, mods)).cloned() } diff --git a/wezterm-gui/src/termwindow/keyevent.rs b/wezterm-gui/src/termwindow/keyevent.rs index 70a24b904..fd2670f9e 100644 --- a/wezterm-gui/src/termwindow/keyevent.rs +++ b/wezterm-gui/src/termwindow/keyevent.rs @@ -246,9 +246,6 @@ impl super::TermWindow { is_down: bool, key_event: Option<&KeyEvent>, ) -> bool { - // We don't allow caps lock or num lock to influence key resolution at the GUI layer. - let raw_modifiers = raw_modifiers.remove_keyboard_status_mods(); - if is_down && !leader_active { // Check to see if this key-press is the leader activating if let Some(duration) = self.input_map.is_leader(&keycode, raw_modifiers) { diff --git a/wezterm-gui/src/termwindow/mouseevent.rs b/wezterm-gui/src/termwindow/mouseevent.rs index 4e75572cb..c9c35b1f8 100644 --- a/wezterm-gui/src/termwindow/mouseevent.rs +++ b/wezterm-gui/src/termwindow/mouseevent.rs @@ -58,15 +58,13 @@ impl super::TermWindow { } } - pub fn mouse_event_impl(&mut self, mut event: MouseEvent, context: &dyn WindowOps) { + pub fn mouse_event_impl(&mut self, event: MouseEvent, context: &dyn WindowOps) { log::trace!("{:?}", event); let pane = match self.get_active_pane_or_overlay() { Some(pane) => pane, None => return, }; - event.modifiers = event.modifiers.remove_keyboard_status_mods(); - self.current_mouse_event.replace(event.clone()); let border = self.get_os_border(); diff --git a/wezterm-input-types/src/lib.rs b/wezterm-input-types/src/lib.rs index dca9a7505..ebcd613a1 100644 --- a/wezterm-input-types/src/lib.rs +++ b/wezterm-input-types/src/lib.rs @@ -461,6 +461,14 @@ impl ToString for KeyCode { } } +bitflags! { + #[derive(Default, FromDynamic, ToDynamic)] + pub struct KeyboardLedStatus: u8 { + const CAPS_LOCK = 1<<1; + const NUM_LOCK = 1<<2; + } +} + bitflags! { #[cfg_attr(feature="serde", derive(Serialize, Deserialize))] #[derive(Default, FromDynamic, ToDynamic)] @@ -480,10 +488,6 @@ bitflags! { const LEFT_SHIFT = 1<<10; const RIGHT_SHIFT = 1<<11; const ENHANCED_KEY = 1<<12; - /// Not really a modifier, but a keyboard driver state - const CAPS_LOCK = 1<<13; - /// Not really a modifier, but a keyboard driver state - const NUM_LOCK = 1<<14; } } @@ -636,24 +640,6 @@ impl Modifiers { "ENHANCED_KEY", "ENHANCED_KEY", ), - ( - Self::CAPS_LOCK, - "CAPS_LOCK", - "CAPS_LOCK", - "CAPS_LOCK", - "CAPS_LOCK", - "CAPS_LOCK", - "CAPS_LOCK", - ), - ( - Self::NUM_LOCK, - "NUM_LOCK", - "NUM_LOCK", - "NUM_LOCK", - "NUM_LOCK", - "NUM_LOCK", - "NUM_LOCK", - ), ] { if !self.contains(value) { continue; @@ -699,11 +685,6 @@ impl Modifiers { | Self::RIGHT_SHIFT | Self::ENHANCED_KEY) } - - /// Remove status indicators that are not true modifiers - pub fn remove_keyboard_status_mods(self) -> Self { - self - (Self::CAPS_LOCK | Self::NUM_LOCK) - } } /// These keycodes identify keys based on their physical @@ -1217,6 +1198,7 @@ impl Eq for Handled {} pub struct RawKeyEvent { pub key: KeyCode, pub modifiers: Modifiers, + pub leds: KeyboardLedStatus, /// The physical location of the key on an ANSI-Standard US layout pub phys_code: Option, @@ -1391,6 +1373,8 @@ pub struct KeyEvent { /// Which modifiers are down pub modifiers: Modifiers, + pub leds: KeyboardLedStatus, + /// How many times this key repeats pub repeat_count: u16, @@ -1402,9 +1386,7 @@ pub struct KeyEvent { } fn normalize_shift(key: KeyCode, modifiers: Modifiers) -> (KeyCode, Modifiers) { - if modifiers.contains(Modifiers::SHIFT | Modifiers::CAPS_LOCK) { - (key, modifiers - (Modifiers::SHIFT | Modifiers::CAPS_LOCK)) - } else if modifiers.contains(Modifiers::SHIFT) { + if modifiers.contains(Modifiers::SHIFT) { match key { KeyCode::Char(c) if c.is_ascii_uppercase() => (key, modifiers - Modifiers::SHIFT), KeyCode::Char(c) if c.is_ascii_lowercase() => ( @@ -1667,10 +1649,10 @@ impl KeyEvent { if self.modifiers.contains(Modifiers::SUPER) { modifiers |= 8; } - if self.modifiers.contains(Modifiers::CAPS_LOCK) { + if self.leds.contains(KeyboardLedStatus::CAPS_LOCK) { modifiers |= 64; } - if self.modifiers.contains(Modifiers::NUM_LOCK) { + if self.leds.contains(KeyboardLedStatus::NUM_LOCK) { modifiers |= 128; } modifiers += 1; @@ -1737,7 +1719,7 @@ impl KeyEvent { }); if let Some(numpad) = is_numpad { - let code = match (numpad, self.modifiers.contains(Modifiers::NUM_LOCK)) { + let code = match (numpad, self.leds.contains(KeyboardLedStatus::NUM_LOCK)) { (PhysKeyCode::Keypad0, true) => 57399, (PhysKeyCode::Keypad0, false) => 57425, (PhysKeyCode::Keypad1, true) => 57400, @@ -2186,6 +2168,7 @@ mod test { KeyEvent { key: KeyCode::Char('o'), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2197,6 +2180,7 @@ mod test { KeyEvent { key: KeyCode::Char('o'), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: false, raw: None @@ -2217,6 +2201,7 @@ mod test { KeyEvent { key: KeyCode::Function(1), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2228,6 +2213,7 @@ mod test { KeyEvent { key: KeyCode::Function(1), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: false, raw: None @@ -2245,6 +2231,7 @@ mod test { KeyEvent { key: KeyCode::Char('i'), modifiers: Modifiers::ALT | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2256,6 +2243,7 @@ mod test { KeyEvent { key: KeyCode::Char('I'), modifiers: Modifiers::ALT | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2268,6 +2256,7 @@ mod test { KeyEvent { key: KeyCode::Char('1'), modifiers: Modifiers::ALT | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2281,6 +2270,7 @@ mod test { KeyEvent { key: KeyCode::Char('!'), modifiers: Modifiers::ALT | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2303,6 +2293,7 @@ mod test { KeyEvent { key: KeyCode::Char('A'), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2314,6 +2305,7 @@ mod test { KeyEvent { key: KeyCode::Char('A'), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: false, raw: None @@ -2356,6 +2348,7 @@ mod test { KeyEvent { key: KeyCode::LeftShift, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2370,6 +2363,7 @@ mod test { KeyEvent { key: KeyCode::LeftShift, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: false, raw: None @@ -2384,6 +2378,7 @@ mod test { KeyEvent { key: KeyCode::LeftControl, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2398,6 +2393,7 @@ mod test { KeyEvent { key: KeyCode::LeftControl, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: false, raw: None @@ -2421,6 +2417,7 @@ mod test { KeyEvent { key: KeyCode::Numpad(0), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2435,6 +2432,7 @@ mod test { KeyEvent { key: KeyCode::Numpad(0), modifiers: Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2450,6 +2448,7 @@ mod test { KeyEvent { key: KeyCode::Numpad(1), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2464,6 +2463,7 @@ mod test { KeyEvent { key: KeyCode::Numpad(1), modifiers: Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2478,7 +2478,8 @@ mod test { make_event_with_raw( KeyEvent { key: KeyCode::Numpad(0), - modifiers: Modifiers::NUM_LOCK, + modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::NUM_LOCK, repeat_count: 1, key_is_down: true, raw: None @@ -2492,7 +2493,8 @@ mod test { make_event_with_raw( KeyEvent { key: KeyCode::Numpad(0), - modifiers: Modifiers::NUM_LOCK | Modifiers::SHIFT, + modifiers: Modifiers::SHIFT, + leds: KeyboardLedStatus::NUM_LOCK, repeat_count: 1, key_is_down: true, raw: None @@ -2512,6 +2514,7 @@ mod test { KeyEvent { key: KeyCode::Char('"'), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2524,6 +2527,7 @@ mod test { KeyEvent { key: KeyCode::Char('"'), modifiers: Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2536,6 +2540,7 @@ mod test { KeyEvent { key: KeyCode::Char('!'), modifiers: Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2548,6 +2553,7 @@ mod test { KeyEvent { key: KeyCode::LeftShift, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2569,6 +2575,7 @@ mod test { KeyEvent { key: KeyCode::Char('ф'), modifiers: Modifiers::CTRL, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2584,6 +2591,7 @@ mod test { KeyEvent { key: KeyCode::Char('Ф'), modifiers: Modifiers::CTRL | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2608,6 +2616,7 @@ mod test { KeyEvent { key: KeyCode::Char('ф'), modifiers: Modifiers::CTRL, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None @@ -2623,6 +2632,7 @@ mod test { KeyEvent { key: KeyCode::Char('Ф'), modifiers: Modifiers::CTRL | Modifiers::SHIFT, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down: true, raw: None diff --git a/window/src/os/macos/window.rs b/window/src/os/macos/window.rs index b6036b1fa..15433a862 100644 --- a/window/src/os/macos/window.rs +++ b/window/src/os/macos/window.rs @@ -49,7 +49,7 @@ use std::rc::Rc; use std::str::FromStr; use std::time::Instant; use wezterm_font::FontConfiguration; -use wezterm_input_types::{is_ascii_control, IntegratedTitleButtonStyle}; +use wezterm_input_types::{is_ascii_control, IntegratedTitleButtonStyle, KeyboardLedStatus}; #[allow(non_upper_case_globals)] const NSViewLayerContentsPlacementTopLeft: NSInteger = 11; @@ -1680,9 +1680,6 @@ fn key_modifiers(flags: NSEventModifierFlags) -> Modifiers { if flags.contains(NSEventModifierFlags::NSCommandKeyMask) { mods |= Modifiers::SUPER; } - if flags.bits() & (1 << 16) != 0 { - mods |= Modifiers::CAPS_LOCK; - } mods } @@ -1813,6 +1810,7 @@ impl WindowView { let event = KeyEvent { key, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down, raw: None, @@ -2282,6 +2280,11 @@ impl WindowView { let unmod = unsafe { nsstring_to_str(nsevent.charactersIgnoringModifiers()) }; let modifier_flags = unsafe { nsevent.modifierFlags() }; let modifiers = key_modifiers(modifier_flags); + let leds = if modifier_flags.bits() & (1 << 16) != 0 { + KeyboardLedStatus::CAPS_LOCK + } else { + KeyboardLedStatus::empty() + }; let virtual_key = unsafe { nsevent.keyCode() }; log::debug!( @@ -2335,6 +2338,7 @@ impl WindowView { }, phys_code, raw_code: virtual_key as _, + leds, modifiers, repeat_count: 1, key_is_down, @@ -2377,6 +2381,7 @@ impl WindowView { let event = KeyEvent { key: KeyCode::composed(&translated), modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), repeat_count: 1, key_is_down, raw: None, @@ -2598,6 +2603,7 @@ impl WindowView { let event = KeyEvent { key, modifiers, + leds, repeat_count: 1, key_is_down, raw: Some(raw_key_event), diff --git a/window/src/os/wayland/window.rs b/window/src/os/wayland/window.rs index db66d7d26..e5654e607 100644 --- a/window/src/os/wayland/window.rs +++ b/window/src/os/wayland/window.rs @@ -140,6 +140,7 @@ pub struct WaylandWindowInner { hscroll_remainder: f64, vscroll_remainder: f64, modifiers: Modifiers, + leds: KeyboardLedStatus, key_repeat: Option<(u32, Arc>)>, pending_event: Arc>, pending_mouse: Arc>, @@ -367,6 +368,7 @@ impl WaylandWindow { hscroll_remainder: 0.0, vscroll_remainder: 0.0, modifiers: Modifiers::NONE, + leds: KeyboardLedStatus::empty(), pending_event, pending_mouse, pending_first_configure: Some(pending_first_configure), @@ -465,6 +467,7 @@ impl WaylandWindowInner { } => { mapper.update_modifier_state(mods_depressed, mods_latched, mods_locked, group); self.modifiers = mapper.get_key_modifiers(); + self.leds = mapper.get_led_status(); } _ => {} } diff --git a/window/src/os/x11/keyboard.rs b/window/src/os/x11/keyboard.rs index cd398f0a9..ca113487b 100644 --- a/window/src/os/x11/keyboard.rs +++ b/window/src/os/x11/keyboard.rs @@ -269,6 +269,7 @@ impl Keyboard { ) -> Option { let phys_code = self.phys_code_map.borrow().get(&xcode).copied(); let raw_modifiers = self.get_key_modifiers(); + let leds = self.get_led_status(); let xsym = self.state.borrow().key_get_one_sym(xcode); let handled = Handled::new(); @@ -281,6 +282,7 @@ impl Keyboard { phys_code, raw_code: xcode, modifiers: raw_modifiers, + leds, repeat_count: 1, key_is_down: pressed, handled: handled.clone(), @@ -358,6 +360,7 @@ impl Keyboard { let event = KeyEvent { key: kc, + leds, modifiers: raw_modifiers, repeat_count: 1, key_is_down: pressed, @@ -386,6 +389,19 @@ impl Keyboard { self.state.borrow().led_name_is_active(led) } + pub fn get_led_status(&self) -> KeyboardLedStatus { + let mut leds = KeyboardLedStatus::empty(); + + if self.led_is_active(xkb::LED_NAME_NUM) { + leds |= KeyboardLedStatus::NUM_LOCK; + } + if self.led_is_active(xkb::LED_NAME_CAPS) { + leds |= KeyboardLedStatus::CAPS_LOCK; + } + + leds + } + pub fn get_key_modifiers(&self) -> Modifiers { let mut res = Modifiers::default(); @@ -403,12 +419,6 @@ impl Keyboard { // Mod4 res |= Modifiers::SUPER; } - if self.led_is_active(xkb::LED_NAME_NUM) { - res |= Modifiers::NUM_LOCK; - } - if self.led_is_active(xkb::LED_NAME_CAPS) { - res |= Modifiers::CAPS_LOCK; - } res }