diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index dd8669a49..cb5c3f0ba 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -174,6 +174,31 @@ impl GuiEventLoop { } } + pub fn spawn_tab(&self, window_id: WindowId) -> Result<(), Error> { + let mut windows = self.windows.borrow_mut(); + + let fd = { + let mut 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()? + }; + + eprintln!("spawned new tab with fd = {}", fd); + + 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(()) + } + pub fn add_window(&self, window: TerminalWindow) -> Result<(), Error> { let window_id = window.window_id(); let fds = window.pty_fds(); diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 08ef845e6..03a856489 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -1,6 +1,6 @@ use super::{Connection, Window}; +use super::super::{get_shell, spawn_window}; use super::super::opengl::render::Renderer; -use super::super::spawn_window; use super::xkeysyms; use clipboard::{Clipboard, ClipboardImpl, Paste}; use config::Config; @@ -9,6 +9,7 @@ use font::FontConfiguration; use futures; use guiloop::{GuiEventLoop, SessionTerminated, WindowId}; use opengl::textureatlas::OutOfTextureSpace; +use pty; use pty::MasterPty; use std::cell::RefCell; use std::io::{self, Read, Write}; @@ -140,6 +141,21 @@ impl<'a> term::TerminalHost for TabHost<'a> { .map_err(|_| ()) })); } + + fn new_tab(&mut self) { + let events = Rc::clone(&self.host.event_loop); + let window_id = self.host.window.window.window_id; + + self.host + .event_loop + .core + .spawn(futures::future::poll_fn(move || { + events + .spawn_tab(window_id) + .map(futures::Async::Ready) + .map_err(|_| ()) + })); + } } impl TerminalWindow { @@ -455,4 +471,33 @@ impl TerminalWindow { } Ok(()) } + + 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) = pty::openpty(rows as u16, cols as u16, self.width, self.height)?; + let process = RefCell::new(slave.spawn_command(Command::new(get_shell()?))?); + eprintln!("spawned: {:?}", process); + + let terminal = RefCell::new(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 { + terminal, + process, + pty: RefCell::new(pty), + }; + + self.tabs.tabs.push(tab); + self.tabs.active = self.tabs.tabs.len() - 1; + + Ok(fd) + } }