diff --git a/src/main.rs b/src/main.rs index 3a336b0..3b412f2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,7 +219,13 @@ async fn main() { Config::default() }; - // TODO: add a map to not check for binaries twice? + fn check_binary(binary: &str) { + if !is_binary_available(binary) { + error!("{binary} is not available, exiting."); + std::process::exit(1); + } + } + match cfg.gamma_backend { GammaBackend::Hyprctl => check_binary("hyprctl"), GammaBackend::Ddcutil => check_binary("ddcutil"), @@ -341,9 +347,39 @@ async fn reload_config(notify: Arc) { match Config::load(&config_path) { Ok(cfg) => { debug!("Config file reloaded successfully"); + + fn check_binary(binary: &str) -> bool { + if !is_binary_available(binary) { + error!("{binary} is not available, exiting."); + return false; + } + true + } + + let gamma_check = match cfg.gamma_backend { + GammaBackend::Hyprctl => check_binary("hyprctl"), + GammaBackend::Ddcutil => check_binary("ddcutil"), + GammaBackend::Xsct => check_binary("xsct"), + GammaBackend::Redshift => check_binary("redshift"), + GammaBackend::Gammastep => check_binary("gammastep"), + GammaBackend::None => true, + }; + + let temp_check = match cfg.temp_backend { + TempBackend::Hyprctl => check_binary("hyprctl"), + TempBackend::Gammastep => check_binary("gammastep"), + TempBackend::Xsct => check_binary("xsct"), + TempBackend::Redshift => check_binary("redshift"), + TempBackend::None => true, + }; + + if !gamma_check || !temp_check { + error!("One or more binaries are not available, retaining old config."); + return; + } + *config = cfg; reset_cache(); - trace!("new config: {:#?}", config); let new_modified = std::fs::metadata(&config_path) .and_then(|m| m.modified()) @@ -359,6 +395,7 @@ async fn reload_config(notify: Arc) { } } } + fn is_binary_available(binary_name: &str) -> bool { use std::fs; if let Ok(paths) = env::var("PATH") { @@ -411,10 +448,3 @@ fn config_handle() -> Arc> { fn get_time() -> Time { Time::now().expect("Failed to get local time") } - -fn check_binary(binary: &str) { - if !is_binary_available(binary) { - error!("{binary} is not available, exiting."); - std::process::exit(1); - } -}