mirror of
https://github.com/RGBCube/superfreq
synced 2025-07-28 01:17:45 +00:00
battery: simplify file permission check and improve threshold inheritance
This commit is contained in:
parent
d8f609fdef
commit
eb97689bc7
4 changed files with 19 additions and 21 deletions
|
@ -168,10 +168,7 @@ fn path_exists_and_writable(path: &Path) -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to open the file with write access to verify write permission
|
// Try to open the file with write access to verify write permission
|
||||||
match fs::OpenOptions::new().write(true).open(path) {
|
fs::OpenOptions::new().write(true).open(path).is_ok()
|
||||||
Ok(_) => true,
|
|
||||||
Err(_) => false,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifies if a battery supports threshold control and which pattern it uses
|
/// Identifies if a battery supports threshold control and which pattern it uses
|
||||||
|
|
|
@ -84,14 +84,17 @@ fn load_and_parse_config(path: &Path) -> Result<AppConfig, ConfigError> {
|
||||||
let mut charger_profile = toml_app_config.charger.clone();
|
let mut charger_profile = toml_app_config.charger.clone();
|
||||||
let mut battery_profile = toml_app_config.battery.clone();
|
let mut battery_profile = toml_app_config.battery.clone();
|
||||||
|
|
||||||
// If profile-specific battery thresholds are not set, inherit from global config
|
// Clone global battery_charge_thresholds once if it exists
|
||||||
if charger_profile.battery_charge_thresholds.is_none() {
|
if let Some(global_thresholds) = toml_app_config.battery_charge_thresholds {
|
||||||
charger_profile.battery_charge_thresholds =
|
// Apply to charger profile if not already set
|
||||||
toml_app_config.battery_charge_thresholds.clone();
|
if charger_profile.battery_charge_thresholds.is_none() {
|
||||||
}
|
charger_profile.battery_charge_thresholds = Some(global_thresholds.clone());
|
||||||
|
}
|
||||||
|
|
||||||
if battery_profile.battery_charge_thresholds.is_none() {
|
// Apply to battery profile if not already set
|
||||||
battery_profile.battery_charge_thresholds = toml_app_config.battery_charge_thresholds;
|
if battery_profile.battery_charge_thresholds.is_none() {
|
||||||
|
battery_profile.battery_charge_thresholds = Some(global_thresholds);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert AppConfigToml to AppConfig
|
// Convert AppConfigToml to AppConfig
|
||||||
|
|
|
@ -257,12 +257,10 @@ fn validate_epb_value(epb: &str) -> Result<()> {
|
||||||
if let Ok(value) = epb.parse::<u8>() {
|
if let Ok(value) = epb.parse::<u8>() {
|
||||||
if value <= 15 {
|
if value <= 15 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else {
|
|
||||||
return Err(ControlError::InvalidValueError(format!(
|
|
||||||
"EPB numeric value must be between 0 and 15, got {}",
|
|
||||||
value
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
|
return Err(ControlError::InvalidValueError(format!(
|
||||||
|
"EPB numeric value must be between 0 and 15, got {value}"
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If not a number, check if it's a recognized string value
|
// If not a number, check if it's a recognized string value
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -396,7 +396,11 @@ fn main() {
|
||||||
// Get available platform profiles and validate early if possible
|
// Get available platform profiles and validate early if possible
|
||||||
match cpu::get_platform_profiles() {
|
match cpu::get_platform_profiles() {
|
||||||
Ok(available_profiles) => {
|
Ok(available_profiles) => {
|
||||||
if !available_profiles.contains(&profile) {
|
if available_profiles.contains(&profile) {
|
||||||
|
info!("Setting platform profile to '{profile}'");
|
||||||
|
cpu::set_platform_profile(&profile)
|
||||||
|
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
|
||||||
|
} else {
|
||||||
error!(
|
error!(
|
||||||
"Invalid platform profile: '{}'. Available profiles: {}",
|
"Invalid platform profile: '{}'. Available profiles: {}",
|
||||||
profile,
|
profile,
|
||||||
|
@ -407,10 +411,6 @@ fn main() {
|
||||||
profile,
|
profile,
|
||||||
available_profiles.join(", ")
|
available_profiles.join(", ")
|
||||||
))) as Box<dyn std::error::Error>)
|
))) as Box<dyn std::error::Error>)
|
||||||
} else {
|
|
||||||
info!("Setting platform profile to '{}'", profile);
|
|
||||||
cpu::set_platform_profile(&profile)
|
|
||||||
.map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue