diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index eca7c2b06..2ce8f8048 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -21,6 +21,7 @@ use term::{MouseButton, MouseEventKind}; use termwiz::hyperlink::Hyperlink; struct Host { + event_loop: Rc, display: glium::Display, pty: MasterPty, clipboard: Clipboard, @@ -100,21 +101,12 @@ impl term::TerminalHost for Host { } fn new_window(&mut self) { - /* - use super::spawn_window; - use futures; - println!("open a new one!"); - let event_loop = Rc::clone(&self.event_loop); - let config = Rc::clone(&self.config); - let fonts = Rc::clone(&self.renderer.fonts); + self.event_loop.request_spawn_window().ok(); + } + fn new_tab(&mut self) { self.event_loop - .core - .spawn(futures::future::poll_fn(move || { - spawn_window(&event_loop, None, &config, &fonts) - .map(futures::Async::Ready) - .map_err(|_| ()) - })); - */ + .request_spawn_tab(self.display.gl_window().id()) + .ok(); } } @@ -177,6 +169,7 @@ impl TerminalWindow { let window_position = display.gl_window().get_position(); let host = Host { + event_loop: Rc::clone(event_loop), display, pty, clipboard: Clipboard::default(), diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 4ed8eff68..d6b2ada55 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -1,7 +1,12 @@ +use crate::config::Config; +use crate::font::FontConfiguration; +pub use crate::gliumwindows::TerminalWindow; +use crate::guiloop::SessionTerminated; use failure::Error; use futures::future; use glium; use glium::glutin::EventsLoopProxy; +pub use glium::glutin::WindowId; use std::cell::RefCell; use std::collections::HashMap; use std::io::Read; @@ -10,10 +15,6 @@ use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError}; use std::thread; use std::time::{Duration, SystemTime}; -pub use crate::gliumwindows::TerminalWindow; -use crate::guiloop::SessionTerminated; -pub use glium::glutin::WindowId; - use crate::futurecore; use crate::gliumwindows; #[cfg(unix)] @@ -45,7 +46,7 @@ pub fn channel(proxy: EventsLoopProxy) -> (GuiSender, Receiver) { } #[derive(Clone)] -enum IOEvent { +pub enum IOEvent { Data { window_id: WindowId, data: Vec }, Terminated { window_id: WindowId }, } @@ -57,6 +58,11 @@ struct Windows { by_id: HashMap, } +enum SpawnRequest { + Window, + Tab(WindowId), +} + /// The `GuiEventLoop` represents the combined gui event processor, /// a remote (running on another thread) mio `Poll` instance, and /// a core for spawning tasks from futures. It acts as the manager @@ -67,6 +73,8 @@ pub struct GuiEventLoop { pub core: futurecore::Core, poll_tx: GuiSender, poll_rx: Receiver, + spawn_tx: GuiSender, + spawn_rx: Receiver, #[cfg(unix)] sigchld_rx: Receiver<()>, tick_rx: Receiver<()>, @@ -82,6 +90,7 @@ impl GuiEventLoop { let (fut_tx, fut_rx) = channel(event_loop.create_proxy()); let core = futurecore::Core::new(fut_tx, fut_rx); + let (spawn_tx, spawn_rx) = channel(event_loop.create_proxy()); let (poll_tx, poll_rx) = channel(event_loop.create_proxy()); #[cfg(unix)] let (sigchld_tx, sigchld_rx) = channel(event_loop.create_proxy()); @@ -101,6 +110,8 @@ impl GuiEventLoop { Ok(Self { core, + spawn_tx, + spawn_rx, poll_tx, poll_rx, tick_rx, @@ -111,6 +122,14 @@ impl GuiEventLoop { }) } + pub fn request_spawn_window(&self) -> Result<(), Error> { + self.spawn_tx.send(SpawnRequest::Window) + } + + pub fn request_spawn_tab(&self, window_id: WindowId) -> Result<(), Error> { + self.spawn_tx.send(SpawnRequest::Tab(window_id)) + } + /// Add a window to the event loop and run it. pub fn add_window(&self, window: gliumwindows::TerminalWindow) -> Result<(), Error> { let window_id = window.window_id(); @@ -220,10 +239,10 @@ impl GuiEventLoop { match self.poll_rx.try_recv() { Ok(IOEvent::Data { window_id, data }) => { let mut windows = self.windows.borrow_mut(); - let window = windows.by_id.get_mut(&window_id).ok_or_else(|| { - format_err!("window_id {:?} not in windows_by_id map", window_id) - })?; - window.process_data_read_from_pty(&data); + windows.by_id.get_mut(&window_id).map(|window| { + window.process_data_read_from_pty(&data); + Some(()) + }); } Ok(IOEvent::Terminated { window_id }) => { self.schedule_window_close(window_id)?; @@ -316,27 +335,51 @@ impl GuiEventLoop { } } + fn process_spawn( + myself: &Rc, + config: &Rc, + fonts: &Rc, + ) -> Result<(), Error> { + loop { + match myself.spawn_rx.try_recv() { + Ok(SpawnRequest::Window) => { + crate::spawn_window(myself, None, config, fonts)?; + } + Ok(SpawnRequest::Tab(_window_id)) => { + eprintln!("Spawning tabs is not yet implemented for glutin"); + } + Err(TryRecvError::Empty) => return Ok(()), + Err(err) => bail!("sigchld_rx disconnected {:?}", err), + } + } + } + /// Run the event loop. Does not return until there is either a fatal /// error, or until there are no more windows left to manage. - pub fn run(&self) -> Result<(), Error> { + pub fn run( + myself: &Rc, + config: &Rc, + fonts: &Rc, + ) -> Result<(), Error> { loop { - self.process_futures(); + myself.process_futures(); // Check the window count; if after processing the futures there // are no windows left, then we are done. { - let windows = self.windows.borrow(); + let windows = myself.windows.borrow(); if windows.by_id.is_empty() { debug!("No more windows; done!"); return Ok(()); } } - self.run_event_loop()?; - self.process_poll()?; + myself.run_event_loop()?; + myself.process_poll()?; + Self::process_spawn(myself, config, fonts)?; #[cfg(unix)] - self.process_sigchld()?; - self.process_tick()?; + myself.process_sigchld()?; + myself.process_tick()?; } } } diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index a5a753710..4ccefa06d 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -105,7 +105,15 @@ impl GuiEventLoop { }) } - pub fn run(&self) -> Result<(), Error> { + pub fn run( + myself: &Rc, + _config: &Rc, + _fonts: &Rc, + ) -> Result<(), Error> { + myself.run_impl() + } + + fn run_impl(&self) -> Result<(), Error> { let mut events = Events::with_capacity(8); let tok_core = Token(TOK_CORE); diff --git a/src/main.rs b/src/main.rs index e6b0b3dc5..8751cc811 100644 --- a/src/main.rs +++ b/src/main.rs @@ -114,8 +114,9 @@ fn main() -> Result<(), Error> { let event_loop = Rc::new(GuiEventLoop::new()?); spawn_window(&event_loop, cmd, &config, &fontconfig)?; - - event_loop.run()?; + // This convoluted run() signature is present because of this issue: + // https://github.com/tomaka/winit/issues/413 + GuiEventLoop::run(&event_loop, &config, &fontconfig)?; Ok(()) }