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:
@@ -35,7 +35,12 @@ Addressed by *temperature*
|
|||||||
|
|
||||||
*input-filename*: ++
|
*input-filename*: ++
|
||||||
typeof: string ++
|
typeof: string ++
|
||||||
The temperature filename of your *hwmon-path-abs*, e.g. *temp1_input*
|
The temperature filename of your *hwmon-path-abs* (also used by *hwmon-by-name*), e.g. *temp1_input*
|
||||||
|
|
||||||
|
*hwmon-by-name*: ++
|
||||||
|
typeof: string ++
|
||||||
|
The substring to search for in */sys/class/hwmon/hwmonX/name* (where hwmonX is any folder in */sys/class/hwmon/*).
|
||||||
|
Waybar will search for every directory in */sys/class/hwmon/* and uses the directory in which the *name* matches *hwmon-by-name*.
|
||||||
|
|
||||||
*warning-threshold*: ++
|
*warning-threshold*: ++
|
||||||
typeof: integer ++
|
typeof: integer ++
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
#include "modules/temperature.hpp"
|
#include "modules/temperature.hpp"
|
||||||
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <optional>
|
||||||
|
#include <stdexcept>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#if defined(__FreeBSD__)
|
#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 (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> {
|
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")) {
|
for (const auto& entry : std::filesystem::directory_iterator("/sys/class/hwmon")) {
|
||||||
std::ifstream f(entry.path() / "name");
|
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");
|
"hwmon-name cannot be used together with hwmon-path or hwmon-path-abs");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (file_path_.empty()) {
|
||||||
// if hwmon_path is an array, loop to find first valid item
|
// if hwmon_path is an array, loop to find first valid item
|
||||||
traverseAsArray(config_["hwmon-path"], [this](const std::string& path) {
|
traverseAsArray(config_["hwmon-path"], [this](const std::string& path) {
|
||||||
if (!std::filesystem::exists(path)) return false;
|
if (!std::filesystem::exists(path)) return false;
|
||||||
file_path_ = path;
|
file_path_ = path;
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (file_path_.empty() && config_["input-filename"].isString()) {
|
if (file_path_.empty() && config_["input-filename"].isString()) {
|
||||||
// fallback to hwmon_paths-abs
|
// fallback to hwmon_paths-abs
|
||||||
|
|||||||
Reference in New Issue
Block a user