Skip to content

Commit 56fc4fb

Browse files
committed
Read the current brightness at startup
1 parent 6526684 commit 56fc4fb

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/backlight.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
fs::{File, OpenOptions, self},
3-
path::PathBuf,
3+
path::{PathBuf, Path},
44
time::Instant,
55
io::Write
66
};
@@ -10,13 +10,19 @@ use input::event::{
1010
};
1111
use crate::TIMEOUT_MS;
1212

13+
fn read_attr(path: &Path, attr: &str) -> u32 {
14+
fs::read_to_string(path.join(attr))
15+
.expect(&format!("Failed to read {attr}"))
16+
.trim()
17+
.parse::<u32>()
18+
.expect(&format!("Failed to parse {attr}"))
19+
}
20+
1321
fn find_backlight() -> Result<PathBuf> {
1422
for entry in fs::read_dir("/sys/class/backlight/")? {
1523
let entry = entry?;
1624
if entry.file_name().to_string_lossy().contains("display-pipe") {
17-
let mut path = entry.path();
18-
path.push("brightness");
19-
return Ok(path);
25+
return Ok(entry.path());
2026
}
2127
}
2228
Err(anyhow!("No backlight device found"))
@@ -36,11 +42,11 @@ pub struct BacklightManager {
3642
impl BacklightManager {
3743
pub fn new() -> BacklightManager {
3844
let bl_path = find_backlight().unwrap();
39-
let bl_file = OpenOptions::new().write(true).open(bl_path).unwrap();
45+
let bl_file = OpenOptions::new().write(true).open(bl_path.join("brightness")).unwrap();
4046
BacklightManager {
4147
bl_file,
4248
lid_state: SwitchState::Off,
43-
current_bl: 42,
49+
current_bl: read_attr(&bl_path, "brightness"),
4450
last_active: Instant::now()
4551
}
4652
}

0 commit comments

Comments
 (0)