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