Merge pull request #3706 from ico277/master

Add an option to search for name in hwmon directory for the temperature module
This commit is contained in:
Alexis Rouillard
2026-07-03 23:20:34 +02:00
committed by GitHub
2 changed files with 43 additions and 7 deletions
+37 -6
View File
@@ -1,6 +1,8 @@
#include "modules/temperature.hpp"
#include <filesystem>
#include <optional>
#include <stdexcept>
#include <string>
#if defined(__FreeBSD__)
@@ -20,6 +22,33 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val
if (check_set_path(item.asString())) break;
};
if (config_["hwmon-by-name"].isString() && config_["input-filename"].isString()) {
auto name = config_["hwmon-by-name"].asString();
auto input_filename = config_["input-filename"].asString();
for (const auto& entry : std::filesystem::directory_iterator("/sys/class/hwmon/")) {
if (std::filesystem::is_directory(entry) && file_path_.empty()) {
auto name_filepath = entry.path().string() + "/name";
auto input_filepath = entry.path().string() + "/" + input_filename;
if (std::filesystem::exists(name_filepath) && std::filesystem::exists(input_filepath)) {
std::ifstream name_file(name_filepath);
if (!name_file.is_open()) {
throw std::runtime_error("Error: Could not open file " + name_filepath);
}
std::string line;
while (std::getline(name_file, line)) {
if (line.find(name) != std::string::npos) {
file_path_ = input_filepath;
break;
}
}
}
}
}
if (file_path_.empty()) throw std::runtime_error("Could not find hwmon by name " + name);
}
auto find_hwmon_by_name = [](const std::string& name) -> std::optional<std::filesystem::path> {
for (const auto& entry : std::filesystem::directory_iterator("/sys/class/hwmon")) {
std::ifstream f(entry.path() / "name");
@@ -38,12 +67,14 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val
"hwmon-name cannot be used together with hwmon-path or hwmon-path-abs");
}
// if hwmon_path is an array, loop to find first valid item
traverseAsArray(config_["hwmon-path"], [this](const std::string& path) {
if (!std::filesystem::exists(path)) return false;
file_path_ = path;
return true;
});
if (file_path_.empty()) {
// if hwmon_path is an array, loop to find first valid item
traverseAsArray(config_["hwmon-path"], [this](const std::string& path) {
if (!std::filesystem::exists(path)) return false;
file_path_ = path;
return true;
});
}
if (file_path_.empty() && config_["input-filename"].isString()) {
// fallback to hwmon_paths-abs