From ce029c341939b88a91ec167ef1968bba2876158c Mon Sep 17 00:00:00 2001 From: Kaosu Date: Sat, 27 Dec 2025 08:16:03 +0100 Subject: [PATCH 1/2] temperature: add hwmon-name option for stable sensor selection Allow selecting a hwmon device for the temperature module by its name instead of a fixed hwmon path. This avoids fragile configurations that break when new hwmon devices are added and the kernel changes hwmon ordering. The hwmon-name option is mutually exclusive with hwmon-path and hwmon-path-abs to prevent ambiguous configurations. --- man/waybar-temperature.5.scd | 6 ++++++ src/modules/temperature.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/man/waybar-temperature.5.scd b/man/waybar-temperature.5.scd index 1e19ec82..672a2518 100644 --- a/man/waybar-temperature.5.scd +++ b/man/waybar-temperature.5.scd @@ -27,6 +27,12 @@ Addressed by *temperature* 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*. +*hwmon-name*: ++ + typeof: string ++ + Select a hwmon device by its name (from /sys/class/hwmon/*/name), e.g. *amdgpu* + Requires *input-filename* to be set. + Cannot be used together with *hwmon-path* or *hwmon-path-abs*. + *input-filename*: ++ typeof: string ++ The temperature filename of your *hwmon-path-abs*, e.g. *temp1_input* diff --git a/src/modules/temperature.cpp b/src/modules/temperature.cpp index fa23ef56..d8108577 100644 --- a/src/modules/temperature.cpp +++ b/src/modules/temperature.cpp @@ -20,6 +20,24 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val if (check_set_path(item.asString())) break; }; + auto find_hwmon_by_name = [](const std::string& name) -> std::optional { + for (const auto& entry : std::filesystem::directory_iterator("/sys/class/hwmon")) { + std::ifstream f(entry.path() / "name"); + std::string hwname; + if (f >> hwname && hwname == name) { + return entry.path(); + } + } + return std::nullopt; + }; + + // ensure either hwmon-name OR old paths are used, not both + if (config_["hwmon-name"].isString() && + (!config_["hwmon-path"].isNull() || !config_["hwmon-path-abs"].isNull())) { + throw std::runtime_error( + "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; @@ -40,6 +58,20 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val }); } + if (file_path_.empty() && config_["hwmon-name"].isString()) { + if (!config_["input-filename"].isString()) { + throw std::runtime_error("hwmon-name requires input-filename to be set"); + } + + auto hwmon = find_hwmon_by_name(config_["hwmon-name"].asString()); + + if (!hwmon) { + throw std::runtime_error("hwmon-name '" + config_["hwmon-name"].asString() + "' not found"); + } + + file_path_ = hwmon->string() + "/" + config_["input-filename"].asString(); + } + if (file_path_.empty()) { auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0; file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone); From 61af3e95faa77afc699aa142846e9feee793a862 Mon Sep 17 00:00:00 2001 From: Kaosu Date: Sat, 27 Dec 2025 08:38:14 +0100 Subject: [PATCH 2/2] fix the doc --- man/waybar-temperature.5.scd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/man/waybar-temperature.5.scd b/man/waybar-temperature.5.scd index 672a2518..0783331e 100644 --- a/man/waybar-temperature.5.scd +++ b/man/waybar-temperature.5.scd @@ -29,7 +29,7 @@ Addressed by *temperature* *hwmon-name*: ++ typeof: string ++ - Select a hwmon device by its name (from /sys/class/hwmon/*/name), e.g. *amdgpu* + Select a hwmon device by its name (from /sys/class/hwmon/\*/name), e.g. *amdgpu*. Requires *input-filename* to be set. Cannot be used together with *hwmon-path* or *hwmon-path-abs*.