about summary refs log tree commit diff
path: root/src/module
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-25 21:25:06 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-25 21:25:06 -0700
commitb3cb3553ab5c4649b0fe6b5f94ac42c5b69e8f89 (patch)
treeed33922a84eeaa541d8a5c772445b81be1eb10fa /src/module
parentc312a47101bf1787e7bbc44c409e15315684fd19 (diff)
refactor JSON code into its own file, now a full parser
also, update to the latest version of the JSON library. it's under active development and had a release today. huh!

Force-Push: yes
Change-Id: I0e162f64c963744cf786a85d00c83dbabade05be
Diffstat (limited to 'src/module')
-rw-r--r--src/module/i3_workspace.rs112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/module/i3_workspace.rs b/src/module/i3_workspace.rs
new file mode 100644
index 0000000..d70bd3d
--- /dev/null
+++ b/src/module/i3_workspace.rs
@@ -0,0 +1,112 @@
+use crate::error::{ Error, Result };
+use crate::facility::{ FacilityMap, FacilityType };
+use crate::facility::i3::{ EventType, I3 };
+use crate::module::{ CreatableModule, Module };
+
+use std::sync::Arc;
+use std::time::Duration;
+
+use async_channel::{ unbounded, Sender, Receiver };
+use async_executor::StaticExecutor;
+use async_lock::Mutex;
+use async_task::Task;
+use futures_lite::future::block_on;
+
+
+pub struct I3WorkspaceModule {
+  _update_task: Task<()>,
+  workspaces: Arc<Mutex<Vec<Workspace>>>,
+}
+
+struct Workspace {
+  name: String,
+  is_focused: bool,
+}
+
+
+impl CreatableModule for I3WorkspaceModule {
+  const REQUIRED_FACILITIES: FacilityType = FacilityType::I3;
+
+  async fn new(facilities: &mut FacilityMap,
+               executor: &'static StaticExecutor,
+               redraw_sender: Sender<()>)
+    -> Result<I3WorkspaceModule>
+  {
+    let i3: &mut I3 = facilities.get().ok_or(Error::custom(
+            "I3 workspace module can't access i3 facility."))?;
+
+    let (sender, receiver) = unbounded();
+
+    if !i3.subscribe(EventType::WORKSPACE, sender).await? {
+      return Err(Error::custom("Unable to subscribe to i3 workspace changes."));
+    }
+
+    let workspaces = Arc::new(Mutex::new(Vec::new()));
+
+    let update_task = executor.spawn(update_loop(receiver,
+                                                 Arc::clone(&workspaces),
+                                                 redraw_sender));
+
+    Ok(I3WorkspaceModule {
+      _update_task: update_task,
+      workspaces,
+    })
+  }
+}
+
+impl Module for I3WorkspaceModule {
+  fn format(&mut self, _facilities: &mut FacilityMap) -> Result<String> {
+    /* A shame we can't be async. */
+    let workspaces = self.workspaces.lock();
+
+    let mut text = String::new();
+    let mut is_first = true;
+    for name in vec!["playful", "cool", "fic", "tech", "nonfic"] {
+      if is_first {
+	text.push_str(" ");
+      }
+
+      text.push_str(name);
+
+      is_first = false;
+    }
+
+    Ok(text)
+  }
+
+  fn sleep_duration(&self) -> Option<Duration> {
+    None
+  }
+
+  fn is_visible(&self) -> bool {
+    block_on(self.workspaces.lock()).len() > 0
+  }
+}
+
+
+async fn update_loop(receiver: Receiver<(EventType, String)>,
+                     workspaces: Arc<Mutex<Vec<Workspace>>>,
+                     redraw_sender: Sender<()>)
+{
+  loop {
+    match receiver.recv().await {
+      Ok((_, body)) => {
+        println!("{}", body);
+        //match parse_change_string(body.as_bytes()) {
+          //Ok(new_workspaces) => {
+            let mut _workspaces = workspaces.lock().await;
+
+            if let Err(error) = redraw_sender.send(()).await {
+              Into::<Error>::into(error).log()
+            }
+          //}
+          //Err(error) => { error.log(); }
+        //}
+      }
+      Err(error) => {
+        Into::<Error>::into(error).log();
+      }
+    }
+  }
+}
+