From e298bf922fbef2f6e3cd3717d3d3710b802fcc4a Mon Sep 17 00:00:00 2001 From: giskard Date: Wed, 8 May 2024 21:00:52 +0800 Subject: [PATCH 1/2] temperature: allow hwmon-path-abs as array --- src/modules/temperature.cpp | 49 ++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index f0629670..922fa639 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -1,6 +1,9 @@ #include "modules/temperature.hpp" +#include + #include +#include #if defined(__FreeBSD__) #include @@ -11,26 +14,32 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val #if defined(__FreeBSD__) // FreeBSD uses sysctlbyname instead of read from a file #else - auto& hwmon_path = config_["hwmon-path"]; - if (hwmon_path.isString()) { - file_path_ = hwmon_path.asString(); - } else if (hwmon_path.isArray()) { - // if hwmon_path is an array, loop to find first valid item - for (auto& item : hwmon_path) { - auto path = item.asString(); - if (std::filesystem::exists(path)) { - file_path_ = path; - break; - } - } - } else if (config_["hwmon-path-abs"].isString() && config_["input-filename"].isString()) { - for (const auto& hwmon : - std::filesystem::directory_iterator(config_["hwmon-path-abs"].asString())) { - if (hwmon.path().filename().string().starts_with("hwmon")) { - file_path_ = hwmon.path().string() + "/" + config_["input-filename"].asString(); - break; - } - } + auto traverseAsArray = [](const Json::Value& value, auto&& check_set_path) { + if (value.isString()) + check_set_path(value.asString()); + else if (value.isArray()) + for (const auto& item : value) + if (check_set_path(item.asString())) break; + }; + + // 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 + traverseAsArray(config_["hwmon-path-abs"], [this](const std::string& path) { + if (!std::filesystem::is_directory(path)) return false; + return std::ranges::any_of( + std::filesystem::directory_iterator(path), [this](const auto& hwmon) { + if (!hwmon.path().filename().string().starts_with("hwmon")) return false; + file_path_ = hwmon.path().string() + "/" + config_["input-filename"].asString(); + return true; + }); + }); } if (file_path_.empty()) { From b980dab6df22468b27b0e85f9da3343fcdff5bf6 Mon Sep 17 00:00:00 2001 From: giskard Date: Wed, 8 May 2024 21:24:54 +0800 Subject: [PATCH 2/2] doc: update waybar-temperature manual page --- man/waybar-temperature.5.scd | 1 + 1 file changed, 1 insertion(+) diff --git a/man/waybar-temperature.5.scd b/man/waybar-temperature.5.scd index ff2168ea..87b60d38 100644 --- a/man/waybar-temperature.5.scd +++ b/man/waybar-temperature.5.scd @@ -25,6 +25,7 @@ Addressed by *temperature* *hwmon-path-abs*: ++ typeof: string ++ The path of the hwmon-directory of the device, e.g. */sys/devices/pci0000:00/0000:00:18.3/hwmon*. (Note that the subdirectory *hwmon/hwmon#*, where *#* is a number is not part of the path!) Has to be used together with *input-filename*. + This can also be an array of strings, for which, it just works like *hwmon-path*. *input-filename*: ++ typeof: string ++