From e403c3b71b498abd8d9bc47a85418382f5e9a63d Mon Sep 17 00:00:00 2001 From: yangyingchao Date: Sat, 17 Jun 2023 11:33:14 +0800 Subject: [PATCH] support multiple items in hwmon-path of temperature module So user can share configuration file among different machines with different hardware configurations. --- man/waybar-temperature.5.scd | 4 +++- src/modules/temperature.cpp | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/man/waybar-temperature.5.scd b/man/waybar-temperature.5.scd index 8d11e517..cc689dc0 100644 --- a/man/waybar-temperature.5.scd +++ b/man/waybar-temperature.5.scd @@ -19,6 +19,8 @@ Addressed by *temperature* *hwmon-path*: ++ typeof: string ++ The temperature path to use, e.g. */sys/class/hwmon/hwmon2/temp1_input* instead of one in */sys/class/thermal/*. + This can also be an array of strings. In this case, waybar will check each item in the array and use the first valid one. + This is suitable if you want to share the same configuration file among different machines with different hardware configurations. *hwmon-path-abs*: ++ typeof: string ++ @@ -117,7 +119,7 @@ Addressed by *temperature* ``` "temperature": { // "thermal-zone": 2, - // "hwmon-path": "/sys/class/hwmon/hwmon2/temp1_input", + // "hwmon-path": ["/sys/class/hwmon/hwmon2/temp1_input", "/sys/class/thermal/thermal_zone0/temp"], // "critical-threshold": 80, // "format-critical": "{temperatureC}°C ", "format": "{temperatureC}°C " diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index ff722d7a..5ef2f4c9 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -11,8 +11,18 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val #if defined(__FreeBSD__) // try to read sysctl? #else - if (config_["hwmon-path"].isString()) { - file_path_ = config_["hwmon-path"].asString(); + 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()) { file_path_ = (*std::filesystem::directory_iterator(config_["hwmon-path-abs"].asString())) .path()