1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* CPU usage plugin to fbpanel
*
* Copyright (C) 2004 by Alexandre Pereira da Silva <alexandre.pereira@poli.usp.br>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*A little bug fixed by Mykola <mykola@2ka.mipt.ru>:) */
#include <string.h>
#include <sys/time.h>
#include <time.h>
#include <sys/sysinfo.h>
#include <stdlib.h>
#include "plugin.h"
#include "panel.h"
#include "gtkbgbox.h"
#define KILOBYTE 1024
#define MAX_WGSIZE 100
//#define DEBUG
#include "dbg.h"
typedef unsigned long tick;
struct cpu_stat {
tick u, n, s, i;
};
typedef struct {
GdkGC *gc_cpu;
GdkColor *ccpu;
GtkWidget *da;
GtkWidget *evbox;
GdkPixmap *pixmap;
GtkTooltips *tip;
int timer;
tick *stats_cpu;
unsigned int ini_stats;
int Wwg;
int Hwg;
struct cpu_stat cpu_anterior;
} cpu_t;
static int
cpu_update(cpu_t *c)
{
int cpu_u=0, cpu_s=0, cpu_n=0, cpu_i=100;
unsigned int i;
struct cpu_stat cpu, cpu_r;
FILE *stat;
float total;
ENTER;
if(!c->pixmap)
RET(TRUE);
stat = fopen("/proc/stat", "r");
if(!stat)
RET(TRUE);
fscanf(stat, "cpu %lu %lu %lu %lu", &cpu.u, &cpu.n, &cpu.s, &cpu.i);
fclose(stat);
cpu_r.u = cpu.u - c->cpu_anterior.u;
cpu_r.n = cpu.n - c->cpu_anterior.n;
cpu_r.s = cpu.s - c->cpu_anterior.s;
cpu_r.i = cpu.i - c->cpu_anterior.i;
total = cpu_r.u + cpu_r.n + cpu_r.s + cpu_r.i;
cpu_u = cpu_r.u * c->Hwg / total;
cpu_s = cpu_r.n * c->Hwg / total;
cpu_n = cpu_r.s * c->Hwg / total;
cpu_i = cpu_r.i * c->Hwg / total;
c->cpu_anterior = cpu;
c->stats_cpu[c->ini_stats++] = cpu_u + cpu_s + cpu_n;
c->ini_stats %= c->Wwg;
gdk_draw_rectangle(c->pixmap, c->da->style->black_gc, TRUE, 0, 0, c->Wwg, c->Hwg);
for (i = 0; i < c->Wwg; i++) {
int val;
val = c->stats_cpu[(i + c->ini_stats) % c->Wwg];
if (val)
gdk_draw_line(c->pixmap, c->gc_cpu, i, c->Hwg, i, c->Hwg - val);
}
gtk_widget_queue_draw(c->da);
RET(TRUE);
}
static gint
configure_event(GtkWidget *widget, GdkEventConfigure *event, cpu_t *c)
{
ENTER;
if (c->pixmap)
g_object_unref(c->pixmap);
c->Wwg = widget->allocation.width;
c->Hwg = widget->allocation.height;
if (c->stats_cpu)
g_free(c->stats_cpu);
c->stats_cpu = g_new0( typeof(*c->stats_cpu), c->Wwg);
c->pixmap = gdk_pixmap_new (widget->window,
widget->allocation.width,
widget->allocation.height,
-1);
gdk_draw_rectangle (c->pixmap,
widget->style->black_gc,
TRUE,
0, 0,
widget->allocation.width,
widget->allocation.height);
RET(TRUE);
}
static gint
expose_event(GtkWidget *widget, GdkEventExpose *event, cpu_t *c)
{
ENTER;
gdk_draw_drawable (widget->window,
c->da->style->black_gc,
c->pixmap,
event->area.x, event->area.y,
event->area.x, event->area.y,
event->area.width, event->area.height);
RET(FALSE);
}
static int
cpu_constructor(plugin *p)
{
cpu_t *c;
ENTER;
c = g_new0(cpu_t, 1);
p->priv = c;
c->da = gtk_drawing_area_new();
gtk_widget_set_size_request(c->da, 40, 30);
gtk_widget_show(c->da);
c->tip = gtk_tooltips_new();
c->gc_cpu = gdk_gc_new(p->panel->topgwin->window);
DBG("here1\n");
c->ccpu = (GdkColor *)malloc(sizeof(GdkColor));
gdk_color_parse("green", c->ccpu);
gdk_colormap_alloc_color(gdk_drawable_get_colormap(p->panel->topgwin->window), c->ccpu, FALSE, TRUE);
gdk_gc_set_foreground(c->gc_cpu, c->ccpu);
gtk_bgbox_set_background(p->pwid, BG_STYLE, 0, 0);
gtk_container_add(GTK_CONTAINER(p->pwid), c->da);
gtk_container_set_border_width (GTK_CONTAINER (p->pwid), 1);
g_signal_connect (G_OBJECT (c->da),"configure_event",
G_CALLBACK (configure_event), (gpointer) c);
g_signal_connect (G_OBJECT (c->da), "expose_event",
G_CALLBACK (expose_event), (gpointer) c);
c->timer = g_timeout_add(1000, (GSourceFunc) cpu_update, (gpointer) c);
RET(1);
}
static void
cpu_destructor(plugin *p)
{
cpu_t *c = (cpu_t *) p->priv;
ENTER;
g_object_unref(c->pixmap);
g_object_unref(c->gc_cpu);
g_free(c->stats_cpu);
g_free(c->ccpu);
g_source_remove(c->timer);
g_free(p->priv);
RET();
}
plugin_class cpu_plugin_class = {
fname: NULL,
count: 0,
type : "cpu",
name : "Cpu usage",
version: "1.0",
description : "Display cpu usage",
constructor : cpu_constructor,
destructor : cpu_destructor,
};
|