1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-22 22:42:48 +03:00

keyboard: move led status to separate enum

This commit is contained in:
Wez Furlong 2023-04-15 14:52:54 -07:00
parent 08972faff2
commit 3c40846d95
No known key found for this signature in database
GPG Key ID: 7A7F66A31EC9B387
8 changed files with 78 additions and 61 deletions

View File

@ -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,

View File

@ -427,9 +427,7 @@ impl InputMap {
pub fn is_leader(&self, key: &KeyCode, mods: Modifiers) -> Option<std::time::Duration> {
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<KeyAssignment> {
mods.mods = mods
.mods
.remove_positional_mods()
.remove_keyboard_status_mods();
mods.mods = mods.mods.remove_positional_mods();
self.mouse.get(&(event, mods)).cloned()
}

View File

@ -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) {

View File

@ -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();

View File

@ -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<PhysKeyCode>,
@ -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

View File

@ -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),

View File

@ -140,6 +140,7 @@ pub struct WaylandWindowInner {
hscroll_remainder: f64,
vscroll_remainder: f64,
modifiers: Modifiers,
leds: KeyboardLedStatus,
key_repeat: Option<(u32, Arc<Mutex<KeyRepeatState>>)>,
pending_event: Arc<Mutex<PendingEvent>>,
pending_mouse: Arc<Mutex<PendingMouse>>,
@ -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();
}
_ => {}
}

View File

@ -269,6 +269,7 @@ impl Keyboard {
) -> Option<WindowKeyEvent> {
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
}