clairlune-platform 0.8.0

Clairlune's platform layer: monitor enumeration and setting the desktop wallpaper
Documentation
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
//! Linux: each desktop sets its wallpaper differently (PLAN.md section 2), so
//! the host drives the desktop's own tools: `gsettings` (GNOME, Cinnamon,
//! MATE), Plasma's D-Bus scripting (KDE), `xfconf-query` (XFCE), `swaymsg`
//! (Sway). One image for all monitors (per-monitor images come later), sized
//! to the largest output `xrandr` reports.
//!
//! The decisions (which desktop, which commands, the screen size) are plain
//! functions with tests; only running the commands touches the system.

use crate::{Capabilities, Monitor, PlatformError, Snapshot, WallpaperHost};
use std::path::{Path, PathBuf};
use std::process::Command;

/// The id of the one "monitor" this host manages: the whole desktop.
const DESKTOP_ID: &str = "linux-desktop";

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Desktop {
    Gnome,
    Cinnamon,
    Mate,
    Kde,
    Xfce,
    Sway,
}

impl Desktop {
    fn name(self) -> &'static str {
        match self {
            Desktop::Gnome => "linux-gnome",
            Desktop::Cinnamon => "linux-cinnamon",
            Desktop::Mate => "linux-mate",
            Desktop::Kde => "linux-kde",
            Desktop::Xfce => "linux-xfce",
            Desktop::Sway => "linux-sway",
        }
    }
}

/// The desktop from `XDG_CURRENT_DESKTOP` (a `:`-separated list such as
/// `ubuntu:GNOME`) and whether a Sway socket is set.
pub(crate) fn detect_desktop(current: &str, sway: bool) -> Option<Desktop> {
    let parts: Vec<String> = current.split(':').map(|p| p.to_ascii_lowercase()).collect();
    let has = |name: &str| parts.iter().any(|p| p == name);
    if has("kde") {
        Some(Desktop::Kde)
    } else if has("x-cinnamon") || has("cinnamon") {
        Some(Desktop::Cinnamon)
    } else if has("mate") {
        Some(Desktop::Mate)
    } else if has("xfce") {
        Some(Desktop::Xfce)
    } else if has("gnome") || has("unity") || has("budgie") || has("pantheon") {
        Some(Desktop::Gnome)
    } else if has("sway") || sway {
        Some(Desktop::Sway)
    } else {
        None
    }
}

/// A `file://` URI with everything but unreserved characters and `/`
/// percent-encoded.
pub(crate) fn file_uri(path: &Path) -> String {
    let mut out = String::from("file://");
    for b in path.to_string_lossy().bytes() {
        if b.is_ascii_alphanumeric() || b"-._~/".contains(&b) {
            out.push(b as char);
        } else {
            out.push_str(&format!("%{b:02X}"));
        }
    }
    out
}

/// The path in a `file://` URI (as `gsettings get` prints it, quoted).
pub(crate) fn path_from_uri(value: &str) -> Option<PathBuf> {
    let v = value.trim().trim_matches('\'').trim_matches('"');
    let rest = v.strip_prefix("file://").unwrap_or(v);
    if rest.is_empty() {
        return None;
    }
    let bytes = rest.as_bytes();
    let mut out = Vec::with_capacity(bytes.len());
    let mut i = 0;
    while i < bytes.len() {
        if bytes[i] == b'%'
            && i + 2 < bytes.len()
            && let Some(b) = std::str::from_utf8(&bytes[i + 1..i + 3])
                .ok()
                .and_then(|h| u8::from_str_radix(h, 16).ok())
        {
            out.push(b);
            i += 3;
            continue;
        }
        out.push(bytes[i]);
        i += 1;
    }
    Some(PathBuf::from(String::from_utf8_lossy(&out).into_owned()))
}

fn argv(parts: &[&str]) -> Vec<String> {
    parts.iter().map(|s| s.to_string()).collect()
}

