From 5c172e4ed2508873817876a718be3920c83fdf9d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 18 Jun 2022 16:10:44 -0700 Subject: [PATCH] term: support kitty keyboard protocol This enables tentative support for https://sw.kovidgoyal.net/kitty/keyboard-protocol It's only been lightly tested with the notcurses-input program and eyeballed against a few random keypresses in kitty running `printf "\x1b[=11u" ; od -c` I tried with neovim, but it doesn't seem like the version available in Fedora 36 supports this yet. refs: https://github.com/wez/wezterm/issues/1141 --- docs/changelog.md | 1 + term/src/terminalstate/keyboard.rs | 2 +- termwiz/src/input.rs | 237 +++++++++++++++++++++++++++-- 3 files changed, 223 insertions(+), 17 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 304645d67..4ea0b4f9c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -14,6 +14,7 @@ As features stabilize some brief notes about them will accumulate here. #### New * [background](config/lua/config/background.md) option for rich background compositing and parallax scrolling effects. * Added [docs for the cli](cli/general.md) +* Support for the [Kitty Keyboard Protocol](https://sw.kovidgoyal.net/kitty/keyboard-protocol) * New [wezterm.mux](config/lua/wezterm.mux/index.md) module, [gui-startup](config/lua/gui-events/gui-startup.md) and [mux-startup](config/lua/mux-events/mux-startup.md) events for spawning programs into your preferred arrangement when wezterm starts. [#674](https://github.com/wez/wezterm/issues/674) * ssh client now supports `BindAddress`. Thanks to [@gpanders](https://github.com/gpanders)! [#1875](https://github.com/wez/wezterm/pull/1875) * [PaneInformation.domain_name](config/lua/PaneInformation.md) and [pane:get_domain_name()](config/lua/pane/get_domain_name.md) which return the name of the domain with which a pane is associated. [#1881](https://github.com/wez/wezterm/issues/1881) diff --git a/term/src/terminalstate/keyboard.rs b/term/src/terminalstate/keyboard.rs index fb4aaaa34..b01622121 100644 --- a/term/src/terminalstate/keyboard.rs +++ b/term/src/terminalstate/keyboard.rs @@ -29,7 +29,7 @@ impl TerminalState { ) -> anyhow::Result<()> { let encoding = self.effective_keyboard_encoding(); - let to_send = key.encode_up_down( + let to_send = key.encode( mods, KeyCodeEncodeModes { encoding, diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index d3fa942fe..59a7595f6 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -256,22 +256,221 @@ impl KeyCode { ) } - pub fn encode_up_down( + /// + fn kitty_function_code(self) -> Option { + use KeyCode::*; + Some(match self { + Escape => 27, + Enter => 13, + Tab => 9, + Backspace => 127, + CapsLock => 57358, + ScrollLock => 57359, + NumLock => 57360, + PrintScreen => 57361, + Pause => 57362, + Menu => 57363, + Function(n) if n >= 13 && n <= 35 => 57376 + n as u32 - 13, + Numpad0 => 57399, + Numpad1 => 57400, + Numpad2 => 57401, + Numpad3 => 57402, + Numpad4 => 57403, + Numpad5 => 57404, + Numpad6 => 57405, + Numpad7 => 57406, + Numpad8 => 57407, + Numpad9 => 57408, + Decimal => 57409, + Divide => 57410, + Multiply => 57411, + Subtract => 57412, + Add => 57413, + // KeypadEnter => 57414, + // KeypadEquals => 57415, + Separator => 57416, + ApplicationLeftArrow => 57417, + ApplicationRightArrow => 57418, + ApplicationUpArrow => 57419, + ApplicationDownArrow => 57420, + PageUp => 57421, + PageDown => 57422, + Home => 57423, + End => 57424, + Insert => 57425, + // KeypadDelete => 57426, + MediaPlayPause => 57430, + MediaStop => 57432, + MediaNextTrack => 57435, + MediaPrevTrack => 57436, + VolumeDown => 57436, + VolumeUp => 57439, + VolumeMute => 57440, + LeftShift => 57441, + LeftControl => 57442, + LeftAlt => 57443, + LeftWindows => 57444, + RightShift => 57447, + RightControl => 57448, + RightAlt => 57449, + RightWindows => 57450, + _ => return None, + }) + } + + fn encode_kitty( + &self, + mods: Modifiers, + is_down: bool, + flags: KittyKeyboardFlags, + ) -> Result { + use KeyCode::*; + + // Normalize + let key = match self { + Char('\r') => Enter, + Char('\t') => Tab, + Char('\x7f') => Delete, + Char('\x08') => Backspace, + c => *c, + }; + + if mods.is_empty() && !flags.contains(KittyKeyboardFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES) { + // Check for simple text generating keys + match key { + Enter => return Ok("\r".to_string()), + Tab => return Ok("\t".to_string()), + Backspace => return Ok("\x7f".to_string()), + Char(c) => return Ok(c.to_string()), + _ => {} + } + } + + let mut modifiers = 0; + if mods.contains(Modifiers::SHIFT) { + modifiers |= 1; + } + if mods.contains(Modifiers::ALT) { + modifiers |= 2; + } + if mods.contains(Modifiers::CTRL) { + modifiers |= 4; + } + if mods.contains(Modifiers::SUPER) { + modifiers |= 8; + } + modifiers += 1; + + let event_type = if flags.contains(KittyKeyboardFlags::REPORT_EVENT_TYPES) && !is_down { + ":3" + } else { + "" + }; + + match key { + Char(shifted_key) => { + let c = shifted_key.to_ascii_lowercase(); + + let key_code = if flags.contains(KittyKeyboardFlags::REPORT_ALTERNATE_KEYS) + && c != shifted_key + { + // Note: we don't have enough information here to know what the base-layout key + // should really be. + let base_layout = c; + format!("{}:{}:{}", c, shifted_key, base_layout) + } else { + (c as u32).to_string() + }; + + Ok(format!("\x1b[{key_code};{modifiers}{event_type}u")) + } + LeftArrow | RightArrow | UpArrow | DownArrow | Home | End => { + let c = match key { + UpArrow => 'A', + DownArrow => 'B', + RightArrow => 'C', + LeftArrow => 'D', + Home => 'H', + End => 'F', + _ => unreachable!(), + }; + Ok(format!("\x1b[1;{modifiers}{event_type}{c}")) + } + PageUp | PageDown | Insert | Delete => { + let c = match key { + Insert => 2, + Delete => 3, + PageUp => 5, + PageDown => 6, + _ => unreachable!(), + }; + + Ok(format!("\x1b[{c};{modifiers}{event_type}~")) + } + Function(n) if n < 13 => { + if mods.is_empty() && n < 5 { + // F1-F4 are encoded using SS3 if there are no modifiers + Ok(format!( + "{}", + match n { + 1 => "\x1bOP", + 2 => "\x1bOQ", + 3 => "\x1bOR", + 4 => "\x1bOS", + _ => unreachable!("wat?"), + } + )) + } else { + // Higher numbered F-keys plus modified F-keys are encoded + // using CSI instead of SS3. + let intro = match n { + 1 => "\x1b[11", + 2 => "\x1b[12", + 3 => "\x1b[13", + 4 => "\x1b[14", + 5 => "\x1b[15", + 6 => "\x1b[17", + 7 => "\x1b[18", + 8 => "\x1b[19", + 9 => "\x1b[20", + 10 => "\x1b[21", + 11 => "\x1b[23", + 12 => "\x1b[24", + _ => unreachable!(), + }; + Ok(format!("{intro};{modifiers}{event_type}~")) + } + } + + _ => { + if let Some(code) = key.kitty_function_code() { + Ok(format!("\x1b[{code};{modifiers}{event_type}u")) + } else { + Ok(String::new()) + } + } + } + } + + /// Returns the byte sequence that represents this KeyCode and Modifier combination, + pub fn encode( &self, mods: Modifiers, modes: KeyCodeEncodeModes, is_down: bool, ) -> Result { + match &modes.encoding { + KeyboardEncoding::Kitty(flags) if *flags != KittyKeyboardFlags::NONE => { + log::info!("{:?}", flags); + return dbg!(self.encode_kitty(mods, is_down, *flags)); + } + _ => {} + } if !is_down { + // We only want down events return Ok(String::new()); } - self.encode(mods, modes) - } - - /// Returns the xterm compatible byte sequence that represents this KeyCode - /// and Modifier combination. - pub fn encode(&self, mods: Modifiers, modes: KeyCodeEncodeModes) -> Result { use KeyCode::*; let key = self.normalize_shift_to_upper_case(mods); @@ -1569,39 +1768,45 @@ mod test { }; assert_eq!( - KeyCode::LeftArrow.encode(Modifiers::NONE, mode).unwrap(), + KeyCode::LeftArrow + .encode(Modifiers::NONE, mode, true) + .unwrap(), "\x1b[D".to_string() ); assert_eq!( - KeyCode::LeftArrow.encode(Modifiers::ALT, mode).unwrap(), + KeyCode::LeftArrow + .encode(Modifiers::ALT, mode, true) + .unwrap(), "\x1b[1;3D".to_string() ); assert_eq!( - KeyCode::Home.encode(Modifiers::NONE, mode).unwrap(), + KeyCode::Home.encode(Modifiers::NONE, mode, true).unwrap(), "\x1b[H".to_string() ); assert_eq!( - KeyCode::Home.encode(Modifiers::ALT, mode).unwrap(), + KeyCode::Home.encode(Modifiers::ALT, mode, true).unwrap(), "\x1b[1;3H".to_string() ); assert_eq!( - KeyCode::End.encode(Modifiers::NONE, mode).unwrap(), + KeyCode::End.encode(Modifiers::NONE, mode, true).unwrap(), "\x1b[F".to_string() ); assert_eq!( - KeyCode::End.encode(Modifiers::ALT, mode).unwrap(), + KeyCode::End.encode(Modifiers::ALT, mode, true).unwrap(), "\x1b[1;3F".to_string() ); assert_eq!( - KeyCode::Tab.encode(Modifiers::ALT, mode).unwrap(), + KeyCode::Tab.encode(Modifiers::ALT, mode, true).unwrap(), "\x1b\t".to_string() ); assert_eq!( - KeyCode::PageUp.encode(Modifiers::ALT, mode).unwrap(), + KeyCode::PageUp.encode(Modifiers::ALT, mode, true).unwrap(), "\x1b[5;3~".to_string() ); assert_eq!( - KeyCode::Function(1).encode(Modifiers::NONE, mode).unwrap(), + KeyCode::Function(1) + .encode(Modifiers::NONE, mode, true) + .unwrap(), "\x1bOP".to_string() ); }