x11: log aw-none if _NET_ACTIVE_WINDOW == 0

This commit is contained in:
powellnorma 2024-10-06 12:49:45 +02:00
parent 990843bb05
commit bd780e1cbb
2 changed files with 59 additions and 34 deletions

View File

@ -67,19 +67,21 @@ impl X11Client {
}) })
} }
pub fn active_window_data(&mut self) -> anyhow::Result<WindowData> { pub fn active_window_data(&mut self) -> anyhow::Result<Option<WindowData>> {
self.execute_with_reconnect(|client| { self.execute_with_reconnect(|client| {
let focus: Window = client.find_active_window()?; let focus = client.find_active_window()?;
match focus {
Some(window) => {
let name = client.get_property( let name = client.get_property(
focus, window,
client.intern_atom("_NET_WM_NAME")?, client.intern_atom("_NET_WM_NAME")?,
"_NET_WM_NAME", "_NET_WM_NAME",
client.intern_atom("UTF8_STRING")?, client.intern_atom("UTF8_STRING")?,
u32::MAX, u32::MAX,
)?; )?;
let class = client.get_property( let class = client.get_property(
focus, window,
AtomEnum::WM_CLASS.into(), AtomEnum::WM_CLASS.into(),
"WM_CLASS", "WM_CLASS",
AtomEnum::STRING.into(), AtomEnum::STRING.into(),
@ -89,11 +91,14 @@ impl X11Client {
let title = str::from_utf8(&name.value).with_context(|| "Invalid title UTF")?; let title = str::from_utf8(&name.value).with_context(|| "Invalid title UTF")?;
let (instance, class) = parse_wm_class(&class)?; let (instance, class) = parse_wm_class(&class)?;
Ok(WindowData { Ok(Some(WindowData {
title: title.to_string(), title: title.to_string(),
app_id: class, app_id: class,
wm_instance: instance, wm_instance: instance,
}) }))
}
None => Ok(None),
}
}) })
} }
@ -122,7 +127,7 @@ impl X11Client {
.atom) .atom)
} }
fn find_active_window(&self) -> anyhow::Result<Window> { fn find_active_window(&self) -> anyhow::Result<Option<Window>> {
let window: Atom = AtomEnum::WINDOW.into(); let window: Atom = AtomEnum::WINDOW.into();
let net_active_window = self.intern_atom("_NET_ACTIVE_WINDOW")?; let net_active_window = self.intern_atom("_NET_ACTIVE_WINDOW")?;
let active_window = self.get_property( let active_window = self.get_property(
@ -134,20 +139,27 @@ impl X11Client {
)?; )?;
if active_window.format == 32 && active_window.length == 1 { if active_window.format == 32 && active_window.length == 1 {
active_window let window_id = active_window
.value32() .value32()
.ok_or(anyhow!("Invalid message. Expected value with format = 32"))? .ok_or(anyhow!("Invalid message. Expected value with format = 32"))?
.next() .next()
.ok_or(anyhow!("Active window is not found")) .ok_or(anyhow!("Active window is not found"))?;
// Check if the window_id is 0 (no active window)
if window_id == 0 {
return Ok(None);
}
Ok(Some(window_id))
} else { } else {
// Query the input focus // Query the input focus
Ok(self Ok(Some(self
.connection .connection
.get_input_focus() .get_input_focus()
.with_context(|| "Failed to get input focus")? .with_context(|| "Failed to get input focus")?
.reply() .reply()
.with_context(|| "Failed to read input focus from reply")? .with_context(|| "Failed to read input focus from reply")?
.focus) .focus))
} }
} }
} }

View File

@ -15,18 +15,31 @@ impl WindowWatcher {
async fn send_active_window(&mut self, client: &ReportClient) -> anyhow::Result<()> { async fn send_active_window(&mut self, client: &ReportClient) -> anyhow::Result<()> {
let data = self.client.active_window_data()?; let data = self.client.active_window_data()?;
if data.app_id != self.last_app_id || data.title != self.last_title || data.wm_instance != self.last_wm_instance { let (app_id, title, wm_instance) = match data {
Some(window_data) => (
window_data.app_id,
window_data.title,
window_data.wm_instance,
),
None => {
// No active window, set all values to "aw-none"
("aw-none".to_string(), "aw-none".to_string(), "aw-none".to_string())
}
};
if app_id != self.last_app_id || title != self.last_title || wm_instance != self.last_wm_instance {
debug!( debug!(
r#"Changed window app_id="{}", title="{}", wm_instance="{}""#, r#"Changed window app_id="{}", title="{}", wm_instance="{}""#,
data.app_id, data.title, data.wm_instance app_id, title, wm_instance
); );
self.last_app_id = data.app_id.clone(); self.last_app_id = app_id.clone();
self.last_title = data.title.clone(); self.last_title = title.clone();
self.last_wm_instance = data.wm_instance.clone(); self.last_wm_instance = wm_instance.clone();
} }
client client
.send_active_window_with_instance(&self.last_app_id, &self.last_title, Some(&self.last_wm_instance)) .send_active_window_with_instance(&app_id, &title, Some(&wm_instance))
.await .await
.with_context(|| "Failed to send heartbeat for active window") .with_context(|| "Failed to send heartbeat for active window")
} }