/// The commands that set `path` as the wallpaper on `desktop`.
pub(crate) fn set_commands(
    desktop: Desktop,
    path: &Path,
    xfce_props: &[String],
) -> Vec<Vec<String>> {
    let uri = file_uri(path);
    let file = path.to_string_lossy().into_owned();
    match desktop {
        // GNOME keeps a separate dark-mode wallpaper; set both.
        Desktop::Gnome => vec![
            argv(&[
                "gsettings",
                "set",
                "org.gnome.desktop.background",
                "picture-uri",
                &uri,
            ]),
            argv(&[
                "gsettings",
                "set",
                "org.gnome.desktop.background",
                "picture-uri-dark",
                &uri,
            ]),
            argv(&[
                "gsettings",
                "set",
                "org.gnome.desktop.background",
                "picture-options",
                "zoom",
            ]),
        ],
        Desktop::Cinnamon => vec![
            argv(&[
                "gsettings",
                "set",
                "org.cinnamon.desktop.background",
                "picture-uri",
                &uri,
            ]),
            argv(&[
                "gsettings",
                "set",
                "org.cinnamon.desktop.background",
                "picture-options",
                "zoom",
            ]),
        ],
        Desktop::Mate => vec![
            argv(&[
                "gsettings",
                "set",
                "org.mate.background",
                "picture-filename",
                &file,
            ]),
            argv(&[
                "gsettings",
                "set",
                "org.mate.background",
                "picture-options",
                "zoom",
            ]),
        ],
        Desktop::Kde => {
            let script = format!(
                "var ds = desktops(); for (var i = 0; i < ds.length; i++) {{ var d = ds[i]; \
                 d.wallpaperPlugin = 'org.kde.image'; \
                 d.currentConfigGroup = Array('Wallpaper', 'org.kde.image', 'General'); \
                 d.writeConfig('Image', '{}'); }}",
                uri.replace('\'', "%27")
            );
            vec![vec![
                "dbus-send".into(),
                "--session".into(),
                "--dest=org.kde.plasmashell".into(),
                "--type=method_call".into(),
                "/PlasmaShell".into(),
                "org.kde.PlasmaShell.evaluateScript".into(),
                format!("string:{script}"),
            ]]
        }
        Desktop::Xfce => xfce_props
            .iter()
            .map(|p| argv(&["xfconf-query", "-c", "xfce4-desktop", "-p", p, "-s", &file]))
            .collect(),
        Desktop::Sway => vec![argv(&["swaymsg", "output", "*", "bg", &file, "fill"])],
    }
}

/// XFCE keeps one `last-image` property per monitor and workspace.
pub(crate) fn xfce_image_props(listing: &str) -> Vec<String> {
    listing
        .lines()
        .map(str::trim)
        .filter(|l| l.ends_with("/last-image"))
        .map(str::to_string)
        .collect()
}

/// The size to render at: the primary output from `xrandr --current`, else
/// the largest connected one.
pub(crate) fn screen_size(xrandr: &str) -> Option<(u32, u32)> {
    let mut best: Option<(bool, u32, u32)> = None;
    for line in xrandr.lines() {
        let words: Vec<&str> = line.split_whitespace().collect();
        if words.get(1) != Some(&"connected") {
            continue;
        }
        let primary = words.contains(&"primary");
        let Some(geom) = words.iter().find(|w| w.contains('x') && w.contains('+')) else {
            continue; // connected but off
        };
        let (wh, _) = geom.split_once('+')?;
        let (w, h) = wh.split_once('x')?;
        let (Ok(w), Ok(h)) = (w.parse::<u32>(), h.parse::<u32>()) else {
            continue;
        };
        let better = match best {
            None => true,
            Some((bp, bw, bh)) => (primary && !bp) || (primary == bp && w * h > bw * bh),
        };
        if better {
            best = Some((primary, w, h));
        }
    }
    best.map(|(_, w, h)| (w, h))
}

