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 start_at_login_needs_approval(exe: &Path) -> bool {
257 #[cfg(target_os = "macos")]
258 {
259 login::needs_approval(exe)
260 }
261 #[cfg(not(target_os = "macos"))]
262 {
263 let _ = exe;
264 false
265 }
266}
267
268pub fn open_login_items_settings() {
270 #[cfg(target_os = "macos")]
271 login::open_login_items();
272}
273
274pub fn set_start_at_login(exe: &Path, enable: bool) -> Result<(), PlatformError> {
277 #[cfg(windows)]
278 {
279 let cmd = login_command(exe);
280 windows::set_start_at_login_command(enable.then_some(cmd.as_str()))
281 }
282 #[cfg(any(target_os = "linux", target_os = "macos"))]
283 {
284 login::set(exe, enable)
285 }
286 #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
287 {
288 let _ = (exe, enable);
289 Err(PlatformError::Unsupported(
290 "starting at login isn't supported on this system".into(),
291 ))
292 }
293}
294
295pub fn taskbar_is_light() -> Option<bool> {
298 #[cfg(windows)]
299 {
300 windows::taskbar_is_light()
301 }
302 #[cfg(not(windows))]
303 {
304 None
305 }
306}
307
308pub fn system_prefers_dark() -> Option<bool> {
310 #[cfg(windows)]
311 {
312 windows::apps_use_dark_theme()
313 }
314 #[cfg(not(windows))]
315 {
316 None
317 }
318}
319
320#[cfg(unix)]
321fn home_dir() -> Option<PathBuf> {
322 std::env::var_os("HOME")
323 .filter(|h| !h.is_empty())
324 .map(PathBuf::from)
325}
326
327pub fn init_dpi_awareness() {
331 #[cfg(windows)]
332 windows::init_dpi_awareness();
333}
334
335pub fn open_url(url: &str) -> Result<(), String> {
337 #[cfg(windows)]
338 {
339 windows::open_url(url)
340 }
341 #[cfg(not(windows))]
342 {
343 let opener = if cfg!(target_os = "macos") {
344 "open"
345 } else {
346 "xdg-open"
347 };
348 std::process::Command::new(opener)
349 .arg(url)
350 .spawn()
351 .map(|_| ())
352 .map_err(|e| e.to_string())
353 }
354}
355
356#[cfg(windows)]
359pub fn attach_parent_console() {
360 windows::attach_parent_console();
361}
362
363#[cfg(any(windows, target_os = "macos", test))]
366pub(crate) fn sort_monitors(monitors: &mut [Monitor]) {
367 monitors.sort_by_key(|m| (!m.primary, m.x, m.y));
368}
369
370#[cfg(test)]
371mod tests {
372 use super::*;
373
374 fn m(id: &str, x: i32, primary: bool) -> Monitor {
375 Monitor {
376 id: id.into(),
377 x,
378 y: 0,
379 width: 1920,
380 height: 1080,
381 primary,
382 }
383 }
384
385 #[test]
386 fn primary_first_then_left_to_right() {
387 let mut v = vec![m("c", 1920, false), m("b", -1920, false), m("a", 0, true)];
388 sort_monitors(&mut v);
389 let ids: Vec<&str> = v.iter().map(|m| m.id.as_str()).collect();
390 assert_eq!(ids, ["a", "b", "c"]);
391 }
392}