Files
wldash/src/widgets/backlight.rs
T

60 lines
1.4 KiB
Rust
Raw Normal View History

2023-01-11 23:54:52 +01:00
use std::{
fs,
2023-01-12 00:13:27 +01:00
io::{Error, ErrorKind},
2023-01-11 23:54:52 +01:00
path::{Path, PathBuf},
};
use crate::{
color::Color,
2023-02-02 23:52:59 +01:00
fonts::FontMap,
2023-01-12 00:13:27 +01:00
widgets::bar_widget::{BarWidget, BarWidgetImpl},
2023-01-11 23:54:52 +01:00
};
fn read_file_as_u64(path: &Path) -> Result<u64, Error> {
let mut s = fs::read_to_string(path)?;
s.pop();
s.parse::<u64>()
.map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value"))
}
pub struct Backlight {
device_path: PathBuf,
cur: u64,
max: u64,
}
impl Backlight {
fn update(&mut self) {
self.cur = read_file_as_u64(self.device_path.join("brightness").as_path()).unwrap();
self.max = read_file_as_u64(self.device_path.join("max_brightness").as_path()).unwrap();
}
2023-02-02 23:52:59 +01:00
pub fn new(path: &str, fm: &mut FontMap, font: &'static str, size: f32) -> BarWidget {
2023-01-12 00:13:27 +01:00
let mut dev = Backlight {
2023-01-11 23:54:52 +01:00
device_path: Path::new("/sys/class/backlight").to_path_buf().join(path),
cur: 0,
max: 0,
};
dev.update();
2023-02-02 23:52:59 +01:00
BarWidget::new(Box::new(dev), fm, font, size)
2023-01-11 23:54:52 +01:00
}
}
impl BarWidgetImpl for Backlight {
fn name(&self) -> &'static str {
"backlight"
}
2023-02-06 18:34:07 +01:00
fn value(&mut self) -> f32 {
2023-01-11 23:54:52 +01:00
if self.cur > self.max {
// what.
return 1.0;
}
self.cur as f32 / self.max as f32
}
fn color(&self) -> Color {
2023-02-02 23:52:59 +01:00
Color::WHITE
2023-01-11 23:54:52 +01:00
}
}