2013-01-09 18:04:51 +04:00
|
|
|
/*
|
|
|
|
* $Id: Panel.cpp 3512 2013-01-09 13:45:49Z karijes $
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012-2013 Sanel Zukan
|
|
|
|
*
|
|
|
|
* 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 2
|
|
|
|
* 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <FL/Fl_Box.H>
|
|
|
|
#include "Applet.h"
|
|
|
|
#include <edelib/Nls.h>
|
2013-01-09 17:45:49 +04:00
|
|
|
|
2013-01-11 23:03:37 +04:00
|
|
|
#if defined(linux)
|
2013-01-09 18:04:51 +04:00
|
|
|
#include <sys/sysinfo.h>
|
2011-09-30 13:47:20 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-09-30 15:41:22 +04:00
|
|
|
#include <FL/Fl.H>
|
|
|
|
#include <FL/fl_draw.H>
|
|
|
|
#include <edelib/Color.h>
|
2011-09-30 13:47:20 +04:00
|
|
|
#include <edelib/Missing.h>
|
|
|
|
|
2011-09-30 15:41:22 +04:00
|
|
|
EDELIB_NS_USING(color_rgb_to_fltk)
|
|
|
|
|
2013-01-11 23:03:37 +04:00
|
|
|
#define UPDATE_INTERVAL 2.0f
|
2011-09-30 13:47:20 +04:00
|
|
|
#define STR_CMP(first, second, n) (strncmp(first, second, n) == 0)
|
|
|
|
|
2013-01-09 18:04:51 +04:00
|
|
|
class MemMonitor : public Fl_Box {
|
|
|
|
private:
|
|
|
|
int mem_usedp, swap_usedp;
|
|
|
|
public:
|
|
|
|
MemMonitor() : Fl_Box(0, 0, 45, 25), mem_usedp(0), swap_usedp(0) {
|
|
|
|
box(FL_THIN_DOWN_BOX);
|
2013-01-11 23:03:37 +04:00
|
|
|
update_status();
|
2013-01-09 18:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void update_status(void);
|
|
|
|
void draw(void);
|
|
|
|
int handle(int e);
|
|
|
|
};
|
|
|
|
|
2013-01-09 17:45:49 +04:00
|
|
|
inline int height_from_perc(int perc, int h) {
|
2011-09-30 15:41:22 +04:00
|
|
|
return (perc * h) / 100;
|
|
|
|
}
|
|
|
|
|
2011-09-30 13:47:20 +04:00
|
|
|
static void mem_timeout_cb(void *d) {
|
|
|
|
((MemMonitor*)d)->update_status();
|
|
|
|
Fl::repeat_timeout(UPDATE_INTERVAL, mem_timeout_cb, d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemMonitor::update_status(void) {
|
2013-01-09 17:45:49 +04:00
|
|
|
struct sysinfo sys;
|
|
|
|
if(sysinfo(&sys) != 0) return;
|
2011-09-30 13:47:20 +04:00
|
|
|
|
|
|
|
long mem_total, mem_free, swap_total, swap_free;
|
2013-01-09 17:45:49 +04:00
|
|
|
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;
|
2011-09-30 13:47:20 +04:00
|
|
|
|
|
|
|
mem_usedp = 100 - (int)(((float)mem_free / (float)mem_total) * 100);
|
2012-12-05 19:14:13 +04:00
|
|
|
swap_usedp = 100 - (int)(((float)swap_free / (float)swap_total) * 100);
|
2011-09-30 15:41:22 +04:00
|
|
|
|
|
|
|
static char tip[100];
|
2012-12-05 19:14:13 +04:00
|
|
|
snprintf(tip, sizeof(tip), _("Memory used: %i%%\nSwap used: %i%%"), mem_usedp, swap_usedp);
|
2011-09-30 15:41:22 +04:00
|
|
|
tooltip(tip);
|
|
|
|
|
|
|
|
redraw();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemMonitor::draw(void) {
|
|
|
|
draw_box();
|
|
|
|
int W, W2, H, X, Y, mh, sh;
|
|
|
|
|
|
|
|
W = w() - Fl::box_dw(box());
|
|
|
|
H = h() - Fl::box_dh(box());
|
|
|
|
X = x() + Fl::box_dx(box());
|
|
|
|
Y = y() + Fl::box_dy(box());
|
|
|
|
W2 = W / 2;
|
|
|
|
|
|
|
|
mh = height_from_perc(mem_usedp, H);
|
|
|
|
sh = height_from_perc(swap_usedp, H);
|
|
|
|
|
2011-10-29 20:10:41 +04:00
|
|
|
fl_rectf(X, Y + H - mh, W2, mh, (Fl_Color)color_rgb_to_fltk(166, 48, 48));
|
|
|
|
fl_rectf(X + W2, Y + H - sh, W2, sh, (Fl_Color)color_rgb_to_fltk(54, 136, 79));
|
2011-09-30 13:47:20 +04:00
|
|
|
}
|
|
|
|
|
2012-04-11 20:15:01 +04:00
|
|
|
int MemMonitor::handle(int e) {
|
|
|
|
switch(e) {
|
|
|
|
case FL_SHOW: {
|
|
|
|
int ret = Fl_Box::handle(e);
|
|
|
|
Fl::add_timeout(UPDATE_INTERVAL, mem_timeout_cb, this);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
case FL_HIDE:
|
|
|
|
Fl::remove_timeout(mem_timeout_cb);
|
|
|
|
/* fallthrough */
|
|
|
|
}
|
|
|
|
|
|
|
|
return Fl_Box::handle(e);
|
|
|
|
}
|
|
|
|
|
2013-01-09 18:04:51 +04:00
|
|
|
#else
|
|
|
|
|
|
|
|
class MemMonitor : public Fl_Box {
|
|
|
|
public:
|
|
|
|
MemMonitor() : Fl_Box(0, 0, 45, 25) {
|
|
|
|
box(FL_THIN_DOWN_BOX);
|
|
|
|
label("n/a");
|
|
|
|
tooltip(_("Memory information not available for this platform"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* linux */
|
|
|
|
|
2011-09-30 13:47:20 +04:00
|
|
|
EDE_PANEL_APPLET_EXPORT (
|
|
|
|
MemMonitor,
|
|
|
|
EDE_PANEL_APPLET_OPTION_ALIGN_RIGHT,
|
|
|
|
"Memory monitor",
|
2013-01-09 18:04:51 +04:00
|
|
|
"0.2",
|
2011-09-30 13:47:20 +04:00
|
|
|
"empty",
|
|
|
|
"Sanel Zukan"
|
|
|
|
)
|