1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 22:33:52 +03:00
wezterm/wezterm-mux-server-impl/src/sessionhandler.rs

740 lines
26 KiB
Rust
Raw Normal View History

use crate::PKI;
use anyhow::anyhow;
2020-10-03 08:39:15 +03:00
use codec::*;
2020-10-02 21:50:50 +03:00
use config::keyassignment::SpawnTabDomain;
2020-10-03 03:42:49 +03:00
use mux::pane::{Pane, PaneId};
use mux::renderable::{RenderableDimensions, StableCursorPosition};
use mux::tab::TabId;
use mux::Mux;
use percent_encoding::percent_decode_str;
use portable_pty::PtySize;
use promise::spawn::spawn_into_main_thread;
use rangeset::RangeSet;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, Mutex};
use std::time::Instant;
use url::Url;
use wezterm_term::terminal::{Clipboard, ClipboardSelection};
use wezterm_term::StableRowIndex;
#[derive(Clone)]
pub struct PduSender {
func: Arc<dyn Fn(DecodedPdu) -> anyhow::Result<()> + Send + Sync>,
}
impl PduSender {
pub fn send(&self, pdu: DecodedPdu) -> anyhow::Result<()> {
(self.func)(pdu)
}
pub fn new<T>(f: T) -> Self
where
T: Fn(DecodedPdu) -> anyhow::Result<()> + Send + Sync + 'static,
{
Self { func: Arc::new(f) }
}
}
#[derive(Default, Debug)]
struct PerPane {
cursor_position: StableCursorPosition,
title: String,
working_dir: Option<Url>,
dimensions: RenderableDimensions,
dirty_lines: RangeSet<StableRowIndex>,
mouse_grabbed: bool,
}
impl PerPane {
fn compute_changes(
&mut self,
pane: &Rc<dyn Pane>,
force_with_input_serial: Option<InputSerial>,
) -> Option<GetPaneRenderChangesResponse> {
let mut changed = false;
let mouse_grabbed = pane.is_mouse_grabbed();
if mouse_grabbed != self.mouse_grabbed {
changed = true;
}
let dims = pane.get_dimensions();
if dims != self.dimensions {
changed = true;
}
let cursor_position = pane.get_cursor_position();
if cursor_position != self.cursor_position {
changed = true;
}
let title = pane.get_title();
if title != self.title {
changed = true;
}
let working_dir = pane.get_current_working_dir();
if working_dir != self.working_dir {
changed = true;
}
let mut all_dirty_lines =
pane.get_dirty_lines(0..dims.physical_top + dims.viewport_rows as StableRowIndex);
let dirty_delta = all_dirty_lines.difference(&self.dirty_lines);
if !dirty_delta.is_empty() {
changed = true;
}
if !changed && !force_with_input_serial.is_some() {
return None;
}
// Figure out what we're going to send as dirty lines vs bonus lines
let viewport_range =
dims.physical_top..dims.physical_top + dims.viewport_rows as StableRowIndex;
let (first_line, lines) = pane.get_lines(viewport_range);
let mut bonus_lines = lines
.into_iter()
.enumerate()
.map(|(idx, line)| {
let stable_row = first_line + idx as StableRowIndex;
all_dirty_lines.remove(stable_row);
(stable_row, line)
})
.collect::<Vec<_>>();
// Always send the cursor's row, as that tends to the busiest and we don't
// have a sequencing concept for our idea of the remote state.
let (cursor_line, lines) = pane.get_lines(cursor_position.y..cursor_position.y + 1);
bonus_lines.push((cursor_line, lines[0].clone()));
self.cursor_position = cursor_position;
self.title = title.clone();
self.working_dir = working_dir.clone();
self.dimensions = dims;
self.dirty_lines = all_dirty_lines;
self.mouse_grabbed = mouse_grabbed;
let dirty_lines = dirty_delta.iter().cloned().collect();
let bonus_lines = bonus_lines.into();
Some(GetPaneRenderChangesResponse {
pane_id: pane.pane_id(),
mouse_grabbed,
dirty_lines,
dimensions: dims,
cursor_position,
title,
bonus_lines,
working_dir: working_dir.map(Into::into),
input_serial: force_with_input_serial,
})
}
fn mark_clean(&mut self, stable_row: StableRowIndex) {
self.dirty_lines.remove(stable_row);
}
}
fn maybe_push_pane_changes(
pane: &Rc<dyn Pane>,
sender: PduSender,
per_pane: Arc<Mutex<PerPane>>,
) -> anyhow::Result<()> {
let mut per_pane = per_pane.lock().unwrap();
if let Some(resp) = per_pane.compute_changes(pane, None) {
sender.send(DecodedPdu {
pdu: Pdu::GetPaneRenderChangesResponse(resp),
serial: 0,
})?;
}
Ok(())
}
pub struct SessionHandler {
to_write_tx: PduSender,
per_pane: HashMap<TabId, Arc<Mutex<PerPane>>>,
}
impl SessionHandler {
pub fn new(to_write_tx: PduSender) -> Self {
Self {
to_write_tx,
per_pane: HashMap::new(),
}
}
fn per_pane(&mut self, pane_id: PaneId) -> Arc<Mutex<PerPane>> {
Arc::clone(
self.per_pane
.entry(pane_id)
.or_insert_with(|| Arc::new(Mutex::new(PerPane::default()))),
)
}
pub fn schedule_pane_push(&mut self, pane_id: PaneId) {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
maybe_push_pane_changes(&pane, sender, per_pane)?;
Ok::<(), anyhow::Error>(())
2020-10-05 10:06:01 +03:00
})
.detach();
}
pub fn process_one(&mut self, decoded: DecodedPdu) {
let start = Instant::now();
let sender = self.to_write_tx.clone();
let serial = decoded.serial;
let send_response = move |result: anyhow::Result<Pdu>| {
let pdu = match result {
Ok(pdu) => pdu,
Err(err) => Pdu::ErrorResponse(ErrorResponse {
reason: format!("Error: {}", err),
}),
};
log::trace!("{} processing time {:?}", serial, start.elapsed());
sender.send(DecodedPdu { pdu, serial }).ok();
};
fn catch<F, SND>(f: F, send_response: SND)
where
F: FnOnce() -> anyhow::Result<Pdu>,
SND: Fn(anyhow::Result<Pdu>),
{
send_response(f());
}
match decoded.pdu {
Pdu::Ping(Ping {}) => send_response(Ok(Pdu::Pong(Pong {}))),
Pdu::ListPanes(ListPanes {}) => {
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let mut tabs = vec![];
for window_id in mux.iter_windows().into_iter() {
let window = mux.get_window(window_id).unwrap();
for tab in window.iter() {
tabs.push(tab.codec_pane_tree());
}
}
2020-12-30 03:33:58 +03:00
log::trace!("ListPanes {:#?}", tabs);
Ok(Pdu::ListPanesResponse(ListPanesResponse { tabs }))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::WriteToPane(WriteToPane { pane_id, data }) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.writer().write_all(&data)?;
maybe_push_pane_changes(&pane, sender, per_pane)?;
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
);
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::KillPane(KillPane { pane_id }) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.kill();
mux.remove_pane(pane_id);
maybe_push_pane_changes(&pane, sender, per_pane)?;
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
);
})
.detach();
}
Pdu::SendPaste(SendPaste { pane_id, data }) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.send_paste(&data)?;
maybe_push_pane_changes(&pane, sender, per_pane)?;
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::SearchScrollbackRequest(SearchScrollbackRequest { pane_id, pattern }) => {
2020-10-03 03:42:49 +03:00
use mux::pane::Pattern;
async fn do_search(pane_id: TabId, pattern: Pattern) -> anyhow::Result<Pdu> {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.search(pattern).await.map(|results| {
Pdu::SearchScrollbackResponse(SearchScrollbackResponse { results })
})
}
spawn_into_main_thread(async move {
promise::spawn::spawn(async move {
let result = do_search(pane_id, pattern).await;
send_response(result);
2020-10-05 10:06:01 +03:00
})
.detach();
})
.detach();
}
Pdu::SetPaneZoomed(SetPaneZoomed {
containing_tab_id,
pane_id,
zoomed,
}) => {
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
let tab = mux
.get_tab(containing_tab_id)
.ok_or_else(|| anyhow!("no such tab {}", containing_tab_id))?;
tab.set_active_pane(&pane);
tab.set_zoomed(zoomed);
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::Resize(Resize {
containing_tab_id,
pane_id,
size,
}) => {
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.resize(size)?;
let tab = mux
.get_tab(containing_tab_id)
.ok_or_else(|| anyhow!("no such tab {}", containing_tab_id))?;
tab.rebuild_splits_sizes_from_contained_panes();
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::SendKeyDown(SendKeyDown {
pane_id,
event,
input_serial,
}) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.key_down(event.key, event.modifiers)?;
// For a key press, we want to always send back the
// cursor position so that the predictive echo doesn't
// leave the cursor in the wrong place
let mut per_pane = per_pane.lock().unwrap();
if let Some(resp) = per_pane.compute_changes(&pane, Some(input_serial))
{
sender.send(DecodedPdu {
pdu: Pdu::GetPaneRenderChangesResponse(resp),
serial: 0,
})?;
}
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::SendMouseEvent(SendMouseEvent { pane_id, event }) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
pane.mouse_event(event)?;
maybe_push_pane_changes(&pane, sender, per_pane)?;
Ok(Pdu::UnitResponse(UnitResponse {}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::Spawn(spawn) => {
let sender = self.to_write_tx.clone();
spawn_into_main_thread(async move {
schedule_domain_spawn(spawn, sender, send_response);
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::SpawnV2(spawn) => {
let sender = self.to_write_tx.clone();
spawn_into_main_thread(async move {
schedule_domain_spawn_v2(spawn, sender, send_response);
})
.detach();
}
Pdu::SplitPane(split) => {
let sender = self.to_write_tx.clone();
spawn_into_main_thread(async move {
schedule_split_pane(split, sender, send_response);
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::GetPaneRenderChanges(GetPaneRenderChanges { pane_id, .. }) => {
let sender = self.to_write_tx.clone();
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let is_alive = match mux.get_pane(pane_id) {
Some(pane) => {
maybe_push_pane_changes(&pane, sender, per_pane)?;
true
}
None => false,
};
Ok(Pdu::LivenessResponse(LivenessResponse {
pane_id,
is_alive,
}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::GetLines(GetLines { pane_id, lines }) => {
let per_pane = self.per_pane(pane_id);
spawn_into_main_thread(async move {
catch(
move || {
let mux = Mux::get().unwrap();
let pane = mux
.get_pane(pane_id)
.ok_or_else(|| anyhow!("no such pane {}", pane_id))?;
let mut lines_and_indices = vec![];
let mut per_pane = per_pane.lock().unwrap();
for range in lines {
let (first_row, lines) = pane.get_lines(range);
for (idx, line) in lines.into_iter().enumerate() {
let stable_row = first_row + idx as StableRowIndex;
per_pane.mark_clean(stable_row);
lines_and_indices.push((stable_row, line));
}
}
Ok(Pdu::GetLinesResponse(GetLinesResponse {
pane_id,
lines: lines_and_indices.into(),
}))
},
send_response,
)
2020-10-05 10:06:01 +03:00
})
.detach();
}
Pdu::GetCodecVersion(_) => {
send_response(Ok(Pdu::GetCodecVersionResponse(GetCodecVersionResponse {
codec_vers: CODEC_VERSION,
version_string: config::wezterm_version().to_owned(),
})))
}
Pdu::GetTlsCreds(_) => {
catch(
move || {
let client_cert_pem = PKI.generate_client_cert()?;
let ca_cert_pem = PKI.ca_pem_string()?;
Ok(Pdu::GetTlsCredsResponse(GetTlsCredsResponse {
client_cert_pem,
ca_cert_pem,
}))
},
send_response,
);
}
Pdu::Invalid { .. } => send_response(Err(anyhow!("invalid PDU {:?}", decoded.pdu))),
Pdu::Pong { .. }
| Pdu::ListPanesResponse { .. }
| Pdu::SetClipboard { .. }
| Pdu::SpawnResponse { .. }
| Pdu::GetPaneRenderChangesResponse { .. }
| Pdu::UnitResponse { .. }
| Pdu::LivenessResponse { .. }
| Pdu::SearchScrollbackResponse { .. }
| Pdu::GetLinesResponse { .. }
| Pdu::GetCodecVersionResponse { .. }
| Pdu::GetTlsCredsResponse { .. }
| Pdu::ErrorResponse { .. } => {
send_response(Err(anyhow!("expected a request, got {:?}", decoded.pdu)))
}
}
}
}
// Dancing around a little bit here; we can't directly spawn_into_main_thread the domain_spawn
// function below because the compiler thinks that all of its locals then need to be Send.
// We need to shimmy through this helper to break that aspect of the compiler flow
// analysis and allow things to compile.
fn schedule_domain_spawn<SND>(spawn: Spawn, sender: PduSender, send_response: SND)
where
SND: Fn(anyhow::Result<Pdu>) + 'static,
{
2020-10-05 10:06:01 +03:00
promise::spawn::spawn(async move { send_response(domain_spawn(spawn, sender).await) }).detach();
}
fn schedule_domain_spawn_v2<SND>(spawn: SpawnV2, sender: PduSender, send_response: SND)
where
SND: Fn(anyhow::Result<Pdu>) + 'static,
{
promise::spawn::spawn(async move { send_response(domain_spawn_v2(spawn, sender).await) })
.detach();
}
fn schedule_split_pane<SND>(split: SplitPane, sender: PduSender, send_response: SND)
where
SND: Fn(anyhow::Result<Pdu>) + 'static,
{
2020-10-05 10:06:01 +03:00
promise::spawn::spawn(async move { send_response(split_pane(split, sender).await) }).detach();
}
struct RemoteClipboard {
sender: PduSender,
pane_id: TabId,
}
impl Clipboard for RemoteClipboard {
fn get_contents(&self, _selection: ClipboardSelection) -> anyhow::Result<String> {
Ok("".to_owned())
}
fn set_contents(
&self,
selection: ClipboardSelection,
clipboard: Option<String>,
) -> anyhow::Result<()> {
self.sender.send(DecodedPdu {
serial: 0,
pdu: Pdu::SetClipboard(SetClipboard {
pane_id: self.pane_id,
clipboard,
selection,
}),
})?;
Ok(())
}
}
async fn split_pane(split: SplitPane, sender: PduSender) -> anyhow::Result<Pdu> {
let mux = Mux::get().unwrap();
let (pane_domain_id, window_id, tab_id) = mux
.resolve_pane_id(split.pane_id)
.ok_or_else(|| anyhow!("pane_id {} invalid", split.pane_id))?;
let domain = match split.domain {
SpawnTabDomain::DefaultDomain => mux.default_domain(),
SpawnTabDomain::CurrentPaneDomain => mux
.get_domain(pane_domain_id)
.expect("resolve_pane_id to give valid domain_id"),
SpawnTabDomain::DomainName(name) => mux
.get_domain_by_name(&name)
.ok_or_else(|| anyhow!("domain name {} is invalid", name))?,
};
let pane_id = split.pane_id;
let cwd = split.command_dir.or_else(|| {
mux.get_pane(pane_id)
.and_then(|pane| pane.get_current_working_dir())
.and_then(|url| {
percent_decode_str(url.path())
.decode_utf8()
.ok()
.map(|path| path.into_owned())
})
.map(|path| {
// On Windows the file URI can produce a path like:
// `/C:\Users` which is valid in a file URI, but the leading slash
// is not liked by the windows file APIs, so we strip it off here.
let bytes = path.as_bytes();
if bytes.len() > 2 && bytes[0] == b'/' && bytes[2] == b':' {
path[1..].to_owned()
} else {
path
}
})
});
let pane = domain
.split_pane(split.command, cwd, tab_id, split.pane_id, split.direction)
.await?;
let dims = pane.get_dimensions();
let size = PtySize {
cols: dims.cols as u16,
rows: dims.viewport_rows as u16,
pixel_height: 0,
pixel_width: 0,
};
let clip: Arc<dyn Clipboard> = Arc::new(RemoteClipboard {
pane_id: pane.pane_id(),
sender,
});
pane.set_clipboard(&clip);
Ok::<Pdu, anyhow::Error>(Pdu::SpawnResponse(SpawnResponse {
pane_id: pane.pane_id(),
tab_id: tab_id,
window_id,
size,
}))
}
async fn domain_spawn(spawn: Spawn, sender: PduSender) -> anyhow::Result<Pdu> {
let mux = Mux::get().unwrap();
let domain = mux
.get_domain(spawn.domain_id)
.ok_or_else(|| anyhow!("domain {} not found on this server", spawn.domain_id))?;
let window_builder;
let window_id = if let Some(window_id) = spawn.window_id {
mux.get_window_mut(window_id)
.ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?;
window_id
} else {
window_builder = mux.new_empty_window();
*window_builder
};
let tab = domain
.spawn(spawn.size, spawn.command, spawn.command_dir, window_id)
.await?;
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("missing active pane on tab!?"))?;
let clip: Arc<dyn Clipboard> = Arc::new(RemoteClipboard {
pane_id: pane.pane_id(),
sender,
});
pane.set_clipboard(&clip);
Ok::<Pdu, anyhow::Error>(Pdu::SpawnResponse(SpawnResponse {
pane_id: pane.pane_id(),
tab_id: tab.tab_id(),
window_id,
size: tab.get_size(),
}))
}
async fn domain_spawn_v2(spawn: SpawnV2, sender: PduSender) -> anyhow::Result<Pdu> {
let mux = Mux::get().unwrap();
let domain = match spawn.domain {
SpawnTabDomain::DefaultDomain => mux.default_domain(),
SpawnTabDomain::CurrentPaneDomain => anyhow::bail!("must give a domain"),
SpawnTabDomain::DomainName(name) => mux
.get_domain_by_name(&name)
.ok_or_else(|| anyhow!("domain name {} is invalid", name))?,
};
let window_builder;
let window_id = if let Some(window_id) = spawn.window_id {
mux.get_window_mut(window_id)
.ok_or_else(|| anyhow!("window_id {} not found on this server", window_id))?;
window_id
} else {
window_builder = mux.new_empty_window();
*window_builder
};
let tab = domain
.spawn(spawn.size, spawn.command, spawn.command_dir, window_id)
.await?;
let pane = tab
.get_active_pane()
.ok_or_else(|| anyhow!("missing active pane on tab!?"))?;
let clip: Arc<dyn Clipboard> = Arc::new(RemoteClipboard {
pane_id: pane.pane_id(),
sender,
});
pane.set_clipboard(&clip);
Ok::<Pdu, anyhow::Error>(Pdu::SpawnResponse(SpawnResponse {
pane_id: pane.pane_id(),
tab_id: tab.tab_id(),
window_id,
size: tab.get_size(),
}))
}