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::{ use std::{
cell::RefCell, cell::RefCell,
error::Error, error::Error,
sync::{Arc, Mutex}, sync::{Arc, Mutex, MutexGuard},
thread::sleep, thread::sleep,
}; };
use std::{ use std::{
@@ -319,6 +319,16 @@ impl crate::CtrlTask for CtrlAnimeTask {
.await .await
.expect("CtrlAnimeTask could not create ManagerProxy"); .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(); let inner = self.inner.clone();
executor executor
.spawn(async move { .spawn(async move {
@@ -326,31 +336,12 @@ impl crate::CtrlTask for CtrlAnimeTask {
notif notif
.for_each(|event| { .for_each(|event| {
if let Ok(args) = event.args() { if let Ok(args) = event.args() {
if args.start { // Loop is required to try an attempt to get the mutex *without* blocking
loop { // other threads - it is possible to end up with deadlocks otherwise.
// Loop is required to try an attempt to get the mutex *without* blocking loop {
// other threads - it is possible to end up with deadlocks otherwise. if let Ok(lock) = inner.clone().try_lock() {
if let Ok(lock) = inner.clone().try_lock() { load_save(args.start, lock, inner.clone());
info!("CtrlAnimeTask running sleep animation"); break;
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;
}
} }
} }
} }
@@ -371,30 +362,9 @@ impl crate::CtrlTask for CtrlAnimeTask {
notif notif
.for_each(|event| { .for_each(|event| {
if let Ok(args) = event.args() { if let Ok(args) = event.args() {
if args.start { loop {
loop { if let Ok(lock) = inner.clone().try_lock() {
if let Ok(lock) = inner.clone().try_lock() { load_save(args.start, lock, inner.clone());
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;
}
} }
} }
} }

View File

@@ -8,32 +8,6 @@ use std::io::{Read, Write};
pub static AURA_CONFIG_PATH: &str = "/etc/asusd/aura.conf"; 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)] #[derive(Deserialize, Serialize)]
pub struct AuraConfigV407 { pub struct AuraConfigV407 {
pub brightness: LedBrightness, pub brightness: LedBrightness,
@@ -49,6 +23,7 @@ impl AuraConfigV407 {
pub(crate) fn into_current(self) -> AuraConfig { pub(crate) fn into_current(self) -> AuraConfig {
AuraConfig { AuraConfig {
brightness: self.brightness, brightness: self.brightness,
last_brightness: LedBrightness::Med,
current_mode: self.current_mode, current_mode: self.current_mode,
builtins: self.builtins, builtins: self.builtins,
multizone: self.multizone, 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)] #[derive(Deserialize, Serialize)]
pub struct AuraConfig { pub struct AuraConfig {
pub brightness: LedBrightness, pub brightness: LedBrightness,
/// Used to re-set brightness on wake from sleep/hibernation
pub last_brightness: LedBrightness,
pub current_mode: AuraModeNum, pub current_mode: AuraModeNum,
pub builtins: BTreeMap<AuraModeNum, AuraEffect>, pub builtins: BTreeMap<AuraModeNum, AuraEffect>,
pub multizone: Option<AuraMultiZone>, pub multizone: Option<AuraMultiZone>,
@@ -76,6 +75,7 @@ impl Default for AuraConfig {
fn default() -> Self { fn default() -> Self {
AuraConfig { AuraConfig {
brightness: LedBrightness::Med, brightness: LedBrightness::Med,
last_brightness: LedBrightness::Med,
current_mode: AuraModeNum::Static, current_mode: AuraModeNum::Static,
builtins: BTreeMap::new(), builtins: BTreeMap::new(),
multizone: None, multizone: None,
@@ -111,12 +111,12 @@ impl AuraConfig {
} else { } else {
if let Ok(data) = serde_json::from_str(&buf) { if let Ok(data) = serde_json::from_str(&buf) {
return data; 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(); let config = data.into_current();
config.write(); config.write();
info!("Updated AuraConfig version"); info!("Updated AuraConfig version");
return config; 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(); let config = data.into_current();
config.write(); config.write();
info!("Updated AuraConfig version"); info!("Updated AuraConfig version");

View File

@@ -16,11 +16,11 @@ use rog_aura::{
}; };
use rog_supported::LedSupportedFunctions; use rog_supported::LedSupportedFunctions;
use smol::{stream::StreamExt, Executor}; use smol::{stream::StreamExt, Executor};
use std::fs::OpenOptions;
use std::io::{Read, Write}; use std::io::{Read, Write};
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use std::sync::Mutex; use std::sync::Mutex;
use std::{fs::OpenOptions, sync::MutexGuard};
use zbus::Connection; use zbus::Connection;
use crate::GetSupported; use crate::GetSupported;
@@ -99,6 +99,24 @@ impl CtrlTask for CtrlKbdLedTask {
.await .await
.expect("CtrlKbdLedTask could not create ManagerProxy"); .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(); let inner = self.inner.clone();
executor executor
.spawn(async move { .spawn(async move {
@@ -106,27 +124,26 @@ impl CtrlTask for CtrlKbdLedTask {
notif notif
.for_each(|event| { .for_each(|event| {
if let Ok(args) = event.args() { if let Ok(args) = event.args() {
// If waking up loop {
if !args.start { // Loop so that we do aquire the lock but also don't block other
info!("CtrlKbdLedTask reloading brightness and modes"); // threads (prevents potential deadlocks)
loop { if let Ok(lock) = inner.clone().try_lock() {
// Loop so that we do aquire the lock but also don't block other load_save(args.start, lock);
// threads (prevents potential deadlocks) break;
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}")) .await;
// .ok(); }
if let Some(mode) = if let Ok(notif) = manager.receive_prepare_for_shutdown().await {
lock.config.builtins.get(&lock.config.current_mode) notif
{ .for_each(|event| {
lock.write_mode(mode) if let Ok(args) = event.args() {
.map_err(|e| error!("CtrlKbdLedTask: {e}")) loop {
.ok(); if let Ok(lock) = inner.clone().try_lock() {
} load_save(args.start, lock);
break; break;
}
} }
} }
} }