Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

main.rs

519 lines17.8 KB

1//! One-shot KWin adapter. The installed executable, not Electron or the AppImage
2//! mount, is the identity authorized by KDE's desktop-entry permission check.
3use serde::{Deserialize, Serialize};
4use std::{
5 collections::HashMap,
6 fs::OpenOptions,
7 io::{Read, Write},
8 os::{fd::AsFd, unix::net::UnixStream},
9 path::Path,
10 sync::mpsc,
11 time::Duration,
12};
13use zbus::{
14 blocking::{Connection, Proxy, connection::Builder},
15 zvariant::{Fd, OwnedValue, Value},
16};
17​
18type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
19const SCREENSHOT: &str = "org.kde.KWin.ScreenShot2";
20const MAX_BYTES: usize = 128 * 1024 * 1024;
21const DEADLINE: Duration = Duration::from_secs(5);
22​
23mod feedback;
24​
25#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
26#[serde(rename_all = "camelCase")]
27struct Window {
28 id: String,
29 title: String,
30 app_name: String,
31 app_identifier: String,
32 process_id: u32,
33 bounds: Bounds,
34 client_bounds: Bounds,
36​
37impl Window {
38 fn matches_after_capture(&self, current: &Self) -> bool {
39 self.id == current.id
40 && self.process_id == current.process_id
41 && self.app_identifier == current.app_identifier
42 && self.app_name == current.app_name
43 && self.bounds == current.bounds
44 && self.client_bounds == current.client_bounds
45 && capture_title(&self.title) == capture_title(&current.title)
46 }
48​
49/// Match the same leading CLI spinner accepted by the Wayland AT-SPI window matcher.
50fn capture_title(title: &str) -> &str {
51 let title = title.trim();
52 let mut chars = title.chars();
53 if matches!(
54 chars.next(),
55 Some('⠋' | '⠙' | '⠹' | '⠸' | '⠼' | '⠴' | '⠦' | '⠧' | '⠇' | '⠏')
56 ) && chars.clone().next().is_some_and(char::is_whitespace)
57 {
58 let label = chars.as_str().trim_start();
59 if !label.is_empty() {
60 return label;
61 }
62 }
63 title
65​
66#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
67struct Bounds {
68 x: i32,
69 y: i32,
70 width: u32,
71 height: u32,
73​
74struct ScriptReply {
75 owner: String,
76 result: mpsc::SyncSender<String>,
78#[zbus::interface(name = "com.t3tools.KdeCapture")]
79impl ScriptReply {
80 fn reply(
81 &self,
82 value: &str,
83 #[zbus(header)] header: zbus::message::Header<'_>,
84 ) -> zbus::fdo::Result<()> {
85 if header.sender().map(|name| name.as_str()) != Some(self.owner.as_str()) {
86 return Err(zbus::fdo::Error::AccessDenied("Not KWin".into()));
87 }
88 if value.len() > 64 * 1024 {
89 return Err(zbus::fdo::Error::InvalidArgs(
90 "Oversized window metadata".into(),
91 ));
92 }
93 let _ = self.result.try_send(value.to_owned());
94 Ok(())
95 }
97​
98/// Scripts exist only for this operation. Unload even when evaluation/callback fails.
99fn script(connection: &Connection, directory: &Path, body: &str) -> Result<String> {
100 let dbus = zbus::blocking::fdo::DBusProxy::new(connection)?;
101 let owner = dbus.get_name_owner("org.kde.KWin".try_into()?)?.to_string();
102 let (send, receive) = mpsc::sync_channel(1);
103 connection.object_server().at(
104 "/com/t3tools/KdeCapture",
105 ScriptReply {
106 owner,
107 result: send,
108 },
109 )?;
110 let path = directory.join("window.js");
111 let name = format!("t3-capture-{}", std::process::id());
112 let destination = serde_json::to_string(
113 connection
114 .unique_name()
115 .ok_or("Missing bus identity")?
116 .as_str(),
117 )?;
118 let source = format!(
119 "function reply(value) {{ callDBus({destination}, '/com/t3tools/KdeCapture', 'com.t3tools.KdeCapture', 'Reply', JSON.stringify(value)); }}\ntry {{ {body} }} catch (error) {{ reply({{error: String(error)}}); }}"
120 );
121 std::fs::write(&path, source)?;
122 let scripting = Proxy::new(
123 connection,
124 "org.kde.KWin",
125 "/Scripting",
126 "org.kde.kwin.Scripting",
127 )?;
128 let result = (|| {
129 let id: i32 = scripting.call(
130 "loadScript",
131 &(path.to_str().ok_or("Invalid script path")?, &name),
132 )?;
133 if id < 0 {
134 return Err("KWin could not load the capture helper script".into());
135 }
136 let result = (|| {
137 let object = format!("/Scripting/Script{id}");
138 let loaded = Proxy::new(
139 connection,
140 "org.kde.KWin",
141 object.as_str(),
142 "org.kde.kwin.Script",
143 )?;
144 let _: () = loaded.call("run", &())?;
145 Ok(receive.recv_timeout(DEADLINE)?)
146 })();
147 let _: std::result::Result<bool, _> = scripting.call("unloadScript", &(&name,));
148 result
149 })();
150 connection
151 .object_server()
152 .remove::<ScriptReply, _>("/com/t3tools/KdeCapture")?;
153 let _ = std::fs::remove_file(path);
154 result
156​
157fn window(connection: &Connection, directory: &Path, id: Option<&str>) -> Result<Option<Window>> {
158 let selector = match id {
159 Some(id) => format!(
160 "workspace.windowList().find(w => String(w.internalId) === {})",
161 serde_json::to_string(id)?
162 ),
163 None => "workspace.activeWindow".into(),
164 };
165 let value = script(
166 connection,
167 directory,
168 &format!(
169 "function bounds(rect) {{ return {{ x: Math.round(rect.x), y: Math.round(rect.y), width: Math.round(rect.width), height: Math.round(rect.height) }}; }}
170 const w = {selector}; reply(!w ? null : {{ id: String(w.internalId), title: w.caption, appName: w.resourceClass || w.desktopFileName || 'Application', appIdentifier: w.desktopFileName || w.resourceClass || '', processId: w.pid, bounds: bounds(w.frameGeometry), clientBounds: bounds(w.clientGeometry) }});"
171 ),
172 )?;
173 Ok(serde_json::from_str(&value)?)
175​
176fn screenshot_proxy(connection: &Connection) -> zbus::Result<Proxy<'_>> {
177 Proxy::new(
178 connection,
179 SCREENSHOT,
180 "/org/kde/KWin/ScreenShot2",
181 SCREENSHOT,
182 )
184​
185/// InvalidWindow follows the authorization check and cannot produce a screenshot.
186fn check(connection: &Connection) -> Result<()> {
187 let proxy = screenshot_proxy(connection)?;
188 if proxy.get_property::<u32>("Version")? < 2 {
189 return Err("KDE's screenshot API is too old".into());
190 }
191 let sink = OpenOptions::new().write(true).open("/dev/null")?;
192 let reply: zbus::Result<HashMap<String, OwnedValue>> = proxy.call(
193 "CaptureWindow",
194 &(
195 "t3-permission-check-not-a-window",
196 HashMap::<&str, Value<'_>>::new(),
197 Fd::from(sink.as_fd()),
198 ),
199 );
200 match reply {
201 Err(zbus::Error::MethodError(name, _, _))
202 if name.as_str() == "org.kde.KWin.ScreenShot2.Error.InvalidWindow" =>
203 {
204 Ok(())
205 }
206 Err(error) => Err(error.into()),
207 Ok(_) => Err("KWin returned an unexpected permission-check result".into()),
208 }
210​
211fn uint(metadata: &HashMap<String, OwnedValue>, key: &str) -> Result<usize> {
212 Ok(u32::try_from(
213 metadata
214 .get(key)
215 .ok_or_else(|| format!("Missing screenshot {key}"))?,
216 )? as usize)
218​
219/// KWin supplies Qt ARGB32/RGB32 or RGBA8888 pixels. Refuse unknown formats.
220fn rgba(raw: &[u8], width: usize, height: usize, stride: usize, format: usize) -> Result<Vec<u8>> {
221 let row = width
222 .checked_mul(4)
223 .ok_or("Invalid screenshot dimensions")?;
224 let bytes = stride
225 .checked_mul(height)
226 .ok_or("Invalid screenshot dimensions")?;
227 if width == 0 || height == 0 || stride < row || bytes > MAX_BYTES || raw.len() != bytes {
228 return Err("Invalid or oversized screenshot dimensions".into());
229 }
230 if ![4, 5, 6, 17, 18].contains(&format) {
231 return Err("Unsupported KDE screenshot pixel format".into());
232 }
233 let mut output = Vec::with_capacity(row * height);
234 for y in 0..height {
235 for pixel in raw[y * stride..y * stride + row].chunks_exact(4) {
236 let (r, g, b, a) = if format >= 17 {
237 (pixel[0], pixel[1], pixel[2], pixel[3])
238 } else {
239 let argb = u32::from_ne_bytes(pixel.try_into()?);
240 (
241 (argb >> 16) as u8,
242 (argb >> 8) as u8,
243 argb as u8,
244 if format == 4 { 255 } else { (argb >> 24) as u8 },
245 )
246 };
247 let straight = |v: u8| {
248 if a == 0 {
249 0
250 } else {
251 ((v as u32 * 255 + a as u32 / 2) / a as u32).min(255) as u8
252 }
253 };
254 if format == 6 || format == 18 {
255 output.extend_from_slice(&[straight(r), straight(g), straight(b), a]);
256 } else {
257 output.extend_from_slice(&[r, g, b, a]);
258 }
259 }
260 }
261 Ok(output)
263​
264/// The desktop app rejects larger capture PNGs (`linuxCaptureSession.ts`).
265const MAX_PNG_BYTES: usize = 32 * 1024 * 1024;
266​
267/// Level 6 dominated capture time, so encode at the much faster level 2 and fall back
268/// to level 6 only when level 2's slightly larger output would be rejected.
269fn encode_png(rgba: &[u8], width: u32, height: u32, limit: usize) -> Result<Vec<u8>> {
270 for level in [2, 6] {
271 let mut png = Vec::new();
272 let mut encoder = png::Encoder::new(&mut png, width, height);
273 encoder.set_color(png::ColorType::Rgba);
274 encoder.set_depth(png::BitDepth::Eight);
275 encoder.set_deflate_compression(png::DeflateCompression::Level(level));
276 encoder.write_header()?.write_image_data(rgba)?;
277 if png.len() <= limit {
278 return Ok(png);
279 }
280 }
281 Err("Screenshot too large".into())
283​
284fn capture(connection: &Connection, directory: &Path) -> Result<Option<Window>> {
285 // Lack of scripting only loses identity/text; never guess which app was captured.
286 let before = window(connection, directory, None).ok().flatten();
287 let proxy = screenshot_proxy(connection)?;
288 let (mut reader, writer) = UnixStream::pair()?;
289 reader.set_read_timeout(Some(DEADLINE))?;
290 let options = HashMap::from([
291 ("include-decoration", Value::from(true)),
292 ("include-shadow", Value::from(false)),
293 ("include-cursor", Value::from(false)),
294 ("native-resolution", Value::from(true)),
295 ]);
296 let metadata: HashMap<String, OwnedValue> = if let Some(window) = &before {
297 proxy.call(
298 "CaptureWindow",
299 &(&window.id, options, Fd::from(writer.as_fd())),
300 )?
301 } else {
302 proxy.call("CaptureActiveWindow", &(options, Fd::from(writer.as_fd())))?
303 };
304 drop(writer);
305 if <&str>::try_from(metadata.get("type").ok_or("Missing screenshot type")?)? != "raw" {
306 return Err("Unsupported KDE screenshot type".into());
307 }
308 let width = uint(&metadata, "width")?;
309 let height = uint(&metadata, "height")?;
310 let stride = metadata
311 .get("stride")
312 .map(|_| uint(&metadata, "stride"))
313 .transpose()?
314 .unwrap_or(width.checked_mul(4).ok_or("Invalid width")?);
315 let bytes = stride
316 .checked_mul(height)
317 .filter(|bytes| *bytes <= MAX_BYTES)
318 .ok_or("Screenshot too large")?;
319 let mut raw = vec![0; bytes];
320 reader.read_exact(&mut raw)?;
321 let pixels = rgba(&raw, width, height, stride, uint(&metadata, "format")?)?;
322 let png = encode_png(&pixels, width as u32, height as u32, MAX_PNG_BYTES)?;
323 OpenOptions::new()
324 .write(true)
325 .create_new(true)
326 .open(directory.join("capture.png"))?
327 .write_all(&png)?;
328 let identity = before.filter(|before| {
329 window(connection, directory, Some(&before.id))
330 .ok()
331 .flatten()
332 .is_some_and(|current| before.matches_after_capture(&current))
333 });
334 Ok(identity)
336​
337fn run() -> Result<()> {
338 let args: Vec<String> = std::env::args().collect();
339 let connection = Builder::session()?.method_timeout(DEADLINE).build()?;
340 match args.get(1).map(String::as_str) {
341 Some("check") => {
342 check(&connection)?;
343 println!(
344 "{}",
345 serde_json::json!({"ready": true, "feedbackAvailable": feedback::supported(&connection)})
346 );
347 }
348 Some("capture") if args.len() == 3 => {
349 let window = capture(&connection, Path::new(&args[2]))?;
350 println!("{}", serde_json::json!({"window": window}));
351 }
352 Some("feedback") if args.len() == 4 => {
353 feedback::run(&connection, Path::new(&args[2]), &args[3])?;
354 }
355 Some("activate") if args.len() == 5 => {
356 let pid: u32 = args[3].parse()?;
357 let title = serde_json::to_string(&args[4])?;
358 let value = script(
359 &connection,
360 Path::new(&args[2]),
361 &format!(
362 "const targetPid = {pid}; const targetTitle = {title};\n{}",
363 include_str!("activate.js")
364 ),
365 )?;
366 if serde_json::from_str::<serde_json::Value>(&value)?["activated"] != true {
367 return Err("KDE could not identify the T3 Code window to activate".into());
368 }
369 println!("{{\"activated\":true}}");
370 }
371 _ => {
372 return Err(
373 "Expected check, capture <directory>, feedback <directory> <options>, or activate <directory> <pid> <title>"
374 .into(),
375 );
376 }
377 }
378 Ok(())
380​
381#[cfg(test)]
382mod transport_tests;
383​
384fn main() {
385 if let Err(error) = run() {
386 let _ = writeln!(std::io::stderr(), "{error}");
387 std::process::exit(1);
388 }
390​
391#[cfg(test)]
392mod tests {
393 use super::*;
394​
395 fn window() -> Window {
396 Window {
397 id: "{window-1}".into(),
398 title: "⠋ Document".into(),
399 app_name: "Editor".into(),
400 app_identifier: "editor".into(),
401 process_id: 123,
402 bounds: Bounds {
403 x: 0,
404 y: 0,
405 width: 800,
406 height: 600,
407 },
408 client_bounds: Bounds {
409 x: 0,
410 y: 29,
411 width: 800,
412 height: 571,
413 },
414 }
415 }
416​
417 #[test]
418 fn accepts_only_a_leading_cli_spinner_with_a_nonempty_label() {
419 let before = window();
420 for title in [
421 "⠋ Document",
422 "⠙ Document",
423 "⠹ Document",
424 "⠸ Document",
425 "⠼ Document",
426 "⠴ Document",
427 "⠦ Document",
428 "⠧ Document",
429 "⠇ Document",
430 "⠏ Document",
431 "Document",
432 ] {
433 let mut after = before.clone();
434 after.title = title.into();
435 assert!(before.matches_after_capture(&after), "{title}");
436 assert!(after.matches_after_capture(&before), "{title}");
437 }
438 for title in ["⠙ Other", "⠙Document", "⠁ Document", "Document ⠙", "⠙", ""] {
439 let mut after = before.clone();
440 after.title = title.into();
441 assert!(!before.matches_after_capture(&after), "{title}");
442 }
443 assert_ne!(capture_title("⠋"), capture_title("⠙"));
444 }
445​
446 #[test]
447 fn spinner_normalization_does_not_weaken_window_identity_checks() {
448 let before = window();
449 for field in [
450 "id",
451 "process_id",
452 "app_identifier",
453 "app_name",
454 "bounds",
455 "client_bounds",
456 ] {
457 let mut after = before.clone();
458 after.title = "⠙ Document".into();
459 match field {
460 "id" => after.id = "{window-2}".into(),
461 "process_id" => after.process_id = 456,
462 "app_identifier" => after.app_identifier = "other".into(),
463 "app_name" => after.app_name = "Other".into(),
464 "bounds" => after.bounds.width = 400,
465 "client_bounds" => after.client_bounds.height = 500,
466 _ => unreachable!(),
467 }
468 assert!(!before.matches_after_capture(&after), "{field}");
469 }
470 }
471​
472 #[test]
473 fn falls_back_to_smaller_png_when_fast_encode_exceeds_limit() {
474 let (width, height) = (256, 256);
475 let rgba: Vec<u8> = (0..width * height * 4)
476 .map(|i| (((i / 4) % 251) ^ (i % 7)) as u8)
477 .collect();
478 let decode = |png: &[u8]| {
479 let mut reader = png::Decoder::new(std::io::Cursor::new(png))
480 .read_info()
481 .unwrap();
482 let mut pixels = vec![0; reader.output_buffer_size().unwrap()];
483 reader.next_frame(&mut pixels).unwrap();
484 pixels
485 };
486 let fast = encode_png(&rgba, width, height, usize::MAX).unwrap();
487 let fallback = encode_png(&rgba, width, height, fast.len() - 1).unwrap();
488 assert!(fallback.len() < fast.len());
489 assert_eq!(decode(&fast), rgba);
490 assert_eq!(decode(&fallback), rgba);
491 assert!(encode_png(&rgba, width, height, fallback.len() - 1).is_err());
492 }
493​
494 #[test]
495 fn decodes_native_argb_and_row_padding() {
496 let raw = [0xff112233_u32.to_ne_bytes(), [0; 4]].concat();
497 assert_eq!(rgba(&raw, 1, 1, 8, 5).unwrap(), [0x11, 0x22, 0x33, 255]);
498 }
499 #[test]
500 fn unpremultiplies_transparency() {
501 assert_eq!(
502 rgba(&[64, 32, 16, 128], 1, 1, 4, 18).unwrap(),
503 [128, 64, 32, 128]
504 );
505 assert_eq!(rgba(&[0, 0, 0, 0], 1, 1, 4, 18).unwrap(), [0, 0, 0, 0]);
506 }
507 #[test]
508 fn rejects_invalid_dimensions_truncation_and_formats() {
509 for (width, height, stride, format) in [
510 (0, 1, 4, 5),
511 (2, 1, 4, 5),
512 (1, 2, 4, 5),
513 (1, 1, 4, 99),
514 (usize::MAX, 1, 4, 5),
515 ] {
516 assert!(rgba(&[0; 4], width, height, stride, format).is_err());
517 }
518 }