aboutsummaryrefslogtreecommitdiff
path: root/alacritty/src/ipc.rs
diff options
context:
space:
mode:
authorChristian Duerr <contact@christianduerr.com>2021-10-23 07:16:47 +0000
committerGitHub <noreply@github.com>2021-10-23 07:16:47 +0000
commit1df7dc5171abfe1eab3e95be964f61c5876198f1 (patch)
tree315ceaa093b86b8e875512825302f38e32f697a4 /alacritty/src/ipc.rs
parentd8a98f88295e59d6518ae780a9857c033a83161c (diff)
downloadr-alacritty-1df7dc5171abfe1eab3e95be964f61c5876198f1.tar.gz
r-alacritty-1df7dc5171abfe1eab3e95be964f61c5876198f1.tar.bz2
r-alacritty-1df7dc5171abfe1eab3e95be964f61c5876198f1.zip
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.
Diffstat (limited to 'alacritty/src/ipc.rs')
-rw-r--r--alacritty/src/ipc.rs145
1 files changed, 145 insertions, 0 deletions
diff --git a/alacritty/src/ipc.rs b/alacritty/src/ipc.rs
new file mode 100644
index 00000000..02aaf85f
--- /dev/null
+++ b/alacritty/src/ipc.rs
@@ -0,0 +1,145 @@
+//! Alacritty socket IPC.
+
+use std::ffi::OsStr;
+use std::io::{Error as IoError, ErrorKind, Result as IoResult};
+use std::os::unix::net::UnixDatagram;
+use std::path::PathBuf;
+use std::{env, fs, process};
+
+use glutin::event_loop::EventLoopProxy;
+use log::warn;
+
+use alacritty_terminal::thread;
+
+use crate::cli::Options;
+use crate::event::{Event, EventType};
+
+/// IPC socket message for creating a new window.
+pub const SOCKET_MESSAGE_CREATE_WINDOW: [u8; 1] = [1];
+
+/// Environment variable name for the IPC socket path.
+const ALACRITTY_SOCKET_ENV: &str = "ALACRITTY_SOCKET";
+
+/// Create an IPC socket.
+pub fn spawn_ipc_socket(options: &Options, event_proxy: EventLoopProxy<Event>) -> Option<PathBuf> {
+ // Create the IPC socket and export its path as env variable if necessary.
+ let socket_path = options.socket.clone().unwrap_or_else(|| {
+ let mut path = socket_dir();
+ path.push(format!("{}-{}.sock", socket_prefix(), process::id()));
+ path
+ });
+ env::set_var(ALACRITTY_SOCKET_ENV, socket_path.as_os_str());
+
+ let socket = match UnixDatagram::bind(&socket_path) {
+ Ok(socket) => socket,
+ Err(err) => {
+ warn!("Unable to create socket: {:?}", err);
+ return None;
+ },
+ };
+
+ // Spawn a thread to listen on the IPC socket.
+ thread::spawn_named("socket listener", move || {
+ // Accept up to 2 bytes to ensure only one byte is received.
+ // This ensures forward-compatibility.
+ let mut buf = [0; 2];
+
+ while let Ok(received) = socket.recv(&mut buf) {
+ if buf[..received] == SOCKET_MESSAGE_CREATE_WINDOW {
+ let _ = event_proxy.send_event(Event::new(EventType::CreateWindow, None));
+ }
+ }
+ });
+
+ Some(socket_path)
+}
+
+/// Send a message to the active Alacritty socket.
+pub fn send_message(socket: Option<PathBuf>, message: &[u8]) -> IoResult<()> {
+ let socket = find_socket(socket)?;
+ socket.send(message)?;
+ Ok(())
+}
+
+/// Directory for the IPC socket file.
+#[cfg(not(target_os = "macos"))]
+fn socket_dir() -> PathBuf {
+ xdg::BaseDirectories::with_prefix("alacritty")
+ .ok()
+ .and_then(|xdg| xdg.get_runtime_directory().map(ToOwned::to_owned).ok())
+ .and_then(|path| fs::create_dir_all(&path).map(|_| path).ok())
+ .unwrap_or_else(env::temp_dir)
+}
+
+/// Directory for the IPC socket file.
+#[cfg(target_os = "macos")]
+fn socket_dir() -> PathBuf {
+ env::temp_dir()
+}
+
+/// Find the IPC socket path.
+fn find_socket(socket_path: Option<PathBuf>) -> IoResult<UnixDatagram> {
+ let socket = UnixDatagram::unbound()?;
+
+ // Handle --socket CLI override.
+ if let Some(socket_path) = socket_path {
+ // Ensure we inform the user about an invalid path.
+ socket.connect(&socket_path).map_err(|err| {
+ let message = format!("invalid socket path {:?}", socket_path);
+ IoError::new(err.kind(), message)
+ })?;
+ }
+
+ // Handle environment variable.
+ if let Ok(path) = env::var(ALACRITTY_SOCKET_ENV) {
+ let socket_path = PathBuf::from(path);
+ if socket.connect(&socket_path).is_ok() {
+ return Ok(socket);
+ }
+ }
+
+ // Search for sockets files.
+ for entry in fs::read_dir(socket_dir())?.filter_map(|entry| entry.ok()) {
+ let path = entry.path();
+
+ // Skip files that aren't Alacritty sockets.
+ let socket_prefix = socket_prefix();
+ if path
+ .file_name()
+ .and_then(OsStr::to_str)
+ .filter(|file| file.starts_with(&socket_prefix) && file.ends_with(".sock"))
+ .is_none()
+ {
+ continue;
+ }
+
+ // Attempt to connect to the socket.
+ match socket.connect(&path) {
+ Ok(_) => return Ok(socket),
+ // Delete orphan sockets.
+ Err(error) if error.kind() == ErrorKind::ConnectionRefused => {
+ let _ = fs::remove_file(&path);
+ },
+ // Ignore other errors like permission issues.
+ Err(_) => (),
+ }
+ }
+
+ Err(IoError::new(ErrorKind::NotFound, "no socket found"))
+}
+
+/// File prefix matching all available sockets.
+///
+/// This prefix will include display server information to allow for environments with multiple
+/// display servers running for the same user.
+#[cfg(not(target_os = "macos"))]
+fn socket_prefix() -> String {
+ let display = env::var("WAYLAND_DISPLAY").or_else(|_| env::var("DISPLAY")).unwrap_or_default();
+ format!("Alacritty-{}", display)
+}
+
+/// File prefix matching all available sockets.
+#[cfg(target_os = "macos")]
+fn socket_prefix() -> String {
+ String::from("Alacritty")
+}