diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-25 01:30:37 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-25 01:30:37 -0700 |
| commit | f49645d0bef662c743e04cd95f9cf6a07566c738 (patch) | |
| tree | 215798143a1463e79ee79bb66e66777f2c14438a /src/facility/i3.rs | |
| parent | 893c15a2eeb16156c0b4f3e4ea815098b8f477be (diff) | |
event subscription skeleton
Force-Push: yes Change-Id: If7ec86fbdc6147031c5f699f1efe6f84e246dfc9
Diffstat (limited to 'src/facility/i3.rs')
| -rw-r--r-- | src/facility/i3.rs | 91 |
1 files changed, 76 insertions, 15 deletions
diff --git a/src/facility/i3.rs b/src/facility/i3.rs index acd8a56..a7a6991 100644 --- a/src/facility/i3.rs +++ b/src/facility/i3.rs @@ -16,6 +16,7 @@ use async_fs::unix::FileTypeExt; use async_lock::Mutex; use async_net::unix::UnixStream; use async_task::Task; +use bitflags::bitflags; use futures_lite::{ AsyncReadExt, AsyncWriteExt, StreamExt }; use futures_lite::io::{ split, ReadHalf, WriteHalf }; @@ -31,6 +32,36 @@ pub struct I3 { pending_commands: Arc<Mutex<Vec<(u32, Sender<String>)>>>, } +bitflags! { + #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] + pub struct EventType: u32 { + const WORKSPACE = 0x00000001; + const OUTPUT = 0x00000002; + const MODE = 0x00000004; + const WINDOW = 0x00000008; + const BARCONFIG_UPDATE = 0x00000010; + const BINDING = 0x00000020; + const SHUTDOWN = 0x00000040; + const TICK = 0x00000080; + } +} + +impl EventType { + fn name(&self) -> Result<&str> { + match *self { + EventType::WORKSPACE => Ok("workspace"), + EventType::OUTPUT => Ok("output"), + EventType::MODE => Ok("mode"), + EventType::WINDOW => Ok("window"), + EventType::BARCONFIG_UPDATE => Ok("barconfig_update"), + EventType::BINDING => Ok("binding"), + EventType::SHUTDOWN => Ok("shutdown"), + EventType::TICK => Ok("tick"), + _ => Err(Error::custom("Only one event type at a time.")) + } + } +} + impl Facility for I3 { fn as_any_mut(&mut self) -> &mut dyn Any { @@ -56,13 +87,14 @@ impl CreatableFacility for I3 { let receive_task = executor.spawn( receive_loop(Arc::clone(&read_socket), - Arc::clone(&pending_commands))); + Arc::clone(&pending_commands))); let mut i3 = I3 { read_socket, write_socket, receive_task, pending_commands }; - let response = i3.command(2, "[ \"mode\" ]").await?; + let (sender, _) = unbounded(); + let response = i3.subscribe(EventType::MODE, sender).await?; println!("it worked??? {}", response); Ok(i3) @@ -147,6 +179,34 @@ impl I3 { Ok(response) } + + async fn subscribe(&mut self, event_types: EventType, + sender: Sender<(EventType, String)>) + -> Result<bool> + { + let mut request = String::new(); + request.push_str("["); + + let mut is_first = true; + for event_type in event_types.iter() { + if !is_first { + request.push_str(","); + } + + request.push_str(" \""); + request.push_str(event_type.name()?); + request.push_str("\""); + + is_first = false; + } + + request.push_str(" ]"); + + let result = self.command(2, &request).await?; + + println!("subscribed? {}", result); + Ok(true) + } } @@ -252,26 +312,27 @@ async fn receive_loop(socket: Arc<Mutex<ReadHalf<UnixStream>>>, } else { let mut pending_commands = pending_commands.lock().await; - let mut found_index = None; + let mut found_index = None; for (index, (pending_type_code, _)) in pending_commands.iter().enumerate() { if *pending_type_code == type_code { - found_index = Some(index); - break; - } + found_index = Some(index); + break; + } } - if let Some(index) = found_index { - let (_, sender) = pending_commands.remove(index); + if let Some(index) = found_index { + let (_, sender) = pending_commands.remove(index); - if let Err(error) = sender.send(body).await { - Into::<Error>::into(error).log(); - } - } else { - // TODO - println!("received unexpected response type code {} body {}", type_code, body); - } + if let Err(error) = sender.send(body).await { + Into::<Error>::into(error).log(); + } + } else { + Error::custom(&format!( + "Unexpected response from i3 type {} body {}", + type_code, body)).log(); + } } } Err(error) => { error.log() } |