Set keyboard brightness on resume. Refactor some tasks

This commit is contained in:
Luke D. Jones
2022-06-24 23:09:45 +12:00
parent 967295fba7
commit 5311972345
3 changed files with 88 additions and 101 deletions

View File

@@ -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<CtrlAnime>, inner: Arc<Mutex<CtrlAnime>>| {
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());
}
}
}

View File

@@ -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<AuraModeNum, AuraEffect>,
pub multizone: Option<AuraMultiZone>,
}
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<AuraModeNum, AuraEffect>,
pub multizone: Option<AuraMultiZone>,
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<AuraModeNum, AuraEffect>,
pub multizone: Option<AuraMultiZone>,
@@ -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::<AuraConfigV352>(&buf) {
} else if let Ok(data) = serde_json::from_str::<AuraConfigV407>(&buf) {
let config = data.into_current();
config.write();
info!("Updated AuraConfig version");
return config;
} else if let Ok(data) = serde_json::from_str::<AuraConfigV407>(&buf) {
} else if let Ok(data) = serde_json::from_str::<AuraConfigV411>(&buf) {
let config = data.into_current();
config.write();
info!("Updated AuraConfig version");

View File

@@ -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<CtrlKbdLed>| {
// 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;
}
}
}