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