Files
wldash/src/backlight.rs
T

174 lines
4.9 KiB
Rust
Raw Normal View History

2019-07-31 00:42:03 +02:00
use crate::buffer::Buffer;
use crate::color::Color;
2019-08-04 14:23:49 +02:00
use crate::draw::{draw_bar, draw_box, draw_text, ROBOTO_REGULAR};
2019-07-31 10:26:12 +02:00
use crate::module::{Input, ModuleImpl};
2019-07-31 00:42:03 +02:00
2019-07-31 10:26:12 +02:00
use std::fs::OpenOptions;
use std::io::{Error, ErrorKind};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
2019-07-31 00:42:03 +02:00
2019-08-01 19:51:06 +02:00
use chrono::{DateTime, Local};
2019-07-31 00:42:03 +02:00
pub struct Backlight {
device_path: PathBuf,
cur_brightness: u64,
max_brightness: u64,
dirty: bool,
}
fn read_file_as_u64(path: &Path) -> Result<u64, Error> {
2019-07-31 10:26:12 +02:00
let mut file = OpenOptions::new().read(true).open(path)?;
2019-07-31 00:42:03 +02:00
let mut str = String::new();
file.read_to_string(&mut str)?;
str.pop();
2019-07-31 10:26:12 +02:00
str.parse::<u64>()
.map_err(|_e| Error::new(ErrorKind::Other, "unable to parse value"))
2019-07-31 00:42:03 +02:00
}
fn write_file_as_u64(path: &Path, value: u64) -> Result<(), Error> {
2019-07-31 10:26:12 +02:00
let mut file = OpenOptions::new().write(true).open(path)?;
2019-07-31 00:42:03 +02:00
file.write_fmt(format_args!("{}", value))
}
impl Backlight {
pub fn update(&mut self) -> Result<(), Error> {
self.cur_brightness = read_file_as_u64(self.device_path.join("brightness").as_path())?;
self.max_brightness = read_file_as_u64(self.device_path.join("max_brightness").as_path())?;
self.dirty = true;
Ok(())
}
pub fn sync(&mut self) -> Result<(), Error> {
2019-07-31 10:26:12 +02:00
write_file_as_u64(
self.device_path.join("brightness").as_path(),
self.cur_brightness,
)?;
2019-07-31 00:42:03 +02:00
self.update()?;
Ok(())
}
pub fn brightness(&self) -> f32 {
if self.cur_brightness > self.max_brightness {
// what.
2019-08-04 14:11:21 +02:00
return 1.0;
2019-07-31 00:42:03 +02:00
}
2019-08-04 14:11:21 +02:00
(self.cur_brightness as f32 / self.max_brightness as f32)
2019-07-31 00:42:03 +02:00
}
pub fn add(&mut self, diff: f32) -> Result<(), Error> {
2019-08-04 14:11:21 +02:00
let inc = (self.max_brightness as f32 * diff) as i64;
2019-07-31 00:42:03 +02:00
self.cur_brightness = if self.cur_brightness as i64 + inc < 1 {
1
} else if self.cur_brightness as i64 + inc > self.max_brightness as i64 {
self.max_brightness
} else {
(self.cur_brightness as i64 + inc) as u64
};
Ok(())
}
2019-07-31 10:26:12 +02:00
pub fn new() -> Result<Self, Error> {
let devices = Path::new("/sys/class/backlight").read_dir()?;
2019-07-31 00:42:03 +02:00
2019-07-31 10:26:12 +02:00
let first_device = match devices.take(1).next() {
Some(v) => match v {
Ok(v) => v,
Err(_) => return Err(Error::new(ErrorKind::Other, "no backlight device")),
},
None => return Err(Error::new(ErrorKind::Other, "no backlight device")),
};
let mut dev = Backlight {
2019-07-31 00:42:03 +02:00
device_path: first_device.path(),
cur_brightness: 0,
max_brightness: 0,
dirty: true,
};
dev.update()?;
Ok(dev)
}
}
impl ModuleImpl for Backlight {
2019-07-31 10:26:12 +02:00
fn draw(
&self,
buf: &mut Buffer,
bg: &Color,
_time: &DateTime<Local>,
) -> Result<Vec<(i32, i32, i32, i32)>, Error> {
2019-07-31 00:42:03 +02:00
buf.memset(bg);
2019-08-02 12:56:10 +02:00
let c = Color::new(1.0, 1.0, 1.0, 1.0);
2019-07-31 00:42:03 +02:00
draw_text(
ROBOTO_REGULAR,
2019-08-02 15:59:10 +02:00
&mut buf.subdimensions((0, 0, 128, 24))?,
2019-07-31 00:42:03 +02:00
bg,
2019-08-02 12:56:10 +02:00
&c,
2019-07-31 00:42:03 +02:00
24.0,
2019-08-01 00:09:51 +02:00
"backlight",
2019-07-31 00:42:03 +02:00
)?;
2019-08-01 00:09:51 +02:00
draw_bar(
2019-08-02 15:59:10 +02:00
&mut buf.subdimensions((128, 0, 432, 24))?,
2019-08-02 12:56:10 +02:00
&c,
432,
2019-08-01 00:09:51 +02:00
24,
2019-08-04 14:11:21 +02:00
self.brightness(),
2019-08-01 11:07:33 +02:00
)?;
2019-08-04 14:23:49 +02:00
draw_box(&mut buf.subdimensions((128, 0, 432, 24))?, &c, (432, 24))?;
2019-07-31 00:42:03 +02:00
Ok(vec![buf.get_signed_bounds()])
}
2019-07-31 10:26:12 +02:00
fn update(&mut self, _time: &DateTime<Local>, force: bool) -> Result<bool, ::std::io::Error> {
2019-07-31 00:42:03 +02:00
if self.dirty || force {
self.dirty = false;
Ok(true)
} else {
Ok(false)
}
}
fn input(&mut self, input: Input) {
match input {
2019-07-31 10:26:12 +02:00
Input::Scroll {
pos: _pos,
x: _x,
y,
} => {
2019-08-04 14:11:21 +02:00
self.add(y as f32 / 800.0).unwrap();
2019-08-02 13:26:32 +01:00
match self.sync() {
Ok(val) => val,
Err(err) => {
eprintln!("Error while trying to change brightness: {}", err);
()
}
}
2019-07-31 10:26:12 +02:00
}
Input::Click { pos: _pos, button } => {
2019-07-31 00:42:03 +02:00
match button {
2019-07-31 10:26:12 +02:00
273 => {
// Right click
2019-07-31 00:42:03 +02:00
self.cur_brightness = if self.cur_brightness == 1 {
self.max_brightness
} else {
1
};
2019-08-02 13:26:32 +01:00
match self.sync() {
Ok(val) => val,
Err(err) => {
eprintln!("Error while trying to change brightness: {}", err);
()
}
}
2019-07-31 10:26:12 +02:00
}
2019-07-31 00:42:03 +02:00
_ => {}
}
2019-07-31 10:26:12 +02:00
}
2019-08-02 15:59:10 +02:00
_ => {}
2019-07-31 00:42:03 +02:00
}
}
2019-07-31 10:26:12 +02:00
}