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