diff --git a/src/main.rs b/src/main.rs index 1841b40..506ee86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 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 }), ], }; diff --git a/src/widgets/backlight.rs b/src/widgets/backlight.rs new file mode 100644 index 0000000..b972ee4 --- /dev/null +++ b/src/widgets/backlight.rs @@ -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 { + let mut s = fs::read_to_string(path)?; + s.pop(); + s.parse::() + .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) + } +} diff --git a/src/widgets/mod.rs b/src/widgets/mod.rs index a95977a..679eb67 100644 --- a/src/widgets/mod.rs +++ b/src/widgets/mod.rs @@ -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;