1#[cfg(target_os = "linux")]
13mod linux;
14mod login;
15#[cfg(target_os = "macos")]
17pub mod macos;
18mod power;
19mod store;
20pub mod usage;
22#[cfg(windows)]
23mod windows;
24#[cfg(windows)]
26pub mod windows_live;
27#[cfg(target_os = "linux")]
29pub mod wlr;
30
31pub use login::BACKGROUND_ARG;
32pub use power::on_battery;
33pub use store::{Snapshot, StillStore};
34
35#[cfg(target_os = "linux")]
39pub fn linux_desktop_monitor() -> Monitor {
40 linux::desktop_monitor()
41}
42
43use std::fmt;
44use std::path::{Path, PathBuf};
45
46#[derive(Clone, Debug, PartialEq, Eq)]
48pub struct Monitor {
49 pub id: String,
52 pub x: i32,
54 pub y: i32,
55 pub width: u32,
56 pub height: u32,
57 pub primary: bool,
58}
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
62pub struct Capabilities {
63 pub per_monitor: bool,
65 pub live: bool,
67}
68
69#[derive(Debug)]
70pub enum PlatformError {
71 Unsupported(String),
73 Os(String),
75 Io(std::io::Error),
76 NoSuchMonitor(String),
78}
79
80impl fmt::Display for PlatformError {
81 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 match self {
83 PlatformError::Unsupported(why) => write!(f, "not supported here: {why}"),
84 PlatformError::Os(e) => write!(f, "system call failed: {e}"),
85 PlatformError::Io(e) => write!(f, "{e}"),
86 PlatformError::NoSuchMonitor(id) => write!(f, "no connected monitor `{id}`"),
87 }
88 }
89}
90
91impl std::error::Error for PlatformError {}
92
93impl From<std::io::Error> for PlatformError {
94 fn from(e: std::io::Error) -> Self {
95 PlatformError::Io(e)
96 }
97}
98
99pub trait WallpaperHost {
101 fn name(&self) -> &'static str;
103 fn capabilities(&self) -> Capabilities;
104 fn monitors(&self) -> Result<Vec<Monitor>, PlatformError>;
106 fn current(&self, monitor: &Monitor) -> Result<Option<PathBuf>, PlatformError>;
108 fn set_static(&self, monitor: &Monitor, path: &Path) -> Result<(), PlatformError>;
111 fn snapshot(&self) -> Result<Snapshot, PlatformError>;
113 fn restore(&self, snapshot: &Snapshot) -> Result<(), PlatformError>;
115}
116
117pub fn detect() -> Result<Box<dyn WallpaperHost>, PlatformError> {
119 #[cfg(windows)]
120 {
121 Ok(Box::new(windows::WindowsHost::new()?))
122 }
123 #[cfg(target_os = "linux")]
124 {
125 Ok(Box::new(linux::LinuxHost::new()?))
126 }
127 #[cfg(target_os = "macos")]
128 {
129 Ok(Box::new(macos::MacHost::new()?))
130 }
131 #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
132 {
133 Err(PlatformError::Unsupported(format!(
134 "setting the wallpaper on {} arrives in a later milestone",
135 std::env::consts::OS
136 )))
137 }
138}
139
140pub fn local_data_dir() -> Option<PathBuf> {
144 #[cfg(windows)]
145 {
146 windows::local_app_data()
147 }
148 #[cfg(target_os = "macos")]
149 {
150 home_dir().map(|h| h.join("Library").join("Application Support"))
151 }
152 #[cfg(all(unix, not(target_os = "macos")))]
153 {
154 std::env::var_os("XDG_DATA_HOME")
155 .map(PathBuf::from)
156 .filter(|p| p.is_absolute())
157 .or_else(|| home_dir().map(|h| h.join(".local").join("share")))
158 }
159}
160
161pub fn config_dir() -> Option<PathBuf> {
165 #[cfg(windows)]
166 {
167 windows::roaming_app_data()
168 }
169 #[cfg(target_os = "macos")]
170 {
171 home_dir().map(|h| h.join("Library").join("Application Support"))
172 }
173 #[cfg(all(unix, not(target_os = "macos")))]
174 {
175 std::env::var_os("XDG_CONFIG_HOME")
176 .map(PathBuf::from)
177 .filter(|p| p.is_absolute())
178 .or_else(|| home_dir().map(|h| h.join(".config")))
179 }
180}
181
182pub fn pictures_dir() -> Option<PathBuf> {
184 #[cfg(windows)]
185 {
186 windows::pictures()
187 }
188 #[cfg(unix)]
189 {
190 home_dir().map(|h| h.join("Pictures"))
191 }
192}
193
194pub fn app_data_dir() -> Option<PathBuf> {
196 local_data_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
197}
198
199pub fn app_config_dir() -> Option<PathBuf> {
201 config_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
202}
203
204#[cfg_attr(not(windows), allow(dead_code))]
206fn login_command(exe: &Path) -> String {
207 format!("\"{}\" {BACKGROUND_ARG}", exe.display())
208}
209
210#[cfg_attr(not(windows), allow(dead_code))]
212fn old_login_command(exe: &Path) -> String {
213 format!("\"{}\"", exe.display())
214}
215
216pub fn start_at_login(exe: &Path) -> Option<bool> {
219 #[cfg(windows)]
220 {
221 let (want, old) = (login_command(exe), old_login_command(exe));
222 Some(
223 windows::start_at_login_command()
224 .is_some_and(|c| c.eq_ignore_ascii_case(&want) || c.eq_ignore_ascii_case(&old)),
225 )
226 }
227 #[cfg(any(target_os = "linux", target_os = "macos"))]
228 {
229 Some(login::registered(exe))
230 }
231 #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
232 {
233 let _ = exe;
234 None
235 }
236}
237
238pub fn login_entry_outdated(exe: &Path) -> bool {
241 #[cfg(windows)]
242 {
243 windows::start_at_login_command()
244 .is_some_and(|c| c.eq_ignore_ascii_case(&old_login_command(exe)))
245 }
246 #[cfg(not(windows))]
247 {
248 let _ = exe;
249 false
250 }
251}
252
253pub fn set_start_at_login(exe: &Path, enable: bool) -> Result<(), PlatformError> {
256 #[cfg(windows)]
257 {
258 let cmd = login_command(exe);
259 windows::set_start_at_login_command(enable.then_some(cmd.as_str()))
260 }
261 #[cfg(any(target_os = "linux", target_os = "macos"))]
262 {
263 login::set(exe, enable)
264 }
265 #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
266 {
267 let _ = (exe, enable);
268 Err(PlatformError::Unsupported(
269 "starting at login isn't supported on this system".into(),
270 ))
271 }
272}
273
274pub fn taskbar_is_light() -> Option<bool> {
277 #[cfg(windows)]
278 {
279 windows::taskbar_is_light()
280 }
281 #[cfg(not(windows))]
282 {
283 None
284 }
285}
286
287pub fn system_prefers_dark() -> Option<bool> {
289 #[cfg(windows)]
290 {
291 windows::apps_use_dark_theme()
292 }
293 #[cfg(not(windows))]
294 {
295 None
296 }
297}
298
299#[cfg(unix)]
300fn home_dir() -> Option<PathBuf> {
301 std::env::var_os("HOME")
302 .filter(|h| !h.is_empty())
303 .map(PathBuf::from)
304}
305
306pub fn init_dpi_awareness() {
310 #[cfg(windows)]
311 windows::init_dpi_awareness();
312}
313
314#[cfg(windows)]
317pub fn attach_parent_console() {
318 windows::attach_parent_console();
319}
320
321#[cfg(any(windows, target_os = "macos", test))]
324pub(crate) fn sort_monitors(monitors: &mut [Monitor]) {
325 monitors.sort_by_key(|m| (!m.primary, m.x, m.y));
326}
327
328#[cfg(test)]
329mod tests {
330 use super::*;
331
332 fn m(id: &str, x: i32, primary: bool) -> Monitor {
333 Monitor {
334 id: id.into(),
335 x,
336 y: 0,
337 width: 1920,
338 height: 1080,
339 primary,
340 }
341 }
342
343 #[test]
344 fn primary_first_then_left_to_right() {
345 let mut v = vec![m("c", 1920, false), m("b", -1920, false), m("a", 0, true)];
346 sort_monitors(&mut v);
347 let ids: Vec<&str> = v.iter().map(|m| m.id.as_str()).collect();
348 assert_eq!(ids, ["a", "b", "c"]);
349 }
350}