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