diff --git a/Cargo.lock b/Cargo.lock index 520b0b0..53c671a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,7 +244,7 @@ checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" [[package]] name = "hinoirisetr" -version = "0.2.0" +version = "0.3.0" dependencies = [ "libloading", "smol", diff --git a/Cargo.toml b/Cargo.toml index fa5c574..45b62cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "hinoirisetr" -version = "0.2.0" +version = "0.3.0" authors = ["Vladimir Rubin "] description = "A daemon to dim the screen at night" license = "MIT" diff --git a/flake.nix b/flake.nix index 6e7c2db..b5905f9 100644 --- a/flake.nix +++ b/flake.nix @@ -42,7 +42,7 @@ packages.default = pkgs.rustPlatform.buildRustPackage { pname = "hinoirisetr"; - version = "0.2.0"; + version = "0.3.0"; src = ./.; diff --git a/src/lib.rs b/src/lib.rs index ba66903..8d78616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,13 @@ pub struct Config { pub sunrise_end: u8, pub notification_timeout: u32, + pub gamma_backend: GammaBackend, +} + +#[derive(Debug, Copy, Clone)] +pub enum GammaBackend { + Hyprctl, + Ddcutil, } impl Default for Config { @@ -40,6 +47,7 @@ impl Default for Config { sunrise_start: 4, sunrise_end: 7, notification_timeout: 5000, + gamma_backend: GammaBackend::Hyprctl, } } } @@ -51,6 +59,7 @@ pub enum ConfigError { InvalidTemperature(String), InvalidTime(String), InvalidGamma(String), + InvalidGammaBackend(String), } impl From for ConfigError { @@ -92,11 +101,17 @@ impl Config { let value = value.trim().replace('"', ""); match current_section.as_str() { - "" => { - if key_trimmed == "notification_timeout" { + "" => match key_trimmed { + "notification_timeout" => { config.notification_timeout = value.parse::()?; } - } + "gamma_backend" => match value.to_lowercase().as_str() { + "hyprctl" => config.gamma_backend = GammaBackend::Hyprctl, + "ddcutil" => config.gamma_backend = GammaBackend::Ddcutil, + _ => return Err(ConfigError::InvalidGammaBackend(value.to_string())), + }, + _ => {} + }, "gamma" => match key_trimmed { "day" => { let parsed = value.parse::()?; @@ -229,7 +244,7 @@ pub fn compute_settings(now: Time, config: &Config) -> (u16, u16) { } /// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands -pub fn apply_settings(temp: u16, gamma: u16) { +pub fn apply_settings(temp: u16, gamma: u16, backend: GammaBackend) { trace!("apply_settings({temp}, {gamma})"); debug!("applying temperature: {temp}"); debug!("applying gamma: {gamma}"); @@ -239,8 +254,18 @@ pub fn apply_settings(temp: u16, gamma: u16) { .output(); trace!("hyprctl hyprsunset temperature {temp}"); - let _ = Command::new("hyprctl") - .args(["hyprsunset", "gamma", &gamma.to_string()]) - .output(); - trace!("hyprctl hyprsunset gamma {gamma}"); + match backend { + GammaBackend::Hyprctl => { + let _ = Command::new("hyprctl") + .args(["hyprsunset", "gamma", &gamma.to_string()]) + .output(); + trace!("hyprctl hyprsunset gamma {gamma}"); + } + GammaBackend::Ddcutil => { + let _ = Command::new("ddcutil") + .args(["setvcp", "10", &gamma.to_string()]) + .output(); + trace!("ddcutil setvcp 10 {gamma}"); + } + } } diff --git a/src/main.rs b/src/main.rs index 85a1ad1..f1ac452 100644 --- a/src/main.rs +++ b/src/main.rs @@ -258,7 +258,7 @@ fn main() { { let now = get_time(); let (temp, gamma) = compute_settings(now, &*config_guard().await); - apply_settings(temp, gamma); + apply_settings(temp, gamma, config_guard().await.gamma_backend); trace!("initial settings applied: {temp}K, {gamma}%"); } @@ -268,11 +268,12 @@ fn main() { apply_settings( config_guard().await.temp_day, config_guard().await.gamma_day, + config_guard().await.gamma_backend, ); } else { let now = get_time(); let (temp, gamma) = compute_settings(now, &*config_guard().await); - apply_settings(temp, gamma); + apply_settings(temp, gamma, config_guard().await.gamma_backend); } let _ = notify_r.recv().await;