diff --git a/acs.toml b/acs.toml index 87f3cd75..8c531da0 100644 --- a/acs.toml +++ b/acs.toml @@ -1,5 +1,4 @@ # acs.toml powersave_under = 20 overheat_threshold = 80 -ignore_power = false -ignore_lid = false +active_rules = [ "battery_percent_rule", "lid_open_rule", "ac_charging_rule", "cpu_usage_rule" ] diff --git a/src/config.rs b/src/config.rs index 2f88f70b..538cff9b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,3 +1,4 @@ +use super::daemon::State; use super::warn_user; use serde::{Deserialize, Serialize}; use std::fmt; @@ -26,25 +27,30 @@ pub fn default_config() -> Config { Config { powersave_under: 20, overheat_threshold: 80, - ignore_power: false, - ignore_lid: false, + high_cpu_threshold: 50, + active_rules: vec![ + State::BatteryLow, + State::LidClosed, + State::Charging, + State::CpuUsageHigh, + ], } } -#[derive(Debug, Deserialize, Serialize)] +#[derive(Debug)] pub struct Config { pub powersave_under: i8, pub overheat_threshold: i8, - pub ignore_power: bool, - pub ignore_lid: bool, + pub high_cpu_threshold: i8, + pub active_rules: Vec, } #[derive(Debug, Deserialize, Serialize)] pub struct SafeConfig { pub powersave_under: Option, pub overheat_threshold: Option, - pub ignore_power: Option, - pub ignore_lid: Option, + pub high_cpu_threshold: Option, + pub active_rules: Option>, } trait SafeFillConfig { @@ -76,12 +82,21 @@ impl SafeFillConfig for SafeConfig { base.overheat_threshold = self.overheat_threshold.unwrap(); } - if self.ignore_power.is_some() { - base.ignore_power = self.ignore_power.unwrap(); + if self.high_cpu_threshold.is_some() { + base.high_cpu_threshold = self.high_cpu_threshold.unwrap(); } - if self.ignore_lid.is_some() { - base.ignore_lid = self.ignore_lid.unwrap(); + if self.active_rules.is_some() { + base.active_rules.clear(); + for rule in self.active_rules.clone().unwrap() { + base.active_rules.push(match rule.as_str() { + "battery_percent_rule" => State::BatteryLow, + "lid_open_rule" => State::LidClosed, + "ac_charging_rule" => State::Charging, + "cpu_usage_rule" => State::CpuUsageHigh, + _ => State::Unknown, + }); + } } return base; @@ -94,8 +109,8 @@ impl fmt::Display for Config { // config iterable. This would also make safe_fill_config a lot easier as well. write!( f, - "powersave_under = {}\noverheat_threshold = {}\nignore_power = {}\nignore_lid = {} ", - self.powersave_under, self.overheat_threshold, self.ignore_power, self.ignore_lid, + "powersave_under = {}\noverheat_threshold = {}\nhigh_cpu_threshold = {}\nacive_rules = {:?}", + self.powersave_under, self.overheat_threshold, self.high_cpu_threshold, self.active_rules, ) } } @@ -114,8 +129,8 @@ fn parse_as_toml(config: String) -> Config { toml::from_str(config.as_str()).unwrap_or_else(|_| SafeConfig { powersave_under: None, overheat_threshold: None, - ignore_power: None, - ignore_lid: None, + high_cpu_threshold: None, + active_rules: None, }); safe_config.safe_fill_config() diff --git a/src/daemon.rs b/src/daemon.rs index de0cd72b..dea263da 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -22,7 +22,7 @@ use super::Error; use crate::display::print_turbo_animation; use crate::warn_user; -#[derive(Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq)] pub enum State { Normal, BatteryLow, @@ -177,38 +177,48 @@ impl Checker for Daemon { fn run_state_machine(&mut self) -> State { let mut state = State::Normal; - if self.usage > 70.0 && self.last_below_cpu_usage_percent.is_none() { - self.last_below_cpu_usage_percent = Some(SystemTime::now()); - } + if self.config.active_rules.contains(&State::CpuUsageHigh) { + if self.usage > self.config.high_cpu_threshold.into() + && self.last_below_cpu_usage_percent.is_none() + { + self.last_below_cpu_usage_percent = Some(SystemTime::now()); + } - if self.usage <= 70.0 { - self.last_below_cpu_usage_percent = None; - } + if self.usage <= self.config.high_cpu_threshold.into() { + self.last_below_cpu_usage_percent = None; + } - match self.last_below_cpu_usage_percent { - Some(last) => { - if SystemTime::now() - .duration_since(last) - .expect("Could not compare times") - .as_secs() - >= 15 - { - state = State::CpuUsageHigh; + match self.last_below_cpu_usage_percent { + Some(last) => { + if SystemTime::now() + .duration_since(last) + .expect("Could not compare times") + .as_secs() + >= 15 + { + state = State::CpuUsageHigh; + } } + None => {} } - None => {} } - if self.lid_state == LidState::Closed { - state = State::LidClosed; + if self.config.active_rules.contains(&State::LidClosed) { + if self.lid_state == LidState::Closed { + state = State::LidClosed; + } } - if self.charging { - state = State::Charging; + if self.config.active_rules.contains(&State::Charging) { + if self.charging { + state = State::Charging; + } } - if self.charge < self.config.powersave_under { - state = State::BatteryLow; + if self.config.active_rules.contains(&State::BatteryLow) { + if self.charge < self.config.powersave_under { + state = State::BatteryLow; + } } state