GTK+2 panel 

GTK+2 panel Git Source Tree

Root/monitor.c

1#include "customwidgets.h"
2
3#include <stdio.h>
4
5
6typedef struct
7{
8 GtkWidget *widget;
9 guint poll;
10
11 GdkPixmap *offscreen;
12 gint width;
13 gint height;
14
15 unsigned long user;
16 unsigned long nice;
17 unsigned long system;
18 unsigned long idle;
19} GpMonitor;
20
21
22static gbooleangp_monitor_configure(GpMonitor*monitor,
23 GdkEventConfigure*event);
24static gbooleangp_monitor_update(GpMonitor*monitor);
25static gbooleangp_monitor_expose(GpMonitor*monitor,
26 GdkEventExpose*event);
27static voidgp_monitor_destroy(GpMonitor*monitor);
28
29
30GtkWidget *
31gp_monitor_new (gchar *widget_name,
32gchar *string1,
33gchar *string2,
34gint int1,
35gint int2)
36{
37 GpMonitor *monitor;
38
39 monitor = g_new0 (GpMonitor, 1);
40
41 monitor->widget = gtk_drawing_area_new ();
42 gtk_widget_set_name (monitor->widget, widget_name);
43 gtk_widget_set_size_request (monitor->widget, 40, -1);
44
45 g_signal_connect_swapped (monitor->widget,"configure-event",
46 G_CALLBACK (gp_monitor_configure), monitor);
47 g_signal_connect_swapped (monitor->widget, "expose-event",
48 G_CALLBACK (gp_monitor_expose), monitor);
49 g_signal_connect_swapped (monitor->widget, "destroy",
50 G_CALLBACK (gp_monitor_destroy), monitor);
51
52 monitor->poll = g_timeout_add (1000, (GSourceFunc) gp_monitor_update, monitor);
53
54 return monitor->widget;
55}
56
57
58static gboolean
59gp_monitor_configure (GpMonitor *monitor,
60 GdkEventConfigure *event)
61{
62 GtkWidget *widget;
63 gint width, height;
64
65 widget = monitor->widget;
66 width = widget->allocation.width;
67 height = widget->allocation.height;
68
69 if (monitor->offscreen)
70 {
71 if (monitor->width == width && monitor->height == height)
72return TRUE;
73
74 /* TODO: the widget size is changed, so I have to recreate the offscreen
75 * buffer. Now, the old offscreen content is discarded ... this is not
76 * the better solution! */
77 g_object_unref (monitor->offscreen);
78 }
79
80 monitor->offscreen = gdk_pixmap_new (widget->window, width, height, -1);
81 monitor->width = width;
82 monitor->height = height;
83 gdk_draw_rectangle (monitor->offscreen,
84 widget->style->bg_gc[GTK_STATE_ACTIVE],
85 TRUE,
86 0, 0,
87 width, height);
88
89 return TRUE;
90}
91
92static gboolean
93gp_monitor_update (GpMonitor *monitor)
94{
95 FILE *stream;
96 int result;
97 unsigned long user, nice, system, idle;
98 unsigned long d_user, d_nice, d_system, d_idle, d_total;
99 unsigned long quotient, error;
100 unsigned long l_user, l_nice, l_system;
101 GtkStyle *style;
102 ginty;
103
104 stream = fopen ("/proc/stat", "r");
105 if (stream == NULL)
106 return FALSE;
107
108 result = fscanf (stream, "cpu %lu %lu %lu %lu", &user, &nice, &system, &idle);
109 fclose (stream);
110
111 /* Read failed: try next time */
112 if (result == EOF)
113 return TRUE;
114
115 d_user = user - monitor->user;
116 d_nice = nice - monitor->nice;
117 d_system = system - monitor->system;
118 d_idle = idle - monitor->idle;
119 d_total = d_user + d_nice + d_system + d_idle;
120
121 /* Better to check d_total validity ... */
122 if (d_total <= 0)
123 return TRUE;
124
125 quotient = d_user * monitor->height;
126 l_user = quotient / d_total;
127 error = quotient % d_total;
128
129 quotient = d_nice * monitor->height + error;
130 l_nice = quotient / d_total;
131 error = quotient % d_total;
132
133 quotient = d_system * monitor->height + error;
134 l_system = quotient / d_total;
135 error = quotient % d_total;
136
137 style = monitor->widget->style;
138 gdk_draw_drawable (monitor->offscreen,
139 style->bg_gc[GTK_STATE_ACTIVE],
140 monitor->offscreen,
141 0, 0,
142 1, 0,
143 monitor->width-1, -1);
144
145 y = (gint) monitor->height;
146 if (l_user)
147 {
148 gdk_draw_line (monitor->offscreen,
149 style->fg_gc[GTK_STATE_ACTIVE],
150 0, y,
151 0, y-l_user-1);
152 y -= l_user;
153 }
154 if (l_nice)
155 {
156 gdk_draw_line (monitor->offscreen,
157 style->fg_gc[GTK_STATE_PRELIGHT],
158 0, y,
159 0, y-l_nice-1);
160 y -= l_nice;
161 }
162 if (l_system)
163 {
164 gdk_draw_line (monitor->offscreen,
165 style->fg_gc[GTK_STATE_SELECTED],
166 0, y,
167 0, y-l_system-1);
168 y -= l_system;
169 }
170 if (y > 0)
171 gdk_draw_line (monitor->offscreen,
172 style->bg_gc[GTK_STATE_ACTIVE],
173 0, y,
174 0, 0);
175
176 monitor->user = user;
177 monitor->nice = nice;
178 monitor->system = system;
179 monitor->idle = idle;
180
181 gtk_widget_queue_draw (monitor->widget);
182 return TRUE;
183}
184
185static gboolean
186gp_monitor_expose (GpMonitor *monitor,
187 GdkEventExpose *event)
188{
189 gdk_draw_drawable (monitor->widget->window,
190 monitor->widget->style->bg_gc[GTK_STATE_ACTIVE],
191 monitor->offscreen,
192 event->area.x, event->area.y,
193 event->area.x, event->area.y,
194 event->area.width, event->area.height);
195
196 return FALSE;
197}
198
199static void
200gp_monitor_destroy (GpMonitor *monitor)
201{
202 if (monitor->poll > 0)
203 g_source_remove (monitor->poll);
204
205 if (monitor->offscreen != NULL)
206 g_object_unref (monitor->offscreen);
207
208 g_free (monitor);
209}

Archive Download this file

Branches

Tags