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