From 5311972345c78574051fb8563d21dc82eff017a2 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Fri, 24 Jun 2022 23:09:45 +1200 Subject: [PATCH] Set keyboard brightness on resume. Refactor some tasks --- daemon/src/ctrl_anime/mod.rs | 72 +++++++++--------------------- daemon/src/ctrl_aura/config.rs | 56 +++++++++++------------ daemon/src/ctrl_aura/controller.rs | 61 ++++++++++++++++--------- 3 files changed, 88 insertions(+), 101 deletions(-) diff --git a/daemon/src/ctrl_anime/mod.rs b/daemon/src/ctrl_anime/mod.rs index 0de3e459..baad79a4 100644 --- a/daemon/src/ctrl_anime/mod.rs +++ b/daemon/src/ctrl_anime/mod.rs @@ -19,7 +19,7 @@ use smol::{stream::StreamExt, Executor}; use std::{ cell::RefCell, error::Error, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, MutexGuard}, thread::sleep, }; use std::{ @@ -173,7 +173,7 @@ impl CtrlAnime { info!("AniMe no previous system thread running (now)"); thread_exit.store(false, Ordering::SeqCst); - + 'main: loop { thread_running.store(true, Ordering::SeqCst); for action in actions.iter() { @@ -319,6 +319,16 @@ impl crate::CtrlTask for CtrlAnimeTask { .await .expect("CtrlAnimeTask could not create ManagerProxy"); + let load_save = |start: bool, lock: MutexGuard, inner: Arc>| { + if start { + info!("CtrlAnimeTask running sleep animation"); + CtrlAnime::run_thread(inner.clone(), lock.cache.shutdown.clone(), true); + } else { + info!("CtrlAnimeTask running wake animation"); + CtrlAnime::run_thread(inner.clone(), lock.cache.wake.clone(), true); + } + }; + let inner = self.inner.clone(); executor .spawn(async move { @@ -326,31 +336,12 @@ impl crate::CtrlTask for CtrlAnimeTask { notif .for_each(|event| { if let Ok(args) = event.args() { - if args.start { - loop { - // Loop is required to try an attempt to get the mutex *without* blocking - // other threads - it is possible to end up with deadlocks otherwise. - if let Ok(lock) = inner.clone().try_lock() { - info!("CtrlAnimeTask running sleep animation"); - CtrlAnime::run_thread( - inner.clone(), - lock.cache.shutdown.clone(), - true, - ); - break; - } - } - } else { - loop { - if let Ok(lock) = inner.clone().try_lock() { - info!("CtrlAnimeTask running wake animation"); - CtrlAnime::run_thread( - inner.clone(), - lock.cache.wake.clone(), - true, - ); - break; - } + // Loop is required to try an attempt to get the mutex *without* blocking + // other threads - it is possible to end up with deadlocks otherwise. + loop { + if let Ok(lock) = inner.clone().try_lock() { + load_save(args.start, lock, inner.clone()); + break; } } } @@ -371,30 +362,9 @@ impl crate::CtrlTask for CtrlAnimeTask { notif .for_each(|event| { if let Ok(args) = event.args() { - if args.start { - loop { - if let Ok(lock) = inner.clone().try_lock() { - info!("CtrlAnimeTask running sleep animation"); - CtrlAnime::run_thread( - inner.clone(), - lock.cache.shutdown.clone(), - true, - ); - break; - } - } - } else { - // If waking up - intention is to catch hibernation event - loop { - if let Ok(lock) = inner.clone().lock() { - info!("CtrlAnimeTask running wake animation"); - CtrlAnime::run_thread( - inner.clone(), - lock.cache.wake.clone(), - true, - ); - break; - } + loop { + if let Ok(lock) = inner.clone().try_lock() { + load_save(args.start, lock, inner.clone()); } } } diff --git a/daemon/src/ctrl_aura/config.rs b/daemon/src/ctrl_aura/config.rs index 5302069e..d78e20df 100644 --- a/daemon/src/ctrl_aura/config.rs +++ b/daemon/src/ctrl_aura/config.rs @@ -8,32 +8,6 @@ use std::io::{Read, Write}; pub static AURA_CONFIG_PATH: &str = "/etc/asusd/aura.conf"; -#[derive(Deserialize, Serialize)] -pub struct AuraConfigV352 { - pub brightness: LedBrightness, - pub current_mode: AuraModeNum, - pub builtins: BTreeMap, - pub multizone: Option, -} - -impl AuraConfigV352 { - pub(crate) fn into_current(self) -> AuraConfig { - AuraConfig { - brightness: self.brightness, - current_mode: self.current_mode, - builtins: self.builtins, - multizone: self.multizone, - power_states: LedPowerStates { - boot_anim: true, - sleep_anim: true, - all_leds: true, - keys_leds: true, - side_leds: true, - }, - } - } -} - #[derive(Deserialize, Serialize)] pub struct AuraConfigV407 { pub brightness: LedBrightness, @@ -49,6 +23,7 @@ impl AuraConfigV407 { pub(crate) fn into_current(self) -> AuraConfig { AuraConfig { brightness: self.brightness, + last_brightness: LedBrightness::Med, current_mode: self.current_mode, builtins: self.builtins, multizone: self.multizone, @@ -63,9 +38,33 @@ impl AuraConfigV407 { } } +#[derive(Deserialize, Serialize)] +pub struct AuraConfigV411 { + pub brightness: LedBrightness, + pub current_mode: AuraModeNum, + pub builtins: BTreeMap, + pub multizone: Option, + pub power_states: LedPowerStates, +} + +impl AuraConfigV411 { + pub(crate) fn into_current(self) -> AuraConfig { + AuraConfig { + brightness: self.brightness, + last_brightness: LedBrightness::Med, + current_mode: self.current_mode, + builtins: self.builtins, + multizone: self.multizone, + power_states: self.power_states, + } + } +} + #[derive(Deserialize, Serialize)] pub struct AuraConfig { pub brightness: LedBrightness, + /// Used to re-set brightness on wake from sleep/hibernation + pub last_brightness: LedBrightness, pub current_mode: AuraModeNum, pub builtins: BTreeMap, pub multizone: Option, @@ -76,6 +75,7 @@ impl Default for AuraConfig { fn default() -> Self { AuraConfig { brightness: LedBrightness::Med, + last_brightness: LedBrightness::Med, current_mode: AuraModeNum::Static, builtins: BTreeMap::new(), multizone: None, @@ -111,12 +111,12 @@ impl AuraConfig { } else { if let Ok(data) = serde_json::from_str(&buf) { return data; - } else if let Ok(data) = serde_json::from_str::(&buf) { + } else if let Ok(data) = serde_json::from_str::(&buf) { let config = data.into_current(); config.write(); info!("Updated AuraConfig version"); return config; - } else if let Ok(data) = serde_json::from_str::(&buf) { + } else if let Ok(data) = serde_json::from_str::(&buf) { let config = data.into_current(); config.write(); info!("Updated AuraConfig version"); diff --git a/daemon/src/ctrl_aura/controller.rs b/daemon/src/ctrl_aura/controller.rs index 5a29eb28..9ad66b3c 100644 --- a/daemon/src/ctrl_aura/controller.rs +++ b/daemon/src/ctrl_aura/controller.rs @@ -16,11 +16,11 @@ use rog_aura::{ }; use rog_supported::LedSupportedFunctions; use smol::{stream::StreamExt, Executor}; -use std::fs::OpenOptions; use std::io::{Read, Write}; use std::path::Path; use std::sync::Arc; use std::sync::Mutex; +use std::{fs::OpenOptions, sync::MutexGuard}; use zbus::Connection; use crate::GetSupported; @@ -99,6 +99,24 @@ impl CtrlTask for CtrlKbdLedTask { .await .expect("CtrlKbdLedTask could not create ManagerProxy"); + let load_save = |start: bool, mut lock: MutexGuard| { + // If waking up + if !start { + info!("CtrlKbdLedTask reloading brightness and modes"); + lock.set_brightness(lock.config.last_brightness) + .map_err(|e| error!("CtrlKbdLedTask: {e}")) + .ok(); + if let Some(mode) = lock.config.builtins.get(&lock.config.current_mode) { + lock.write_mode(mode) + .map_err(|e| error!("CtrlKbdLedTask: {e}")) + .ok(); + } + } else if start { + info!("CtrlKbdLedTask saving last brightness"); + lock.config.last_brightness = lock.config.brightness; + } + }; + let inner = self.inner.clone(); executor .spawn(async move { @@ -106,27 +124,26 @@ impl CtrlTask for CtrlKbdLedTask { notif .for_each(|event| { if let Ok(args) = event.args() { - // If waking up - if !args.start { - info!("CtrlKbdLedTask reloading brightness and modes"); - loop { - // Loop so that we do aquire the lock but also don't block other - // threads (prevents potential deadlocks) - if let Ok(lock) = inner.clone().try_lock() { - // Can't reload brightness due to system setting the brightness on sleep/wake - // and the config update task saving that change. - // lock.set_brightness(lock.config.brightness) - // .map_err(|e| error!("CtrlKbdLedTask: {e}")) - // .ok(); - if let Some(mode) = - lock.config.builtins.get(&lock.config.current_mode) - { - lock.write_mode(mode) - .map_err(|e| error!("CtrlKbdLedTask: {e}")) - .ok(); - } - break; - } + loop { + // Loop so that we do aquire the lock but also don't block other + // threads (prevents potential deadlocks) + if let Ok(lock) = inner.clone().try_lock() { + load_save(args.start, lock); + break; + } + } + } + }) + .await; + } + if let Ok(notif) = manager.receive_prepare_for_shutdown().await { + notif + .for_each(|event| { + if let Ok(args) = event.args() { + loop { + if let Ok(lock) = inner.clone().try_lock() { + load_save(args.start, lock); + break; } } }