Merge branch 'v1.1'
[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
301         if (!times)
302                 times = g_hash_table_new_full(g_str_hash,
303                                               g_str_equal,
304                                               free,
305                                               free);
306
307         stimes = g_hash_table_lookup(times, s->id);
308
309         cairo_set_source_rgb(cr,
310                              s->color->red,
311                              s->color->green,
312                              s->color->blue);
313
314         /* search the index of the first measure used as a start point
315          * of a Bezier curve. The start and end points of the Bezier
316          * curves must be preserved to ensure the same overall shape
317          * of the graph. */
318         i = 0;
319         if (stimes) {
320                 while (i < s->values_max_length) {
321                         t = s->measures[i].time.tv_sec;
322                         v = s->measures[i].value;
323
324                         found = 0;
325                         if (v != UNKNOWN_DBL_VALUE && t) {
326                                 k = 0;
327                                 while (stimes[k]) {
328                                         if (t == stimes[k]) {
329                                                 found = 1;
330                                                 break;
331                                         }
332                                         k++;
333                                 }
334                         }
335
336                         if (found)
337                                 break;
338
339                         i++;
340                 }
341         }
342
343         stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
344         memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
345         g_hash_table_insert(times, strdup(s->id), stimes);
346
347         if (i == s->values_max_length)
348                 i = 0;
349
350         k = 0;
351         dt = et - bt;
352         while (i < s->values_max_length) {
353                 j = 0;
354                 t = 0;
355                 while (i < s->values_max_length && j < 4) {
356                         t = s->measures[i].time.tv_sec;
357                         v = s->measures[i].value;
358
359                         if (v == UNKNOWN_DBL_VALUE || !t) {
360                                 i++;
361                                 continue;
362                         }
363
364                         vdt = t - bt;
365
366                         x[0 + j] = ((double)vdt * info->g_width)
367                                 / dt + info->g_xoff;
368                         y[0 + j] = compute_y(v,
369                                              min,
370                                              max,
371                                              info->g_height,
372                                              info->g_yoff);
373
374                         if (j == 0)
375                                 t0 = t;
376
377                         i++;
378                         j++;
379                 }
380
381                 if (j == 4) {
382                         cairo_move_to(cr, x[0], y[0]);
383                         cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
384                         stimes[k++] = t0;
385                         i--;
386                 }
387         }
388
389         cairo_stroke(cr);
390 }
391
392 static void draw_sensor_curve(struct psensor *s,
393                               cairo_t *cr,
394                               double min,
395                               double max,
396                               int bt,
397                               int et,
398                               struct graph_info *info)
399 {
400         int first, i, t, dt, vdt;
401         double v, x, y;
402
403         cairo_set_source_rgb(cr,
404                              s->color->red,
405                              s->color->green,
406                              s->color->blue);
407
408         dt = et - bt;
409         first = 1;
410         for (i = 0; i < s->values_max_length; i++) {
411                 t = s->measures[i].time.tv_sec;
412                 v = s->measures[i].value;
413
414                 if (v == UNKNOWN_DBL_VALUE || !t)
415                         continue;
416
417                 vdt = t - bt;
418
419                 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
420
421                 y = compute_y(v, min, max, info->g_height, info->g_yoff);
422
423                 if (first) {
424                         cairo_move_to(cr, x, y);
425                         first = 0;
426                 } else {
427                         cairo_line_to(cr, x, y);
428                 }
429
430         }
431         cairo_stroke(cr);
432 }
433
434 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
435 {
436         char *msg;
437
438         msg = strdup(_("No graphs enabled"));
439
440         cairo_select_font_face(cr,
441                                "sans-serif",
442                                CAIRO_FONT_SLANT_NORMAL,
443                                CAIRO_FONT_WEIGHT_NORMAL);
444         cairo_set_font_size(cr, 18.0);
445
446         cairo_move_to(cr, x, y);
447         cairo_show_text(cr, msg);
448
449         free(msg);
450 }
451
452 void
453 graph_update(struct psensor **sensors,
454              GtkWidget *w_graph,
455              struct config *config,
456              GtkWidget *window)
457 {
458         int et, bt, width, height, g_width, g_height;
459         double min_rpm, max_rpm, mint, maxt, min, max;
460         char *strmin, *strmax;
461         /* horizontal and vertical offset of the graph */
462         int g_xoff, g_yoff, no_graphs;
463         cairo_surface_t *cst;
464         cairo_t *cr, *cr_pixmap;
465         char *str_btime, *str_etime;
466         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
467         struct psensor **sensor_cur, **enabled_sensors;
468         GtkAllocation galloc;
469         GtkStyleContext *style_ctx;
470         struct graph_info info;
471
472         if (!gtk_widget_is_drawable(w_graph))
473                 return;
474
475         enabled_sensors = list_filter_graph_enabled(sensors);
476
477         min_rpm = get_min_rpm(enabled_sensors);
478         max_rpm = get_max_rpm(enabled_sensors);
479
480         mint = get_min_temp(enabled_sensors);
481         strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
482                                       mint,
483                                       config->temperature_unit == CELSIUS);
484
485         maxt = get_max_temp(enabled_sensors);
486         strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
487                                       maxt,
488                                       config->temperature_unit == CELSIUS);
489
490         et = get_graph_end_time_s(enabled_sensors);
491         bt = get_graph_begin_time_s(config, et);
492
493         str_btime = time_to_str(bt);
494         str_etime = time_to_str(et);
495
496         gtk_widget_get_allocation(w_graph, &galloc);
497         width = galloc.width;
498         info.width = galloc.width;
499         height = galloc.height;
500         info.height = height;
501
502
503         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
504         cr = cairo_create(cst);
505
506         cairo_select_font_face(cr,
507                                "sans-serif",
508                                CAIRO_FONT_SLANT_NORMAL,
509                                CAIRO_FONT_WEIGHT_NORMAL);
510         cairo_set_font_size(cr, 10.0);
511
512         cairo_text_extents(cr, str_etime, &te_etime);
513         cairo_text_extents(cr, str_btime, &te_btime);
514         cairo_text_extents(cr, strmax, &te_max);
515         cairo_text_extents(cr, strmin, &te_min);
516
517         g_yoff = GRAPH_V_PADDING;
518         info.g_yoff = g_yoff;
519
520         g_height = height - GRAPH_V_PADDING;
521         if (te_etime.height > te_btime.height)
522                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
523         else
524                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
525
526         info.g_height = g_height;
527
528         if (te_min.width > te_max.width)
529                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
530         else
531                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
532
533         info.g_xoff = g_xoff;
534
535         style_ctx = gtk_widget_get_style_context(window);
536         gtk_style_context_get_background_color(style_ctx,
537                                                GTK_STATE_FLAG_NORMAL,
538                                                &info.theme_bg_color);
539         gtk_style_context_get_color(style_ctx,
540                                     GTK_STATE_FLAG_NORMAL,
541                                     &info.theme_fg_color);
542
543         g_width = width - g_xoff - GRAPH_H_PADDING;
544         info.g_width = g_width;
545
546         draw_graph_background(cr, config, &info);
547
548         /* Set the color for text drawing */
549         cairo_set_source_rgb(cr,
550                              info.theme_fg_color.red,
551                              info.theme_fg_color.green,
552                              info.theme_fg_color.blue);
553
554         /* draw graph begin time */
555         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
556         cairo_show_text(cr, str_btime);
557         free(str_btime);
558
559         /* draw graph end time */
560         cairo_move_to(cr,
561                       width - te_etime.width - GRAPH_H_PADDING,
562                       height - GRAPH_V_PADDING);
563         cairo_show_text(cr, str_etime);
564         free(str_etime);
565
566         draw_background_lines(cr, mint, maxt, config, &info);
567
568         /* .. and finaly draws the temperature graphs */
569         if (bt && et) {
570                 sensor_cur = enabled_sensors;
571
572                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
573                 cairo_set_line_width(cr, 1);
574                 no_graphs = 1;
575                 while (*sensor_cur) {
576                         struct psensor *s = *sensor_cur;
577
578                         no_graphs = 0;
579                         if (s->type & SENSOR_TYPE_RPM) {
580                                 min = min_rpm;
581                                 max = max_rpm;
582                         } else if (s->type & SENSOR_TYPE_PERCENT) {
583                                 min = 0;
584                                 max = get_max_value(enabled_sensors,
585                                                     SENSOR_TYPE_PERCENT);
586                         } else {
587                                 min = mint;
588                                 max = maxt;
589                         }
590
591                         if (is_smooth_curves_enabled)
592                                 draw_sensor_smooth_curve(s, cr,
593                                                          min, max,
594                                                          bt, et,
595                                                          &info);
596                         else
597                                 draw_sensor_curve(s, cr,
598                                                   min, max,
599                                                   bt, et,
600                                                   &info);
601
602                         sensor_cur++;
603                 }
604
605                 if (no_graphs)
606                         display_no_graphs_warning(cr,
607                                                   g_xoff + 12,
608                                                   g_height / 2);
609         }
610
611         draw_left_region(cr, &info);
612         draw_right_region(cr, &info);
613
614         /* draw min and max temp */
615         cairo_set_source_rgb(cr,
616                              info.theme_fg_color.red,
617                              info.theme_fg_color.green,
618                              info.theme_fg_color.blue);
619
620         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
621         cairo_show_text(cr, strmax);
622         free(strmax);
623
624         cairo_move_to(cr,
625                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
626         cairo_show_text(cr, strmin);
627         free(strmin);
628
629         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
630
631         if (cr_pixmap) {
632                 if (config->alpha_channel_enabled)
633                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
634
635                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
636                 cairo_paint(cr_pixmap);
637         }
638
639         free(enabled_sensors);
640
641         cairo_destroy(cr_pixmap);
642         cairo_surface_destroy(cst);
643         cairo_destroy(cr);
644 }
645
646 int compute_values_max_length(struct config *c)
647 {
648         int n, duration, interval;
649
650         duration = c->graph_monitoring_duration * 60;
651         interval = c->sensor_update_interval;
652
653         n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;
654
655         return n;
656 }