wallpape_rs/
lib.rs

1//! This library gets and sets the desktop wallpaper/background.
2//!
3//! The supported desktops are:
4//! * Windows
5//! * macOS
6//! * GNOME
7//! * KDE
8//! * Cinnamon
9//! * Unity
10//! * Budgie
11//! * XFCE
12//! * LXDE
13//! * MATE
14//! * Deepin
15//! * Most Wayland compositors (set only, requires swaybg)
16//! * i3 (set only, requires feh)
17//!
18//! # Example
19//! ```
20//! use wallpaper;
21//!
22//!fn main() {
23//!    println!("{:?}", wallpaper::get());
24//!    wallpaper::set_from_path("/usr/share/backgrounds/gnome/adwaita-day.png").unwrap();
25//!    wallpaper::set_mode(wallpaper::Mode::Crop).unwrap();
26//!    println!("{:?}", wallpaper::get());
27//!}
28//! ```
29
30use std::error::Error;
31
32#[cfg(all(unix, not(target_os = "macos")))]
33mod linux;
34
35#[cfg(all(unix, not(target_os = "macos")))]
36pub use crate::linux::*;
37
38// macos
39#[cfg(target_os = "macos")]
40mod macos;
41
42#[cfg(target_os = "macos")]
43pub use macos::*;
44
45#[cfg(windows)]
46mod win;
47
48#[cfg(windows)]
49pub use win::*;
50
51// unsupported
52#[cfg(not(any(unix, windows)))]
53mod unsupported;
54
55#[cfg(not(any(unix, windows)))]
56pub use unsupported::*;
57
58// from_url feature
59#[cfg(feature = "from_url")]
60mod from_url;
61
62#[cfg(feature = "from_url")]
63pub(crate) use from_url::download_image;
64
65type Result<T> = std::result::Result<T, Box<dyn Error>>;
66
67#[derive(Clone, Debug)]
68pub enum Mode {
69    Center,
70    Crop,
71    Fit,
72    Span,
73    Stretch,
74    Tile,
75}
76
77#[cfg(unix)]
78fn get_stdout(command: &str, args: &[&str]) -> Result<String> {
79    use std::process::Command;
80
81    let output = Command::new(command).args(args).output()?;
82    if output.status.success() {
83        Ok(String::from_utf8(output.stdout)?.trim().into())
84    } else {
85        Err(format!(
86            "{} exited with status code {}",
87            command,
88            output.status.code().unwrap_or(-1),
89        )
90        .into())
91    }
92}
93
94#[cfg(unix)]
95#[inline]
96fn run(command: &str, args: &[&str]) -> Result<()> {
97    get_stdout(command, args).map(|_| ())
98}