2018-08-09 01:54:33 +02:00
|
|
|
#include "modules/cpu.hpp"
|
|
|
|
|
2018-08-09 13:30:11 +02:00
|
|
|
waybar::modules::Cpu::Cpu(Json::Value config)
|
2018-08-16 14:29:41 +02:00
|
|
|
: config_(std::move(config))
|
2018-08-09 01:54:33 +02:00
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
label_.set_name("cpu");
|
|
|
|
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 10;
|
|
|
|
thread_ = [this, interval] {
|
2018-08-11 02:40:13 +02:00
|
|
|
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Cpu::update));
|
2018-08-16 14:29:41 +02:00
|
|
|
thread_.sleep_for(chrono::seconds(interval));
|
2018-08-09 01:54:33 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2018-08-09 12:05:48 +02:00
|
|
|
auto waybar::modules::Cpu::update() -> void
|
|
|
|
{
|
2018-08-16 14:29:41 +02:00
|
|
|
struct sysinfo info = {};
|
|
|
|
if (sysinfo(&info) == 0) {
|
|
|
|
float f_load = 1.f / (1U << SI_LOAD_SHIFT);
|
|
|
|
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 12:05:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-09 01:54:33 +02:00
|
|
|
waybar::modules::Cpu::operator Gtk::Widget &() {
|
2018-08-16 14:29:41 +02:00
|
|
|
return label_;
|
2018-08-09 01:54:33 +02:00
|
|
|
}
|