From de3b803f14cf6b505f1fb752af69676f7fb36743 Mon Sep 17 00:00:00 2001 From: Luke D Jones Date: Sun, 25 Oct 2020 15:43:33 +1300 Subject: [PATCH] Add DBUS methods to toggle next/previous aura mode --- CHANGELOG.md | 1 + README.md | 14 ++++++++ asus-nb-ctrl/src/ctrl_leds.rs | 62 ++++++++++++++++++++++++++++++++--- asus-nb-ctrl/src/main.rs | 15 +++++++-- asus-nb/src/core_dbus.rs | 24 +++++++++++++- asus-nb/src/dbus_ledmode.rs | 16 +++++++-- 6 files changed, 121 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03cd0cf7..a5767259 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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 +- Add DBUS method to toggle to next/prev Aura mode # [2.0.5] - 2020-09-29 ### Changed diff --git a/README.md b/README.md index 59d83ddd..459afd9b 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,20 @@ If you model isn't getting the correct led modes, you can edit the file use `cat /sys/class/dmi/id/product_name` to get details about your laptop. +# Keybinds + +To switch to next/previous Aura modes you will need to bind both the aura keys (if available) to one of: +**Next** +``` +asusctl led-mode -n +``` +**Previous** +``` +asusctl led-mode -p +``` + +To switch Fan/Thermal profiles you need to bind the Fn+F5 key to `asusctl profile -n`. + # BUILDING Requirements are: diff --git a/asus-nb-ctrl/src/ctrl_leds.rs b/asus-nb-ctrl/src/ctrl_leds.rs index be2eeeff..4a174577 100644 --- a/asus-nb-ctrl/src/ctrl_leds.rs +++ b/asus-nb-ctrl/src/ctrl_leds.rs @@ -72,6 +72,24 @@ impl DbusKbdBacklight { } } + fn next_led_mode(&self) { + if let Ok(mut ctrl) = self.inner.try_lock() { + if let Ok(mut cfg) = ctrl.config.clone().try_lock() { + ctrl.toggle_mode(false, &mut cfg) + .unwrap_or_else(|err| warn!("{}", err)); + } + } + } + + fn prev_led_mode(&self) { + if let Ok(mut ctrl) = self.inner.try_lock() { + if let Ok(mut cfg) = ctrl.config.clone().try_lock() { + ctrl.toggle_mode(true, &mut cfg) + .unwrap_or_else(|err| warn!("{}", err)); + } + } + } + /// Return the current mode data fn led_mode(&self) -> String { if let Ok(ctrl) = self.inner.try_lock() { @@ -165,11 +183,11 @@ impl crate::CtrlTask for CtrlKbdBacklight { let mut file = OpenOptions::new() .read(true) .open(&self.bright_node) - .map_err(|err| { - match err.kind() { - std::io::ErrorKind::NotFound => RogError::MissingLedBrightNode((&self.bright_node).into(), err), - _ => RogError::Path((&self.bright_node).into(), err), + .map_err(|err| match err.kind() { + std::io::ErrorKind::NotFound => { + RogError::MissingLedBrightNode((&self.bright_node).into(), err) } + _ => RogError::Path((&self.bright_node).into(), err), })?; let mut buf = [0u8; 1]; file.read_exact(&mut buf) @@ -324,7 +342,9 @@ impl CtrlKbdBacklight { fn write_bytes(&self, message: &[u8]) -> Result<(), RogError> { if let Some(led_node) = &self.led_node { if let Ok(mut file) = OpenOptions::new().write(true).open(led_node) { - return file.write_all(message).map_err(|err| RogError::Write("write_bytes".into(), err)); + return file + .write_all(message) + .map_err(|err| RogError::Write("write_bytes".into(), err)); } } Err(RogError::NotSupported) @@ -380,6 +400,38 @@ impl CtrlKbdBacklight { Ok(()) } + #[inline] + fn toggle_mode(&mut self, reverse: bool, config: &mut Config) -> Result<(), RogError> { + let current = config.kbd_backlight_mode; + if let Some(idx) = self.supported_modes.iter().position(|v| *v == current) { + + let mut idx = idx; + // goes past end of array + if reverse { + if idx == 0 { + idx = self.supported_modes.len() - 1; + } else { + idx -= 1; + } + } else { + idx += 1; + if idx == self.supported_modes.len() { + idx = 0; + } + } + let next = self.supported_modes[idx]; + + config.read(); + if let Some(data) = config.get_led_mode_data(next) { + self.write_mode(&data)?; + config.kbd_backlight_mode = next; + } + config.write(); + } + + Ok(()) + } + #[inline] fn write_mode(&mut self, mode: &AuraModes) -> Result<(), RogError> { match mode { diff --git a/asus-nb-ctrl/src/main.rs b/asus-nb-ctrl/src/main.rs index cce874c4..5ce75d59 100644 --- a/asus-nb-ctrl/src/main.rs +++ b/asus-nb-ctrl/src/main.rs @@ -44,7 +44,11 @@ enum CliCommand { struct LedModeCommand { #[options(help = "print help message")] help: bool, - #[options(command, required)] + #[options(help = "switch to next aura mode")] + next_mode: bool, + #[options(help = "switch to previous aura mode")] + prev_mode: bool, + #[options(command)] command: Option, } @@ -118,7 +122,14 @@ fn main() -> Result<(), Box> { match parsed.command { Some(CliCommand::LedMode(mode)) => { - if let Some(command) = mode.command { + if mode.next_mode && mode.prev_mode { + println!("Please specify either next or previous") + } + if mode.next_mode { + writer.next_keyboard_led_mode()?; + } else if mode.prev_mode { + writer.prev_keyboard_led_mode()?; + } else if let Some(command) = mode.command { writer.write_builtin_mode(&command.into())? } } diff --git a/asus-nb/src/core_dbus.rs b/asus-nb/src/core_dbus.rs index c946b1ae..723a08db 100644 --- a/asus-nb/src/core_dbus.rs +++ b/asus-nb/src/core_dbus.rs @@ -231,6 +231,28 @@ impl AuraDbusClient { Ok(()) } + #[inline] + pub fn next_keyboard_led_mode(&self) -> Result<(), Box> { + let proxy = self.connection.with_proxy( + "org.asuslinux.Daemon", + "/org/asuslinux/Led", + Duration::from_secs(2), + ); + proxy.next_led_mode()?; + Ok(()) + } + + #[inline] + pub fn prev_keyboard_led_mode(&self) -> Result<(), Box> { + let proxy = self.connection.with_proxy( + "org.asuslinux.Daemon", + "/org/asuslinux/Led", + Duration::from_secs(2), + ); + proxy.prev_led_mode()?; + Ok(()) + } + #[inline] pub fn get_gfx_pwr(&self) -> Result> { let proxy = self.connection.with_proxy( @@ -323,7 +345,7 @@ impl AuraDbusClient { "/org/asuslinux/Led", Duration::from_secs(2), ); - match proxy.led_bright()? { + match proxy.led_brightness()? { -1 => Ok(LedBrightness::new(None)), level => Ok(LedBrightness::new(Some(level as u8))), } diff --git a/asus-nb/src/dbus_ledmode.rs b/asus-nb/src/dbus_ledmode.rs index 73f2ab68..6005131f 100644 --- a/asus-nb/src/dbus_ledmode.rs +++ b/asus-nb/src/dbus_ledmode.rs @@ -6,9 +6,11 @@ use dbus::blocking; pub trait OrgAsuslinuxDaemon { fn set_led_mode(&self, data: &str) -> Result<(), dbus::Error>; + fn next_led_mode(&self) -> Result<(), dbus::Error>; + fn prev_led_mode(&self) -> Result<(), dbus::Error>; fn led_mode(&self) -> Result; fn led_modes(&self) -> Result; - fn led_bright(&self) -> Result; + fn led_brightness(&self) -> Result; } impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslinuxDaemon for blocking::Proxy<'a, C> { @@ -17,6 +19,14 @@ impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslin self.method_call("org.asuslinux.Daemon", "SetLedMode", (data, )) } + fn next_led_mode(&self) -> Result<(), dbus::Error> { + self.method_call("org.asuslinux.Daemon", "NextLedMode", ()) + } + + fn prev_led_mode(&self) -> Result<(), dbus::Error> { + self.method_call("org.asuslinux.Daemon", "PrevLedMode", ()) + } + fn led_mode(&self) -> Result { self.method_call("org.asuslinux.Daemon", "LedMode", ()) .and_then(|r: (String, )| Ok(r.0, )) @@ -27,8 +37,8 @@ impl<'a, T: blocking::BlockingSender, C: ::std::ops::Deref> OrgAsuslin .and_then(|r: (String, )| Ok(r.0, )) } - fn led_bright(&self) -> Result { - self.method_call("org.asuslinux.Daemon", "LedBright", ()) + fn led_brightness(&self) -> Result { + self.method_call("org.asuslinux.Daemon", "LedBrightness", ()) .and_then(|r: (i16, )| Ok(r.0, )) } }