diff --git a/src/lib.rs b/src/lib.rs index 056a590..7c746a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -14,6 +14,9 @@ pub mod log; pub mod notify; pub mod time; +static LAST_TEMP: AtomicU16 = AtomicU16::new(0); +static LAST_GAMMA: AtomicU16 = AtomicU16::new(0); + #[derive(Debug, Copy, Clone)] pub struct Config { pub temp_day: u16, @@ -28,13 +31,11 @@ pub struct Config { pub sunrise_end: u8, pub notification_timeout: u32, + pub disable_timeout: u32, pub gamma_backend: GammaBackend, pub temp_backend: TempBackend, } -static LAST_TEMP: AtomicU16 = AtomicU16::new(0); -static LAST_GAMMA: AtomicU16 = AtomicU16::new(0); - #[derive(Debug, PartialEq, Copy, Clone)] pub enum GammaBackend { Hyprctl, @@ -65,6 +66,7 @@ impl Default for Config { sunset_end: 22, sunrise_start: 4, sunrise_end: 7, + disable_timeout: 300, notification_timeout: 5000, gamma_backend: GammaBackend::Hyprctl, temp_backend: TempBackend::Hyprctl, @@ -135,6 +137,9 @@ impl Config { "notification_timeout" => { config.notification_timeout = value.parse::()?; } + "disable_timeout" => { + config.disable_timeout = value.parse::()?; + } "gamma_backend" => match value.to_lowercase().as_str() { "hyprctl" => config.gamma_backend = GammaBackend::Hyprctl, "ddcutil" => config.gamma_backend = GammaBackend::Ddcutil, diff --git a/src/main.rs b/src/main.rs index 82f5ade..d482840 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,9 +87,42 @@ async fn socket_server(disabled: Arc, notify: Arc) { match line.trim() { "disable" => { trace!("disable dispatched"); + let config = *config_guard().await; + let disabled_clone = Arc::clone(&disabled); + let notify_state = match hinoirisetr::notify::InitializedNotificationSystem::new( + "hinoirisetr", + ) { + Ok(not) => NotifyState::Enabled(not), + Err(_) => NotifyState::Disabled, + }; + disabled.store(true, Ordering::SeqCst); notify.notify_one(); debug!("dimming is disabled"); + + // Spawn a background task to remind after timeout + println!("{}", config.disable_timeout); + if config.disable_timeout != 0 { + debug!("Spawning dimming timeout task"); + tokio::spawn(async move { + let timeout_secs = config.disable_timeout; + tokio::time::sleep(Duration::from_secs(timeout_secs.into())).await; + + if disabled_clone.load(Ordering::SeqCst) { + warn!("You've had dimming disabled for too long!"); + + // Show notification + if let NotifyState::Enabled(ref not) = notify_state { + let _ = not.show_notification( + "Dimming Reminder", + "You have dimming disabled!", + "notification-icon", + (config.notification_timeout as i32) * 5, + ); + } + } + }); + } } "enable" => { trace!("enable dispatched");