diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 3b3c22e87..62ec2745d 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -3,7 +3,8 @@ use crate::config::Config; use crate::failure::Error; use crate::font::FontConfiguration; -use crate::guiloop::{GuiEventLoop, SessionTerminated}; +use crate::guiloop::glutinloop::GuiEventLoop; +use crate::guiloop::SessionTerminated; use crate::opengl::render::Renderer; use crate::opengl::textureatlas::OutOfTextureSpace; use crate::Child; diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 6f0c03862..51525aaba 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -1,7 +1,12 @@ use crate::config::Config; use crate::font::FontConfiguration; +use crate::futurecore; +use crate::gliumwindows; pub use crate::gliumwindows::TerminalWindow; use crate::guiloop::SessionTerminated; +#[cfg(unix)] +use crate::sigchld; +use crate::{Child, MasterPty}; use failure::Error; use futures::future; use glium; @@ -15,11 +20,6 @@ use std::sync::mpsc::{self, Receiver, SyncSender, TryRecvError}; use std::thread; use std::time::{Duration, SystemTime}; -use crate::futurecore; -use crate::gliumwindows; -#[cfg(unix)] -use crate::sigchld; - #[derive(Clone)] pub struct GuiSender { tx: SyncSender, @@ -83,7 +83,91 @@ pub struct GuiEventLoop { const TICK_INTERVAL: Duration = Duration::from_millis(50); const MAX_POLL_LOOP_DURATION: Duration = Duration::from_millis(500); -impl super::GuiSystem for GuiEventLoop {} +pub struct GlutinGuiSystem { + event_loop: Rc, +} + +use super::GuiSystem; + +impl GlutinGuiSystem { + pub fn new() -> Result, Error> { + let event_loop = Rc::new(GuiEventLoop::new()?); + Ok(Rc::new(Self { event_loop })) + } + + fn process_spawn( + &self, + config: &Rc, + fonts: &Rc, + ) -> Result<(), Error> { + loop { + match self.event_loop.spawn_rx.try_recv() { + Ok(SpawnRequest::Window) => { + crate::spawn_window(&*self, 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), + } + } + } +} + +impl GuiSystem for GlutinGuiSystem { + /// 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> { + // 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(); + + // Check the window count; if after processing the futures there + // are no windows left, then we are done. + { + let windows = myself.windows.borrow(); + if windows.by_id.is_empty() { + debug!("No more windows; done!"); + return Ok(()); + } + } + + myself.run_event_loop()?; + myself.process_poll()?; + self.process_spawn(config, fontconfig)?; + #[cfg(unix)] + myself.process_sigchld()?; + myself.process_tick()?; + } + } + + fn spawn_new_window( + &self, + terminal: term::Terminal, + master: MasterPty, + child: Child, + config: &Rc, + fontconfig: &Rc, + ) -> Result<(), Error> { + let window = TerminalWindow::new( + &self.event_loop, + terminal, + master, + child, + fontconfig, + config, + )?; + + self.event_loop.add_window(window) + } +} impl GuiEventLoop { pub fn new() -> Result { @@ -336,52 +420,4 @@ 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( - myself: &Rc, - config: &Rc, - fonts: &Rc, - ) -> Result<(), Error> { - loop { - myself.process_futures(); - - // Check the window count; if after processing the futures there - // are no windows left, then we are done. - { - let windows = myself.windows.borrow(); - if windows.by_id.is_empty() { - debug!("No more windows; done!"); - return Ok(()); - } - } - - myself.run_event_loop()?; - myself.process_poll()?; - Self::process_spawn(myself, config, fonts)?; - #[cfg(unix)] - myself.process_sigchld()?; - myself.process_tick()?; - } - } } diff --git a/src/guiloop/mod.rs b/src/guiloop/mod.rs index aa2aa5d94..81d9f4b96 100644 --- a/src/guiloop/mod.rs +++ b/src/guiloop/mod.rs @@ -1,5 +1,9 @@ use super::ExitStatus; +use crate::config::Config; +use crate::font::FontConfiguration; +use crate::{Child, MasterPty}; use failure::Error; +use std::rc::Rc; #[derive(Debug, Deserialize, Clone, Copy)] pub enum GuiSelection { @@ -19,10 +23,36 @@ impl Default for GuiSelection { } } -pub trait GuiSystem {} +impl GuiSelection { + pub fn new(&self) -> Result, Error> { + match self { + GuiSelection::Glutin => glutinloop::GlutinGuiSystem::new(), + GuiSelection::X11 => { + //#[cfg!(all(unix, not(target_os = "macos")))] + bail!("X11 not compiled in"); + } + } + } +} -#[cfg(any(windows, feature = "force-glutin", target_os = "macos"))] -mod glutinloop; +pub trait GuiSystem { + fn run_forever( + &self, + config: &Rc, + fontconfig: &Rc, + ) -> Result<(), Error>; + + fn spawn_new_window( + &self, + terminal: term::Terminal, + master: MasterPty, + child: Child, + config: &Rc, + fontconfig: &Rc, + ) -> Result<(), Error>; +} + +pub mod glutinloop; #[cfg(any(windows, feature = "force-glutin", target_os = "macos"))] pub use self::glutinloop::{GuiEventLoop, GuiSender, TerminalWindow, WindowId}; diff --git a/src/main.rs b/src/main.rs index 3c020541c..588088ff4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,7 @@ mod opengl; mod gliumwindows; mod guiloop; -use crate::guiloop::{GuiEventLoop, TerminalWindow}; +use crate::guiloop::GuiSystem; mod font; use crate::font::FontConfiguration; @@ -105,17 +105,16 @@ fn main() -> Result<(), Error> { None }; - let event_loop = Rc::new(GuiEventLoop::new()?); + let guitype = guiloop::GuiSelection::default(); - spawn_window(&event_loop, cmd, &config, &fontconfig)?; - // This convoluted run() signature is present because of this issue: - // https://github.com/tomaka/winit/issues/413 - GuiEventLoop::run(&event_loop, &config, &fontconfig)?; - Ok(()) + let gui = guitype.new()?; + + spawn_window(&*gui, cmd, &config, &fontconfig)?; + gui.run_forever(&config, &fontconfig) } fn spawn_window( - event_loop: &Rc, + gui: &GuiSystem, cmd: Option>, config: &Rc, fontconfig: &Rc, @@ -163,7 +162,5 @@ fn spawn_window( config.hyperlink_rules.clone(), ); - let window = TerminalWindow::new(event_loop, terminal, master, child, fontconfig, config)?; - - event_loop.add_window(window) + gui.spawn_new_window(terminal, master, child, config, fontconfig) }