The best way to control Claude Code, Codex and more from any computer or phone

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