widgets/backlight: Set support
This commit is contained in:
@@ -1,11 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs::{self, OpenOptions},
|
||||||
io::{Error, ErrorKind},
|
io::{Write, Error, ErrorKind},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
color::Color,
|
color::Color,
|
||||||
|
event::PointerButton,
|
||||||
fonts::FontMap,
|
fonts::FontMap,
|
||||||
widgets::bar_widget::{BarWidget, BarWidgetImpl},
|
widgets::bar_widget::{BarWidget, BarWidgetImpl},
|
||||||
};
|
};
|
||||||
@@ -17,10 +18,16 @@ fn read_file_as_u64(path: &Path) -> Result<u64, Error> {
|
|||||||
.map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value"))
|
.map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> {
|
||||||
|
let mut file = OpenOptions::new().write(true).open(path)?;
|
||||||
|
file.write_fmt(format_args!("{}", value))
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Backlight {
|
pub struct Backlight {
|
||||||
device_path: PathBuf,
|
device_path: PathBuf,
|
||||||
cur: u64,
|
cur: u64,
|
||||||
max: u64,
|
max: u64,
|
||||||
|
dirty: bool
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Backlight {
|
impl Backlight {
|
||||||
@@ -34,6 +41,7 @@ impl Backlight {
|
|||||||
device_path: Path::new("/sys/class/backlight").to_path_buf().join(path),
|
device_path: Path::new("/sys/class/backlight").to_path_buf().join(path),
|
||||||
cur: 0,
|
cur: 0,
|
||||||
max: 0,
|
max: 0,
|
||||||
|
dirty: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
dev.update();
|
dev.update();
|
||||||
@@ -47,21 +55,38 @@ impl Backlight {
|
|||||||
Err(_) => false,
|
Err(_) => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, brightness: f32) {
|
||||||
|
let val = (self.max as f32 * brightness) as u64;
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BarWidgetImpl for Backlight {
|
impl BarWidgetImpl for Backlight {
|
||||||
fn name(&self) -> &'static str {
|
fn name(&self) -> &'static str {
|
||||||
"backlight"
|
"backlight"
|
||||||
}
|
}
|
||||||
|
fn get_dirty(&self) -> bool {
|
||||||
|
self.dirty
|
||||||
|
}
|
||||||
fn value(&mut self) -> f32 {
|
fn value(&mut self) -> f32 {
|
||||||
if self.cur > self.max {
|
if self.cur > self.max {
|
||||||
// what.
|
// what.
|
||||||
return 1.0;
|
return 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.dirty = false;
|
||||||
self.cur as f32 / self.max as f32
|
self.cur as f32 / self.max as f32
|
||||||
}
|
}
|
||||||
fn color(&self) -> Color {
|
fn color(&self) -> Color {
|
||||||
Color::WHITE
|
Color::WHITE
|
||||||
}
|
}
|
||||||
|
fn click(&mut self, pos: f32, btn: PointerButton) {
|
||||||
|
self.dirty = true;
|
||||||
|
match btn {
|
||||||
|
PointerButton::Left => self.set(pos),
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user