Client can get window props from demon

timeout_old
Tassilo Horn 4 years ago
parent 94cb31baa5
commit 07e64338b3
  1. 17
      src/bin/swayr.rs
  2. 7
      src/bin/swayrd.rs
  3. 2
      src/demon.rs
  4. 2
      src/ipc.rs
  5. 2
      src/lib.rs
  6. 20
      src/window.rs

@ -1,11 +1,20 @@
use swayr::con; use std::collections::HashMap;
use std::os::unix::net::UnixStream;
use swayr::ipc; use swayr::ipc;
use swayr::window;
fn main() { fn main() {
println!("sway here!"); println!("sway here!");
let root_node = ipc::get_tree(); let root_node = ipc::get_tree();
for con in con::get_cons(&root_node) { for win in window::get_windows(&root_node) {
println!(" {}", con); println!(" {}", win);
}
if let Ok(sock) = UnixStream::connect(ipc::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)
} else {
panic!("Could not connect to socket!")
} }
println!("Yes!")
} }

@ -16,11 +16,14 @@ fn main() {
let subscriber_handle = let subscriber_handle =
thread::spawn(move || demon::monitor_window_events(win_props_for_ev_handler)); thread::spawn(move || demon::monitor_window_events(win_props_for_ev_handler));
demon::serve_client_requests(win_props); match demon::serve_client_requests(win_props) {
Ok(()) => {
let subscriber_result = subscriber_handle.join(); let subscriber_result = subscriber_handle.join();
match subscriber_result { match subscriber_result {
Ok(()) => println!("Subscriber thread shut down cleanly."), Ok(()) => println!("Subscriber thread shut down cleanly."),
Err(err) => panic!(err), Err(err) => panic!(err),
} }
}
Err(err) => panic!(err),
}
} }

@ -65,7 +65,7 @@ pub fn serve_client_requests(
win_props: Arc<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>, win_props: Arc<RwLock<HashMap<ipc::Id, ipc::WindowProps>>>,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
// FIXME: Use sensible path. // 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() { for stream in listener.incoming() {
match stream { match stream {

@ -5,6 +5,8 @@ use serde::{Deserialize, Serialize};
use std::process as proc; use std::process as proc;
pub const SWAYR_SOCKET_PATH: &str = "/home/horn/tmp/SWAYR_SOCKET";
pub type Id = u32; pub type Id = u32;
pub type Dim = u16; pub type Dim = u16;
pub type Pid = u16; pub type Pid = u16;

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

@ -1,28 +1,30 @@
use crate::ipc; use crate::ipc;
#[allow(dead_code)] #[allow(dead_code)]
pub struct Con<'a> { pub struct Window<'a> {
name: &'a str, name: &'a str,
id: ipc::Id, id: ipc::Id,
app_id: Option<&'a str>, 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> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
write!(f, "{} — {}", self.app_id.unwrap_or(""), self.name) write!(f, "{} — {}", self.app_id.unwrap_or(""), self.name)
} }
} }
/// Gets all cons (aka, application windows) of the tree. /// Gets all application windows of the tree.
pub fn get_cons<'a>(tree: &'a ipc::Node) -> Vec<Con<'a>> { pub fn get_windows(tree: &ipc::Node) -> Vec<Window> {
let mut v = vec![]; let mut v = vec![];
for n in tree.iter() { for n in tree.iter() {
if n.r#type == ipc::NodeType::Con || n.r#type == ipc::NodeType::FloatingCon { if n.name.is_some()
v.push(Con { && (n.r#type == ipc::NodeType::Con || n.r#type == ipc::NodeType::FloatingCon)
{
v.push(Window {
name: &n name: &n
.name .name
.as_ref() .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, id: n.id,
app_id: match &n.app_id { app_id: match &n.app_id {
Some(s) => Some(s.as_ref()), Some(s) => Some(s.as_ref()),
@ -36,9 +38,9 @@ pub fn get_cons<'a>(tree: &'a ipc::Node) -> Vec<Con<'a>> {
} }
#[test] #[test]
fn test_get_cons() { fn test_get_windows() {
let tree = ipc::get_tree(); let tree = ipc::get_tree();
let cons = get_cons(&tree); let cons = get_windows(&tree);
println!("There are {} cons.", cons.len()); println!("There are {} cons.", cons.len());
Loading…
Cancel
Save