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