wallpaper/linux/
mod.rs

1mod gnome;
2mod lxde;
3
4use crate::{get_stdout, run, Error, Mode, Result};
5use std::{env, process::Command};
6
7/// Returns the wallpaper of the current desktop.
8pub fn get() -> Result<String> {
9    let desktop = env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
10
11    if gnome::is_compliant(&desktop) {
12        return gnome::get();
13    }
14
15    match desktop.as_str() {
16        "MATE" => parse_dconf(
17            "dconf",
18            &["read", "/org/mate/desktop/background/picture-filename"],
19        ),
20        "LXDE" => lxde::get(),
21        "Deepin" => parse_dconf(
22            "dconf",
23            &[
24                "read",
25                "/com/deepin/wrap/gnome/desktop/background/picture-uri",
26            ],
27        ),
28        _ => Err(Error::UnsupportedDesktop),
29    }
30}
31
32/// Sets the wallpaper for the current desktop from a file path.
33pub fn set_from_path(path: &str) -> Result<()> {
34    let desktop = env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
35
36    if gnome::is_compliant(&desktop) {
37        return gnome::set(path);
38    }
39
40    match desktop.as_str() {
41        "X-Cinnamon" => run(
42            "dconf",
43            &[
44                "write",
45                "/org/cinnamon/desktop/background/picture-uri",
46                &enquote::enquote('"', &format!("file://{}", path)),
47            ],
48        ),
49        "MATE" => run(
50            "dconf",
51            &[
52                "write",
53                "/org/mate/desktop/background/picture-filename",
54                &enquote::enquote('"', path),
55            ],
56        ),
57        "LXDE" => lxde::set(path),
58        "Deepin" => run(
59            "dconf",
60            &[
61                "write",
62                "/com/deepin/wrap/gnome/desktop/background/picture-uri",
63                &enquote::enquote('"', &format!("file://{}", path)),
64            ],
65        ),
66        _ => {
67            if let Ok(mut child) = Command::new("swaybg").args(["-i", path]).spawn() {
68                child.stdout = None;
69                child.stderr = None;
70                return Ok(());
71            }
72
73            run("feh", &["--bg-fill", path])
74        }
75    }
76}
77
78/// Sets the wallpaper style.
79pub fn set_mode(mode: Mode) -> Result<()> {
80    let desktop = env::var("XDG_CURRENT_DESKTOP").unwrap_or_default();
81
82    if gnome::is_compliant(&desktop) {
83        return gnome::set_mode(mode);
84    }
85
86    match desktop.as_str() {
87        "X-Cinnamon" => run(
88            "dconf",
89            &[
90                "write",
91                "/org/cinnamon/desktop/background/picture-options",
92                &mode.get_gnome_string(),
93            ],
94        ),
95        "MATE" => run(
96            "dconf",
97            &[
98                "write",
99                "/org/mate/desktop/background/picture-options",
100                &mode.get_gnome_string(),
101            ],
102        ),
103        "LXDE" => lxde::set_mode(mode),
104        "Deepin" => run(
105            "dconf",
106            &[
107                "write",
108                "/com/deepin/wrap/gnome/desktop/background/picture-options",
109                &mode.get_gnome_string(),
110            ],
111        ),
112        _ => Err(Error::UnsupportedDesktop),
113    }
114}
115
116fn parse_dconf(command: &str, args: &[&str]) -> Result<String> {
117    let mut stdout = enquote::unquote(&get_stdout(command, args)?)?;
118    // removes file protocol
119    if stdout.starts_with("file://") {
120        stdout = stdout[7..].into();
121    }
122    Ok(stdout)
123}