Skip to content

Commit

Permalink
Add reload config ability
Browse files Browse the repository at this point in the history
Signed-off-by: Avimitin <[email protected]>
  • Loading branch information
Avimitin committed Sep 28, 2023
1 parent 3d45adf commit 321b4ff
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
7 changes: 6 additions & 1 deletion 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 All @@ -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}")
}
},
}
}
7 changes: 7 additions & 0 deletions daemon/src/ipc_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 27 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,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 {
Expand Down
2 changes: 2 additions & 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 All @@ -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<PathBuf, BaseDirectoriesError> {
Expand Down

0 comments on commit 321b4ff

Please sign in to comment.