diff options
| author | Irene Knapp <ireneista@irenes.space> | 2026-11-14 02:35:53 -0700 |
|---|---|---|
| committer | Irene Knapp <ireneista@irenes.space> | 2026-11-14 02:35:53 -0700 |
| commit | 815fb567bdfdbc3901148a4b9f49fd42a56c4f27 (patch) | |
| tree | a0fdd871ccf943e1b3d0247b01a359bcec48f078 | |
| parent | d0c5a0eeec74de59a4919a42412d41fafb2593e3 (diff) | |
return a sleep duration to the event loop
this isn't immediately useful, but it will be when threading is added... Force-Push: yes Change-Id: I7abc35826d4ff4a7a63d57f4f7f5e0e0461d23d9
| -rw-r--r-- | src/main.rs | 47 | ||||
| -rw-r--r-- | src/modules/time.rs | 8 | ||||
| -rw-r--r-- | src/x/connection.rs | 17 |
3 files changed, 68 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 60b6648..a61f93a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,9 +3,11 @@ use crate::modules::*; use crate::x::*; use std::sync::Arc; +use std::time::Duration; use xcb::{ Xid }; use xcb::x::{ + ClearArea, ClientMessageData, Gc, ChangeGc, Cw, CreateWindow, @@ -55,7 +57,7 @@ fn main() -> xcb::Result<()> { // #5c01be let foreground = display.allocate_color(23644, 257, 48830)?; - let dock = Dock::new(Arc::clone(&display), DockConfiguration { + let mut dock = Dock::new(Arc::clone(&display), DockConfiguration { thickness: 34, baseline_offset: 2, edge_inset: 12, @@ -68,7 +70,15 @@ fn main() -> xcb::Result<()> { loop { match display.wait_for_event()? { xcb::Event::X(xcb::x::Event::Expose(_event)) => { - let _ = dock.redraw(); // TODO + match dock.redraw() { + Ok(Some(duration)) => { + dock.connection().flush()?; + println!("sleeping"); + std::thread::sleep(duration); + } + Ok(None) => { } + Err(_) => { } // TODO + } } xcb::Event::X(xcb::x::Event::ButtonPress(event)) => { @@ -227,11 +237,14 @@ impl Dock { } - fn redraw(&self) -> xcb::Result<()> { + fn redraw(&mut self) -> xcb::Result<Option<Duration>> { let drawable = Drawable::Window(self.window_id()); let context = GraphicContext::new( Arc::clone(self.display()), drawable)?; + let foo = self.erase_all()?; + println!("{:?}", foo); + if self.configuration.use_rounded_inside_corners || self.configuration.use_rounded_outside_corners { @@ -243,6 +256,34 @@ impl Dock { let time = self.time_module.format(); self.draw_text(&time, Alignment::Right)?; + let mut shortest_duration = None; + + let next_duration = self.time_module.sleep_duration(); + if let Some(old_value) = shortest_duration { + if let Some(new_value) = next_duration + && new_value < old_value + { + shortest_duration = Some(new_value); + } + } else { + shortest_duration = next_duration; + } + + Ok(shortest_duration) + } + + + fn erase_all(&self) -> xcb::Result<()> { + let cookie = self.send_request_checked(&ClearArea { + exposures: true, + window: self.window().window_id(), + x: 0, + y: 0, + width: self.bounds.width, + height: self.bounds.height, + }); + self.check_request(cookie)?; + Ok(()) } diff --git a/src/modules/time.rs b/src/modules/time.rs index 61a0cf8..8f6f417 100644 --- a/src/modules/time.rs +++ b/src/modules/time.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use time::OffsetDateTime; use time::format_description::{ FormatDescriptionV3, parse_owned }; @@ -18,10 +20,14 @@ impl TimeModule { TimeModule { time_format } } - pub fn format(&self) -> String { + pub fn format(&mut self) -> String { let time = OffsetDateTime::now_local().unwrap(); time.format(&self.time_format).unwrap() } + + pub fn sleep_duration(&self) -> Option<Duration> { + Some(Duration::from_millis(1000)) + } } diff --git a/src/x/connection.rs b/src/x/connection.rs index 78e721d..c4e879f 100644 --- a/src/x/connection.rs +++ b/src/x/connection.rs @@ -1,5 +1,6 @@ use crate::x::atoms::Atoms; +use x11::xlib::XFlush; use xcb::XidNew; use xcb::x::{ Atom, InternAtom }; @@ -26,6 +27,22 @@ impl HasConnection for Connection { pub trait ConnectionExt: HasConnection { + fn flush(&self) -> xcb::Result<()> { + self.connection().0.flush()?; + + /* xlib is managing our socket. That means: + * + * we aren't fully flushed + * til we're xlib'fully flushed. + * + * Young people will not have this commercial in their heads. Lucky! + */ + let xlib_display = self.get_xlib_display(); + let _ = unsafe { XFlush(xlib_display) }; + + Ok(()) + } + fn get_setup(&self) -> &xcb::x::Setup { self.connection().0.get_setup() } |