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