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