clairlune-platform 0.29.0

Clairlune's platform layer: monitor enumeration and setting the desktop wallpaper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Platform layer: finding the monitors and setting the desktop wallpaper.
//!
//! The OS wallpaper APIs only set static images. A
//! [`WallpaperHost`] wraps one OS's API: list the monitors with stable ids and
//! physical sizes, read and set each monitor's wallpaper. [`detect`] picks the
//! host for the running system. [`StillStore`] writes each still under a new
//! file name, because some systems don't reload an unchanged path.
//!
//! Windows (`IDesktopWallpaper`, per monitor) and macOS (`NSWorkspace`, per
//! screen) are implemented. Elsewhere [`detect`] says the wallpaper isn't
//! set here: the scenes still render (`clairlune-cli render`, `gif`).

mod login;
/// macOS: the wallpaper per screen, and Space changes.
#[cfg(target_os = "macos")]
pub mod macos;
mod power;
mod store;
/// What this process uses (CPU time, memory), for the log.
pub mod usage;
#[cfg(windows)]
mod windows;
/// Live wallpaper windows in the desktop layer (Windows).
#[cfg(windows)]
pub mod windows_live;

pub use login::BACKGROUND_ARG;
pub use power::on_battery;
pub use store::{Snapshot, StillStore};

use std::fmt;
use std::path::{Path, PathBuf};

/// One monitor: its size in physical pixels, and where it sits in the
/// system's display arrangement.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Monitor {
    /// Stable id that survives reboots and re-plugging: the device path on
    /// Windows. Per-monitor seeds and saved scenes key on it.
    pub id: String,
    /// Its top-left corner in the system's display arrangement (the origin
    /// can be negative), in the arrangement's units: pixels on Windows,
    /// points on macOS.
    pub x: i32,
    pub y: i32,
    /// Its size in physical pixels.
    pub width: u32,
    pub height: u32,
    pub primary: bool,
    /// Physical pixels per arrangement unit: 2 on a Retina screen, 1
    /// elsewhere (and always on Windows, which arranges in pixels).
    pub scale: u32,
}

impl Monitor {
    /// Its size in the arrangement's units (points on macOS).
    pub fn arranged_size(&self) -> (u32, u32) {
        let s = self.scale.max(1);
        (self.width.div_ceil(s), self.height.div_ceil(s))
    }
}

/// What a host can do.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Capabilities {
    /// A different image per monitor.
    pub per_monitor: bool,
    /// An animated host behind the desktop icons (later milestone).
    pub live: bool,
}

#[derive(Debug)]
pub enum PlatformError {
    /// No host for this system yet, with a reason for the user.
    Unsupported(String),
    /// The OS call failed.
    Os(String),
    Io(std::io::Error),
    /// The monitor id is not (or no longer) connected.
    NoSuchMonitor(String),
}

impl fmt::Display for PlatformError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            PlatformError::Unsupported(why) => write!(f, "not supported here: {why}"),
            PlatformError::Os(e) => write!(f, "system call failed: {e}"),
            PlatformError::Io(e) => write!(f, "{e}"),
            PlatformError::NoSuchMonitor(id) => write!(f, "no connected monitor `{id}`"),
        }
    }
}

impl std::error::Error for PlatformError {}

impl From<std::io::Error> for PlatformError {
    fn from(e: std::io::Error) -> Self {
        PlatformError::Io(e)
    }
}

/// One system's wallpaper API.
pub trait WallpaperHost {
    /// Short name for logs and the settings screen, e.g. `windows-desktop-wallpaper`.
    fn name(&self) -> &'static str;
    fn capabilities(&self) -> Capabilities;
    /// Connected monitors, primary first.
    fn monitors(&self) -> Result<Vec<Monitor>, PlatformError>;
    /// The image currently set on a monitor, if any (a solid colour has none).
    fn current(&self, monitor: &Monitor) -> Result<Option<PathBuf>, PlatformError>;
    /// Sets a still image on one monitor. The path must be absolute, and the
    /// image should already be the monitor's size.
    fn set_static(&self, monitor: &Monitor, path: &Path) -> Result<(), PlatformError>;
    /// Everything needed to put the current wallpaper back later.
    fn snapshot(&self) -> Result<Snapshot, PlatformError>;
    /// Puts a snapshot back. Monitors that are gone are skipped.
    fn restore(&self, snapshot: &Snapshot) -> Result<(), PlatformError>;
}

