From fec7616b482224244d07900533ea160bf8997b2b Mon Sep 17 00:00:00 2001 From: Matias Linares Date: Sun, 4 Sep 2016 15:10:49 -0300 Subject: Better path handling for socket --- src/main.rs | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index f745e92..31d0423 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,17 +29,37 @@ use x11::xlib; use x11::keysym; use std::fs; +use std::env; +use std::path::{PathBuf}; use std::io::{Read,Write}; use unix_socket::UnixListener; -const SOCKET_PATH: &'static str = "./dotwm.sock"; +fn dotwm_dir() -> PathBuf { + let mut path = env::home_dir().unwrap(); + path.push(".config"); + path.push("dotwm"); + + if !path.exists() && !path.is_dir() { + fs::create_dir(&path).unwrap(); + } + + path +} + +fn socket_path() -> PathBuf { + let mut path = dotwm_dir(); + path.push("dotwm.sock"); + path +} fn run_autostart() { - let paths = &["./autostart", "/home/matias/.config/dotwm/autostart"]; - for path in paths { - if exec_cmd(path, &[]).is_ok() { - break; - } + let mut autostart = dotwm_dir(); + autostart.push("autostart"); + + let found = exec_cmd(autostart, &[]).is_ok(); + + if !found { + exec_cmd("./autostart", &[]).unwrap(); } } @@ -51,7 +71,8 @@ fn main() { add_binding(&mut dotwm, &mut bindings, keysym::XK_q, xlib::Mod4Mask | xlib::ShiftMask, quit_dotwm, &[]); - let listener = UnixListener::bind(SOCKET_PATH).unwrap(); + let sp = socket_path(); + let listener = UnixListener::bind(&sp).unwrap(); run_autostart(); @@ -115,5 +136,5 @@ fn main() { collect_zombies(); } - let _ = fs::remove_file(SOCKET_PATH); + let _ = fs::remove_file(sp); } -- cgit v1.2.3-70-g09d2