2019-03-13 12:18:08 +00:00
|
|
|
#include "modules/temperature.hpp"
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2020-03-12 21:04:00 +00:00
|
|
|
#include <filesystem>
|
2019-03-13 12:18:08 +00:00
|
|
|
|
2022-09-30 19:07:31 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2019-03-13 12:18:08 +00:00
|
|
|
waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config)
|
2022-11-24 11:28:52 +00:00
|
|
|
: ALabel(config, "temperature", id, "{temperatureC}°C", 10) {
|
2022-09-30 19:07:31 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
// try to read sysctl?
|
|
|
|
#else
|
2023-06-17 03:33:14 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 21:04:00 +00:00
|
|
|
} else if (config_["hwmon-path-abs"].isString() && config_["input-filename"].isString()) {
|
2022-04-06 06:37:19 +00:00
|
|
|
file_path_ = (*std::filesystem::directory_iterator(config_["hwmon-path-abs"].asString()))
|
|
|
|
.path()
|
|
|
|
.string() +
|
|
|
|
"/" + config_["input-filename"].asString();
|
2019-03-13 12:18:08 +00:00
|
|
|
} else {
|
2019-04-18 15:52:00 +00:00
|
|
|
auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0;
|
2019-03-13 12:18:08 +00:00
|
|
|
file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone);
|
|
|
|
}
|
2019-05-12 17:53:14 +00:00
|
|
|
std::ifstream temp(file_path_);
|
|
|
|
if (!temp.is_open()) {
|
2019-04-11 13:08:23 +00:00
|
|
|
throw std::runtime_error("Can't open " + file_path_);
|
|
|
|
}
|
2022-09-30 19:07:31 +00:00
|
|
|
#endif
|
2019-03-13 12:18:08 +00:00
|
|
|
thread_ = [this] {
|
|
|
|
dp.emit();
|
|
|
|
thread_.sleep_for(interval_);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Temperature::update() -> void {
|
2020-04-25 16:44:48 +00:00
|
|
|
auto temperature = getTemperature();
|
|
|
|
uint16_t temperature_c = std::round(temperature);
|
|
|
|
uint16_t temperature_f = std::round(temperature * 1.8 + 32);
|
|
|
|
uint16_t temperature_k = std::round(temperature + 273.15);
|
2019-03-13 12:18:08 +00:00
|
|
|
auto critical = isCritical(temperature_c);
|
|
|
|
auto format = format_;
|
|
|
|
if (critical) {
|
2019-04-18 15:52:00 +00:00
|
|
|
format = config_["format-critical"].isString() ? config_["format-critical"].asString() : format;
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.get_style_context()->add_class("critical");
|
2019-03-13 12:18:08 +00:00
|
|
|
} else {
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.get_style_context()->remove_class("critical");
|
2019-03-13 12:18:08 +00:00
|
|
|
}
|
2022-03-16 12:50:00 +00:00
|
|
|
|
2022-04-06 06:37:19 +00:00
|
|
|
if (format.empty()) {
|
2022-03-16 12:50:00 +00:00
|
|
|
event_box_.hide();
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
event_box_.show();
|
|
|
|
}
|
|
|
|
|
2019-05-13 09:31:05 +00:00
|
|
|
auto max_temp = config_["critical-threshold"].isInt() ? config_["critical-threshold"].asInt() : 0;
|
2023-01-16 21:24:55 +00:00
|
|
|
label_.set_markup(fmt::format(fmt::runtime(format), fmt::arg("temperatureC", temperature_c),
|
2022-11-24 11:28:52 +00:00
|
|
|
fmt::arg("temperatureF", temperature_f),
|
|
|
|
fmt::arg("temperatureK", temperature_k),
|
|
|
|
fmt::arg("icon", getIcon(temperature_c, "", max_temp))));
|
2020-10-21 17:10:10 +00:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
std::string tooltip_format = "{temperatureC}°C";
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
}
|
2023-01-16 21:24:55 +00:00
|
|
|
label_.set_tooltip_text(fmt::format(
|
|
|
|
fmt::runtime(tooltip_format), fmt::arg("temperatureC", temperature_c),
|
|
|
|
fmt::arg("temperatureF", temperature_f), fmt::arg("temperatureK", temperature_k)));
|
2020-10-21 17:10:10 +00:00
|
|
|
}
|
2020-04-12 16:30:21 +00:00
|
|
|
// Call parent update
|
2022-11-24 11:28:52 +00:00
|
|
|
ALabel::update();
|
2019-03-13 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 16:44:48 +00:00
|
|
|
float waybar::modules::Temperature::getTemperature() {
|
2022-09-30 19:07:31 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
int temp;
|
|
|
|
size_t size = sizeof temp;
|
|
|
|
|
2022-10-04 05:29:16 +00:00
|
|
|
auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0;
|
|
|
|
auto sysctl_thermal = fmt::format("hw.acpi.thermal.tz{}.temperature", zone);
|
|
|
|
|
2022-09-30 19:07:31 +00:00
|
|
|
if (sysctlbyname("hw.acpi.thermal.tz0.temperature", &temp, &size, NULL, 0) != 0) {
|
2021-05-27 13:40:54 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"sysctl hw.acpi.thermal.tz0.temperature or dev.cpu.0.temperature failed");
|
2022-09-30 19:07:31 +00:00
|
|
|
}
|
2022-10-04 06:03:54 +00:00
|
|
|
auto temperature_c = ((float)temp - 2732) / 10;
|
2022-09-30 19:07:31 +00:00
|
|
|
return temperature_c;
|
|
|
|
|
2022-10-04 06:03:54 +00:00
|
|
|
#else // Linux
|
2019-03-13 12:18:08 +00:00
|
|
|
std::ifstream temp(file_path_);
|
|
|
|
if (!temp.is_open()) {
|
|
|
|
throw std::runtime_error("Can't open " + file_path_);
|
|
|
|
}
|
|
|
|
std::string line;
|
|
|
|
if (temp.good()) {
|
|
|
|
getline(temp, line);
|
|
|
|
}
|
|
|
|
temp.close();
|
2022-04-06 06:37:19 +00:00
|
|
|
auto temperature_c = std::strtol(line.c_str(), nullptr, 10) / 1000.0;
|
2020-04-25 16:44:48 +00:00
|
|
|
return temperature_c;
|
2022-09-30 19:07:31 +00:00
|
|
|
#endif
|
2019-03-13 12:18:08 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
bool waybar::modules::Temperature::isCritical(uint16_t temperature_c) {
|
|
|
|
return config_["critical-threshold"].isInt() &&
|
|
|
|
temperature_c >= config_["critical-threshold"].asInt();
|
2019-05-22 10:06:24 +00:00
|
|
|
}
|