functionality and test for subscribe and send tick events

This commit is contained in:
Michael 2022-12-13 22:18:49 +00:00
parent 5ac5326614
commit aa131bb9a9
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C
2 changed files with 31 additions and 15 deletions

View File

@ -9,8 +9,8 @@ pub struct I3msg<'payload> {
} }
impl<'payload> I3msg<'payload> { impl<'payload> I3msg<'payload> {
pub fn new(payload: &str, payload_type: u32) -> I3msg { pub fn new(payload: &'payload str, payload_type: u32) -> Self {
return I3msg{ Self {
magic_string: "i3-ipc", magic_string: "i3-ipc",
payload_type: payload_type, payload_type: payload_type,
payload: payload, payload: payload,

View File

@ -1,3 +1,5 @@
#![warn(clippy::all, rust_2018_idioms)]
mod types; mod types;
mod i3msg; 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_INPUTS: u32 = 100;
pub const IPC_GET_SEATS: u32 = 101; pub const IPC_GET_SEATS: u32 = 101;
pub const EVENT_WORKSPACE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x00]; pub const EVENT_WORKSPACE: &str = "workspace";
pub const EVENT_MODE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x02]; pub const EVENT_MODE: &str = "mode";
pub const EVENT_WINDOW: &[u8; 4] = &[0x80, 0x00, 0x00, 0x03]; pub const EVENT_WINDOW: &str = "window";
pub const EVENT_BARCONFIG_UPDATE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x04]; pub const EVENT_BARCONFIG_UPDATE: &str = "barconfig_update";
pub const EVENT_BINDING: &[u8; 4] = &[0x80, 0x00, 0x00, 0x05]; pub const EVENT_BINDING: &str = "binding";
pub const EVENT_SHUTDOWN: &[u8; 4] = &[0x80, 0x00, 0x00, 0x06]; pub const EVENT_SHUTDOWN: &str = "shutdown";
pub const EVENT_TICK: &[u8; 4] = &[0x80, 0x00, 0x00, 0x07]; pub const EVENT_TICK: &str = "tick";
pub const EVENT_BAR_STATE_UPDATE: &[u8; 4] = &[0x80, 0x00, 0x00, 0x14]; pub const EVENT_BAR_STATE_UPDATE: &str = "bar_state_update";
pub const EVENT_INPUT: &[u8; 4] = &[0x80, 0x00, 0x00, 0x15]; pub const EVENT_INPUT: &str = "input";
pub struct Sway { pub struct Sway {
stream: UnixStream, stream: UnixStream,
@ -58,8 +60,12 @@ impl Sway {
} }
/// Subscribe the IPC connection to the event listed in the payload /// Subscribe the IPC connection to the event listed in the payload
pub fn subscribe(&mut self, payload: &str) -> Result<Success> { pub fn subscribe(&mut self, events: Vec<&str>) -> Result<Success> {
self.run(payload, IPC_SUBSCRIBE) 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 /// Get the list of current outputs
@ -147,6 +153,13 @@ impl Sway {
Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())), Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())),
} }
} }
fn parse_events(&mut self, events: Vec<&str>) -> Result<String> {
match serde_json::to_string(&events) {
Ok(json) => Ok(json),
Err(e) => return Err(Error::new(ErrorKind::Other, e.to_string())),
}
}
} }
#[test] #[test]
@ -175,7 +188,7 @@ fn get_workspaces() {
#[test] #[test]
fn subscribe() { 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), Ok(response) => assert_eq!(response.success, true),
Err(e) => panic!("{}", e.to_string()), Err(e) => panic!("{}", e.to_string()),
} }
@ -242,7 +255,10 @@ fn get_config() {
#[test] #[test]
fn send_tick() { 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] #[test]