From b65303ede0750e9d83df4d2c5df67f143c9171c4 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Wed, 25 Nov 2026 03:40:14 -0700 Subject: 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 --- src/module/battery.rs | 4 +++- src/module/i3_mode.rs | 42 +++++++++++++++++++++++++++++++++--------- src/module/time.rs | 4 +++- 3 files changed, 39 insertions(+), 11 deletions(-) (limited to 'src/module') 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 { 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>, } @@ -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 { 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(¤t_mode), + redraw_sender)); + + Ok(I3ModeModule { + _update_task: update_task, + current_mode, + }) } } impl Module for I3ModeModule { fn format(&mut self, _facilities: &mut FacilityMap) -> Result { - 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 { @@ -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>, + 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::::into(error).log() + } + } + Err(error) => { error.log(); } + } } Err(error) => { Into::::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 { let format_string = -- cgit 1.4.1