perf: cache last temp/gamma
This commit is contained in:
parent
beb7f13932
commit
f123fe1941
1 changed files with 79 additions and 63 deletions
20
src/lib.rs
20
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,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")
|
||||
|
@ -345,7 +359,9 @@ pub fn apply_settings(temp: u16, gamma: u16, config: &Config) {
|
|||
.args(["-O", "6500", "-g", &(&gamma / 100).to_string()])
|
||||
.output();
|
||||
trace!("redshift -O 6500 -g {gamma}");
|
||||
},
|
||||
}
|
||||
GammaBackend::None => {}
|
||||
}
|
||||
LAST_GAMMA.store(gamma, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue