theoretically deal with a single tab closing on its own

This commit is contained in:
Wez Furlong committed 2018-03-04 13:26:40 -08:00
1 parent dfafebebef
commit eb7153eb50
2 files changed
+101 -33

No files matched your search

+47 -22
View File
@@ -19,12 +19,12 @@ use xwindows::xwin::TerminalWindow;
#[cfg(all(unix, not(target_os = "macos")))]
pub use xcb::xproto::Window as WindowId;
struct WindowEntry {
struct TabEntry {
fd: RawFd,
window_id: WindowId,
}
impl Evented for WindowEntry {
impl Evented for TabEntry {
fn register(
&self,
poll: &Poll,
@@ -53,7 +53,7 @@ impl Evented for WindowEntry {
#[derive(Default)]
struct Windows {
by_id: HashMap<WindowId, TerminalWindow>,
by_fd: HashMap<RawFd, Rc<WindowEntry>>,
by_fd: HashMap<RawFd, Rc<TabEntry>>,
}
pub struct GuiEventLoop {
@@ -176,18 +176,21 @@ impl GuiEventLoop {
pub fn add_window(&self, window: TerminalWindow) -> Result<(), Error> {
let window_id = window.window_id();
let fd = window.pty_fd();
let fds = window.pty_fds();
let entry = Rc::new(WindowEntry { fd, window_id });
let mut windows = self.windows.borrow_mut();
windows.by_id.insert(window_id, window);
windows.by_fd.insert(fd, Rc::clone(&entry));
self.poll.register(
&*entry,
Token(fd as usize),
Ready::readable(),
PollOpt::edge(),
)?;
for fd in fds {
let entry = Rc::new(TabEntry { fd, window_id });
windows.by_fd.insert(fd, Rc::clone(&entry));
self.poll.register(
&*entry,
Token(fd as usize),
Ready::readable(),
PollOpt::edge(),
)?;
}
Ok(())
}
@@ -227,12 +230,12 @@ impl GuiEventLoop {
entry.window_id
)
})?;
(entry.window_id, window.try_read_pty())
(entry.window_id, window.try_read_pty(fd))
};
if let Err(err) = result {
if err.downcast_ref::<SessionTerminated>().is_some() {
self.schedule_window_close(window_id)?;
self.schedule_window_close(window_id, Some(fd))?;
} else {
bail!("{:?}", err);
}
@@ -240,23 +243,45 @@ impl GuiEventLoop {
Ok(())
}
fn schedule_window_close(&self, window_id: WindowId) -> Result<(), Error> {
fn schedule_window_close(&self, window_id: WindowId, fd: Option<RawFd>) -> Result<(), Error> {
let mut windows = self.windows.borrow_mut();
let fd = {
let (fds, window_close) = {
let window = windows.by_id.get_mut(&window_id).ok_or_else(|| {
format_err!("no window_id {:?} in the windows_by_id map", window_id)
})?;
window.pty_fd()
let all_fds = window.pty_fds();
let num_fds = all_fds.len();
// If no fd was specified, close all of them
let close_fds = match fd {
Some(fd) => vec![fd],
None => all_fds,
};
let window_close = close_fds.len() == num_fds;
(close_fds, window_close)
};
if let Some(entry) = windows.by_fd.get_mut(&fd) {
self.poll.deregister(&**entry)?;
for fd in &fds {
if let Some(entry) = windows.by_fd.get_mut(fd) {
self.poll.deregister(&**entry)?;
}
windows.by_fd.remove(fd);
}
windows.by_id.remove(&window_id);
windows.by_fd.remove(&fd);
if window_close {
windows.by_id.remove(&window_id);
} else {
let window = windows.by_id.get_mut(&window_id).ok_or_else(|| {
format_err!("no window_id {:?} in the windows_by_id map", window_id)
})?;
for fd in fds {
window.close_tab_for_fd(fd)?;
}
}
Ok(())
}
@@ -377,7 +402,7 @@ impl GuiEventLoop {
.collect();
for window_id in window_ids {
self.schedule_window_close(window_id)?;
self.schedule_window_close(window_id, None)?;
}
}
Err(TryRecvError::Empty) => return Ok(()),
+54 -11
View File
@@ -43,6 +43,33 @@ impl Tabs {
fn get_active(&self) -> &Tab {
&self.tabs[self.active]
}
fn get_for_fd(&self, fd: RawFd) -> Option<&Tab> {
for t in &self.tabs {
if t.pty.borrow().as_raw_fd() == fd {
return Some(t);
}
}
None
}
fn index_for_fd(&self, fd: RawFd) -> Option<usize> {
for i in 0..self.tabs.len() {
if self.tabs[i].pty.borrow().as_raw_fd() == fd {
return Some(i);
}
}
None
}
fn remove_tab_for_fd(&mut self, fd: RawFd) {
if let Some(idx) = self.index_for_fd(fd) {
self.tabs.remove(idx);
if self.active == idx && idx >= self.tabs.len() {
self.active = self.tabs.len() - 1;
}
}
}
}
/// Implements `TerminalHost` for a Tab.
@@ -181,8 +208,12 @@ impl TerminalWindow {
self.host.window.window.window_id
}
pub fn pty_fd(&self) -> RawFd {
self.tabs.get_active().pty.borrow().as_raw_fd()
pub fn pty_fds(&self) -> Vec<RawFd> {
self.tabs
.tabs
.iter()
.map(|tab| tab.pty.borrow().as_raw_fd())
.collect()
}
pub fn resize_surfaces(&mut self, width: u16, height: u16) -> Result<bool, Error> {
@@ -287,25 +318,37 @@ impl TerminalWindow {
}
}
pub fn try_read_pty(&mut self) -> Result<(), Error> {
pub fn close_tab_for_fd(&mut self, fd: RawFd) -> Result<(), Error> {
self.tabs.remove_tab_for_fd(fd);
Ok(())
}
pub fn try_read_pty(&mut self, fd: RawFd) -> Result<(), Error> {
const BUFSIZE: usize = 8192;
let mut buf = [0; BUFSIZE];
let result = self.tabs.get_active().pty.borrow_mut().read(&mut buf);
let tab = self.tabs
.get_for_fd(fd)
.ok_or_else(|| format_err!("no tab for fd {}", fd))?;
let result = tab.pty.borrow_mut().read(&mut buf);
match result {
Ok(size) => self.tabs.get_active().terminal.borrow_mut().advance_bytes(
&buf[0..size],
&mut TabHost {
pty: &mut *self.tabs.get_active().pty.borrow_mut(),
host: &mut self.host,
},
),
Ok(size) => {
tab.terminal.borrow_mut().advance_bytes(
&buf[0..size],
&mut TabHost {
pty: &mut *tab.pty.borrow_mut(),
host: &mut self.host,
},
);
}
Err(err) => {
if err.kind() != io::ErrorKind::WouldBlock {
return Err(SessionTerminated::Error { err: err.into() }.into());
}
}
}
Ok(())
}