From 86cd39eb4e50d90403783b013a746157f2c9161d Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 9 Jun 2019 16:33:57 -0700 Subject: [PATCH] we can now send input to a client tab --- src/server/client.rs | 2 ++ src/server/codec.rs | 14 ++++++++++ src/server/listener.rs | 62 +++++++++++++++++++++++++++++++++++++++++- src/server/tab.rs | 22 +++++++++++---- term/src/input.rs | 10 +++---- termwiz/src/input.rs | 11 ++++---- 6 files changed, 105 insertions(+), 16 deletions(-) diff --git a/src/server/client.rs b/src/server/client.rs index 96c715b04..896054661 100644 --- a/src/server/client.rs +++ b/src/server/client.rs @@ -72,4 +72,6 @@ impl Client { ); rpc!(spawn, Spawn, SpawnResponse); rpc!(write_to_tab, WriteToTab, UnitResponse); + rpc!(key_down, SendKeyDown, UnitResponse); + rpc!(mouse_event, SendMouseEvent, UnitResponse); } diff --git a/src/server/codec.rs b/src/server/codec.rs index c6274ad8e..061ac4c1e 100644 --- a/src/server/codec.rs +++ b/src/server/codec.rs @@ -216,6 +216,8 @@ pdu! { SpawnResponse: 8, WriteToTab: 9, UnitResponse: 10, + SendKeyDown: 11, + SendMouseEvent: 12, } #[derive(Deserialize, Serialize, PartialEq, Debug)] @@ -278,6 +280,18 @@ pub struct WriteToTab { pub data: Vec, } +#[derive(Deserialize, Serialize, PartialEq, Debug)] +pub struct SendKeyDown { + pub tab_id: TabId, + pub event: termwiz::input::KeyEvent, +} + +#[derive(Deserialize, Serialize, PartialEq, Debug)] +pub struct SendMouseEvent { + pub tab_id: TabId, + pub event: term::input::MouseEvent, +} + #[cfg(test)] mod test { use super::*; diff --git a/src/server/listener.rs b/src/server/listener.rs index 8def29fa5..837409e25 100644 --- a/src/server/listener.rs +++ b/src/server/listener.rs @@ -5,7 +5,7 @@ use crate::server::{UnixListener, UnixStream}; use failure::{err_msg, format_err, Error}; #[cfg(unix)] use libc::{mode_t, umask}; -use log::{debug, error}; +use log::{debug, error, warn}; use promise::{Executor, Future}; use std::collections::HashMap; use std::fs::{remove_file, DirBuilder}; @@ -47,6 +47,36 @@ pub struct ClientSession { executor: Box, } +struct BufferedTerminalHost<'a> { + write: std::cell::RefMut<'a, dyn std::io::Write>, + clipboard: Option>, + title: Option, +} + +impl<'a> term::TerminalHost for BufferedTerminalHost<'a> { + fn writer(&mut self) -> &mut dyn std::io::Write { + &mut *self.write + } + + fn click_link(&mut self, link: &Arc) { + error!("ignoring url open of {:?}", link.uri()); + } + + fn get_clipboard(&mut self) -> Result { + warn!("peer requested clipboard; ignoring"); + Ok("".into()) + } + + fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { + self.clipboard.replace(clip); + Ok(()) + } + + fn set_title(&mut self, title: &str) { + self.title.replace(title.to_owned()); + } +} + impl ClientSession { fn new(stream: UnixStream, executor: Box) -> Self { Self { stream, executor } @@ -118,6 +148,36 @@ impl ClientSession { Pdu::UnitResponse(UnitResponse {}).encode(&mut self.stream, decoded.serial)?; } + Pdu::SendKeyDown(SendKeyDown { tab_id, event }) => { + Future::with_executor(self.executor.clone_executor(), move || { + let mux = Mux::get().unwrap(); + let tab = mux + .get_tab(tab_id) + .ok_or_else(|| format_err!("no such tab {}", tab_id))?; + tab.key_down(event.key, event.modifiers)?; + Ok(()) + }) + .wait()?; + Pdu::UnitResponse(UnitResponse {}).encode(&mut self.stream, decoded.serial)?; + } + Pdu::SendMouseEvent(SendMouseEvent { tab_id, event }) => { + Future::with_executor(self.executor.clone_executor(), move || { + let mux = Mux::get().unwrap(); + let tab = mux + .get_tab(tab_id) + .ok_or_else(|| format_err!("no such tab {}", tab_id))?; + let mut host = BufferedTerminalHost { + write: tab.writer(), + clipboard: None, + title: None, + }; + tab.mouse_event(event, &mut host)?; + Ok(()) + }) + .wait()?; + Pdu::UnitResponse(UnitResponse {}).encode(&mut self.stream, decoded.serial)?; + } + Pdu::Spawn(spawn) => { let result = Future::with_executor(self.executor.clone_executor(), move || { let mux = Mux::get().unwrap(); diff --git a/src/server/tab.rs b/src/server/tab.rs index e6d6400a7..9e2909559 100644 --- a/src/server/tab.rs +++ b/src/server/tab.rs @@ -1,9 +1,7 @@ use crate::mux::domain::DomainId; use crate::mux::renderable::Renderable; use crate::mux::tab::{alloc_tab_id, Tab, TabId}; -use crate::server::codec::{ - GetCoarseTabRenderableData, GetCoarseTabRenderableDataResponse, WriteToTab, -}; +use crate::server::codec::*; use crate::server::domain::ClientInner; use failure::{bail, Fallible}; use filedescriptor::Pipe; @@ -17,6 +15,7 @@ use term::color::ColorPalette; use term::{CursorPosition, Line}; use term::{KeyCode, KeyModifiers, MouseEvent, TerminalHost}; use termwiz::hyperlink::Hyperlink; +use termwiz::input::KeyEvent; pub struct ClientTab { client: Arc, @@ -83,11 +82,24 @@ impl Tab for ClientTab { } fn key_down(&self, key: KeyCode, mods: KeyModifiers) -> Fallible<()> { - bail!("ClientTab::key_down not impl"); + let mut client = self.client.client.lock().unwrap(); + client.key_down(SendKeyDown { + tab_id: self.remote_tab_id, + event: KeyEvent { + key: key, + modifiers: mods, + }, + })?; + Ok(()) } fn mouse_event(&self, event: MouseEvent, host: &mut dyn TerminalHost) -> Fallible<()> { - bail!("ClientTab::mouse_event not impl"); + let mut client = self.client.client.lock().unwrap(); + client.mouse_event(SendMouseEvent { + tab_id: self.remote_tab_id, + event, + })?; + Ok(()) } fn advance_bytes(&self, buf: &[u8], host: &mut dyn TerminalHost) { diff --git a/term/src/input.rs b/term/src/input.rs index aca9bfda1..c485ceba4 100644 --- a/term/src/input.rs +++ b/term/src/input.rs @@ -1,14 +1,14 @@ // clippy hates bitflags #![cfg_attr(feature = "cargo-clippy", allow(clippy::suspicious_arithmetic_impl, clippy::redundant_field_names))] -use std::time::{Duration, Instant}; - use super::VisibleRowIndex; +use serde_derive::*; +use std::time::{Duration, Instant}; pub use termwiz::input::KeyCode; pub use termwiz::input::Modifiers as KeyModifiers; -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)] pub enum MouseButton { Left, Middle, @@ -18,14 +18,14 @@ pub enum MouseButton { None, } -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)] pub enum MouseEventKind { Press, Release, Move, } -#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Deserialize, Serialize)] pub struct MouseEvent { pub kind: MouseEventKind, pub x: usize, diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index 6971fa6e8..eb1bd8b2b 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -6,6 +6,7 @@ use crate::escape::{Action, CSI}; use crate::keymap::{Found, KeyMap}; use crate::readbuf::ReadBuffer; use bitflags::bitflags; +use serde_derive::*; use std; #[cfg(windows)] @@ -15,7 +16,7 @@ use winapi::um::wincon::{ }; bitflags! { - #[derive(Default)] + #[derive(Default, Serialize, Deserialize)] pub struct Modifiers: u8 { const NONE = 0; const SHIFT = 1<<1; @@ -25,7 +26,7 @@ bitflags! { } } bitflags! { - #[derive(Default)] + #[derive(Default, Serialize, Deserialize)] pub struct MouseButtons: u8 { const NONE = 0; const LEFT = 1<<1; @@ -55,7 +56,7 @@ pub enum InputEvent { Wake, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct MouseEvent { pub x: u16, pub y: u16, @@ -63,7 +64,7 @@ pub struct MouseEvent { pub modifiers: Modifiers, } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct KeyEvent { /// Which key was pressed pub key: KeyCode, @@ -75,7 +76,7 @@ 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, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub enum KeyCode { /// The decoded unicode character Char(char),