From 8d7341da6e9a5186dcba8f993123c2d4d151bd99 Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Sat, 29 Jul 2023 16:02:36 +0200 Subject: [PATCH] cpu module: Reuse getLoad of load module --- include/modules/cpu.hpp | 2 -- include/modules/load.hpp | 4 +++- src/modules/cpu/common.cpp | 13 +++---------- src/modules/load.cpp | 2 +- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/include/modules/cpu.hpp b/include/modules/cpu.hpp index aff4c508..7f78c165 100644 --- a/include/modules/cpu.hpp +++ b/include/modules/cpu.hpp @@ -21,8 +21,6 @@ class Cpu : public ALabel { auto update() -> void override; private: - double getCpuLoad(); - std::vector> prev_times_; util::SleeperThread thread_; diff --git a/include/modules/load.hpp b/include/modules/load.hpp index e4e91213..2c4ce610 100644 --- a/include/modules/load.hpp +++ b/include/modules/load.hpp @@ -20,8 +20,10 @@ class Load : public ALabel { virtual ~Load() = default; auto update() -> void override; + // This is a static member because it is also used by the cpu module. + static std::tuple getLoad(); + private: - std::tuple getLoad(); util::SleeperThread thread_; }; diff --git a/src/modules/cpu/common.cpp b/src/modules/cpu/common.cpp index 5259dcb9..8373d79e 100644 --- a/src/modules/cpu/common.cpp +++ b/src/modules/cpu/common.cpp @@ -1,6 +1,7 @@ #include "modules/cpu.hpp" #include "modules/cpu_frequency.hpp" #include "modules/cpu_usage.hpp" +#include "modules/load.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 @@ -21,7 +22,7 @@ waybar::modules::Cpu::Cpu(const std::string& id, const Json::Value& config) auto waybar::modules::Cpu::update() -> void { // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both - auto cpu_load = getCpuLoad(); + auto [load1, load5, load15] = Load::getLoad(); auto [cpu_usage, tooltip] = CpuUsage::getCpuUsage(prev_times_); auto [max_frequency, min_frequency, avg_frequency] = CpuFrequency::getCpuFrequency(); if (tooltipEnabled()) { @@ -40,7 +41,7 @@ auto waybar::modules::Cpu::update() -> void { event_box_.show(); auto icons = std::vector{state}; fmt::dynamic_format_arg_store store; - store.push_back(fmt::arg("load", cpu_load)); + store.push_back(fmt::arg("load", load1)); store.push_back(fmt::arg("usage", total_usage)); store.push_back(fmt::arg("icon", getIcon(total_usage, icons))); store.push_back(fmt::arg("max_frequency", max_frequency)); @@ -59,11 +60,3 @@ auto waybar::modules::Cpu::update() -> void { // Call parent update ALabel::update(); } - -double waybar::modules::Cpu::getCpuLoad() { - double load[1]; - if (getloadavg(load, 1) != -1) { - return std::ceil(load[0] * 100.0) / 100.0; - } - throw std::runtime_error("Can't get Cpu load"); -} diff --git a/src/modules/load.cpp b/src/modules/load.cpp index 9ee4b764..69a37b4e 100644 --- a/src/modules/load.cpp +++ b/src/modules/load.cpp @@ -19,7 +19,7 @@ waybar::modules::Load::Load(const std::string& id, const Json::Value& config) auto waybar::modules::Load::update() -> void { // TODO: as creating dynamic fmt::arg arrays is buggy we have to calc both - auto [load1, load5, load15] = getLoad(); + auto [load1, load5, load15] = Load::getLoad(); if (tooltipEnabled()) { auto tooltip = fmt::format("Load 1: {}\nLoad 5: {}\nLoad 15: {}", load1, load5, load15); label_.set_tooltip_text(tooltip);