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