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)
|
|
|
|
: _config(config)
|
2018-08-09 01:54:33 +02:00
|
|
|
{
|
2018-08-09 10:50:16 +02:00
|
|
|
_label.get_style_context()->add_class("cpu");
|
2018-08-10 23:21:21 +02:00
|
|
|
int interval = _config["interval"] ? _config["inveral"].asInt() : 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-09 14:34:39 +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
|
|
|
|
{
|
|
|
|
struct sysinfo info;
|
|
|
|
if (!sysinfo(&info)) {
|
|
|
|
float f_load = 1.f / (1 << SI_LOAD_SHIFT);
|
2018-08-09 13:30:11 +02:00
|
|
|
int 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 &() {
|
|
|
|
return _label;
|
|
|
|
}
|