clairlune_platform/
lib.rs1#[cfg(target_os = "linux")]
13mod linux;
14#[cfg(target_os = "macos")]
16pub mod macos;
17mod store;
18#[cfg(windows)]
19mod windows;
20#[cfg(windows)]
22pub mod windows_live;
23#[cfg(target_os = "linux")]
25pub mod wlr;
26
27pub use store::{Snapshot, StillStore};
28
29use std::fmt;
30use std::path::{Path, PathBuf};
31
32#[derive(Clone, Debug, PartialEq, Eq)]
34pub struct Monitor {
35 pub id: String,
38 pub x: i32,
40 pub y: i32,
41 pub width: u32,
42 pub height: u32,
43 pub primary: bool,
44}
45
46#[derive(Clone, Copy, Debug, PartialEq, Eq)]
48pub struct Capabilities {
49 pub per_monitor: bool,
51 pub live: bool,
53}
54
55#[derive(Debug)]
56pub enum PlatformError {
57 Unsupported(String),
59 Os(String),
61 Io(std::io::Error),
62 NoSuchMonitor(String),
64}
65
66impl fmt::Display for PlatformError {
67 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 match self {
69 PlatformError::Unsupported(why) => write!(f, "not supported here: {why}"),
70 PlatformError::Os(e) => write!(f, "system call failed: {e}"),
71 PlatformError::Io(e) => write!(f, "{e}"),
72 PlatformError::NoSuchMonitor(id) => write!(f, "no connected monitor `{id}`"),
73 }
74 }
75}
76
77impl std::error::Error for PlatformError {}
78
79impl From<std::io::Error> for PlatformError {
80 fn from(e: std::io::Error) -> Self {
81 PlatformError::Io(e)
82 }
83}
84
85pub trait WallpaperHost {
87 fn name(&self) -> &'static str;
89 fn capabilities(&self) -> Capabilities;
90 fn monitors(&self) -> Result<Vec<Monitor>, PlatformError>;
92 fn current(&self, monitor: &Monitor) -> Result<Option<PathBuf>, PlatformError>;
94 fn set_static(&self, monitor: &Monitor, path: &Path) -> Result<(), PlatformError>;
97 fn snapshot(&self) -> Result<Snapshot, PlatformError>;
99 fn restore(&self, snapshot: &Snapshot) -> Result<(), PlatformError>;
101}
102
103pub fn detect() -> Result<Box<dyn WallpaperHost>, PlatformError> {
105 #[cfg(windows)]
106 {
107 Ok(Box::new(windows::WindowsHost::new()?))
108 }
109 #[cfg(target_os = "linux")]
110 {
111 Ok(Box::new(linux::LinuxHost::new()?))
112 }
113 #[cfg(target_os = "macos")]
114 {
115 Ok(Box::new(macos::MacHost::new()?))
116 }
117 #[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
118 {
119 Err(PlatformError::Unsupported(format!(
120 "setting the wallpaper on {} arrives in a later milestone",
121 std::env::consts::OS
122 )))
123 }
124}
125
126pub fn local_data_dir() -> Option<PathBuf> {
130 #[cfg(windows)]
131 {
132 windows::local_app_data()
133 }
134 #[cfg(target_os = "macos")]
135 {
136 home_dir().map(|h| h.join("Library").join("Application Support"))
137 }
138 #[cfg(all(unix, not(target_os = "macos")))]
139 {
140 std::env::var_os("XDG_DATA_HOME")
141 .map(PathBuf::from)
142 .filter(|p| p.is_absolute())
143 .or_else(|| home_dir().map(|h| h.join(".local").join("share")))
144 }
145}
146
147pub fn config_dir() -> Option<PathBuf> {
151 #[cfg(windows)]
152 {
153 windows::roaming_app_data()
154 }
155 #[cfg(target_os = "macos")]
156 {
157 home_dir().map(|h| h.join("Library").join("Application Support"))
158 }
159 #[cfg(all(unix, not(target_os = "macos")))]
160 {
161 std::env::var_os("XDG_CONFIG_HOME")
162 .map(PathBuf::from)
163 .filter(|p| p.is_absolute())
164 .or_else(|| home_dir().map(|h| h.join(".config")))
165 }
166}
167
168pub fn pictures_dir() -> Option<PathBuf> {
170 #[cfg(windows)]
171 {
172 windows::pictures()
173 }
174 #[cfg(unix)]
175 {
176 home_dir().map(|h| h.join("Pictures"))
177 }
178}
179
180pub fn app_data_dir() -> Option<PathBuf> {
182 local_data_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
183}
184
185pub fn app_config_dir() -> Option<PathBuf> {
187 config_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
188}
189
190fn login_command(exe: &Path) -> String {
192 format!("\"{}\"", exe.display())
193}
194
195pub fn start_at_login(exe: &Path) -> Option<bool> {
198 #[cfg(windows)]
199 {
200 let want = login_command(exe);
201 Some(windows::start_at_login_command().is_some_and(|c| c.eq_ignore_ascii_case(&want)))
202 }
203 #[cfg(not(windows))]
204 {
205 let _ = login_command(exe);
206 None
207 }
208}
209
210pub fn set_start_at_login(exe: &Path, enable: bool) -> Result<(), PlatformError> {
212 #[cfg(windows)]
213 {
214 let cmd = login_command(exe);
215 windows::set_start_at_login_command(enable.then_some(cmd.as_str()))
216 }
217 #[cfg(not(windows))]
218 {
219 let _ = (exe, enable);
220 Err(PlatformError::Unsupported(
221 "starting at login arrives with the installers".into(),
222 ))
223 }
224}
225
226pub fn system_prefers_dark() -> Option<bool> {
228 #[cfg(windows)]
229 {
230 windows::apps_use_dark_theme()
231 }
232 #[cfg(not(windows))]
233 {
234 None
235 }
236}
237
238#[cfg(unix)]
239fn home_dir() -> Option<PathBuf> {
240 std::env::var_os("HOME")
241 .filter(|h| !h.is_empty())
242 .map(PathBuf::from)
243}
244
245pub fn init_dpi_awareness() {
249 #[cfg(windows)]
250 windows::init_dpi_awareness();
251}
252
253#[cfg(windows)]
256pub fn attach_parent_console() {
257 windows::attach_parent_console();
258}
259
260#[cfg_attr(not(windows), allow(dead_code))] pub(crate) fn sort_monitors(monitors: &mut [Monitor]) {
264 monitors.sort_by_key(|m| (!m.primary, m.x, m.y));
265}
266
267#[cfg(test)]
268mod tests {
269 use super::*;
270
271 fn m(id: &str, x: i32, primary: bool) -> Monitor {
272 Monitor {
273 id: id.into(),
274 x,
275 y: 0,
276 width: 1920,
277 height: 1080,
278 primary,
279 }
280 }
281
282 #[test]
283 fn primary_first_then_left_to_right() {
284 let mut v = vec![m("c", 1920, false), m("b", -1920, false), m("a", 0, true)];
285 sort_monitors(&mut v);
286 let ids: Vec<&str> = v.iter().map(|m| m.id.as_str()).collect();
287 assert_eq!(ids, ["a", "b", "c"]);
288 }
289}