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