From 56285916cd79bb93a47cacb76b24b92e76fc4a7f Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Wed, 21 Sep 2022 19:04:28 +1200 Subject: [PATCH] daemon: inotify for panel_od and gu_mux_mode --- Cargo.lock | 1 + daemon/Cargo.toml | 1 + daemon/src/ctrl_platform.rs | 67 +++++++++++++++++++++++++++----- daemon/src/ctrl_profiles/zbus.rs | 6 ++- rog-control-center/src/notify.rs | 2 +- rog-dbus/src/zbus_platform.rs | 2 +- rog-platform/src/macros.rs | 1 + rog-platform/src/platform.rs | 8 +++- 8 files changed, 73 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82668193..c43a1218 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -575,6 +575,7 @@ name = "daemon" version = "4.4.0" dependencies = [ "async-trait", + "concat-idents", "env_logger", "inotify", "log", diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 135c8f46..7e94e8c0 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -45,3 +45,4 @@ toml = "^0.5.8" sysfs-class = "^0.1.2" # used for backlight control and baord ID inotify = "0.10.0" +concat-idents = "1.1.3" \ No newline at end of file diff --git a/daemon/src/ctrl_platform.rs b/daemon/src/ctrl_platform.rs index 117d91fe..81565e44 100644 --- a/daemon/src/ctrl_platform.rs +++ b/daemon/src/ctrl_platform.rs @@ -154,7 +154,7 @@ impl CtrlRogBios { Self::notify_gpu_mux_mode(&ctxt, mode).await.ok(); } - fn gpu_mux_mode(&self) -> GpuMode { + fn get_gpu_mux_mode(&self) -> GpuMode { match self.platform.get_gpu_mux_mode() { Ok(m) => GpuMode::from_mux(m), Err(e) => { @@ -203,6 +203,7 @@ impl CtrlRogBios { overdrive: bool, ) { if self + .platform .set_panel_od(overdrive) .map_err(|err| { warn!("CtrlRogBios: set_panel_overdrive {}", err); @@ -214,25 +215,29 @@ impl CtrlRogBios { lock.panel_od = overdrive; lock.write(); } - Self::notify_panel_overdrive(&ctxt, overdrive).await.ok(); + Self::notify_panel_od(&ctxt, overdrive).await.ok(); } } - fn panel_overdrive(&self) -> bool { - self.platform + /// Get the `panel_od` value from platform. Updates the stored value in internal config also. + fn get_panel_od(&self) -> bool { + let od = self + .platform .get_panel_od() .map_err(|err| { warn!("CtrlRogBios: get panel overdrive {}", err); err }) - .unwrap_or(false) + .unwrap_or(false); + if let Ok(mut lock) = self.config.try_lock() { + lock.panel_od = od; + lock.write(); + } + od } #[dbus_interface(signal)] - async fn notify_panel_overdrive( - signal_ctxt: &SignalContext<'_>, - overdrive: bool, - ) -> zbus::Result<()> { + async fn notify_panel_od(signal_ctxt: &SignalContext<'_>, overdrive: bool) -> zbus::Result<()> { } } @@ -257,12 +262,50 @@ impl crate::Reloadable for CtrlRogBios { } } +macro_rules! watch_item { + ($name:ident) => { + concat_idents::concat_idents!(fn_name = watch_, $name { + async fn fn_name<'a>( + &self, + executor: &mut Executor<'a>, + signal_ctxt: SignalContext<'a>, + ) -> Result<(), RogError> { + let ctrl = self.clone(); + concat_idents::concat_idents!(watch_fn = monitor_, $name { + let mut watch = self.platform.watch_fn()?; + executor + .spawn(async move { + let mut buffer = [0; 1024]; + loop { + if let Ok(events) = watch.read_events_blocking(&mut buffer) { + for _ in events { + let value = concat_idents::concat_idents!(get_fn = get_, $name { ctrl.get_fn() }); + concat_idents::concat_idents!(notif_fn = notify_, $name { + Self::notif_fn(&signal_ctxt, value).await.unwrap(); + }); + } + } + } + }) + .detach(); + }); + Ok(()) + } + }); + }; +} + +impl CtrlRogBios { + watch_item!(panel_od); + watch_item!(gpu_mux_mode); +} + #[async_trait] impl CtrlTask for CtrlRogBios { async fn create_tasks<'a>( &self, executor: &mut Executor<'a>, - _: SignalContext<'a>, + signal_ctxt: SignalContext<'a>, ) -> Result<(), RogError> { let platform1 = self.clone(); let platform2 = self.clone(); @@ -301,6 +344,10 @@ impl CtrlTask for CtrlRogBios { ) .await; + self.watch_panel_od(executor, signal_ctxt.clone()).await?; + self.watch_gpu_mux_mode(executor, signal_ctxt.clone()) + .await?; + Ok(()) } } diff --git a/daemon/src/ctrl_profiles/zbus.rs b/daemon/src/ctrl_profiles/zbus.rs index 36ca471e..389e8b3d 100644 --- a/daemon/src/ctrl_profiles/zbus.rs +++ b/daemon/src/ctrl_profiles/zbus.rs @@ -222,7 +222,7 @@ impl CtrlTask for ProfileZbus { async fn create_tasks<'a>( &self, executor: &mut Executor<'a>, - signal: SignalContext<'a>, + signal_ctxt: SignalContext<'a>, ) -> Result<(), RogError> { let ctrl = self.inner.clone(); let mut inotify = Inotify::init()?; @@ -247,7 +247,9 @@ impl CtrlTask for ProfileZbus { } if let Some(active_profile) = active_profile { - Self::notify_profile(&signal, active_profile).await.ok(); + Self::notify_profile(&signal_ctxt, active_profile) + .await + .ok(); } } } diff --git a/rog-control-center/src/notify.rs b/rog-control-center/src/notify.rs index 2012ba96..8a106217 100644 --- a/rog-control-center/src/notify.rs +++ b/rog-control-center/src/notify.rs @@ -86,7 +86,7 @@ pub fn start_notifications( .spawn(async move { let conn = zbus::Connection::system().await.unwrap(); let proxy = RogBiosProxy::new(&conn).await.unwrap(); - if let Ok(p) = proxy.receive_notify_panel_overdrive().await { + if let Ok(p) = proxy.receive_notify_panel_od().await { p.for_each(|_| { bios_notified.store(true, Ordering::SeqCst); future::ready(()) diff --git a/rog-dbus/src/zbus_platform.rs b/rog-dbus/src/zbus_platform.rs index 9009dc65..d8452b94 100644 --- a/rog-dbus/src/zbus_platform.rs +++ b/rog-dbus/src/zbus_platform.rs @@ -55,5 +55,5 @@ trait RogBios { /// NotifyPanelOverdrive signal #[dbus_proxy(signal)] - fn notify_panel_overdrive(&self, overdrive: bool) -> zbus::Result<()>; + fn notify_panel_od(&self, overdrive: bool) -> zbus::Result<()>; } diff --git a/rog-platform/src/macros.rs b/rog-platform/src/macros.rs index 244fdb94..e9bf431a 100644 --- a/rog-platform/src/macros.rs +++ b/rog-platform/src/macros.rs @@ -127,5 +127,6 @@ macro_rules! attr_u8_array { crate::has_attr!($attr_name $item); crate::get_attr_u8_array!($attr_name $item); crate::set_attr_u8_array!($attr_name $item); + crate::watch_attr!($attr_name $item); }; } diff --git a/rog-platform/src/platform.rs b/rog-platform/src/platform.rs index 46aee4bd..580ac83b 100644 --- a/rog-platform/src/platform.rs +++ b/rog-platform/src/platform.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::{path::PathBuf, str::FromStr}; use log::{info, warn}; use serde::{Deserialize, Serialize}; @@ -20,6 +20,7 @@ use crate::{ #[derive(Debug, PartialEq, PartialOrd, Clone)] pub struct AsusPlatform { path: PathBuf, + pp_path: PathBuf, } impl AsusPlatform { @@ -44,6 +45,7 @@ impl AsusPlatform { info!("Found platform support at {:?}", device.sysname()); return Ok(Self { path: device.syspath().to_owned(), + pp_path: PathBuf::from_str("/sys/firmware/acpi").unwrap(), }); } Err(PlatformError::MissingFunction( @@ -55,6 +57,10 @@ impl AsusPlatform { attr_bool!("egpu_enable", path); attr_bool!("panel_od", path); attr_u8!("gpu_mux_mode", path); + attr_u8!("throttle_thermal_policy", path); + + // The acpi platform_profile support + attr_u8!("platform_profile", pp_path); } #[derive(Serialize, Deserialize, Type, Debug, PartialEq, Clone, Copy)]