wdotool 0.1.6

xdotool-compatible automation for Wayland
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! KDE backend — libei input plus KWin-scripting window management over D-Bus.
//!
//! The window half uses the same trick as `kdotool`: generate a JavaScript
//! snippet, hand it to `org.kde.KWin.Scripting.loadScriptFromText`, run it,
//! and let the script call back into a zbus service we registered at
//! startup. This avoids the `print`-signal-scraping approach (fragile and
//! needs journal access) and works on Plasma 5 and 6 identically.

use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use serde::Deserialize;
use tokio::sync::{oneshot, Mutex};
use tracing::debug;
use zbus::{interface, proxy, Connection};

use super::libei::LibeiBackend;
use super::Backend;
use crate::error::{Result, WdoError};
use crate::types::{Capabilities, KeyDirection, MouseButton, WindowId, WindowInfo};

const NAME: &str = "kde-dbus";

const BRIDGE_SERVICE: &str = "com.wdotool.KdeBridge";
const BRIDGE_PATH: &str = "/com/wdotool/KdeBridge";
const BRIDGE_IFACE: &str = "com.wdotool.KdeBridge";

pub struct KdeBackend {
    libei: LibeiBackend,
    conn: Connection,
    pending: Arc<Mutex<PendingState>>,
    next_id: AtomicU64,
    input_caps: Capabilities,
}

#[derive(Default)]
struct PendingState {
    list_waiters: HashMap<u64, oneshot::Sender<String>>,
    active_waiters: HashMap<u64, oneshot::Sender<String>>,
    action_waiters: HashMap<u64, oneshot::Sender<bool>>,
}

/// D-Bus interface our KWin scripts call back into. Names are PascalCase on
/// the wire so `callDBus(..., "ReportWindows", ...)` matches zbus's default
/// camelCase → PascalCase mapping.
struct Bridge {
    pending: Arc<Mutex<PendingState>>,
}

#[interface(name = "com.wdotool.KdeBridge")]
impl Bridge {
    async fn report_windows(&self, req_id: u64, json: String) {
        let sender = self.pending.lock().await.list_waiters.remove(&req_id);
        if let Some(tx) = sender {
            let _ = tx.send(json);
        }
    }

    async fn report_active(&self, req_id: u64, json: String) {
        let sender = self.pending.lock().await.active_waiters.remove(&req_id);
        if let Some(tx) = sender {
            let _ = tx.send(json);
        }
    }

    async fn report_action(&self, req_id: u64, ok: bool) {
        let sender = self.pending.lock().await.action_waiters.remove(&req_id);
        if let Some(tx) = sender {
            let _ = tx.send(ok);
        }
    }
}

#[proxy(
    interface = "org.kde.kwin.Scripting",
    default_service = "org.kde.KWin",
    default_path = "/Scripting"
)]
trait KwinScripting {
    #[zbus(name = "loadScriptFromText")]
    fn load_script_from_text(&self, source: &str, name: &str) -> zbus::Result<i32>;
    fn start(&self) -> zbus::Result<()>;
}

#[proxy(interface = "org.kde.kwin.Script", default_service = "org.kde.KWin")]
trait KwinScript {
    fn run(&self) -> zbus::Result<()>;
}

#[derive(Deserialize)]
struct ScriptWindow {
    id: String,
    #[serde(default)]
    title: String,
    #[serde(default)]
    app_id: Option<String>,
    #[serde(default)]
    pid: Option<u32>,
}

impl From<ScriptWindow> for WindowInfo {
    fn from(s: ScriptWindow) -> Self {
        WindowInfo {
            id: WindowId(s.id),
            title: s.title,
            app_id: s.app_id,
            pid: s.pid,
        }
    }
}

impl KdeBackend {
    pub async fn try_new() -> Result<Self> {
        // Input path: libei over the KDE portal. KDE Plasma ships a
        // RemoteDesktop portal implementation, so this bootstraps cleanly
        // on actual KDE sessions.
        let libei = LibeiBackend::try_new()
            .await
            .map_err(|err| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("libei input init failed: {err}"),
            })?;
        let input_caps = libei.capabilities();

