about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-11-17 03:00:43 -0700
committerIrene Knapp <ireneista@irenes.space>2026-11-17 03:00:43 -0700
commit0a3ceb20be3c174641f3e1fb9eee600edbfcd90d (patch)
tree97a01b9d39ca1ea3f2a861382fed1189e6f41276 /src
parentbcc115777299fd67074f70cc57cea13a92a63e70 (diff)
yes it's true, the whole visibility sleep thing works now! yay :)
now the way is clear to do the nontrivial modules

Force-Push: yes
Change-Id: I0df07a29cf10646f3ce47b9364d9ec44bbc0cd47
Diffstat (limited to 'src')
-rw-r--r--src/main.rs59
1 files changed, 43 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs
index a8c6c6e..a397217 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -23,6 +23,7 @@ use xcb::x::{
   EventMask,
   PolyFillArc, PolyFillRectangle,
   Screen,
+  Visibility,
   WindowClass
 };
 
@@ -49,12 +50,15 @@ struct DockConfiguration {
 struct Dock {
   configuration: DockConfiguration,
   window: Window,
+  is_visible: bool,
   bounds: Arc<Rectangle<u16>>,
   font: Arc<Font>,
   baseline_position: i16,
   time_module: Arc<Mutex<TimeModule>>,
 }
 
+unsafe impl Send for Dock { }
+
 impl HasWindow for Dock {
   fn window(&self) -> &Window { &self.window }
 }
@@ -91,20 +95,19 @@ async fn async_main(_shutdown_sender: Sender<()>) -> Result<()> {
 
   let (redraw_sender, redraw_receiver) = async_channel::unbounded();
   let event_task = EXECUTOR.spawn(
-          event_loop(display, Sender::clone(&redraw_sender)));
+          event_loop(display, Arc::clone(&dock),
+                     Sender::clone(&redraw_sender)));
 
   let (sleep_sender, sleep_receiver) = async_channel::unbounded();
   let sleep_task = EXECUTOR.spawn(sleep_loop(sleep_receiver, redraw_sender));
 
-  /*   Recall that Dock is non-Send, because it holds pointers to xft
-   * state, which are opaque from our perspective. Thus, we have to do
-   * this on the primary thread.
-   *
-   *   Or we could Mutex-ify everything, but this is simpler.
+  /*   Since we declared Dock as Send, we could do this on another
+   * thread, but it's easier to do it on the primary one.
    */
   redraw_loop(dock, redraw_receiver, sleep_sender).await?;
 
   event_task.cancel();
+  sleep_task.cancel();
 
   Ok(())
 }
@@ -152,7 +155,9 @@ impl Dock {
       visual: screen.root_visual(),
       value_list: &[
         Cw::BackPixel(configuration.background.as_pixel()),
-        Cw::EventMask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS),
+        Cw::EventMask(EventMask::EXPOSURE
+                      | EventMask::VISIBILITY_CHANGE
+                      | EventMask::BUTTON_PRESS),
       ],
     })?;
 
@@ -167,8 +172,12 @@ impl Dock {
 
     let time_module = TimeModule::new();
 
+    /*   We start is_visible at false. Since redraw events set it to true, this
+     * won't make us miss any drawing.
+     */
     let dock = Dock {
       window, configuration,
+      is_visible: false,
       bounds: Arc::new(dock_bounds),
       font,
       baseline_position,
@@ -272,8 +281,7 @@ impl Dock {
     let context = GraphicContext::new(
                       Arc::clone(self.display()), drawable)?;
 
-    let foo = self.erase_all()?;
-    println!("{:?}", foo);
+    self.erase_all()?;
 
     if self.configuration.use_rounded_inside_corners
        || self.configuration.use_rounded_outside_corners
@@ -421,14 +429,32 @@ impl HasConnection for Dock {
 }
 
 
-async fn event_loop(display: Arc<Display>, sender: Sender<()>) {
+async fn event_loop(display: Arc<Display>,
+                    dock: Arc<Mutex<Dock>>,
+                    redraw_sender: Sender<()>)
+{
   loop {
-    println!("waiting for event");
     match display.wait_for_event() {
       result@Err(_) => log_and_ignore_errors(result),
 
       Ok(xcb::Event::X(xcb::x::Event::Expose(_event))) => {
-        log_and_ignore_errors(sender.send(()).await);
+        if let Ok(mut dock) = dock.lock() {
+          dock.is_visible = true;
+        }
+
+        log_and_ignore_errors(redraw_sender.send(()).await);
+      }
+
+      Ok(xcb::Event::X(xcb::x::Event::VisibilityNotify(event))) => {
+        if let Ok(mut dock) = dock.lock() {
+          if event.window() == dock.window.window_id() {
+            dock.is_visible = event.state() != Visibility::FullyObscured;
+
+            /*   We don't initiate a redraw here, instead waiting for the
+             * redraw event.
+             */
+          }
+        }
       }
 
       Ok(xcb::Event::X(xcb::x::Event::ButtonPress(event))) => {
@@ -454,9 +480,11 @@ async fn sleep_loop(sleep_receiver: Receiver<Duration>,
 {
   loop {
     if let Ok(duration) = sleep_receiver.recv().await {
-      println!("sleeping");
       std::thread::sleep(duration);
 
+      /*   If it's not visible, the redraw loop will skip the actual redraw, so
+       * we don't need to worry at this end.
+       */
       log_and_ignore_errors(redraw_sender.send(()).await);
     } else { break }
   }
@@ -469,14 +497,13 @@ async fn redraw_loop(dock: Arc<Mutex<Dock>>,
   -> Result<()>
 {
   loop {
-    println!("awaiting redraw notification");
     if let Ok(()) = redraw_receiver.recv().await {
-      println!("redrawing");
       let duration = match dock.lock() {
         Ok(dock) => {
+          if !dock.is_visible { continue }
+
           match dock.redraw().await {
             Ok(Some(duration)) => {
-              println!("redrawing was okay");
               if let Err(error) = dock.connection().flush() {
                 Into::<Error>::into(error).log();
               }