diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index f552344b7..a3de95f98 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -339,12 +339,13 @@ impl TerminalWindow { fn update_title(&mut self) { let num_tabs = self.tabs.len(); + if num_tabs == 0 { return; } let tab_no = self.tabs.get_active_idx(); - let terminal = self.tabs.get_active().terminal(); + let terminal = self.tabs.get_active().unwrap().terminal(); let title = terminal.get_title(); let window = self.host.display.gl_window(); @@ -360,14 +361,20 @@ impl TerminalWindow { } pub fn clone_current_pty(&self) -> Result { - self.tabs.get_active().pty().try_clone() + self.tabs + .get_active() + .ok_or_else(|| format_err!("no active tab"))? + .pty() + .try_clone() } pub fn paint(&mut self) -> Result<(), Error> { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; let mut target = self.host.display.draw(); - let res = self - .renderer - .paint(&mut target, &mut self.tabs.get_active().terminal()); + let res = self.renderer.paint(&mut target, &mut tab.terminal()); // Ensure that we finish() the target before we let the // error bubble up, otherwise we lose the context. target.finish().unwrap(); @@ -380,7 +387,7 @@ impl TerminalWindow { if let Some(&OutOfTextureSpace { size }) = err.downcast_ref::() { eprintln!("out of texture space, allocating {}", size); self.renderer.recreate_atlas(&self.host.display, size)?; - self.tabs.get_active().terminal().make_all_lines_dirty(); + tab.terminal().make_all_lines_dirty(); // Recursively initiate a new paint return self.paint(); } @@ -458,9 +465,14 @@ impl TerminalWindow { position: PhysicalPosition, modifiers: glium::glutin::ModifiersState, ) -> Result<(), Error> { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + self.last_mouse_coords = position; let (x, y): (i32, i32) = position.into(); - self.tabs.get_active().terminal().mouse_event( + tab.terminal().mouse_event( term::MouseEvent { kind: MouseEventKind::Move, button: MouseButton::None, @@ -469,7 +481,7 @@ impl TerminalWindow { modifiers: Self::decode_modifiers(modifiers), }, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -479,13 +491,8 @@ impl TerminalWindow { // When hovering over a hyperlink, show an appropriate // mouse cursor to give the cue that it is clickable - let cursor = if self - .tabs - .get_active() - .terminal() - .current_highlight() - .is_some() - { + + let cursor = if tab.terminal().current_highlight().is_some() { MouseCursor::Hand } else { MouseCursor::Text @@ -501,7 +508,12 @@ impl TerminalWindow { button: glutin::MouseButton, modifiers: glium::glutin::ModifiersState, ) -> Result<(), Error> { - self.tabs.get_active().terminal().mouse_event( + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + + tab.terminal().mouse_event( term::MouseEvent { kind: match state { ElementState::Pressed => MouseEventKind::Press, @@ -518,7 +530,7 @@ impl TerminalWindow { modifiers: Self::decode_modifiers(modifiers), }, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -563,8 +575,13 @@ impl TerminalWindow { } _ => return Ok(()), }; + + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; for _ in 0..times { - self.tabs.get_active().terminal().mouse_event( + tab.terminal().mouse_event( term::MouseEvent { kind: MouseEventKind::Press, button, @@ -573,7 +590,7 @@ impl TerminalWindow { modifiers: Self::decode_modifiers(modifiers), }, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -780,26 +797,30 @@ impl TerminalWindow { } fn key_event(&mut self, event: glium::glutin::KeyboardInput) -> Result<(), Error> { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; let mods = Self::decode_modifiers(event.modifiers); self.last_modifiers = mods; self.allow_received_character = false; if let Some(key) = Self::keycode_from_input(&event) { // debug!("event {:?} -> {:?}", event, key); match event.state { - ElementState::Pressed => self.tabs.get_active().terminal().key_down( + ElementState::Pressed => tab.terminal().key_down( key, mods, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?, - ElementState::Released => self.tabs.get_active().terminal().key_up( + ElementState::Released => tab.terminal().key_up( key, mods, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?, @@ -812,10 +833,11 @@ impl TerminalWindow { } pub fn paint_if_needed(&mut self) -> Result<(), Error> { - if self.tabs.is_empty() { - return Ok(()); - } - if self.tabs.get_active().terminal().has_dirty_lines() { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + if tab.terminal().has_dirty_lines() { self.paint()?; } Ok(()) @@ -830,7 +852,9 @@ impl TerminalWindow { "TerminalWindow::scaling_changed dpi_scale={} font_scale={}", dpi_scale, font_scale ); - self.tabs.get_active().terminal().make_all_lines_dirty(); + self.tabs + .get_active() + .map(|tab| tab.terminal().make_all_lines_dirty()); self.fonts.change_scaling(font_scale, dpi_scale); let metrics = self.fonts.default_font_metrics()?; @@ -887,11 +911,15 @@ impl TerminalWindow { // eprintln!("ReceivedCharacter {} {:?}", c as u32, c); if self.allow_received_character { self.allow_received_character = false; - self.tabs.get_active().terminal().key_down( + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + tab.terminal().key_down( KeyCode::Char(c), self.last_modifiers, &mut TabHost { - pty: &mut *self.tabs.get_active().pty(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -951,9 +979,12 @@ impl TerminalWindow { pub fn tab_did_terminate(&mut self, tab_id: TabId) { self.tabs.remove_by_id(tab_id); - if !self.tabs.is_empty() { - self.tabs.get_active().terminal().make_all_lines_dirty(); - self.update_title(); + match self.tabs.get_active() { + Some(tab) => { + tab.terminal().make_all_lines_dirty(); + self.update_title(); + } + None => (), } } diff --git a/src/guicommon/tabs.rs b/src/guicommon/tabs.rs index cd732f586..39a795085 100644 --- a/src/guicommon/tabs.rs +++ b/src/guicommon/tabs.rs @@ -107,9 +107,8 @@ impl Tabs { } } - pub fn get_active(&self) -> &Tab { - // FIXME: Optional - &self.tabs[self.active] + pub fn get_active(&self) -> Option<&Tab> { + self.tabs.get(self.active) } #[inline] diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index c9520c98a..76179dbaa 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -2,6 +2,7 @@ use super::{GuiSystem, SessionTerminated}; use crate::config::Config; use crate::font::FontConfiguration; use crate::futurecore; +use crate::guicommon::tabs::TabId; use crate::xwindows::xwin::TerminalWindow; use crate::xwindows::Connection; use crate::{spawn_window_impl, Child, MasterPty}; @@ -12,6 +13,7 @@ use mio_extras::channel::{channel, Receiver as GuiReceiver, Sender as GuiSender} use std::cell::RefCell; use std::collections::HashMap; use std::io; +use std::os::unix::io::AsRawFd; use std::os::unix::io::RawFd; use std::rc::Rc; use std::sync::mpsc::TryRecvError; @@ -34,6 +36,7 @@ impl futurecore::CoreReceiver for GuiReceiver { } struct TabEntry { + tab_id: TabId, fd: RawFd, window_id: WindowId, } @@ -67,7 +70,7 @@ impl Evented for TabEntry { #[derive(Default)] struct Windows { by_id: HashMap, - by_fd: HashMap>, + tab_by_id: HashMap>, } pub struct GuiEventLoop { @@ -175,12 +178,12 @@ impl GuiEventLoop { self.process_pty_event(event)?; } } - self.process_sigchld()?; + self.process_sigchld(); // Check the window count; if after processing the futures there // are no windows left, then we are done. { let windows = self.windows.borrow(); - if windows.by_id.is_empty() && windows.by_fd.is_empty() { + if windows.by_id.is_empty() && windows.tab_by_id.is_empty() { debug!("No more windows; done!"); return Ok(()); } @@ -216,21 +219,29 @@ impl GuiEventLoop { pub fn spawn_tab(&self, window_id: WindowId) -> Result<(), Error> { let mut windows = self.windows.borrow_mut(); - let fd = { + let (tab_id, fd) = { 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.spawn_tab()? + let tab_id = window.spawn_tab()?; + + let fd = window.tabs().get_by_id(tab_id).unwrap().pty().as_raw_fd(); + + (tab_id, fd) }; eprintln!("spawned new tab with fd = {}", fd); - let entry = Rc::new(TabEntry { fd, window_id }); - windows.by_fd.insert(fd, Rc::clone(&entry)); + let entry = Rc::new(TabEntry { + fd, + tab_id, + window_id, + }); + windows.tab_by_id.insert(tab_id, Rc::clone(&entry)); self.poll.register( &*entry, - Token(fd as usize), + Token(tab_id as usize), Ready::readable(), PollOpt::edge(), )?; @@ -253,21 +264,26 @@ impl GuiEventLoop { pub fn add_window(&self, window: TerminalWindow) -> Result<(), Error> { let window_id = window.window_id(); - let fds = window.pty_fds(); let mut windows = self.windows.borrow_mut(); - windows.by_id.insert(window_id, window); - for fd in fds { - let entry = Rc::new(TabEntry { fd, window_id }); - windows.by_fd.insert(fd, Rc::clone(&entry)); + for tab in window.tabs().iter() { + let fd = tab.pty().as_raw_fd(); + let tab_id = tab.tab_id(); + let entry = Rc::new(TabEntry { + fd, + tab_id, + window_id, + }); + windows.tab_by_id.insert(tab_id, Rc::clone(&entry)); self.poll.register( &*entry, - Token(fd as usize), + Token(tab_id as usize), Ready::readable(), PollOpt::edge(), )?; } + windows.by_id.insert(window_id, window); Ok(()) } @@ -286,33 +302,47 @@ impl GuiEventLoop { /// At this time, all such events correspond to readable events /// for the pty associated with a window. fn process_pty_event(&self, event: Event) -> Result<(), Error> { - // The token is the fd - let fd = event.token().0 as RawFd; + // The token is the tab_id + let tab_id = event.token().0 as TabId; - let (window_id, result) = { + let (window_id, tab_id, result) = { let mut windows = self.windows.borrow_mut(); let entry = windows - .by_fd - .get(&fd) + .tab_by_id + .get(&tab_id) .ok_or_else(|| { - format_err!("fd {} has no associated window in windows_by_fd map", fd) + format_err!( + "tab_id {} has no associated window in windows_tab_by_id map", + tab_id + ) }) - .map(|w| Rc::clone(w))?; + .map(Rc::clone)?; let window = windows.by_id.get_mut(&entry.window_id).ok_or_else(|| { format_err!( - "fd {} -> window_id {:?} but no associated window is in the windows_by_id map", - fd, + "tab {} -> window_id {:?} but no associated window is in the windows_tab_by_id map", + tab_id, entry.window_id ) })?; - (entry.window_id, window.try_read_pty(fd)) + ( + entry.window_id, + entry.tab_id, + window.try_read_pty(entry.tab_id), + ) }; if let Err(err) = result { if err.downcast_ref::().is_some() { - self.schedule_window_close(window_id, Some(fd))?; + self.windows + .borrow_mut() + .by_id + .get_mut(&window_id) + .map(|window| { + window.tab_did_terminate(tab_id); + Some(()) + }); } else { bail!("{:?}", err); } @@ -320,45 +350,46 @@ impl GuiEventLoop { Ok(()) } - fn schedule_window_close(&self, window_id: WindowId, fd: Option) -> Result<(), Error> { + pub fn deregister_tab(&self, tab_id: TabId) -> Result<(), Error> { + let mut windows = self.windows.borrow_mut(); + if let Some(entry) = windows.tab_by_id.get_mut(&tab_id) { + self.poll.deregister(&**entry).ok(); + } + windows.tab_by_id.remove(&tab_id); + Ok(()) + } + + fn schedule_window_close(&self, window_id: WindowId) -> Result<(), Error> { + eprintln!("schedule_window_close {:?}", window_id); + let mut windows = self.windows.borrow_mut(); - 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) - })?; + let tab_ids: Vec = windows + .tab_by_id + .iter() + .filter_map(|(tab_id, entry)| { + if entry.window_id == window_id { + Some(*tab_id) + } else { + None + } + }) + .collect(); - 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) - }; - - for fd in &fds { - if let Some(entry) = windows.by_fd.get_mut(fd) { - self.poll.deregister(&**entry)?; + for tab_id in &tab_ids { + if let Some(entry) = windows.tab_by_id.get_mut(&tab_id) { + self.poll.deregister(&**entry).ok(); } - windows.by_fd.remove(fd); + windows.tab_by_id.remove(&tab_id); } - 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)?; - } - } + windows.by_id.remove(&window_id); + eprintln!( + "deregistered tabs {:?}. Remaining counts {} {}", + tab_ids, + windows.tab_by_id.len(), + windows.by_id.len() + ); Ok(()) } @@ -456,21 +487,20 @@ impl GuiEventLoop { /// If we were signalled by a child process completion, zip through /// the windows and have then notice and prepare to close. - fn process_sigchld(&self) -> Result<(), Error> { + fn process_sigchld(&self) { let window_ids: Vec = self .windows .borrow_mut() .by_id .iter_mut() .filter_map(|(window_id, window)| match window.test_for_child_exit() { - Ok(_) => None, - Err(_) => Some(*window_id), + false => None, + true => Some(*window_id), }) .collect(); for window_id in window_ids { - self.schedule_window_close(window_id, None)?; + self.schedule_window_close(window_id).ok(); } - Ok(()) } } diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 137537ad4..6b03f7fdc 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -4,6 +4,7 @@ use super::xkeysyms; use super::{Connection, Window}; use crate::config::Config; use crate::font::FontConfiguration; +use crate::guicommon::tabs::{Tab, TabId, Tabs}; use crate::guiloop::x11::{GuiEventLoop, WindowId}; use crate::guiloop::SessionTerminated; use crate::opengl::textureatlas::OutOfTextureSpace; @@ -11,81 +12,12 @@ use crate::{openpty, MasterPty}; use clipboard::{ClipboardContext, ClipboardProvider}; use failure::Error; use futures; -use std::cell::RefCell; use std::io::{self, Read, Write}; -use std::os::unix::io::{AsRawFd, RawFd}; use std::rc::Rc; use term::{self, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; use termwiz::hyperlink::Hyperlink; use xcb; -/// Holds the terminal state for a tab owned by this window -struct Tab { - terminal: RefCell, - process: RefCell, - pty: RefCell, -} - -impl Drop for Tab { - fn drop(&mut self) { - // Avoid lingering zombies - self.process.borrow_mut().kill().ok(); - self.process.borrow_mut().wait().ok(); - } -} - -struct Tabs { - tabs: Vec, - active: usize, -} - -impl Tabs { - fn new(tab: Tab) -> Self { - Self { - tabs: vec![tab], - active: 0, - } - } - - fn get_active(&self) -> &Tab { - &self.tabs[self.active] - } - - fn set_active(&mut self, idx: usize) { - assert!(idx < self.tabs.len()); - self.active = idx; - self.tabs[idx].terminal.borrow_mut().make_all_lines_dirty(); - } - - 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 { - 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); - let len = self.tabs.len(); - if len > 0 && self.active == idx && idx >= len { - self.set_active(len - 1); - } - } - } -} - /// Implements `TerminalHost` for a Tab. /// `TabHost` instances are short lived and borrow references to /// other state. @@ -267,11 +199,7 @@ impl TerminalWindow { host.window.show(); - let tab = Tab { - terminal: RefCell::new(terminal), - process: RefCell::new(process), - pty: RefCell::new(pty), - }; + let tab = Tab::new(terminal, process, pty); Ok(TerminalWindow { host, @@ -290,14 +218,6 @@ impl TerminalWindow { self.host.window.window.window_id } - pub fn pty_fds(&self) -> Vec { - self.tabs - .tabs - .iter() - .map(|tab| tab.pty.borrow().as_raw_fd()) - .collect() - } - pub fn scaling_changed(&mut self, font_scale: Option) -> Result<(), Error> { let font_scale = font_scale.unwrap_or_else(|| self.fonts.get_font_scale()); eprintln!("TerminalWindow::scaling_changed font_scale={}", font_scale); @@ -333,11 +253,9 @@ impl TerminalWindow { let rows = ((height as usize + 1) / self.cell_height) as u16; let cols = ((width as usize + 1) / self.cell_width) as u16; - for tab in &mut self.tabs.tabs { - tab.pty.borrow_mut().resize(rows, cols, width, height)?; - tab.terminal - .borrow_mut() - .resize(rows as usize, cols as usize); + for tab in self.tabs.iter() { + tab.pty().resize(rows, cols, width, height)?; + tab.terminal().resize(rows as usize, cols as usize); } Ok(true) @@ -352,11 +270,13 @@ impl TerminalWindow { } pub fn paint(&mut self) -> Result<(), Error> { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + let mut target = self.host.window.draw(); - let res = self.renderer.paint( - &mut target, - &mut self.tabs.get_active().terminal.borrow_mut(), - ); + let res = self.renderer.paint(&mut target, &mut tab.terminal()); // Ensure that we finish() the target before we let the // error bubble up, otherwise we lose the context. target @@ -371,11 +291,7 @@ impl TerminalWindow { if let Some(&OutOfTextureSpace { size }) = err.downcast_ref::() { eprintln!("out of texture space, allocating {}", size); self.renderer.recreate_atlas(&self.host.window, size)?; - self.tabs - .get_active() - .terminal - .borrow_mut() - .make_all_lines_dirty(); + tab.terminal().make_all_lines_dirty(); // Recursively initiate a new paint return self.paint(); } @@ -386,37 +302,70 @@ impl TerminalWindow { } pub fn paint_if_needed(&mut self) -> Result<(), Error> { - let dirty = self.tabs.get_active().terminal.borrow().has_dirty_lines(); - if dirty { + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + if tab.terminal().has_dirty_lines() { self.paint()?; } Ok(()) } - pub fn test_for_child_exit(&mut self) -> Result<(), SessionTerminated> { - match self.tabs.get_active().process.borrow_mut().try_wait() { - Ok(Some(status)) => Err(SessionTerminated::ProcessStatus { status }), - Ok(None) => Ok(()), - Err(e) => Err(SessionTerminated::Error { err: e.into() }), - } + pub fn tabs<'a>(&'a self) -> &'a Tabs { + &self.tabs } - pub fn try_read_pty(&mut self, fd: RawFd) -> Result<(), Error> { + pub fn tab_did_terminate(&mut self, tab_id: TabId) { + self.tabs.remove_by_id(tab_id); + match self.tabs.get_active() { + Some(tab) => { + tab.terminal().make_all_lines_dirty(); + self.update_title(); + } + None => (), + } + + let events = Rc::clone(&self.host.event_loop); + self.host + .event_loop + .core + .spawn(futures::future::poll_fn(move || { + events + .deregister_tab(tab_id) + .map(futures::Async::Ready) + .map_err(|_| ()) + })); + } + + pub fn test_for_child_exit(&mut self) -> bool { + let dead_tabs: Vec = self + .tabs + .iter() + .filter_map(|tab| match tab.process().try_wait() { + Ok(None) => None, + _ => Some(tab.tab_id()), + }) + .collect(); + for tab_id in dead_tabs { + self.tab_did_terminate(tab_id); + } + self.tabs.is_empty() + } + + pub fn try_read_pty(&mut self, tab_id: TabId) -> Result<(), Error> { const BUFSIZE: usize = 8192; let mut buf = [0; BUFSIZE]; - let tab = self - .tabs - .get_for_fd(fd) - .ok_or_else(|| format_err!("no tab for fd {}", fd))?; + let tab = self.tabs.get_by_id(tab_id)?; - let result = tab.pty.borrow_mut().read(&mut buf); + let result = tab.pty().read(&mut buf); match result { Ok(size) => { - tab.terminal.borrow_mut().advance_bytes( + tab.terminal().advance_bytes( &buf[0..size], &mut TabHost { - pty: &mut *tab.pty.borrow_mut(), + pty: &mut *tab.pty(), host: &mut self.host, }, ); @@ -436,10 +385,14 @@ impl TerminalWindow { } fn mouse_event(&mut self, event: MouseEvent) -> Result<(), Error> { - self.tabs.get_active().terminal.borrow_mut().mouse_event( + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + tab.terminal().mouse_event( event, &mut TabHost { - pty: &mut *self.tabs.get_active().pty.borrow_mut(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -459,12 +412,16 @@ impl TerminalWindow { } xcb::KEY_PRESS => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(event) }; + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; if let Some((code, mods)) = self.decode_key(key_press) { - self.tabs.get_active().terminal.borrow_mut().key_down( + tab.terminal().key_down( code, mods, &mut TabHost { - pty: &mut *self.tabs.get_active().pty.borrow_mut(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -473,11 +430,15 @@ impl TerminalWindow { xcb::KEY_RELEASE => { let key_press: &xcb::KeyPressEvent = unsafe { xcb::cast_event(event) }; if let Some((code, mods)) = self.decode_key(key_press) { - self.tabs.get_active().terminal.borrow_mut().key_up( + let tab = match self.tabs.get_active() { + Some(tab) => tab, + None => return Ok(()), + }; + tab.terminal().key_up( code, mods, &mut TabHost { - pty: &mut *self.tabs.get_active().pty.borrow_mut(), + pty: &mut *tab.pty(), host: &mut self.host, }, )?; @@ -534,52 +495,41 @@ impl TerminalWindow { Ok(()) } - pub fn spawn_tab(&mut self) -> Result { + pub fn spawn_tab(&mut self) -> Result { let rows = (self.height as usize + 1) / self.cell_height; let cols = (self.width as usize + 1) / self.cell_width; let (pty, slave) = openpty(rows as u16, cols as u16, self.width, self.height)?; let cmd = self.host.config.build_prog(None)?; - let process = RefCell::new(slave.spawn_command(cmd)?); + let process = slave.spawn_command(cmd)?; eprintln!("spawned: {:?}", process); - let terminal = RefCell::new(term::Terminal::new( + let terminal = term::Terminal::new( rows, cols, self.host.config.scrollback_lines.unwrap_or(3500), self.host.config.hyperlink_rules.clone(), - )); + ); - let fd = pty.as_raw_fd(); + let tab = Tab::new(terminal, process, pty); + let tab_id = tab.tab_id(); - let tab = Tab { - terminal, - process, - pty: RefCell::new(pty), - }; - - self.tabs.tabs.push(tab); - let len = self.tabs.tabs.len(); + self.tabs.push(tab); + let len = self.tabs.len(); self.activate_tab(len - 1)?; - Ok(fd) - } - - pub fn close_tab_for_fd(&mut self, fd: RawFd) -> Result<(), Error> { - self.tabs.remove_tab_for_fd(fd); - self.update_title(); - Ok(()) + Ok(tab_id) } fn update_title(&mut self) { - let num_tabs = self.tabs.tabs.len(); + let num_tabs = self.tabs.len(); if num_tabs == 0 { return; } - let tab_no = self.tabs.active; + let tab_no = self.tabs.get_active_idx(); - let terminal = self.tabs.get_active().terminal.borrow(); + let terminal = self.tabs.get_active().unwrap().terminal(); if num_tabs == 1 { self.host.window.set_title(terminal.get_title()); @@ -594,7 +544,7 @@ impl TerminalWindow { } pub fn activate_tab(&mut self, tab: usize) -> Result<(), Error> { - let max = self.tabs.tabs.len(); + let max = self.tabs.len(); if tab < max { self.tabs.set_active(tab); self.update_title(); @@ -603,8 +553,8 @@ impl TerminalWindow { } pub fn activate_tab_relative(&mut self, delta: isize) -> Result<(), Error> { - let max = self.tabs.tabs.len(); - let active = self.tabs.active as isize; + let max = self.tabs.len(); + let active = self.tabs.get_active_idx() as isize; let tab = active + delta; let tab = if tab < 0 { max as isize + tab } else { tab }; self.activate_tab(tab as usize % max)