From 6936dc8c1222a84f46b910a1907ff7fc7689b886 Mon Sep 17 00:00:00 2001 From: Irene Knapp Date: Sat, 21 Nov 2026 16:46:25 -0700 Subject: the envsys facility is fully passed through to the battery module now yay!!!! Force-Push: yes Change-Id: I2b1a0ca9858edc0b5d25d6268282fe3a90b99de5 --- src/facility/envsys.rs | 96 ++++++++++++++++++++++++++++++++------------------ src/main.rs | 38 ++++++++++++++------ src/module.rs | 6 ++-- src/module/battery.rs | 23 +++++++++--- src/module/time.rs | 8 ++--- 5 files changed, 114 insertions(+), 57 deletions(-) (limited to 'src') diff --git a/src/facility/envsys.rs b/src/facility/envsys.rs index dfecf94..ba2e53a 100644 --- a/src/facility/envsys.rs +++ b/src/facility/envsys.rs @@ -14,8 +14,10 @@ use rustix::mm::munmap; pub struct EnvSys { sysmon: File, + dictionary: Dictionary, } + #[repr(C)] #[derive(Debug)] struct PropertyListReference { @@ -34,14 +36,10 @@ impl CreatableFacility for EnvSys { const FACILITY_TYPE: FacilityType = FacilityType::ENVSYS; async fn new() -> Result { - println!("loading envsys"); let sysmon = File::open("/dev/sysmon").await?; + let dictionary = get_dictionary(&sysmon).await?; - let envsys = EnvSys { sysmon }; - - envsys.get_dictionary().await?; - - Ok(envsys) + Ok(EnvSys { sysmon, dictionary }) } } @@ -55,35 +53,63 @@ impl EnvSys { const ENVSYS_REMOVEPROPS: Opcode = read_write::(b'E', 2); - async fn get_dictionary(&self) -> Result { - let call = unsafe { - Getter::<{ Self::ENVSYS_GETDICTIONARY }, PropertyListReference>::new() - }; - - /* If the call fails, we can safely return because there's no resulting - * memory allocation. - */ - let result = unsafe { ioctl(&self.sysmon, call) }?; - - /* We're expecting the final byte to be a null, acting as a C string - * terminator. We ignore it. - */ - let xml: &[u8] = unsafe { from_raw_parts(result.data, result.size - 1) }; - - /* This returns a Result, but we want to clean up before we peek inside. - */ - let dictionary = parse_dictionary(xml); - - /* So. Fun story. Inspecting NetBSD src/common/lib/libprop/prop_kern.c, - * we find that the kernel creates a new memory mapping for the dictionary - * it returns. When we're done, we're expected to munmap() it. Wild. - * - * Anyway, if munmap() fails we have no way to clean up regardless, so we - * just return. - */ - unsafe { munmap(result.data as *mut c_void, result.size) }?; - - dictionary + pub async fn refresh_dictionary(&mut self) -> Result<()> { + self.dictionary = get_dictionary(&self.sysmon).await?; + + Ok(()) + } + + pub fn device_iter(&self) -> impl Iterator { + let hash_iterator = self.dictionary.iter(); + + DeviceNames { hash_iterator } + } +} + + +struct DeviceNames<'a> { + hash_iterator: std::collections::hash_map::Iter<'a, String, Value>, +} + +impl<'a> Iterator for DeviceNames<'a> { + type Item = &'a str; + + fn next(&mut self) -> Option { + if let Some((key, _)) = self.hash_iterator.next() { + Some(key) + } else { None } } } + +async fn get_dictionary(sysmon: &File) -> Result { + let call = unsafe { + Getter::<{ EnvSys::ENVSYS_GETDICTIONARY }, PropertyListReference>::new() + }; + + /* If the call fails, we can safely return because there's no resulting + * memory allocation. + */ + let result = unsafe { ioctl(&sysmon, call) }?; + + /* We're expecting the final byte to be a null, acting as a C string + * terminator. We ignore it. + */ + let xml: &[u8] = unsafe { from_raw_parts(result.data, result.size - 1) }; + + /* This returns a Result, but we want to clean up before we peek inside. + */ + let dictionary = parse_dictionary(xml); + + /* So. Fun story. Inspecting NetBSD src/common/lib/libprop/prop_kern.c, + * we find that the kernel creates a new memory mapping for the dictionary + * it returns. When we're done, we're expected to munmap() it. Wild. + * + * Anyway, if munmap() fails we have no way to clean up regardless, so we + * just return. + */ + unsafe { munmap(result.data as *mut c_void, result.size) }?; + + dictionary +} + diff --git a/src/main.rs b/src/main.rs index 356652b..3753292 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use crate::geometry::*; use crate::module::*; use crate::x::*; +use std::borrow::BorrowMut; use std::process::ExitCode; use std::sync::Arc; use std::sync::Mutex; @@ -56,7 +57,7 @@ struct Dock { bounds: Arc>, font: Arc, baseline_position: i16, - facilities: FacilityMap, + facilities: Arc>, modules: Vec<(Arc>>, ModuleFraming)>, } @@ -181,17 +182,30 @@ impl Dock { required_facilities |= BatteryModule::REQUIRED_FACILITIES; required_facilities |= TimeModule::REQUIRED_FACILITIES; - let mut facilities = FacilityMap::new(); + let facilities = Arc::new(Mutex::new(FacilityMap::new())); for facility_type in required_facilities.iter() { - facilities.insert(facility_type.clone(), facility_type.new().await?); + facilities.lock()?.insert(facility_type.clone(), + facility_type.new().await?); } let mut modules = Vec::new(); - modules.push((Arc::new(Mutex::new(TimeModule::new()?.as_box())), - ModuleFraming { - alignment: Alignment::Right, - })); + let mut facilities = facilities.lock()?; + + modules.push((Arc::new(Mutex::new(TimeModule::new( + facilities.borrow_mut())?.as_box())), + ModuleFraming + { + alignment: Alignment::Right, + })); + + modules.push((Arc::new(Mutex::new(BatteryModule::new( + facilities.borrow_mut())?.as_box())), + ModuleFraming + { + alignment: Alignment::Right, + })); + } /* We start is_visible at false. Since redraw events set it to true, this * won't make us miss any drawing. @@ -313,9 +327,13 @@ impl Dock { self.draw_text("playful cool fic tech nonfic", Alignment::Left)?; - for (module, framing) in &self.modules { - let text = module.lock()?.format(); - self.draw_text(&text, framing.alignment)?; + { + let mut facilities = self.facilities.lock()?; + + for (module, framing) in &self.modules { + let text = module.lock()?.format(facilities.borrow_mut())?; + self.draw_text(&text, framing.alignment)?; + } } let mut shortest_duration = None; diff --git a/src/module.rs b/src/module.rs index 9d4baa0..6074457 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,5 +1,5 @@ use crate::error::Result; -use crate::facility::FacilityType; +use crate::facility::{ FacilityMap, FacilityType }; use std::time::Duration; @@ -17,7 +17,7 @@ pub mod time; pub trait CreatableModule: Module + Sized { const REQUIRED_FACILITIES: FacilityType; - fn new() -> Result; + fn new(facilities: &mut FacilityMap) -> Result; /* The only way to coerce to dyn is to return as it! */ fn as_box<'a>(self) -> Box @@ -28,7 +28,7 @@ pub trait CreatableModule: Module + Sized { } pub trait Module { - fn format(&mut self) -> String; + fn format(&mut self, facilities: &mut FacilityMap) -> Result; fn sleep_duration(&self) -> Option; } diff --git a/src/module/battery.rs b/src/module/battery.rs index 05f551f..8df4d31 100644 --- a/src/module/battery.rs +++ b/src/module/battery.rs @@ -1,5 +1,6 @@ -use crate::error::Result; -use crate::facility::FacilityType; +use crate::error::{ Error, Result }; +use crate::facility::{ FacilityMap, FacilityType }; +use crate::facility::envsys::EnvSys; use crate::module::{ CreatableModule, Module }; use std::time::Duration; @@ -12,15 +13,27 @@ pub struct BatteryModule { impl CreatableModule for BatteryModule { const REQUIRED_FACILITIES: FacilityType = FacilityType::ENVSYS; - fn new() -> Result { + fn new(facilities: &mut FacilityMap) -> Result { + let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( + "Battery module can't access envsys facility."))?; + + for name in envsys.device_iter() { + if name.starts_with("acpibat") { + println!("battery {}", name); + } + } + Ok(BatteryModule { }) } } impl Module for BatteryModule { - fn format(&mut self) -> String { - "battery".to_string() + fn format(&mut self, facilities: &mut FacilityMap) -> Result { + let envsys: &mut EnvSys = facilities.get().ok_or(Error::custom( + "Battery module can't access envsys facility."))?; + + Ok("battery".to_string()) } fn sleep_duration(&self) -> Option { diff --git a/src/module/time.rs b/src/module/time.rs index 0388f62..777afdd 100644 --- a/src/module/time.rs +++ b/src/module/time.rs @@ -1,5 +1,5 @@ use crate::error::Result; -use crate::facility::FacilityType; +use crate::facility::{ FacilityMap, FacilityType }; use crate::module::{ CreatableModule, Module }; use std::time::Duration; @@ -17,7 +17,7 @@ pub struct TimeModule { impl CreatableModule for TimeModule { const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); - fn new() -> Result { + fn new(_facilities: &mut FacilityMap) -> Result { let format_string = "[weekday repr:short] [year]-[month]-[day] ".to_owned() + "[hour repr:24]:[minute]:[second]"; @@ -32,12 +32,12 @@ impl CreatableModule for TimeModule { } impl Module for TimeModule { - fn format(&mut self) -> String { + fn format(&mut self, _facilities: &mut FacilityMap) -> Result { let time = OffsetDateTime::now_local().unwrap(); self.most_recent_report = Some(time); - time.format(&self.time_format).unwrap() + Ok(time.format(&self.time_format).unwrap()) } fn sleep_duration(&self) -> Option { -- cgit 1.4.1