From 07e64338b3d3f344e3f5889322c2fa2f58ebce5c Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sun, 17 Jan 2021 21:05:18 +0100 Subject: [PATCH] Client can get window props from demon --- src/bin/swayr.rs | 17 +++++++++++++---- src/bin/swayrd.rs | 13 ++++++++----- src/demon.rs | 2 +- src/ipc.rs | 2 ++ src/lib.rs | 2 +- src/{con.rs => window.rs} | 20 +++++++++++--------- 6 files changed, 36 insertions(+), 20 deletions(-) rename src/{con.rs => window.rs} (63%) diff --git a/src/bin/swayr.rs b/src/bin/swayr.rs index eadf98e..3b263a8 100644 --- a/src/bin/swayr.rs +++ b/src/bin/swayr.rs @@ -1,11 +1,20 @@ -use swayr::con; +use std::collections::HashMap; +use std::os::unix::net::UnixStream; use swayr::ipc; +use swayr::window; fn main() { println!("sway here!"); let root_node = ipc::get_tree(); - for con in con::get_cons(&root_node) { - println!(" {}", con); + for win in window::get_windows(&root_node) { + println!(" {}", win); + } + + if let Ok(sock) = UnixStream::connect(ipc::SWAYR_SOCKET_PATH) { + let win_props: Result, serde_json::Error> = + serde_json::from_reader(sock); + println!("Here are the window properties:\n{:#?}", win_props) + } else { + panic!("Could not connect to socket!") } - println!("Yes!") } diff --git a/src/bin/swayrd.rs b/src/bin/swayrd.rs index ec21f89..3ae61fb 100644 --- a/src/bin/swayrd.rs +++ b/src/bin/swayrd.rs @@ -16,11 +16,14 @@ fn main() { let subscriber_handle = thread::spawn(move || demon::monitor_window_events(win_props_for_ev_handler)); - demon::serve_client_requests(win_props); - - let subscriber_result = subscriber_handle.join(); - match subscriber_result { - Ok(()) => println!("Subscriber thread shut down cleanly."), + match demon::serve_client_requests(win_props) { + Ok(()) => { + let subscriber_result = subscriber_handle.join(); + match subscriber_result { + Ok(()) => println!("Subscriber thread shut down cleanly."), + Err(err) => panic!(err), + } + } Err(err) => panic!(err), } } diff --git a/src/demon.rs b/src/demon.rs index dd7c857..5bf7a1d 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -65,7 +65,7 @@ pub fn serve_client_requests( win_props: Arc>>, ) -> std::io::Result<()> { // FIXME: Use sensible path. - let listener = UnixListener::bind("/home/horn/tmp/SWAYR_SOCKET")?; + let listener = UnixListener::bind(ipc::SWAYR_SOCKET_PATH)?; for stream in listener.incoming() { match stream { diff --git a/src/ipc.rs b/src/ipc.rs index fad30e3..1defa46 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -5,6 +5,8 @@ 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 646d3dc..a5df7b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ -pub mod con; pub mod demon; pub mod ipc; +pub mod window; diff --git a/src/con.rs b/src/window.rs similarity index 63% rename from src/con.rs rename to src/window.rs index 5818d24..ae532d3 100644 --- a/src/con.rs +++ b/src/window.rs @@ -1,28 +1,30 @@ use crate::ipc; #[allow(dead_code)] -pub struct Con<'a> { +pub struct Window<'a> { name: &'a str, id: ipc::Id, app_id: Option<&'a str>, } -impl<'a> std::fmt::Display for Con<'a> { +impl<'a> std::fmt::Display for Window<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { write!(f, "{} — {}", self.app_id.unwrap_or(""), self.name) } } -/// Gets all cons (aka, application windows) of the tree. -pub fn get_cons<'a>(tree: &'a ipc::Node) -> Vec> { +/// Gets all application windows of the tree. +pub fn get_windows(tree: &ipc::Node) -> Vec { let mut v = vec![]; for n in tree.iter() { - if n.r#type == ipc::NodeType::Con || n.r#type == ipc::NodeType::FloatingCon { - v.push(Con { + if n.name.is_some() + && (n.r#type == ipc::NodeType::Con || n.r#type == ipc::NodeType::FloatingCon) + { + v.push(Window { name: &n .name .as_ref() - .expect(format!("Con without name. id = {}", n.id).as_str()), + .unwrap_or_else(|| panic!("Con without name. id = {}", n.id)), id: n.id, app_id: match &n.app_id { Some(s) => Some(s.as_ref()), @@ -36,9 +38,9 @@ pub fn get_cons<'a>(tree: &'a ipc::Node) -> Vec> { } #[test] -fn test_get_cons() { +fn test_get_windows() { let tree = ipc::get_tree(); - let cons = get_cons(&tree); + let cons = get_windows(&tree); println!("There are {} cons.", cons.len());