Rename panel, plugin, and plugin_class to Panel, Plugin, and
[lxde/lxpanel.git] / src / plugins / cpu / cpu.c
CommitLineData
a52c2257
HJYP
1/*
2 * CPU usage plugin to lxpanel
3 *
4 * Copyright (C) 2004 by Alexandre Pereira da Silva <alexandre.pereira@poli.usp.br>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
4542c20d 19 *
a52c2257
HJYP
20 */
21/*A little bug fixed by Mykola <mykola@2ka.mipt.ru>:) */
22
23
24#include <string.h>
25#include <sys/time.h>
26#include <time.h>
27#include <sys/sysinfo.h>
28#include <stdlib.h>
e7cb732b 29#include <glib/gi18n.h>
a52c2257
HJYP
30
31#include "plugin.h"
32#include "panel.h"
8ccd023a 33#include "misc.h"
a52c2257
HJYP
34
35#define KILOBYTE 1024
36#define MAX_WGSIZE 100
37
38//#define DEBUG
39#include "dbg.h"
40typedef unsigned long tick;
41
42struct cpu_stat {
43 tick u, n, s, i;
44};
45
46
47typedef struct {
48 GdkGC *gc_cpu;
49 GdkColor *ccpu;
50 GtkWidget *da;
51 GtkWidget *evbox;
52 GdkPixmap *pixmap;
3806c5aa 53 /* GtkTooltips *tip; */
a52c2257
HJYP
54
55 int timer;
56 tick *stats_cpu;
57 unsigned int ini_stats;
58 int Wwg;
59 int Hwg;
60 struct cpu_stat cpu_anterior;
61} cpu_t;
62
63
64static int
65cpu_update(cpu_t *c)
66{
67 int cpu_u=0, cpu_s=0, cpu_n=0, cpu_i=100;
68 unsigned int i;
69 struct cpu_stat cpu, cpu_r;
70 FILE *stat;
71 float total;
4542c20d 72
a52c2257
HJYP
73 ENTER;
74 if(!c->pixmap)
4542c20d
HJYP
75 RET(TRUE);
76
a52c2257
HJYP
77 stat = fopen("/proc/stat", "r");
78 if(!stat)
79 RET(TRUE);
80 fscanf(stat, "cpu %lu %lu %lu %lu", &cpu.u, &cpu.n, &cpu.s, &cpu.i);
81 fclose(stat);
82
83 cpu_r.u = cpu.u - c->cpu_anterior.u;
84 cpu_r.n = cpu.n - c->cpu_anterior.n;
85 cpu_r.s = cpu.s - c->cpu_anterior.s;
86 cpu_r.i = cpu.i - c->cpu_anterior.i;
87
88 total = cpu_r.u + cpu_r.n + cpu_r.s + cpu_r.i;
89 cpu_u = cpu_r.u * c->Hwg / total;
90 cpu_s = cpu_r.n * c->Hwg / total;
91 cpu_n = cpu_r.s * c->Hwg / total;
92 cpu_i = cpu_r.i * c->Hwg / total;
93
94 c->cpu_anterior = cpu;
4542c20d 95
a52c2257
HJYP
96 c->stats_cpu[c->ini_stats++] = cpu_u + cpu_s + cpu_n;
97 c->ini_stats %= c->Wwg;
98
99 gdk_draw_rectangle(c->pixmap, c->da->style->black_gc, TRUE, 0, 0, c->Wwg, c->Hwg);
100 for (i = 0; i < c->Wwg; i++) {
4542c20d
HJYP
101 int val;
102
103 val = c->stats_cpu[(i + c->ini_stats) % c->Wwg];
a52c2257
HJYP
104 if (val)
105 gdk_draw_line(c->pixmap, c->gc_cpu, i, c->Hwg, i, c->Hwg - val);
106 }
107 gtk_widget_queue_draw(c->da);
108 RET(TRUE);
109}
110
111static gint
112configure_event(GtkWidget *widget, GdkEventConfigure *event, cpu_t *c)
113{
114 ENTER;
115 if (c->pixmap)
116 g_object_unref(c->pixmap);
117 c->Wwg = widget->allocation.width;
118 c->Hwg = widget->allocation.height;
119 if (c->stats_cpu)
120 g_free(c->stats_cpu);
121 c->stats_cpu = g_new0( typeof(*c->stats_cpu), c->Wwg);
122 c->pixmap = gdk_pixmap_new (widget->window,
123 widget->allocation.width,
124 widget->allocation.height,
125 -1);
126 gdk_draw_rectangle (c->pixmap,
127 widget->style->black_gc,
128 TRUE,
129 0, 0,
130 widget->allocation.width,
131 widget->allocation.height);
4542c20d 132
a52c2257
HJYP
133 RET(TRUE);
134}
135
136
137static gint
138expose_event(GtkWidget *widget, GdkEventExpose *event, cpu_t *c)
139{
140 ENTER;
141 gdk_draw_drawable (widget->window,
142 c->da->style->black_gc,
143 c->pixmap,
144 event->area.x, event->area.y,
145 event->area.x, event->area.y,
146 event->area.width, event->area.height);
4542c20d 147
a52c2257
HJYP
148 RET(FALSE);
149}
150
151static int
22242ed4 152cpu_constructor(Plugin *p, char **fp)
a52c2257
HJYP
153{
154 cpu_t *c;
155
156 ENTER;
157 c = g_new0(cpu_t, 1);
158 p->priv = c;
159
4542c20d
HJYP
160 p->pwid = gtk_event_box_new();
161 GTK_WIDGET_SET_FLAGS( p->pwid, GTK_NO_WINDOW );
162
a52c2257
HJYP
163 c->da = gtk_drawing_area_new();
164 gtk_widget_set_size_request(c->da, 40, 20);
165
166 gtk_widget_show(c->da);
167
a52c2257 168 c->gc_cpu = gdk_gc_new(p->panel->topgwin->window);
22242ed4 169
a52c2257
HJYP
170 c->ccpu = (GdkColor *)malloc(sizeof(GdkColor));
171 gdk_color_parse("green", c->ccpu);
172 gdk_colormap_alloc_color(gdk_drawable_get_colormap(p->panel->topgwin->window), c->ccpu, FALSE, TRUE);
173 gdk_gc_set_foreground(c->gc_cpu, c->ccpu);
4542c20d 174
a52c2257
HJYP
175 gtk_container_add(GTK_CONTAINER(p->pwid), c->da);
176 gtk_container_set_border_width (GTK_CONTAINER (p->pwid), 1);
4542c20d 177
a52c2257
HJYP
178 g_signal_connect (G_OBJECT (c->da),"configure_event",
179 G_CALLBACK (configure_event), (gpointer) c);
180 g_signal_connect (G_OBJECT (c->da), "expose_event",
181 G_CALLBACK (expose_event), (gpointer) c);
4542c20d 182
a52c2257
HJYP
183 c->timer = g_timeout_add(1000, (GSourceFunc) cpu_update, (gpointer) c);
184 RET(1);
185}
186
187static void
22242ed4 188cpu_destructor(Plugin *p)
a52c2257
HJYP
189{
190 cpu_t *c = (cpu_t *) p->priv;
191
192 ENTER;
193 g_object_unref(c->pixmap);
194 g_object_unref(c->gc_cpu);
195 g_free(c->stats_cpu);
196 g_free(c->ccpu);
197 g_source_remove(c->timer);
3806c5aa 198 /* g_object_unref( c->tip ); */
a52c2257
HJYP
199 g_free(p->priv);
200 RET();
201}
202
203
22242ed4 204PluginClass cpu_plugin_class = {
a52c2257
HJYP
205 fname: NULL,
206 count: 0,
207
208 type : "cpu",
ce522551 209 name : N_("CPU Usage Monitor"),
a52c2257 210 version: "1.0",
ce522551 211 description : N_("Display CPU usage"),
a52c2257
HJYP
212
213 constructor : cpu_constructor,
214 destructor : cpu_destructor,
9c97f69e
HJYP
215 config : NULL,
216 save : NULL
a52c2257 217};