refactor: move core logic to lib.rs

This commit is contained in:
Vladimir Rubin 2025-04-23 22:22:40 +03:00
parent 42f3eb66d0
commit ecd67419b8
Signed by: vavakado
GPG key ID: CAB744727F36B524
2 changed files with 66 additions and 89 deletions

61
src/lib.rs Normal file
View file

@ -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<Local>) -> (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();
}

View file

@ -1,5 +1,5 @@
use chrono::{Local, Timelike}; use chrono::Local;
use std::process::Command; use hinoirisetr::{apply_settings, compute_settings};
use std::sync::{ use std::sync::{
atomic::{AtomicBool, Ordering}, atomic::{AtomicBool, Ordering},
Arc, Arc,
@ -12,32 +12,8 @@ use tokio::{
time::sleep, 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"; 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<AtomicBool>, notify: Arc<Notify>) { async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
let _ = std::fs::remove_file(SOCKET_PATH); let _ = std::fs::remove_file(SOCKET_PATH);
@ -65,36 +41,7 @@ async fn socket_server(disabled: Arc<AtomicBool>, notify: Arc<Notify>) {
"status" => { "status" => {
// compute current temp/gamma // compute current temp/gamma
let now = Local::now(); let now = Local::now();
let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; let (cur_temp, cur_gamma) = compute_settings(now);
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)
};
println!( println!(
"dimming is {} — temp: {}K, gamma: {}%", "dimming is {} — temp: {}K, gamma: {}%",
@ -129,41 +76,10 @@ async fn main() {
loop { loop {
if disabled.load(Ordering::SeqCst) { if disabled.load(Ordering::SeqCst) {
apply_settings(TEMP_DAY, GAMMA_DAY); apply_settings(hinoirisetr::TEMP_DAY, hinoirisetr::GAMMA_DAY);
} else { } else {
let now = Local::now(); let now = Local::now();
let time_in_hours = now.hour() as f64 + now.minute() as f64 / 60.0; let (temp, gamma) = compute_settings(now);
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)
};
apply_settings(temp, gamma); apply_settings(temp, gamma);
} }