Is smol blocking or inotify blocking it?

This commit is contained in:
Luke D. Jones
2022-09-21 22:17:55 +12:00
parent 56285916cd
commit 5d87747d96
13 changed files with 142 additions and 69 deletions

View File

@@ -5,8 +5,20 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased - 4.4.1] ## [Unreleased - 4.4.1]
### Added
- intofy watches on:
- `charge_control_end_threshold`
- `panel_od`
- `gpu_mux_mode`
- `platform_profile`
- These allow for updating any associated config and sending dbus notifications.
### Changed ### Changed
- Use loops to ensure that mutex is gained for LED changes. - Use loops to ensure that mutex is gained for LED changes.
### Breaking
- DBUS: all charge control methods renamed:
- `ChargeControlEndThreshold`
- `SetChargeControlEndThreshold`
- `NotifyChargeControlEndThreshold`
## [v4.4.0] - 2022-08-29 ## [v4.4.0] - 2022-08-29
### Added ### Added

View File

@@ -70,7 +70,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.spawn(async move { .spawn(async move {
let conn = zbus::Connection::system().await.unwrap(); let conn = zbus::Connection::system().await.unwrap();
let proxy = ChargeProxy::new(&conn).await.unwrap(); let proxy = ChargeProxy::new(&conn).await.unwrap();
if let Ok(p) = proxy.receive_notify_charge().await { if let Ok(p) = proxy.receive_notify_charge_control_end_threshold().await {
p.for_each(|e| { p.for_each(|e| {
if let Ok(out) = e.args() { if let Ok(out) = e.args() {
if let Ok(ref mut lock) = x.try_lock() { if let Ok(ref mut lock) = x.try_lock() {

View File

@@ -211,7 +211,7 @@ fn do_parsed(
} }
if let Some(chg_limit) = parsed.chg_limit { if let Some(chg_limit) = parsed.chg_limit {
dbus.proxies().charge().set_limit(chg_limit)?; dbus.proxies().charge().set_charge_control_end_threshold(chg_limit)?;
} }
Ok(()) Ok(())
@@ -803,10 +803,10 @@ fn handle_bios_option(
} }
if let Some(opt) = cmd.panel_overdrive_set { if let Some(opt) = cmd.panel_overdrive_set {
dbus.proxies().rog_bios().set_panel_overdrive(opt)?; dbus.proxies().rog_bios().set_panel_od(opt)?;
} }
if cmd.panel_overdrive_get { if cmd.panel_overdrive_get {
let res = dbus.proxies().rog_bios().panel_overdrive()?; let res = dbus.proxies().rog_bios().panel_od()?;
println!("Panel overdrive on: {}", res); println!("Panel overdrive on: {}", res);
} }
} }

View File

@@ -140,7 +140,7 @@ impl CtrlTask for CtrlKbdLedTask {
}, },
move || loop { move || loop {
if let Ok(lock) = inner3.clone().try_lock() { if let Ok(lock) = inner3.clone().try_lock() {
load_save(false, lock); load_save(true, lock);
break; break;
} }
}, },
@@ -153,6 +153,25 @@ impl CtrlTask for CtrlKbdLedTask {
) )
.await; .await;
let ctrl2 = self.inner.clone();
if let Ok(ctrl) = self.inner.lock() {
let mut watch = ctrl.kd_brightness.monitor_brightness()?;
executor
.spawn(async move {
let mut buffer = [0; 1024];
loop {
if let Ok(events) = watch.read_events_blocking(&mut buffer) {
for _ in events {
if let Ok(lock) = ctrl2.try_lock() {
load_save(true, lock);
}
}
}
}
})
.detach();
}
Ok(()) Ok(())
} }
} }

View File

@@ -1,4 +1,4 @@
use crate::CtrlTask; use crate::{CtrlTask, task_watch_item};
use crate::{config::Config, error::RogError, GetSupported}; use crate::{config::Config, error::RogError, GetSupported};
use async_trait::async_trait; use async_trait::async_trait;
use log::{info, warn}; use log::{info, warn};
@@ -154,7 +154,7 @@ impl CtrlRogBios {
Self::notify_gpu_mux_mode(&ctxt, mode).await.ok(); Self::notify_gpu_mux_mode(&ctxt, mode).await.ok();
} }
fn get_gpu_mux_mode(&self) -> GpuMode { fn gpu_mux_mode(&self) -> GpuMode {
match self.platform.get_gpu_mux_mode() { match self.platform.get_gpu_mux_mode() {
Ok(m) => GpuMode::from_mux(m), Ok(m) => GpuMode::from_mux(m),
Err(e) => { Err(e) => {
@@ -220,7 +220,7 @@ impl CtrlRogBios {
} }
/// Get the `panel_od` value from platform. Updates the stored value in internal config also. /// Get the `panel_od` value from platform. Updates the stored value in internal config also.
fn get_panel_od(&self) -> bool { fn panel_od(&self) -> bool {
let od = self let od = self
.platform .platform
.get_panel_od() .get_panel_od()
@@ -262,42 +262,9 @@ 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 { impl CtrlRogBios {
watch_item!(panel_od); task_watch_item!(panel_od platform);
watch_item!(gpu_mux_mode); task_watch_item!(gpu_mux_mode platform);
} }
#[async_trait] #[async_trait]
@@ -344,9 +311,8 @@ impl CtrlTask for CtrlRogBios {
) )
.await; .await;
self.watch_panel_od(executor, signal_ctxt.clone()).await?; self.watch_panel_od(executor, signal_ctxt.clone())?;
self.watch_gpu_mux_mode(executor, signal_ctxt.clone()) self.watch_gpu_mux_mode(executor, signal_ctxt.clone())?;
.await?;
Ok(()) Ok(())
} }

View File

@@ -1,5 +1,5 @@
use crate::CtrlTask;
use crate::{config::Config, error::RogError, GetSupported}; use crate::{config::Config, error::RogError, GetSupported};
use crate::{task_watch_item, CtrlTask};
use async_trait::async_trait; use async_trait::async_trait;
use log::{info, warn}; use log::{info, warn};
use rog_platform::power::AsusPower; use rog_platform::power::AsusPower;
@@ -33,7 +33,7 @@ pub struct CtrlPower {
#[dbus_interface(name = "org.asuslinux.Daemon")] #[dbus_interface(name = "org.asuslinux.Daemon")]
impl CtrlPower { impl CtrlPower {
async fn set_limit( async fn set_charge_control_end_threshold(
&mut self, &mut self,
#[zbus(signal_context)] ctxt: SignalContext<'_>, #[zbus(signal_context)] ctxt: SignalContext<'_>,
limit: u8, limit: u8,
@@ -47,19 +47,38 @@ impl CtrlPower {
err err
}) })
.ok(); .ok();
Self::notify_charge(&ctxt, limit).await?; Self::notify_charge_control_end_threshold(&ctxt, limit).await?;
Ok(()) Ok(())
} }
fn limit(&self) -> i8 { fn charge_control_end_threshold(&self) -> u8 {
if let Ok(config) = self.config.try_lock() { loop {
return config.bat_charge_limit as i8; if let Ok(config) = self.config.try_lock() {
let limit = self
.power
.get_charge_control_end_threshold()
.map_err(|err| {
warn!("CtrlCharge: get_charge_control_end_threshold {}", err);
err
})
.unwrap_or(100);
self.set(limit)
.map_err(|err| {
warn!("CtrlCharge: set_limit {}", err);
err
})
.ok();
return config.bat_charge_limit;
}
} }
-1
} }
#[dbus_interface(signal)] #[dbus_interface(signal)]
async fn notify_charge(ctxt: &SignalContext<'_>, limit: u8) -> zbus::Result<()>; async fn notify_charge_control_end_threshold(
ctxt: &SignalContext<'_>,
limit: u8,
) -> zbus::Result<()>;
} }
#[async_trait] #[async_trait]
@@ -104,6 +123,8 @@ impl CtrlPower {
Ok(()) Ok(())
} }
task_watch_item!(charge_control_end_threshold power);
} }
#[async_trait] #[async_trait]
@@ -111,7 +132,7 @@ impl CtrlTask for CtrlPower {
async fn create_tasks<'a>( async fn create_tasks<'a>(
&self, &self,
executor: &mut Executor<'a>, executor: &mut Executor<'a>,
_: SignalContext<'a>, signal_ctxt: SignalContext<'a>,
) -> Result<(), RogError> { ) -> Result<(), RogError> {
let power1 = self.clone(); let power1 = self.clone();
let power2 = self.clone(); let power2 = self.clone();
@@ -146,6 +167,8 @@ impl CtrlTask for CtrlPower {
) )
.await; .await;
self.watch_charge_control_end_threshold(executor, signal_ctxt)?;
Ok(()) Ok(())
} }
} }

