From 0c97cf710de13a07c8cbacfdf52d84e967d09b20 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Tue, 20 Sep 2022 20:57:39 +1200 Subject: [PATCH] Trial single inotify test --- CHANGELOG.md | 4 ++- Cargo.lock | 1 + daemon/Cargo.toml | 2 ++ daemon/src/ctrl_anime/mod.rs | 7 +++- daemon/src/ctrl_aura/controller.rs | 7 +++- daemon/src/ctrl_platform.rs | 6 +++- daemon/src/ctrl_power.rs | 6 +++- daemon/src/ctrl_profiles/controller.rs | 34 +----------------- daemon/src/ctrl_profiles/zbus.rs | 49 ++++++++++++++++++++++++++ daemon/src/daemon.rs | 21 ++++++----- daemon/src/lib.rs | 37 +++++++++++++++++-- 11 files changed, 126 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 629f9f47..8224a9c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,9 @@ All notable changes to this project will be documented in this file. 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). -## [Unreleased ] +## [Unreleased - 4.4.1] +### Changed +- Use loops to ensure that mutex is gained for LED changes. ## [v4.4.0] - 2022-08-29 ### Added diff --git a/Cargo.lock b/Cargo.lock index 3fb39f30..82668193 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -576,6 +576,7 @@ version = "4.4.0" dependencies = [ "async-trait", "env_logger", + "inotify", "log", "logind-zbus", "rog_anime", diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index c0c10929..135c8f46 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -43,3 +43,5 @@ toml = "^0.5.8" # Device control sysfs-class = "^0.1.2" # used for backlight control and baord ID + +inotify = "0.10.0" diff --git a/daemon/src/ctrl_anime/mod.rs b/daemon/src/ctrl_anime/mod.rs index 01291047..c734b8f9 100644 --- a/daemon/src/ctrl_anime/mod.rs +++ b/daemon/src/ctrl_anime/mod.rs @@ -1,6 +1,7 @@ pub mod config; pub mod zbus; +use ::zbus::SignalContext; use async_trait::async_trait; use log::{error, info, warn}; use rog_anime::{ @@ -229,7 +230,11 @@ impl CtrlAnimeTask { #[async_trait] impl crate::CtrlTask for CtrlAnimeTask { - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError> { + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + _: SignalContext<'a>, + ) -> Result<(), RogError> { let run_action = |start: bool, lock: MutexGuard, inner: Arc>| { if start { diff --git a/daemon/src/ctrl_aura/controller.rs b/daemon/src/ctrl_aura/controller.rs index b7f09336..2545e2ae 100644 --- a/daemon/src/ctrl_aura/controller.rs +++ b/daemon/src/ctrl_aura/controller.rs @@ -16,6 +16,7 @@ use std::collections::BTreeMap; use std::sync::Arc; use std::sync::Mutex; use std::sync::MutexGuard; +use zbus::SignalContext; use crate::GetSupported; @@ -94,7 +95,11 @@ impl CtrlKbdLedTask { #[async_trait] impl CtrlTask for CtrlKbdLedTask { - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError> { + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + _: SignalContext<'a>, + ) -> Result<(), RogError> { let load_save = |start: bool, mut lock: MutexGuard| { // If waking up if !start { diff --git a/daemon/src/ctrl_platform.rs b/daemon/src/ctrl_platform.rs index fa3e251d..117d91fe 100644 --- a/daemon/src/ctrl_platform.rs +++ b/daemon/src/ctrl_platform.rs @@ -259,7 +259,11 @@ impl crate::Reloadable for CtrlRogBios { #[async_trait] impl CtrlTask for CtrlRogBios { - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError> { + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + _: SignalContext<'a>, + ) -> Result<(), RogError> { let platform1 = self.clone(); let platform2 = self.clone(); self.create_sys_event_tasks( diff --git a/daemon/src/ctrl_power.rs b/daemon/src/ctrl_power.rs index 6f01fd30..5100bbdc 100644 --- a/daemon/src/ctrl_power.rs +++ b/daemon/src/ctrl_power.rs @@ -108,7 +108,11 @@ impl CtrlPower { #[async_trait] impl CtrlTask for CtrlPower { - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError> { + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + _: SignalContext<'a>, + ) -> Result<(), RogError> { let power1 = self.clone(); let power2 = self.clone(); self.create_sys_event_tasks( diff --git a/daemon/src/ctrl_profiles/controller.rs b/daemon/src/ctrl_profiles/controller.rs index 80b9a60d..4666bbfe 100644 --- a/daemon/src/ctrl_profiles/controller.rs +++ b/daemon/src/ctrl_profiles/controller.rs @@ -1,12 +1,9 @@ use crate::error::RogError; -use crate::{CtrlTask, GetSupported}; -use async_trait::async_trait; +use crate::GetSupported; use log::{info, warn}; use rog_platform::supported::PlatformProfileFunctions; use rog_profiles::error::ProfileError; use rog_profiles::{FanCurveProfiles, Profile}; -use smol::Executor; -use std::sync::{Arc, Mutex}; use super::config::ProfileConfig; @@ -129,32 +126,3 @@ impl CtrlPlatformProfile { Ok(()) } } - -pub struct CtrlProfileTask { - ctrl: Arc>, -} - -impl CtrlProfileTask { - pub fn new(ctrl: Arc>) -> Self { - Self { ctrl } - } -} - -#[async_trait] -impl CtrlTask for CtrlProfileTask { - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError> { - let ctrl = self.ctrl.clone(); - self.repeating_task(666, executor, move || { - if let Ok(ref mut lock) = ctrl.try_lock() { - let new_profile = Profile::get_active_profile().unwrap(); - if new_profile != lock.config.active_profile { - lock.config.active_profile = new_profile; - lock.write_profile_curve_to_platform().unwrap(); - lock.save_config(); - } - } - }) - .await; - Ok(()) - } -} diff --git a/daemon/src/ctrl_profiles/zbus.rs b/daemon/src/ctrl_profiles/zbus.rs index 91fdcd08..36ca471e 100644 --- a/daemon/src/ctrl_profiles/zbus.rs +++ b/daemon/src/ctrl_profiles/zbus.rs @@ -1,8 +1,12 @@ use async_trait::async_trait; +use inotify::Inotify; +use inotify::WatchMask; use log::warn; use rog_profiles::fan_curve_set::CurveData; use rog_profiles::fan_curve_set::FanCurveSet; use rog_profiles::Profile; +use rog_profiles::PLATFORM_PROFILE; +use smol::Executor; use zbus::Connection; use zbus::SignalContext; @@ -10,6 +14,9 @@ use std::sync::Arc; use std::sync::Mutex; use zbus::{dbus_interface, fdo::Error}; +use crate::error::RogError; +use crate::CtrlTask; + use super::controller::CtrlPlatformProfile; static UNSUPPORTED_MSG: &str = @@ -209,3 +216,45 @@ impl crate::ZbusAdd for ProfileZbus { Self::add_to_server_helper(self, "/org/asuslinux/Profile", server).await; } } + +#[async_trait] +impl CtrlTask for ProfileZbus { + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + signal: SignalContext<'a>, + ) -> Result<(), RogError> { + let ctrl = self.inner.clone(); + let mut inotify = Inotify::init()?; + inotify.add_watch(PLATFORM_PROFILE, WatchMask::MODIFY)?; + + executor + .spawn(async move { + let mut buffer = [0; 1024]; + loop { + if let Ok(events) = inotify.read_events_blocking(&mut buffer) { + for _ in events { + let mut active_profile = None; + + if let Ok(ref mut lock) = ctrl.try_lock() { + let new_profile = Profile::get_active_profile().unwrap(); + if new_profile != lock.config.active_profile { + lock.config.active_profile = new_profile; + lock.write_profile_curve_to_platform().unwrap(); + lock.save_config(); + active_profile = Some(lock.config.active_profile); + } + } + + if let Some(active_profile) = active_profile { + Self::notify_profile(&signal, active_profile).await.ok(); + } + } + } + } + }) + .detach(); + + Ok(()) + } +} diff --git a/daemon/src/daemon.rs b/daemon/src/daemon.rs index d0a39896..2bb39275 100644 --- a/daemon/src/daemon.rs +++ b/daemon/src/daemon.rs @@ -3,8 +3,7 @@ use std::error::Error; use std::io::Write; use std::sync::{Arc, Mutex}; -use ::zbus::Connection; -use daemon::ctrl_profiles::controller::CtrlProfileTask; +use ::zbus::{Connection, SignalContext}; use log::LevelFilter; use log::{error, info, warn}; use smol::Executor; @@ -90,7 +89,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box> ctrl.add_to_server(&mut connection).await; let task = CtrlRogBios::new(config.clone())?; - task.create_tasks(executor).await.ok(); + let sig = SignalContext::new(&connection, "/org/asuslinux/Platform")?; + task.create_tasks(executor, sig).await.ok(); } Err(err) => { error!("rog_bios_control: {}", err); @@ -106,7 +106,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box> ctrl.add_to_server(&mut connection).await; let task = CtrlPower::new(config)?; - task.create_tasks(executor).await.ok(); + let sig = SignalContext::new(&connection, "/org/asuslinux/Charge")?; + task.create_tasks(executor, sig).await.ok(); } Err(err) => { error!("charge_control: {}", err); @@ -121,10 +122,12 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box> .unwrap_or_else(|err| warn!("Profile control: {}", err)); let tmp = Arc::new(Mutex::new(ctrl)); - let task = CtrlProfileTask::new(tmp.clone()); - task.create_tasks(executor).await.ok(); + //let task = CtrlProfileTask::new(tmp.clone()); + //task.create_tasks(executor).await.ok(); + let sig = SignalContext::new(&connection, "/org/asuslinux/Profile")?; let task = ProfileZbus::new(tmp.clone()); + task.create_tasks(executor, sig).await.ok(); task.add_to_server(&mut connection).await; } Err(err) => { @@ -148,7 +151,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box> zbus.add_to_server(&mut connection).await; let task = CtrlAnimeTask::new(inner).await; - task.create_tasks(executor).await.ok(); + let sig = SignalContext::new(&connection, "/org/asuslinux/Anime")?; + task.create_tasks(executor, sig).await.ok(); } Err(err) => { error!("AniMe control: {}", err); @@ -171,7 +175,8 @@ async fn start_daemon(executor: &mut Executor<'_>) -> Result<(), Box> .await; let task = CtrlKbdLedTask::new(inner); - task.create_tasks(executor).await.ok(); + let sig = SignalContext::new(&connection, "/org/asuslinux/Aura")?; + task.create_tasks(executor, sig).await.ok(); } Err(err) => { error!("Keyboard control: {}", err); diff --git a/daemon/src/lib.rs b/daemon/src/lib.rs index 1009f8be..87f1b14c 100644 --- a/daemon/src/lib.rs +++ b/daemon/src/lib.rs @@ -24,10 +24,11 @@ use std::time::Duration; use crate::error::RogError; use async_trait::async_trait; use config::Config; +use inotify::{Inotify, WatchMask}; use log::warn; use logind_zbus::manager::ManagerProxy; use smol::{stream::StreamExt, Executor, Timer}; -use zbus::Connection; +use zbus::{Connection, SignalContext}; use zvariant::ObjectPath; pub const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -62,7 +63,39 @@ pub trait ZbusAdd { pub trait CtrlTask { /// Implement to set up various tasks that may be required, using the `Executor`. /// No blocking loops are allowed, or they must be run on a separate thread. - async fn create_tasks(&self, executor: &mut Executor) -> Result<(), RogError>; + async fn create_tasks<'a>( + &self, + executor: &mut Executor<'a>, + signal: SignalContext<'a>, + ) -> Result<(), RogError>; + + /// Free method to run a task when the path is modified + /// + /// Not very useful if you need to also do a zbus notification. + fn create_tasks_inotify( + &self, + executor: &mut Executor, + path: &str, + mut task: impl FnMut() + Send + 'static, + ) -> Result<(), RogError> { + let mut inotify = Inotify::init()?; + inotify.add_watch(path, WatchMask::MODIFY)?; + let mut buffer = [0; 1024]; + + executor + .spawn(async move { + loop { + if let Ok(events) = inotify.read_events_blocking(&mut buffer) { + for _ in events { + task() + } + } + } + }) + .detach(); + + Ok(()) + } /// Create a timed repeating task async fn repeating_task(