        let conn = Connection::session().await.map_err(dbus_err)?;

        let pending: Arc<Mutex<PendingState>> = Arc::new(Mutex::new(PendingState::default()));
        conn.object_server()
            .at(
                BRIDGE_PATH,
                Bridge {
                    pending: pending.clone(),
                },
            )
            .await
            .map_err(dbus_err)?;
        conn.request_name(BRIDGE_SERVICE).await.map_err(dbus_err)?;

        debug!("kde bridge registered at {BRIDGE_PATH}");
        Ok(Self {
            libei,
            conn,
            pending,
            next_id: AtomicU64::new(1),
            input_caps,
        })
    }

    fn next_request_id(&self) -> u64 {
        self.next_id.fetch_add(1, Ordering::Relaxed)
    }

    async fn run_kwin_script(&self, script: &str) -> Result<()> {
        let scripting = KwinScriptingProxy::new(&self.conn)
            .await
            .map_err(dbus_err)?;
        let script_id = scripting
            .load_script_from_text(script, "wdotool")
            .await
            .map_err(dbus_err)?;
        if script_id < 0 {
            return Err(WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("loadScriptFromText returned {script_id}"),
            });
        }
        // Plasma 6 runs newly-loaded scripts when `start()` is called on
        // the Scripting object; Plasma 5 auto-starts on load. Calling
        // start() is idempotent on both.
        let _ = scripting.start().await;
        Ok(())
    }

    async fn list_windows_impl(&self) -> Result<Vec<WindowInfo>> {
        let req_id = self.next_request_id();
        let (tx, rx) = oneshot::channel::<String>();
        self.pending.lock().await.list_waiters.insert(req_id, tx);

        let script = list_windows_script(req_id);
        self.run_kwin_script(&script).await?;

        let json = tokio::time::timeout(Duration::from_secs(3), rx)
            .await
            .map_err(|_| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("timed out waiting for KWin script callback"),
            })?
            .map_err(|_| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("KWin script aborted before callback"),
            })?;

        let parsed: Vec<ScriptWindow> =
            serde_json::from_str(&json).map_err(|e| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("invalid windows payload: {e}"),
            })?;
        Ok(parsed.into_iter().map(Into::into).collect())
    }

    async fn active_window_impl(&self) -> Result<Option<WindowInfo>> {
        let req_id = self.next_request_id();
        let (tx, rx) = oneshot::channel::<String>();
        self.pending.lock().await.active_waiters.insert(req_id, tx);

        let script = active_window_script(req_id);
        self.run_kwin_script(&script).await?;

        let json = tokio::time::timeout(Duration::from_secs(3), rx)
            .await
            .map_err(|_| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("timed out waiting for KWin script callback"),
            })?
            .map_err(|_| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("KWin script aborted before callback"),
            })?;

        if json.is_empty() || json == "null" {
            return Ok(None);
        }
        let parsed: ScriptWindow = serde_json::from_str(&json).map_err(|e| WdoError::Backend {
            backend: NAME,
            source: anyhow::anyhow!("invalid active-window payload: {e}"),
        })?;
        Ok(Some(parsed.into()))
    }

    /// Register a oneshot waiter, ask the caller to build the matching script
    /// (so the request id embedded in the JS lines up with the waiter key),
    /// run it, and return the script's boolean result. Callers map `false`
    /// onto `WdoError::WindowNotFound` with their own context.
    async fn call_action(
        &self,
        what: &'static str,
        build_script: impl FnOnce(u64) -> String,
    ) -> Result<bool> {
        let req_id = self.next_request_id();
        let (tx, rx) = oneshot::channel::<bool>();
        self.pending.lock().await.action_waiters.insert(req_id, tx);

        let script = build_script(req_id);
        self.run_kwin_script(&script).await?;

        let ok = tokio::time::timeout(Duration::from_secs(3), rx)
            .await
            .map_err(|_| WdoError::Backend {
                backend: NAME,
                source: anyhow::anyhow!("timed out waiting for KWin {what} callback"),
            })?
            .unwrap_or(false);
        Ok(ok)
    }
}

