From 3a900f23fe8efb75420fa9916ec9452efbf965f3 Mon Sep 17 00:00:00 2001 From: Luke Jones Date: Mon, 3 Mar 2025 20:19:56 +1300 Subject: [PATCH] ROGCC: better handling of fan curve profile --- rog-control-center/src/error.rs | 8 ++ rog-control-center/src/ui/setup_fans.rs | 183 ++++++++++++++---------- 2 files changed, 113 insertions(+), 78 deletions(-) diff --git a/rog-control-center/src/error.rs b/rog-control-center/src/error.rs index 951d0b0f..b658cc2c 100644 --- a/rog-control-center/src/error.rs +++ b/rog-control-center/src/error.rs @@ -9,6 +9,7 @@ pub enum Error { ConfigLockFail, XdgVars, Zbus(zbus::Error), + ZbusFdo(zbus::fdo::Error), Notification(notify_rust::error::Error), } @@ -21,6 +22,7 @@ impl fmt::Display for Error { Error::ConfigLockFail => write!(f, "Failed to lock user config"), Error::XdgVars => write!(f, "XDG environment vars appear unset"), Error::Zbus(err) => write!(f, "Error: {}", err), + Error::ZbusFdo(err) => write!(f, "Error: {}", err), Error::Notification(err) => write!(f, "Notification Error: {}", err), } } @@ -40,6 +42,12 @@ impl From for Error { } } +impl From for Error { + fn from(err: zbus::fdo::Error) -> Self { + Error::ZbusFdo(err) + } +} + impl From for Error { fn from(err: notify_rust::error::Error) -> Self { Error::Notification(err) diff --git a/rog-control-center/src/ui/setup_fans.rs b/rog-control-center/src/ui/setup_fans.rs index 0b72461b..4ff607fe 100644 --- a/rog-control-center/src/ui/setup_fans.rs +++ b/rog-control-center/src/ui/setup_fans.rs @@ -1,7 +1,8 @@ use std::sync::{Arc, Mutex}; -use log::{error, info}; +use log::error; use rog_dbus::zbus_fan_curves::FanCurvesProxy; +use rog_dbus::zbus_platform::PlatformProxy; use rog_platform::platform::PlatformProfile; use rog_profiles::fan_curve_set::CurveData; use slint::{ComponentHandle, Model, Weak}; @@ -100,96 +101,122 @@ pub fn setup_fan_curve_page(ui: &MainWindow, _config: Arc>) { tokio::spawn(async move { // Create the connections/proxies here to prevent future delays in process - let conn = if let Ok(conn) = zbus::Connection::system().await.map_err(|e| error!("{e:}")) { - conn - } else { - return; + let conn = match zbus::Connection::system().await { + Ok(conn) => conn, + Err(e) => { + error!("{e:}"); + return; + } }; - let fans = if let Ok(fans) = FanCurvesProxy::new(&conn).await.map_err(|e| error!("{e:}")) { - fans - } else { - info!( - "This device may not have an Fan Curve control. If not then the error can be \ - ignored" - ); - return; + + let fans = match FanCurvesProxy::new(&conn).await { + Ok(fans) => fans, + Err(e) => { + error!("{e:}"); + return; + } + }; + + let platform = match PlatformProxy::new(&conn).await { + Ok(platform) => platform, + Err(e) => { + error!("{e:}"); + return; + } + }; + + let platform_profile_choices = match platform.platform_profile_choices().await { + Ok(choices) => choices, + Err(e) => { + error!("{e:}"); + return; + } }; let handle_copy = handle.clone(); // Do initial setup - let Ok(balanced) = fans - .fan_curve_data(PlatformProfile::Balanced) - .await - .map_err(|e| error!("Couldn't get balanced data: {e:}")) - else { - return; + let balanced = match fans.fan_curve_data(PlatformProfile::Balanced).await { + Ok(data) => data, + Err(e) => { + error!("Couldn't get balanced data: {e:}"); + return; + } }; - let Ok(perf) = fans - .fan_curve_data(PlatformProfile::Performance) - .await - .map_err(|e| error!("Couldn't get performance data: {e:}")) - else { - return; + + let perf = match fans.fan_curve_data(PlatformProfile::Performance).await { + Ok(data) => data, + Err(e) => { + error!("Couldn't get performance data: {e:}"); + return; + } }; - let Ok(quiet) = fans - .fan_curve_data(PlatformProfile::Quiet) - .await - .map_err(|e| error!("Couldn't get quiet data: {e:}")) - else { - return; + + // TODO: the fan curve stuff was written donkeys ago with the expectation that + // only 3 profiles existed + let profile = if platform_profile_choices.contains(&PlatformProfile::Quiet) { + PlatformProfile::Quiet + } else { + PlatformProfile::Quiet }; + let quiet = match fans.fan_curve_data(profile).await { + Ok(data) => data, + Err(e) => { + error!("Couldn't get quiet data: {e:}"); + return; + } + }; + update_fan_data(handle, balanced, perf, quiet); let handle_next1 = handle_copy.clone(); - handle_copy - .upgrade_in_event_loop(move |handle| { - let global = handle.global::(); - let fans1 = fans.clone(); - global.on_set_profile_default(move |profile| { - let fans = fans1.clone(); - let handle_next = handle_next1.clone(); - tokio::spawn(async move { - if fans.set_curves_to_defaults(profile.into()).await.is_err() { - return; - } - let Ok(balanced) = fans - .fan_curve_data(PlatformProfile::Balanced) - .await - .map_err(|e| error!("{e:}")) - else { - return; - }; - let Ok(perf) = fans - .fan_curve_data(PlatformProfile::Performance) - .await - .map_err(|e| error!("{e:}")) - else { - return; - }; - let Ok(quiet) = fans - .fan_curve_data(PlatformProfile::Quiet) - .await - .map_err(|e| error!("{e:}")) - else { - return; - }; - update_fan_data(handle_next, balanced, perf, quiet); - }); + if let Err(e) = handle_copy.upgrade_in_event_loop(move |handle| { + let global = handle.global::(); + let fans1 = fans.clone(); + global.on_set_profile_default(move |profile| { + let fans = fans1.clone(); + let handle_next = handle_next1.clone(); + tokio::spawn(async move { + if fans.set_curves_to_defaults(profile.into()).await.is_err() { + return; + } + let Ok(balanced) = fans + .fan_curve_data(PlatformProfile::Balanced) + .await + .map_err(|e| error!("{e:}")) + else { + return; + }; + let Ok(perf) = fans + .fan_curve_data(PlatformProfile::Performance) + .await + .map_err(|e| error!("{e:}")) + else { + return; + }; + let Ok(quiet) = fans + .fan_curve_data(PlatformProfile::Quiet) + .await + .map_err(|e| error!("{e:}")) + else { + return; + }; + update_fan_data(handle_next, balanced, perf, quiet); }); - global.on_set_fan_data(move |fan, profile, enabled, data| { - let fans = fans.clone(); - let data: Vec = data.iter().collect(); - let data = fan_data_for(fan, enabled, data); - tokio::spawn(async move { - fans.set_fan_curve(profile.into(), data) - .await - .map_err(|e| error!("{e:}")) - .ok() - }); + }); + global.on_set_fan_data(move |fan, profile, enabled, data| { + let fans = fans.clone(); + let data: Vec = data.iter().collect(); + let data = fan_data_for(fan, enabled, data); + tokio::spawn(async move { + fans.set_fan_curve(profile.into(), data) + .await + .map_err(|e| error!("{e:}")) + .ok() }); - }) - .map_err(|e| error!("setup_fan_curve_page: upgrade_in_event_loop: {e:?}")) - .ok(); + }); + }) { + error!("setup_fan_curve_page: upgrade_in_event_loop: {e:?}"); + } }); }