mirror of
https://github.com/rad4day/Waybar.git
synced 2023-12-21 10:22:59 +01:00
Use ifstream to reaad /proc/meminfo
This commit is contained in:
parent
6b62079d8a
commit
9ea0815dea
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
#include <stdio.h>
|
#include <fstream>
|
||||||
#include "util/chrono.hpp"
|
#include "util/chrono.hpp"
|
||||||
#include "ALabel.hpp"
|
#include "ALabel.hpp"
|
||||||
|
|
||||||
|
@ -14,28 +14,56 @@ waybar::modules::Memory::Memory(const Json::Value& config)
|
|||||||
auto waybar::modules::Memory::update() -> void
|
auto waybar::modules::Memory::update() -> void
|
||||||
{
|
{
|
||||||
parseMeminfo();
|
parseMeminfo();
|
||||||
|
if(memtotal_ > 0 && memfree_ >= 0) {
|
||||||
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
|
int used_ram_percentage = 100 * (memtotal_ - memfree_) / memtotal_;
|
||||||
label_.set_text(fmt::format(format_, used_ram_percentage));
|
label_.set_text(fmt::format(format_, used_ram_percentage));
|
||||||
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
|
auto used_ram_gigabytes = (memtotal_ - memfree_) / std::pow(1024, 2);
|
||||||
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
|
label_.set_tooltip_text(fmt::format("{:.{}f}Gb used", used_ram_gigabytes, 1));
|
||||||
|
label_.show();
|
||||||
|
} else {
|
||||||
|
label_.hide();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void waybar::modules::Memory::parseMeminfo()
|
void waybar::modules::Memory::parseMeminfo()
|
||||||
{
|
{
|
||||||
int memtotal, memfree, memavail, membuffer, memcache;
|
long memtotal = -1, memfree = -1, membuffer = -1, memcache = -1, memavail = -1;
|
||||||
FILE* info = fopen("/proc/meminfo","r");
|
int count = 0;
|
||||||
if(fscanf (info, "MemTotal: %d kB MemFree: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &membuffer, &memcache) < 4) { // Old meminfo format
|
std::string line;
|
||||||
rewind(info);
|
std::ifstream info("/proc/meminfo");
|
||||||
if(fscanf(info, "MemTotal: %d kB MemFree: %d kB MemAvailable: %d kB Buffers: %d kB Cached: %d kB",&memtotal, &memfree, &memavail, &membuffer, &memcache) < 5) { // Current meminfo format
|
if(info.is_open()) {
|
||||||
memtotal_ = -1;
|
while(getline(info, line)) {
|
||||||
memfree_ = -1;
|
auto posDelim = line.find(":");
|
||||||
} else {
|
std::string name = line.substr(0, posDelim);
|
||||||
memtotal_ = memtotal;
|
long value = std::stol(line.substr(posDelim + 1));
|
||||||
memfree_ = memavail;
|
|
||||||
|
if(name.compare("MemTotal") == 0) {
|
||||||
|
memtotal = value;
|
||||||
|
count++;
|
||||||
|
} else if(name.compare("MemAvailable") == 0) {
|
||||||
|
memavail = value;
|
||||||
|
count++;
|
||||||
|
} else if(name.compare("MemFree") == 0) {
|
||||||
|
memfree = value;
|
||||||
|
count++;
|
||||||
|
} else if(name.compare("Buffers") == 0) {
|
||||||
|
membuffer = value;
|
||||||
|
count++;
|
||||||
|
} else if(name.compare("Cached") == 0) {
|
||||||
|
memcache = value;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
if (count >= 5 || (count >= 4 && memavail >= -1)) {
|
||||||
|
info.close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
throw std::runtime_error("Can't open /proc/meminfo");
|
||||||
|
}
|
||||||
memtotal_ = memtotal;
|
memtotal_ = memtotal;
|
||||||
|
if(memavail >= 0) {
|
||||||
|
memfree_ = memavail;
|
||||||
|
} else {
|
||||||
memfree_ = memfree + (membuffer + memcache);
|
memfree_ = memfree + (membuffer + memcache);
|
||||||
}
|
}
|
||||||
fclose(info);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user