Hackily retry keyboard/pointer grabs if they fail
When using a global key combo to lock the screen, often the WM (or whatever) will still have an active grab on the keyboard by the time we try to get our own grab.
This commit is contained in:
parent
ab543afcbc
commit
d7a7d57ccd
@ -1,5 +1,5 @@
|
|||||||
use log::{debug, warn};
|
use log::{debug, error, warn};
|
||||||
use std::cmp;
|
use std::{cmp, thread, time::Duration};
|
||||||
use xcb::{x, randr, Xid};
|
use xcb::{x, randr, Xid};
|
||||||
|
|
||||||
use bscreensaver_util::{BSCREENSAVER_WM_CLASS, create_atom, destroy_cursor, destroy_pixmap, destroy_gc, destroy_window};
|
use bscreensaver_util::{BSCREENSAVER_WM_CLASS, create_atom, destroy_cursor, destroy_pixmap, destroy_gc, destroy_window};
|
||||||
@ -238,6 +238,19 @@ impl<'a> Monitor<'a> {
|
|||||||
self.conn.check_request(cookie)?;
|
self.conn.check_request(cookie)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Err(err) = self.grab_keyboard() {
|
||||||
|
warn!("Failed to grab keyboard on window {:?}: {:?}", self.blanker_window, err);
|
||||||
|
}
|
||||||
|
if let Err(err) = self.grab_pointer() {
|
||||||
|
error!("Failed to grab pointer on window {:?}: {:?}", self.blanker_window, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn grab_keyboard(&self) -> anyhow::Result<()> {
|
||||||
|
let mut attempts_remaining = 10;
|
||||||
|
loop {
|
||||||
let cookie = self.conn.send_request(&x::GrabKeyboard {
|
let cookie = self.conn.send_request(&x::GrabKeyboard {
|
||||||
owner_events: true,
|
owner_events: true,
|
||||||
grab_window: self.unlock_window,
|
grab_window: self.unlock_window,
|
||||||
@ -246,11 +259,21 @@ impl<'a> Monitor<'a> {
|
|||||||
keyboard_mode: x::GrabMode::Async,
|
keyboard_mode: x::GrabMode::Async,
|
||||||
});
|
});
|
||||||
let reply = self.conn.wait_for_reply(cookie)?;
|
let reply = self.conn.wait_for_reply(cookie)?;
|
||||||
if reply.status() != x::GrabStatus::Success {
|
if reply.status() == x::GrabStatus::Success {
|
||||||
// FIXME: try to grab later?
|
break Ok(());
|
||||||
warn!("Failed to grab keyboard on window {:?}: {:?}", self.blanker_window, reply.status());
|
} else if attempts_remaining > 0 {
|
||||||
|
attempts_remaining -= 1;
|
||||||
|
warn!("Failed to grab keyboard ({:?}); retrying", reply.status());
|
||||||
|
thread::sleep(Duration::from_millis(25));
|
||||||
|
} else {
|
||||||
|
break Err(anyhow::anyhow!("{:?}", reply.status()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn grab_pointer(&self) -> anyhow::Result<()> {
|
||||||
|
let mut attempts_remaining = 10;
|
||||||
|
loop {
|
||||||
let cookie = self.conn.send_request(&x::GrabPointer {
|
let cookie = self.conn.send_request(&x::GrabPointer {
|
||||||
owner_events: true,
|
owner_events: true,
|
||||||
grab_window: self.unlock_window,
|
grab_window: self.unlock_window,
|
||||||
@ -262,12 +285,16 @@ impl<'a> Monitor<'a> {
|
|||||||
time: x::CURRENT_TIME,
|
time: x::CURRENT_TIME,
|
||||||
});
|
});
|
||||||
let reply = self.conn.wait_for_reply(cookie)?;
|
let reply = self.conn.wait_for_reply(cookie)?;
|
||||||
if reply.status() != x::GrabStatus::Success {
|
if reply.status() == x::GrabStatus::Success {
|
||||||
// FIXME: try to grab later?
|
break Ok(());
|
||||||
warn!("Failed to grab pointer on window {:?}: {:?}", self.blanker_window, reply.status());
|
} else if attempts_remaining > 0 {
|
||||||
|
attempts_remaining -= 1;
|
||||||
|
warn!("Failed to grab pointer ({:?}); retrying", reply.status());
|
||||||
|
thread::sleep(Duration::from_millis(25));
|
||||||
|
} else {
|
||||||
|
break Err(anyhow::anyhow!("{:?}", reply.status()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unlock(&self) -> anyhow::Result<()> {
|
pub fn unlock(&self) -> anyhow::Result<()> {
|
||||||
|
Loading…
Reference in New Issue
Block a user