added comments
[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 /* Keys: sensor identifier.
174  *
175  * Values: array of time_t. Each time_t is corresponding to a sensor
176  * measure which has been used as the start point of a Bezier curve.
177  */
178 static GHashTable *times;
179
180 static void draw_sensor_smooth_curve(struct psensor *s,
181                                      cairo_t *cr,
182                                      double min,
183                                      double max,
184                                      int bt,
185                                      int et,
186                                      int g_width,
187                                      int g_height,
188                                      int g_xoff,
189                                      int g_yoff)
190 {
191         int i, dt, vdt, j, k, found;
192         double x[4], y[4], v;
193         time_t t, t0, *stimes;
194
195         if (!times)
196                 times = g_hash_table_new_full(g_str_hash,
197                                               g_str_equal,
198                                               free,
199                                               free);
200
201         stimes = g_hash_table_lookup(times, s->id);
202
203         cairo_set_source_rgb(cr,
204                              s->color->red,
205                              s->color->green,
206                              s->color->blue);
207
208         /* search the index of the first measure used as a start point
209          * of a Bezier curve. The start and end points of the Bezier
210          * curves must be preserved to ensure the same overall shape
211          * of the graph. */
212         i = 0;
213         if (stimes) {
214                 while (i < s->values_max_length) {
215                         t = s->measures[i].time.tv_sec;
216                         v = s->measures[i].value;
217
218                         found = 0;
219                         if (v != UNKNOWN_DBL_VALUE && t) {
220                                 k = 0;
221                                 while (stimes[k]) {
222                                         if (t == stimes[k]) {
223                                                 found = 1;
224                                                 break;
225                                         }
226                                         k++;
227                                 }
228                         }
229
230                         if (found)
231                                 break;
232
233                         i++;
234                 }
235         }
236
237         stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
238         memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
239         g_hash_table_insert(times, strdup(s->id), stimes);
240
241         if (i == s->values_max_length)
242                 i = 0;
243
244         k = 0;
245         dt = et - bt;
246         while (i < s->values_max_length) {
247                 j = 0;
248                 t = 0;
249                 while (i < s->values_max_length && j < 4) {
250                         t = s->measures[i].time.tv_sec;
251                         v = s->measures[i].value;
252
253                         if (v == UNKNOWN_DBL_VALUE || !t) {
254                                 i++;
255                                 continue;
256                         }
257
258                         vdt = t - bt;
259
260                         x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
261                         y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
262
263                         if (j == 0)
264                                 t0 = t;
265
266                         i++;
267                         j++;
268                 }
269
270                 if (j == 4) {
271                         cairo_move_to(cr, x[0], y[0]);
272                         cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
273                         stimes[k++] = t0;
274                         i--;
275                 }
276         }
277
278         cairo_stroke(cr);
279 }
280
281 static void draw_sensor_curve(struct psensor *s,
282                               cairo_t *cr,
283                               double min,
284                               double max,
285                               int bt,
286                               int et,
287                               int g_width,
288                               int g_height,
289                               int g_xoff,
290                               int g_yoff)
291 {
292         int first, i, t, dt, vdt;
293         double v, x, y;
294
295         cairo_set_source_rgb(cr,
296                              s->color->red,
297                              s->color->green,
298                              s->color->blue);
299
300         dt = et - bt;
301         first = 1;
302         for (i = 0; i < s->values_max_length; i++) {
303                 t = s->measures[i].time.tv_sec;
304                 v = s->measures[i].value;
305
306                 if (v == UNKNOWN_DBL_VALUE || !t)
307                         continue;
308
309                 vdt = t - bt;
310                 if (vdt < 0)
311                         continue;
312
313                 x = ((double)vdt * g_width) / dt + g_xoff;
314
315                 y = compute_y(v, min, max, g_height, g_yoff);
316
317                 if (first) {
318                         cairo_move_to(cr, x, y);
319                         first = 0;
320                 } else {
321                         cairo_line_to(cr, x, y);
322                 }
323
324         }
325         cairo_stroke(cr);
326 }
327
328 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
329 {
330         char *msg;
331
332         msg = strdup(_("No graphs enabled"));
333
334         cairo_select_font_face(cr,
335                                "sans-serif",
336                                CAIRO_FONT_SLANT_NORMAL,
337                                CAIRO_FONT_WEIGHT_NORMAL);
338         cairo_set_font_size(cr, 18.0);
339
340         cairo_move_to(cr, x, y);
341         cairo_show_text(cr, msg);
342
343         free(msg);
344 }
345
346 void
347 graph_update(struct psensor **sensors,
348              GtkWidget *w_graph,
349              struct config *config,
350              GtkWidget *window)
351 {
352         struct color *fgcolor = config->graph_fgcolor;
353         int et, bt, width, height, g_width, g_height;
354         double min_rpm, max_rpm, mint, maxt;
355         char *strmin, *strmax;
356         /* horizontal and vertical offset of the graph */
357         int g_xoff, g_yoff, no_graphs, min, max;
358         cairo_surface_t *cst;
359         cairo_t *cr, *cr_pixmap;
360         char *str_btime, *str_etime;
361         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
362         struct psensor **sensor_cur, **enabled_sensors;
363         GtkAllocation galloc;
364         GtkStyleContext *style_ctx;
365         GdkRGBA rgba;
366
367         if (!gtk_widget_is_drawable(w_graph))
368                 return ;
369
370         enabled_sensors = psensor_list_filter_graph_enabled(sensors);
371
372         min_rpm = get_min_rpm(enabled_sensors);
373         max_rpm = get_max_rpm(enabled_sensors);
374
375         mint = get_min_temp(enabled_sensors);
376         strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
377                                       mint,
378                                       config->temperature_unit == CELSIUS);
379
380         maxt = get_max_temp(enabled_sensors);
381         strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
382                                       maxt,
383                                       config->temperature_unit == CELSIUS);
384
385         str_btime = time_to_str(get_graph_begin_time_s(config));
386         str_etime = time_to_str(get_graph_end_time_s());
387
388         gtk_widget_get_allocation(w_graph, &galloc);
389         width = galloc.width;
390         height = galloc.height;
391
392         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
393         cr = cairo_create(cst);
394
395         cairo_select_font_face(cr,
396                                "sans-serif",
397                                CAIRO_FONT_SLANT_NORMAL,
398                                CAIRO_FONT_WEIGHT_NORMAL);
399         cairo_set_font_size(cr, 10.0);
400
401         cairo_text_extents(cr, str_etime, &te_etime);
402         cairo_text_extents(cr, str_btime, &te_btime);
403         cairo_text_extents(cr, strmax, &te_max);
404         cairo_text_extents(cr, strmin, &te_min);
405
406         g_yoff = GRAPH_V_PADDING;
407
408         g_height = height - GRAPH_V_PADDING;
409         if (te_etime.height > te_btime.height)
410                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
411         else
412                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
413
414         if (te_min.width > te_max.width)
415                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
416         else
417                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
418
419         g_width = width - g_xoff - GRAPH_H_PADDING;
420
421         draw_graph_background(cr,
422                               g_xoff, g_yoff, g_width, g_height,
423                               width, height, config,
424                               w_graph,
425                               window);
426
427         /** Set the color for text drawing */
428         style_ctx = gtk_widget_get_style_context(window);
429         gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
430         cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
431
432         /* draw graph begin time */
433         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
434         cairo_show_text(cr, str_btime);
435         free(str_btime);
436
437         /* draw graph end time */
438         cairo_move_to(cr,
439                       width - te_etime.width - GRAPH_H_PADDING,
440                       height - GRAPH_V_PADDING);
441         cairo_show_text(cr, str_etime);
442         free(str_etime);
443
444         /* draw min and max temp */
445         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
446         cairo_show_text(cr, strmax);
447         free(strmax);
448
449         cairo_move_to(cr,
450                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
451         cairo_show_text(cr, strmin);
452         free(strmin);
453
454         draw_background_lines(cr, fgcolor,
455                               g_width, g_height,
456                               g_xoff, g_yoff,
457                               mint, maxt);
458
459         /* .. and finaly draws the temperature graphs */
460         bt = get_graph_begin_time_s(config);
461         et = get_graph_end_time_s();
462
463         if (bt && et) {
464                 sensor_cur = enabled_sensors;
465
466                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
467                 cairo_set_line_width(cr, 1);
468                 no_graphs = 1;
469                 while (*sensor_cur) {
470                         struct psensor *s = *sensor_cur;
471
472                         no_graphs = 0;
473                         if (s->type & SENSOR_TYPE_RPM) {
474                                 min = min_rpm;
475                                 max = max_rpm;
476                         } else if (s->type & SENSOR_TYPE_PERCENT) {
477                                 min = 0;
478                                 max = get_max_value(enabled_sensors,
479                                                     SENSOR_TYPE_PERCENT);
480                         } else {
481                                 min = mint;
482                                 max = maxt;
483                         }
484
485                         if (1)
486                                 draw_sensor_smooth_curve(s, cr,
487                                                          min, max,
488                                                          bt, et,
489                                                          g_width, g_height,
490                                                          g_xoff, g_yoff);
491                         else
492                                 draw_sensor_curve(s, cr,
493                                                   min, max,
494                                                   bt, et,
495                                                   g_width, g_height,
496                                                   g_xoff, g_yoff);
497
498                         sensor_cur++;
499                 }
500
501                 if (no_graphs)
502                         display_no_graphs_warning(cr,
503                                                   g_xoff + 12,
504                                                   g_height / 2);
505         }
506
507         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
508
509         if (cr_pixmap) {
510                 if (config->alpha_channel_enabled)
511                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
512
513                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
514                 cairo_paint(cr_pixmap);
515         }
516
517         free(enabled_sensors);
518
519         cairo_destroy(cr_pixmap);
520         cairo_surface_destroy(cst);
521         cairo_destroy(cr);
522 }