backlight: Auto-detect type

This commit is contained in:
Kenny Levinsen
2025-01-12 22:47:16 +01:00
parent 5776886884
commit f09abf1d53
2 changed files with 35 additions and 15 deletions
+12 -5
View File
@@ -87,7 +87,7 @@ pub struct Config {
impl Config { impl Config {
pub fn generate_v1() -> Config { pub fn generate_v1() -> Config {
let has_battery = Battery::detect(); let has_battery = Battery::detect();
let has_backlight = Backlight::detect("intel_backlight"); let has_backlight = Backlight::detect();
let sans = find_font("sans"); let sans = find_font("sans");
let monospace = find_font("monospace"); let monospace = find_font("monospace");
@@ -105,7 +105,7 @@ impl Config {
bars.push(Widget::Margin { bars.push(Widget::Margin {
margins: (0, 0, 0, 8), margins: (0, 0, 0, 8),
widget: Box::new(Widget::Backlight { widget: Box::new(Widget::Backlight {
device: Some("intel_backlight".to_string()), device: None,
font: None, font: None,
font_size: 24., font_size: 24.,
}), }),
@@ -168,7 +168,7 @@ impl Config {
pub fn generate_v2(clay: bool) -> Config { pub fn generate_v2(clay: bool) -> Config {
let has_battery = Battery::detect(); let has_battery = Battery::detect();
let has_backlight = Backlight::detect("intel_backlight"); let has_backlight = Backlight::detect();
let sans = find_font("sans"); let sans = find_font("sans");
let monospace = find_font("monospace"); let monospace = find_font("monospace");
@@ -186,7 +186,7 @@ impl Config {
bars.push(Widget::Margin { bars.push(Widget::Margin {
margins: (16, 8, 8, 0), margins: (16, 8, 8, 0),
widget: Box::new(Widget::Backlight { widget: Box::new(Widget::Backlight {
device: Some("intel_backlight".to_string()), device: None,
font: None, font: None,
font_size: 24., font_size: 24.,
}), }),
@@ -267,6 +267,13 @@ fn leak_or_default(v: Option<String>, d: &'static str) -> &'static str {
} }
} }
fn leak_or_none(v: Option<String>) -> Option<&'static str> {
match v {
Some(v) => Some(Box::leak(v.into_boxed_str())),
None => None,
}
}
impl Widget { impl Widget {
pub fn construct_widgets( pub fn construct_widgets(
self, self,
@@ -331,7 +338,7 @@ impl Widget {
font, font,
font_size, font_size,
} => v.push(Box::new(Backlight::new( } => v.push(Box::new(Backlight::new(
leak_or_default(device, "intel_backlight"), leak_or_none(device),
&mut fm, &mut fm,
leak_or_default(font, "sans"), leak_or_default(font, "sans"),
font_size, font_size,
+23 -10
View File
@@ -1,9 +1,11 @@
use std::{ use std::{
fs::{self, OpenOptions}, fs::{self, OpenOptions},
io::{Write, Error, ErrorKind}, io::{Error, ErrorKind, Write},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use walkdir::WalkDir;
use crate::{ use crate::{
color::Color, color::Color,
event::PointerButton, event::PointerButton,
@@ -27,7 +29,7 @@ pub struct Backlight {
device_path: PathBuf, device_path: PathBuf,
cur: u64, cur: u64,
max: u64, max: u64,
dirty: bool dirty: bool,
} }
impl Backlight { impl Backlight {
@@ -36,9 +38,19 @@ impl Backlight {
self.max = read_file_as_u64(self.device_path.join("max_brightness").as_path()).unwrap(); 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 { 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, cur: 0,
max: 0, max: 0,
dirty: true, dirty: true,
@@ -48,17 +60,18 @@ impl Backlight {
BarWidget::new(Box::new(dev), fm, font, size) BarWidget::new(Box::new(dev), fm, font, size)
} }
pub fn detect(path: &str) -> bool { pub fn detect() -> bool {
let path = Path::new("/sys/class/backlight").to_path_buf().join(path); for entry in WalkDir::new("/sys/class/backlight") {
match read_file_as_u64(path.join("brightness").as_path()) { if entry.is_ok() {
Ok(_) => true, return true;
Err(_) => false, }
} }
return false;
} }
pub fn set(&mut self, brightness: f32) { pub fn set(&mut self, brightness: f32) {
let val = (self.max as f32 * brightness) as u64; 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(); self.cur = read_file_as_u64(self.device_path.join("brightness").as_path()).unwrap();
} }
} }