diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-18 16:38:51 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-18 16:38:51 -0700 |
| commit | dcf0c6ce5ce21647640b2b35715ede50a5a97881 (patch) | |
| tree | 7cf9e269511074553181883df16a4b1e6269bb37 | |
| parent | 0bcfe7b468a83436a077824ca60e09c5fdd3f803 (diff) | |
dyn Module
fun this also seemed a good time to clean up the bibliography and publish it Force-Push: yes Change-Id: Ia8014fb84c1af37be4f3c915b8b8f70bc2022034
| -rw-r--r-- | README.txt | 26 | ||||
| -rw-r--r-- | src/main.rs | 28 | ||||
| -rw-r--r-- | src/module.rs | 17 | ||||
| -rw-r--r-- | src/module/time.rs | 13 |
4 files changed, 68 insertions, 16 deletions
diff --git a/README.txt b/README.txt new file mode 100644 index 0000000..57136c1 --- /dev/null +++ b/README.txt @@ -0,0 +1,26 @@ + This program is not ready for use yet, but here it is anyway. + + Writing this program in 2026, we aren't sure how we'd have learned the basics +of x11, or found reference material, if we didn't already know a lot of it. +Therefore we feel a particular obligation to credit the sources we did end up +using, knowing that learners ten years from now may have an even harder time +finding any. We hope our bibliography here is useful to y'all, whenever you're +reading. + + As someone learning xcb for the first time while already familiar with xlib, +we found freekdesktop's porting guide[1] to be useful. They are also the party +who hosts the most easily searchable copy of the protocol reference[2], but +institutional support for xorg has proven fickle over the decades, so we urge +readers to obtain a local copy instead. Regarding xft, as an entry point we +liked Andrew Chant's overview[3] and there are several deeper resources by +keithp that were helpful[4] or interesting[5]. For Rust topics, the deep +tutorial by quinedot[5] was particularly helpful, especially around the +subtleties of dyn. + +[1] https://xcb.freedesktop.org/xlibtoxcbtranslationguide/ +[2] https://xorg.freedesktop.org/archive/X11R7.7/doc/xproto/x11protocol.html +[3] http://individual.utoronto.ca/chant/X/ +[4] https://www.keithp.com/~keithp/render/Xft.tutorial +[5] https://keithp.com/~keithp/talks/usenix2003/net.pdf +[6] https://quinedot.github.io/rust-learning/ + diff --git a/src/main.rs b/src/main.rs index 2d3c1d1..38a993f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,11 @@ struct Dock { font: Arc<Font>, baseline_position: i16, facilities: FacilityMap, - time_module: Arc<Mutex<TimeModule>>, + modules: Vec<(Arc<Mutex<Box<dyn Module>>>, ModuleFraming)>, +} + +struct ModuleFraming { + alignment: Alignment, } unsafe impl Send for Dock { } @@ -175,7 +179,12 @@ impl Dock { let facilities = FacilityMap::new(); - let time_module = TimeModule::new()?; + let mut modules = Vec::new(); + modules.push((Arc::new(Mutex::new(TimeModule::new()?.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. @@ -187,7 +196,7 @@ impl Dock { font, baseline_position, facilities, - time_module, + modules, }; dock.set_string_property(&xcb::x::ATOM_WM_NAME, b"sandbar")?; @@ -297,13 +306,16 @@ impl Dock { self.draw_text("playful cool fic tech nonfic", Alignment::Left)?; - let time = self.time_module.lock()?.format(); - self.draw_text(&time, Alignment::Right)?; + for (module, framing) in &self.modules { + let text = module.lock()?.format(); + self.draw_text(&text, framing.alignment)?; + } let mut shortest_duration = None; - - let next_duration = self.time_module.lock()?.sleep_duration(); - shortest_duration = shorter_duration(shortest_duration, next_duration); + for (module, _) in &self.modules { + let next_duration = module.lock()?.sleep_duration(); + shortest_duration = shorter_duration(shortest_duration, next_duration); + } Ok(shortest_duration) } diff --git a/src/module.rs b/src/module.rs index c859bd3..d10e436 100644 --- a/src/module.rs +++ b/src/module.rs @@ -10,10 +10,23 @@ pub use time::TimeModule; pub mod time; -pub trait Module { +/* As with Facility, we need a separate trait for the stuff that wouldn't be + * dyn-compatible. + */ +pub trait CreatableModule: Module + Sized { const REQUIRED_FACILITIES: FacilityType; - fn new() -> Result<Arc<Mutex<Self>>>; + fn new() -> Result<Self>; + + /* The only way to coerce to dyn is to return as it! */ + fn as_box<'a>(self) -> Box<dyn Module + 'a> + where Self: 'a + { + Box::new(self) + } +} + +pub trait Module { fn format(&mut self) -> String; fn sleep_duration(&self) -> Option<Duration>; } diff --git a/src/module/time.rs b/src/module/time.rs index f334fd0..0388f62 100644 --- a/src/module/time.rs +++ b/src/module/time.rs @@ -1,8 +1,7 @@ use crate::error::Result; use crate::facility::FacilityType; -use crate::module::Module; +use crate::module::{ CreatableModule, Module }; -use std::sync::{ Arc, Mutex }; use std::time::Duration; use time::OffsetDateTime; @@ -15,22 +14,24 @@ pub struct TimeModule { } -impl Module for TimeModule { +impl CreatableModule for TimeModule { const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); - fn new() -> Result<Arc<Mutex<TimeModule>>> { + fn new() -> Result<TimeModule> { let format_string = "[weekday repr:short] [year]-[month]-[day] ".to_owned() + "[hour repr:24]:[minute]:[second]"; let time_format = parse_owned::<3>(&format_string).unwrap(); - Ok(Arc::new(Mutex::new(TimeModule { + Ok(TimeModule { time_format, most_recent_report: None, - }))) + }) } +} +impl Module for TimeModule { fn format(&mut self) -> String { let time = OffsetDateTime::now_local().unwrap(); |