Patch for building LXPanel with GTK3
[lxde/lxpanel.git] / plugins / monitors / monitors.c
1 /**
2 * Plugin for the lxpanel.
3 *
4 * Displays several monitors in the panel.
5 *
6 * A lot of code in this plugin comes from the CPU plugin (that only displays a
7 * CPU monitor), that is distributed under the following terms :
8 *
9 * Copyright (C) 2010 Cyril Roelandt <steap@users.sourceforge.net>
10 * 2012-2014 Henry Gebhardt <hsggebhardt@googlemail.com>
11 * 2012 Rafał Mużyło <galtgendo@gmail.com>
12 * 2014 Andriy Grytsenko <andrej@rep.kiev.ua>
13 *
14 * <terms>
15 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
16 * Copyright (C) 2004 by Alexandre Pereira da Silva <alexandre.pereira@poli.usp.br>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 * </terms>
32 *
33 */
34
35 /*
36 * HOWTO : Add your own monitor for the resource "foo".
37 *
38 * 1) Write the foo_update() function, that fills in the stats.
39 * 2) Write the foo_tooltip_update() function, that updates your tooltip. This
40 * is optional, but recommended.
41 * 3) Add a #define FOO_POSITION, and increment N_MONITORS.
42 * 4) Add :
43 * - the default color of your plugin ("default_colors" table)
44 * - the update function ("update_functions" table)
45 * - the tooltip update function ("tooltip_update" table)
46 * 5) Configuration :
47 * - edit the monitors_config() function so that a "Display FOO usage"
48 * checkbox appears in the prefs dialog.
49 * - edit the monitors_save() function so that a "DisplayFOO" string appears
50 * in the config file ("~/.config/lxpanel/<profile>/config")
51 * - edit the monitors_config() function so that a "FOO color" entry appears
52 * in the prefs dialog.
53 * - edit the monitors_save() function so that a "FOOColor" string appears
54 * in the config file.
55 * - edit the monitors_constructor() function so that options are correctly
56 * aplied. Adding something like :
57 *
58 * else if (g_ascii_strcasecmp(s.t[0], "DisplayFOO") == 0)
59 * mp->displayed_monitors[FOO_POSITION] = atoi(s.t[1])
60 * else if (g_ascii_strcasecmp(s.t[0], "FOOColor") == 0)
61 * colors[FOO_POSITION] = g_strndup(s.t[1], COLOR_SIZE-1);
62 *
63 * should be enough.
64 * 6) Enjoy.
65 */
66
67 /*
68 * FIXME : known BUGS :
69 * - when removing a monitor and re-adding it, it is drawn with a white
70 * border of BORDER_SIZE pixels around it.
71 */
72
73 #include <stdlib.h>
74 #include <glib/gi18n.h>
75 #include <errno.h>
76 #include <libfm/fm-gtk.h>
77
78 #include "plugin.h"
79
80 #include "dbg.h"
81
82
83 #define PLUGIN_NAME "MonitorsPlugin"
84 #define BORDER_SIZE 2 /* Pixels */
85 #define DEFAULT_WIDTH 40 /* Pixels */
86 #define UPDATE_PERIOD 1 /* Seconds */
87 #define COLOR_SIZE 8 /* In chars : #xxxxxx\0 */
88
89 #ifndef ENTER
90 #define ENTER fprintf(stderr, "Entering %s\n", __func__);
91 #endif
92
93 /*
94 * Stats are stored in a circular buffer.
95 * Newest values are on the left of the ring cursor.
96 * Oldest values are on the right of the ring cursor.
97 */
98 typedef float stats_set;
99
100 struct Monitor {
101 GdkColor foreground_color; /* Foreground color for drawing area */
102 GtkWidget *da; /* Drawing area */
103 cairo_surface_t *pixmap; /* Pixmap to be drawn on drawing area */
104 gint pixmap_width; /* Width and size of the buffer */
105 gint pixmap_height; /* Does not include border size */
106 stats_set *stats; /* Circular buffer of values */
107 stats_set total; /* Maximum possible value, as in mem_total*/
108 gint ring_cursor; /* Cursor for ring/circular buffer */
109 gchar *color; /* Color of the graph */
110 gboolean (*update) (struct Monitor *); /* Update function */
111 void (*update_tooltip) (struct Monitor *);
112 };
113
114 typedef struct Monitor Monitor;
115 typedef gboolean (*update_func) (Monitor *);
116 typedef void (*tooltip_update_func) (Monitor *);
117
118 /*
119 * Position of our monitors : monitor 0 will always be on the left of the
120 * plugin, monitor 1 on the right of monitor 0 (or on the left of the plugin if
121 * monitor 0 is not displayed), etc.
122 */
123 #define CPU_POSITION 0
124 #define MEM_POSITION 1
125 #define N_MONITORS 2
126
127 /* Our plugin */
128 typedef struct {
129 LXPanel *panel;
130 config_setting_t *settings;
131 Monitor *monitors[N_MONITORS]; /* Monitors */
132 int displayed_monitors[N_MONITORS]; /* Booleans */
133 char *action; /* What to do on click */
134 guint timer; /* Timer for regular updates */
135 } MonitorsPlugin;
136
137 /*
138 * Prototypes
139 */
140 static void monitor_set_foreground_color(MonitorsPlugin *, Monitor *, const gchar *);
141
142 /* CPU Monitor */
143 static gboolean cpu_update(Monitor *);
144 static void cpu_tooltip_update (Monitor *m);
145
146 /* RAM Monitor */
147 static gboolean mem_update(Monitor *);
148 static void mem_tooltip_update (Monitor *m);
149
150
151 static gboolean configure_event(GtkWidget*, GdkEventConfigure*, gpointer);
152 #if !GTK_CHECK_VERSION(3, 0, 0)
153 static gboolean expose_event(GtkWidget *, GdkEventExpose *, Monitor *);
154 #else
155 static gboolean draw(GtkWidget *, cairo_t *, Monitor *);
156 #endif
157 static void redraw_pixmap (Monitor *m);
158
159 /* Monitors functions */
160 static void monitors_destructor(gpointer);
161 static gboolean monitors_apply_config(gpointer);
162
163
164 /******************************************************************************
165 * Monitor functions *
166 ******************************************************************************/
167 static Monitor*
168 monitor_init(MonitorsPlugin *mp, Monitor *m, gchar *color)
169 {
170 ENTER;
171
172 m->da = gtk_drawing_area_new();
173 gtk_widget_set_size_request(m->da, DEFAULT_WIDTH, panel_get_height(mp->panel));
174 gtk_widget_add_events(m->da, GDK_BUTTON_PRESS_MASK);
175
176 monitor_set_foreground_color(mp, m, color);
177
178 /* Signals */
179 g_signal_connect(G_OBJECT(m->da), "configure-event",
180 G_CALLBACK(configure_event), (gpointer) m);
181 #if !GTK_CHECK_VERSION(3, 0, 0)
182 g_signal_connect (G_OBJECT(m->da), "expose-event",
183 G_CALLBACK(expose_event), (gpointer) m);
184 #else
185 g_signal_connect (G_OBJECT(m->da), "draw",
186 G_CALLBACK(draw), (gpointer) m);
187 #endif
188 /* g_signal_connect(G_OBJECT(m->da), "button-press-event",
189 G_CALLBACK(plugin_button_press_event), p); */
190
191 return m;
192 }
193
194 static void
195 monitor_free(Monitor *m)
196 {
197 if (!m)
198 return;
199
200 g_free(m->color);
201 if (m->pixmap)
202 cairo_surface_destroy(m->pixmap);
203 if (m->stats)
204 g_free(m->stats);
205 g_free(m);
206
207 return;
208 }
209
210 static void
211 monitor_set_foreground_color(MonitorsPlugin *mp, Monitor *m, const gchar *color)
212 {
213 g_free(m->color);
214 m->color = g_strndup(color, COLOR_SIZE - 1);
215 gdk_color_parse(color, &m->foreground_color);
216 }
217 /******************************************************************************
218 * End of monitor functions *
219 ******************************************************************************/
220
221 /******************************************************************************
222 * CPU monitor *
223 ******************************************************************************/
224 typedef unsigned long long CPUTick;/* Value from /proc/stat */
225 typedef float CPUSample; /* Saved CPU utilization value as 0.0..1.0 */
226
227 struct cpu_stat {
228 CPUTick u, n, s, i; /* User, nice, system, idle */
229 };
230
231 static gboolean
232 cpu_update(Monitor * c)
233 {
234 static struct cpu_stat previous_cpu_stat = { 0, 0, 0, 0 };
235
236 if ((c->stats != NULL) && (c->pixmap != NULL))
237 {
238 /* Open statistics file and scan out CPU usage. */
239 struct cpu_stat cpu;
240 FILE * stat = fopen("/proc/stat", "r");
241 if (stat == NULL)
242 return TRUE;
243 int fscanf_result = fscanf(stat, "cpu %llu %llu %llu %llu",
244 &cpu.u, &cpu.n, &cpu.s, &cpu.i);
245 fclose(stat);
246
247 /* Ensure that fscanf succeeded. */
248 if (fscanf_result == 4)
249 {
250 /* Comcolors delta from previous statistics. */
251 struct cpu_stat cpu_delta;
252 cpu_delta.u = cpu.u - previous_cpu_stat.u;
253 cpu_delta.n = cpu.n - previous_cpu_stat.n;
254 cpu_delta.s = cpu.s - previous_cpu_stat.s;
255 cpu_delta.i = cpu.i - previous_cpu_stat.i;
256
257 /* Copy current to previous. */
258 memcpy(&previous_cpu_stat, &cpu, sizeof(struct cpu_stat));
259
260 /* Comcolors user+nice+system as a fraction of total.
261 * Introduce this sample to ring buffer, increment and wrap ring
262 * buffer cursor. */
263 float cpu_uns = cpu_delta.u + cpu_delta.n + cpu_delta.s;
264 c->stats[c->ring_cursor] = cpu_uns / (cpu_uns + cpu_delta.i);
265 c->ring_cursor += 1;
266 if (c->ring_cursor >= c->pixmap_width)
267 c->ring_cursor = 0;
268
269 /* Redraw with the new sample. */
270 redraw_pixmap(c);
271 }
272 }
273 return TRUE;
274 }
275
276 static void
277 cpu_tooltip_update (Monitor *m)
278 {
279 if (m && m->stats) {
280 gchar *tooltip_text;
281 gint ring_pos = (m->ring_cursor == 0)
282 ? m->pixmap_width - 1 : m->ring_cursor - 1;
283 tooltip_text = g_strdup_printf(_("CPU usage: %.2f%%"),
284 m->stats[ring_pos] * 100);
285 gtk_widget_set_tooltip_text(m->da, tooltip_text);
286 g_free(tooltip_text);
287 }
288 }
289
290 /******************************************************************************
291 * End of CPU Monitor *
292 ******************************************************************************/
293
294 /******************************************************************************
295 * RAM Monitor *
296 ******************************************************************************/
297 static gboolean
298 mem_update(Monitor * m)
299 {
300 ENTER;
301
302 FILE *meminfo;
303 char buf[80];
304 long int mem_total = 0;
305 long int mem_free = 0;
306 long int mem_buffers = 0;
307 long int mem_cached = 0;
308 unsigned int readmask = 0x8 | 0x4 | 0x2 | 0x1;
309
310 if (!m->stats || !m->pixmap)
311 RET(TRUE);
312
313 meminfo = fopen("/proc/meminfo", "r");
314 if (!meminfo) {
315 g_warning("monitors: Could not open /proc/meminfo: %d, %s",
316 errno, strerror(errno));
317 RET(FALSE);
318 }
319
320 while (readmask && fgets(buf, sizeof(buf), meminfo)) {
321 if (sscanf(buf, "MemTotal: %ld kB\n", &mem_total) == 1) {
322 readmask ^= 0x1;
323 continue;
324 }
325 if (sscanf(buf, "MemFree: %ld kB\n", &mem_free) == 1) {
326 readmask ^= 0x2;
327 continue;
328 }
329 if (sscanf(buf, "Buffers: %ld kB\n", &mem_buffers) == 1) {
330 readmask ^= 0x4;
331 continue;
332 }
333 if (sscanf(buf, "Cached: %ld kB\n", &mem_cached) == 1) {
334 readmask ^= 0x8;
335 continue;
336 }
337 }
338
339 fclose(meminfo);
340
341 if (readmask) {
342 g_warning("monitors: Couldn't read all values from /proc/meminfo: "
343 "readmask %x", readmask);
344 RET(FALSE);
345 }
346
347 m->total = mem_total;
348
349 /* Adding stats to the buffer:
350 * It is debatable if 'mem_buffers' counts as free or not. I'll go with
351 * 'free', because it can be flushed fairly quickly, and generally
352 * isn't necessary to keep in memory.
353 * It is hard to draw the line, which caches should be counted as free,
354 * and which not. Pagecaches, dentry, and inode caches are quickly
355 * filled up again for almost any use case. Hence I would not count
356 * them as 'free'.
357 * 'mem_cached' definitely counts as 'free' because it is immediately
358 * released should any application need it. */
359 m->stats[m->ring_cursor] = (mem_total - mem_buffers - mem_free -
360 mem_cached) / (float)mem_total;
361
362 m->ring_cursor++;
363 if (m->ring_cursor >= m->pixmap_width)
364 m->ring_cursor = 0;
365
366 /* Redraw the pixmap, with the new sample */
367 redraw_pixmap (m);
368
369 RET(TRUE);
370 }
371
372 static void
373 mem_tooltip_update (Monitor *m)
374 {
375 if (m && m->stats) {
376 gchar *tooltip_text;
377 gint ring_pos = (m->ring_cursor == 0)
378 ? m->pixmap_width - 1 : m->ring_cursor - 1;
379 tooltip_text = g_strdup_printf(_("RAM usage: %.1fMB (%.2f%%)"),
380 m->stats[ring_pos] * m->total / 1024,
381 m->stats[ring_pos] * 100);
382 gtk_widget_set_tooltip_text(m->da, tooltip_text);
383 g_free(tooltip_text);
384 }
385 }
386 /******************************************************************************
387 * End of RAM Monitor *
388 ******************************************************************************/
389
390 /******************************************************************************
391 * Basic events handlers *
392 ******************************************************************************/
393 static gboolean
394 configure_event(GtkWidget* widget, GdkEventConfigure* dummy, gpointer data)
395 {
396 (void) dummy;
397 GtkAllocation allocation;
398
399 int new_pixmap_width, new_pixmap_height;
400
401 gtk_widget_get_allocation(widget, &allocation);
402 new_pixmap_width = allocation.width - BORDER_SIZE * 2;
403 new_pixmap_height = allocation.height - BORDER_SIZE *2;
404 Monitor *m;
405
406 m = (Monitor *) data;
407
408 if (new_pixmap_width > 0 && new_pixmap_height > 0)
409 {
410 /*
411 * If the stats buffer does not exist (first time we get inside this
412 * function) or its size changed, reallocate the buffer and preserve
413 * existing data.
414 */
415 if (!m->stats || (new_pixmap_width != m->pixmap_width))
416 {
417 stats_set *new_stats = g_new0(stats_set, new_pixmap_width);
418
419 if (!new_stats)
420 return TRUE;
421
422 if (m->stats)
423 {
424 /* New allocation is larger.
425 * Add new "oldest" samples of zero following the cursor*/
426 if (new_pixmap_width > m->pixmap_width)
427 {
428 /* Number of values between the ring cursor and the end of
429 * the buffer */
430 int nvalues = m->pixmap_width - m->ring_cursor;
431
432 memcpy(new_stats,
433 m->stats,
434 m->ring_cursor * sizeof (stats_set));
435 memcpy(new_stats + nvalues,
436 m->stats + m->ring_cursor,
437 nvalues * sizeof(stats_set));
438 }
439 /* New allocation is smaller, but still larger than the ring
440 * buffer cursor */
441 else if (m->ring_cursor <= new_pixmap_width)
442 {
443 /* Numver of values that can be stored between the end of
444 * the new buffer and the ring cursor */
445 int nvalues = new_pixmap_width - m->ring_cursor;
446 memcpy(new_stats,
447 m->stats,
448 m->ring_cursor * sizeof(stats_set));
449 memcpy(new_stats + m->ring_cursor,
450 m->stats + m->pixmap_width - nvalues,
451 nvalues * sizeof(stats_set));
452 }
453 /* New allocation is smaller, and also smaller than the ring
454 * buffer cursor. Discard all oldest samples following the ring
455 * buffer cursor and additional samples at the beginning of the
456 * buffer. */
457 else
458 {
459 memcpy(new_stats,
460 m->stats + m->ring_cursor - new_pixmap_width,
461 new_pixmap_width * sizeof(stats_set));
462 }
463 g_free(m->stats);
464 }
465 m->stats = new_stats;
466 }
467
468 m->pixmap_width = new_pixmap_width;
469 m->pixmap_height = new_pixmap_height;
470 if (m->pixmap)
471 cairo_surface_destroy(m->pixmap);
472 m->pixmap = cairo_image_surface_create(CAIRO_FORMAT_RGB24,
473 m->pixmap_width,
474 m->pixmap_height);
475 check_cairo_surface_status(&m->pixmap);
476 redraw_pixmap(m);
477 }
478
479 return TRUE;
480 }
481
482 #if !GTK_CHECK_VERSION(3, 0, 0)
483 static gboolean
484 expose_event(GtkWidget * widget, GdkEventExpose * event, Monitor *m)
485 {
486 /* Draw the requested part of the pixmap onto the drawing area.
487 * Translate it in both x and y by the border size. */
488 if (m->pixmap != NULL)
489 {
490 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
491 GtkStyle *style = gtk_widget_get_style(m->da);
492 gdk_cairo_region(cr, event->region);
493 cairo_clip(cr);
494 gdk_cairo_set_source_color(cr, &style->black);
495 cairo_set_source_surface(cr, m->pixmap, BORDER_SIZE, BORDER_SIZE);
496 cairo_paint(cr);
497 check_cairo_status(cr);
498 cairo_destroy(cr);
499 }
500
501 return FALSE;
502 }
503 #else
504 static gboolean
505 draw(GtkWidget * widget, cairo_t * cr, Monitor *m)
506 {
507 /* Draw the requested part of the pixmap onto the drawing area.
508 * Translate it in both x and y by the border size. */
509 if (m->pixmap != NULL)
510 {
511 cairo_set_source_rgb(cr, 0, 0, 0);
512 cairo_set_source_surface(cr, m->pixmap, BORDER_SIZE, BORDER_SIZE);
513 cairo_paint(cr);
514 check_cairo_status(cr);
515 }
516
517 return FALSE;
518 }
519 #endif
520
521
522 static gboolean monitors_button_press_event(GtkWidget* widget, GdkEventButton* evt, LXPanel *panel)
523 {
524 MonitorsPlugin* mp = lxpanel_plugin_get_data(widget);
525
526 if (mp->action != NULL)
527 fm_launch_command_simple(NULL, NULL, 0, mp->action, NULL);
528 else
529 fm_launch_command_simple(NULL, NULL, 0, "lxtask", NULL);
530
531 return TRUE;
532 }
533 /******************************************************************************
534 * End of basic events handlers *
535 ******************************************************************************/
536
537 static void
538 redraw_pixmap (Monitor *m)
539 {
540 int i;
541 cairo_t *cr = cairo_create(m->pixmap);
542 GtkStyle *style = gtk_widget_get_style(m->da);
543
544 cairo_set_line_width (cr, 1.0);
545
546 /* Erase pixmap */
547 gdk_cairo_set_source_color(cr, &style->black);
548 cairo_paint(cr);
549
550 gdk_cairo_set_source_color(cr, &m->foreground_color);
551 for (i = 0; i < m->pixmap_width; i++)
552 {
553 unsigned int drawing_cursor = (m->ring_cursor + i) % m->pixmap_width;
554
555 /* Draw one bar of the graph */
556 cairo_move_to(cr, i + 0.5, m->pixmap_height);
557 cairo_line_to(cr, i + 0.5, (1.0 - m->stats[drawing_cursor]) * m->pixmap_height);
558 cairo_stroke(cr);
559 }
560
561 check_cairo_status(cr);
562 cairo_destroy(cr);
563 /* Redraw pixmap */
564 gtk_widget_queue_draw(m->da);
565 }
566
567
568 static update_func update_functions [N_MONITORS] = {
569 [CPU_POSITION] = cpu_update,
570 [MEM_POSITION] = mem_update
571 };
572
573 static char *default_colors[N_MONITORS] = {
574 [CPU_POSITION] = "#0000FF",
575 [MEM_POSITION] = "#FF0000"
576 };
577
578
579 static tooltip_update_func tooltip_update[N_MONITORS] = {
580 [CPU_POSITION] = cpu_tooltip_update,
581 [MEM_POSITION] = mem_tooltip_update
582 };
583
584 /* Colors currently used. We cannot store them in the "struct Monitor"s where
585 * they belong, because we free these when the user removes them. And since we
586 * want the colors to stay the same even after removing/adding a widget... */
587 static char *colors[N_MONITORS] = {
588 NULL,
589 NULL
590 };
591
592 /*
593 * This function is called every UPDATE_PERIOD seconds. It updates all
594 * monitors.
595 */
596 static gboolean
597 monitors_update(gpointer data)
598 {
599 MonitorsPlugin *mp;
600 int i;
601
602 if (g_source_is_destroyed(g_main_current_source()))
603 return FALSE;
604 mp = (MonitorsPlugin *) data;
605 if (!mp)
606 RET(FALSE);
607
608 for (i = 0; i < N_MONITORS; i++)
609 {
610 if (mp->monitors[i])
611 {
612 mp->monitors[i]->update(mp->monitors[i]);
613 if (mp->monitors[i]->update_tooltip)
614 mp->monitors[i]->update_tooltip(mp->monitors[i]);
615 }
616 }
617
618 return TRUE;
619 }
620
621 static Monitor*
622 monitors_add_monitor (GtkWidget *p, MonitorsPlugin *mp, update_func update,
623 tooltip_update_func update_tooltip, gchar *color)
624 {
625 ENTER;
626
627 Monitor *m;
628
629 m = g_new0(Monitor, 1);
630 m = monitor_init(mp, m, color);
631 m->update = update;
632 m->update_tooltip = update_tooltip;
633 gtk_box_pack_start(GTK_BOX(p), m->da, FALSE, FALSE, 0);
634 gtk_widget_show(m->da);
635
636 RET(m);
637 }
638
639 static GtkWidget *
640 monitors_constructor(LXPanel *panel, config_setting_t *settings)
641 {
642 ENTER;
643 int i;
644 MonitorsPlugin *mp;
645 GtkWidget *p;
646 const char *tmp;
647
648 mp = g_new0(MonitorsPlugin, 1);
649 mp->panel = panel;
650 mp->settings = settings;
651
652 p = gtk_hbox_new(TRUE, 2);
653 lxpanel_plugin_set_data(p, mp, monitors_destructor);
654
655 /* First time we use this plugin : only display CPU usage */
656 mp->displayed_monitors[CPU_POSITION] = 1;
657
658 /* Apply options */
659 config_setting_lookup_int(settings, "DisplayCPU",
660 &mp->displayed_monitors[CPU_POSITION]);
661 config_setting_lookup_int(settings, "DisplayRAM",
662 &mp->displayed_monitors[MEM_POSITION]);
663 if (config_setting_lookup_string(settings, "Action", &tmp))
664 mp->action = g_strdup(tmp);
665 if (config_setting_lookup_string(settings, "CPUColor", &tmp))
666 colors[CPU_POSITION] = g_strndup(tmp, COLOR_SIZE-1);
667 if (config_setting_lookup_string(settings, "RAMColor", &tmp))
668 colors[MEM_POSITION] = g_strndup(tmp, COLOR_SIZE-1);
669
670 /* Initializing monitors */
671 for (i = 0; i < N_MONITORS; i++)
672 {
673 if (!colors[i])
674 colors[i] = g_strndup(default_colors[i], COLOR_SIZE-1);
675
676 if (mp->displayed_monitors[i])
677 {
678 mp->monitors[i] = monitors_add_monitor(p, mp,
679 update_functions[i],
680 tooltip_update[i],
681 colors[i]);
682 }
683 }
684
685 /* Adding a timer : monitors will be updated every UPDATE_PERIOD
686 * seconds */
687 mp->timer = g_timeout_add_seconds(UPDATE_PERIOD, (GSourceFunc) monitors_update,
688 (gpointer) mp);
689 RET(p);
690 }
691
692 static void
693 monitors_destructor(gpointer user_data)
694 {
695 ENTER;
696 int i;
697 MonitorsPlugin *mp;
698
699 mp = (MonitorsPlugin *) user_data;
700
701 /* Removing timer */
702 g_source_remove(mp->timer);
703
704 /* Freeing all monitors */
705 for (i = 0; i < N_MONITORS; i++)
706 {
707 if (mp->monitors[i])
708 monitor_free(mp->monitors[i]);
709 }
710
711 g_free(mp->action);
712 g_free(mp);
713
714 RET();
715 }
716
717
718 static GtkWidget *
719 monitors_config (LXPanel *panel, GtkWidget *p)
720 {
721 ENTER;
722
723 GtkWidget *dialog;
724 MonitorsPlugin *mp;
725
726 mp = lxpanel_plugin_get_data(p);
727
728 dialog = lxpanel_generic_config_dlg(_("Resource monitors"),
729 panel, monitors_apply_config, p,
730 _("Display CPU usage"), &mp->displayed_monitors[0], CONF_TYPE_BOOL,
731 _("CPU color"), &colors[CPU_POSITION], CONF_TYPE_STR,
732 _("Display RAM usage"), &mp->displayed_monitors[1], CONF_TYPE_BOOL,
733 _("RAM color"), &colors[MEM_POSITION], CONF_TYPE_STR,
734 _("Action when clicked (default: lxtask)"), &mp->action, CONF_TYPE_STR,
735 NULL);
736
737 RET(dialog);
738 }
739
740 static gboolean
741 monitors_apply_config (gpointer user_data)
742 {
743 ENTER;
744 GtkWidget *p = user_data;
745 MonitorsPlugin *mp;
746 mp = lxpanel_plugin_get_data(p);
747
748 int i;
749 int current_n_monitors = 0;
750
751 start:
752 for (i = 0; i < N_MONITORS; i++)
753 {
754 if (mp->displayed_monitors[i])
755 current_n_monitors++;
756
757 if (mp->displayed_monitors[i] && !mp->monitors[i])
758 {
759 /* We've just activated monitor<i> */
760 mp->monitors[i] = monitors_add_monitor(p, mp,
761 update_functions[i],
762 tooltip_update[i],
763 colors[i]);
764 /*
765 * It is probably best for users if their monitors are always
766 * displayed in the same order : the CPU monitor always on the left,
767 * the RAM monitor always on the right of the CPU monitor (if the
768 * CPU monitor is displayed), etc. That's why we do not just use
769 * gtk_box_pack_start/gtk_box_pack_end, and use
770 * gtk_box_reorder_child.
771 */
772 gtk_box_reorder_child(GTK_BOX(p),
773 mp->monitors[i]->da,current_n_monitors-1);
774 }
775 else if (!mp->displayed_monitors[i] && mp->monitors[i])
776 {
777 /* We've just removed monitor<i> */
778 gtk_widget_destroy(mp->monitors[i]->da);
779 monitor_free(mp->monitors[i]);
780 mp->monitors[i] = NULL;
781 }
782 if (mp->monitors[i] &&
783 strncmp(mp->monitors[i]->color, colors[i], COLOR_SIZE) != 0)
784 {
785 /* We've changed the color */
786 monitor_set_foreground_color(mp, mp->monitors[i], colors[i]);
787 }
788 }
789
790 /* Workaround meant to prevent users to display no monitor at all.
791 * FIXME : write something clean. When there is only one monitor displayed,
792 * its toggle button should not be clickable in the prefs. */
793 if (current_n_monitors == 0)
794 {
795 mp->displayed_monitors[0] = 1;
796 goto start;
797 }
798 config_group_set_int(mp->settings, "DisplayCPU", mp->displayed_monitors[CPU_POSITION]);
799 config_group_set_int(mp->settings, "DisplayRAM", mp->displayed_monitors[MEM_POSITION]);
800 config_group_set_string(mp->settings, "Action", mp->action);
801 config_group_set_string(mp->settings, "CPUColor",
802 mp->monitors[CPU_POSITION] ? colors[CPU_POSITION] : NULL);
803 config_group_set_string(mp->settings, "RAMColor",
804 mp->monitors[MEM_POSITION] ? colors[MEM_POSITION] : NULL);
805
806 RET(FALSE);
807 }
808
809
810 FM_DEFINE_MODULE(lxpanel_gtk, monitors)
811
812 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
813 .name = N_("Resource monitors"),
814 .description = N_("Display monitors (CPU, RAM)"),
815 .new_instance = monitors_constructor,
816 .config = monitors_config,
817 .button_press_event = monitors_button_press_event
818 };
819
820 /* vim: set sw=4 sts=4 et : */