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