perf: cache last temp/gamma

This commit is contained in:
Vladimir Rubin 2025-05-03 02:50:21 +03:00
parent beb7f13932
commit f123fe1941
Signed by: vavakado
GPG key ID: CAB744727F36B524

View file

@ -5,6 +5,7 @@ use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::Path;
use std::process::Command;
use std::sync::atomic::{AtomicU16, Ordering};
use time::Time;
@ -30,6 +31,9 @@ pub struct Config {
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,
@ -274,9 +278,16 @@ 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, config: &Config) {
trace!("apply_settings({temp}, {gamma})");
let last_temp = LAST_TEMP.load(Ordering::SeqCst);
let last_gamma = LAST_GAMMA.load(Ordering::SeqCst);
if last_temp == temp && last_gamma == gamma {
trace!("Settings unchanged, skipping application");
return;
}
debug!("applying temperature: {temp}");
debug!("applying gamma: {gamma}");
if temp != last_temp {
match config.temp_backend {
TempBackend::Hyprctl => {
let _ = Command::new("hyprctl")
@ -299,9 +310,12 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
let _ = Command::new("gammastep")
.args(["-O", &temp.to_string()])
.output();
},
}
}
LAST_TEMP.store(temp, Ordering::SeqCst);
}
if gamma != last_gamma {
match config.gamma_backend {
GammaBackend::Hyprctl => {
let _ = Command::new("hyprctl")
@ -317,7 +331,7 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
}
GammaBackend::Gammastep => {
let _ = Command::new("gammastep")
.args(["-O", "6500", "-g", &(&gamma/100).to_string()])
.args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
.output();
trace!("gammastep -O 6500 -g {gamma}");
}
@ -336,16 +350,18 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
}
let _ = Command::new("xsct")
.args([&ttemp.to_string(), &(&gamma/100).to_string()])
.args([&ttemp.to_string(), &(&gamma / 100).to_string()])
.output();
trace!("xsct {ttemp} {gamma}");
}
GammaBackend::Redshift => {
let _ = Command::new("redshift")
.args(["-O", "6500", "-g", &(&gamma/100).to_string()])
.args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
.output();
trace!("redshift -O 6500 -g {gamma}");
},
}
GammaBackend::None => {}
}
LAST_GAMMA.store(gamma, Ordering::SeqCst);
}
}