1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-23 21:32:13 +03:00

Add TerminalHost trait

This will make it easier to add copy/paste handling in a follow up.
This commit is contained in:
Wez Furlong 2018-02-04 22:35:33 -08:00
parent 5261f9f3d8
commit b24ac14c05
2 changed files with 57 additions and 19 deletions

View File

@ -97,6 +97,16 @@ struct CachedGlyph {
bearing_y: isize, bearing_y: isize,
} }
struct Host<'a> {
pty: &'a mut Write,
}
impl<'a> term::TerminalHost for Host<'a> {
fn writer(&mut self) -> &mut Write {
self.pty
}
}
impl<'a> TerminalWindow<'a> { impl<'a> TerminalWindow<'a> {
pub fn new( pub fn new(
conn: &Connection, conn: &Connection,
@ -651,12 +661,20 @@ impl<'a> TerminalWindow<'a> {
xcb::KEY_PRESS => { xcb::KEY_PRESS => {
let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) };
let (code, mods) = self.decode_key(key_press); let (code, mods) = self.decode_key(key_press);
self.terminal.key_down(code, mods, &mut self.pty)?; self.terminal.key_down(
code,
mods,
&mut Host { pty: &mut self.pty },
)?;
} }
xcb::KEY_RELEASE => { xcb::KEY_RELEASE => {
let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) }; let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(&event) };
let (code, mods) = self.decode_key(key_press); let (code, mods) = self.decode_key(key_press);
self.terminal.key_up(code, mods, &mut self.pty)?; self.terminal.key_up(
code,
mods,
&mut Host { pty: &mut self.pty },
)?;
} }
xcb::MOTION_NOTIFY => { xcb::MOTION_NOTIFY => {
let motion: &xcb::MotionNotifyEvent = unsafe { xcb::cast_event(&event) }; let motion: &xcb::MotionNotifyEvent = unsafe { xcb::cast_event(&event) };
@ -668,7 +686,10 @@ impl<'a> TerminalWindow<'a> {
y: (motion.event_y() as f64 / self.cell_height).floor() as i64, y: (motion.event_y() as f64 / self.cell_height).floor() as i64,
modifiers: xkeysyms::modifiers_from_state(motion.state()), modifiers: xkeysyms::modifiers_from_state(motion.state()),
}; };
self.terminal.mouse_event(event, &mut self.pty)?; self.terminal.mouse_event(
event,
&mut Host { pty: &mut self.pty },
)?;
} }
xcb::BUTTON_PRESS | xcb::BUTTON_PRESS |
xcb::BUTTON_RELEASE => { xcb::BUTTON_RELEASE => {
@ -696,7 +717,10 @@ impl<'a> TerminalWindow<'a> {
modifiers: xkeysyms::modifiers_from_state(button_press.state()), modifiers: xkeysyms::modifiers_from_state(button_press.state()),
}; };
self.terminal.mouse_event(event, &mut self.pty)?; self.terminal.mouse_event(
event,
&mut Host { pty: &mut self.pty },
)?;
} }
xcb::CLIENT_MESSAGE => { xcb::CLIENT_MESSAGE => {
let msg: &xcb::ClientMessageEvent = unsafe { xcb::cast_event(&event) }; let msg: &xcb::ClientMessageEvent = unsafe { xcb::cast_event(&event) };

View File

@ -759,11 +759,7 @@ impl TerminalState {
} }
} }
pub fn mouse_event<W: std::io::Write>( pub fn mouse_event(&mut self, event: MouseEvent, host: &mut TerminalHost) -> Result<(), Error> {
&mut self,
event: MouseEvent,
write: &mut W,
) -> Result<(), Error> {
match event { match event {
MouseEvent { MouseEvent {
kind: MouseEventKind::Press, kind: MouseEventKind::Press,
@ -783,7 +779,7 @@ impl TerminalState {
if self.sgr_mouse { if self.sgr_mouse {
write!( write!(
write, host.writer(),
"\x1b[<{};{};{}M", "\x1b[<{};{};{}M",
report_button, report_button,
event.x + 1, event.x + 1,
@ -791,7 +787,7 @@ impl TerminalState {
)?; )?;
} else if self.alt_screen_is_active { } else if self.alt_screen_is_active {
// Send cursor keys instead (equivalent to xterm's alternateScroll mode) // Send cursor keys instead (equivalent to xterm's alternateScroll mode)
self.key_down(key, KeyModifiers::default(), write)?; self.key_down(key, KeyModifiers::default(), host)?;
} else { } else {
self.scroll_viewport(scroll_delta) self.scroll_viewport(scroll_delta)
} }
@ -806,14 +802,20 @@ impl TerminalState {
} }
{ {
if self.sgr_mouse { if self.sgr_mouse {
write!(write, "\x1b[<{};{};{}M", button, event.x + 1, event.y + 1)?; write!(
host.writer(),
"\x1b[<{};{};{}M",
button,
event.x + 1,
event.y + 1
)?;
} }
} }
} }
MouseEvent { kind: MouseEventKind::Release, .. } => { MouseEvent { kind: MouseEventKind::Release, .. } => {
self.current_mouse_button = MouseButton::None; self.current_mouse_button = MouseButton::None;
if self.sgr_mouse { if self.sgr_mouse {
write!(write, "\x1b[<3;{};{}m", event.x + 1, event.y + 1)?; write!(host.writer(), "\x1b[<3;{};{}m", event.x + 1, event.y + 1)?;
} }
} }
MouseEvent { kind: MouseEventKind::Move, .. } => { MouseEvent { kind: MouseEventKind::Move, .. } => {
@ -826,7 +828,13 @@ impl TerminalState {
} }
{ {
if self.sgr_mouse { if self.sgr_mouse {
write!(write, "\x1b[<{};{};{}M", button, event.x + 1, event.y + 1)?; write!(
host.writer(),
"\x1b[<{};{};{}M",
button,
event.x + 1,
event.y + 1
)?;
} }
} }
} }
@ -838,11 +846,11 @@ impl TerminalState {
/// that is embedding the Terminal. This method translates the /// that is embedding the Terminal. This method translates the
/// keycode into a sequence of bytes to send to the slave end /// keycode into a sequence of bytes to send to the slave end
/// of the pty via the `Write`-able object provided by the caller. /// of the pty via the `Write`-able object provided by the caller.
pub fn key_down<W: std::io::Write>( pub fn key_down(
&mut self, &mut self,
key: KeyCode, key: KeyCode,
mods: KeyModifiers, mods: KeyModifiers,
write: &mut W, host: &mut TerminalHost,
) -> Result<(), Error> { ) -> Result<(), Error> {
const CTRL: KeyModifiers = KeyModifiers::CTRL; const CTRL: KeyModifiers = KeyModifiers::CTRL;
const SHIFT: KeyModifiers = KeyModifiers::SHIFT; const SHIFT: KeyModifiers = KeyModifiers::SHIFT;
@ -905,7 +913,7 @@ impl TerminalState {
(Unknown, ..) => "", (Unknown, ..) => "",
}; };
write.write(&to_send.as_bytes())?; host.writer().write(&to_send.as_bytes())?;
// Reset the viewport if we sent data to the parser // Reset the viewport if we sent data to the parser
if to_send.len() > 0 && self.viewport_offset != 0 { if to_send.len() > 0 && self.viewport_offset != 0 {
@ -916,11 +924,11 @@ impl TerminalState {
Ok(()) Ok(())
} }
pub fn key_up<W: std::io::Write>( pub fn key_up(
&mut self, &mut self,
_: KeyCode, _: KeyCode,
_: KeyModifiers, _: KeyModifiers,
_: &mut W, _: &mut TerminalHost,
) -> Result<(), Error> { ) -> Result<(), Error> {
Ok(()) Ok(())
} }
@ -1303,6 +1311,12 @@ impl Terminal {
} }
} }
/// Represents the host of the terminal.
/// Provides a means for sending data to the connected pty,
/// and for operating on the clipboard
pub trait TerminalHost {
fn writer(&mut self) -> &mut std::io::Write;
}
impl vte::Perform for TerminalState { impl vte::Perform for TerminalState {
/// Draw a character to the screen /// Draw a character to the screen