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