mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-02-06 00:15:04 +01:00
Watch and save kbd LED brightness
This commit is contained in:
@@ -11,7 +11,7 @@ use dbus::{channel::Sender, nonblock::SyncConnection, tree::Signal};
|
|||||||
use log::{info, warn};
|
use log::{info, warn};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::OpenOptions;
|
use std::fs::OpenOptions;
|
||||||
use std::io::Write;
|
use std::io::{Read, Write};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::mpsc::Receiver;
|
use tokio::sync::mpsc::Receiver;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
@@ -19,6 +19,7 @@ use tokio::task::JoinHandle;
|
|||||||
|
|
||||||
pub struct CtrlKbdBacklight {
|
pub struct CtrlKbdBacklight {
|
||||||
dev_node: String,
|
dev_node: String,
|
||||||
|
bright_node: String,
|
||||||
supported_modes: Vec<u8>,
|
supported_modes: Vec<u8>,
|
||||||
flip_effect_write: bool,
|
flip_effect_write: bool,
|
||||||
}
|
}
|
||||||
@@ -31,24 +32,30 @@ impl crate::Controller for CtrlKbdBacklight {
|
|||||||
|
|
||||||
/// Spawns two tasks which continuously check for changes
|
/// Spawns two tasks which continuously check for changes
|
||||||
fn spawn_task_loop(
|
fn spawn_task_loop(
|
||||||
mut self,
|
self,
|
||||||
config: Arc<Mutex<Config>>,
|
config: Arc<Mutex<Config>>,
|
||||||
mut recv: Receiver<Self::A>,
|
mut recv: Receiver<Self::A>,
|
||||||
connection: Option<Arc<SyncConnection>>,
|
connection: Option<Arc<SyncConnection>>,
|
||||||
signal: Option<Arc<Signal<()>>>,
|
signal: Option<Arc<Signal<()>>>,
|
||||||
) -> Vec<JoinHandle<()>> {
|
) -> Vec<JoinHandle<()>> {
|
||||||
vec![tokio::spawn(async move {
|
let gate1 = Arc::new(Mutex::new(self));
|
||||||
|
let gate2 = gate1.clone();
|
||||||
|
|
||||||
|
let config1 = config.clone();
|
||||||
|
vec![
|
||||||
|
tokio::spawn(async move {
|
||||||
while let Some(command) = recv.recv().await {
|
while let Some(command) = recv.recv().await {
|
||||||
let mut config = config.lock().await;
|
let mut config = config1.lock().await;
|
||||||
|
let mut lock = gate1.lock().await;
|
||||||
match &command {
|
match &command {
|
||||||
AuraModes::PerKey(_) => {
|
AuraModes::PerKey(_) => {
|
||||||
self.do_command(command, &mut config)
|
lock.do_command(command, &mut config)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|err| warn!("{}", err));
|
.unwrap_or_else(|err| warn!("{}", err));
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let json = serde_json::to_string(&command).unwrap();
|
let json = serde_json::to_string(&command).unwrap();
|
||||||
self.do_command(command, &mut config)
|
lock.do_command(command, &mut config)
|
||||||
.await
|
.await
|
||||||
.unwrap_or_else(|err| warn!("{}", err));
|
.unwrap_or_else(|err| warn!("{}", err));
|
||||||
connection
|
connection
|
||||||
@@ -65,7 +72,17 @@ impl crate::Controller for CtrlKbdBacklight {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})]
|
}),
|
||||||
|
tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let mut lock = gate2.lock().await;
|
||||||
|
let mut config = config.lock().await;
|
||||||
|
lock.let_bright_check_change(&mut config)
|
||||||
|
.unwrap_or_else(|err| warn!("{:?}", err));
|
||||||
|
tokio::time::delay_for(std::time::Duration::from_millis(500)).await;
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn reload_from_config(&mut self, config: &mut Config) -> Result<(), Box<dyn Error>> {
|
async fn reload_from_config(&mut self, config: &mut Config) -> Result<(), Box<dyn Error>> {
|
||||||
@@ -126,6 +143,8 @@ impl CtrlKbdBacklight {
|
|||||||
info!("Using device at: {:?} for LED control", dev_node);
|
info!("Using device at: {:?} for LED control", dev_node);
|
||||||
return Ok(CtrlKbdBacklight {
|
return Ok(CtrlKbdBacklight {
|
||||||
dev_node: dev_node.to_string_lossy().to_string(),
|
dev_node: dev_node.to_string_lossy().to_string(),
|
||||||
|
bright_node: "/sys/class/leds/asus::kbd_backlight/brightness"
|
||||||
|
.to_string(),
|
||||||
supported_modes,
|
supported_modes,
|
||||||
flip_effect_write: false,
|
flip_effect_write: false,
|
||||||
});
|
});
|
||||||
@@ -139,6 +158,25 @@ impl CtrlKbdBacklight {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn let_bright_check_change(&mut self, config: &mut Config) -> Result<(), Box<dyn Error>> {
|
||||||
|
let mut file = OpenOptions::new().read(true).open(&self.bright_node)?;
|
||||||
|
let mut buf = [0u8; 1];
|
||||||
|
file.read_exact(&mut buf)?;
|
||||||
|
if let Some(num) = char::from(buf[0]).to_digit(10) {
|
||||||
|
if config.power_profile != num as u8 {
|
||||||
|
config.read();
|
||||||
|
config.kbd_boot_brightness = num as u8;
|
||||||
|
config.write();
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let err = std::io::Error::new(
|
||||||
|
std::io::ErrorKind::InvalidData,
|
||||||
|
"LED brightness could not be parsed",
|
||||||
|
);
|
||||||
|
Err(Box::new(err))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn do_command(
|
pub async fn do_command(
|
||||||
&mut self,
|
&mut self,
|
||||||
mode: AuraModes,
|
mode: AuraModes,
|
||||||
|
|||||||
Reference in New Issue
Block a user