Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x11: log aw-none if _NET_ACTIVE_WINDOW == 0 #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions watchers/src/watchers/x11_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,33 +67,38 @@ 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| {
let focus: Window = client.find_active_window()?;

let name = client.get_property(
focus,
client.intern_atom("_NET_WM_NAME")?,
"_NET_WM_NAME",
client.intern_atom("UTF8_STRING")?,
u32::MAX,
)?;
let class = client.get_property(
focus,
AtomEnum::WM_CLASS.into(),
"WM_CLASS",
AtomEnum::STRING.into(),
u32::MAX,
)?;

let title = str::from_utf8(&name.value).with_context(|| "Invalid title UTF")?;
let (instance, class) = parse_wm_class(&class)?;

Ok(WindowData {
title: title.to_string(),
app_id: class,
wm_instance: instance,
})
let focus = client.find_active_window()?;

match focus {
Some(window) => {
let name = client.get_property(
window,
client.intern_atom("_NET_WM_NAME")?,
"_NET_WM_NAME",
client.intern_atom("UTF8_STRING")?,
u32::MAX,
)?;
let class = client.get_property(
window,
AtomEnum::WM_CLASS.into(),
"WM_CLASS",
AtomEnum::STRING.into(),
u32::MAX,
)?;

let title = str::from_utf8(&name.value).with_context(|| "Invalid title UTF")?;
let (instance, class) = parse_wm_class(&class)?;

Ok(Some(WindowData {
title: title.to_string(),
app_id: class,
wm_instance: instance,
}))
}
None => Ok(None),
}
})
}

Expand Down Expand Up @@ -122,7 +127,7 @@ impl X11Client {
.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 net_active_window = self.intern_atom("_NET_ACTIVE_WINDOW")?;
let active_window = self.get_property(
Expand All @@ -134,20 +139,27 @@ impl X11Client {
)?;

if active_window.format == 32 && active_window.length == 1 {
active_window
let window_id = active_window
.value32()
.ok_or(anyhow!("Invalid message. Expected value with format = 32"))?
.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 {
// Query the input focus
Ok(self
Ok(Some(self
.connection
.get_input_focus()
.with_context(|| "Failed to get input focus")?
.reply()
.with_context(|| "Failed to read input focus from reply")?
.focus)
.focus))
}
}
}
Expand Down
25 changes: 19 additions & 6 deletions watchers/src/watchers/x11_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,31 @@ impl WindowWatcher {
async fn send_active_window(&mut self, client: &ReportClient) -> anyhow::Result<()> {
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!(
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_title = data.title.clone();
self.last_wm_instance = data.wm_instance.clone();
self.last_app_id = app_id.clone();
self.last_title = title.clone();
self.last_wm_instance = wm_instance.clone();

}

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
.with_context(|| "Failed to send heartbeat for active window")
}
Expand Down