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::io::{BufRead, BufReader};
use std::path::Path; use std::path::Path;
use std::process::Command; use std::process::Command;
use std::sync::atomic::{AtomicU16, Ordering};
use time::Time; use time::Time;
@ -30,6 +31,9 @@ pub struct Config {
pub temp_backend: TempBackend, pub temp_backend: TempBackend,
} }
static LAST_TEMP: AtomicU16 = AtomicU16::new(0);
static LAST_GAMMA: AtomicU16 = AtomicU16::new(0);
#[derive(Debug, PartialEq, Copy, Clone)] #[derive(Debug, PartialEq, Copy, Clone)]
pub enum GammaBackend { pub enum GammaBackend {
Hyprctl, Hyprctl,
@ -274,9 +278,16 @@ pub fn compute_settings(now: Time, config: &Config) -> (u16, u16) {
/// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands /// Apply given temperature (Kelvin) and gamma (%) via hyprctl commands
pub fn apply_settings(temp: u16, gamma: u16, config: &Config) { pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
trace!("apply_settings({temp}, {gamma})"); 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 temperature: {temp}");
debug!("applying gamma: {gamma}"); debug!("applying gamma: {gamma}");
if temp != last_temp {
match config.temp_backend { match config.temp_backend {
TempBackend::Hyprctl => { TempBackend::Hyprctl => {
let _ = Command::new("hyprctl") let _ = Command::new("hyprctl")
@ -299,9 +310,12 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
let _ = Command::new("gammastep") let _ = Command::new("gammastep")
.args(["-O", &temp.to_string()]) .args(["-O", &temp.to_string()])
.output(); .output();
}, }
}
LAST_TEMP.store(temp, Ordering::SeqCst);
} }
if gamma != last_gamma {
match config.gamma_backend { match config.gamma_backend {
GammaBackend::Hyprctl => { GammaBackend::Hyprctl => {
let _ = Command::new("hyprctl") let _ = Command::new("hyprctl")
@ -345,7 +359,9 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
.args(["-O", "6500", "-g", &(&gamma / 100).to_string()]) .args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
.output(); .output();
trace!("redshift -O 6500 -g {gamma}"); trace!("redshift -O 6500 -g {gamma}");
}, }
GammaBackend::None => {} GammaBackend::None => {}
} }
LAST_GAMMA.store(gamma, Ordering::SeqCst);
}
} }