11use 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} ;
1111use 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+
1321fn 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 {
3642impl 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