~ubuntu-branches/ubuntu/wily/lxpanel/wily-proposed

« back to all changes in this revision

Viewing changes to src/plugins/monitors/monitors.c

  • Committer: Package Import Robot
  • Author(s): Julien Lavergne
  • Date: 2015-01-31 15:30:45 UTC
  • mfrom: (1.3.3)
  • mto: This revision was merged to the branch mainline in revision 45.
  • Revision ID: package-import@ubuntu.com-20150131153045-1r9i4602vrplnx3i
ImportĀ upstreamĀ versionĀ 0.7.2

Show diffs side-by-side

added added

removed removed

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