Add basic event commands

This commit is contained in:
NGnius (Graham) 2023-05-28 10:34:20 -04:00
parent f8ea999604
commit 8c77630fb4
7 changed files with 81 additions and 1 deletions

View file

@ -290,6 +290,16 @@ impl ApiMessageHandler {
let settings_clone = settings.json(); let settings_clone = settings.json();
let save_json: SettingsJson = settings_clone.into(); let save_json: SettingsJson = settings_clone.into();
unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings"); unwrap_maybe_fatal(save_json.save(&save_path), "Failed to save settings");
if let Some(event) = &settings.general.on_event().on_save {
if !event.is_empty() {
unwrap_maybe_fatal(
std::process::Command::new("/bin/bash")
.args(&["-c", event])
.spawn(),
"Failed to start on_save event command",
);
}
}
log::debug!("Saved settings to {}", save_path.display()); log::debug!("Saved settings to {}", save_path.display());
if let Err(e) = crate::utility::chown_settings_dir() { if let Err(e) = crate::utility::chown_settings_dir() {
log::error!("Failed to change config dir permissions: {}", e); log::error!("Failed to change config dir permissions: {}", e);

View file

@ -5,6 +5,25 @@ use serde::{Deserialize, Serialize};
use super::JsonError; use super::JsonError;
use super::{BatteryJson, CpuJson, DriverJson, GpuJson}; use super::{BatteryJson, CpuJson, DriverJson, GpuJson};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct OnEventJson {
pub on_save: Option<String>,
pub on_load: Option<String>,
pub on_set: Option<String>,
pub on_resume: Option<String>,
}
impl Default for OnEventJson {
fn default() -> Self {
Self {
on_save: None,
on_load: None,
on_set: None,
on_resume: None,
}
}
}
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct SettingsJson { pub struct SettingsJson {
pub version: u64, pub version: u64,
@ -14,6 +33,7 @@ pub struct SettingsJson {
pub gpu: GpuJson, pub gpu: GpuJson,
pub battery: BatteryJson, pub battery: BatteryJson,
pub provider: Option<DriverJson>, pub provider: Option<DriverJson>,
pub events: Option<OnEventJson>,
} }
impl Default for SettingsJson { impl Default for SettingsJson {
@ -26,6 +46,7 @@ impl Default for SettingsJson {
gpu: GpuJson::default(), gpu: GpuJson::default(),
battery: BatteryJson::default(), battery: BatteryJson::default(),
provider: None, provider: None,
events: None,
} }
} }
} }

View file

@ -8,7 +8,7 @@ mod gpu;
pub use battery::{BatteryEventJson, BatteryJson}; pub use battery::{BatteryEventJson, BatteryJson};
pub use cpu::CpuJson; pub use cpu::CpuJson;
pub use driver::DriverJson; pub use driver::DriverJson;
pub use general::{MinMaxJson, SettingsJson}; pub use general::{MinMaxJson, OnEventJson, SettingsJson};
pub use gpu::GpuJson; pub use gpu::GpuJson;
pub use error::JsonError; pub use error::JsonError;

View file

@ -303,6 +303,7 @@ impl DriverBuilder {
path: json_path, path: json_path,
name: profile_name, name: profile_name,
driver: DriverJson::AutoDetect, driver: DriverJson::AutoDetect,
events: Default::default(),
}), }),
cpus: None, cpus: None,
gpu: None, gpu: None,

View file

