Skip to content

Commit 990843b

Browse files
committed
x11: include wm_instance
1 parent a045780 commit 990843b

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

watchers/src/report_client.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ impl ReportClient {
9494
}
9595

9696
pub async fn send_active_window(&self, app_id: &str, title: &str) -> anyhow::Result<()> {
97+
self.send_active_window_with_instance(app_id, title, None).await
98+
}
99+
100+
pub async fn send_active_window_with_instance(
101+
&self,
102+
app_id: &str,
103+
title: &str,
104+
wm_instance: Option<&str>,
105+
) -> anyhow::Result<()> {
97106
let mut data = Map::new();
98107

99108
let replacement = self.config.window_data_replacement(app_id, title);
@@ -116,6 +125,11 @@ impl ReportClient {
116125
);
117126
data.insert("app".to_string(), Value::String(inserted_app_id));
118127
data.insert("title".to_string(), Value::String(inserted_title));
128+
129+
if let Some(instance) = wm_instance {
130+
data.insert("wm_instance".to_string(), Value::String(instance.to_string()));
131+
}
132+
119133
let event = AwEvent {
120134
id: None,
121135
timestamp: Utc::now(),

watchers/src/watchers/x11_connection.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use x11rb::rust_connection::RustConnection;
99
pub struct WindowData {
1010
pub title: String,
1111
pub app_id: String,
12+
pub wm_instance: String,
1213
}
1314

1415
pub struct X11Client {
@@ -86,10 +87,12 @@ impl X11Client {
8687
)?;
8788

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

9092
Ok(WindowData {
9193
title: title.to_string(),
92-
app_id: parse_wm_class(&class)?.to_string(),
94+
app_id: class,
95+
wm_instance: instance,
9396
})
9497
})
9598
}
@@ -149,21 +152,26 @@ impl X11Client {
149152
}
150153
}
151154

152-
fn parse_wm_class(property: &GetPropertyReply) -> anyhow::Result<&str> {
155+
fn parse_wm_class(property: &GetPropertyReply) -> anyhow::Result<(String, String)> {
153156
if property.format != 8 {
154157
bail!("Malformed property: wrong format");
155158
}
156159
let value = &property.value;
157160
// The property should contain two null-terminated strings. Find them.
158161
if let Some(middle) = value.iter().position(|&b| b == 0) {
159-
let (_, class) = value.split_at(middle);
160-
// Skip the null byte at the beginning
162+
let (instance, class) = value.split_at(middle);
163+
// Remove the null byte at the end of the instance
164+
let instance = &instance[..instance.len()];
165+
// Skip the null byte at the beginning of the class
161166
let mut class = &class[1..];
162167
// Remove the last null byte from the class, if it is there.
163168
if class.last() == Some(&0) {
164169
class = &class[..class.len() - 1];
165170
}
166-
Ok(std::str::from_utf8(class)?)
171+
Ok((
172+
std::str::from_utf8(instance)?.to_string(),
173+
std::str::from_utf8(class)?.to_string(),
174+
))
167175
} else {
168176
bail!("Missing null byte")
169177
}

watchers/src/watchers/x11_window.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,27 @@ use std::sync::Arc;
66

77
pub struct WindowWatcher {
88
client: X11Client,
9-
last_title: String,
109
last_app_id: String,
10+
last_title: String,
11+
last_wm_instance: String,
1112
}
1213

1314
impl WindowWatcher {
1415
async fn send_active_window(&mut self, client: &ReportClient) -> anyhow::Result<()> {
1516
let data = self.client.active_window_data()?;
1617

17-
if data.app_id != self.last_app_id || data.title != self.last_title {
18+
if data.app_id != self.last_app_id || data.title != self.last_title || data.wm_instance != self.last_wm_instance {
1819
debug!(
19-
r#"Changed window app_id="{}", title="{}""#,
20-
data.app_id, data.title
20+
r#"Changed window app_id="{}", title="{}", wm_instance="{}""#,
21+
data.app_id, data.title, data.wm_instance
2122
);
22-
self.last_app_id = data.app_id;
23-
self.last_title = data.title;
23+
self.last_app_id = data.app_id.clone();
24+
self.last_title = data.title.clone();
25+
self.last_wm_instance = data.wm_instance.clone();
2426
}
2527

2628
client
27-
.send_active_window(&self.last_app_id, &self.last_title)
29+
.send_active_window_with_instance(&self.last_app_id, &self.last_title, Some(&self.last_wm_instance))
2830
.await
2931
.with_context(|| "Failed to send heartbeat for active window")
3032
}
@@ -40,6 +42,7 @@ impl Watcher for WindowWatcher {
4042
client,
4143
last_title: String::new(),
4244
last_app_id: String::new(),
45+
last_wm_instance: String::new(),
4346
})
4447
}
4548

0 commit comments

Comments
 (0)