Skip to content

Commit

Permalink
Don't hard-code temp1_input
Browse files Browse the repository at this point in the history
  • Loading branch information
nokyan committed Feb 2, 2025
1 parent aaed86f commit 20fb8bc
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/utils/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,22 @@ static CPU_TEMPERATURE_PATH: LazyLock<Option<PathBuf>> = LazyLock::new(|| {
cpu_temperature_path.map(|(_, path)| path)
});

fn search_for_first_temp<S: AsRef<str>>(base_path: S) -> Option<PathBuf> {
glob(&format!("{}/temp*_input", base_path.as_ref()))
.unwrap()
.flatten()
.next()
}

/// Looks for hwmons with the given names.
/// This function is a bit inefficient since the `names` array is considered to be ordered by priority.
fn search_for_hwmons(names: &[&'static str]) -> Option<(&'static str, PathBuf)> {
for temp_name in names {
for path in (glob("/sys/class/hwmon/hwmon*").unwrap()).flatten() {
if let Ok(read_name) = std::fs::read_to_string(path.join("name")) {
if &read_name.trim_end() == temp_name {
return Some((temp_name, path.join("temp1_input")));
return search_for_first_temp(path.to_string_lossy())
.map(|first_temp| (*temp_name, first_temp));
}
}
}
Expand Down

0 comments on commit 20fb8bc

Please sign in to comment.