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/facility/i3.rs | 24 ++++++++++++++++++++++++ src/main.rs | 19 ++++++++++++++----- src/module.rs | 8 +++++++- src/module/battery.rs | 4 +++- src/module/i3_mode.rs | 42 +++++++++++++++++++++++++++++++++--------- src/module/time.rs | 4 +++- 6 files changed, 84 insertions(+), 17 deletions(-) (limited to 'src') 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 { } } + +pub fn parse_change_string(json: &[u8]) -> Result { + 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, configuration: DockConfiguration) + async fn new(display: Arc, + configuration: DockConfiguration, + redraw_sender: Sender<()>) -> Result>> { 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; /* 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; fn sleep_duration(&self) -> Option; 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 { 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