From 60eef662badb39241d4fee8c01d381a2258483c2 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Fri, 15 Jan 2021 22:13:18 +0100 Subject: [PATCH] Started swayrd --- src/bin/swayrd.rs | 45 ++++++++++++++++++++++++++++-- src/ipc.rs | 70 +++++++++++++++++++++++++++++++++++++---------- 2 files changed, 99 insertions(+), 16 deletions(-) diff --git a/src/bin/swayrd.rs b/src/bin/swayrd.rs index 96341c9..e1549fb 100644 --- a/src/bin/swayrd.rs +++ b/src/bin/swayrd.rs @@ -1,6 +1,47 @@ -use swayr::con; +extern crate serde; +extern crate serde_json; + +use serde_json::Deserializer; +use std::collections::HashMap; +use std::process as proc; +use std::sync::Arc; +use std::sync::RwLock; +use std::thread; use swayr::ipc; fn main() { - println!("swayd here!"); + let win_props: Arc>> = + Arc::new(RwLock::new(HashMap::new())); + + let subscriber_handle = thread::spawn(|| { + let x = proc::Command::new("swaymsg") + .arg("-t") + .arg("subscribe") + .arg("[window]") + .output() + .expect("Failed to subscribe to window events"); + println!( + "{}", + String::from_utf8(x.stdout).expect("Wrong string data!") + ); + let child = proc::Command::new("swaymsg") + .arg("--monitor") + .arg("-t") + .arg("subscribe") + .arg("'[window]'") + .stdout(proc::Stdio::piped()) + .spawn() + .expect("Failed to subscribe to window events"); + let stdout: std::process::ChildStdout = child.stdout.unwrap(); + // TODO: Before the WindowEvents, there's one Reply. How to read that? + let stream = Deserializer::from_reader(stdout).into_iter::(); + for res in stream { + match res { + Ok(msg) => println!("Got msg: {:?}", msg), + Err(err) => panic!("{:?}", err), + } + } + }); + + subscriber_handle.join(); } diff --git a/src/ipc.rs b/src/ipc.rs index e496492..fe72752 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -9,7 +9,7 @@ pub type Id = u32; pub type Dim = u16; pub type Pid = u16; -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[allow(dead_code)] pub struct Rect { pub x: Dim, @@ -19,7 +19,7 @@ pub struct Rect { } // TODO: Maybe there are more? -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub enum Border { #[serde(rename = "none")] None, @@ -30,7 +30,7 @@ pub enum Border { } // TODO: Maybe there are more? -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub enum Layout { #[serde(rename = "splith")] SplitH, @@ -46,7 +46,7 @@ pub enum Layout { None, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub enum Orientation { #[serde(rename = "horizontal")] Horizontal, @@ -70,7 +70,7 @@ pub enum NodeType { FloatingCon, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] pub enum ShellType { #[serde(rename = "xdg_shell")] XdgShell, @@ -78,7 +78,7 @@ pub enum ShellType { XWayland, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[allow(dead_code)] pub struct WindowProperties { pub class: Option, @@ -88,7 +88,7 @@ pub struct WindowProperties { //pub transient_for: DONTKNOW, } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] #[allow(dead_code)] pub struct Node { pub id: Id, @@ -121,22 +121,22 @@ pub struct Node { } impl Node { - pub fn iter(&self) -> PreOrderNodeIter { - PreOrderNodeIter::new(self) + pub fn iter(&self) -> NodeIter { + NodeIter::new(self) } } -pub struct PreOrderNodeIter<'a> { +pub struct NodeIter<'a> { stack: Vec<&'a Node>, } -impl<'a> PreOrderNodeIter<'a> { - fn new(node: &'a Node) -> PreOrderNodeIter { - PreOrderNodeIter { stack: vec![node] } +impl<'a> NodeIter<'a> { + fn new(node: &'a Node) -> NodeIter { + NodeIter { stack: vec![node] } } } -impl<'a> Iterator for PreOrderNodeIter<'a> { +impl<'a> Iterator for NodeIter<'a> { type Item = &'a Node; fn next(&mut self) -> Option { @@ -184,3 +184,45 @@ fn test_get_tree() { println!(" id: {}, type: {:?}", n.id, n.r#type); } } + +#[derive(Deserialize, Debug)] +#[allow(dead_code)] +pub enum WindowEventType { + #[serde(rename = "new")] + New, + #[serde(rename = "close")] + Close, + #[serde(rename = "focus")] + Focus, + #[serde(rename = "title")] + Title, + #[serde(rename = "fullscreen_mode")] + FullscreenMode, + #[serde(rename = "move")] + Move, + #[serde(rename = "floating")] + Floating, + #[serde(rename = "urgent")] + Urgent, + #[serde(rename = "mark")] + Mark, +} + +#[derive(Deserialize, Debug)] +#[allow(dead_code)] +pub struct Reply { + pub success: bool, + pub error: String, +} + +#[derive(Deserialize, Debug)] +#[allow(dead_code)] +pub struct WindowEvent { + pub change: WindowEventType, + pub container: Node, +} + +pub struct WindowProps { + /// Milliseconds since UNIX epoch. + last_focus_time: u128, +}