Moving memory query status code to use sysinfo() instead of /proc.

This commit is contained in:
Sanel Zukan 2013-01-09 13:45:49 +00:00
parent e01506d58a
commit 465eb7dfe3
4 changed files with 25 additions and 51 deletions

View File

@ -112,7 +112,6 @@ bool AppletManager::load(const char *path) {
return false;
}
AppletData *data = new AppletData;
data->dl = a;
data->awidget = NULL;
@ -132,11 +131,10 @@ bool AppletManager::load(const char *path) {
}
void AppletManager::clear(void) {
if(applet_list.empty())
return;
E_RETURN_IF_FAIL(applet_list.size() > 0);
AListIter it = applet_list.begin(), it_end = applet_list.end();
while(it != it_end) {
AListIter it = applet_list.begin(), ite = applet_list.end();
while(it != ite) {
clear_applet(*it);
it = applet_list.erase(it);
}
@ -147,29 +145,29 @@ void AppletManager::clear(void) {
* added to the group.
*/
void AppletManager::fill_group(Panel *p) {
AListIter it = applet_list.begin(), it_end = applet_list.end();
AListIter it = applet_list.begin(), ite = applet_list.end();
AppletData *applet;
for(; it != it_end; ++it) {
for(; it != ite; ++it) {
applet = *it;
/* allocate memory for widget and append it to group */
/* allocate memory for widget and append it to the group */
applet->awidget = applet->create_func();
p->add(applet->awidget);
}
}
void AppletManager::unfill_group(Panel *p) {
AListIter it = applet_list.begin(), it_end = applet_list.end();
AListIter it = applet_list.begin(), ite = applet_list.end();
for(; it != it_end; ++it)
for(; it != ite; ++it)
p->remove((*it)->awidget);
}
bool AppletManager::get_applet_options(Fl_Widget *o, unsigned long &opts) {
AListIter it = applet_list.begin(), it_end = applet_list.end();
AListIter it = applet_list.begin(), ite = applet_list.end();
for(; it != it_end; ++it) {
for(; it != ite; ++it) {
if(o == (*it)->awidget) {
opts = (*it)->ainfo->options;
return true;

View File

@ -11,7 +11,7 @@
SubDir TOP ede-panel ;
EdeProgram ede-panel : Hider.cpp Panel.cpp AppletManager.cpp ede-panel.cpp ;
#ObjectC++Flags Panel.cpp : -DEDE_PANEL_LOCAL_APPLETS ;
ObjectC++Flags Panel.cpp : -DEDE_PANEL_LOCAL_APPLETS ;
if $(OS) != "SOLARIS" {
# also must use this flag (on anything but Solaris) or program will crash

View File

@ -534,8 +534,8 @@ void Panel::load_applets(void) {
mgr.fill_group(this);
#else
mgr.load("./applets/quick-launch/quick_launch.so");
mgr.load("./applets/start-menu/start_menu.so");
mgr.load("./applets/quick-launch/quick_launch.so");
mgr.load("./applets/pager/pager.so");
mgr.load("./applets/clock/clock.so");
mgr.load("./applets/taskbar/taskbar.so");

View File

@ -1,5 +1,7 @@
/* assume Linux here */
#include <sys/sysinfo.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <FL/Fl.H>
#include <FL/fl_draw.H>
@ -13,25 +15,10 @@ EDELIB_NS_USING(color_rgb_to_fltk)
#define UPDATE_INTERVAL 1.0f
#define STR_CMP(first, second, n) (strncmp(first, second, n) == 0)
static int height_from_perc(int perc, int h) {
inline int height_from_perc(int perc, int h) {
return (perc * h) / 100;
}
static long get_number(const char *ln) {
char *s = edelib_strndup(ln, 128);
int i = 1;
for(char *p = strtok(s, " "); p; p = strtok(NULL, " "), i++) {
if(i == 2) {
long ret = atol(p);
free(s);
return ret;
}
}
free(s);
return 0;
}
static void mem_timeout_cb(void *d) {
((MemMonitor*)d)->update_status();
Fl::repeat_timeout(UPDATE_INTERVAL, mem_timeout_cb, d);
@ -42,25 +29,14 @@ MemMonitor::MemMonitor() : Fl_Box(0, 0, 45, 25), mem_usedp(0), swap_usedp(0) {
}
void MemMonitor::update_status(void) {
FILE *fd = fopen("/proc/meminfo", "r");
if(!fd) return;
struct sysinfo sys;
if(sysinfo(&sys) != 0) return;
long mem_total, mem_free, swap_total, swap_free;
mem_total = mem_free = swap_total = swap_free = 0;
static char buf[128];
while(fgets(buf, sizeof(buf), fd) != 0) {
if(STR_CMP(buf, "MemTotal:", 9))
mem_total = get_number(buf);
else if(STR_CMP(buf, "MemFree:", 8))
mem_free = get_number(buf);
else if(STR_CMP(buf, "SwapTotal:", 10))
swap_total = get_number(buf);
else if(STR_CMP(buf, "SwapFree:", 9))
swap_free = get_number(buf);
}
fclose(fd);
mem_total = (float)sys.totalram * (float)sys.mem_unit / 1048576.0f;
mem_free = (float)sys.freeram * (float)sys.mem_unit / 1048576.0f;
swap_total = (float)sys.totalswap * (float)sys.mem_unit / 1048576.0f;
swap_free = (float)sys.freeswap * (float)sys.mem_unit / 1048576.0f;
mem_usedp = 100 - (int)(((float)mem_free / (float)mem_total) * 100);
swap_usedp = 100 - (int)(((float)swap_free / (float)swap_total) * 100);