a04873243bc24edeb8818ef166b10411afeb5852
[psensor.git] / src / graph.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include <sys/time.h>
23
24 #include <glib/gi18n.h>
25 #include <gtk/gtk.h>
26
27 #include <math.h>
28
29 #include <cfg.h>
30 #include <plog.h>
31 #include <psensor.h>
32
33 /* horizontal padding */
34 #define GRAPH_H_PADDING 4
35 /* vertical padding */
36 #define GRAPH_V_PADDING 4
37
38 bool is_smooth_curves_enabled;
39
40 struct graph_info {
41         /* Horizontal position of the central region (curves) */
42         int g_xoff;
43         /* Vertical position of the central region (curves) */
44         int g_yoff;
45
46         /* Height of the drawing canvas */
47         int height;
48
49         /* Background color of the current desktop theme */
50         GdkRGBA theme_bg_color;
51
52         /* Foreground color of the current desktop theme */
53         GdkRGBA theme_fg_color;
54 };
55
56
57 /* Return the end time of the graph i.e. the more recent measure.  If
58  * no measure are available, return 0.
59  */
60 static time_t get_graph_end_time_s(struct psensor **sensors)
61 {
62         time_t ret, t;
63         struct psensor *s;
64         struct measure *measures;
65         int i;
66
67         ret = 0;
68         while (*sensors) {
69                 s = *sensors;
70                 measures = s->measures;
71
72                 for (i = s->values_max_length - 1; i >= 0; i--) {
73                         if (measures[i].value != UNKNOWN_DBL_VALUE) {
74                                 t = measures[i].time.tv_sec;
75
76                                 if (t > ret)
77                                         ret = t;
78                         }
79                         i--;
80                 }
81
82                 sensors++;
83         }
84
85         return ret;
86 }
87
88 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
89 {
90         if (!etime)
91                 return 0;
92
93         return etime - cfg->graph_monitoring_duration * 60;
94 }
95
96 static double
97 compute_y(double value, double min, double max, int height, int off)
98 {
99         double t = value - min;
100
101         return height - ((double)height * (t / (max - min))) + off;
102 }
103
104 static char *time_to_str(time_t s)
105 {
106         char *str;
107         /* note: localtime returns a static field, no free required */
108         struct tm *tm = localtime(&s);
109
110         if (!tm)
111                 return NULL;
112
113         str = malloc(6);
114         strftime(str, 6, "%H:%M", tm);
115
116         return str;
117 }
118
119 static void draw_left_region(cairo_t *cr, struct graph_info *info)
120 {
121         cairo_set_source_rgb(cr,
122                              info->theme_bg_color.red,
123                              info->theme_bg_color.green,
124                              info->theme_bg_color.blue);
125
126         cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
127         cairo_fill(cr);
128 }
129
130 static void
131 draw_graph_background(cairo_t *cr,
132                       int g_xoff, int g_yoff,
133                       int g_width, int g_height,
134                       int width, int height, struct config *config,
135                       GtkWidget *widget,
136                       GtkWidget *window)
137 {
138         GtkStyleContext *style_ctx;
139         struct color *bgcolor;
140         GdkRGBA rgba;
141
142         bgcolor = config->graph_bgcolor;
143
144         style_ctx = gtk_widget_get_style_context(window);
145         gtk_style_context_get_background_color(style_ctx,
146                                                GTK_STATE_FLAG_NORMAL,
147                                                &rgba);
148
149         if (config->alpha_channel_enabled)
150                 cairo_set_source_rgba(cr,
151                                       rgba.red,
152                                       rgba.green,
153                                       rgba.blue,
154                                       config->graph_bg_alpha);
155         else
156                 cairo_set_source_rgb(cr,
157                                      rgba.red,
158                                      rgba.green,
159                                      rgba.blue);
160
161         cairo_rectangle(cr, g_xoff, 0, g_width, height);
162         cairo_fill(cr);
163
164         if (config->alpha_channel_enabled)
165                 cairo_set_source_rgba(cr,
166                                       bgcolor->red,
167                                       bgcolor->green,
168                                       bgcolor->blue,
169                                       config->graph_bg_alpha);
170         else
171                 cairo_set_source_rgb(cr,
172                                      bgcolor->red,
173                                      bgcolor->green,
174                                      bgcolor->blue);
175
176         cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
177         cairo_fill(cr);
178 }
179
180 /* setup dash style */
181 static double dashes[] = {
182         1.0,            /* ink */
183         2.0,            /* skip */
184 };
185 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
186
187 static void draw_background_lines(cairo_t *cr,
188                                   struct color *color,
189                                   int g_width, int g_height,
190                                   int g_xoff, int g_yoff,
191                                   int min, int max)
192 {
193         int i;
194
195         /* draw background lines */
196         cairo_set_line_width(cr, 1);
197         cairo_set_dash(cr, dashes, ndash, 0);
198         cairo_set_source_rgb(cr,
199                              color->red, color->green, color->blue);
200
201         /* vertical lines representing time steps */
202         for (i = 0; i <= 5; i++) {
203                 int x = i * (g_width / 5) + g_xoff;
204
205                 cairo_move_to(cr, x, g_yoff);
206                 cairo_line_to(cr, x, g_yoff + g_height);
207                 cairo_stroke(cr);
208         }
209
210         /* horizontal lines draws a line for each 10C step */
211         for (i = min; i < max; i++) {
212                 if (i % 10 == 0) {
213                         int y = compute_y(i, min, max, g_height, g_yoff);
214
215                         cairo_move_to(cr, g_xoff, y);
216                         cairo_line_to(cr, g_xoff + g_width, y);
217                         cairo_stroke(cr);
218                 }
219         }
220
221         /* back to normal line style */
222         cairo_set_dash(cr, 0, 0, 0);
223 }
224
225 /* Keys: sensor identifier.
226  *
227  * Values: array of time_t. Each time_t is corresponding to a sensor
228  * measure which has been used as the start point of a Bezier curve.
229  */
230 static GHashTable *times;
231
232 static void draw_sensor_smooth_curve(struct psensor *s,
233                                      cairo_t *cr,
234                                      double min,
235                                      double max,
236                                      int bt,
237                                      int et,
238                                      int g_width,
239                                      int g_height,
240                                      int g_xoff,
241                                      int g_yoff)
242 {
243         int i, dt, vdt, j, k, found;
244         double x[4], y[4], v;
245         time_t t, t0, *stimes;
246
247         if (!times)
248                 times = g_hash_table_new_full(g_str_hash,
249                                               g_str_equal,
250                                               free,
251                                               free);
252
253         stimes = g_hash_table_lookup(times, s->id);
254
255         cairo_set_source_rgb(cr,
256                              s->color->red,
257                              s->color->green,
258                              s->color->blue);
259
260         /* search the index of the first measure used as a start point
261          * of a Bezier curve. The start and end points of the Bezier
262          * curves must be preserved to ensure the same overall shape
263          * of the graph. */
264         i = 0;
265         if (stimes) {
266                 while (i < s->values_max_length) {
267                         t = s->measures[i].time.tv_sec;
268                         v = s->measures[i].value;
269
270                         found = 0;
271                         if (v != UNKNOWN_DBL_VALUE && t) {
272                                 k = 0;
273                                 while (stimes[k]) {
274                                         if (t == stimes[k]) {
275                                                 found = 1;
276                                                 break;
277                                         }
278                                         k++;
279                                 }
280                         }
281
282                         if (found)
283                                 break;
284
285                         i++;
286                 }
287         }
288
289         stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
290         memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
291         g_hash_table_insert(times, strdup(s->id), stimes);
292
293         if (i == s->values_max_length)
294                 i = 0;
295
296         k = 0;
297         dt = et - bt;
298         while (i < s->values_max_length) {
299                 j = 0;
300                 t = 0;
301                 while (i < s->values_max_length && j < 4) {
302                         t = s->measures[i].time.tv_sec;
303                         v = s->measures[i].value;
304
305                         if (v == UNKNOWN_DBL_VALUE || !t) {
306                                 i++;
307                                 continue;
308                         }
309
310                         vdt = t - bt;
311
312                         x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
313                         y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
314
315                         if (j == 0)
316                                 t0 = t;
317
318                         i++;
319                         j++;
320                 }
321
322                 if (j == 4) {
323                         cairo_move_to(cr, x[0], y[0]);
324                         cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
325                         stimes[k++] = t0;
326                         i--;
327                 }
328         }
329
330         cairo_stroke(cr);
331 }
332
333 static void draw_sensor_curve(struct psensor *s,
334                               cairo_t *cr,
335                               double min,
336                               double max,
337                               int bt,
338                               int et,
339                               int g_width,
340                               int g_height,
341                               int g_xoff,
342                               int g_yoff)
343 {
344         int first, i, t, dt, vdt;
345         double v, x, y;
346
347         cairo_set_source_rgb(cr,
348                              s->color->red,
349                              s->color->green,
350                              s->color->blue);
351
352         dt = et - bt;
353         first = 1;
354         for (i = 0; i < s->values_max_length; i++) {
355                 t = s->measures[i].time.tv_sec;
356                 v = s->measures[i].value;
357
358                 if (v == UNKNOWN_DBL_VALUE || !t)
359                         continue;
360
361                 vdt = t - bt;
362
363                 x = ((double)vdt * g_width) / dt + g_xoff;
364
365                 y = compute_y(v, min, max, g_height, g_yoff);
366
367                 if (first) {
368                         cairo_move_to(cr, x, y);
369                         first = 0;
370                 } else {
371                         cairo_line_to(cr, x, y);
372                 }
373
374         }
375         cairo_stroke(cr);
376 }
377
378 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
379 {
380         char *msg;
381
382         msg = strdup(_("No graphs enabled"));
383
384         cairo_select_font_face(cr,
385                                "sans-serif",
386                                CAIRO_FONT_SLANT_NORMAL,
387                                CAIRO_FONT_WEIGHT_NORMAL);
388         cairo_set_font_size(cr, 18.0);
389
390         cairo_move_to(cr, x, y);
391         cairo_show_text(cr, msg);
392
393         free(msg);
394 }
395
396 void
397 graph_update(struct psensor **sensors,
398              GtkWidget *w_graph,
399              struct config *config,
400              GtkWidget *window)
401 {
402         struct color *fgcolor = config->graph_fgcolor;
403         int et, bt, width, height, g_width, g_height;
404         double min_rpm, max_rpm, mint, maxt, min, max;
405         char *strmin, *strmax;
406         /* horizontal and vertical offset of the graph */
407         int g_xoff, g_yoff, no_graphs;
408         cairo_surface_t *cst;
409         cairo_t *cr, *cr_pixmap;
410         char *str_btime, *str_etime;
411         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
412         struct psensor **sensor_cur, **enabled_sensors;
413         GtkAllocation galloc;
414         GtkStyleContext *style_ctx;
415         struct graph_info info;
416
417         if (!gtk_widget_is_drawable(w_graph))
418                 return;
419
420         enabled_sensors = psensor_list_filter_graph_enabled(sensors);
421
422         min_rpm = get_min_rpm(enabled_sensors);
423         max_rpm = get_max_rpm(enabled_sensors);
424
425         mint = get_min_temp(enabled_sensors);
426         strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
427                                       mint,
428                                       config->temperature_unit == CELSIUS);
429
430         maxt = get_max_temp(enabled_sensors);
431         strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
432                                       maxt,
433                                       config->temperature_unit == CELSIUS);
434
435         et = get_graph_end_time_s(enabled_sensors);
436         bt = get_graph_begin_time_s(config, et);
437
438         str_btime = time_to_str(bt);
439         str_etime = time_to_str(et);
440
441         gtk_widget_get_allocation(w_graph, &galloc);
442         width = galloc.width;
443         height = galloc.height;
444
445         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
446         cr = cairo_create(cst);
447
448         cairo_select_font_face(cr,
449                                "sans-serif",
450                                CAIRO_FONT_SLANT_NORMAL,
451                                CAIRO_FONT_WEIGHT_NORMAL);
452         cairo_set_font_size(cr, 10.0);
453
454         cairo_text_extents(cr, str_etime, &te_etime);
455         cairo_text_extents(cr, str_btime, &te_btime);
456         cairo_text_extents(cr, strmax, &te_max);
457         cairo_text_extents(cr, strmin, &te_min);
458
459         g_yoff = GRAPH_V_PADDING;
460
461         g_height = height - GRAPH_V_PADDING;
462         if (te_etime.height > te_btime.height)
463                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
464         else
465                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
466
467         if (te_min.width > te_max.width)
468                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
469         else
470                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
471
472         info.g_xoff = g_xoff;
473         info.g_yoff = g_yoff;
474         info.height = height;
475
476         style_ctx = gtk_widget_get_style_context(window);
477         gtk_style_context_get_background_color(style_ctx,
478                                                GTK_STATE_FLAG_NORMAL,
479                                                &info.theme_bg_color);
480         gtk_style_context_get_color(style_ctx,
481                                     GTK_STATE_FLAG_NORMAL,
482                                     &info.theme_fg_color);
483
484
485         g_width = width - g_xoff - GRAPH_H_PADDING;
486
487         draw_graph_background(cr,
488                               g_xoff, g_yoff, g_width, g_height,
489                               width, height, config,
490                               w_graph,
491                               window);
492
493         /* Set the color for text drawing */
494         cairo_set_source_rgb(cr,
495                              info.theme_fg_color.red,
496                              info.theme_fg_color.green,
497                              info.theme_fg_color.blue);
498
499         /* draw graph begin time */
500         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
501         cairo_show_text(cr, str_btime);
502         free(str_btime);
503
504         /* draw graph end time */
505         cairo_move_to(cr,
506                       width - te_etime.width - GRAPH_H_PADDING,
507                       height - GRAPH_V_PADDING);
508         cairo_show_text(cr, str_etime);
509         free(str_etime);
510
511         draw_background_lines(cr, fgcolor,
512                               g_width, g_height,
513                               g_xoff, g_yoff,
514                               mint, maxt);
515
516         /* .. and finaly draws the temperature graphs */
517         if (bt && et) {
518                 sensor_cur = enabled_sensors;
519
520                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
521                 cairo_set_line_width(cr, 1);
522                 no_graphs = 1;
523                 while (*sensor_cur) {
524                         struct psensor *s = *sensor_cur;
525
526                         no_graphs = 0;
527                         if (s->type & SENSOR_TYPE_RPM) {
528                                 min = min_rpm;
529                                 max = max_rpm;
530                         } else if (s->type & SENSOR_TYPE_PERCENT) {
531                                 min = 0;
532                                 max = get_max_value(enabled_sensors,
533                                                     SENSOR_TYPE_PERCENT);
534                         } else {
535                                 min = mint;
536                                 max = maxt;
537                         }
538
539                         if (is_smooth_curves_enabled)
540                                 draw_sensor_smooth_curve(s, cr,
541                                                          min, max,
542                                                          bt, et,
543                                                          g_width, g_height,
544                                                          g_xoff, g_yoff);
545                         else
546                                 draw_sensor_curve(s, cr,
547                                                   min, max,
548                                                   bt, et,
549                                                   g_width, g_height,
550                                                   g_xoff, g_yoff);
551
552                         sensor_cur++;
553                 }
554
555                 if (no_graphs)
556                         display_no_graphs_warning(cr,
557                                                   g_xoff + 12,
558                                                   g_height / 2);
559         }
560
561         draw_left_region(cr, &info);
562
563         /* draw min and max temp */
564         cairo_set_source_rgb(cr,
565                              info.theme_fg_color.red,
566                              info.theme_fg_color.green,
567                              info.theme_fg_color.blue);
568
569         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
570         cairo_show_text(cr, strmax);
571         free(strmax);
572
573         cairo_move_to(cr,
574                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
575         cairo_show_text(cr, strmin);
576         free(strmin);
577
578         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
579
580         if (cr_pixmap) {
581                 if (config->alpha_channel_enabled)
582                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
583
584                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
585                 cairo_paint(cr_pixmap);
586         }
587
588         free(enabled_sensors);
589
590         cairo_destroy(cr_pixmap);
591         cairo_surface_destroy(cst);
592         cairo_destroy(cr);
593 }
594
595 int compute_values_max_length(struct config *c)
596 {
597         int n, duration, interval;
598
599         duration = c->graph_monitoring_duration * 60;
600         interval = c->sensor_update_interval;
601
602         n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;
603
604         return n;
605 }