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