Trial single inotify test

This commit is contained in:
Luke D. Jones
2022-09-20 20:57:39 +12:00
parent 62c7338b2d
commit 0c97cf710d
11 changed files with 126 additions and 48 deletions

View File

@@ -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<CtrlAnime>, inner: Arc<Mutex<CtrlAnime>>| {
if start {

View File

@@ -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<CtrlKbdLed>| {
// If waking up
if !start {

View File

@@ -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(

View File

@@ -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(

View File

@@ -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<Mutex<CtrlPlatformProfile>>,
}
impl CtrlProfileTask {
pub fn new(ctrl: Arc<Mutex<CtrlPlatformProfile>>) -> 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(())
}
}

View File

@@ -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(())
}
}

View File

@@ -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<dyn Error>>
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<dyn Error>>
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<dyn Error>>
.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<dyn Error>>
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<dyn Error>>
.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);

View File

@@ -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(