2018-08-08 23:54:33 +00:00
|
|
|
#include "modules/cpu.hpp"
|
|
|
|
|
2018-08-09 11:30:11 +00:00
|
|
|
waybar::modules::Cpu::Cpu(Json::Value config)
|
2018-08-18 09:43:48 +00:00
|
|
|
: ALabel(std::move(config))
|
2018-08-08 23:54:33 +00:00
|
|
|
{
|
2018-08-16 12:29:41 +00:00
|
|
|
label_.set_name("cpu");
|
|
|
|
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10;
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.connect(sigc::mem_fun(*this, &Cpu::update));
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_ = [this, interval] {
|
2018-08-19 11:39:57 +00:00
|
|
|
thread_.sig_update.emit();
|
2018-08-16 12:29:41 +00:00
|
|
|
thread_.sleep_for(chrono::seconds(interval));
|
2018-08-08 23:54:33 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-08 23:54:33 +00:00
|
|
|
|
2018-08-09 10:05:48 +00:00
|
|
|
auto waybar::modules::Cpu::update() -> void
|
|
|
|
{
|
2018-08-17 12:24:00 +00:00
|
|
|
struct sysinfo info = {0};
|
2018-08-16 12:29:41 +00:00
|
|
|
if (sysinfo(&info) == 0) {
|
2018-08-18 13:05:18 +00:00
|
|
|
float f_load = 1.f / (1u << SI_LOAD_SHIFT);
|
2018-08-16 12:29:41 +00:00
|
|
|
uint16_t load = info.loads[0] * f_load * 100 / get_nprocs();
|
|
|
|
auto format = config_["format"] ? config_["format"].asString() : "{}%";
|
|
|
|
label_.set_text(fmt::format(format, load));
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|
|
|
|
}
|