From cb88c9f0e287b63be3390711656b7e7b73dd816b Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Sun, 25 Jun 2023 20:43:43 +1200 Subject: [PATCH] Fix: prevent multiple notifications from profile change --- CHANGELOG.md | 1 + asusd/src/ctrl_profiles/controller.rs | 30 ++++++-------------------- asusd/src/ctrl_profiles/trait_impls.rs | 27 +++++++++++++---------- rog-profiles/src/lib.rs | 9 ++++++++ 4 files changed, 33 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a8b9a88..54e2b31f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Move FX506LH to FX506L in arua DB to catch full series of this range - Remove notification handle tracking limit, fixes KDE issue with profile notif - Rename daemon and daemon-user crates to asusd and asusd-user to not be confusing in workspace naming +- Prevent the multiple notifications from a profile change from occuring (too many functions with side effects!) ### BREAKING - All Anime related DBUS methods/notifs are changed diff --git a/asusd/src/ctrl_profiles/controller.rs b/asusd/src/ctrl_profiles/controller.rs index b6752bc1..381d6adb 100644 --- a/asusd/src/ctrl_profiles/controller.rs +++ b/asusd/src/ctrl_profiles/controller.rs @@ -101,7 +101,13 @@ impl CtrlPlatformProfile { // For each profile we need to switch to it before we // can read the existing values from hardware. The ACPI method used // for this is what limits us. - controller.set_next_profile()?; + let next = + Profile::get_next_profile(controller.profile_config.active_profile); + Profile::set_profile(next) + .map_err(|e| warn!("{MOD_NAME}: set_profile, {}", e)) + .ok(); + controller.profile_config.active_profile = next; + // Make sure to set the baseline to default controller.set_active_curve_to_defaults()?; let active = Profile::get_active_profile().unwrap_or(Profile::Balanced); @@ -141,28 +147,6 @@ impl CtrlPlatformProfile { } } - /// Toggle to next profile in list. This will first read the config, switch, - /// then write out - pub(super) fn set_next_profile(&mut self) -> Result<(), RogError> { - // Read first just incase the user has modified the config before calling this - match self.profile_config.active_profile { - Profile::Balanced => { - Profile::set_profile(Profile::Performance)?; - self.profile_config.active_profile = Profile::Performance; - } - Profile::Performance => { - Profile::set_profile(Profile::Quiet)?; - self.profile_config.active_profile = Profile::Quiet; - } - Profile::Quiet => { - Profile::set_profile(Profile::Balanced)?; - self.profile_config.active_profile = Profile::Balanced; - } - } - self.write_profile_curve_to_platform()?; - Ok(()) - } - /// Set the curve for the active profile active pub(super) fn write_profile_curve_to_platform(&mut self) -> Result<(), RogError> { if let Some(curves) = &mut self.fan_curves { diff --git a/asusd/src/ctrl_profiles/trait_impls.rs b/asusd/src/ctrl_profiles/trait_impls.rs index 76b5c41c..9f26fd72 100644 --- a/asusd/src/ctrl_profiles/trait_impls.rs +++ b/asusd/src/ctrl_profiles/trait_impls.rs @@ -40,8 +40,11 @@ impl ProfileZbus { /// If fan-curves are supported will also activate a fan curve for profile. async fn next_profile(&mut self, #[zbus(signal_context)] ctxt: SignalContext<'_>) { let mut ctrl = self.0.lock().await; - ctrl.set_next_profile() - .unwrap_or_else(|err| warn!("{MOD_NAME}: {}", err)); + let next = Profile::get_next_profile(ctrl.profile_config.active_profile); + Profile::set_profile(next) + .map_err(|e| warn!("{MOD_NAME}: set_profile, {}", e)) + .ok(); + ctrl.profile_config.active_profile = next; ctrl.save_config(); Self::notify_profile(&ctxt, ctrl.profile_config.active_profile) @@ -236,10 +239,11 @@ impl CtrlTask for ProfileZbus { error!("Profile::set_profile() error: {e}"); }) .ok(); + + Self::notify_profile(&sig_ctx, lock.profile_config.active_profile) + .await + .ok(); } - Self::notify_profile(&sig_ctx, lock.profile_config.active_profile) - .await - .ok(); } }) .await; @@ -271,13 +275,14 @@ impl CtrlTask for ProfileZbus { error!("Profile::set_profile() error: {e}"); }) .ok(); + + Self::notify_profile( + &signal_ctxt, + lock.profile_config.active_profile, + ) + .await + .ok(); } - Self::notify_profile( - &signal_ctxt, - lock.profile_config.active_profile, - ) - .await - .ok(); } } }) diff --git a/rog-profiles/src/lib.rs b/rog-profiles/src/lib.rs index e7242fd7..df663c14 100644 --- a/rog-profiles/src/lib.rs +++ b/rog-profiles/src/lib.rs @@ -71,6 +71,15 @@ impl Profile { _ => Self::Balanced, } } + + pub fn get_next_profile(current: Profile) -> Profile { + // Read first just incase the user has modified the config before calling this + match current { + Profile::Balanced => Profile::Performance, + Profile::Performance => Profile::Quiet, + Profile::Quiet => Profile::Balanced, + } + } } impl Default for Profile {