From 8feacf863ad3d5bbb82dbbcf1b5adf055d2d7a3e Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Sat, 10 Dec 2022 20:51:00 +1300 Subject: [PATCH] asusd: Very basic support for running a command on AC/Battery switching --- CHANGELOG.md | 8 +++++++- daemon/src/config.rs | 26 +++++++++++++++++++++++++- daemon/src/ctrl_power.rs | 29 ++++++++++++++++++++++++++++- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d794f42..f99113d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - ROGCC: Track if reboot required when not using supergfxd - Add env var for logging levels to daemon and gui (`RUST_LOG=`) - ROGCC: Very basic support for running a command on AC/Battery switching, this is in config at `~/.config/rog/rog-control-center.cfg`, and for now must be edited by hand and ROGCC restarted (run ROGCC in BG to use effectively) - + + Run ROGCC from terminal to see errors of the AC/Battery command + + Support for editing via ROGCC GUI will come in future + + This is ideal for userspace tasks +- asusd: Very basic support for running a command on AC/Battery switching, this is in config at `/etc/asusd/asusd.conf`. A restart of asusd is not required if edited. + + This is ideal for tasks that require root access (BE SAFE!) +- The above AC/Battery commands are probably best set to run a script for more complex tasks + ## [v4.5.5] ### Changed - remove an unwrap() causing panic on main ROGCC thread diff --git a/daemon/src/config.rs b/daemon/src/config.rs index 676afcc0..2cba83a6 100644 --- a/daemon/src/config.rs +++ b/daemon/src/config.rs @@ -7,11 +7,12 @@ use std::path::PathBuf; pub static CONFIG_PATH: &str = "/etc/asusd/asusd.conf"; #[derive(Deserialize, Serialize, Default)] -#[serde(default)] pub struct Config { /// Save charge limit for restoring on boot pub bat_charge_limit: u8, pub panel_od: bool, + pub ac_command: String, + pub bat_command: String, } impl Config { @@ -19,6 +20,8 @@ impl Config { Config { bat_charge_limit: 100, panel_od: false, + ac_command: String::new(), + bat_command: String::new(), } } @@ -37,6 +40,8 @@ impl Config { config = Self::new(); } else if let Ok(data) = serde_json::from_str(&buf) { config = data; + } else if let Ok(data) = serde_json::from_str::(&buf) { + config = data.into(); } else { warn!( "Could not deserialise {}.\nWill rename to {}-old and recreate config", @@ -81,3 +86,22 @@ impl Config { .unwrap_or_else(|err| error!("Could not write config: {}", err)); } } + +#[derive(Deserialize, Serialize, Default)] +#[serde(default)] +pub struct Config455 { + /// Save charge limit for restoring on boot + pub bat_charge_limit: u8, + pub panel_od: bool, +} + +impl From for Config { + fn from(c: Config455) -> Self { + Self { + bat_charge_limit: c.bat_charge_limit, + panel_od: c.panel_od, + ac_command: String::new(), + bat_command: String::new(), + } + } +} diff --git a/daemon/src/ctrl_power.rs b/daemon/src/ctrl_power.rs index 2e58816b..26c83d14 100644 --- a/daemon/src/ctrl_power.rs +++ b/daemon/src/ctrl_power.rs @@ -2,9 +2,10 @@ use crate::systemd::{do_systemd_unit_action, SystemdUnitAction}; use crate::{config::Config, error::RogError, GetSupported}; use crate::{task_watch_item, CtrlTask}; use async_trait::async_trait; -use log::{info, warn}; +use log::{error, info, warn}; use rog_platform::power::AsusPower; use rog_platform::supported::ChargeSupportedFunctions; +use std::process::Command; use std::sync::Arc; use std::time::Duration; use tokio::time::sleep; @@ -210,6 +211,7 @@ impl CtrlTask for CtrlPower { ) .await; + let config = self.config.clone(); self.watch_charge_control_end_threshold(signal_ctxt.clone()) .await?; @@ -232,6 +234,31 @@ impl CtrlTask for CtrlPower { Self::notify_mains_online(&signal_ctxt, value == 1) .await .unwrap(); + + let mut config = config.lock().await; + config.read(); + let mut prog: Vec<&str> = Vec::new(); + if value == 1 { + // AC ONLINE + prog = config.ac_command.split_whitespace().collect(); + } else if value == 0 { + // BATTERY + prog = config.bat_command.split_whitespace().collect(); + } + + if prog.len() > 1 { + let mut cmd = Command::new(prog[0]); + for arg in prog.iter().skip(1) { + cmd.arg(*arg); + } + if let Err(e) = cmd.spawn() { + if value == 1 { + error!("AC power command error: {e}"); + } else { + error!("Battery power command error: {e}"); + } + } + } } } // The inotify doesn't pick up events when the kernel changes internal value