diff --git a/src/main.rs b/src/main.rs index b3b4952e0..607d756cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -406,7 +406,7 @@ fn run_ssh(config: config::ConfigHandle, opts: &SshCommand) -> anyhow::Result<() let mux = Mux::get().unwrap(); mux.add_domain(&domain); mux.set_default_domain(&domain); - domain.attach()?; + domain.attach(); let window_id = mux.new_empty_window(); let tab = domain.spawn(PtySize::default(), cmd, window_id)?; @@ -438,7 +438,7 @@ fn run_serial(config: config::ConfigHandle, opts: &SerialCommand) -> anyhow::Res let front_end = opts.front_end.unwrap_or(config.front_end); let gui = front_end.try_new()?; - domain.attach()?; + domain.attach(); let window_id = mux.new_empty_window(); let tab = domain.spawn(PtySize::default(), None, window_id)?; @@ -474,35 +474,41 @@ fn run_mux_client(config: config::ConfigHandle, opts: &ConnectCommand) -> anyhow ) })?; - let fontconfig = Rc::new(FontConfiguration::new()); - let domain: Arc = Arc::new(ClientDomain::new(client_config)); let mux = Rc::new(mux::Mux::new(Some(domain.clone()))); Mux::set_mux(&mux); - let front_end = opts.front_end.unwrap_or(config.front_end); - let gui = front_end.try_new()?; - domain.attach()?; + let front_end_selection = opts.front_end.unwrap_or(config.front_end); + let gui = front_end_selection.try_new()?; + let opts = opts.clone(); + domain.attach().map(move |_| { + Future::with_executor(executor(), move || { + let mux = Mux::get().unwrap(); - if mux.is_empty() { - let cmd = if !opts.prog.is_empty() { - let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect(); - let mut builder = CommandBuilder::new(&argv[0]); - builder.args(&argv[1..]); - Some(builder) - } else { - None - }; - let window_id = mux.new_empty_window(); - let tab = mux - .default_domain() - .spawn(PtySize::default(), cmd, window_id)?; - gui.spawn_new_window(&fontconfig, &tab, window_id)?; - } + if mux.is_empty() { + let cmd = if !opts.prog.is_empty() { + let argv: Vec<&std::ffi::OsStr> = + opts.prog.iter().map(|x| x.as_os_str()).collect(); + let mut builder = CommandBuilder::new(&argv[0]); + builder.args(&argv[1..]); + Some(builder) + } else { + None + }; + let window_id = mux.new_empty_window(); + let tab = mux + .default_domain() + .spawn(PtySize::default(), cmd, window_id)?; + let fontconfig = Rc::new(FontConfiguration::new()); + front_end() + .unwrap() + .spawn_new_window(&fontconfig, &tab, window_id)?; + } - for dom in mux.iter_domains() { - log::info!("domain {} state {:?}", dom.domain_id(), dom.state()); - } + Ok(()) + }); + Ok(()) + }); gui.run_forever() } @@ -553,8 +559,6 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: &StartCommand) -> anyhow .unwrap_or(config.font_rasterizer) .set_default(); - let fontconfig = Rc::new(FontConfiguration::new()); - let cmd = if !opts.prog.is_empty() { let argv: Vec<&std::ffi::OsStr> = opts.prog.iter().map(|x| x.as_os_str()).collect(); let mut builder = CommandBuilder::new(&argv[0]); @@ -568,9 +572,29 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: &StartCommand) -> anyhow let mux = Rc::new(mux::Mux::new(Some(domain.clone()))); Mux::set_mux(&mux); - let front_end = opts.front_end.unwrap_or(config.front_end); - let gui = front_end.try_new()?; - domain.attach()?; + let front_end_selection = opts.front_end.unwrap_or(config.front_end); + let gui = front_end_selection.try_new()?; + domain.attach().map(move |_| { + Future::with_executor(executor(), move || { + let mux = Mux::get().unwrap(); + + if mux.is_empty() { + if mux.is_empty() { + let window_id = mux.new_empty_window(); + let tab = mux + .default_domain() + .spawn(PtySize::default(), cmd, window_id)?; + let fontconfig = Rc::new(FontConfiguration::new()); + front_end() + .unwrap() + .spawn_new_window(&fontconfig, &tab, window_id)?; + } + } + Ok(()) + }); + + Ok(()) + }); fn record_domain(mux: &Rc, client: ClientDomain) -> anyhow::Result> { let domain: Arc = Arc::new(client); @@ -578,28 +602,16 @@ fn run_terminal_gui(config: config::ConfigHandle, opts: &StartCommand) -> anyhow Ok(domain) } - if front_end != FrontEndSelection::MuxServer && !opts.no_auto_connect { + if front_end_selection != FrontEndSelection::MuxServer && !opts.no_auto_connect { for client_config in client_domains(&config) { let connect_automatically = client_config.connect_automatically(); let dom = record_domain(&mux, ClientDomain::new(client_config))?; if connect_automatically { - dom.attach()?; + dom.attach(); } } } - if mux.is_empty() { - let window_id = mux.new_empty_window(); - let tab = mux - .default_domain() - .spawn(PtySize::default(), cmd, window_id)?; - gui.spawn_new_window(&fontconfig, &tab, window_id)?; - } - - for dom in mux.iter_domains() { - log::info!("domain {} state {:?}", dom.domain_id(), dom.state()); - } - gui.run_forever() } diff --git a/src/mux/domain.rs b/src/mux/domain.rs index 6de37b8e9..95cd131a6 100644 --- a/src/mux/domain.rs +++ b/src/mux/domain.rs @@ -15,6 +15,7 @@ use downcast_rs::{impl_downcast, Downcast}; use log::info; use portable_pty::cmdbuilder::CommandBuilder; use portable_pty::{PtySize, PtySystem}; +use promise::Future; use std::rc::Rc; static DOMAIN_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0); @@ -47,7 +48,7 @@ pub trait Domain: Downcast { fn domain_name(&self) -> &str; /// Re-attach to any tabs that might be pre-existing in this domain - fn attach(&self) -> anyhow::Result<()>; + fn attach(&self) -> Future<()>; /// Detach all tabs fn detach(&self) -> anyhow::Result<()>; @@ -120,8 +121,8 @@ impl Domain for LocalDomain { &self.name } - fn attach(&self) -> anyhow::Result<()> { - Ok(()) + fn attach(&self) -> Future<()> { + Future::ok(()) } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/mux/mod.rs b/src/mux/mod.rs index 18cbdc663..4f83e8640 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -310,6 +310,7 @@ impl Mux { self.windows.borrow().keys().cloned().collect() } + #[allow(unused)] pub fn iter_domains(&self) -> Vec> { self.domains.borrow().values().cloned().collect() } diff --git a/src/server/domain.rs b/src/server/domain.rs index f5ca0951e..70c62405c 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -1,6 +1,6 @@ use crate::config::{SshDomain, TlsDomainClient, UnixDomain}; use crate::font::FontConfiguration; -use crate::frontend::front_end; +use crate::frontend::{executor, front_end}; use crate::mux::domain::{alloc_domain_id, Domain, DomainId, DomainState}; use crate::mux::tab::{Tab, TabId}; use crate::mux::window::WindowId; @@ -10,6 +10,7 @@ use crate::server::codec::Spawn; use crate::server::tab::ClientTab; use anyhow::{anyhow, bail}; use portable_pty::{CommandBuilder, PtySize}; +use promise::{Future, Promise}; use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; @@ -144,6 +145,59 @@ impl ClientDomain { } None } + + fn finish_attach(domain_id: DomainId, client: Client) -> anyhow::Result<()> { + let mux = Mux::get().unwrap(); + let domain = mux + .get_domain(domain_id) + .ok_or_else(|| anyhow!("invalid domain id {}", domain_id))?; + let domain = domain + .downcast_ref::() + .ok_or_else(|| anyhow!("domain {} is not a ClientDomain", domain_id))?; + + let inner = Arc::new(ClientInner::new(domain_id, client)); + *domain.inner.borrow_mut() = Some(Arc::clone(&inner)); + + let tabs = inner.client.list_tabs().wait()?; + log::debug!("ListTabs result {:#?}", tabs); + + for entry in tabs.tabs.iter() { + log::info!( + "attaching to remote tab {} in remote window {} {}", + entry.tab_id, + entry.window_id, + entry.title + ); + let tab: Rc = Rc::new(ClientTab::new( + &inner, + entry.tab_id, + entry.size, + &entry.title, + )); + mux.add_tab(&tab)?; + + if let Some(local_window_id) = inner.remote_to_local_window(entry.window_id) { + let mut window = mux + .get_window_mut(local_window_id) + .expect("no such window!?"); + log::info!("already have a local window for this one"); + window.push(&tab); + } else { + log::info!("spawn new local window"); + let fonts = Rc::new(FontConfiguration::new()); + let local_window_id = mux.new_empty_window(); + inner.record_remote_to_local_window_mapping(entry.window_id, local_window_id); + mux.add_tab_to_window(&tab, local_window_id)?; + + front_end() + .unwrap() + .spawn_new_window(&fonts, &tab, local_window_id) + .unwrap(); + } + } + + Ok(()) + } } impl Domain for ClientDomain { @@ -187,59 +241,37 @@ impl Domain for ClientDomain { Ok(tab) } - fn attach(&self) -> anyhow::Result<()> { - let mux = Mux::get().unwrap(); - let client = match &self.config { - ClientDomainConfig::Unix(unix) => { - let initial = true; - Client::new_unix_domain(self.local_domain_id, unix, initial)? + fn attach(&self) -> Future<()> { + let domain_id = self.local_domain_id; + let config = self.config.clone(); + + let mut promise = Promise::new(); + let future = promise.get_future().unwrap(); + let activity = crate::frontend::activity::Activity::new(); + + std::thread::spawn(move || { + let client = match &config { + ClientDomainConfig::Unix(unix) => { + let initial = true; + Client::new_unix_domain(domain_id, unix, initial) + } + ClientDomainConfig::Tls(tls) => Client::new_tls(domain_id, tls), + ClientDomainConfig::Ssh(ssh) => Client::new_ssh(domain_id, ssh), + }; + + match client { + Err(err) => promise.result(Err(err)), + Ok(client) => { + Future::with_executor(executor(), move || { + promise.result(ClientDomain::finish_attach(domain_id, client)); + drop(activity); + Ok(()) + }); + } } - ClientDomainConfig::Tls(tls) => Client::new_tls(self.local_domain_id, tls)?, - ClientDomainConfig::Ssh(ssh) => Client::new_ssh(self.local_domain_id, ssh)?, - }; + }); - let inner = Arc::new(ClientInner::new(self.local_domain_id, client)); - *self.inner.borrow_mut() = Some(Arc::clone(&inner)); - - let tabs = inner.client.list_tabs().wait()?; - log::debug!("ListTabs result {:#?}", tabs); - - for entry in tabs.tabs.iter() { - log::info!( - "attaching to remote tab {} in remote window {} {}", - entry.tab_id, - entry.window_id, - entry.title - ); - let tab: Rc = Rc::new(ClientTab::new( - &inner, - entry.tab_id, - entry.size, - &entry.title, - )); - mux.add_tab(&tab)?; - - if let Some(local_window_id) = inner.remote_to_local_window(entry.window_id) { - let mut window = mux - .get_window_mut(local_window_id) - .expect("no such window!?"); - log::info!("already have a local window for this one"); - window.push(&tab); - } else { - log::info!("spawn new local window"); - let fonts = Rc::new(FontConfiguration::new()); - let local_window_id = mux.new_empty_window(); - inner.record_remote_to_local_window_mapping(entry.window_id, local_window_id); - mux.add_tab_to_window(&tab, local_window_id)?; - - front_end() - .unwrap() - .spawn_new_window(&fonts, &tab, local_window_id) - .unwrap(); - } - } - - Ok(()) + future } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/ssh.rs b/src/ssh.rs index 10d795426..619acd6a7 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -363,8 +363,8 @@ impl Domain for RemoteSshDomain { &self.name } - fn attach(&self) -> anyhow::Result<()> { - Ok(()) + fn attach(&self) -> promise::Future<()> { + promise::Future::ok(()) } fn detach(&self) -> anyhow::Result<()> { diff --git a/src/termwiztermtab.rs b/src/termwiztermtab.rs index c0b816171..8ebc28ae7 100644 --- a/src/termwiztermtab.rs +++ b/src/termwiztermtab.rs @@ -162,8 +162,8 @@ impl Domain for TermWizTerminalDomain { fn domain_name(&self) -> &str { "TermWizTerminalDomain" } - fn attach(&self) -> anyhow::Result<()> { - Ok(()) + fn attach(&self) -> promise::Future<()> { + promise::Future::ok(()) } fn detach(&self) -> anyhow::Result<()> {