fn run(cmd: &[String]) -> Result<String, PlatformError> {
    let out = Command::new(&cmd[0])
        .args(&cmd[1..])
        .output()
        .map_err(|e| {
            if e.kind() == std::io::ErrorKind::NotFound {
                PlatformError::Unsupported(format!("`{}` is not installed", cmd[0]))
            } else {
                PlatformError::Os(format!("{}: {e}", cmd[0]))
            }
        })?;
    if !out.status.success() {
        return Err(PlatformError::Os(format!(
            "{} failed: {}",
            cmd[0],
            String::from_utf8_lossy(&out.stderr).trim()
        )));
    }
    Ok(String::from_utf8_lossy(&out.stdout).into_owned())
}

pub(crate) struct LinuxHost {
    desktop: Desktop,
}

impl LinuxHost {
    pub(crate) fn new() -> Result<Self, PlatformError> {
        let current = std::env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
        let sway = std::env::var_os("SWAYSOCK").is_some();
        detect_desktop(&current, sway)
            .map(|desktop| LinuxHost { desktop })
            .ok_or_else(|| {
                PlatformError::Unsupported(format!(
                    "desktop `{current}` isn't supported yet (GNOME, Cinnamon, MATE, KDE Plasma, XFCE and Sway are)"
                ))
            })
    }

    fn xfce_props(&self) -> Vec<String> {
        run(&argv(&["xfconf-query", "-c", "xfce4-desktop", "-l"]))
            .map(|l| xfce_image_props(&l))
            .unwrap_or_default()
    }
}

