2018-08-09 01:42:52 +02:00
|
|
|
#include "modules/memory.hpp"
|
|
|
|
|
2018-12-18 17:30:54 +01:00
|
|
|
waybar::modules::Memory::Memory(const std::string& id, const Json::Value& config)
|
2019-04-18 17:52:00 +02:00
|
|
|
: ALabel(config, "{}%", 30) {
|
2018-12-18 17:30:54 +01:00
|
|
|
label_.set_name("memory");
|
|
|
|
if (!id.empty()) {
|
|
|
|
label_.get_style_context()->add_class(id);
|
|
|
|
}
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_ = [this] {
|
2018-08-20 14:50:45 +02:00
|
|
|
dp.emit();
|
2018-11-23 11:57:37 +01:00
|
|
|
thread_.sleep_for(interval_);
|
2018-08-09 01:42:52 +02:00
|
|
|
};
|
2018-08-18 11:43:48 +02:00
|
|
|
}
|
2018-08-09 01:42:52 +02:00
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
auto waybar::modules::Memory::update() -> void {
|
2018-11-08 21:09:56 +01:00
|
|
|
parseMeminfo();
|
2018-11-09 22:55:25 +01:00
|
|
|
if (memtotal_ > 0 && memfree_ >= 0) {
|
2018-11-09 16:24:13 +01:00
|
|
|
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
|
2019-05-03 04:19:47 +02:00
|
|
|
getState(used_ram_percentage);
|
2018-11-21 20:49:09 +01:00
|
|
|
label_.set_markup(fmt::format(format_, used_ram_percentage));
|
2018-11-09 16:24:13 +01:00
|
|
|
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
|
2019-02-22 11:35:26 +01:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
|
|
|
|
}
|
2018-12-18 17:30:54 +01:00
|
|
|
event_box_.show();
|
2018-11-09 16:24:13 +01:00
|
|
|
} else {
|
2018-12-18 17:30:54 +01:00
|
|
|
event_box_.hide();
|
2018-11-09 16:24:13 +01:00
|
|
|
}
|
2018-11-08 21:09:56 +01:00
|
|
|
}
|
|
|
|
|
2019-04-18 17:52:00 +02:00
|
|
|
void waybar::modules::Memory::parseMeminfo() {
|
2019-04-24 12:37:24 +02:00
|
|
|
int64_t memfree = -1, membuffer = -1, memcache = -1, memavail = -1;
|
2018-11-09 22:55:25 +01:00
|
|
|
std::ifstream info(data_dir_);
|
|
|
|
if (!info.is_open()) {
|
|
|
|
throw std::runtime_error("Can't open " + data_dir_);
|
|
|
|
}
|
2018-11-09 16:24:13 +01:00
|
|
|
std::string line;
|
2018-11-09 22:55:25 +01:00
|
|
|
while (getline(info, line)) {
|
2019-04-24 12:37:24 +02:00
|
|
|
auto posDelim = line.find(':');
|
2018-11-09 22:55:25 +01:00
|
|
|
if (posDelim == std::string::npos) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
std::string name = line.substr(0, posDelim);
|
2019-04-24 12:37:24 +02:00
|
|
|
int64_t value = std::stol(line.substr(posDelim + 1));
|
2018-11-09 16:24:13 +01:00
|
|
|
|
2018-11-09 22:55:25 +01:00
|
|
|
if (name.compare("MemTotal") == 0) {
|
|
|
|
memtotal_ = value;
|
|
|
|
} else if (name.compare("MemAvailable") == 0) {
|
|
|
|
memavail = value;
|
|
|
|
} else if (name.compare("MemFree") == 0) {
|
|
|
|
memfree = value;
|
|
|
|
} else if (name.compare("Buffers") == 0) {
|
|
|
|
membuffer = value;
|
|
|
|
} else if (name.compare("Cached") == 0) {
|
|
|
|
memcache = value;
|
|
|
|
}
|
2019-04-18 17:52:00 +02:00
|
|
|
if (memtotal_ > 0 && (memavail >= 0 || (memfree > -1 && membuffer > -1 && memcache > -1))) {
|
2018-11-09 22:55:25 +01:00
|
|
|
break;
|
2018-11-08 21:09:56 +01:00
|
|
|
}
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|
2018-11-09 22:55:25 +01:00
|
|
|
memfree_ = memavail >= 0 ? memavail : memfree + membuffer + memcache;
|
2018-08-09 12:05:48 +02:00
|
|
|
}
|