From 0558f919c45e1125486d628f5a4ea748ffa544b0 Mon Sep 17 00:00:00 2001 From: Luke D Jones Date: Sun, 25 Oct 2020 15:02:35 +1300 Subject: [PATCH] Add DBUS method to toggle to next profile --- CHANGELOG.md | 9 +++++++ Cargo.lock | 4 +-- README_DBUS.md | 6 +++++ asus-nb-ctrl/Cargo.toml | 2 +- asus-nb-ctrl/src/ctrl_fan_cpu.rs | 24 ++++++++++++++++-- asus-nb-ctrl/src/ctrl_leds.rs | 5 +++- asus-nb-ctrl/src/main.rs | 42 ++++++++++++-------------------- asus-nb/Cargo.toml | 2 +- asus-nb/src/core_dbus.rs | 11 +++++++++ asus-nb/src/dbus_profile.rs | 5 ++++ asus-nb/src/profile.rs | 2 ++ 11 files changed, 79 insertions(+), 33 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 40233c6e..03cd0cf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +# [2.1.0] - 2020-10-25 +### Added +- Option to turn off AniMe display (@asere) +### Changed +- Change option -k to show current LED bright (@asere) +- Correctly disable GFX control via config +- Panic and exit if config can't be parsed +- Add DBUS method to toggle to next fan/thermal profile + # [2.0.5] - 2020-09-29 ### Changed - Bugfixes diff --git a/Cargo.lock b/Cargo.lock index eeb260f3..05ee93b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,7 +29,7 @@ checksum = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" [[package]] name = "asus-nb" -version = "2.0.5" +version = "2.1.0" dependencies = [ "ctrl-gfx", "dbus", @@ -46,7 +46,7 @@ dependencies = [ [[package]] name = "asus-nb-ctrl" -version = "2.0.5" +version = "2.1.0" dependencies = [ "asus-nb", "ctrl-gfx", diff --git a/README_DBUS.md b/README_DBUS.md index b082279a..618ef6f5 100644 --- a/README_DBUS.md +++ b/README_DBUS.md @@ -76,6 +76,12 @@ Accepts an integer from the following: - `1`: Boost mode - `2`: Silent mode +## dbus-send examples: + +``` +dbus-send --system --type=method_call --dest=org.asuslinux.Daemon /org/asuslinux/Profile org.asuslinux.Daemon.NextProfile +``` + ## dbus-send examples OUTDATED ``` diff --git a/asus-nb-ctrl/Cargo.toml b/asus-nb-ctrl/Cargo.toml index f9621f37..e1993217 100644 --- a/asus-nb-ctrl/Cargo.toml +++ b/asus-nb-ctrl/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "asus-nb-ctrl" -version = "2.0.5" +version = "2.1.0" license = "MPL-2.0" readme = "README.md" authors = ["Luke "] diff --git a/asus-nb-ctrl/src/ctrl_fan_cpu.rs b/asus-nb-ctrl/src/ctrl_fan_cpu.rs index c4169604..efbeca19 100644 --- a/asus-nb-ctrl/src/ctrl_fan_cpu.rs +++ b/asus-nb-ctrl/src/ctrl_fan_cpu.rs @@ -31,6 +31,7 @@ impl DbusFanAndCpu { #[dbus_interface(name = "org.asuslinux.Daemon")] impl DbusFanAndCpu { + /// Set profile details fn set_profile(&self, profile: String) { if let Ok(event) = serde_json::from_str(&profile) { if let Ok(mut ctrl) = self.inner.try_lock() { @@ -45,6 +46,23 @@ impl DbusFanAndCpu { } } + /// Fetch the active profile name + fn next_profile(&mut self) { + if let Ok(mut ctrl) = self.inner.try_lock() { + if let Ok(mut cfg) = ctrl.config.clone().try_lock() { + ctrl.do_next_profile(&mut cfg) + .unwrap_or_else(|err| warn!("{}", err)); + if let Some(profile) = cfg.power_profiles.get(&cfg.active_profile) { + if let Ok(json) = serde_json::to_string(profile) { + self.notify_profile(&json) + .unwrap_or_else(|err| warn!("{}", err)); + } + } + } + } + } + + /// Fetch the active profile name fn active_profile_name(&mut self) -> String { if let Ok(ctrl) = self.inner.try_lock() { if let Ok(mut cfg) = ctrl.config.try_lock() { @@ -55,6 +73,7 @@ impl DbusFanAndCpu { "Failed".to_string() } + /// Fetch the active profile details fn profile(&mut self) -> String { if let Ok(ctrl) = self.inner.try_lock() { if let Ok(mut cfg) = ctrl.config.try_lock() { @@ -178,7 +197,8 @@ impl CtrlFanAndCPU { } } - pub(super) fn do_update(&mut self, config: &mut Config) -> Result<(), RogError> { + /// Toggle to next profile in list + pub(super) fn do_next_profile(&mut self, config: &mut Config) -> Result<(), RogError> { config.read(); let mut i = config @@ -232,7 +252,7 @@ impl CtrlFanAndCPU { config: &mut Config, ) -> Result<(), RogError> { match event { - ProfileEvent::Toggle => self.do_update(config)?, + ProfileEvent::Toggle => self.do_next_profile(config)?, ProfileEvent::ChangeMode(mode) => { self.set_fan_mode(*mode, config)?; } diff --git a/asus-nb-ctrl/src/ctrl_leds.rs b/asus-nb-ctrl/src/ctrl_leds.rs index 8c820f1b..be2eeeff 100644 --- a/asus-nb-ctrl/src/ctrl_leds.rs +++ b/asus-nb-ctrl/src/ctrl_leds.rs @@ -72,6 +72,7 @@ impl DbusKbdBacklight { } } + /// Return the current mode data fn led_mode(&self) -> String { if let Ok(ctrl) = self.inner.try_lock() { if let Ok(cfg) = ctrl.config.clone().try_lock() { @@ -86,6 +87,7 @@ impl DbusKbdBacklight { "SetKeyBacklight could not deserialise".to_string() } + /// Return a list of available modes fn led_modes(&self) -> String { if let Ok(ctrl) = self.inner.try_lock() { if let Ok(cfg) = ctrl.config.clone().try_lock() { @@ -98,7 +100,8 @@ impl DbusKbdBacklight { "SetKeyBacklight could not deserialise".to_string() } - fn led_bright(&self) -> i8 { + /// Return the current LED brightness + fn led_brightness(&self) -> i8 { if let Ok(ctrl) = self.inner.try_lock() { if let Ok(cfg) = ctrl.config.clone().try_lock() { return cfg.kbd_led_brightness as i8; diff --git a/asus-nb-ctrl/src/main.rs b/asus-nb-ctrl/src/main.rs index 280d8ee1..cce874c4 100644 --- a/asus-nb-ctrl/src/main.rs +++ b/asus-nb-ctrl/src/main.rs @@ -1,25 +1,14 @@ use asus_nb::{ - cli_options::{ - LedBrightness, - SetAuraBuiltin, - AniMeActions, - }, - core_dbus::AuraDbusClient, anime_dbus::AniMeDbusWriter, + cli_options::{AniMeActions, LedBrightness, SetAuraBuiltin}, + core_dbus::AuraDbusClient, profile::{ProfileCommand, ProfileEvent}, }; use ctrl_gfx::vendors::GfxVendors; use daemon::ctrl_fan_cpu::FanLevel; -use gumdrop::{ - Opt, - Options, -}; +use gumdrop::{Opt, Options}; use log::LevelFilter; -use std::{ - env::args, - io::Write, - process::Command, -}; +use std::{env::args, io::Write, process::Command}; use yansi_term::Colour::Green; use yansi_term::Colour::Red; @@ -77,11 +66,9 @@ struct GraphicsCommand { struct AniMeCommand { #[options(help = "print help message")] help: bool, - #[options(help = "turn on the panel (and accept write requests)", - no_short)] + #[options(help = "turn on the panel (and accept write requests)", no_short)] on: bool, - #[options(help = "turn off the panel (and reject write requests)", - no_short)] + #[options(help = "turn off the panel (and reject write requests)", no_short)] off: bool, #[options(command)] command: Option, @@ -95,10 +82,10 @@ fn main() -> Result<(), Box> { .filter(None, LevelFilter::Info) .init(); - let mut args : Vec = args().collect(); + let mut args: Vec = args().collect(); args.remove(0); - let parsed : CLIStart; + let parsed: CLIStart; let missing_argument_k = gumdrop::Error::missing_argument(Opt::Short('k')); match CLIStart::parse_args_default(&args) { Ok(p) => { @@ -136,7 +123,11 @@ fn main() -> Result<(), Box> { } } Some(CliCommand::Profile(command)) => { - writer.write_profile_command(&ProfileEvent::Cli(command))? + if command.next { + writer.next_fan_profile()?; + } else { + writer.write_profile_command(&ProfileEvent::Cli(command))? + } } Some(CliCommand::Graphics(command)) => do_gfx(command, &writer)?, Some(CliCommand::AniMe(anime)) => { @@ -153,16 +144,15 @@ fn main() -> Result<(), Box> { } } } - None => () + None => (), } if let Some(brightness) = parsed.kbd_bright { match brightness.level() { None => { let level = writer.get_led_brightness()?; - println!("Current keyboard led brightness: {}", - level.to_string()); - }, + println!("Current keyboard led brightness: {}", level.to_string()); + } Some(level) => writer.write_brightness(level)?, } } diff --git a/asus-nb/Cargo.toml b/asus-nb/Cargo.toml index a182bfac..04b384eb 100644 --- a/asus-nb/Cargo.toml +++ b/asus-nb/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "asus-nb" -version = "2.0.5" +version = "2.1.0" license = "MPL-2.0" readme = "README.md" authors = ["Luke "] diff --git a/asus-nb/src/core_dbus.rs b/asus-nb/src/core_dbus.rs index b20c51c5..c946b1ae 100644 --- a/asus-nb/src/core_dbus.rs +++ b/asus-nb/src/core_dbus.rs @@ -264,6 +264,17 @@ impl AuraDbusClient { Ok(()) } + #[inline] + pub fn next_fan_profile(&self) -> Result<(), Box> { + let proxy = self.connection.with_proxy( + "org.asuslinux.Daemon", + "/org/asuslinux/Profile", + Duration::from_secs(2), + ); + proxy.next_profile()?; + Ok(()) + } + #[inline] pub fn write_fan_mode(&self, level: u8) -> Result<(), Box> { let proxy = self.connection.with_proxy( diff --git a/asus-nb/src/dbus_profile.rs b/asus-nb/src/dbus_profile.rs index f2457a75..de7d2f5e 100644 --- a/asus-nb/src/dbus_profile.rs +++ b/asus-nb/src/dbus_profile.rs @@ -6,6 +6,7 @@ use dbus::blocking; pub trait OrgAsuslinuxDaemon { fn set_profile(&self, profile: &str) -> Result<(), dbus::Error>; + fn next_profile(&self) -> Result<(), dbus::Error>; fn active_profile_name(&self) -> Result; fn profile(&self) -> Result; fn profiles(&self) -> Result; @@ -17,6 +18,10 @@ impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslin self.method_call("org.asuslinux.Daemon", "SetProfile", (profile, )) } + fn next_profile(&self) -> Result<(), dbus::Error> { + self.method_call("org.asuslinux.Daemon", "NextProfile", ()) + } + fn active_profile_name(&self) -> Result { self.method_call("org.asuslinux.Daemon", "ActiveProfileName", ()) .and_then(|r: (String, )| Ok(r.0, )) diff --git a/asus-nb/src/profile.rs b/asus-nb/src/profile.rs index f327dc63..c725fdbe 100644 --- a/asus-nb/src/profile.rs +++ b/asus-nb/src/profile.rs @@ -76,6 +76,8 @@ fn parse_fan_curve(data: &str) -> Result { pub struct ProfileCommand { #[options(help = "print help message")] help: bool, + #[options(help = "toggle to next profile in list")] + pub next: bool, #[options(help = "create the profile if it doesn't exist")] pub create: bool,