about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-25 03:40:14 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-25 04:16:31 -0700
commitb65303ede0750e9d83df4d2c5df67f143c9171c4 (patch)
treea8d15bdaa3493ed7a8b66092e6f58cf6ebe1909f
parent5ec7ae026ce9d8c48ef5f0590a300fa308efa487 (diff)
i3 mode module fully works now; some API changes
most notably, modules now have a way to request immediate redrawing

Change-Id: Id060c88d3ab484addfd8ca574fbc4e2ffdbb5c8f
Force-Push: yes
-rw-r--r--src/facility/i3.rs24
-rw-r--r--src/main.rs19
-rw-r--r--src/module.rs8
-rw-r--r--src/module/battery.rs4
-rw-r--r--src/module/i3_mode.rs42
-rw-r--r--src/module/time.rs4
6 files changed, 84 insertions, 17 deletions
diff --git a/src/facility/i3.rs b/src/facility/i3.rs
index d914548..53258f8 100644
--- a/src/facility/i3.rs
+++ b/src/facility/i3.rs
@@ -530,3 +530,27 @@ fn parse_success_object(json: &[u8]) -> Result<bool> {
   }
 }
 
+
+pub fn parse_change_string(json: &[u8]) -> Result<String> {
+  let mut parser = SliceJsonParser::new(json);
+
+  if let JsonEvent::StartObject = parser.parse_next()? { } else {
+    return Err(Error::custom("Reading i3 JSON, expected object start."));
+  }
+
+  if let JsonEvent::ObjectKey(key) = parser.parse_next()? {
+    if key == "change" {
+      if let JsonEvent::String(result) = parser.parse_next()? {
+        /* That's our answer; exit early. */
+        return Ok(result.into_owned());
+      } else {
+        return Err(Error::custom("Reading i3 JSON, expected string."));
+      }
+    } else {
+      return Err(Error::custom("Reading i3 JSON, expected \"change\" key."));
+    }
+  } else {
+    return Err(Error::custom("Reading i3 JSON, expected object key."));
+  }
+}
+
diff --git a/src/main.rs b/src/main.rs
index a2baf8f..225ca2c 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -117,9 +117,13 @@ fn main() -> ExitCode {
 async fn async_main(_shutdown_sender: Sender<()>) -> Result<()> {
   let display = Arc::new(Display::new()?);
   let configuration = DockConfiguration::new(&display)?;
-  let dock = Dock::new(Arc::clone(&display), configuration).await?;
 
   let (redraw_sender, redraw_receiver) = async_channel::unbounded();
+
+  let dock = Dock::new(Arc::clone(&display),
+                       configuration,
+                       Sender::clone(&redraw_sender)).await?;
+
   let event_task = EXECUTOR.spawn(
           event_loop(display, Arc::clone(&dock),
                      Sender::clone(&redraw_sender)));
@@ -162,7 +166,9 @@ impl DockConfiguration {
 
 
 impl Dock {
-  async fn new(display: Arc<Display>, configuration: DockConfiguration)
+  async fn new(display: Arc<Display>,
+               configuration: DockConfiguration,
+               redraw_sender: Sender<()>)
     -> Result<Arc<Mutex<Dock>>>
   {
     let screen = display.screen();
@@ -218,7 +224,8 @@ impl Dock {
 
       modules.push((Arc::new(Mutex::new(TimeModule::new(
                         facilities.borrow_mut(),
-                        &EXECUTOR).await?.as_box())),
+                        &EXECUTOR,
+                        Sender::clone(&redraw_sender)).await?.as_box())),
                     ModuleFraming
       {
         alignment: Alignment::Right,
@@ -226,7 +233,8 @@ impl Dock {
 
       modules.push((Arc::new(Mutex::new(BatteryModule::new(
                         facilities.borrow_mut(),
-                        &EXECUTOR).await?.as_box())),
+                        &EXECUTOR,
+                        Sender::clone(&redraw_sender)).await?.as_box())),
                     ModuleFraming
       {
         alignment: Alignment::Right,
@@ -234,7 +242,8 @@ impl Dock {
 
       modules.push((Arc::new(Mutex::new(I3ModeModule::new(
                         facilities.borrow_mut(),
-                        &EXECUTOR).await?.as_box())),
+                        &EXECUTOR,
+                        Sender::clone(&redraw_sender)).await?.as_box())),
                     ModuleFraming
       {
         alignment: Alignment::Left,
diff --git a/src/module.rs b/src/module.rs
index a716fa7..8d5264f 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -3,6 +3,7 @@ use crate::facility::{ FacilityMap, FacilityType };
 
 use std::time::Duration;
 
+use async_channel::Sender;
 use async_executor::StaticExecutor;
 
 
@@ -21,7 +22,9 @@ pub mod time;
 pub trait CreatableModule: Module + Sized {
   const REQUIRED_FACILITIES: FacilityType;
 
-  async fn new(facilities: &mut FacilityMap, executor: &'static StaticExecutor)
+  async fn new(facilities: &mut FacilityMap,
+               executor: &'static StaticExecutor,
+               redraw_sender: Sender<()>)
       -> Result<Self>;
 
   /* The only way to coerce to dyn is to return as it! */
@@ -33,6 +36,9 @@ pub trait CreatableModule: Module + Sized {
 }
 
 pub trait Module {
+  /*   While it would be nice to make this all async, that wouldn't be
+   * dyn-compatible.
+   */
   fn format(&mut self, facilities: &mut FacilityMap) -> Result<String>;
   fn sleep_duration(&self) -> Option<Duration>;
   fn is_visible(&self) -> bool;
diff --git a/src/module/battery.rs b/src/module/battery.rs
index d276a9c..81733b8 100644
--- a/src/module/battery.rs
+++ b/src/module/battery.rs
@@ -5,6 +5,7 @@ use crate::module::{ CreatableModule, Module };
 
 use std::time::Duration;
 
+use async_channel::Sender;
 use async_executor::StaticExecutor;
 
 
@@ -18,7 +19,8 @@ impl CreatableModule for BatteryModule {
   const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS;
 
   async fn new(facilities: &mut FacilityMap,
-               _executor: &'static StaticExecutor)
+               _executor: &'static StaticExecutor,
+               _redraw_sender: Sender<()>)
     -> Result<BatteryModule>
   {
     let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom(
diff --git a/src/module/i3_mode.rs b/src/module/i3_mode.rs
index d02f12d..c1b6484 100644
--- a/src/module/i3_mode.rs
+++ b/src/module/i3_mode.rs
@@ -1,17 +1,21 @@
 use crate::error::{ Error, Result };
 use crate::facility::{ FacilityMap, FacilityType };
-use crate::facility::i3::{ EventType, I3 };
+use crate::facility::i3::{ parse_change_string, EventType, I3 };
 use crate::module::{ CreatableModule, Module };
 
+use std::sync::Arc;
 use std::time::Duration;
 
-use async_channel::{ unbounded, Receiver };
+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 I3ModeModule {
-  update_task: Task<()>,
+  _update_task: Task<()>,
+  current_mode: Arc<Mutex<String>>,
 }
 
 
@@ -19,7 +23,8 @@ impl CreatableModule for I3ModeModule {
   const REQUIRED_FACILITIES: FacilityType = FacilityType::I3;
 
   async fn new(facilities: &mut FacilityMap,
-               executor: &'static StaticExecutor)
+               executor: &'static StaticExecutor,
+               redraw_sender: Sender<()>)
     -> Result<I3ModeModule>
   {
     let i3: &mut I3 = facilities.get().ok_or(Error::custom(
@@ -31,15 +36,23 @@ impl CreatableModule for I3ModeModule {
       return Err(Error::custom("Unable to subscribe to i3 mode changes."));
     }
 
-    let update_task = executor.spawn(update_loop(receiver));
+    let current_mode = Arc::new(Mutex::new("unknown".to_string()));
 
-    Ok(I3ModeModule { update_task })
+    let update_task = executor.spawn(update_loop(receiver,
+                                                 Arc::clone(&current_mode),
+                                                 redraw_sender));
+
+    Ok(I3ModeModule {
+      _update_task: update_task,
+      current_mode,
+    })
   }
 }
 
 impl Module for I3ModeModule {
   fn format(&mut self, _facilities: &mut FacilityMap) -> Result<String> {
-    Ok("mode goes here".to_string())
+    /* A shame we can't be async. */
+    Ok(format!("--{}--", block_on(self.current_mode.lock())))
   }
 
   fn sleep_duration(&self) -> Option<Duration> {
@@ -52,11 +65,22 @@ impl Module for I3ModeModule {
 }
 
 
-async fn update_loop(receiver: Receiver<(EventType, String)>) {
+async fn update_loop(receiver: Receiver<(EventType, String)>,
+                     current_mode: Arc<Mutex<String>>,
+                     redraw_sender: Sender<()>)
+{
   loop {
     match receiver.recv().await {
       Ok((_, body)) => {
-        println!("it worked??? {}", body);
+        match parse_change_string(body.as_bytes()) {
+          Ok(new_mode) => {
+            *current_mode.lock().await = new_mode;
+            if let Err(error) = redraw_sender.send(()).await {
+              Into::<Error>::into(error).log()
+            }
+          }
+          Err(error) => { error.log(); }
+        }
       }
       Err(error) => {
         Into::<Error>::into(error).log();
diff --git a/src/module/time.rs b/src/module/time.rs
index 8c42aca..b875454 100644
--- a/src/module/time.rs
+++ b/src/module/time.rs
@@ -4,6 +4,7 @@ use crate::module::{ CreatableModule, Module };
 
 use std::time::Duration;
 
+use async_channel::Sender;
 use async_executor::StaticExecutor;
 use time::OffsetDateTime;
 use time::format_description::{ FormatDescriptionV3, parse_owned };
@@ -19,7 +20,8 @@ impl CreatableModule for TimeModule {
   const REQUIRED_FACILITIES: FacilityType = FacilityType::empty();
 
   async fn new(_facilities: &mut FacilityMap,
-               _executor: &'static StaticExecutor)
+               _executor: &'static StaticExecutor,
+               _redraw_sender: Sender<()>)
     -> Result<TimeModule>
   {
     let format_string =