feature: screenpad settings config store

This commit is contained in:
Luke Jones
2025-04-06 02:09:45 +13:00
parent 61f2216c25
commit 836575c0a8
3 changed files with 87 additions and 19 deletions

View File

@@ -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<FirmwareAttribute, i32>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub screenpad_gamma: Option<f32>,
#[serde(skip_serializing_if = "Option::is_none", default)]
pub screenpad_sync_primary: Option<bool>,
/// 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<Config601> for Config {}
impl StdConfigLoad2<Config611, Config601> 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<FirmwareAttribute, i32>,
#[serde(skip)]
pub last_power_plugged: u8,
}
impl From<Config611> 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<Config601> for Config {
ac_profile_tunings: HashMap::default(),
dc_profile_tunings: HashMap::default(),
armoury_settings: HashMap::default(),
screenpad_gamma: None,
screenpad_sync_primary: Default::default(),
}
}
}

View File

@@ -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<Backlight>,
sync_all: Arc<Mutex<bool>>,
gamma: Arc<Mutex<f32>>,
config: Arc<Mutex<Config>>,
}
impl CtrlBacklight {
pub fn new() -> Result<Self, RogError> {
pub fn new(config: Arc<Mutex<Config>>) -> Result<Self, RogError> {
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(())
}

View File

@@ -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<dyn Error>> {
}
}
match CtrlBacklight::new() {
match CtrlBacklight::new(config.clone()) {
Ok(backlight) => {
backlight.add_to_server(&mut server).await;
}