diff --git a/README.md b/README.md index e4b375c..1161de8 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ abstraction of the manual tiling structure. For more history and motivation, see [history.md](docs/history.md). +## Sway + +Since sway is mostly compatible with i3, rust version supports sway in i3 mode, by using +sway socket for i3 IPC. + ## Preview Preview demonstrates directional switching (without touching active tabs) and tab navigation diff --git a/rust/src/backend/i3/backend.rs b/rust/src/backend/i3/backend.rs index 48d3a11..df95e5b 100644 --- a/rust/src/backend/i3/backend.rs +++ b/rust/src/backend/i3/backend.rs @@ -1,5 +1,6 @@ use crate::backend::traits::*; use crate::logging::ResultExt; +use crate::logging::OptionExt; use crate::logging; use crate::types::Windows; use super::client::{Client, Request}; @@ -7,20 +8,32 @@ use super::compass; use serde_json as json; use std::process; +use std::thread; pub struct Backend { client: Client, root: json::Value, } +fn get_sock_path(executable: &str) -> Option { + process::Command::new(executable).arg("--get-socketpath").output() + .ok().filter(|o| o.status.success()).and_then(|o| String::from_utf8(o.stdout).ok()) + .map(|s| s.trim().to_owned()).filter(|s| !s.is_empty()) +} + +fn get_sock_path_async() -> Option { + thread::scope(|s| { + ["i3", "sway"].iter().map(|&bin| s.spawn(move || get_sock_path(bin))) + .find_map(|h| h.join().unwrap_or(None)) + }) +} + impl Backend { pub fn new() -> Self { // Establish a connection to the i3 IPC server and get the tree structure - let i3_socket_path_output = process::Command::new("i3").arg("--get-socketpath").output() - .expect_log("Failed to get i3 socket path"); - let i3_path = String::from_utf8(i3_socket_path_output.stdout) - .expect_log("Failed to parse i3 socket path output"); - let mut client = Client::new(&i3_path.trim()) + let socket_path = get_sock_path_async() + .wanted("Failed to get socket path from i3 or sway").unwrap_or_default(); + let mut client = Client::new(&socket_path.trim()) .expect_log("Failed to connect to i3 IPC server"); let root_string = client.request(Request::GetTree, "") .expect_log("Failed to get i3 tree JSON");