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