From e1001ca28916dd24142240e802f07e4722eba8fc Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 1 Mar 2018 22:56:07 -0800 Subject: [PATCH] tidy up session close handling Return a discrete Fail type for process termination and window close events. Handle those a bit more cleanly; when we see that they've gone, remove things from the maps that we've built. When there are no more windows left, wind down the event loop. --- src/gliumwindows.rs | 25 ++++++++++++--------- src/main.rs | 54 ++++++++++++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index b6efcd3c7..3179757fe 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -11,8 +11,7 @@ use opengl::textureatlas::OutOfTextureSpace; use pty::MasterPty; use std::io; use std::io::{Read, Write}; -use std::process::Child; -use std::process::Command; +use std::process::{Child, Command, ExitStatus}; use std::rc::Rc; use term::{self, Terminal}; use term::{MouseButton, MouseEventKind}; @@ -21,6 +20,16 @@ use term::KeyModifiers; use term::hyperlink::Hyperlink; use wakeup::Wakeup; +#[derive(Debug, Fail)] +pub enum SessionTerminated { + #[fail(display = "Process exited: {:?}", status)] + ProcessStatus { status: ExitStatus }, + #[fail(display = "Error: {:?}", err)] + Error { err: Error }, + #[fail(display = "Window Closed")] + WindowClosed, +} + struct Host { display: glium::Display, pty: MasterPty, @@ -465,7 +474,7 @@ impl TerminalWindow { event: WindowEvent::Closed, .. } => { - bail!("window close requested!"); + return Err(SessionTerminated::WindowClosed.into()); } Event::WindowEvent { event: WindowEvent::HiDPIFactorChanged(_), @@ -579,15 +588,11 @@ impl TerminalWindow { self.terminal.has_dirty_lines() } - pub fn test_for_child_exit(&mut self) -> Result<(), Error> { + pub fn test_for_child_exit(&mut self) -> Result<(), SessionTerminated> { match self.process.try_wait() { - Ok(Some(status)) => { - bail!("child exited: {}", status); - } + Ok(Some(status)) => Err(SessionTerminated::ProcessStatus { status }), Ok(None) => Ok(()), - Err(e) => { - bail!("failed to wait for child: {}", e); - } + Err(e) => Err(SessionTerminated::Error { err: e.into() }), } } } diff --git a/src/main.rs b/src/main.rs index 89f9d283e..269ad8283 100644 --- a/src/main.rs +++ b/src/main.rs @@ -204,20 +204,29 @@ fn run_glium( pty_sender.send(PtyMsg::AddPty(window_id, master_fd))?; events_loop.run_forever(|event| { + use glium::glutin::ControlFlow::{Break, Continue}; use glium::glutin::Event; - match event { + + let mut dead_windows = Vec::new(); + let result = match event { Event::WindowEvent { window_id, .. } => match windows_by_id.get_mut(&window_id) { Some(window) => match window.dispatch_event(event) { - Ok(_) => glium::glutin::ControlFlow::Continue, - Err(err) => { - eprintln!("{:?}", err); - glium::glutin::ControlFlow::Break - } + Ok(_) => Continue, + Err(err) => match err.downcast_ref::() { + Some(_) => { + dead_windows.push(window_id.clone()); + Continue + } + _ => { + eprintln!("{:?}", err); + Break + } + }, }, None => { // This happens surprisingly often! // eprintln!("window event for unknown {:?}", window_id); - glium::glutin::ControlFlow::Continue + Continue } }, Event::Awakened => loop { @@ -225,8 +234,14 @@ fn run_glium( Ok(WakeupMsg::PtyReadable(window_id)) => { windows_by_id.get_mut(&window_id).map(|w| w.try_read_pty()); } - Ok(WakeupMsg::SigChld) => for (_, window) in windows_by_id.iter_mut() { - window.test_for_child_exit().unwrap(); + Ok(WakeupMsg::SigChld) => for (window_id, window) in windows_by_id.iter_mut() { + match window.test_for_child_exit() { + Ok(_) => {} + Err(err) => { + eprintln!("pty finished: {:?}", err); + dead_windows.push(window_id.clone()); + } + } }, Ok(WakeupMsg::Paint) => for (_, window) in windows_by_id.iter_mut() { window.paint_if_needed().unwrap(); @@ -236,10 +251,27 @@ fn run_glium( .get_mut(&window_id) .map(|w| w.process_clipboard()); } - Err(_) => return glium::glutin::ControlFlow::Continue, + Err(_) => break Continue, } }, - _ => glium::glutin::ControlFlow::Continue, + _ => Continue, + }; + + match result { + Continue => { + for window_id in dead_windows { + // TODO: DelPty + windows_by_id.remove(&window_id); + } + + if windows_by_id.len() == 0 { + // If we have no more windows left to manage, we're done + Break + } else { + Continue + } + } + Break => Break, } });