From f09abf1d536dca48319dd3a781afba2567003ef4 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Sun, 12 Jan 2025 22:47:16 +0100 Subject: [PATCH] backlight: Auto-detect type --- src/config.rs | 17 ++++++++++++----- src/widgets/backlight.rs | 33 +++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/src/config.rs b/src/config.rs index 7bf9f1d..2322059 100644 --- a/src/config.rs +++ b/src/config.rs @@ -87,7 +87,7 @@ pub struct Config { impl Config { pub fn generate_v1() -> Config { let has_battery = Battery::detect(); - let has_backlight = Backlight::detect("intel_backlight"); + let has_backlight = Backlight::detect(); let sans = find_font("sans"); let monospace = find_font("monospace"); @@ -105,7 +105,7 @@ impl Config { bars.push(Widget::Margin { margins: (0, 0, 0, 8), widget: Box::new(Widget::Backlight { - device: Some("intel_backlight".to_string()), + device: None, font: None, font_size: 24., }), @@ -168,7 +168,7 @@ impl Config { pub fn generate_v2(clay: bool) -> Config { let has_battery = Battery::detect(); - let has_backlight = Backlight::detect("intel_backlight"); + let has_backlight = Backlight::detect(); let sans = find_font("sans"); let monospace = find_font("monospace"); @@ -186,7 +186,7 @@ impl Config { bars.push(Widget::Margin { margins: (16, 8, 8, 0), widget: Box::new(Widget::Backlight { - device: Some("intel_backlight".to_string()), + device: None, font: None, font_size: 24., }), @@ -267,6 +267,13 @@ fn leak_or_default(v: Option, d: &'static str) -> &'static str { } } +fn leak_or_none(v: Option) -> Option<&'static str> { + match v { + Some(v) => Some(Box::leak(v.into_boxed_str())), + None => None, + } +} + impl Widget { pub fn construct_widgets( self, @@ -331,7 +338,7 @@ impl Widget { font, font_size, } => v.push(Box::new(Backlight::new( - leak_or_default(device, "intel_backlight"), + leak_or_none(device), &mut fm, leak_or_default(font, "sans"), font_size, diff --git a/src/widgets/backlight.rs b/src/widgets/backlight.rs index 2edec0f..21853fd 100644 --- a/src/widgets/backlight.rs +++ b/src/widgets/backlight.rs @@ -1,9 +1,11 @@ use std::{ fs::{self, OpenOptions}, - io::{Write, Error, ErrorKind}, + io::{Error, ErrorKind, Write}, path::{Path, PathBuf}, }; +use walkdir::WalkDir; + use crate::{ color::Color, event::PointerButton, @@ -27,7 +29,7 @@ pub struct Backlight { device_path: PathBuf, cur: u64, max: u64, - dirty: bool + dirty: bool, } impl Backlight { @@ -36,9 +38,19 @@ impl Backlight { self.max = read_file_as_u64(self.device_path.join("max_brightness").as_path()).unwrap(); } - pub fn new(path: &str, fm: &mut FontMap, font: &'static str, size: f32) -> BarWidget { + pub fn new(path: Option<&str>, fm: &mut FontMap, font: &'static str, size: f32) -> BarWidget { + let mut device_path = None; + if let Some(path) = path { + device_path = Some(Path::new("/sys/class/backlight").to_path_buf().join(path)); + } else { + for entry in WalkDir::new("/sys/class/backlight") { + if let Ok(entry) = entry { + device_path = Some(entry.path().to_path_buf()); + } + } + } let mut dev = Backlight { - device_path: Path::new("/sys/class/backlight").to_path_buf().join(path), + device_path: device_path.expect("did not find backlight device"), cur: 0, max: 0, dirty: true, @@ -48,17 +60,18 @@ impl Backlight { BarWidget::new(Box::new(dev), fm, font, size) } - pub fn detect(path: &str) -> bool { - let path = Path::new("/sys/class/backlight").to_path_buf().join(path); - match read_file_as_u64(path.join("brightness").as_path()) { - Ok(_) => true, - Err(_) => false, + pub fn detect() -> bool { + for entry in WalkDir::new("/sys/class/backlight") { + if entry.is_ok() { + return true; + } } + return false; } pub fn set(&mut self, brightness: f32) { let val = (self.max as f32 * brightness) as u64; - write_file_as_u64(self.device_path.join("brightness").as_path(), val); + let _ = write_file_as_u64(self.device_path.join("brightness").as_path(), val); self.cur = read_file_as_u64(self.device_path.join("brightness").as_path()).unwrap(); } }