Adding upstream version 0.9.0.
[debian/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-2016 Andriy Grytsenko <andrej@rep.kiev.ua>
13 * 2015 Rafał Mużyło <galtgendo@gmail.com>
14 *
15 * <terms>
16 * Copyright (c) 2008-2014 LxDE Developers, see the file AUTHORS for details.
17 * Copyright (C) 2004 by Alexandre Pereira da Silva <alexandre.pereira@poli.usp.br>
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 * </terms>
33 *
34 */
35
36 /*
37 * HOWTO : Add your own monitor for the resource "foo".
38 *
39 * 1) Write the foo_update() function, that fills in the stats.
40 * 2) Write the foo_tooltip_update() function, that updates your tooltip. This
41 * is optional, but recommended.
42 * 3) Add a #define FOO_POSITION, and increment N_MONITORS.
43 * 4) Add :
44 * - the default color of your plugin ("default_colors" table)
45 * - the update function ("update_functions" table)
46 * - the tooltip update function ("tooltip_update" table)
47 * 5) Configuration :
48 * - edit the monitors_config() function so that a "Display FOO usage"
49 * checkbox appears in the prefs dialog.
50 * - edit the monitors_save() function so that a "DisplayFOO" string appears
51 * in the config file ("~/.config/lxpanel/<profile>/config")
52 * - edit the monitors_config() function so that a "FOO color" entry appears
53 * in the prefs dialog.
54 * - edit the monitors_save() function so that a "FOOColor" string appears
55 * in the config file.
56 * - edit the monitors_constructor() function so that options are correctly
57 * aplied. Adding something like :
58 *
59 * else if (g_ascii_strcasecmp(s.t[0], "DisplayFOO") == 0)
60 * mp->displayed_monitors[FOO_POSITION] = atoi(s.t[1])
61 * else if (g_ascii_strcasecmp(s.t[0], "FOOColor") == 0)
62 * colors[FOO_POSITION] = g_strndup(s.t[1], COLOR_SIZE-1);
63 *
64 * should be enough.
65 * 6) Enjoy.
66 */
67
68 /*
69 * FIXME : known BUGS :
70 * - when removing a monitor and re-adding it, it is drawn with a white
71 * border of BORDER_SIZE pixels around it.
72 */
73
74 #include <stdlib.h>
75 #include <glib/gi18n.h>
76 #include <errno.h>
77 #include <libfm/fm-gtk.h>
78
79 #include "plugin.h"
80
81 #include "dbg.h"
82
83
84 #define PLUGIN_NAME "MonitorsPlugin"
85 #define BORDER_SIZE 2 /* Pixels */
86 #define DEFAULT_WIDTH 40 /* Pixels */
87 #define UPDATE_PERIOD 1 /* Seconds */
88 #define COLOR_SIZE 8 /* In chars : #xxxxxx\0 */
89
90 #ifndef ENTER
91 #define ENTER fprintf(stderr, "Entering %s\n", __func__);
92 #endif
93
94 /*
95 * Stats are stored in a circular buffer.
96 * Newest values are on the left of the ring cursor.
97 * Oldest values are on the right of the ring cursor.
98 */
99 typedef float stats_set;
100
101 struct Monitor {
102 GdkColor foreground_color; /* Foreground color for drawing area */
103 GtkWidget *da; /* Drawing area */
104 cairo_surface_t *pixmap; /* Pixmap to be drawn on drawing area */
105 gint pixmap_width; /* Width and size of the buffer */
106 gint pixmap_height; /* Does not include border size */
107 stats_set *stats; /* Circular buffer of values */
108 stats_set total; /* Maximum possible value, as in mem_total*/
109 gint ring_cursor; /* Cursor for ring/circular buffer */
110 gchar *color; /* Color of the graph */
111 gboolean (*update) (struct Monitor *); /* Update function */
112 void (*update_tooltip) (struct Monitor *);
113 };
114
115 typedef struct Monitor Monitor;
116 typedef gboolean (*update_func) (Monitor *);
117 typedef void (*tooltip_update_func) (Monitor *);
118
119 /*
120 * Position of our monitors : monitor 0 will always be on the left of the
121 * plugin, monitor 1 on the right of monitor 0 (or on the left of the plugin if
122 * monitor 0 is not displayed), etc.
123 */
124 #define CPU_POSITION 0
125 #define MEM_POSITION 1
126 #define N_MONITORS 2
127
128 /* Our plugin */
129 typedef struct {
130 LXPanel *panel;
131 config_setting_t *settings;
132 Monitor *monitors[N_MONITORS]; /* Monitors */
133 int displayed_monitors[N_MONITORS]; /* Booleans */
134 char *action; /* What to do on click */
135 guint timer; /* Timer for regular updates */
136 } MonitorsPlugin;
137
138 /*
139 * Prototypes
140 */
141 static void monitor_set_foreground_color(MonitorsPlugin *, Monitor *, const gchar *);
142
143 /* CPU Monitor */
144 static gboolean cpu_update(Monitor *);
145 static void cpu_tooltip_update (Monitor *m);
146
147 /* RAM Monitor */
148 static gboolean mem_update(Monitor *);
149 static void mem_tooltip_update (Monitor *m);
150
151
152 static gboolean configure_event(GtkWidget*, GdkEventConfigure*, gpointer);
153 #if !GTK_CHECK_VERSION(3, 0, 0)
154 static gboolean expose_event(GtkWidget *, GdkEventExpose *, Monitor *);
155 #else
156 static gboolean draw(GtkWidget *, cairo_t *, Monitor *);
157 #endif
158 static void redraw_pixmap (Monitor *m);
159
160 /* Monitors functions */
161 static void monitors_destructor(gpointer);
162 static gboolean monitors_apply_config(gpointer);
163
164
165 /******************************************************************************
166 * Monitor functions *
167 ******************************************************************************/
168 static Monitor*
169 monitor_init(MonitorsPlugin *mp, Monitor *m, gchar *color)
170 {
171 ENTER;
172
173 m->da = gtk_drawing_area_new();
174 gtk_widget_add_events(m->da, GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
175 GDK_BUTTON_MOTION_MASK);
176 gtk_widget_set_size_request(m->da, DEFAULT_WIDTH, panel_get_height(mp->panel));
177
178 monitor_set_foreground_color(mp, m, color);
179
180 /* Signals */
181 g_signal_connect(G_OBJECT(m->da), "configure-event",
182 G_CALLBACK(configure_event), (gpointer) m);
183 #if !GTK_CHECK_VERSION(3, 0, 0)
184 g_signal_connect (G_OBJECT(m->da), "expose-event",
185 G_CALLBACK(expose_event), (gpointer) m);
186 #else
187 g_signal_connect (G_OBJECT(m->da), "draw",
188 G_CALLBACK(draw), (gpointer) m);
189 #endif
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 #else
486 static gboolean
487 draw(GtkWidget * widget, cairo_t * cr, Monitor *m)
488 #endif
489 {
490 /* Draw the requested part of the pixmap onto the drawing area.
491 * Translate it in both x and y by the border size. */
492 if (m->pixmap != NULL)
493 {
494 #if !GTK_CHECK_VERSION(3, 0, 0)
495 cairo_t *cr = gdk_cairo_create(gtk_widget_get_window(widget));
496 GtkStyle *style = gtk_widget_get_style(m->da);
497 gdk_cairo_region(cr, event->region);
498 cairo_clip(cr);
499 gdk_cairo_set_source_color(cr, &style->black);
500 #else
501 cairo_set_source_rgb(cr, 0, 0, 0); // FIXME: set the color from style
502 #endif
503 cairo_set_source_surface(cr, m->pixmap, BORDER_SIZE, BORDER_SIZE);
504 cairo_paint(cr);
505 check_cairo_status(cr);
506 #if !GTK_CHECK_VERSION(3, 0, 0)
507 cairo_destroy(cr);
508 #endif
509 }
510
511 return FALSE;
512 }
513
514
515 static gboolean monitors_button_press_event(GtkWidget* widget, GdkEventButton* evt, LXPanel *panel)
516 {
517 MonitorsPlugin* mp;
518
519 if (evt->button != 1)
520 return FALSE;
521
522 mp = lxpanel_plugin_get_data(widget);
523 if (mp->action != NULL)
524 fm_launch_command_simple(NULL, NULL, 0, mp->action, NULL);
525 else
526 fm_launch_command_simple(NULL, NULL, 0, "lxtask", NULL);
527
528 return TRUE;
529 }
530 /******************************************************************************
531 * End of basic events handlers *
532 ******************************************************************************/
533
534 static void
535 redraw_pixmap (Monitor *m)
536 {
537 int i;
538 cairo_t *cr = cairo_create(m->pixmap);
539 GtkStyle *style = gtk_widget_get_style(m->da);
540
541 cairo_set_line_width (cr, 1.0);
542
543 /* Erase pixmap */
544 gdk_cairo_set_source_color(cr, &style->black);
545 cairo_paint(cr);
546
547 gdk_cairo_set_source_color(cr, &m->foreground_color);
548 for (i = 0; i < m->pixmap_width; i++)
549 {
550 unsigned int drawing_cursor = (m->ring_cursor + i) % m->pixmap_width;
551
552 /* Draw one bar of the graph */
553 cairo_move_to(cr, i + 0.5, m->pixmap_height);
554 cairo_line_to(cr, i + 0.5, (1.0 - m->stats[drawing_cursor]) * m->pixmap_height);
555 cairo_stroke(cr);
556 }
557
558 check_cairo_status(cr);
559 cairo_destroy(cr);
560 /* Redraw pixmap */
561 gtk_widget_queue_draw(m->da);
562 }
563
564
565 static update_func update_functions [N_MONITORS] = {
566 [CPU_POSITION] = cpu_update,
567 [MEM_POSITION] = mem_update
568 };
569
570 static char *default_colors[N_MONITORS] = {
571 [CPU_POSITION] = "#0000FF",
572 [MEM_POSITION] = "#FF0000"
573 };
574
575
576 static tooltip_update_func tooltip_update[N_MONITORS] = {
577 [CPU_POSITION] = cpu_tooltip_update,
578 [MEM_POSITION] = mem_tooltip_update
579 };
580
581 /* Colors currently used. We cannot store them in the "struct Monitor"s where
582 * they belong, because we free these when the user removes them. And since we
583 * want the colors to stay the same even after removing/adding a widget... */
584 static char *colors[N_MONITORS] = {
585 NULL,
586 NULL
587 };
588
589 /*
590 * This function is called every UPDATE_PERIOD seconds. It updates all
591 * monitors.
592 */
593 static gboolean
594 monitors_update(gpointer data)
595 {
596 MonitorsPlugin *mp;
597 int i;
598
599 if (g_source_is_destroyed(g_main_current_source()))
600 return FALSE;
601 mp = (MonitorsPlugin *) data;
602 if (!mp)
603 RET(FALSE);
604
605 for (i = 0; i < N_MONITORS; i++)
606 {
607 if (mp->monitors[i])
608 {
609 mp->monitors[i]->update(mp->monitors[i]);
610 if (mp->monitors[i]->update_tooltip)
611 mp->monitors[i]->update_tooltip(mp->monitors[i]);
612 }
613 }
614
615 return TRUE;
616 }
617
618 static Monitor*
619 monitors_add_monitor (GtkWidget *p, MonitorsPlugin *mp, update_func update,
620 tooltip_update_func update_tooltip, gchar *color)
621 {
622 ENTER;
623
624 Monitor *m;
625
626 m = g_new0(Monitor, 1);
627 m = monitor_init(mp, m, color);
628 m->update = update;
629 m->update_tooltip = update_tooltip;
630 gtk_box_pack_start(GTK_BOX(p), m->da, FALSE, FALSE, 0);
631 gtk_widget_show(m->da);
632
633 RET(m);
634 }
635
636 static GtkWidget *
637 monitors_constructor(LXPanel *panel, config_setting_t *settings)
638 {
639 ENTER;
640 int i;
641 MonitorsPlugin *mp;
642 GtkWidget *p;
643 const char *tmp;
644
645 mp = g_new0(MonitorsPlugin, 1);
646 mp->panel = panel;
647 mp->settings = settings;
648
649 p = gtk_hbox_new(TRUE, 2);
650 lxpanel_plugin_set_data(p, mp, monitors_destructor);
651
652 /* First time we use this plugin : only display CPU usage */
653 mp->displayed_monitors[CPU_POSITION] = 1;
654
655 /* Apply options */
656 config_setting_lookup_int(settings, "DisplayCPU",
657 &mp->displayed_monitors[CPU_POSITION]);
658 config_setting_lookup_int(settings, "DisplayRAM",
659 &mp->displayed_monitors[MEM_POSITION]);
660 if (config_setting_lookup_string(settings, "Action", &tmp))
661 mp->action = g_strdup(tmp);
662 if (config_setting_lookup_string(settings, "CPUColor", &tmp))
663 colors[CPU_POSITION] = g_strndup(tmp, COLOR_SIZE-1);
664 if (config_setting_lookup_string(settings, "RAMColor", &tmp))
665 colors[MEM_POSITION] = g_strndup(tmp, COLOR_SIZE-1);
666
667 /* Initializing monitors */
668 for (i = 0; i < N_MONITORS; i++)
669 {
670 if (!colors[i])
671 colors[i] = g_strndup(default_colors[i], COLOR_SIZE-1);
672
673 if (mp->displayed_monitors[i])
674 {
675 mp->monitors[i] = monitors_add_monitor(p, mp,
676 update_functions[i],
677 tooltip_update[i],
678 colors[i]);
679 }
680 }
681
682 /* Adding a timer : monitors will be updated every UPDATE_PERIOD
683 * seconds */
684 mp->timer = g_timeout_add_seconds(UPDATE_PERIOD, (GSourceFunc) monitors_update,
685 (gpointer) mp);
686 RET(p);
687 }
688
689 static void
690 monitors_destructor(gpointer user_data)
691 {
692 ENTER;
693 int i;
694 MonitorsPlugin *mp;
695
696 mp = (MonitorsPlugin *) user_data;
697
698 /* Removing timer */
699 g_source_remove(mp->timer);
700
701 /* Freeing all monitors */
702 for (i = 0; i < N_MONITORS; i++)
703 {
704 if (mp->monitors[i])
705 monitor_free(mp->monitors[i]);
706 }
707
708 g_free(mp->action);
709 g_free(mp);
710
711 RET();
712 }
713
714
715 static GtkWidget *
716 monitors_config (LXPanel *panel, GtkWidget *p)
717 {
718 ENTER;
719
720 GtkWidget *dialog;
721 MonitorsPlugin *mp;
722
723 mp = lxpanel_plugin_get_data(p);
724
725 dialog = lxpanel_generic_config_dlg(_("Resource monitors"),
726 panel, monitors_apply_config, p,
727 _("Display CPU usage"), &mp->displayed_monitors[0], CONF_TYPE_BOOL,
728 _("CPU color"), &colors[CPU_POSITION], CONF_TYPE_STR,
729 _("Display RAM usage"), &mp->displayed_monitors[1], CONF_TYPE_BOOL,
730 _("RAM color"), &colors[MEM_POSITION], CONF_TYPE_STR,
731 _("Action when clicked (default: lxtask)"), &mp->action, CONF_TYPE_STR,
732 NULL);
733
734 RET(dialog);
735 }
736
737 static gboolean
738 monitors_apply_config (gpointer user_data)
739 {
740 ENTER;
741 GtkWidget *p = user_data;
742 MonitorsPlugin *mp;
743 mp = lxpanel_plugin_get_data(p);
744
745 int i;
746 int current_n_monitors = 0;
747
748 start:
749 for (i = 0; i < N_MONITORS; i++)
750 {
751 if (mp->displayed_monitors[i])
752 current_n_monitors++;
753
754 if (mp->displayed_monitors[i] && !mp->monitors[i])
755 {
756 /* We've just activated monitor<i> */
757 mp->monitors[i] = monitors_add_monitor(p, mp,
758 update_functions[i],
759 tooltip_update[i],
760 colors[i]);
761 /*
762 * It is probably best for users if their monitors are always
763 * displayed in the same order : the CPU monitor always on the left,
764 * the RAM monitor always on the right of the CPU monitor (if the
765 * CPU monitor is displayed), etc. That's why we do not just use
766 * gtk_box_pack_start/gtk_box_pack_end, and use
767 * gtk_box_reorder_child.
768 */
769 gtk_box_reorder_child(GTK_BOX(p),
770 mp->monitors[i]->da,current_n_monitors-1);
771 }
772 else if (!mp->displayed_monitors[i] && mp->monitors[i])
773 {
774 /* We've just removed monitor<i> */
775 gtk_widget_destroy(mp->monitors[i]->da);
776 monitor_free(mp->monitors[i]);
777 mp->monitors[i] = NULL;
778 }
779 if (mp->monitors[i] &&
780 strncmp(mp->monitors[i]->color, colors[i], COLOR_SIZE) != 0)
781 {
782 /* We've changed the color */
783 monitor_set_foreground_color(mp, mp->monitors[i], colors[i]);
784 }
785 }
786
787 /* Workaround meant to prevent users to display no monitor at all.
788 * FIXME : write something clean. When there is only one monitor displayed,
789 * its toggle button should not be clickable in the prefs. */
790 if (current_n_monitors == 0)
791 {
792 mp->displayed_monitors[0] = 1;
793 goto start;
794 }
795 config_group_set_int(mp->settings, "DisplayCPU", mp->displayed_monitors[CPU_POSITION]);
796 config_group_set_int(mp->settings, "DisplayRAM", mp->displayed_monitors[MEM_POSITION]);
797 config_group_set_string(mp->settings, "Action", mp->action);
798 config_group_set_string(mp->settings, "CPUColor",
799 mp->monitors[CPU_POSITION] ? colors[CPU_POSITION] : NULL);
800 config_group_set_string(mp->settings, "RAMColor",
801 mp->monitors[MEM_POSITION] ? colors[MEM_POSITION] : NULL);
802
803 RET(FALSE);
804 }
805
806
807 FM_DEFINE_MODULE(lxpanel_gtk, monitors)
808
809 LXPanelPluginInit fm_module_init_lxpanel_gtk = {
810 .name = N_("Resource monitors"),
811 .description = N_("Display monitors (CPU, RAM)"),
812 .new_instance = monitors_constructor,
813 .config = monitors_config,
814 .button_press_event = monitors_button_press_event
815 };
816
817 /* vim: set sw=4 sts=4 et : */