#[cfg(target_os = "linux")]
mod linux;
mod login;
#[cfg(target_os = "macos")]
pub mod macos;
mod power;
mod store;
pub mod usage;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub mod windows_live;
#[cfg(target_os = "linux")]
pub mod wlr;
pub use login::BACKGROUND_ARG;
pub use power::on_battery;
pub use store::{Snapshot, StillStore};
#[cfg(target_os = "linux")]
pub fn linux_desktop_monitor() -> Monitor {
linux::desktop_monitor()
}
use std::fmt;
use std::path::{Path, PathBuf};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Monitor {
pub id: String,
pub x: i32,
pub y: i32,
pub width: u32,
pub height: u32,
pub primary: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Capabilities {
pub per_monitor: bool,
pub live: bool,
}
#[derive(Debug)]
pub enum PlatformError {
Unsupported(String),
Os(String),
Io(std::io::Error),
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)
}
}
pub trait WallpaperHost {
fn name(&self) -> &'static str;
fn capabilities(&self) -> Capabilities;
fn monitors(&self) -> Result<Vec<Monitor>, PlatformError>;
fn current(&self, monitor: &Monitor) -> Result<Option<PathBuf>, PlatformError>;
fn set_static(&self, monitor: &Monitor, path: &Path) -> Result<(), PlatformError>;
fn snapshot(&self) -> Result<Snapshot, PlatformError>;
fn restore(&self, snapshot: &Snapshot) -> Result<(), PlatformError>;
}
pub fn detect() -> Result<Box<dyn WallpaperHost>, PlatformError> {
#[cfg(windows)]
{
Ok(Box::new(windows::WindowsHost::new()?))
}
#[cfg(target_os = "linux")]
{
Ok(Box::new(linux::LinuxHost::new()?))
}
#[cfg(target_os = "macos")]
{
Ok(Box::new(macos::MacHost::new()?))
}
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
{
Err(PlatformError::Unsupported(format!(
"setting the wallpaper on {} arrives in a later milestone",
std::env::consts::OS
)))
}
}
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")))
}
}
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")))
}
}
pub fn pictures_dir() -> Option<PathBuf> {
#[cfg(windows)]
{
windows::pictures()
}
#[cfg(unix)]
{
home_dir().map(|h| h.join("Pictures"))
}
}
pub fn app_data_dir() -> Option<PathBuf> {
local_data_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
}
pub fn app_config_dir() -> Option<PathBuf> {
config_dir().map(|d| d.join(clairlune_scene::ident::APP_ID))
}
#[cfg_attr(not(windows), allow(dead_code))]
fn login_command(exe: &Path) -> String {
format!("\"{}\" {BACKGROUND_ARG}", exe.display())
}
#[cfg_attr(not(windows), allow(dead_code))]
fn old_login_command(exe: &Path) -> String {
format!("\"{}\"", exe.display())
}
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(any(target_os = "linux", target_os = "macos"))]
{
Some(login::registered(exe))
}
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
{
let _ = exe;
None
}
}
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
}
}
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(any(target_os = "linux", target_os = "macos"))]
{
login::set(exe, enable)
}
#[cfg(not(any(windows, target_os = "linux", target_os = "macos")))]
{
let _ = (exe, enable);
Err(PlatformError::Unsupported(
"starting at login isn't supported on this system".into(),
))
}
}
pub fn taskbar_is_light() -> Option<bool> {
#[cfg(windows)]
{
windows::taskbar_is_light()
}
#[cfg(not(windows))]
{
None
}
}
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)
}
pub fn init_dpi_awareness() {
#[cfg(windows)]
windows::init_dpi_awareness();
}
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())
}
}
#[cfg(windows)]
pub fn attach_parent_console() {
windows::attach_parent_console();
}
#[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,
}
}
#[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"]);
}
}