mirror of
https://gitlab.com/asus-linux/asusctl.git
synced 2026-02-06 00:15:04 +01:00
Cleaned up
This commit is contained in:
@@ -4,7 +4,11 @@ static LED_SET: [u8; 17] = [0x5d, 0xb5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
|
||||
|
||||
static KBD_BRIGHT_PATH: &str = "/sys/class/leds/asus::kbd_backlight/brightness";
|
||||
|
||||
use crate::{config_aura::AuraConfig, error::RogError, laptops::{ASUS_KEYBOARD_DEVICES, HELP_ADDRESS, LaptopLedData, laptop_data}};
|
||||
use crate::{
|
||||
config_aura::AuraConfig,
|
||||
error::RogError,
|
||||
laptops::{LaptopLedData, ASUS_KEYBOARD_DEVICES},
|
||||
};
|
||||
use log::{error, info, warn};
|
||||
use rog_types::{
|
||||
aura_modes::{AuraEffect, AuraModeNum},
|
||||
@@ -35,15 +39,19 @@ impl GetSupported for CtrlKbdBacklight {
|
||||
// let mode = <&str>::from(&<AuraModes>::from(*mode));
|
||||
let multizone_led_mode = false;
|
||||
let per_key_led_mode = false;
|
||||
let laptop = laptop_data();
|
||||
let stock_led_modes = if laptop.supported_modes().standard.is_empty() {
|
||||
None
|
||||
let laptop = LaptopLedData::get_data();
|
||||
let stock_led_modes = if let Some(data) = laptop {
|
||||
if data.standard.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(data.standard)
|
||||
}
|
||||
} else {
|
||||
Some(laptop.supported_modes().standard.clone())
|
||||
None
|
||||
};
|
||||
|
||||
LedSupportedFunctions {
|
||||
brightness_set: CtrlKbdBacklight::get_kbd_bright_path().is_ok(),
|
||||
brightness_set: CtrlKbdBacklight::get_kbd_bright_path().is_some(),
|
||||
stock_led_modes,
|
||||
multizone_led_mode,
|
||||
per_key_led_mode,
|
||||
@@ -254,7 +262,7 @@ impl CtrlKbdBacklight {
|
||||
// TODO: return error if *all* nodes are None
|
||||
let mut led_node = None;
|
||||
for prod in ASUS_KEYBOARD_DEVICES.iter() {
|
||||
match Self::get_node_failover(prod, Self::scan_led_node) {
|
||||
match Self::find_led_node(prod) {
|
||||
Ok(node) => {
|
||||
led_node = Some(node);
|
||||
break;
|
||||
@@ -263,7 +271,7 @@ impl CtrlKbdBacklight {
|
||||
}
|
||||
}
|
||||
|
||||
let bright_node = Self::get_kbd_bright_path().map_or_else(|_| None, Some);
|
||||
let bright_node = Self::get_kbd_bright_path();
|
||||
|
||||
if led_node.is_none() && bright_node.is_none() {
|
||||
return Err(RogError::MissingFunction(
|
||||
@@ -274,11 +282,9 @@ impl CtrlKbdBacklight {
|
||||
|
||||
if bright_node.is_none() {
|
||||
return Err(RogError::MissingFunction(
|
||||
"No brightness control, you may require a v5.11 series kernel or newer"
|
||||
.into(),
|
||||
"No brightness control, you may require a v5.11 series kernel or newer".into(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
let ctrl = CtrlKbdBacklight {
|
||||
led_node,
|
||||
@@ -290,14 +296,11 @@ impl CtrlKbdBacklight {
|
||||
Ok(ctrl)
|
||||
}
|
||||
|
||||
fn get_kbd_bright_path() -> Result<String, RogError> {
|
||||
fn get_kbd_bright_path() -> Option<String> {
|
||||
if Path::new(KBD_BRIGHT_PATH).exists() {
|
||||
Ok(KBD_BRIGHT_PATH.to_string())
|
||||
} else {
|
||||
Err(RogError::MissingFunction(
|
||||
"Keyboard features missing, you may require a v5.11 series kernel or newer".into(),
|
||||
))
|
||||
return Some(KBD_BRIGHT_PATH.to_string());
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn get_brightness(&self) -> Result<u8, RogError> {
|
||||
@@ -331,20 +334,7 @@ impl CtrlKbdBacklight {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_node_failover(
|
||||
id_product: &str,
|
||||
fun: fn(&str) -> Result<String, RogError>,
|
||||
) -> Result<String, RogError> {
|
||||
match fun(id_product) {
|
||||
Ok(o) => return Ok(o),
|
||||
Err(e) => {
|
||||
warn!("Looking for node: {}", e.to_string());
|
||||
}
|
||||
}
|
||||
Err(RogError::NotFound(format!("{}", id_product)))
|
||||
}
|
||||
|
||||
fn scan_led_node(id_product: &str) -> Result<String, RogError> {
|
||||
fn find_led_node(id_product: &str) -> Result<String, RogError> {
|
||||
let mut enumerator = udev::Enumerator::new().map_err(|err| {
|
||||
warn!("{}", err);
|
||||
RogError::Udev("enumerator failed".into(), err)
|
||||
@@ -377,7 +367,6 @@ impl CtrlKbdBacklight {
|
||||
}
|
||||
}
|
||||
}
|
||||
warn!("Did not find a hidraw node for LED control, your device may be unsupported or require a kernel patch, see: {}", HELP_ADDRESS);
|
||||
Err(RogError::MissingFunction(
|
||||
"ASUS LED device node not found".into(),
|
||||
))
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use daemon::{ctrl_fan_cpu::{CtrlFanAndCPU, DbusFanAndCpu}, laptops::laptop_data};
|
||||
use daemon::{ctrl_fan_cpu::{CtrlFanAndCPU, DbusFanAndCpu}, laptops::LaptopLedData};
|
||||
use daemon::ctrl_leds::{CtrlKbdBacklight, DbusKbdBacklight};
|
||||
use daemon::{
|
||||
config::Config, ctrl_supported::SupportedFunctions, laptops::print_board_info, GetSupported,
|
||||
@@ -135,12 +135,12 @@ fn start_daemon() -> Result<(), Box<dyn Error>> {
|
||||
DbusFanAndCpu::new(tmp).add_to_server(&mut object_server);
|
||||
};
|
||||
|
||||
let laptop = laptop_data();
|
||||
if !laptop.supported_modes().standard.is_empty() {
|
||||
let aura_config = AuraConfig::load(laptop.supported_modes());
|
||||
if let Some(laptop) = LaptopLedData::get_data() {
|
||||
if !laptop.standard.is_empty() {
|
||||
let aura_config = AuraConfig::load(&laptop);
|
||||
|
||||
if let Ok(ctrl) = CtrlKbdBacklight::new(
|
||||
laptop.supported_modes().to_owned(),
|
||||
laptop,
|
||||
aura_config,
|
||||
)
|
||||
.map_err(|err| {
|
||||
@@ -151,7 +151,7 @@ fn start_daemon() -> Result<(), Box<dyn Error>> {
|
||||
DbusKbdBacklight::new(tmp.clone()).add_to_server(&mut object_server);
|
||||
tasks.push(tmp);
|
||||
}
|
||||
}
|
||||
}}
|
||||
|
||||
// TODO: implement messaging between threads to check fails
|
||||
// These tasks generally read a sys path or file to check for a
|
||||
|
||||
@@ -4,45 +4,8 @@ use serde_derive::{Deserialize, Serialize};
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::Read;
|
||||
|
||||
pub const LEDMODE_CONFIG_PATH: &str = "/etc/asusd/asusd-ledmodes.toml";
|
||||
pub const HELP_ADDRESS: &str = "https://gitlab.com/asus-linux/asus-nb-ctrl";
|
||||
pub const ASUS_KEYBOARD_DEVICES: [&str; 4] = ["0x1866", "0x1869", "0x1854", "0x19b6"];
|
||||
|
||||
/// A helper of sorts specifically for functions tied to laptop models
|
||||
#[derive(Debug)]
|
||||
pub struct LaptopBase {
|
||||
led_support: LaptopLedData,
|
||||
}
|
||||
|
||||
impl LaptopBase {
|
||||
pub fn supported_modes(&self) -> &LaptopLedData {
|
||||
&self.led_support
|
||||
}
|
||||
}
|
||||
|
||||
pub fn laptop_data() -> LaptopBase {
|
||||
let dmi = sysfs_class::DmiId::default();
|
||||
let board_name = dmi.board_name().expect("Could not get board_name");
|
||||
let prod_family = dmi.product_family().expect("Could not get product_family");
|
||||
|
||||
let mut laptop = LaptopBase {
|
||||
led_support: LaptopLedData {
|
||||
board_names: vec![],
|
||||
prod_family: String::new(),
|
||||
standard: vec![],
|
||||
multizone: false,
|
||||
per_key: false,
|
||||
},
|
||||
};
|
||||
|
||||
if let Some(modes) = LedSupportFile::load_from_config() {
|
||||
if let Some(led_modes) = modes.matcher(&prod_family, &board_name) {
|
||||
laptop.led_support = led_modes;
|
||||
return laptop;
|
||||
}
|
||||
}
|
||||
laptop
|
||||
}
|
||||
pub const ASUS_LED_MODE_CONF: &str = "/etc/asusd/asusd-ledmodes.toml";
|
||||
pub const ASUS_KEYBOARD_DEVICES: [&str; 4] = ["1866", "1869", "1854", "19b6"];
|
||||
|
||||
pub fn print_board_info() {
|
||||
let dmi = sysfs_class::DmiId::default();
|
||||
@@ -63,8 +26,8 @@ pub fn print_modes(supported_modes: &[u8]) {
|
||||
info!("- {}", mode);
|
||||
}
|
||||
info!(
|
||||
"If these modes are incorrect or missing please request support at {}",
|
||||
HELP_ADDRESS
|
||||
"If these modes are incorrect you can edit {}",
|
||||
ASUS_LED_MODE_CONF
|
||||
);
|
||||
} else {
|
||||
info!("No RGB control available");
|
||||
@@ -85,6 +48,19 @@ pub struct LaptopLedData {
|
||||
pub per_key: bool,
|
||||
}
|
||||
|
||||
impl LaptopLedData {
|
||||
pub fn get_data() -> Option<Self> {
|
||||
let dmi = sysfs_class::DmiId::default();
|
||||
let board_name = dmi.board_name().expect("Could not get board_name");
|
||||
let prod_family = dmi.product_family().expect("Could not get product_family");
|
||||
|
||||
if let Some(modes) = LedSupportFile::load_from_config() {
|
||||
return modes.matcher(&prod_family, &board_name);
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl LedSupportFile {
|
||||
/// Consumes the LEDModes
|
||||
fn matcher(self, prod_family: &str, board_name: &str) -> Option<LaptopLedData> {
|
||||
@@ -102,19 +78,19 @@ impl LedSupportFile {
|
||||
}
|
||||
|
||||
fn load_from_config() -> Option<Self> {
|
||||
if let Ok(mut file) = OpenOptions::new().read(true).open(&LEDMODE_CONFIG_PATH) {
|
||||
if let Ok(mut file) = OpenOptions::new().read(true).open(&ASUS_LED_MODE_CONF) {
|
||||
let mut buf = String::new();
|
||||
if let Ok(l) = file.read_to_string(&mut buf) {
|
||||
if l == 0 {
|
||||
warn!("{} is empty", LEDMODE_CONFIG_PATH);
|
||||
warn!("{} is empty", ASUS_LED_MODE_CONF);
|
||||
} else {
|
||||
return Some(toml::from_str(&buf).unwrap_or_else(|_| {
|
||||
panic!("Could not deserialise {}", LEDMODE_CONFIG_PATH)
|
||||
panic!("Could not deserialise {}", ASUS_LED_MODE_CONF)
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
warn!("Does {} exist?", LEDMODE_CONFIG_PATH);
|
||||
warn!("Does {} exist?", ASUS_LED_MODE_CONF);
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user