ROGCC: better handling of fan curve profile

This commit is contained in:
Luke Jones
2025-03-03 20:19:56 +13:00
parent aee465aced
commit 3a900f23fe
2 changed files with 113 additions and 78 deletions

View File

@@ -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<zbus::Error> for Error {
}
}
impl From<zbus::fdo::Error> for Error {
fn from(err: zbus::fdo::Error) -> Self {
Error::ZbusFdo(err)
}
}
impl From<notify_rust::error::Error> for Error {
fn from(err: notify_rust::error::Error) -> Self {
Error::Notification(err)

View File

@@ -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<Mutex<Config>>) {
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::<FanPageData>();
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::<FanPageData>();
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<Node> = 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<Node> = 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:?}");
}
});
}