Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303#![warn(missing_docs)]use clap::clap_derive::ArgEnum;use log::{error, info, trace, warn};use rand::Rng;use std::{collections::VecDeque, fs, path::PathBuf, process::Command, time::Duration};
use crate::daemon::{Config, DaemonArgs};
#[derive(Debug)]struct History { previous: VecDeque<PathBuf>, // Never empty next: Vec<PathBuf>, // Possibly empty history_max_size: usize,}
impl History { fn has_next(&self) -> bool { !self.next.is_empty() }
fn has_previous(&self) -> bool { // previous must not be empty self.previous.len() >= 2 }
fn go_next(&mut self) { if self.has_next() { let image = self.next.pop().unwrap(); self.push_back(image); } else { warn!("Calling go_next without next image existing"); } }
fn go_previous(&mut self) { if self.has_previous() { self.next.push(self.previous.pop_back().unwrap()); } else { warn!("Calling go_previous without previous image existing"); } }
fn push_back(&mut self, path: PathBuf) { if self.previous.len() >= self.history_max_size { self.previous.pop_front(); } self.previous.push_back(path); }
fn contains(&self, path: &PathBuf) -> bool { self.previous.contains(path) }}
#[derive(Debug)]pub struct WallpaperCommands { pub wallpaper_cmd: String, pub wallpaper_post_cmd: Option<String>, pub wallpaper_post_offset: Option<usize>,}
impl WallpaperCommands { pub fn new(args: &DaemonArgs, config: &Config) -> Self { let wallpaper_cmd = args .wallpaper_change_command .as_ref() .unwrap_or(&config.wallpaper_change_command); let wallpaper_post_cmd = args .wallpaper_post_change_command .as_ref() .or(config.wallpaper_post_change_command.as_ref()); let wallpaper_post_offset = args .wallpaper_post_change_offset .as_ref() .or(config.wallpaper_post_change_offset.as_ref());
WallpaperCommands { wallpaper_cmd: wallpaper_cmd.to_owned(), wallpaper_post_cmd: wallpaper_post_cmd.cloned(), wallpaper_post_offset: wallpaper_post_offset.cloned(), } }}
/// Global object to store the current state#[derive(Debug)]pub struct State { history: History, action: NextImage, previous_action: NextImage, change_interval: Duration, image_dir: PathBuf, use_fallback: bool, default_image: PathBuf, wallpaper_cmds: WallpaperCommands,}
#[derive(Debug, Clone, PartialEq, Eq, Copy, ArgEnum)]pub enum NextImage { Random, Linear, Static,}
pub enum ChangeImageDirection { Next, Previous,}
impl State { pub fn new( change_interval: Duration, image_dir: PathBuf, default_image: PathBuf, action: NextImage, wallpaper_cmds: WallpaperCommands, history_max_size: usize, ) -> Self { let mut history = VecDeque::new(); history.push_back(default_image.clone());
let image_dir = if image_dir.starts_with("~/") { let mut home = PathBuf::from(std::env::var("HOME").unwrap()); home.push(image_dir.components().skip(1).collect::<PathBuf>()); home } else { image_dir };
let default_image = if default_image.starts_with("~/") { let mut home = PathBuf::from(std::env::var("HOME").unwrap()); home.push(default_image.components().skip(1).collect::<PathBuf>()); home } else { default_image };
State { history: History { previous: history, next: Vec::new(), history_max_size, }, action, previous_action: action, change_interval, image_dir, use_fallback: false, default_image, wallpaper_cmds, } }
pub fn change_image(&mut self, direction: ChangeImageDirection) { if self.use_fallback { info!("Can't change image while using fallback"); return; } if let NextImage::Static = self.action { info!("Can't change image while in static mode"); return; }
match direction { ChangeImageDirection::Next => { info!("Going to the next image"); // "Redo" if self.history.has_next() { self.history.go_next(); } else { let num_pics = fs::read_dir(&self.image_dir).unwrap().count();
loop { let idx = if self.action == NextImage::Random { rand::thread_rng().gen_range(0..num_pics) } else { let mut idx = fs::read_dir(&self.image_dir) .unwrap() .filter_map(|res| res.ok().map(|e| e.path())) .position(|elem| elem == *self.history.previous.back().unwrap()) .unwrap_or(0); idx += 1; idx %= num_pics; idx };
let wallpaper_path = fs::read_dir(&self.image_dir) .unwrap() .filter_map(|res| res.ok().map(|e| e.path())) .nth(idx) .unwrap();
if !self.history.contains(&wallpaper_path) || num_pics <= self.history.history_max_size { self.history.push_back(wallpaper_path); break; } } } } ChangeImageDirection::Previous => { info!("Going to the previous image"); if self.history.has_previous() { self.history.go_previous(); } else { info!("There is no previous image"); } } }
// Update current image if self.update().is_err() { error!("Error setting the wallpaper"); } }
pub fn update(&self) -> Result<(), ()> { info!("Updating current wallpaper"); let path = self.get_current_image(); trace!("setting wallpaper to {}", path.to_string_lossy());
let wallpaper_cmd = self .wallpaper_cmds .wallpaper_cmd .replace("%wallpaper%", path.to_str().unwrap());
trace!("Calling {:?}", wallpaper_cmd); let _process = Command::new("sh") .arg("-c") .arg(wallpaper_cmd) .output() .unwrap();
if let Some(delay) = self.wallpaper_cmds.wallpaper_post_offset { if let Some(command) = &self.wallpaper_cmds.wallpaper_post_cmd { if let Some(prev) = self.history.previous.iter().rev().nth(delay) { let prev = command.replace("%wallpaper%", prev.to_str().unwrap()); trace!("Calling {:?}", prev); let _process = Command::new("sh").arg("-c").arg(&prev).output().unwrap(); } } } Ok(()) }
pub fn update_action(&mut self, action: NextImage, image: Option<PathBuf>) { info!("Setting action to {:?}", action); self.action = action; if let Some(image) = image { self.history.push_back(image); if self.update().is_err() { error!("Error setting the wallpaper"); } } }
pub fn save(&mut self) { self.use_fallback = !self.use_fallback; info!("Setting fallback to {}", self.use_fallback); if self.use_fallback { self.previous_action = self.action; self.action = NextImage::Static; self.history.push_back(self.default_image.clone()); } else { self.action = self.previous_action; self.history.previous.pop_back(); } if self.update().is_err() { error!("Error setting the wallpaper"); } }
pub fn set_image_dir(&mut self, dir: PathBuf) { self.image_dir = dir; self.update().unwrap(); }
pub fn get_image_dir(&self) -> &PathBuf { &self.image_dir }
pub fn get_current_image(&self) -> &PathBuf { self.history.previous.back().unwrap() }
pub fn get_action(&self) -> NextImage { self.action }
pub fn change_interval(&mut self, i: Duration) { self.change_interval = i; }
pub fn get_change_interval(&self) -> Duration { self.change_interval }
pub fn get_fallback(&self) -> bool { self.use_fallback }}