From 836575c0a8cd766acd1833094bcac554cb11e3ae Mon Sep 17 00:00:00 2001 From: Luke Jones Date: Sun, 6 Apr 2025 02:09:45 +1300 Subject: [PATCH] feature: screenpad settings config store --- asusd/src/config.rs | 64 +++++++++++++++++++++++++++++++++++-- asusd/src/ctrl_backlight.rs | 38 +++++++++++++--------- asusd/src/daemon.rs | 4 +-- 3 files changed, 87 insertions(+), 19 deletions(-) diff --git a/asusd/src/config.rs b/asusd/src/config.rs index 3a6b6c0d..db7f9cf2 100644 --- a/asusd/src/config.rs +++ b/asusd/src/config.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; -use config_traits::{StdConfig, StdConfigLoad1}; +use config_traits::{StdConfig, StdConfigLoad2}; use rog_platform::asus_armoury::FirmwareAttribute; use rog_platform::cpu::CPUEPP; use rog_platform::platform::PlatformProfile; @@ -49,6 +49,10 @@ pub struct Config { pub ac_profile_tunings: Tunings, pub dc_profile_tunings: Tunings, pub armoury_settings: HashMap, + #[serde(skip_serializing_if = "Option::is_none", default)] + pub screenpad_gamma: Option, + #[serde(skip_serializing_if = "Option::is_none", default)] + pub screenpad_sync_primary: Option, /// Temporary state for AC/Batt #[serde(skip)] pub last_power_plugged: u8, @@ -86,6 +90,8 @@ impl Default for Config { dc_profile_tunings: HashMap::default(), armoury_settings: HashMap::default(), last_power_plugged: Default::default(), + screenpad_gamma: Default::default(), + screenpad_sync_primary: Default::default(), } } } @@ -112,7 +118,59 @@ impl StdConfig for Config { } } -impl StdConfigLoad1 for Config {} +impl StdConfigLoad2 for Config {} + +#[derive(Deserialize, Serialize)] +pub struct Config611 { + pub charge_control_end_threshold: u8, + #[serde(skip)] + pub base_charge_control_end_threshold: u8, + pub disable_nvidia_powerd_on_battery: bool, + pub ac_command: String, + pub bat_command: String, + pub platform_profile_linked_epp: bool, + pub platform_profile_on_battery: PlatformProfile, + pub change_platform_profile_on_battery: bool, + pub platform_profile_on_ac: PlatformProfile, + pub change_platform_profile_on_ac: bool, + pub profile_quiet_epp: CPUEPP, + pub profile_balanced_epp: CPUEPP, + pub profile_custom_epp: CPUEPP, + pub profile_performance_epp: CPUEPP, + pub ac_profile_tunings: Tunings, + pub dc_profile_tunings: Tunings, + pub armoury_settings: HashMap, + #[serde(skip)] + pub last_power_plugged: u8, +} + +impl From for Config { + fn from(c: Config611) -> Self { + Self { + // Restore the base charge limit + charge_control_end_threshold: c.charge_control_end_threshold, + base_charge_control_end_threshold: c.charge_control_end_threshold, + disable_nvidia_powerd_on_battery: c.disable_nvidia_powerd_on_battery, + ac_command: c.ac_command, + bat_command: c.bat_command, + platform_profile_linked_epp: c.platform_profile_linked_epp, + platform_profile_on_battery: c.platform_profile_on_battery, + change_platform_profile_on_battery: c.change_platform_profile_on_battery, + platform_profile_on_ac: c.platform_profile_on_ac, + change_platform_profile_on_ac: c.change_platform_profile_on_ac, + profile_quiet_epp: c.profile_quiet_epp, + profile_balanced_epp: c.profile_balanced_epp, + profile_performance_epp: c.profile_performance_epp, + profile_custom_epp: c.profile_performance_epp, + last_power_plugged: c.last_power_plugged, + ac_profile_tunings: HashMap::default(), + dc_profile_tunings: HashMap::default(), + armoury_settings: HashMap::default(), + screenpad_gamma: None, + screenpad_sync_primary: Default::default(), + } + } +} #[derive(Deserialize, Serialize)] pub struct Config601 { @@ -175,6 +233,8 @@ impl From for Config { ac_profile_tunings: HashMap::default(), dc_profile_tunings: HashMap::default(), armoury_settings: HashMap::default(), + screenpad_gamma: None, + screenpad_sync_primary: Default::default(), } } } diff --git a/asusd/src/ctrl_backlight.rs b/asusd/src/ctrl_backlight.rs index 0c23e1fa..8139aed3 100644 --- a/asusd/src/ctrl_backlight.rs +++ b/asusd/src/ctrl_backlight.rs @@ -1,5 +1,6 @@ use std::sync::Arc; +use config_traits::StdConfig; use futures_util::lock::Mutex; use log::{info, warn}; use rog_platform::backlight::{Backlight, BacklightType}; @@ -7,18 +8,18 @@ use zbus::fdo::Error as FdoErr; use zbus::object_server::SignalEmitter; use zbus::{interface, Connection}; +use crate::config::Config; use crate::error::RogError; use crate::ASUS_ZBUS_PATH; #[derive(Debug, Clone)] pub struct CtrlBacklight { backlights: Vec, - sync_all: Arc>, - gamma: Arc>, + config: Arc>, } impl CtrlBacklight { - pub fn new() -> Result { + pub fn new(config: Arc>) -> Result { let mut backlights = Vec::new(); if let Ok(primary) = Backlight::new(BacklightType::Primary) { @@ -35,11 +36,7 @@ impl CtrlBacklight { return Err(RogError::MissingFunction("No backlights found".into())); } - Ok(Self { - backlights, - sync_all: Arc::new(Mutex::new(true)), - gamma: Arc::new(Mutex::new(1.0)), - }) + Ok(Self { backlights, config }) } fn get_backlight(&self, device_type: &BacklightType) -> Option<&Backlight> { @@ -59,9 +56,9 @@ impl CtrlBacklight { FdoErr::Failed(format!("Failed to get max brightness: {}", e)) })?; + let gamma = self.config.lock().await.screenpad_gamma.unwrap_or(1.0); let scaled = if *device_type == BacklightType::Screenpad { // Apply non-linear scaling with the configurable gamma value only for Screenpad - let gamma = *self.gamma.lock().await; let normalized_level = level as f32 / 100.0; let gamma_corrected = normalized_level.powf(gamma); (gamma_corrected * max as f32) as i32 @@ -75,7 +72,13 @@ impl CtrlBacklight { FdoErr::Failed(format!("Failed to set brightness: {}", e)) })?; - if *self.sync_all.lock().await { + let sync = self + .config + .lock() + .await + .screenpad_sync_primary + .unwrap_or_default(); + if sync { for other in self .backlights .iter() @@ -84,7 +87,6 @@ impl CtrlBacklight { if let Ok(other_max) = other.get_max_brightness() { let other_scaled = if other.device_type() == &BacklightType::Screenpad { // Apply gamma only to Screenpad - let gamma = *self.gamma.lock().await; let normalized_level = level as f32 / 100.0; let gamma_corrected = normalized_level.powf(gamma); (gamma_corrected * other_max as f32) as i32 @@ -132,18 +134,23 @@ impl CtrlBacklight { impl CtrlBacklight { #[zbus(property)] async fn screenpad_sync_with_primary(&self) -> bool { - *self.sync_all.lock().await + self.config + .lock() + .await + .screenpad_sync_primary + .unwrap_or_default() } #[zbus(property)] async fn set_screenpad_sync_with_primary(&self, sync: bool) -> Result<(), zbus::Error> { - *self.sync_all.lock().await = sync; + self.config.lock().await.screenpad_sync_primary = Some(sync); + self.config.lock().await.write(); Ok(()) } #[zbus(property)] async fn screenpad_gamma(&self) -> String { - (*self.gamma.lock().await).to_string() + (self.config.lock().await.screenpad_gamma.unwrap_or(1.0)).to_string() } #[zbus(property)] @@ -158,7 +165,8 @@ impl CtrlBacklight { if gamma > 2.0 { return Err(FdoErr::Failed("Gamma value must be 2.0 or less".into()).into()); } - *self.gamma.lock().await = gamma; + self.config.lock().await.screenpad_gamma = Some(gamma); + self.config.lock().await.write(); Ok(()) } diff --git a/asusd/src/daemon.rs b/asusd/src/daemon.rs index 544b86e5..5bdafbab 100644 --- a/asusd/src/daemon.rs +++ b/asusd/src/daemon.rs @@ -10,7 +10,7 @@ use asusd::ctrl_backlight::CtrlBacklight; use asusd::ctrl_fancurves::CtrlFanCurveZbus; use asusd::ctrl_platform::CtrlPlatform; use asusd::{print_board_info, start_tasks, CtrlTask, ZbusRun, DBUS_NAME}; -use config_traits::{StdConfig, StdConfigLoad1}; +use config_traits::{StdConfig, StdConfigLoad2}; use futures_util::lock::Mutex; use log::{error, info}; use rog_platform::asus_armoury::FirmwareAttributes; @@ -91,7 +91,7 @@ async fn start_daemon() -> Result<(), Box> { } } - match CtrlBacklight::new() { + match CtrlBacklight::new(config.clone()) { Ok(backlight) => { backlight.add_to_server(&mut server).await; }