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";
|
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 log::{error, info, warn};
|
||||||
use rog_types::{
|
use rog_types::{
|
||||||
aura_modes::{AuraEffect, AuraModeNum},
|
aura_modes::{AuraEffect, AuraModeNum},
|
||||||
@@ -35,15 +39,19 @@ impl GetSupported for CtrlKbdBacklight {
|
|||||||
// let mode = <&str>::from(&<AuraModes>::from(*mode));
|
// let mode = <&str>::from(&<AuraModes>::from(*mode));
|
||||||
let multizone_led_mode = false;
|
let multizone_led_mode = false;
|
||||||
let per_key_led_mode = false;
|
let per_key_led_mode = false;
|
||||||
let laptop = laptop_data();
|
let laptop = LaptopLedData::get_data();
|
||||||
let stock_led_modes = if laptop.supported_modes().standard.is_empty() {
|
let stock_led_modes = if let Some(data) = laptop {
|
||||||
None
|
if data.standard.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(data.standard)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
Some(laptop.supported_modes().standard.clone())
|
None
|
||||||
};
|
};
|
||||||
|
|
||||||
LedSupportedFunctions {
|
LedSupportedFunctions {
|
||||||
brightness_set: CtrlKbdBacklight::get_kbd_bright_path().is_ok(),
|
brightness_set: CtrlKbdBacklight::get_kbd_bright_path().is_some(),
|
||||||
stock_led_modes,
|
stock_led_modes,
|
||||||
multizone_led_mode,
|
multizone_led_mode,
|
||||||
per_key_led_mode,
|
per_key_led_mode,
|
||||||
@@ -254,7 +262,7 @@ impl CtrlKbdBacklight {
|
|||||||
// TODO: return error if *all* nodes are None
|
// TODO: return error if *all* nodes are None
|
||||||
let mut led_node = None;
|
let mut led_node = None;
|
||||||
for prod in ASUS_KEYBOARD_DEVICES.iter() {
|
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) => {
|
Ok(node) => {
|
||||||
led_node = Some(node);
|
led_node = Some(node);
|
||||||
break;
|
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() {
|
if led_node.is_none() && bright_node.is_none() {
|
||||||
return Err(RogError::MissingFunction(
|
return Err(RogError::MissingFunction(
|
||||||
@@ -274,11 +282,9 @@ impl CtrlKbdBacklight {
|
|||||||
|
|
||||||
if bright_node.is_none() {
|
if bright_node.is_none() {
|
||||||
return Err(RogError::MissingFunction(
|
return Err(RogError::MissingFunction(
|
||||||
"No brightness control, you may require a v5.11 series kernel or newer"
|
"No brightness control, you may require a v5.11 series kernel or newer".into(),
|
||||||
.into(),
|
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
let ctrl = CtrlKbdBacklight {
|
let ctrl = CtrlKbdBacklight {
|
||||||
led_node,
|
led_node,
|
||||||
@@ -290,14 +296,11 @@ impl CtrlKbdBacklight {
|
|||||||
Ok(ctrl)
|
Ok(ctrl)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_kbd_bright_path() -> Result<String, RogError> {
|
fn get_kbd_bright_path() -> Option<String> {
|
||||||
if Path::new(KBD_BRIGHT_PATH).exists() {
|
if Path::new(KBD_BRIGHT_PATH).exists() {
|
||||||
Ok(KBD_BRIGHT_PATH.to_string())
|
return Some(KBD_BRIGHT_PATH.to_string());
|
||||||
} else {
|
|
||||||
Err(RogError::MissingFunction(
|
|
||||||
"Keyboard features missing, you may require a v5.11 series kernel or newer".into(),
|
|
||||||
))
|
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_brightness(&self) -> Result<u8, RogError> {
|
pub fn get_brightness(&self) -> Result<u8, RogError> {
|
||||||
@@ -331,20 +334,7 @@ impl CtrlKbdBacklight {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_node_failover(
|
fn find_led_node(id_product: &str) -> Result<String, RogError> {
|
||||||
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> {
|
|
||||||
let mut enumerator = udev::Enumerator::new().map_err(|err| {
|
let mut enumerator = udev::Enumerator::new().map_err(|err| {
|
||||||
warn!("{}", err);
|
warn!("{}", err);
|
||||||
RogError::Udev("enumerator failed".into(), 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(
|
Err(RogError::MissingFunction(
|
||||||
"ASUS LED device node not found".into(),
|
"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::ctrl_leds::{CtrlKbdBacklight, DbusKbdBacklight};
|
||||||
use daemon::{
|
use daemon::{
|
||||||
config::Config, ctrl_supported::SupportedFunctions, laptops::print_board_info, GetSupported,
|
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);
|
DbusFanAndCpu::new(tmp).add_to_server(&mut object_server);
|
||||||
};
|
};
|
||||||
|
|
||||||
let laptop = laptop_data();
|
if let Some(laptop) = LaptopLedData::get_data() {
|
||||||
if !laptop.supported_modes().standard.is_empty() {
|
if !laptop.standard.is_empty() {
|
||||||
let aura_config = AuraConfig::load(laptop.supported_modes());
|
let aura_config = AuraConfig::load(&laptop);
|
||||||
|
|
||||||
if let Ok(ctrl) = CtrlKbdBacklight::new(
|
if let Ok(ctrl) = CtrlKbdBacklight::new(
|
||||||
laptop.supported_modes().to_owned(),
|
laptop,
|
||||||
aura_config,
|
aura_config,
|
||||||
)
|
)
|
||||||
.map_err(|err| {
|
.map_err(|err| {
|
||||||
@@ -151,7 +151,7 @@ fn start_daemon() -> Result<(), Box<dyn Error>> {
|
|||||||
DbusKbdBacklight::new(tmp.clone()).add_to_server(&mut object_server);
|
DbusKbdBacklight::new(tmp.clone()).add_to_server(&mut object_server);
|
||||||
tasks.push(tmp);
|
tasks.push(tmp);
|
||||||
}
|
}
|
||||||
}
|
}}
|
||||||
|
|
||||||
// TODO: implement messaging between threads to check fails
|
// TODO: implement messaging between threads to check fails
|
||||||
// These tasks generally read a sys path or file to check for a
|
// 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::fs::OpenOptions;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
pub const LEDMODE_CONFIG_PATH: &str = "/etc/asusd/asusd-ledmodes.toml";
|
pub const ASUS_LED_MODE_CONF: &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] = ["1866", "1869", "1854", "19b6"];
|
||||||
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 fn print_board_info() {
|
pub fn print_board_info() {
|
||||||
let dmi = sysfs_class::DmiId::default();
|
let dmi = sysfs_class::DmiId::default();
|
||||||
@@ -63,8 +26,8 @@ pub fn print_modes(supported_modes: &[u8]) {
|
|||||||
info!("- {}", mode);
|
info!("- {}", mode);
|
||||||
}
|
}
|
||||||
info!(
|
info!(
|
||||||
"If these modes are incorrect or missing please request support at {}",
|
"If these modes are incorrect you can edit {}",
|
||||||
HELP_ADDRESS
|
ASUS_LED_MODE_CONF
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
info!("No RGB control available");
|
info!("No RGB control available");
|
||||||
@@ -85,6 +48,19 @@ pub struct LaptopLedData {
|
|||||||
pub per_key: bool,
|
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 {
|
impl LedSupportFile {
|
||||||
/// Consumes the LEDModes
|
/// Consumes the LEDModes
|
||||||
fn matcher(self, prod_family: &str, board_name: &str) -> Option<LaptopLedData> {
|
fn matcher(self, prod_family: &str, board_name: &str) -> Option<LaptopLedData> {
|
||||||
@@ -102,19 +78,19 @@ impl LedSupportFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn load_from_config() -> Option<Self> {
|
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();
|
let mut buf = String::new();
|
||||||
if let Ok(l) = file.read_to_string(&mut buf) {
|
if let Ok(l) = file.read_to_string(&mut buf) {
|
||||||
if l == 0 {
|
if l == 0 {
|
||||||
warn!("{} is empty", LEDMODE_CONFIG_PATH);
|
warn!("{} is empty", ASUS_LED_MODE_CONF);
|
||||||
} else {
|
} else {
|
||||||
return Some(toml::from_str(&buf).unwrap_or_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
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user