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