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,78 +278,90 @@ 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}");
match config.temp_backend { if temp != last_temp {
TempBackend::Hyprctl => { match config.temp_backend {
let _ = Command::new("hyprctl") TempBackend::Hyprctl => {
.args(["hyprsunset", "temperature", &temp.to_string()]) let _ = Command::new("hyprctl")
.output(); .args(["hyprsunset", "temperature", &temp.to_string()])
trace!("hyprctl hyprsunset temperature {temp}"); .output();
trace!("hyprctl hyprsunset temperature {temp}");
}
TempBackend::Redshift => {
let _ = Command::new("redshift")
.args(["-o", "-t", &temp.to_string()])
.output();
trace!("redshift -o -t {temp}");
}
TempBackend::Xsct => {
let _ = Command::new("xsct").args([&temp.to_string()]).output();
trace!("xsct {temp}");
}
TempBackend::None => {}
TempBackend::Gammastep => {
let _ = Command::new("gammastep")
.args(["-O", &temp.to_string()])
.output();
}
} }
TempBackend::Redshift => { LAST_TEMP.store(temp, Ordering::SeqCst);
let _ = Command::new("redshift")
.args(["-o", "-t", &temp.to_string()])
.output();
trace!("redshift -o -t {temp}");
}
TempBackend::Xsct => {
let _ = Command::new("xsct").args([&temp.to_string()]).output();
trace!("xsct {temp}");
}
TempBackend::None => {}
TempBackend::Gammastep => {
let _ = Command::new("gammastep")
.args(["-O", &temp.to_string()])
.output();
},
} }
match config.gamma_backend { if gamma != last_gamma {
GammaBackend::Hyprctl => { match config.gamma_backend {
let _ = Command::new("hyprctl") GammaBackend::Hyprctl => {
.args(["hyprsunset", "gamma", &gamma.to_string()]) let _ = Command::new("hyprctl")
.output(); .args(["hyprsunset", "gamma", &gamma.to_string()])
trace!("hyprctl hyprsunset gamma {gamma}"); .output();
} trace!("hyprctl hyprsunset gamma {gamma}");
GammaBackend::Ddcutil => { }
let _ = Command::new("ddcutil") GammaBackend::Ddcutil => {
.args(["setvcp", "10", &gamma.to_string()]) let _ = Command::new("ddcutil")
.output(); .args(["setvcp", "10", &gamma.to_string()])
trace!("ddcutil setvcp 10 {gamma}"); .output();
} trace!("ddcutil setvcp 10 {gamma}");
GammaBackend::Gammastep => { }
let _ = Command::new("gammastep") GammaBackend::Gammastep => {
.args(["-O", "6500", "-g", &(&gamma/100).to_string()]) let _ = Command::new("gammastep")
.output(); .args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
trace!("gammastep -O 6500 -g {gamma}"); .output();
} trace!("gammastep -O 6500 -g {gamma}");
GammaBackend::Xsct => { }
let output = Command::new("xsct").output().expect("xsct failed"); GammaBackend::Xsct => {
let stdout = String::from_utf8_lossy(&output.stdout); let output = Command::new("xsct").output().expect("xsct failed");
let mut ttemp = 6000; let stdout = String::from_utf8_lossy(&output.stdout);
for line in stdout.lines() { let mut ttemp = 6000;
if line.contains("temperature") { for line in stdout.lines() {
// example: "Screen 0: temperature ~ 6000 0.598234" if line.contains("temperature") {
let parts: Vec<&str> = line.split_whitespace().collect(); // example: "Screen 0: temperature ~ 6000 0.598234"
if parts.len() >= 6 { let parts: Vec<&str> = line.split_whitespace().collect();
ttemp = parts[4].parse::<u16>().unwrap(); if parts.len() >= 6 {
ttemp = parts[4].parse::<u16>().unwrap();
}
} }
} }
}
let _ = Command::new("xsct") let _ = Command::new("xsct")
.args([&ttemp.to_string(), &(&gamma/100).to_string()]) .args([&ttemp.to_string(), &(&gamma / 100).to_string()])
.output(); .output();
trace!("xsct {ttemp} {gamma}"); trace!("xsct {ttemp} {gamma}");
}
GammaBackend::Redshift => {
let _ = Command::new("redshift")
.args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
.output();
trace!("redshift -O 6500 -g {gamma}");
}
GammaBackend::None => {}
} }
GammaBackend::Redshift => { LAST_GAMMA.store(gamma, Ordering::SeqCst);
let _ = Command::new("redshift")
.args(["-O", "6500", "-g", &(&gamma/100).to_string()])
.output();
trace!("redshift -O 6500 -g {gamma}");
},
GammaBackend::None => {}
} }
} }