support monitor lid option to disable closed monitors

This commit is contained in:
2025-01-01 18:02:56 +00:00
parent 20ec42d126
commit 818df0f7e8
6 changed files with 156 additions and 62 deletions

45
src/lid.rs Normal file
View File

@@ -0,0 +1,45 @@
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")
}