/// The host for the running system.
pub fn detect() -> Result<Box<dyn WallpaperHost>, PlatformError> {
    #[cfg(windows)]
    {
        Ok(Box::new(windows::WindowsHost::new()?))
    }
    #[cfg(target_os = "macos")]
    {
        Ok(Box::new(macos::MacHost::new()?))
    }
    #[cfg(not(any(windows, target_os = "macos")))]
    {
        Err(PlatformError::Unsupported(format!(
            "Clairlune sets the wallpaper on Windows and macOS, not on {}; `render` and `gif` make the images",
            std::env::consts::OS
        )))
    }
}

/// The per-user local data directory: Local AppData on Windows,
/// `~/Library/Application Support` on macOS, `$XDG_DATA_HOME` (or
/// `~/.local/share`) elsewhere.
pub fn local_data_dir() -> Option<PathBuf> {
    #[cfg(windows)]
    {
        windows::local_app_data()
    }
    #[cfg(target_os = "macos")]
    {
        home_dir().map(|h| h.join("Library").join("Application Support"))
    }
    #[cfg(all(unix, not(target_os = "macos")))]
    {
        std::env::var_os("XDG_DATA_HOME")
            .map(PathBuf::from)
            .filter(|p| p.is_absolute())
            .or_else(|| home_dir().map(|h| h.join(".local").join("share")))
    }
}

/// The per-user configuration directory: Roaming AppData on Windows,
/// `~/Library/Application Support` on macOS, `$XDG_CONFIG_HOME` (or
/// `~/.config`) elsewhere.
pub fn config_dir() -> Option<PathBuf> {
    #[cfg(windows)]
    {
        windows::roaming_app_data()
    }
    #[cfg(target_os = "macos")]
    {
        home_dir().map(|h| h.join("Library").join("Application Support"))
    }
    #[cfg(all(unix, not(target_os = "macos")))]
    {
        std::env::var_os("XDG_CONFIG_HOME")
            .map(PathBuf::from)
            .filter(|p| p.is_absolute())
            .or_else(|| home_dir().map(|h| h.join(".config")))
    }
}

/// The user's Pictures folder (exports go to `Pictures/Clairlune`).
pub fn pictures_dir() -> Option<PathBuf> {
    #[cfg(windows)]
    {
        windows::pictures()
    }
    #[cfg(unix)]
    {
        home_dir().map(|h| h.join("Pictures"))
    }
}

/// Clairlune's data directory (stills, archive, logs): `<local data>/clairlune`.
pub fn app_data_dir() -> Option<PathBuf> {
    local_data_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
}

/// Clairlune's configuration directory: `<config>/clairlune`.
pub fn app_config_dir() -> Option<PathBuf> {
    config_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
}

/// The command line that starts `exe` at login, quietly.
#[cfg_attr(not(windows), allow(dead_code))]
fn login_command(exe: &Path) -> String {
    format!("\"{}\" {BACKGROUND_ARG}", exe.display())
}

/// The command 0.12 and older registered: no background argument.
#[cfg_attr(not(windows), allow(dead_code))]
fn old_login_command(exe: &Path) -> String {
    format!("\"{}\"", exe.display())
}

/// Whether `exe` is registered to start at login; `None` where this build
/// can't manage login items.
pub fn start_at_login(exe: &Path) -> Option<bool> {
    #[cfg(windows)]
    {
        let (want, old) = (login_command(exe), old_login_command(exe));
        Some(
            windows::start_at_login_command()
                .is_some_and(|c| c.eq_ignore_ascii_case(&want) || c.eq_ignore_ascii_case(&old)),
        )
    }
    #[cfg(target_os = "macos")]
    {
        Some(login::registered(exe))
    }
    #[cfg(not(any(windows, target_os = "macos")))]
    {
        let _ = exe;
        None
    }
}

/// Whether the login entry for `exe` is one from 0.12 or older (Windows),
/// which starts it without [`BACKGROUND_ARG`].
pub fn login_entry_outdated(exe: &Path) -> bool {
    #[cfg(windows)]
    {
        windows::start_at_login_command()
            .is_some_and(|c| c.eq_ignore_ascii_case(&old_login_command(exe)))
    }
    #[cfg(not(windows))]
    {
        let _ = exe;
        false
    }
}

