45 lines
1.2 KiB
Rust
45 lines
1.2 KiB
Rust
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::path::PathBuf;
|
|
use crate::config::Monitor;
|
|
|
|
pub fn is_lid_closed(monitor: &Monitor) -> bool {
|
|
if let Some(lid) = &monitor.lid {
|
|
is_acpi_closed(lid)
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
/// Determine if the lid is closed.
|
|
/// lid is the name of a directory in /proc/acpi/button/lid/.
|
|
fn is_acpi_closed(lid: &String) -> bool {
|
|
let mut path_buf = PathBuf::from("/proc/acpi/button/lid");
|
|
path_buf.push(&lid);
|
|
path_buf.push("state");
|
|
|
|
if let Ok(ok) = path_buf.try_exists() {
|
|
if ok {
|
|
File::open(path_buf).is_ok_and(|mut file| {
|
|
let mut str = String::new();
|
|
if let Ok(_size) = file.read_to_string(&mut str) {
|
|
is_state_closed(str)
|
|
} else {
|
|
// error reading file
|
|
false
|
|
}
|
|
})
|
|
} else {
|
|
// no file
|
|
false
|
|
}
|
|
} else {
|
|
// error checking for file
|
|
false
|
|
}
|
|
}
|
|
|
|
/// Parse a /proc/acpi/button/lid/*/state file and return true if the lid is closed
|
|
fn is_state_closed(str:String) -> bool {
|
|
str.contains("closed")
|
|
} |