impl WallpaperHost for LinuxHost {
    fn name(&self) -> &'static str {
        self.desktop.name()
    }

    fn capabilities(&self) -> Capabilities {
        Capabilities {
            per_monitor: false,
            live: false,
        }
    }

    fn monitors(&self) -> Result<Vec<Monitor>, PlatformError> {
        let (width, height) = run(&argv(&["xrandr", "--current"]))
            .ok()
            .and_then(|o| screen_size(&o))
            .unwrap_or((1920, 1080));
        Ok(vec![Monitor {
            id: DESKTOP_ID.into(),
            x: 0,
            y: 0,
            width,
            height,
            primary: true,
        }])
    }

    fn current(&self, _monitor: &Monitor) -> Result<Option<PathBuf>, PlatformError> {
        let get = |schema: &str, key: &str| {
            run(&argv(&["gsettings", "get", schema, key])).map(|v| path_from_uri(&v))
        };
        match self.desktop {
            Desktop::Gnome => get("org.gnome.desktop.background", "picture-uri"),
            Desktop::Cinnamon => get("org.cinnamon.desktop.background", "picture-uri"),
            Desktop::Mate => get("org.mate.background", "picture-filename"),
            Desktop::Xfce => match self.xfce_props().first() {
                Some(p) => run(&argv(&["xfconf-query", "-c", "xfce4-desktop", "-p", p]))
                    .map(|v| path_from_uri(&v)),
                None => Ok(None),
            },
            // Plasma and Sway don't report it simply; restore can't bring
            // back what it never saw.
            Desktop::Kde | Desktop::Sway => Ok(None),
        }
    }

    fn set_static(&self, _monitor: &Monitor, path: &Path) -> Result<(), PlatformError> {
        if !path.is_absolute() {
            return Err(PlatformError::Os(format!(
                "wallpaper path must be absolute: {}",
                path.display()
            )));
        }
        let props = if self.desktop == Desktop::Xfce {
            self.xfce_props()
        } else {
            Vec::new()
        };
        for cmd in set_commands(self.desktop, path, &props) {
            run(&cmd)?;
        }
        Ok(())
    }

    fn snapshot(&self) -> Result<Snapshot, PlatformError> {
        let m = &self.monitors()?[0];
        Ok(Snapshot {
            monitors: vec![(m.id.clone(), self.current(m)?)],
            position: None,
        })
    }

    fn restore(&self, snapshot: &Snapshot) -> Result<(), PlatformError> {
        let m = &self.monitors()?[0];
        match snapshot.monitors.first().and_then(|(_, p)| p.as_deref()) {
            Some(path) => self.set_static(m, path),
            None => Ok(()),
        }
    }
}

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

    #[test]
    fn desktops_from_the_environment() {
        assert_eq!(detect_desktop("ubuntu:GNOME", false), Some(Desktop::Gnome));
        assert_eq!(detect_desktop("KDE", false), Some(Desktop::Kde));
        assert_eq!(detect_desktop("X-Cinnamon", false), Some(Desktop::Cinnamon));
        assert_eq!(detect_desktop("MATE", false), Some(Desktop::Mate));
        assert_eq!(detect_desktop("XFCE", false), Some(Desktop::Xfce));
        assert_eq!(detect_desktop("Budgie:GNOME", false), Some(Desktop::Gnome));
        assert_eq!(detect_desktop("", true), Some(Desktop::Sway));
        assert_eq!(detect_desktop("Hyprland", false), None);
    }

    #[test]
    fn uris_round_trip() {
        let p = Path::new("/home/a b/.local/share/clairlune/current/x%y.png");
        let uri = file_uri(p);
        assert_eq!(
            uri,
            "file:///home/a%20b/.local/share/clairlune/current/x%25y.png"
        );
        assert_eq!(path_from_uri(&format!("'{uri}'")).as_deref(), Some(p));
        assert_eq!(path_from_uri("''"), None);
    }

    #[test]
    fn gnome_sets_light_and_dark() {
        let cmds = set_commands(Desktop::Gnome, Path::new("/tmp/w.png"), &[]);
        let keys: Vec<&str> = cmds.iter().map(|c| c[3].as_str()).collect();
        assert_eq!(keys, ["picture-uri", "picture-uri-dark", "picture-options"]);
        assert_eq!(cmds[0][4], "file:///tmp/w.png");
    }

    #[test]
    fn xfce_sets_every_monitor() {
        let listing = "/backdrop/screen0/monitorDP-1/workspace0/last-image\n/backdrop/screen0/monitorDP-1/workspace0/image-style\n/backdrop/screen0/monitorHDMI-1/workspace0/last-image\n";
        let props = xfce_image_props(listing);
        assert_eq!(props.len(), 2);
        let cmds = set_commands(Desktop::Xfce, Path::new("/tmp/w.png"), &props);
        assert_eq!(cmds.len(), 2);
        assert_eq!(
            cmds[1][4],
            "/backdrop/screen0/monitorHDMI-1/workspace0/last-image"
        );
        assert_eq!(cmds[1][6], "/tmp/w.png");
    }

    #[test]
    fn kde_script_names_the_image() {
        let cmds = set_commands(Desktop::Kde, Path::new("/tmp/w.png"), &[]);
        assert_eq!(cmds.len(), 1);
        assert!(
            cmds[0]
                .last()
                .unwrap()
                .contains("writeConfig('Image', 'file:///tmp/w.png')")
        );
    }

    #[test]
    fn screen_size_prefers_the_primary_output() {
        let x = "Screen 0: minimum 320 x 200, current 4480 x 1440, maximum 16384 x 16384\n\
                 HDMI-1 connected 1920x1080+2560+180 (normal left inverted right x axis y axis) 527mm x 296mm\n\
                 DP-1 connected primary 2560x1440+0+0 (normal left inverted right x axis y axis) 597mm x 336mm\n\
                 DP-2 disconnected (normal left inverted right x axis y axis)\n";
        assert_eq!(screen_size(x), Some((2560, 1440)));
        let no_primary =
            "HDMI-1 connected 1920x1080+0+0 (normal)\nDP-1 connected 3840x2160+1920+0 (normal)\n";
        assert_eq!(screen_size(no_primary), Some((3840, 2160)));
        assert_eq!(screen_size("eDP-1 connected (normal)\n"), None);
    }
}