From f39fd6dfbbfa4b6d95fac56e53c6d8c1e5528cca Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Fri, 15 Jul 2022 09:30:10 +1200 Subject: [PATCH] Use hashset in aura power config --- asusctl/src/main.rs | 4 +--- daemon/src/ctrl_aura/config.rs | 8 ++++---- daemon/src/ctrl_aura/controller.rs | 3 ++- daemon/src/ctrl_aura/zbus.rs | 19 ++++++++----------- rog-aura/src/usb.rs | 2 +- 5 files changed, 16 insertions(+), 20 deletions(-) diff --git a/asusctl/src/main.rs b/asusctl/src/main.rs index 8e158bc7..8702ce7f 100644 --- a/asusctl/src/main.rs +++ b/asusctl/src/main.rs @@ -603,9 +603,7 @@ fn handle_fan_curve( ) -> Result<(), Box> { if !supported.fan_curves { println!("Fan-curves not supported by either this kernel or by the laptop."); - println!( - "This requires kernel 5.17 or the fan curve patch listed in the readme." - ); + println!("This requires kernel 5.17 or the fan curve patch listed in the readme."); return Err(ProfileError::NotSupported.into()); } diff --git a/daemon/src/ctrl_aura/config.rs b/daemon/src/ctrl_aura/config.rs index bfde0275..7e7d9543 100644 --- a/daemon/src/ctrl_aura/config.rs +++ b/daemon/src/ctrl_aura/config.rs @@ -3,7 +3,7 @@ use log::{error, warn}; use rog_aura::usb::AuraControl; use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness}; use serde_derive::{Deserialize, Serialize}; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashSet}; use std::fs::{File, OpenOptions}; use std::io::{Read, Write}; @@ -16,7 +16,7 @@ pub struct AuraConfig { pub current_mode: AuraModeNum, pub builtins: BTreeMap, pub multizone: Option>>, - pub enabled: Vec, + pub enabled: HashSet, } impl Default for AuraConfig { @@ -26,7 +26,7 @@ impl Default for AuraConfig { current_mode: AuraModeNum::Static, builtins: BTreeMap::new(), multizone: None, - enabled: vec![ + enabled: HashSet::from([ AuraControl::BootLogo, AuraControl::BootKeyb, AuraControl::SleepLogo, @@ -39,7 +39,7 @@ impl Default for AuraConfig { AuraControl::BootBar, AuraControl::SleepBar, AuraControl::ShutdownBar, - ], + ]), } } } diff --git a/daemon/src/ctrl_aura/controller.rs b/daemon/src/ctrl_aura/controller.rs index 6665db8d..734bbb39 100644 --- a/daemon/src/ctrl_aura/controller.rs +++ b/daemon/src/ctrl_aura/controller.rs @@ -298,7 +298,8 @@ impl CtrlKbdLed { /// Set combination state for boot animation/sleep animation/all leds/keys leds/side leds LED active pub(super) fn set_power_states(&self, config: &AuraConfig) -> Result<(), RogError> { - let bytes = AuraControl::to_bytes(&config.enabled); + let set: Vec = config.enabled.iter().map(|v| *v).collect(); + let bytes = AuraControl::to_bytes(&set); // Quite ugly, must be a more idiomatic way to do let message = [ diff --git a/daemon/src/ctrl_aura/zbus.rs b/daemon/src/ctrl_aura/zbus.rs index 28fee02e..fadf1b9e 100644 --- a/daemon/src/ctrl_aura/zbus.rs +++ b/daemon/src/ctrl_aura/zbus.rs @@ -35,9 +35,7 @@ impl CtrlKbdLedZbus { let mut states = None; if let Ok(mut ctrl) = self.0.try_lock() { for s in enabled { - if !ctrl.config.enabled.contains(&s) { - ctrl.config.enabled.push(s); - } + ctrl.config.enabled.insert(s); } ctrl.config.write(); @@ -45,7 +43,8 @@ impl CtrlKbdLedZbus { .map_err(|err| warn!("{}", err)) .ok(); - states = Some(ctrl.config.enabled.clone()); + let set: Vec = ctrl.config.enabled.iter().map(|v| *v).collect(); + states = Some(set); } // Need to pull state out like this due to MutexGuard if let Some(states) = states { @@ -63,11 +62,7 @@ impl CtrlKbdLedZbus { let mut states = None; if let Ok(mut ctrl) = self.0.try_lock() { for s in disabled { - if ctrl.config.enabled.contains(&s) { - if let Ok(idx) = ctrl.config.enabled.binary_search(&s) { - ctrl.config.enabled.remove(idx); - } - } + ctrl.config.enabled.remove(&s); } ctrl.config.write(); @@ -75,7 +70,8 @@ impl CtrlKbdLedZbus { .map_err(|err| warn!("{}", err)) .ok(); - states = Some(ctrl.config.enabled.clone()); + let set: Vec = ctrl.config.enabled.iter().map(|v| *v).collect(); + states = Some(set); } // Need to pull state out like this due to MutexGuard if let Some(states) = states { @@ -161,7 +157,8 @@ impl CtrlKbdLedZbus { #[dbus_interface(property)] async fn leds_enabled(&self) -> Vec { if let Ok(ctrl) = self.0.try_lock() { - return AuraControl::to_bytes(&ctrl.config.enabled).to_vec(); + let set: Vec = ctrl.config.enabled.iter().map(|v| *v).collect(); + return AuraControl::to_bytes(&set).to_vec(); } vec![0, 0] } diff --git a/rog-aura/src/usb.rs b/rog-aura/src/usb.rs index 72719e02..fed74faa 100644 --- a/rog-aura/src/usb.rs +++ b/rog-aura/src/usb.rs @@ -21,7 +21,7 @@ pub const fn aura_brightness_bytes(brightness: u8) -> [u8; 17] { } #[cfg_attr(feature = "dbus", derive(Type))] -#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Eq, Ord, Hash, Serialize, Deserialize)] #[repr(u16)] pub enum AuraControl { BootLogo = 1,