Skip to content

Commit

Permalink
Add configurable window query timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
arusahni committed Dec 28, 2023
1 parent 1f505c3 commit 3b0b3ba
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub(crate) struct Instance {
pub(crate) matcher: WindowMatcher,
#[serde(default)]
pub(crate) class_name: Option<String>,
#[serde(default)]
pub(crate) window_delay_ms: Option<u64>,
}

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
Expand Down Expand Up @@ -65,6 +67,7 @@ pub fn add_instance(
command: command.into(),
matcher,
class_name,
window_delay_ms: None,
},
);
let file_path = get_config_path()?;
Expand Down
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,14 @@ fn handle_socket_messages(listener: UnixListener, tx: mpsc::Sender<String>) -> R
}

/// Find and position the window
fn block_for_window(matcher: &x11::WindowMatcher) -> u32 {
fn block_for_window(matcher: &x11::WindowMatcher, window_delay: Option<u64>) -> u32 {
trace!("blocking for window {:?}", matcher);
let mut count = 0;
let start = SystemTime::now();
loop {
match x11::map_qurop_window(matcher) {
Ok(window_id) => {
x11::position_window(window_id);
x11::position_window(window_id, window_delay);
return window_id;
}
Err(Error::WindowNotFound) => {
Expand Down Expand Up @@ -160,7 +160,7 @@ pub(crate) fn program_thread(
write_ctx.matcher = x11::WindowMatcher::ProcessId(Some(program.id()));
trace!("[{}] Set a new PID {}", instance.name, program.id());
}
write_ctx.window_id = Some(block_for_window(&write_ctx.matcher));
write_ctx.window_id = Some(block_for_window(&write_ctx.matcher, instance.window_delay));
trace!(
"[{}] Set a new Window ID {:?}",
instance.name,
Expand Down Expand Up @@ -198,11 +198,12 @@ pub(crate) fn program_thread(
trace!("[{}] Setting new pid {}", instance.name, program.id());
write_ctx.matcher = x11::WindowMatcher::ProcessId(Some(program.id()));
}
write_ctx.window_id = Some(block_for_window(&write_ctx.matcher));
write_ctx.window_id =
Some(block_for_window(&write_ctx.matcher, instance.window_delay));
} else {
let window_id = read_ctx.read().unwrap().window_id.unwrap();
x11::map_window(window_id);
x11::position_window(window_id);
x11::position_window(window_id, instance.window_delay);
}
}
"kill" => {
Expand Down Expand Up @@ -277,6 +278,7 @@ struct Instance {
name: String,
command: String,
matcher: x11::WindowMatcher,
window_delay: Option<u64>,
}

fn main() -> Result<(), Error> {
Expand Down Expand Up @@ -333,6 +335,7 @@ fn main() -> Result<(), Error> {
),
config::WindowMatcher::Process => x11::WindowMatcher::ProcessId(None),
},
window_delay: instance.window_delay_ms.or(Some(100)),
};
match get_socket(&instance_name)? {
StreamState::Exists(mut stream) => {
Expand Down
8 changes: 5 additions & 3 deletions src/x11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub(crate) fn map_window(window_id: u32) {
}

/// Position the window and set decoration properties.
pub(crate) fn position_window(window_id: u32) {
pub(crate) fn position_window(window_id: u32, window_delay: Option<u64>) {
let (connection, num) = x11rb::connect(None).expect("x11 connection missing");
let screen = &connection.setup().roots[num];
let width = ((screen.width_in_pixels as f64) * 0.66) as u32;
Expand Down Expand Up @@ -335,7 +335,9 @@ pub(crate) fn position_window(window_id: u32) {
connection
.configure_window(window_id, &window_geometry_config)
.expect("couldn't configure window");
// Ugly hack that makes me sad.
thread::sleep(Duration::from_millis(100));
if let Some(window_delay_ms) = window_delay {
// Ugly hack that makes me sad.
thread::sleep(Duration::from_millis(window_delay_ms));
}
connection.flush_and_sync();
}

0 comments on commit 3b0b3ba

Please sign in to comment.