From 4f73904be4e4fe705f67f2a19f87de4e7f1feec6 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 23 Feb 2019 16:39:38 -0800 Subject: [PATCH] remove SpawnRequest in favor of using the futurecore This wasn't hooked up right, partly because the glutin backend bit rotted and I forgot that there was a future core. This simplifies dispatching of requests that need to happen outside of the gui loop control. --- src/gliumwindows.rs | 79 ++++++++++++------ src/guiloop/glutinloop.rs | 171 +++++++------------------------------- src/guiloop/mod.rs | 6 +- src/guiloop/x11.rs | 6 +- src/main.rs | 18 +++- 5 files changed, 97 insertions(+), 183 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 82da2b63c..ed5f183ae 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -7,7 +7,7 @@ use crate::guiloop::glutinloop::GuiEventLoop; use crate::guiloop::SessionTerminated; use crate::opengl::render::Renderer; use crate::opengl::textureatlas::OutOfTextureSpace; -use crate::{openpty, Child, MasterPty}; +use crate::{openpty, spawn_window_impl, Child, MasterPty}; use clipboard::{ClipboardContext, ClipboardProvider}; use glium; use glium::glutin::dpi::{LogicalPosition, LogicalSize, PhysicalPosition}; @@ -42,6 +42,8 @@ struct Host { /// if is_some, holds position to be restored after exiting /// fullscreen mode. is_fullscreen: Option, + config: Rc, + fonts: Rc, } /// macOS gets unhappy if we set up the clipboard too early, @@ -128,47 +130,70 @@ impl<'a> term::TerminalHost for TabHost<'a> { } fn new_window(&mut self) { - self.host.event_loop.request_spawn_window().ok(); + let event_loop = Rc::clone(&self.host.event_loop); + let config = Rc::clone(&self.host.config); + let fonts = Rc::clone(&self.host.fonts); + self.host.event_loop.spawn_fn(move || { + let (terminal, master, child) = spawn_window_impl(None, &config, &fonts)?; + let window = + TerminalWindow::new(&event_loop, terminal, master, child, &fonts, &config)?; + + event_loop.add_window(window) + }); } + fn new_tab(&mut self) { - self.host - .event_loop - .request_spawn_tab(self.host.display.gl_window().id()) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + |win| win.spawn_tab(), + ); } + fn activate_tab(&mut self, tab: usize) { - self.host - .event_loop - .request_activate_tab(self.host.display.gl_window().id(), tab) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + move |win| win.activate_tab(tab), + ); } fn activate_tab_relative(&mut self, tab: isize) { - self.host - .event_loop - .request_activate_tab_relative(self.host.display.gl_window().id(), tab) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + move |win| win.activate_tab_relative(tab), + ); } fn increase_font_size(&mut self) { - self.host - .event_loop - .request_increase_font_size(self.host.display.gl_window().id()) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + |win| { + let scale = win.fonts.get_font_scale(); + win.scaling_changed(Some(scale * 1.1)) + }, + ); } fn decrease_font_size(&mut self) { - self.host - .event_loop - .request_decrease_font_size(self.host.display.gl_window().id()) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + |win| { + let scale = win.fonts.get_font_scale(); + win.scaling_changed(Some(scale * 0.9)) + }, + ); } fn reset_font_size(&mut self) { - self.host - .event_loop - .request_reset_font_size(self.host.display.gl_window().id()) - .ok(); + GuiEventLoop::with_window( + &self.host.event_loop, + self.host.display.gl_window().id(), + |win| win.scaling_changed(Some(1.0)), + ); } } @@ -312,6 +337,8 @@ impl TerminalWindow { clipboard: Clipboard::default(), window_position, is_fullscreen: None, + config: Rc::clone(config), + fonts: Rc::clone(fonts), }; host.display.gl_window().set_cursor(MouseCursor::Text); diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 76725fee5..28c6496f9 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -1,5 +1,3 @@ -use crate::config::Config; -use crate::font::FontConfiguration; use crate::futurecore; use crate::gliumwindows; pub use crate::gliumwindows::TerminalWindow; @@ -75,16 +73,6 @@ struct Windows { by_id: HashMap, } -enum SpawnRequest { - Window, - Tab(WindowId), - FontLarger(WindowId), - FontSmaller(WindowId), - FontReset(WindowId), - ActivateTab(WindowId, usize), - ActivateTabRelative(WindowId, isize), -} - /// 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 @@ -92,11 +80,9 @@ enum SpawnRequest { pub struct GuiEventLoop { pub event_loop: RefCell, windows: Rc>, - pub core: futurecore::Core, + core: futurecore::Core, poll_tx: GuiSender, poll_rx: Receiver, - spawn_tx: GuiSender, - spawn_rx: Receiver, tick_rx: Receiver<()>, } @@ -115,102 +101,25 @@ impl GlutinGuiSystem { Ok(Rc::new(Self { event_loop })) } - fn process_spawn( - &self, - config: &Rc, - fonts: &Rc, - ) -> Result<(), Error> { + /// Loop through the core and dispatch any tasks that have been + /// notified as ready to run. Returns once all such tasks have + /// been polled and there are no more pending task notifications. + fn process_futures(&self) { loop { - match self.event_loop.spawn_rx.try_recv() { - Ok(SpawnRequest::Window) => { - crate::spawn_window(&*self, None, config, fonts)?; - } - Ok(SpawnRequest::Tab(window_id)) => { - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.spawn_tab().ok(); - } - } - Ok(SpawnRequest::FontLarger(window_id)) => { - let scale = fonts.get_font_scale() * 1.1; - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.scaling_changed(Some(scale)).ok(); - } - } - Ok(SpawnRequest::FontSmaller(window_id)) => { - let scale = fonts.get_font_scale() * 0.9; - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.scaling_changed(Some(scale)).ok(); - } - } - Ok(SpawnRequest::FontReset(window_id)) => { - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.scaling_changed(Some(1.0)).ok(); - } - } - Ok(SpawnRequest::ActivateTab(window_id, tab)) => { - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.activate_tab(tab).ok(); - } - } - Ok(SpawnRequest::ActivateTabRelative(window_id, tab)) => { - if let Some(window) = self - .event_loop - .windows - .borrow_mut() - .by_id - .get_mut(&window_id) - { - window.activate_tab_relative(tab).ok(); - } - } - Err(TryRecvError::Empty) => return Ok(()), - Err(err) => bail!("spawn_rx disconnected {:?}", err), + if !self.event_loop.core.turn() { + break; } } } } impl GuiSystem for GlutinGuiSystem { - fn run_forever( - &self, - config: &Rc, - fontconfig: &Rc, - ) -> Result<(), Error> { + fn run_forever(&self) -> Result<(), Error> { // This convoluted run() signature is present because of this issue: // https://github.com/tomaka/winit/issues/413 let myself = &self.event_loop; loop { - myself.process_futures(); + self.process_futures(); // Check the window count; if after processing the futures there // are no windows left, then we are done. @@ -224,7 +133,6 @@ impl GuiSystem for GlutinGuiSystem { myself.run_event_loop()?; myself.process_poll()?; - self.process_spawn(config, fontconfig)?; myself.process_tick()?; } } @@ -258,7 +166,6 @@ impl GuiEventLoop { let fut_tx2 = fut_tx.clone(); let core = futurecore::Core::new(Box::new(fut_tx), Box::new(fut_tx2), Box::new(fut_rx)); - let (spawn_tx, spawn_rx) = channel(event_loop.create_proxy()); let (poll_tx, poll_rx) = channel(event_loop.create_proxy()); // The glutin/glium plumbing has no native tick/timer stuff, so @@ -273,8 +180,6 @@ impl GuiEventLoop { Ok(Self { core, - spawn_tx, - spawn_rx, poll_tx, poll_rx, tick_rx, @@ -283,35 +188,26 @@ impl GuiEventLoop { }) } - pub fn request_increase_font_size(&self, window_id: WindowId) -> Result<(), Error> { - self.spawn_tx.send(SpawnRequest::FontLarger(window_id)) - } - pub fn request_decrease_font_size(&self, window_id: WindowId) -> Result<(), Error> { - self.spawn_tx.send(SpawnRequest::FontSmaller(window_id)) - } - pub fn request_reset_font_size(&self, window_id: WindowId) -> Result<(), Error> { - self.spawn_tx.send(SpawnRequest::FontReset(window_id)) - } - - 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)) - } - - pub fn request_activate_tab(&self, window_id: WindowId, tabidx: usize) -> Result<(), Error> { - self.spawn_tx - .send(SpawnRequest::ActivateTab(window_id, tabidx)) - } - pub fn request_activate_tab_relative( - &self, + pub fn with_window Result<(), Error>>( + events: &Rc, window_id: WindowId, - tab: isize, - ) -> Result<(), Error> { - self.spawn_tx - .send(SpawnRequest::ActivateTabRelative(window_id, tab)) + func: F, + ) { + let myself = Rc::clone(events); + events.core.spawn(futures::future::poll_fn(move || { + let mut windows = myself.windows.borrow_mut(); + if let Some(window) = windows.by_id.get_mut(&window_id) { + func(window).map(futures::Async::Ready).map_err(|_| ()) + } else { + Err(()) + } + })); + } + + pub fn spawn_fn Result<(), Error>>(&self, func: F) { + self.core.spawn(futures::future::poll_fn(move || { + func().map(futures::Async::Ready).map_err(|_| ()) + })) } pub fn schedule_read_pty( @@ -518,15 +414,4 @@ impl GuiEventLoop { }); Ok(()) } - - /// Loop through the core and dispatch any tasks that have been - /// notified as ready to run. Returns once all such tasks have - /// been polled and there are no more pending task notifications. - fn process_futures(&self) { - loop { - if !self.core.turn() { - break; - } - } - } } diff --git a/src/guiloop/mod.rs b/src/guiloop/mod.rs index 2a7cde9dd..7748aa8b7 100644 --- a/src/guiloop/mod.rs +++ b/src/guiloop/mod.rs @@ -60,11 +60,7 @@ impl std::str::FromStr for GuiSelection { pub trait GuiSystem { /// Run the event loop. Does not return until there is either a fatal /// error, or until there are no more windows left to manage. - fn run_forever( - &self, - config: &Rc, - fontconfig: &Rc, - ) -> Result<(), Error>; + fn run_forever(&self) -> Result<(), Error>; fn spawn_new_window( &self, diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index 068206dc1..2e8d7a619 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -92,11 +92,7 @@ impl X11GuiSystem { } impl super::GuiSystem for X11GuiSystem { - fn run_forever( - &self, - _config: &Rc, - _fontconfig: &Rc, - ) -> Result<(), Error> { + fn run_forever(&self) -> Result<(), Error> { self.event_loop.run() } fn spawn_new_window( diff --git a/src/main.rs b/src/main.rs index cbf2afead..e1a9d94b6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,15 +120,14 @@ fn main() -> Result<(), Error> { let gui = gui_system.new()?; spawn_window(&*gui, cmd, &config, &fontconfig)?; - gui.run_forever(&config, &fontconfig) + gui.run_forever() } -fn spawn_window( - gui: &GuiSystem, +fn spawn_window_impl( cmd: Option>, config: &Rc, fontconfig: &Rc, -) -> Result<(), Error> { +) -> Result<(term::Terminal, MasterPty, Child), Error> { let cmd = config.build_prog(cmd)?; // First step is to figure out the font metrics so that we know how @@ -160,5 +159,16 @@ fn spawn_window( config.hyperlink_rules.clone(), ); + Ok((terminal, master, child)) +} + +fn spawn_window( + gui: &GuiSystem, + cmd: Option>, + config: &Rc, + fontconfig: &Rc, +) -> Result<(), Error> { + let (terminal, master, child) = spawn_window_impl(cmd, config, fontconfig)?; + gui.spawn_new_window(terminal, master, child, config, fontconfig) }