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