#![cfg_attr(
not(any(windows, target_os = "macos", target_os = "linux")),
allow(dead_code)
)]
use crate::apply::LiveScene;
use std::time::{Duration, Instant};
#[cfg(windows)]
type Host = clairlune_live::desktop::Desktop;
#[cfg(target_os = "macos")]
type Host = clairlune_live::mac::MacDesktop;
#[cfg(target_os = "linux")]
type Host = clairlune_live::linux::LinuxDesktop;
#[cfg_attr(not(windows), allow(dead_code))]
const HEALTH_CHECK: Duration = Duration::from_secs(1);
pub struct Live {
scenes: Vec<LiveScene>,
enabled: bool,
fps: u32,
cover_pause: crate::settings::CoverPause,
battery_saver: bool,
battery_pause: bool,
on_battery: bool,
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
desktop: Option<Host>,
next: Option<Instant>,
checked: Option<Instant>,
pub status: Option<String>,
}
#[cfg(not(target_os = "linux"))]
pub fn supported() -> Result<(), String> {
if cfg!(any(windows, target_os = "macos")) {
Ok(())
} else {
Err(
"Live wallpapers run on Windows, macOS and Linux so far; here the still frame is used."
.into(),
)
}
}
#[cfg(target_os = "linux")]
pub fn supported() -> Result<(), String> {
static ANSWER: std::sync::OnceLock<Result<(), String>> = std::sync::OnceLock::new();
ANSWER
.get_or_init(|| {
clairlune_live::linux::available()
.map_err(|e| format!("Live isn't available here: {e}. The still frame is used."))
})
.clone()
}
impl Live {
pub fn new(enabled: bool, fps: u32) -> Live {
Live {
scenes: Vec::new(),
enabled: enabled && supported().is_ok(),
fps: fps.clamp(1, 60),
cover_pause: crate::settings::CoverPause::default(),
battery_saver: true,
battery_pause: false,
on_battery: false,
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
desktop: None,
next: None,
checked: None,
status: None,
}
}
#[cfg(target_os = "macos")]
pub fn reset_stills(&self) {
let Ok(host) = clairlune_platform::detect() else {
return;
};
for s in &self.scenes {
if let Some(still) = &s.still
&& let Err(e) = host.set_static(&s.monitor, still)
{
tracing::warn!(target: "clairlune", error = %e, "could not set the still again");
}
}
}
#[cfg_attr(not(windows), allow(dead_code))]
pub fn refresh(&mut self) {
self.restart();
}
pub fn show(&mut self, scenes: Vec<LiveScene>) {
self.scenes = scenes;
self.restart();
}
pub fn set_enabled(&mut self, on: bool) {
let on = on && supported().is_ok();
if on != self.enabled {
self.enabled = on;
self.restart();
}
}
pub fn set_fps(&mut self, fps: u32) {
self.fps = fps.clamp(1, 60);
}
pub fn set_battery_policy(&mut self, saver: bool, pause: bool) {
self.battery_saver = saver;
self.battery_pause = pause;
}
pub fn set_cover_pause(&mut self, pause: crate::settings::CoverPause) {
self.cover_pause = pause;
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
if let Some(d) = &mut self.desktop {
d.cover_policy = cover_policy(pause);
}
}
fn effective_fps(&self) -> u32 {
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
let cap = self.desktop.as_ref().map_or(60, |d| d.max_fps());
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
let cap = 60;
let fps = self.fps.min(cap);
if self.on_battery && self.battery_saver {
fps.min(6)
} else {
fps
}
}
pub const SCROLL_MIN_FPS: u32 = 12;
pub fn scrolling_ok(&self) -> bool {
!self.enabled || self.effective_fps() >= Self::SCROLL_MIN_FPS
}
pub fn frame_interval(&self) -> Duration {
self.frame()
}
fn frame(&self) -> Duration {
Duration::from_secs_f64(1.0 / f64::from(self.effective_fps()))
}
fn restart(&mut self) {
self.stop();
if !self.enabled || self.scenes.is_empty() {
return;
}
self.start();
}
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
fn start(&mut self) {
self.status = supported().err();
}
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
fn start(&mut self) {
use clairlune_live::Player;
let players: Option<Vec<_>> = self
.scenes
.iter()
.map(|s| {
match s.view {
Some(v) => Player::new_view(s.package.clone(), s.theme, s.buddy, v),
None => Player::new(s.package.clone(), s.theme, s.buddy),
}
.map(|p| (s.monitor.clone(), p))
})
.collect();
let Some(players) = players else {
self.status = Some("Live isn't available: a scene lacks its theme.".into());
return;
};
match Host::start(players) {
Ok(mut desktop) => {
desktop.cover_policy = cover_policy(self.cover_pause);
let n = self.scenes.len();
let capped = if desktop.max_fps() < self.fps {
format!(" (at most {} fps here)", desktop.max_fps())
} else {
String::new()
};
self.status = Some(format!(
"Playing on {n} monitor{} at {} fps{capped}.",
if n == 1 { "" } else { "s" },
self.fps.min(desktop.max_fps())
));
self.desktop = Some(desktop);
self.next = Some(Instant::now());
self.checked = Some(Instant::now());
}
Err(e) => {
tracing::warn!(target: "clairlune", error = %e, "live wallpaper didn't start");
self.status = Some(format!(
"Live isn't available here ({e}); the still frame is the wallpaper."
));
}
}
}
fn stop(&mut self) {
self.next = None;
#[cfg(any(target_os = "macos", target_os = "linux"))]
if self.desktop.take().is_some() {
tracing::info!(target: "clairlune", "live wallpaper stopped");
}
#[cfg(windows)]
if let Some(desktop) = self.desktop.take() {
let classic = desktop.layout() == "classic";
drop(desktop);
if classic && let Ok(host) = clairlune_platform::detect() {
for s in &self.scenes {
if let Some(still) = &s.still {
let _ = host.set_static(&s.monitor, still);
}
}
}
tracing::info!(target: "clairlune", "live wallpaper stopped");
}
if !self.enabled {
self.status = None;
}
}
pub fn tick(&mut self, now: Instant) -> Option<Instant> {
#[cfg(windows)]
{
let desktop = self.desktop.as_mut()?;
if self.checked.is_none_or(|t| now >= t + HEALTH_CHECK) {
self.checked = Some(now);
let battery = clairlune_platform::windows_live::on_battery();
if battery != self.on_battery {
tracing::info!(target: "clairlune", on_battery = battery, "live: power source");
self.on_battery = battery;
}
if !desktop.healthy() {
tracing::info!(target: "clairlune", "the desktop layer changed (Explorer restarted?); starting live again");
self.restart();
return self.next;
}
}
let next = self.next?;
if self.on_battery && self.battery_pause {
return self.checked.map(|t| t + HEALTH_CHECK);
}
if now >= next {
if let Err(e) = desktop.tick() {
tracing::warn!(target: "clairlune", error = %e, "live frame failed; stopping");
self.stop();
self.status = Some(format!(
"Live stopped ({e}); the still frame is the wallpaper."
));
return None;
}
self.next = Some((next + self.frame()).max(now));
}
self.next
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
{
let desktop = self.desktop.as_mut()?;
let next = self.next?;
if now >= next {
if let Err(e) = desktop.tick() {
tracing::warn!(target: "clairlune", error = %e, "live frame failed; stopping");
self.stop();
self.status = Some(format!(
"Live stopped ({e}); the still frame is the wallpaper."
));
return None;
}
self.next = Some((next + self.frame()).max(now));
}
self.next
}
#[cfg(not(any(windows, target_os = "macos", target_os = "linux")))]
{
let _ = now;
None
}
}
}
#[cfg(any(windows, target_os = "macos", target_os = "linux"))]
fn cover_policy(p: crate::settings::CoverPause) -> clairlune_live::CoverPolicy {
use crate::settings::CoverPause;
use clairlune_live::CoverPolicy;
match p {
CoverPause::ThatMonitor => CoverPolicy::ThatMonitor,
CoverPause::AllMonitors => CoverPolicy::AllMonitors,
CoverPause::Off => CoverPolicy::Off,
}
}
impl Drop for Live {
fn drop(&mut self) {
self.stop();
}
}