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