Merge pull request #3011 from nicolarevelant/master

Use the correct thermal zone in FreeBSD
pull/3015/head
Alexis Rouillard 2024-03-15 17:37:07 +01:00 committed by GitHub
commit 8174dce776
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 16 additions and 5 deletions

View File

@ -9,7 +9,7 @@
waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config) waybar::modules::Temperature::Temperature(const std::string& id, const Json::Value& config)
: ALabel(config, "temperature", id, "{temperatureC}°C", 10) { : ALabel(config, "temperature", id, "{temperatureC}°C", 10) {
#if defined(__FreeBSD__) #if defined(__FreeBSD__)
// try to read sysctl? // FreeBSD uses sysctlbyname instead of read from a file
#else #else
auto& hwmon_path = config_["hwmon-path"]; auto& hwmon_path = config_["hwmon-path"];
if (hwmon_path.isString()) { if (hwmon_path.isString()) {
@ -37,11 +37,19 @@ waybar::modules::Temperature::Temperature(const std::string& id, const Json::Val
auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0; auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0;
file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone); file_path_ = fmt::format("/sys/class/thermal/thermal_zone{}/temp", zone);
} }
// check if file_path_ can be used to retrive the temperature
std::ifstream temp(file_path_); std::ifstream temp(file_path_);
if (!temp.is_open()) { if (!temp.is_open()) {
throw std::runtime_error("Can't open " + file_path_); throw std::runtime_error("Can't open " + file_path_);
} }
if (!temp.good()) {
temp.close();
throw std::runtime_error("Can't read from " + file_path_);
}
temp.close();
#endif #endif
thread_ = [this] { thread_ = [this] {
dp.emit(); dp.emit();
thread_.sleep_for(interval_); thread_.sleep_for(interval_);
@ -93,11 +101,11 @@ float waybar::modules::Temperature::getTemperature() {
size_t size = sizeof temp; size_t size = sizeof temp;
auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0; auto zone = config_["thermal-zone"].isInt() ? config_["thermal-zone"].asInt() : 0;
auto sysctl_thermal = fmt::format("hw.acpi.thermal.tz{}.temperature", zone);
if (sysctlbyname("hw.acpi.thermal.tz0.temperature", &temp, &size, NULL, 0) != 0) { if (sysctlbyname(fmt::format("hw.acpi.thermal.tz{}.temperature", zone).c_str(), &temp, &size,
throw std::runtime_error( NULL, 0) != 0) {
"sysctl hw.acpi.thermal.tz0.temperature or dev.cpu.0.temperature failed"); throw std::runtime_error(fmt::format(
"sysctl hw.acpi.thermal.tz{}.temperature or dev.cpu.{}.temperature failed", zone, zone));
} }
auto temperature_c = ((float)temp - 2732) / 10; auto temperature_c = ((float)temp - 2732) / 10;
return temperature_c; return temperature_c;
@ -110,6 +118,9 @@ float waybar::modules::Temperature::getTemperature() {
std::string line; std::string line;
if (temp.good()) { if (temp.good()) {
getline(temp, line); getline(temp, line);
} else {
temp.close();
throw std::runtime_error("Can't read from " + file_path_);
} }
temp.close(); temp.close();
auto temperature_c = std::strtol(line.c_str(), nullptr, 10) / 1000.0; auto temperature_c = std::strtol(line.c_str(), nullptr, 10) / 1000.0;