/** * I3cstat prints a configurable status bar for the i3 window manager * Copyright (C) 2022 Aaron Ball * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "config_mem.h" void config_mem_init(struct node* n) { n->data = NULL; n->type = CTYPE_MEM; n->loadfunc = &config_mem_load; n->loadkey = &load_mem_key; } int _meminfo_read(struct _meminfo* minfo) { FILE* fd; char buf[64]; fd = fopen("/proc/meminfo", "r"); if(!fd) return -1; while(fgets(buf, 64, fd) != NULL) { if(strncmp(buf, "MemTotal: ", 10) == 0) sscanf(buf, "MemTotal: %ld kB", &minfo->total); else if(strncmp(buf, "MemFree: ", 9) == 0) sscanf(buf, "MemFree: %ld", &minfo->free); else if(strncmp(buf, "MemAvailable: ", 14) == 0) sscanf(buf, "MemAvailable: %ld ", &minfo->available); else if(strncmp(buf, "Buffers: ", 9) == 0) sscanf(buf, "Buffers: %ld ", &minfo->buffers); else if(strncmp(buf, "Cached: ", 8) == 0) sscanf(buf, "Cached: %ld ", &minfo->cached); else if(strncmp(buf, "SwapCached: ", 12) == 0) sscanf(buf, "SwapCached: %ld ", &minfo->swapcached); } fclose(fd); return 0; } void load_mem_key(struct node* n, char* key, char* val) { } int config_mem_load(struct node* n) { struct _meminfo minfo; long used; int percent; _meminfo_read(&minfo); used = minfo.total - minfo.free - minfo.buffers - minfo.cached; percent = used * 100 / minfo.total; if(percent < 20) { strcpy(n->color, C_GREEN); } else if(percent < 40) { strcpy(n->color, C_YELLOW); } else if(percent < 60) { strcpy(n->color, C_ORANGE); } else { strcpy(n->color, C_RED); } strcpy(n->label_color, C_LGREY); if(strcmp(n->display, "bar") == 0) { print_bar(n->width, (double)used / minfo.total, n->text); } else { sprintf(n->text, "%02d%%", percent); } return 0; }