backlight: Add
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
use std::{
|
||||
fs,
|
||||
io::{ErrorKind, Error},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
color::Color,
|
||||
widgets::bar_widget::{
|
||||
BarWidget,
|
||||
BarWidgetImpl,
|
||||
},
|
||||
};
|
||||
|
||||
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,
|
||||
dirty: bool,
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
pub fn new(path: &str) -> BarWidget {
|
||||
let mut dev = Backlight{
|
||||
device_path: Path::new("/sys/class/backlight").to_path_buf().join(path),
|
||||
cur: 0,
|
||||
max: 0,
|
||||
dirty: false,
|
||||
};
|
||||
|
||||
dev.update();
|
||||
BarWidget::new(Box::new(dev))
|
||||
}
|
||||
}
|
||||
|
||||
impl BarWidgetImpl for Backlight {
|
||||
fn get_dirty(&self) -> bool {
|
||||
self.dirty
|
||||
}
|
||||
fn set_dirty(&mut self, dirty: bool) {
|
||||
self.dirty = dirty
|
||||
}
|
||||
fn name(&self) -> &'static str {
|
||||
"backlight"
|
||||
}
|
||||
fn value(&self) -> f32 {
|
||||
if self.cur > self.max {
|
||||
// what.
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
self.cur as f32 / self.max as f32
|
||||
}
|
||||
fn color(&self) -> Color {
|
||||
Color::new(1.0, 1.0, 1.0, 1.0)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod bar_widget;
|
||||
mod battery;
|
||||
mod backlight;
|
||||
mod clock;
|
||||
mod date;
|
||||
mod launcher;
|
||||
@@ -7,6 +8,7 @@ mod line;
|
||||
mod widget;
|
||||
|
||||
pub use battery::Battery;
|
||||
pub use backlight::Backlight;
|
||||
pub use clock::Clock;
|
||||
pub use date::Date;
|
||||
pub use launcher::Interface;
|
||||
|
||||
Reference in New Issue
Block a user