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.
This commit is contained in:
Wez Furlong committed 2018-03-01 22:56:07 -08:00
1 parent 34cc57ceea
commit e1001ca289
2 files changed
+58 -21

No files matched your search

+15 -10
View File
@@ -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() }),
}
}
}
+43 -11
View File
@@ -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::<gliumwindows::SessionTerminated>() {
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,
}
});