View File

@@ -58,6 +58,58 @@ pub trait ZbusAdd {
} }
} }
/// This macro adds a function which spawns an `inotify` task on the passed in `Executor`.
///
/// The generated function is `watch_<name>()`. Self requires the following methods to be available:
/// - `<name>() -> SomeValue`, functionally is a getter, but is allowed to have side effects.
/// - `notify_<name>(SignalContext, SomeValue)`
///
/// In most cases if `SomeValue` is stored in a config then `<name>()` getter is expected to update it.
///
/// # Example
///
/// ```ignore
/// impl CtrlRogBios {
/// task_watch_item!(panel_od platform);
/// task_watch_item!(gpu_mux_mode platform);
/// }
/// ```
#[macro_export]
macro_rules! task_watch_item {
($name:ident $self_inner:ident) => {
concat_idents::concat_idents!(fn_name = watch_, $name {
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.$self_inner.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 = ctrl.$name();
dbg!(value);
concat_idents::concat_idents!(notif_fn = notify_, $name {
Self::notif_fn(&signal_ctxt, value).await.unwrap();
});
}
}
}
})
.detach();
dbg!("SPWADEWFWEFE");
});
Ok(())
}
});
};
}
/// Set up a task to run on the async executor /// Set up a task to run on the async executor
#[async_trait] #[async_trait]
pub trait CtrlTask { pub trait CtrlTask {

View File

@@ -103,7 +103,7 @@ pub fn start_notifications(
.spawn(async move { .spawn(async move {
let conn = zbus::Connection::system().await.unwrap(); let conn = zbus::Connection::system().await.unwrap();
let proxy = ChargeProxy::new(&conn).await.unwrap(); let proxy = ChargeProxy::new(&conn).await.unwrap();
if let Ok(p) = proxy.receive_notify_charge().await { if let Ok(p) = proxy.receive_notify_charge_control_end_threshold().await {
p.for_each(|e| { p.for_each(|e| {
if let Ok(out) = e.args() { if let Ok(out) = e.args() {
if notifs_enabled1.load(Ordering::SeqCst) { if notifs_enabled1.load(Ordering::SeqCst) {

View File

@@ -45,7 +45,7 @@ impl BiosState {
GpuMode::NotSupported GpuMode::NotSupported
}, },
panel_overdrive: if supported.rog_bios_ctrl.panel_overdrive { panel_overdrive: if supported.rog_bios_ctrl.panel_overdrive {
dbus.proxies().rog_bios().panel_overdrive()? dbus.proxies().rog_bios().panel_od()?
} else { } else {
false false
}, },
@@ -258,7 +258,7 @@ pub struct PageDataStates {
pub anime: AnimeState, pub anime: AnimeState,
pub profiles: ProfilesState, pub profiles: ProfilesState,
pub fan_curves: FanCurvesState, pub fan_curves: FanCurvesState,
pub charge_limit: i16, pub charge_limit: u8,
pub error: Option<String>, pub error: Option<String>,
} }
@@ -279,7 +279,7 @@ impl PageDataStates {
keyboard_layout, keyboard_layout,
notifs_enabled, notifs_enabled,
was_notified: charge_notified, was_notified: charge_notified,
charge_limit: dbus.proxies().charge().limit()?, charge_limit: dbus.proxies().charge().charge_control_end_threshold()?,
bios: BiosState::new(bios_notified, supported, dbus)?, bios: BiosState::new(bios_notified, supported, dbus)?,
aura: AuraState::new(aura_notified, supported, dbus)?, aura: AuraState::new(aura_notified, supported, dbus)?,
anime: AnimeState::new(anime_notified, supported, dbus)?, anime: AnimeState::new(anime_notified, supported, dbus)?,
@@ -296,7 +296,7 @@ impl PageDataStates {
) -> Result<bool> { ) -> Result<bool> {
let mut notified = false; let mut notified = false;
if self.was_notified.load(Ordering::SeqCst) { if self.was_notified.load(Ordering::SeqCst) {
self.charge_limit = dbus.proxies().charge().limit()?; self.charge_limit = dbus.proxies().charge().charge_control_end_threshold()?;
self.was_notified.store(false, Ordering::SeqCst); self.was_notified.store(false, Ordering::SeqCst);
notified = true; notified = true;
} }

View File

@@ -47,7 +47,7 @@ pub fn rog_bios_group(
if ui.add(slider).drag_released() { if ui.add(slider).drag_released() {
dbus.proxies() dbus.proxies()
.charge() .charge()
.set_limit(states.charge_limit as u8) .set_charge_control_end_threshold(states.charge_limit as u8)
.map_err(|err| { .map_err(|err| {
states.error = Some(err.to_string()); states.error = Some(err.to_string());
}) })
@@ -82,7 +82,7 @@ pub fn rog_bios_group(
{ {
dbus.proxies() dbus.proxies()
.rog_bios() .rog_bios()
.set_panel_overdrive(states.bios.panel_overdrive) .set_panel_od(states.bios.panel_overdrive)
.map_err(|err| { .map_err(|err| {
states.error = Some(err.to_string()); states.error = Some(err.to_string());
}) })

View File

@@ -26,13 +26,13 @@ use zbus_macros::dbus_proxy;
default_path = "/org/asuslinux/Charge" default_path = "/org/asuslinux/Charge"
)] )]
trait Charge { trait Charge {
/// Limit method /// charge_control_end_threshold method
fn limit(&self) -> zbus::Result<i16>; fn charge_control_end_threshold(&self) -> zbus::Result<u8>;
/// SetLimit method /// set_charge_control_end_threshold method
fn set_limit(&self, limit: u8) -> zbus::Result<()>; fn set_charge_control_end_threshold(&self, limit: u8) -> zbus::Result<()>;
/// NotifyCharge signal /// NotifyCharge signal
#[dbus_proxy(signal)] #[dbus_proxy(signal)]
fn notify_charge(&self, limit: u8) -> zbus::Result<u8>; fn notify_charge_control_end_threshold(&self, limit: u8) -> zbus::Result<u8>;
} }

View File

@@ -40,10 +40,10 @@ trait RogBios {
fn set_post_boot_sound(&self, on: bool) -> zbus::Result<()>; fn set_post_boot_sound(&self, on: bool) -> zbus::Result<()>;
/// PanelOverdrive method /// PanelOverdrive method
fn panel_overdrive(&self) -> zbus::Result<bool>; fn panel_od(&self) -> zbus::Result<bool>;
/// SetPanelOverdrive method /// SetPanelOverdrive method
fn set_panel_overdrive(&self, overdrive: bool) -> zbus::Result<()>; fn set_panel_od(&self, overdrive: bool) -> zbus::Result<()>;
/// NotifyDedicatedGraphicMode signal /// NotifyDedicatedGraphicMode signal
#[dbus_proxy(signal)] #[dbus_proxy(signal)]

View File

@@ -21,6 +21,7 @@ macro_rules! watch_attr {
pub fn fn_name(&self) -> Result<inotify::Inotify> { pub fn fn_name(&self) -> Result<inotify::Inotify> {
let mut path = self.$item.clone(); let mut path = self.$item.clone();
path.push($attr_name); path.push($attr_name);
dbg!(&path);
let mut inotify = inotify::Inotify::init().unwrap(); let mut inotify = inotify::Inotify::init().unwrap();
inotify.add_watch(path.to_str().unwrap(), inotify::WatchMask::MODIFY).unwrap(); inotify.add_watch(path.to_str().unwrap(), inotify::WatchMask::MODIFY).unwrap();
Ok(inotify) Ok(inotify)