diff --git a/src/manager/user.rs b/src/manager/user.rs index 4663c26..ed4aa1a 100644 --- a/src/manager/user.rs +++ b/src/manager/user.rs @@ -146,6 +146,7 @@ struct Manager2 { struct PerformanceProfile1 { proxy: Proxy<'static>, + tdp_limit_manager: Option>, } struct Storage1 { @@ -492,8 +493,29 @@ impl PerformanceProfile1 { } #[zbus(property)] - async fn set_performance_profile(&self, profile: &str) -> zbus::Result<()> { - self.proxy.call("SetPerformanceProfile", &(profile)).await + async fn set_performance_profile( + &self, + profile: &str, + #[zbus(connection)] connection: &Connection, + ) -> zbus::Result<()> { + let _: () = self.proxy.call("SetPerformanceProfile", &(profile)).await?; + if self.tdp_limit_manager.is_some() { + let connection = connection.clone(); + let manager = tdp_limit_manager().await.map_err(to_zbus_error)?; + let proxy = self.proxy.clone(); + tokio::spawn(async move { + if manager.is_active().await.map_err(to_zbus_error)? { + let tdp_limit = TdpLimit1 { proxy, manager }; + connection.object_server().at(MANAGER_PATH, tdp_limit).await + } else { + connection + .object_server() + .remove::(MANAGER_PATH) + .await + } + }); + } + Ok(()) } #[zbus(property(emits_changed_signal = "const"))] @@ -654,6 +676,7 @@ async fn create_config_interfaces( }; let performance_profile = PerformanceProfile1 { proxy: proxy.clone(), + tdp_limit_manager: tdp_limit_manager().await.ok(), }; let storage = Storage1 { proxy: proxy.clone(), @@ -676,6 +699,20 @@ async fn create_config_interfaces( object_server.at(MANAGER_PATH, fan_control).await?; } + if let Ok(manager) = tdp_limit_manager().await { + if manager.is_active().await? { + object_server + .at( + MANAGER_PATH, + TdpLimit1 { + proxy: proxy.clone(), + manager, + }, + ) + .await?; + } + } + if let Some(ref config) = config.performance_profile { if !get_available_platform_profiles(&config.platform_profile_name) .await @@ -802,18 +839,6 @@ pub(crate) async fn create_interfaces( object_server.at(MANAGER_PATH, manager2).await?; - if let Ok(manager) = tdp_limit_manager().await { - object_server - .at( - MANAGER_PATH, - TdpLimit1 { - proxy: proxy.clone(), - manager, - }, - ) - .await?; - } - if steam_deck_variant().await.unwrap_or_default() == SteamDeckVariant::Galileo { object_server.at(MANAGER_PATH, wifi_debug).await?; } diff --git a/src/power.rs b/src/power.rs index bfef6af..c8655da 100644 --- a/src/power.rs +++ b/src/power.rs @@ -107,6 +107,9 @@ pub(crate) trait TdpLimitManager: Send + Sync { async fn get_tdp_limit(&self) -> Result; async fn set_tdp_limit(&self, limit: u32) -> Result<()>; async fn get_tdp_limit_range(&self) -> Result>; + async fn is_active(&self) -> Result { + Ok(true) + } } pub(crate) async fn tdp_limit_manager() -> Result> { @@ -531,6 +534,18 @@ impl TdpLimitManager for LenovoWmiTdpLimitManager { .map_err(|e| anyhow!("Error parsing value: {e}"))?; Ok(min..=max) } + + async fn is_active(&self) -> Result { + let config = platform_config().await?; + if let Some(config) = config + .as_ref() + .and_then(|config| config.performance_profile.as_ref()) + { + Ok(get_platform_profile(&config.platform_profile_name).await? == "custom") + } else { + Ok(true) + } + } } pub(crate) async fn get_max_charge_level() -> Result {