diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-25 03:40:14 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-25 04:16:31 -0700 |
| commit | b65303ede0750e9d83df4d2c5df67f143c9171c4 (patch) | |
| tree | a8d15bdaa3493ed7a8b66092e6f58cf6ebe1909f /src/module | |
| parent | 5ec7ae026ce9d8c48ef5f0590a300fa308efa487 (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
Diffstat (limited to 'src/module')
| -rw-r--r-- | src/module/battery.rs | 4 | ||||
| -rw-r--r-- | src/module/i3_mode.rs | 42 | ||||
| -rw-r--r-- | src/module/time.rs | 4 |
3 files changed, 39 insertions, 11 deletions
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(¤t_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 = |