Improve socket discovery, handling, and logging

Implement the following logic:
* first try SWAYSOCK
* if failed, then try I3SOCK, the legacy one coming from i3
* if failed, then we abort with an error
This commit is contained in:
Skia 2023-11-07 08:36:08 +02:00
parent cdd5a24dc6
commit 6d26286583

View file

@ -91,19 +91,35 @@ struct MoveAction {
} }
fn get_stream() -> UnixStream { fn get_stream() -> UnixStream {
let socket_path = match env::var("I3SOCK") { for socket_var in ["SWAYSOCK", "I3SOCK"] {
Ok(val) => val, let socket_path = match env::var(socket_var) {
Err(_e) => { Ok(val) => val,
panic!("couldn't find i3/sway socket"); Err(_e) => {
eprintln!("{} not found in environment", socket_var);
continue;
}
};
let socket = Path::new(&socket_path);
match UnixStream::connect(&socket) {
Err(_) => {
eprintln!(
"counldn't connect to socket '{}' found in ${}",
socket_path, socket_var
);
continue;
}
Ok(stream) => {
eprintln!(
"successful connection to socket '{}' found in ${}",
socket_path, socket_var
);
return stream;
}
} }
};
let socket = Path::new(&socket_path);
match UnixStream::connect(&socket) {
Err(_) => panic!("couldn't connect to i3/sway socket"),
Ok(stream) => stream,
} }
panic!("couldn't find any i3/sway socket")
} }
fn send_msg(mut stream: &UnixStream, msg_type: u32, payload: &str) { fn send_msg(mut stream: &UnixStream, msg_type: u32, payload: &str) {