#include
#include
#include
#include
static int timer;
void cb_quit()
{
gtk_timeout_remove (timer);
gtk_main_quit();
}
float
cal_load (void)
{
FILE * fp;
static long pre_user, pre_nice, pre_system, pre_idle; //in order to extend easily later, i defined four vars separately
long user, nice, system, idle;
long d_user, d_nice, d_system, d_idle;
static long total;
float scale;
char buf[1024];
if ((fp = fopen ("/proc/stat", "r")) != NULL){
while (fgets (buf, sizeof(buf), fp)){
if( *buf == 'c' && *(buf+1) == 'p') break;
}
fclose (fp);
}
else
return 0.0;
sscanf(buf, "cpu %ld %ld %ld %ld", &user, &nice, &system, &idle);
// calculate delta value
d_user = user - pre_user;
d_nice = nice - pre_nice;
d_system = system - pre_system;
d_idle = idle - pre_idle;
total = d_user + d_nice + d_system + d_idle;
if (total < 1) total = 1;
scale = 1.0 / total;
pre_user = user;
pre_nice = nice;
pre_system = system;
pre_idle = idle;
return (d_user + d_nice + d_system) * scale;
}
gint Repaint (gpointer da)
{
GtkWidget * drawing_area = (GtkWidget *)da;
GdkDrawable * drawable = drawing_area->window;
GdkGC *gc_chart, *gc_text;
GdkColor color;
static unsigned coory[201];
int da_width, da_height;
int x;
float load;
char buf[30];
// set chart green
gc_chart = gdk_gc_new (drawing_area->window);
color.red = 0;
color.green = 0xffff;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_chart, &color );
// set text red
gc_text = gdk_gc_new (drawing_area->window);
color.red = 0xffff;
color.green = 0;
color.blue = 0;
gdk_gc_set_rgb_fg_color(gc_text, &color );