From f123fe1941c822fe4da95644c90779e0a1ca7611 Mon Sep 17 00:00:00 2001 From: Vladimir Rubin Date: Sat, 3 May 2025 02:50:21 +0300 Subject: [PATCH] perf: cache last temp/gamma --- src/lib.rs | 142 +++++++++++++++++++++++++++++------------------------ 1 file changed, 79 insertions(+), 63 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 537fb43..caec01e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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,78 +278,90 @@ 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}"); - match config.temp_backend { - TempBackend::Hyprctl => { - let _ = Command::new("hyprctl") - .args(["hyprsunset", "temperature", &temp.to_string()]) - .output(); - trace!("hyprctl hyprsunset temperature {temp}"); + if temp != last_temp { + match config.temp_backend { + TempBackend::Hyprctl => { + let _ = Command::new("hyprctl") + .args(["hyprsunset", "temperature", &temp.to_string()]) + .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 => { - 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(); - }, + LAST_TEMP.store(temp, Ordering::SeqCst); } - match config.gamma_backend { - GammaBackend::Hyprctl => { - let _ = Command::new("hyprctl") - .args(["hyprsunset", "gamma", &gamma.to_string()]) - .output(); - trace!("hyprctl hyprsunset gamma {gamma}"); - } - GammaBackend::Ddcutil => { - let _ = Command::new("ddcutil") - .args(["setvcp", "10", &gamma.to_string()]) - .output(); - trace!("ddcutil setvcp 10 {gamma}"); - } - GammaBackend::Gammastep => { - let _ = Command::new("gammastep") - .args(["-O", "6500", "-g", &(&gamma/100).to_string()]) - .output(); - trace!("gammastep -O 6500 -g {gamma}"); - } - GammaBackend::Xsct => { - let output = Command::new("xsct").output().expect("xsct failed"); - let stdout = String::from_utf8_lossy(&output.stdout); - let mut ttemp = 6000; - for line in stdout.lines() { - if line.contains("temperature") { - // example: "Screen 0: temperature ~ 6000 0.598234" - let parts: Vec<&str> = line.split_whitespace().collect(); - if parts.len() >= 6 { - ttemp = parts[4].parse::().unwrap(); + if gamma != last_gamma { + match config.gamma_backend { + GammaBackend::Hyprctl => { + let _ = Command::new("hyprctl") + .args(["hyprsunset", "gamma", &gamma.to_string()]) + .output(); + trace!("hyprctl hyprsunset gamma {gamma}"); + } + GammaBackend::Ddcutil => { + let _ = Command::new("ddcutil") + .args(["setvcp", "10", &gamma.to_string()]) + .output(); + trace!("ddcutil setvcp 10 {gamma}"); + } + GammaBackend::Gammastep => { + let _ = Command::new("gammastep") + .args(["-O", "6500", "-g", &(&gamma / 100).to_string()]) + .output(); + trace!("gammastep -O 6500 -g {gamma}"); + } + GammaBackend::Xsct => { + let output = Command::new("xsct").output().expect("xsct failed"); + let stdout = String::from_utf8_lossy(&output.stdout); + let mut ttemp = 6000; + for line in stdout.lines() { + if line.contains("temperature") { + // example: "Screen 0: temperature ~ 6000 0.598234" + let parts: Vec<&str> = line.split_whitespace().collect(); + if parts.len() >= 6 { + ttemp = parts[4].parse::().unwrap(); + } } } - } - let _ = Command::new("xsct") - .args([&ttemp.to_string(), &(&gamma/100).to_string()]) - .output(); - trace!("xsct {ttemp} {gamma}"); + let _ = Command::new("xsct") + .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()]) + .output(); + trace!("redshift -O 6500 -g {gamma}"); + } + GammaBackend::None => {} } - GammaBackend::Redshift => { - let _ = Command::new("redshift") - .args(["-O", "6500", "-g", &(&gamma/100).to_string()]) - .output(); - trace!("redshift -O 6500 -g {gamma}"); - }, - GammaBackend::None => {} + LAST_GAMMA.store(gamma, Ordering::SeqCst); } }