fn dbus_err(e: zbus::Error) -> WdoError {
    WdoError::Backend {
        backend: NAME,
        source: anyhow::Error::new(e),
    }
}

// ---- JS script generators --------------------------------------------------

fn list_windows_script(req_id: u64) -> String {
    format!(
        r#"
(function() {{
  var out = [];
  var list = (typeof workspace.windowList === "function")
    ? workspace.windowList()
    : workspace.clientList();
  for (var i = 0; i < list.length; i++) {{
    var w = list[i];
    out.push({{
      id: (w.internalId || w.windowId || i).toString(),
      title: String(w.caption || ""),
      app_id: String(w.resourceClass || w.resourceName || ""),
      pid: (w.pid | 0)
    }});
  }}
  callDBus(
    "{service}", "{path}", "{iface}", "ReportWindows",
    {id}, JSON.stringify(out)
  );
}})();
"#,
        service = BRIDGE_SERVICE,
        path = BRIDGE_PATH,
        iface = BRIDGE_IFACE,
        id = req_id
    )
}

fn active_window_script(req_id: u64) -> String {
    format!(
        r#"
(function() {{
  var w = workspace.activeWindow || workspace.activeClient;
  var payload = "null";
  if (w) {{
    payload = JSON.stringify({{
      id: (w.internalId || w.windowId || 0).toString(),
      title: String(w.caption || ""),
      app_id: String(w.resourceClass || w.resourceName || ""),
      pid: (w.pid | 0)
    }});
  }}
  callDBus(
    "{service}", "{path}", "{iface}", "ReportActive",
    {id}, payload
  );
}})();
"#,
        service = BRIDGE_SERVICE,
        path = BRIDGE_PATH,
        iface = BRIDGE_IFACE,
        id = req_id
    )
}

/// JSON-encode a string as a JS literal. `{:?}` would ~work for ASCII ids but
/// diverges from JS escape syntax on exotic codepoints; JSON strings are a
/// subset of JS string literals, so this is always safe to paste in.
fn js_string_literal(s: &str) -> String {
    serde_json::to_string(s).expect("serde_json cannot fail on &str")
}

fn activate_window_script(req_id: u64, target_id: &str) -> String {
    format!(
        r#"
(function() {{
  var target = {target};
  var list = (typeof workspace.windowList === "function")
    ? workspace.windowList()
    : workspace.clientList();
  var found = false;
  for (var i = 0; i < list.length; i++) {{
    var w = list[i];
    var id = (w.internalId || w.windowId || i).toString();
    if (id === target) {{
      workspace.activeWindow = w;
      found = true;
      break;
    }}
  }}
  callDBus(
    "{service}", "{path}", "{iface}", "ReportAction",
    {id}, found
  );
}})();
"#,
        service = BRIDGE_SERVICE,
        path = BRIDGE_PATH,
        iface = BRIDGE_IFACE,
        id = req_id,
        target = js_string_literal(target_id)
    )
}

fn close_window_script(req_id: u64, target_id: &str) -> String {
    format!(
        r#"
(function() {{
  var target = {target};
  var list = (typeof workspace.windowList === "function")
    ? workspace.windowList()
    : workspace.clientList();
  var found = false;
  for (var i = 0; i < list.length; i++) {{
    var w = list[i];
    var id = (w.internalId || w.windowId || i).toString();
    if (id === target) {{
      if (typeof w.closeWindow === "function") {{
        w.closeWindow();
      }} else if (typeof w.close === "function") {{
        w.close();
      }}
      found = true;
      break;
    }}
  }}
  callDBus(
    "{service}", "{path}", "{iface}", "ReportAction",
    {id}, found
  );
}})();
"#,
        service = BRIDGE_SERVICE,
        path = BRIDGE_PATH,
        iface = BRIDGE_IFACE,
        id = req_id,
        target = js_string_literal(target_id)
    )
}

