Files
Waybar/src/modules/cpu_frequency/common.cpp
T
Alex 8dbba448ce refactor(modules): migrate state modules to ALabel tooltip helper
Migrate cpu, cpu_usage, cpu_frequency and memory to the generic
updateLabelAndTooltip/ForState helper so label and tooltip rendering go
through the dedup-aware setters and shared tooltip-format resolution.

Add store-accepting overloads of the helper to ALabel for modules that
build a dynamic fmt argument store (per-core cpu stats). cpu keeps its
tooltip-format-<state> selection via the state overload; the others keep
their existing tooltip-format-only behavior.
2026-07-04 00:05:17 +02:00

63 lines
2.2 KiB
C++

#include "modules/cpu_frequency.hpp"
// In the 80000 version of fmt library authors decided to optimize imports
// and moved declarations required for fmt::dynamic_format_arg_store in new
// header fmt/args.h
#if (FMT_VERSION >= 80000)
#include <fmt/args.h>
#else
#include <fmt/core.h>
#endif
waybar::modules::CpuFrequency::CpuFrequency(const std::string& id, const Json::Value& config)
: ALabel(config, "cpu_frequency", id, "{avg_frequency}", 10) {
thread_ = [this] {
dp.emit();
thread_.sleep_for(interval_);
};
}
auto waybar::modules::CpuFrequency::update() -> void {
// TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both
auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency();
auto format = format_;
auto state = getState(avg_frequency);
if (!state.empty() && config_["format-" + state].isString()) {
format = config_["format-" + state].asString();
}
if (format.empty()) {
event_box_.hide();
} else {
event_box_.show();
auto icons = std::vector<std::string>{state};
updateLabelAndTooltip(
format,
"Minimum frequency: {min_frequency}\nAverage frequency: {avg_frequency}\nMaximum "
"frequency: {max_frequency}\n",
fmt::arg("icon", getIcon(avg_frequency, icons)), fmt::arg("max_frequency", max_frequency),
fmt::arg("min_frequency", min_frequency), fmt::arg("avg_frequency", avg_frequency));
}
// Call parent update
ALabel::update();
}
std::tuple<float, float, float> waybar::modules::CpuFrequency::getCpuFrequency() {
std::vector<float> frequencies = CpuFrequency::parseCpuFrequencies();
if (frequencies.empty()) {
return {0.f, 0.f, 0.f};
}
auto [min, max] = std::minmax_element(std::begin(frequencies), std::end(frequencies));
float avg_frequency =
std::accumulate(std::begin(frequencies), std::end(frequencies), 0.0) / frequencies.size();
// Round frequencies with double decimal precision to get GHz
float max_frequency = std::ceil(*max / 10.0) / 100.0;
float min_frequency = std::ceil(*min / 10.0) / 100.0;
avg_frequency = std::ceil(avg_frequency / 10.0) / 100.0;
return {max_frequency, min_frequency, avg_frequency};
}