From 1df7dc5171abfe1eab3e95be964f61c5876198f1 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Sat, 23 Oct 2021 07:16:47 +0000 Subject: Add multi-window support Previously Alacritty would always initialize only a single terminal emulator window feeding into the winit event loop, however some platforms like macOS expect all windows to be spawned by the same process and this "daemon-mode" can also come with the advantage of increased memory efficiency. The event loop has been restructured to handle all window-specific events only by the event processing context with the associated window id. This makes it possible to add new terminal windows at any time using the WindowContext::new function call. Some preliminary tests have shown that for empty terminals, this reduces the cost of additional terminal emulators from ~100M to ~6M. However at this point the robustness of the daemon against issues with individual terminals has not been refined, making the reliability of this system questionable. New windows can be created either by using the new `CreateNewWindow` action, or with the `alacritty msg create-window` subcommand. The subcommand sends a message to an IPC socket which Alacritty listens on, its location can be found in the `ALACRITTY_SOCKET` environment variable. Fixes #607. --- alacritty_terminal/src/event.rs | 10 ++++----- alacritty_terminal/src/event_loop.rs | 6 +++--- alacritty_terminal/src/term/mod.rs | 12 +++++------ alacritty_terminal/src/tty/unix.rs | 42 ++++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 31 deletions(-) (limited to 'alacritty_terminal') diff --git a/alacritty_terminal/src/event.rs b/alacritty_terminal/src/event.rs index fac7a56a..1ddf820b 100644 --- a/alacritty_terminal/src/event.rs +++ b/alacritty_terminal/src/event.rs @@ -39,7 +39,7 @@ pub enum Event { PtyWrite(String), /// Cursor blinking state has changed. - CursorBlinkingChange(bool), + CursorBlinkingChange, /// New terminal content available. Wakeup, @@ -54,17 +54,17 @@ pub enum Event { impl Debug for Event { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { match self { - Event::MouseCursorDirty => write!(f, "MouseCursorDirty"), - Event::Title(title) => write!(f, "Title({})", title), - Event::ResetTitle => write!(f, "ResetTitle"), Event::ClipboardStore(ty, text) => write!(f, "ClipboardStore({:?}, {})", ty, text), Event::ClipboardLoad(ty, _) => write!(f, "ClipboardLoad({:?})", ty), Event::ColorRequest(index, _) => write!(f, "ColorRequest({})", index), Event::PtyWrite(text) => write!(f, "PtyWrite({})", text), + Event::Title(title) => write!(f, "Title({})", title), + Event::CursorBlinkingChange => write!(f, "CursorBlinkingChange"), + Event::MouseCursorDirty => write!(f, "MouseCursorDirty"), + Event::ResetTitle => write!(f, "ResetTitle"), Event::Wakeup => write!(f, "Wakeup"), Event::Bell => write!(f, "Bell"), Event::Exit => write!(f, "Exit"), - Event::CursorBlinkingChange(blinking) => write!(f, "CursorBlinking({})", blinking), } } } diff --git a/alacritty_terminal/src/event_loop.rs b/alacritty_terminal/src/event_loop.rs index fbd882ad..36392581 100644 --- a/alacritty_terminal/src/event_loop.rs +++ b/alacritty_terminal/src/event_loop.rs @@ -73,13 +73,13 @@ impl event::Notify for Notifier { return; } - self.0.send(Msg::Input(bytes)).expect("send event loop msg"); + let _ = self.0.send(Msg::Input(bytes)); } } impl event::OnResize for Notifier { fn on_resize(&mut self, size: &SizeInfo) { - self.0.send(Msg::Resize(*size)).expect("expected send event loop msg"); + let _ = self.0.send(Msg::Resize(*size)); } } @@ -182,8 +182,8 @@ where while let Ok(msg) = self.rx.try_recv() { match msg { Msg::Input(input) => state.write_list.push_back(input), - Msg::Shutdown => return false, Msg::Resize(size) => self.pty.on_resize(&size), + Msg::Shutdown => return false, } } diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs index 1808f3aa..894bd763 100644 --- a/alacritty_terminal/src/term/mod.rs +++ b/alacritty_terminal/src/term/mod.rs @@ -640,7 +640,7 @@ impl Term { } // Update UI about cursor blinking state changes. - self.event_proxy.send_event(Event::CursorBlinkingChange(self.cursor_style().blinking)); + self.event_proxy.send_event(Event::CursorBlinkingChange); } /// Move vi mode cursor. @@ -1471,8 +1471,7 @@ impl Handler for Term { self.mode &= TermMode::VI; self.mode.insert(TermMode::default()); - let blinking = self.cursor_style().blinking; - self.event_proxy.send_event(Event::CursorBlinkingChange(blinking)); + self.event_proxy.send_event(Event::CursorBlinkingChange); } #[inline] @@ -1576,7 +1575,7 @@ impl Handler for Term { ansi::Mode::BlinkingCursor => { let style = self.cursor_style.get_or_insert(self.default_cursor_style); style.blinking = true; - self.event_proxy.send_event(Event::CursorBlinkingChange(true)); + self.event_proxy.send_event(Event::CursorBlinkingChange); }, } } @@ -1618,7 +1617,7 @@ impl Handler for Term { ansi::Mode::BlinkingCursor => { let style = self.cursor_style.get_or_insert(self.default_cursor_style); style.blinking = false; - self.event_proxy.send_event(Event::CursorBlinkingChange(false)); + self.event_proxy.send_event(Event::CursorBlinkingChange); }, } } @@ -1678,8 +1677,7 @@ impl Handler for Term { self.cursor_style = style; // Notify UI about blinking changes. - let blinking = style.unwrap_or(self.default_cursor_style).blinking; - self.event_proxy.send_event(Event::CursorBlinkingChange(blinking)); + self.event_proxy.send_event(Event::CursorBlinkingChange); } #[inline] diff --git a/alacritty_terminal/src/tty/unix.rs b/alacritty_terminal/src/tty/unix.rs index 483333e7..a52f8329 100644 --- a/alacritty_terminal/src/tty/unix.rs +++ b/alacritty_terminal/src/tty/unix.rs @@ -246,6 +246,16 @@ pub fn new(config: &Config, size: &SizeInfo, window_id: Option) -> } } +impl Drop for Pty { + fn drop(&mut self) { + // Make sure the PTY is terminated properly. + unsafe { + libc::kill(self.child.id() as i32, libc::SIGHUP); + } + let _ = self.child.wait(); + } +} + impl EventedReadWrite for Pty { type Reader = File; type Writer = File; @@ -339,6 +349,22 @@ impl EventedPty for Pty { } } +impl OnResize for Pty { + /// Resize the PTY. + /// + /// Tells the kernel that the window size changed with the new pixel + /// dimensions and line/column counts. + fn on_resize(&mut self, size: &SizeInfo) { + let win = size.to_winsize(); + + let res = unsafe { libc::ioctl(self.fd.as_raw_fd(), libc::TIOCSWINSZ, &win as *const _) }; + + if res < 0 { + die!("ioctl TIOCSWINSZ failed: {}", io::Error::last_os_error()); + } + } +} + /// Types that can produce a `libc::winsize`. pub trait ToWinsize { /// Get a `libc::winsize`. @@ -356,22 +382,6 @@ impl<'a> ToWinsize for &'a SizeInfo { } } -impl OnResize for Pty { - /// Resize the PTY. - /// - /// Tells the kernel that the window size changed with the new pixel - /// dimensions and line/column counts. - fn on_resize(&mut self, size: &SizeInfo) { - let win = size.to_winsize(); - - let res = unsafe { libc::ioctl(self.fd.as_raw_fd(), libc::TIOCSWINSZ, &win as *const _) }; - - if res < 0 { - die!("ioctl TIOCSWINSZ failed: {}", io::Error::last_os_error()); - } - } -} - unsafe fn set_nonblocking(fd: c_int) { use libc::{fcntl, F_GETFL, F_SETFL, O_NONBLOCK}; -- cgit v1.2.3