// ---- Backend trait impl ----------------------------------------------------

#[async_trait]
impl Backend for KdeBackend {
    fn name(&self) -> &'static str {
        NAME
    }

    fn capabilities(&self) -> Capabilities {
        let mut caps = self.input_caps.clone();
        caps.list_windows = true;
        caps.active_window = true;
        caps.activate_window = true;
        caps.close_window = true;
        caps
    }

    async fn key(&self, keysym: &str, dir: KeyDirection) -> Result<()> {
        self.libei.key(keysym, dir).await
    }

    async fn type_text(&self, text: &str, delay: Duration) -> Result<()> {
        self.libei.type_text(text, delay).await
    }

    async fn mouse_move(&self, x: i32, y: i32, absolute: bool) -> Result<()> {
        self.libei.mouse_move(x, y, absolute).await
    }

    async fn mouse_button(&self, btn: MouseButton, dir: KeyDirection) -> Result<()> {
        self.libei.mouse_button(btn, dir).await
    }

    async fn scroll(&self, dx: f64, dy: f64) -> Result<()> {
        self.libei.scroll(dx, dy).await
    }

    async fn list_windows(&self) -> Result<Vec<WindowInfo>> {
        self.list_windows_impl().await
    }

    async fn active_window(&self) -> Result<Option<WindowInfo>> {
        self.active_window_impl().await
    }

    async fn activate_window(&self, id: &WindowId) -> Result<()> {
        let ok = self
            .call_action("activate", |req_id| activate_window_script(req_id, &id.0))
            .await?;
        if !ok {
            return Err(WdoError::WindowNotFound(id.0.clone()));
        }
        Ok(())
    }

    async fn close_window(&self, id: &WindowId) -> Result<()> {
        let ok = self
            .call_action("close", |req_id| close_window_script(req_id, &id.0))
            .await?;
        if !ok {
            return Err(WdoError::WindowNotFound(id.0.clone()));
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn list_and_active_scripts_embed_request_id_and_bridge_addr() {
        let s = list_windows_script(42);
        assert!(s.contains("\"ReportWindows\""));
        assert!(s.contains(BRIDGE_SERVICE));
        assert!(s.contains(BRIDGE_PATH));
        assert!(s.contains(BRIDGE_IFACE));
        assert!(s.contains("42"));
        // Uses windowList() / clientList() for Plasma 6 / 5 compatibility.
        assert!(s.contains("windowList"));
        assert!(s.contains("clientList"));

        let s = active_window_script(7);
        assert!(s.contains("\"ReportActive\""));
        assert!(s.contains("activeWindow"));
        assert!(s.contains("activeClient"));
        assert!(s.contains(" 7,"));
    }

    #[test]
    fn action_scripts_json_encode_the_target_id() {
        // A target id with JS-meaningful chars (quotes, backslashes, a newline)
        // must come out as a valid JS string literal — not a Rust Debug repr.
        let weird = "ab\"c\\d\ne";
        let s = activate_window_script(1, weird);
        // JSON encodes " → \" and \ → \\ and \n → \n, and wraps in double quotes.
        assert!(s.contains(r#"var target = "ab\"c\\d\ne";"#));
        assert!(s.contains("ReportAction"));
        assert!(s.contains("workspace.activeWindow = w"));

        let s = close_window_script(1, weird);
        assert!(s.contains(r#"var target = "ab\"c\\d\ne";"#));
        // Plasma 6 uses closeWindow, Plasma 5 uses close — both dispatched.
        assert!(s.contains("closeWindow"));
        assert!(s.contains("w.close()"));
    }

    #[test]
    fn js_string_literal_is_valid_json() {
        // The output is always a quoted JSON string, parseable back to the input.
        for s in ["", "plain", "with \"quotes\"", "unicode ☃ €", "\0\n\t\r"] {
            let encoded = js_string_literal(s);
            assert!(encoded.starts_with('"') && encoded.ends_with('"'));
            let round_trip: String = serde_json::from_str(&encoded).unwrap();
            assert_eq!(round_trip, s);
        }
    }
}