From aa131bb9a955d4df8b2cbbac557057aedbe6c5f5 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 13 Dec 2022 22:18:49 +0000 Subject: [PATCH] functionality and test for subscribe and send tick events --- src/i3msg.rs | 4 ++-- src/lib.rs | 42 +++++++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/i3msg.rs b/src/i3msg.rs index a2a35e2..9b3ad32 100644 --- a/src/i3msg.rs +++ b/src/i3msg.rs @@ -9,8 +9,8 @@ pub struct I3msg<'payload> { } impl<'payload> I3msg<'payload> { - pub fn new(payload: &str, payload_type: u32) -> I3msg { - return I3msg{ + pub fn new(payload: &'payload str, payload_type: u32) -> Self { + Self { magic_string: "i3-ipc", payload_type: payload_type, payload: payload, diff --git a/src/lib.rs b/src/lib.rs index 50ab8d7..182d096 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![warn(clippy::all, rust_2018_idioms)] + mod types; mod i3msg; @@ -25,15 +27,15 @@ pub const IPC_GET_BINDING_STATE: u32 = 12; pub const IPC_GET_INPUTS: u32 = 100; pub const IPC_GET_SEATS: u32 = 101; -pub const EVENT_WORKSPACE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x00]; -pub const EVENT_MODE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x02]; -pub const EVENT_WINDOW: &[u8; 4] = &[0x80, 0x00, 0x00, 0x03]; -pub const EVENT_BARCONFIG_UPDATE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x04]; -pub const EVENT_BINDING: &[u8; 4] = &[0x80, 0x00, 0x00, 0x05]; -pub const EVENT_SHUTDOWN: &[u8; 4] = &[0x80, 0x00, 0x00, 0x06]; -pub const EVENT_TICK: &[u8; 4] = &[0x80, 0x00, 0x00, 0x07]; -pub const EVENT_BAR_STATE_UPDATE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x14]; -pub const EVENT_INPUT: &[u8; 4] = &[0x80, 0x00, 0x00, 0x15]; +pub const EVENT_WORKSPACE: &str = "workspace"; +pub const EVENT_MODE: &str = "mode"; +pub const EVENT_WINDOW: &str = "window"; +pub const EVENT_BARCONFIG_UPDATE: &str = "barconfig_update"; +pub const EVENT_BINDING: &str = "binding"; +pub const EVENT_SHUTDOWN: &str = "shutdown"; +pub const EVENT_TICK: &str = "tick"; +pub const EVENT_BAR_STATE_UPDATE: &str = "bar_state_update"; +pub const EVENT_INPUT: &str = "input"; pub struct Sway { stream: UnixStream, @@ -58,8 +60,12 @@ impl Sway { } /// Subscribe the IPC connection to the event listed in the payload - pub fn subscribe(&mut self, payload: &str) -> Result { - self.run(payload, IPC_SUBSCRIBE) + pub fn subscribe(&mut self, events: Vec<&str>) -> Result { + let events = match self.parse_events(events) { + Ok(events) => events, + Err(e) => return Err(e), + }; + self.run(&events, IPC_SUBSCRIBE) } /// Get the list of current outputs @@ -147,6 +153,13 @@ impl Sway { Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())), } } + + fn parse_events(&mut self, events: Vec<&str>) -> Result { + match serde_json::to_string(&events) { + Ok(json) => Ok(json), + Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())), + } + } } #[test] @@ -175,7 +188,7 @@ fn get_workspaces() { #[test] fn subscribe() { - match Sway::new(env!("SWAYSOCK")).unwrap().subscribe("") { + match Sway::new(env!("SWAYSOCK")).unwrap().subscribe(vec!(EVENT_WORKSPACE, EVENT_MODE)) { Ok(response) => assert_eq!(response.success, true), Err(e) => panic!("{}", e.to_string()), } @@ -242,7 +255,10 @@ fn get_config() { #[test] fn send_tick() { - todo!() + match Sway::new(env!("SWAYSOCK")).unwrap().send_tick("") { + Ok(response) => assert_eq!(response.success, true), + Err(e) => panic!("{}", e.to_string()), + } } #[test]