Skip to content

Commit

Permalink
Merge pull request #43 from Avimitin/reload-config
Browse files Browse the repository at this point in the history
Add reload config ability
  • Loading branch information
danyspin97 authored Oct 10, 2023
2 parents 3d45adf + 655b8f1 commit 94023bf
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum SubCmd {
AllWallpapers,
NextWallpaper { monitors: Vec<String> },
PreviousWallpaper { monitors: Vec<String> },
ReloadConfig,
}

fn main() {
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions daemon/src/ipc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ fn handle_message(buffer: &mut String, ustream: UnixStream, wpaperd: &mut Wpaper

IpcResponse::Ok
}),

IpcMessage::ReloadConfig => {
wpaperd.reload_config()?;
Ok(IpcResponse::Ok)
}
};

let mut stream = BufWriter::new(ustream);
Expand Down
8 changes: 7 additions & 1 deletion daemon/src/wallpaper_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde::Deserialize;

use crate::wallpaper_info::WallpaperInfo;

#[derive(Deserialize, PartialEq)]
#[derive(Deserialize)]
pub struct WallpaperConfig {
#[serde(flatten)]
data: HashMap<String, Arc<WallpaperInfo>>,
Expand Down Expand Up @@ -53,3 +53,9 @@ Either remove `duration` or set `path` to a directory"
self.data.get(name).unwrap_or(&self.default_config).clone()
}
}

impl PartialEq for WallpaperConfig {
fn eq(&self, other: &Self) -> bool {
self.data == other.data
}
}
25 changes: 25 additions & 0 deletions daemon/src/wpaperd.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -50,6 +51,30 @@ 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 !(*wallpaper_config == config) {
*wallpaper_config = config;
log::info!("Configuration updated");
}
Ok(())
}
Err(err) => {
log::error!("{:?}", err);
Err(err)
}
}
}
}

impl CompositorHandler for Wpaperd {
Expand Down
1 change: 1 addition & 0 deletions ipc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub enum IpcMessage {
NextWallpaper { monitors: Vec<String> },
PreviousWallpaper { monitors: Vec<String> },
AllWallpapers,
ReloadConfig,
}

#[derive(Serialize, Deserialize)]
Expand Down

0 comments on commit 94023bf

Please sign in to comment.