From 321b4ff1bcdd16f45d620d1ec4fbffbe9b445775 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Fri, 29 Sep 2023 00:24:52 +0800 Subject: [PATCH] Add reload config ability Signed-off-by: Avimitin --- cli/src/main.rs | 7 ++++++- daemon/src/ipc_server.rs | 7 +++++++ daemon/src/wpaperd.rs | 27 +++++++++++++++++++++++++++ ipc/src/lib.rs | 2 ++ 4 files changed, 42 insertions(+), 1 deletion(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index a12c457..2bb7d24 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -20,6 +20,7 @@ enum SubCmd { AllWallpapers, NextWallpaper { monitors: Vec }, PreviousWallpaper { monitors: Vec }, + ReloadConfig, } fn main() { @@ -31,6 +32,7 @@ fn main() { SubCmd::AllWallpapers => IpcMessage::AllWallpapers, SubCmd::NextWallpaper { monitors } => IpcMessage::NextWallpaper { monitors }, SubCmd::PreviousWallpaper { monitors } => IpcMessage::PreviousWallpaper { monitors }, + SubCmd::ReloadConfig => IpcMessage::ReloadConfig, }; conn.write_all(&serde_json::to_vec(&msg).unwrap()).unwrap(); // Add a new line after the message @@ -46,12 +48,15 @@ fn main() { println!("{monitor}: {path:?}"); } } - IpcResponse::Ok => (), + IpcResponse::Ok => println!("daemon response: OK"), }, Err(err) => match err { IpcError::MonitorNotFound { monitor } => { eprintln!("monitor {monitor} could not be found") } + IpcError::Other { error } => { + eprintln!("{error}") + } }, } } diff --git a/daemon/src/ipc_server.rs b/daemon/src/ipc_server.rs index 7a6b7fa..288eae4 100644 --- a/daemon/src/ipc_server.rs +++ b/daemon/src/ipc_server.rs @@ -120,6 +120,13 @@ fn handle_message(buffer: &mut String, ustream: UnixStream, wpaperd: &mut Wpaper IpcResponse::Ok }), + + IpcMessage::ReloadConfig => { + wpaperd.reload_config().map(|_| IpcResponse::Ok)?; + // Force update + wpaperd.surfaces.iter_mut().for_each(|sur| sur.next_image()); + Ok(IpcResponse::Ok) + } }; let mut stream = BufWriter::new(ustream); diff --git a/daemon/src/wpaperd.rs b/daemon/src/wpaperd.rs index ef1b491..cace1b1 100644 --- a/daemon/src/wpaperd.rs +++ b/daemon/src/wpaperd.rs @@ -1,5 +1,6 @@ use std::sync::{Arc, Mutex}; +use color_eyre::eyre::Context; use color_eyre::Result; use smithay_client_toolkit::compositor::{CompositorHandler, CompositorState}; use smithay_client_toolkit::output::{OutputHandler, OutputState}; @@ -50,6 +51,32 @@ impl Wpaperd { use_scaled_window, }) } + + pub fn reload_config(&mut self) -> Result<()> { + let mut wallpaper_config = self.wallpaper_config.lock().unwrap(); + let new_config = + WallpaperConfig::new_from_path(&wallpaper_config.path).with_context(|| { + format!( + "reading configuration from file {:?}", + wallpaper_config.path + ) + }); + match new_config { + Ok(config) => { + if config != *wallpaper_config { + *wallpaper_config = config; + println!("Configuration updated"); + } else { + println!("Configuration unchanged"); + } + Ok(()) + } + Err(err) => { + log::error!("{:?}", err); + Err(err) + } + } + } } impl CompositorHandler for Wpaperd { diff --git a/ipc/src/lib.rs b/ipc/src/lib.rs index 92869e1..282f4b0 100644 --- a/ipc/src/lib.rs +++ b/ipc/src/lib.rs @@ -9,6 +9,7 @@ pub enum IpcMessage { NextWallpaper { monitors: Vec }, PreviousWallpaper { monitors: Vec }, AllWallpapers, + ReloadConfig, } #[derive(Serialize, Deserialize)] @@ -21,6 +22,7 @@ pub enum IpcResponse { #[derive(Serialize, Deserialize, Debug)] pub enum IpcError { MonitorNotFound { monitor: String }, + Other { error: String }, } pub fn socket_path() -> Result {