/// Start at login is registered but switched off where only the user can
/// switch it on: macOS's System Settings > General > Login Items, or
/// Windows' Settings > Apps > Startup. `false` elsewhere.
pub fn start_at_login_needs_approval(exe: &Path) -> bool {
    #[cfg(target_os = "macos")]
    {
        login::needs_approval(exe)
    }
    #[cfg(windows)]
    {
        let _ = exe;
        windows::startup_switched_off()
    }
    #[cfg(not(any(target_os = "macos", windows)))]
    {
        let _ = exe;
        false
    }
}

/// Opens where the user switches start at login on: System Settings >
/// General > Login Items (macOS), Settings > Apps > Startup (Windows).
/// Nothing elsewhere.
pub fn open_login_items_settings() {
    #[cfg(target_os = "macos")]
    login::open_login_items();
    #[cfg(windows)]
    let _ = windows::open_url("ms-settings:startupapps");
}

/// Registers or removes `exe` as a login item (per user, no admin rights),
/// started with [`BACKGROUND_ARG`].
pub fn set_start_at_login(exe: &Path, enable: bool) -> Result<(), PlatformError> {
    #[cfg(windows)]
    {
        let cmd = login_command(exe);
        windows::set_start_at_login_command(enable.then_some(cmd.as_str()))
    }
    #[cfg(target_os = "macos")]
    {
        login::set(exe, enable)
    }
    #[cfg(not(any(windows, target_os = "macos")))]
    {
        let _ = (exe, enable);
        Err(PlatformError::Unsupported(
            "starting at login isn't supported on this system".into(),
        ))
    }
}

/// Whether the taskbar (where the tray icon sits) is light, when that is
/// known: Windows only (macOS tints a template icon itself).
pub fn taskbar_is_light() -> Option<bool> {
    #[cfg(windows)]
    {
        windows::taskbar_is_light()
    }
    #[cfg(not(windows))]
    {
        None
    }
}

/// Draws pop-up menus (the tray's) dark or light, or as apps are (`None`).
/// Windows only; macOS menus follow the system by themselves.
pub fn set_menu_theme(dark: Option<bool>) {
    #[cfg(windows)]
    windows::set_menu_theme(dark);
    #[cfg(not(windows))]
    let _ = dark;
}

/// Whether the system asks apps for a dark appearance, when that is known.
pub fn system_prefers_dark() -> Option<bool> {
    #[cfg(windows)]
    {
        windows::apps_use_dark_theme()
    }
    #[cfg(not(windows))]
    {
        None
    }
}

#[cfg(unix)]
fn home_dir() -> Option<PathBuf> {
    std::env::var_os("HOME")
        .filter(|h| !h.is_empty())
        .map(PathBuf::from)
}

/// Makes the process aware of each monitor's DPI so sizes come back in physical
/// pixels. Call once, before any window or monitor query. A no-op where it
/// does not apply.
pub fn init_dpi_awareness() {
    #[cfg(windows)]
    windows::init_dpi_awareness();
}

/// Opens a web page in the default browser (`&` and `%` in it are safe).
pub fn open_url(url: &str) -> Result<(), String> {
    #[cfg(windows)]
    {
        windows::open_url(url)
    }
    #[cfg(not(windows))]
    {
        let opener = if cfg!(target_os = "macos") {
            "open"
        } else {
            "xdg-open"
        };
        std::process::Command::new(opener)
            .arg(url)
            .spawn()
            .map(|_| ())
            .map_err(|e| e.to_string())
    }
}

/// Lets a GUI-built exe print to the terminal it was started from (Windows;
/// elsewhere the process already shares it).
#[cfg(windows)]
pub fn attach_parent_console() {
    windows::attach_parent_console();
}

/// Sorts monitors primary first, then left to right, top to bottom, so the
/// order is the same on every run.
#[cfg(any(windows, target_os = "macos", test))]
pub(crate) fn sort_monitors(monitors: &mut [Monitor]) {
    monitors.sort_by_key(|m| (!m.primary, m.x, m.y));
}

#[cfg(test)]
mod tests {
    use super::*;

    fn m(id: &str, x: i32, primary: bool) -> Monitor {
        Monitor {
            id: id.into(),
            x,
            y: 0,
            width: 1920,
            height: 1080,
            primary,
            scale: 1,
        }
    }

    #[test]
    fn primary_first_then_left_to_right() {
        let mut v = vec![m("c", 1920, false), m("b", -1920, false), m("a", 0, true)];
        sort_monitors(&mut v);
        let ids: Vec<&str> = v.iter().map(|m| m.id.as_str()).collect();
        assert_eq!(ids, ["a", "b", "c"]);
    }
}