Use hashset in aura power config

This commit is contained in:
Luke D. Jones
2022-07-15 09:30:10 +12:00
parent 95598f2a76
commit f39fd6dfbb
5 changed files with 16 additions and 20 deletions

View File

@@ -603,9 +603,7 @@ fn handle_fan_curve(
) -> Result<(), Box<dyn std::error::Error>> { ) -> Result<(), Box<dyn std::error::Error>> {
if !supported.fan_curves { if !supported.fan_curves {
println!("Fan-curves not supported by either this kernel or by the laptop."); println!("Fan-curves not supported by either this kernel or by the laptop.");
println!( println!("This requires kernel 5.17 or the fan curve patch listed in the readme.");
"This requires kernel 5.17 or the fan curve patch listed in the readme."
);
return Err(ProfileError::NotSupported.into()); return Err(ProfileError::NotSupported.into());
} }

View File

@@ -3,7 +3,7 @@ use log::{error, warn};
use rog_aura::usb::AuraControl; use rog_aura::usb::AuraControl;
use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness}; use rog_aura::{AuraEffect, AuraModeNum, AuraZone, LedBrightness};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use std::collections::BTreeMap; use std::collections::{BTreeMap, HashSet};
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::io::{Read, Write}; use std::io::{Read, Write};
@@ -16,7 +16,7 @@ pub struct AuraConfig {
pub current_mode: AuraModeNum, pub current_mode: AuraModeNum,
pub builtins: BTreeMap<AuraModeNum, AuraEffect>, pub builtins: BTreeMap<AuraModeNum, AuraEffect>,
pub multizone: Option<BTreeMap<AuraModeNum, Vec<AuraEffect>>>, pub multizone: Option<BTreeMap<AuraModeNum, Vec<AuraEffect>>>,
pub enabled: Vec<AuraControl>, pub enabled: HashSet<AuraControl>,
} }
impl Default for AuraConfig { impl Default for AuraConfig {
@@ -26,7 +26,7 @@ impl Default for AuraConfig {
current_mode: AuraModeNum::Static, current_mode: AuraModeNum::Static,
builtins: BTreeMap::new(), builtins: BTreeMap::new(),
multizone: None, multizone: None,
enabled: vec![ enabled: HashSet::from([
AuraControl::BootLogo, AuraControl::BootLogo,
AuraControl::BootKeyb, AuraControl::BootKeyb,
AuraControl::SleepLogo, AuraControl::SleepLogo,
@@ -39,7 +39,7 @@ impl Default for AuraConfig {
AuraControl::BootBar, AuraControl::BootBar,
AuraControl::SleepBar, AuraControl::SleepBar,
AuraControl::ShutdownBar, AuraControl::ShutdownBar,
], ]),
} }
} }
} }

View File

@@ -298,7 +298,8 @@ impl CtrlKbdLed {
/// Set combination state for boot animation/sleep animation/all leds/keys leds/side leds LED active /// 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> { pub(super) fn set_power_states(&self, config: &AuraConfig) -> Result<(), RogError> {
let bytes = AuraControl::to_bytes(&config.enabled); let set: Vec<AuraControl> = config.enabled.iter().map(|v| *v).collect();
let bytes = AuraControl::to_bytes(&set);
// Quite ugly, must be a more idiomatic way to do // Quite ugly, must be a more idiomatic way to do
let message = [ let message = [

View File

@@ -35,9 +35,7 @@ impl CtrlKbdLedZbus {
let mut states = None; let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() { if let Ok(mut ctrl) = self.0.try_lock() {
for s in enabled { for s in enabled {
if !ctrl.config.enabled.contains(&s) { ctrl.config.enabled.insert(s);
ctrl.config.enabled.push(s);
}
} }
ctrl.config.write(); ctrl.config.write();
@@ -45,7 +43,8 @@ impl CtrlKbdLedZbus {
.map_err(|err| warn!("{}", err)) .map_err(|err| warn!("{}", err))
.ok(); .ok();
states = Some(ctrl.config.enabled.clone()); let set: Vec<AuraControl> = ctrl.config.enabled.iter().map(|v| *v).collect();
states = Some(set);
} }
// Need to pull state out like this due to MutexGuard // Need to pull state out like this due to MutexGuard
if let Some(states) = states { if let Some(states) = states {
@@ -63,11 +62,7 @@ impl CtrlKbdLedZbus {
let mut states = None; let mut states = None;
if let Ok(mut ctrl) = self.0.try_lock() { if let Ok(mut ctrl) = self.0.try_lock() {
for s in disabled { for s in disabled {
if ctrl.config.enabled.contains(&s) { ctrl.config.enabled.remove(&s);
if let Ok(idx) = ctrl.config.enabled.binary_search(&s) {
ctrl.config.enabled.remove(idx);
}
}
} }
ctrl.config.write(); ctrl.config.write();
@@ -75,7 +70,8 @@ impl CtrlKbdLedZbus {
.map_err(|err| warn!("{}", err)) .map_err(|err| warn!("{}", err))
.ok(); .ok();
states = Some(ctrl.config.enabled.clone()); let set: Vec<AuraControl> = ctrl.config.enabled.iter().map(|v| *v).collect();
states = Some(set);
} }
// Need to pull state out like this due to MutexGuard // Need to pull state out like this due to MutexGuard
if let Some(states) = states { if let Some(states) = states {
@@ -161,7 +157,8 @@ impl CtrlKbdLedZbus {
#[dbus_interface(property)] #[dbus_interface(property)]
async fn leds_enabled(&self) -> Vec<u8> { async fn leds_enabled(&self) -> Vec<u8> {
if let Ok(ctrl) = self.0.try_lock() { if let Ok(ctrl) = self.0.try_lock() {
return AuraControl::to_bytes(&ctrl.config.enabled).to_vec(); let set: Vec<AuraControl> = ctrl.config.enabled.iter().map(|v| *v).collect();
return AuraControl::to_bytes(&set).to_vec();
} }
vec![0, 0] vec![0, 0]
} }

View File

@@ -21,7 +21,7 @@ pub const fn aura_brightness_bytes(brightness: u8) -> [u8; 17] {
} }
#[cfg_attr(feature = "dbus", derive(Type))] #[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)] #[repr(u16)]
pub enum AuraControl { pub enum AuraControl {
BootLogo = 1, BootLogo = 1,