diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..d6d7472 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,61 @@ +//! hinoirisetr library +//! Contains core logic for computing temperature and gamma and applying settings. + +use chrono::{DateTime, Local, Timelike}; +use std::process::Command; + +// Constants for colors and timings +pub const TEMP_DAY: i32 = 6500; +pub const TEMP_NIGHT: i32 = 2700; + +pub const GAMMA_DAY: i32 = 100; +pub const GAMMA_NIGHT: i32 = 95; + +pub const SUNSET_START: u32 = 19; +pub const SUNSET_END: u32 = 22; +pub const SUNRISE_START: u32 = 4; +pub const SUNRISE_END: u32 = 7; + +/// Linearly interpolate between start and end by factor [0.0, 1.0] +pub fn interpolate(start: i32, end: i32, factor: f64) -> i32 { + (start as f64 + (end - start) as f64 * factor).round() as i32 +} + +/// Compute current temperature and gamma based on provided time +pub fn compute_settings(now: DateTime) -> (i32, i32) { + let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; + + if (time_in_hours >= SUNSET_START as f64) && (time_in_hours <= SUNSET_END as f64) { + let factor = ((time_in_hours - SUNSET_START as f64) + / (SUNSET_END - SUNSET_START) as f64) + .clamp(0.0, 1.0); + ( + interpolate(TEMP_DAY, TEMP_NIGHT, factor), + interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), + ) + } else if (time_in_hours >= SUNRISE_START as f64) && (time_in_hours <= SUNRISE_END as f64) { + let factor = 1.0 + - ((time_in_hours - SUNRISE_START as f64) + / (SUNRISE_END - SUNRISE_START) as f64) + .clamp(0.0, 1.0); + ( + interpolate(TEMP_DAY, TEMP_NIGHT, factor), + interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), + ) + } else if time_in_hours > SUNSET_END as f64 || time_in_hours < SUNRISE_START as f64 { + (TEMP_NIGHT, GAMMA_NIGHT) + } else { + (TEMP_DAY, GAMMA_DAY) + } +} + +/// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands +pub fn apply_settings(temp: i32, gamma: i32) { + let _ = Command::new("hyprctl") + .args(["hyprsunset", "temperature", &temp.to_string()]) + .output(); + + let _ = Command::new("hyprctl") + .args(["hyprsunset", "gamma", &gamma.to_string()]) + .output(); +} diff --git a/src/main.rs b/src/main.rs index d7e3a80..b30ef48 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use chrono::{Local, Timelike}; -use std::process::Command; +use chrono::Local; +use hinoirisetr::{apply_settings, compute_settings}; use std::sync::{ atomic::{AtomicBool, Ordering}, Arc, @@ -12,32 +12,8 @@ use tokio::{ time::sleep, }; -const TEMP_DAY: i32 = 6500; -const TEMP_NIGHT: i32 = 2700; - -const GAMMA_DAY: i32 = 100; -const GAMMA_NIGHT: i32 = 95; - -const SUNSET_START: u32 = 19; -const SUNSET_END: u32 = 22; -const SUNRISE_START: u32 = 4; -const SUNRISE_END: u32 = 7; - const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock"; -fn interpolate(start: i32, end: i32, factor: f64) -> i32 { - (start as f64 + (end - start) as f64 * factor).round() as i32 -} - -fn apply_settings(temp: i32, gamma: i32) { - let _ = Command::new("hyprctl") - .args(["hyprsunset", "temperature", &temp.to_string()]) - .output(); - - let _ = Command::new("hyprctl") - .args(["hyprsunset", "gamma", &gamma.to_string()]) - .output(); -} async fn socket_server(disabled: Arc, notify: Arc) { let _ = std::fs::remove_file(SOCKET_PATH); @@ -65,36 +41,7 @@ async fn socket_server(disabled: Arc, notify: Arc) { "status" => { // compute current temp/gamma let now = Local::now(); - let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; - - let (cur_temp, cur_gamma) = if (time_in_hours >= SUNSET_START as f64) - && (time_in_hours <= SUNSET_END as f64) - { - let factor = ((time_in_hours - SUNSET_START as f64) - / (SUNSET_END - SUNSET_START) as f64) - .clamp(0.0, 1.0); - ( - interpolate(TEMP_DAY, TEMP_NIGHT, factor), - interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), - ) - } else if (time_in_hours >= SUNRISE_START as f64) - && (time_in_hours <= SUNRISE_END as f64) - { - let factor = 1.0 - - ((time_in_hours - SUNRISE_START as f64) - / (SUNRISE_END - SUNRISE_START) as f64) - .clamp(0.0, 1.0); - ( - interpolate(TEMP_DAY, TEMP_NIGHT, factor), - interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), - ) - } else if time_in_hours > SUNSET_END as f64 - || time_in_hours < SUNRISE_START as f64 - { - (TEMP_NIGHT, GAMMA_NIGHT) - } else { - (TEMP_DAY, GAMMA_DAY) - }; + let (cur_temp, cur_gamma) = compute_settings(now); println!( "dimming is {} — temp: {}K, gamma: {}%", @@ -129,41 +76,10 @@ async fn main() { loop { if disabled.load(Ordering::SeqCst) { - apply_settings(TEMP_DAY, GAMMA_DAY); + apply_settings(hinoirisetr::TEMP_DAY, hinoirisetr::GAMMA_DAY); } else { let now = Local::now(); - let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; - - let (temp, gamma) = if (time_in_hours >= SUNSET_START as f64) - && (time_in_hours <= SUNSET_END as f64) - { - // sunset transition - let factor = ((time_in_hours - SUNSET_START as f64) - / (SUNSET_END - SUNSET_START) as f64) - .clamp(0.0, 1.0); - ( - interpolate(TEMP_DAY, TEMP_NIGHT, factor), - interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), - ) - } else if (time_in_hours >= SUNRISE_START as f64) - && (time_in_hours <= SUNRISE_END as f64) - { - // sunrise transition - let factor = 1.0 - - ((time_in_hours - SUNRISE_START as f64) - / (SUNRISE_END - SUNRISE_START) as f64) - .clamp(0.0, 1.0); - ( - interpolate(TEMP_DAY, TEMP_NIGHT, factor), - interpolate(GAMMA_DAY, GAMMA_NIGHT, factor), - ) - } else if time_in_hours > SUNSET_END as f64 || time_in_hours < SUNRISE_START as f64 { - // full night - (TEMP_NIGHT, GAMMA_NIGHT) - } else { - // full day - (TEMP_DAY, GAMMA_DAY) - }; + let (temp, gamma) = compute_settings(now); apply_settings(temp, gamma); }