backlight: Add

This commit is contained in:
Kenny Levinsen
2023-01-11 23:54:52 +01:00
parent dd790468ad
commit 25644c2b15
3 changed files with 86 additions and 6 deletions
+15 -6
View File
@@ -30,7 +30,7 @@ use color::Color;
use fonts::FontMap;
use keyboard::Keyboard;
use widgets::{
Battery, Clock, Date, Geometry, HorizontalLayout, IndexedLayout, Interface, Layout, Line,
Battery, Backlight, Clock, Date, Geometry, HorizontalLayout, IndexedLayout, Interface, Layout, Line,
Margin, VerticalLayout, Widget, WidgetUpdater,
};
@@ -150,6 +150,7 @@ fn main() {
let line = Box::new(Line::new());
let launcher = Box::new(Interface::new());
let battery = Box::new(Battery::new(ping_sender));
let backlight = Box::new(Backlight::new("intel_backlight"));
let mut event_loop: EventLoop<State> =
EventLoop::try_new().expect("Failed to initialize the event loop!");
@@ -200,7 +201,7 @@ fn main() {
configured: false,
dimensions: (320, 240),
bufmgr: bufmgr,
widgets: vec![clock, date, battery, line, launcher],
widgets: vec![clock, date, battery, backlight, line, launcher],
keyboard: Keyboard::new(),
fonts: MaybeFontMap::Waiting(font_thread),
};
@@ -474,14 +475,22 @@ impl Dispatch<xdg_toplevel::XdgToplevel, ()> for State {
widget: Box::new(IndexedLayout { widget_idx: 1 }),
margin: (16, 8, 0, 0),
}),
Box::new(Margin {
widget: Box::new(IndexedLayout { widget_idx: 2 }),
margin: (16, 8, 8, 0),
Box::new(VerticalLayout{
widgets: vec![
Box::new(Margin {
widget: Box::new(IndexedLayout { widget_idx: 2 }),
margin: (16, 8, 8, 0),
}),
Box::new(Margin {
widget: Box::new(IndexedLayout { widget_idx: 3 }),
margin: (16, 8, 8, 0),
}),
],
}),
],
}),
Box::new(IndexedLayout { widget_idx: 3 }),
Box::new(IndexedLayout { widget_idx: 4 }),
Box::new(IndexedLayout { widget_idx: 5 }),
],
};
+69
View File
@@ -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)
}
}
+2
View File
@@ -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;