Skip to content

Commit

Permalink
Migrate window timeouts to use wallclock times instead of absolute co…
Browse files Browse the repository at this point in the history
…unts
  • Loading branch information
arusahni committed Dec 26, 2023
1 parent ccdcc30 commit aacd7ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::{
mpsc, Arc, RwLock,
},
thread,
time::Duration,
time::{Duration, SystemTime},
};

use clap::Parser;
Expand Down Expand Up @@ -112,6 +112,7 @@ fn handle_socket_messages(listener: UnixListener, tx: mpsc::Sender<String>) -> R
fn block_for_window(matcher: &x11::WindowMatcher) -> u32 {
trace!("blocking for window {:?}", matcher);
let mut count = 0;
let start = SystemTime::now();
loop {
match x11::map_qurop_window(matcher) {
Ok(window_id) => {
Expand All @@ -128,8 +129,11 @@ fn block_for_window(matcher: &x11::WindowMatcher) -> u32 {
warn!("could not find window in {} attempts", count);
thread::sleep(Duration::from_millis(100));
}
if count > 20 {
panic!("could not find window in {} attempts", count);
if SystemTime::now().duration_since(start).unwrap() > Duration::from_secs(5) {
panic!(
"could not find window after 5 seconds and {} attempts",
count
);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/x11.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
sync::{mpsc, Arc, RwLock},
thread,
time::Duration,
time::{Duration, SystemTime},
};
use tracing::{debug, info, trace, warn};
use x11rb::{
Expand Down Expand Up @@ -163,8 +163,9 @@ pub(crate) fn handle_window(tx: mpsc::Sender<String>, ctx: &Arc<RwLock<crate::Co
let root = screen.root;
let atoms = Atoms::new(&connection).unwrap().reply().unwrap();
let active_atom = atoms._NET_ACTIVE_WINDOW;
let mut count = 0;
connection.flush_and_sync();
let mut count = 0;
let start = SystemTime::now();
let window_id = loop {
match ctx.read().unwrap().window_id {
Some(win_id) => break win_id,
Expand All @@ -174,8 +175,8 @@ pub(crate) fn handle_window(tx: mpsc::Sender<String>, ctx: &Arc<RwLock<crate::Co
warn!("could not find window in {} attempts", count);
thread::sleep(Duration::from_millis(100));
}
if count > 5000000 {
panic!("could not find window in {} attempts", count);
if SystemTime::now().duration_since(start).unwrap() > Duration::from_secs(5) {
panic!("could not find window in 5 seconds and {} attempts", count);
}
}
};
Expand Down

0 comments on commit aacd7ec

Please sign in to comment.