From d773db3b615c72173df766736bc21a7c082bfbeb Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Wed, 20 Jan 2021 08:11:35 +0100 Subject: [PATCH] Delete stale socket on restart (and more) --- Cargo.toml | 1 + src/bin/swayr.rs | 3 ++- src/demon.rs | 28 ++++++++++++++++++++-------- src/ipc.rs | 4 +--- src/lib.rs | 1 + src/util.rs | 7 +++++++ 6 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 src/util.rs diff --git a/Cargo.toml b/Cargo.toml index ab636ff..1ca2713 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ edition = "2018" serde = { version = "1.0.117", features = ["derive"] } serde_json = "1.0.59" clap = "3.0.0-beta.2" +users = "0.11.0" diff --git a/src/bin/swayr.rs b/src/bin/swayr.rs index 3b263a8..fb4e077 100644 --- a/src/bin/swayr.rs +++ b/src/bin/swayr.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use std::os::unix::net::UnixStream; use swayr::ipc; +use swayr::util; use swayr::window; fn main() { @@ -10,7 +11,7 @@ fn main() { println!(" {}", win); } - if let Ok(sock) = UnixStream::connect(ipc::SWAYR_SOCKET_PATH) { + if let Ok(sock) = UnixStream::connect(util::get_swayr_socket_path()) { let win_props: Result, serde_json::Error> = serde_json::from_reader(sock); println!("Here are the window properties:\n{:#?}", win_props) diff --git a/src/demon.rs b/src/demon.rs index 5bf7a1d..3742ea8 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -1,4 +1,5 @@ use crate::ipc; +use crate::util; use serde_json::Deserializer; use std::collections::HashMap; use std::io::Write; @@ -33,6 +34,12 @@ fn handle_window_event( ev: ipc::WindowEvent, win_props: Arc>>, ) { + if util::is_debug() { + println!( + "Handling {:?} event for container {}", + ev.change, ev.container.id + ); + } match ev.change { ipc::WindowEventType::Focus => { let mut write_lock = win_props.write().unwrap(); @@ -52,6 +59,13 @@ fn handle_window_event( } _ => (), } + + if util::is_debug() { + println!( + "New window properties state:\n{:#?}", + win_props.read().unwrap() + ); + } } fn get_epoch_time_as_millis() -> u128 { @@ -64,21 +78,19 @@ fn get_epoch_time_as_millis() -> u128 { pub fn serve_client_requests( win_props: Arc>>, ) -> std::io::Result<()> { - // FIXME: Use sensible path. - let listener = UnixListener::bind(ipc::SWAYR_SOCKET_PATH)?; + match std::fs::remove_file(util::get_swayr_socket_path()) { + Ok(()) => println!("Deleted stale socket from previous run."), + Err(e) => eprintln!("{:?}", e), + } + let listener = UnixListener::bind(util::get_swayr_socket_path())?; for stream in listener.incoming() { match stream { Ok(stream) => { - /* connection succeeded */ let wp_clone = win_props.clone(); thread::spawn(move || handle_client_request(stream, wp_clone)); } - Err(err) => { - /* connection failed */ - eprintln!("Could not accept client request: {:?}", err); - break; - } + Err(err) => return Err(err), } } Ok(()) diff --git a/src/ipc.rs b/src/ipc.rs index 1defa46..8a1d8d1 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -1,12 +1,10 @@ extern crate serde; extern crate serde_json; +extern crate users; use serde::{Deserialize, Serialize}; - use std::process as proc; -pub const SWAYR_SOCKET_PATH: &str = "/home/horn/tmp/SWAYR_SOCKET"; - pub type Id = u32; pub type Dim = u16; pub type Pid = u16; diff --git a/src/lib.rs b/src/lib.rs index a5df7b1..896df33 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ pub mod demon; pub mod ipc; +pub mod util; pub mod window; diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..af7dd99 --- /dev/null +++ b/src/util.rs @@ -0,0 +1,7 @@ +pub fn is_debug() -> bool { + true +} + +pub fn get_swayr_socket_path() -> String { + format!("/run/user/{}/swayr-sock", users::get_current_uid()) +}