From 1c48ab227df6fd35ae353f05e9db2bf504efcb75 Mon Sep 17 00:00:00 2001 From: Tony Dwire Date: Fri, 19 Mar 2021 22:24:59 -0500 Subject: [PATCH] Added --remove ability to profile subcommand --- asusctl/src/main.rs | 4 ++++ daemon/src/ctrl_fan_cpu.rs | 30 ++++++++++++++++++++++++++++-- rog-dbus/src/zbus_profile.rs | 8 ++++++++ rog-types/src/profile.rs | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 56aa8107..f72863f3 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -366,6 +366,7 @@ fn handle_profile( if !cmd.next && !cmd.create && !cmd.list + && cmd.remove.is_none() && cmd.curve.is_none() && cmd.max_percentage.is_none() && cmd.min_percentage.is_none() @@ -392,11 +393,14 @@ fn handle_profile( } std::process::exit(1); } + if cmd.next { dbus.proxies().profile().next_fan()?; } else if cmd.list { let profile_names = dbus.proxies().profile().profile_names()?; println!("Available profiles are {}", profile_names); + } else if let Some(profile) = &cmd.remove { + dbus.proxies().profile().remove(profile)? } else { dbus.proxies() .profile() diff --git a/daemon/src/ctrl_fan_cpu.rs b/daemon/src/ctrl_fan_cpu.rs index a397dad2..e5b62734 100644 --- a/daemon/src/ctrl_fan_cpu.rs +++ b/daemon/src/ctrl_fan_cpu.rs @@ -11,7 +11,7 @@ use std::io::Write; use std::path::Path; use std::sync::Arc; use std::sync::Mutex; -use zbus::dbus_interface; +use zbus::{dbus_interface, fdo::Error}; static FAN_TYPE_1_PATH: &str = "/sys/devices/platform/asus-nb-wmi/throttle_thermal_policy"; static FAN_TYPE_2_PATH: &str = "/sys/devices/platform/asus-nb-wmi/fan_boost_mode"; @@ -146,9 +146,35 @@ impl DbusFanAndCpu { "Failed".to_string() } + fn remove(&self, profile: &str) -> zbus::fdo::Result<()> { + if let Ok(ctrl) = self.inner.try_lock() { + if let Ok(mut cfg) = ctrl.config.try_lock() { + cfg.read(); + + if !cfg.power_profiles.contains_key(profile) { + return Err(Error::Failed("Invalid profile specified".to_string())); + } + + if cfg.power_profiles.keys().len() == 1 { + return Err(Error::Failed("Cannot delete the last profile".to_string())); + } + + if cfg.active_profile == *profile { + return Err(Error::Failed("Cannot delete the active profile".to_string())); + } + + cfg.power_profiles.remove(profile); + cfg.write(); + + return Ok(()); + } + } + + return Err(Error::Failed("Failed to lock configuration".to_string())); + } + #[dbus_interface(signal)] fn notify_profile(&self, profile: &str) -> zbus::Result<()> {} - } impl crate::ZbusAdd for DbusFanAndCpu { diff --git a/rog-dbus/src/zbus_profile.rs b/rog-dbus/src/zbus_profile.rs index 5e692916..504b10dc 100644 --- a/rog-dbus/src/zbus_profile.rs +++ b/rog-dbus/src/zbus_profile.rs @@ -44,6 +44,9 @@ trait Daemon { /// ProfileNames method fn profile_names(&self) -> zbus::Result; + /// Remove method + fn remove(&self, profile: &str) -> zbus::Result<()>; + /// SetProfile method fn set_profile(&self, profile: &str) -> zbus::Result<()>; @@ -90,6 +93,11 @@ impl<'a> ProfileProxy<'a> { self.0.profile_names() } + #[inline] + pub fn remove(&self, profile: &str) -> Result<()> { + self.0.remove(profile) + } + #[inline] pub fn connect_notify_profile( &self, diff --git a/rog-types/src/profile.rs b/rog-types/src/profile.rs index ea222cf4..415df1e7 100644 --- a/rog-types/src/profile.rs +++ b/rog-types/src/profile.rs @@ -100,4 +100,6 @@ pub struct ProfileCommand { pub curve: Option, #[options(free)] pub profile: Option, + #[options(help = "remove a profile by name")] + pub remove: Option, }