@ -21,6 +21,7 @@ impl Driver {
path: json_path, path: json_path,
name: settings.name, name: settings.name,
driver: DriverJson::SteamDeck, driver: DriverJson::SteamDeck,
events: settings.events.unwrap_or_default(),
}), }),
cpus: Box::new(super::steam_deck::Cpus::from_json( cpus: Box::new(super::steam_deck::Cpus::from_json(
settings.cpus, settings.cpus,
@ -51,6 +52,7 @@ impl Driver {
path: json_path, path: json_path,
name: settings.name, name: settings.name,
driver: DriverJson::SteamDeck, driver: DriverJson::SteamDeck,
events: settings.events.unwrap_or_default(),
}), }),
cpus: Box::new(super::steam_deck::Cpus::from_json( cpus: Box::new(super::steam_deck::Cpus::from_json(
settings.cpus, settings.cpus,
@ -72,6 +74,7 @@ impl Driver {
path: json_path, path: json_path,
name: settings.name, name: settings.name,
driver: DriverJson::SteamDeckAdvance, driver: DriverJson::SteamDeckAdvance,
events: settings.events.unwrap_or_default(),
}), }),
cpus: Box::new(super::steam_deck::Cpus::from_json( cpus: Box::new(super::steam_deck::Cpus::from_json(
settings.cpus, settings.cpus,

View file

@ -34,16 +34,43 @@ pub struct General {
pub path: PathBuf, pub path: PathBuf,
pub name: String, pub name: String,
pub driver: crate::persist::DriverJson, pub driver: crate::persist::DriverJson,
pub events: crate::persist::OnEventJson,
} }
impl OnSet for General { impl OnSet for General {
fn on_set(&mut self) -> Result<(), Vec<SettingError>> { fn on_set(&mut self) -> Result<(), Vec<SettingError>> {
if let Some(event) = &self.events.on_set {
if !event.is_empty() {
std::process::Command::new("/bin/bash")
.args(&["-c", event])
.spawn()
.map_err(|e| {
vec![SettingError {
msg: format!("on_set event command error: {}", e),
setting: SettingVariant::General,
}]
})?;
}
}
Ok(()) Ok(())
} }
} }
impl OnResume for General { impl OnResume for General {
fn on_resume(&self) -> Result<(), Vec<SettingError>> { fn on_resume(&self) -> Result<(), Vec<SettingError>> {
if let Some(event) = &self.events.on_resume {
if !event.is_empty() {
std::process::Command::new("/bin/bash")
.args(&["-c", event])
.spawn()
.map_err(|e| {
vec![SettingError {
msg: format!("on_resume event command error: {}", e),
setting: SettingVariant::General,
}]
})?;
}
}
Ok(()) Ok(())
} }
} }
@ -82,6 +109,10 @@ impl TGeneral for General {
fn provider(&self) -> crate::persist::DriverJson { fn provider(&self) -> crate::persist::DriverJson {
self.driver.clone() self.driver.clone()
} }
fn on_event(&self) -> &crate::persist::OnEventJson {
&self.events
}
} }
#[derive(Debug)] #[derive(Debug)]
@ -214,6 +245,17 @@ impl Settings {
*self.general.persistent() = false; *self.general.persistent() = false;
} }
self.general.path(filename); self.general.path(filename);
if let Some(event) = &self.general.on_event().on_load {
if !event.is_empty() {
std::process::Command::new("/bin/bash")
.args(&["-c", event])
.spawn()
.map_err(|e| SettingError {
msg: format!("on_save event command error: {}", e),
setting: SettingVariant::General,
})?;
}
}
Ok(*self.general.persistent()) Ok(*self.general.persistent())
} }
@ -257,6 +299,7 @@ impl Settings {
gpu: self.gpu.json(), gpu: self.gpu.json(),
battery: self.battery.json(), battery: self.battery.json(),
provider: Some(self.general.provider()), provider: Some(self.general.provider()),
events: Some(self.general.on_event().clone()),
} }
} }
} }

View file

@ -104,6 +104,8 @@ pub trait TGeneral: OnSet + OnResume + OnPowerEvent + Debug + Send {
fn name(&mut self, name: String); fn name(&mut self, name: String);
fn provider(&self) -> crate::persist::DriverJson; fn provider(&self) -> crate::persist::DriverJson;
fn on_event(&self) -> &'_ crate::persist::OnEventJson;
} }
pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send { pub trait TBattery: OnSet + OnResume + OnPowerEvent + Debug + Send {