Delete stale socket on restart (and more)

timeout_old
Tassilo Horn 4 years ago
parent 07e64338b3
commit d773db3b61
  1. 1
      Cargo.toml
  2. 3
      src/bin/swayr.rs
  3. 28
      src/demon.rs
  4. 4
      src/ipc.rs
  5. 1
      src/lib.rs
  6. 7
      src/util.rs

@ -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"

@ -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<HashMap<ipc::Id, ipc::WindowProps>, serde_json::Error> =
serde_json::from_reader(sock);
println!("Here are the window properties:\n{:#?}", win_props)

@ -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<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>,
) {
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<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>,
) -> 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(())

@ -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;

@ -1,3 +1,4 @@
pub mod demon;
pub mod ipc;
pub mod util;
pub mod window;

@ -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())
}
Loading…
Cancel
Save