Imported Upstream version 1.0.3
[psensor-pkg-debian.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 "cfg.h"
28 #include <plog.h>
29 #include "psensor.h"
30
31 /* horizontal padding */
32 #define GRAPH_H_PADDING 4
33 /* vertical padding */
34 #define GRAPH_V_PADDING 4
35
36 static time_t get_graph_end_time_s()
37 {
38         struct timeval tv;
39
40         if (gettimeofday(&tv, NULL) == 0)
41                 return tv.tv_sec;
42         else
43                 return 0;
44 }
45
46 static time_t get_graph_begin_time_s(struct config *cfg)
47 {
48         int ct;
49
50         ct = get_graph_end_time_s();
51
52         if (!ct)
53                 return 0;
54
55         return ct - cfg->graph_monitoring_duration * 60;
56 }
57
58 static int compute_y(double value, double min, double max, int height, int off)
59 {
60         double t = value - min;
61         return height - ((double)height * (t / (max - min))) + off;
62 }
63
64 static char *time_to_str(time_t s)
65 {
66         char *str;
67         /* note: localtime returns a static field, no free required */
68         struct tm *tm = localtime(&s);
69
70         if (!tm)
71                 return NULL;
72
73         str = malloc(6);
74         strftime(str, 6, "%H:%M", tm);
75
76         return str;
77 }
78
79 static void
80 draw_graph_background(cairo_t *cr,
81                       int g_xoff, int g_yoff,
82                       int g_width, int g_height,
83                       int width, int height, struct config *config,
84                       GtkWidget *widget,
85                       GtkWidget *window)
86 {
87         GtkStyleContext *style_ctx;
88         struct color *bgcolor;
89         GdkRGBA rgba;
90
91         bgcolor = config->graph_bgcolor;
92
93         style_ctx = gtk_widget_get_style_context(window);
94         gtk_style_context_get_background_color(style_ctx,
95                                                GTK_STATE_FLAG_NORMAL,
96                                                &rgba);
97
98         if (config->alpha_channel_enabled)
99                 cairo_set_source_rgba(cr,
100                                       rgba.red,
101                                       rgba.green,
102                                       rgba.blue,
103                                       config->graph_bg_alpha);
104         else
105                 cairo_set_source_rgb(cr,
106                                      rgba.red,
107                                      rgba.green,
108                                      rgba.blue);
109
110         cairo_rectangle(cr, 0, 0, width, height);
111         cairo_fill(cr);
112         if (config->alpha_channel_enabled)
113                 cairo_set_source_rgba(cr,
114                                       bgcolor->f_red,
115                                       bgcolor->f_green,
116                                       bgcolor->f_blue,
117                                       config->graph_bg_alpha);
118         else
119                 cairo_set_source_rgb(cr,
120                                      bgcolor->f_red,
121                                      bgcolor->f_green,
122                                      bgcolor->f_blue);
123
124         cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
125         cairo_fill(cr);
126 }
127
128 /* setup dash style */
129 static double dashes[] = {
130         1.0,            /* ink */
131         2.0,            /* skip */
132 };
133 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
134
135 static void draw_background_lines(cairo_t *cr,
136                                   struct color *color,
137                                   int g_width, int g_height,
138                                   int g_xoff, int g_yoff,
139                                   int min, int max)
140 {
141         int i;
142
143         /* draw background lines */
144         cairo_set_line_width(cr, 1);
145         cairo_set_dash(cr, dashes, ndash, 0);
146         cairo_set_source_rgb(cr,
147                              color->f_red, color->f_green, color->f_blue);
148
149         /* vertical lines representing time steps */
150         for (i = 0; i <= 5; i++) {
151                 int x = i * (g_width / 5) + g_xoff;
152                 cairo_move_to(cr, x, g_yoff);
153                 cairo_line_to(cr, x, g_yoff + g_height);
154                 cairo_stroke(cr);
155         }
156
157         /* horizontal lines draws a line for each 10C step */
158         for (i = min; i < max; i++) {
159                 if (i % 10 == 0) {
160                         int y = compute_y(i, min, max, g_height, g_yoff);
161
162                         cairo_move_to(cr, g_xoff, y);
163                         cairo_line_to(cr, g_xoff + g_width, y);
164                         cairo_stroke(cr);
165                 }
166         }
167
168         /* back to normal line style */
169         cairo_set_dash(cr, 0, 0, 0);
170 }
171
172 static void draw_sensor_curve(struct psensor *s,
173                               cairo_t *cr,
174                               double min,
175                               double max,
176                               int bt,
177                               int et,
178                               int g_width,
179                               int g_height,
180                               int g_xoff,
181                               int g_yoff)
182 {
183         int first, i, x, y, t, dt, vdt;
184         double v;
185
186         cairo_set_source_rgb(cr,
187                              s->color->f_red,
188                              s->color->f_green,
189                              s->color->f_blue);
190
191         dt = et - bt;
192         first = 1;
193         for (i = 0; i < s->values_max_length; i++) {
194                 t = s->measures[i].time.tv_sec;
195                 v = s->measures[i].value;
196
197                 if (v == UNKNOWN_DBL_VALUE || !t)
198                         continue;
199
200                 vdt = t - bt;
201                 if (vdt < 0)
202                         continue;
203
204                 x = vdt * g_width / dt + g_xoff;
205
206                 y = compute_y(v, min, max, g_height, g_yoff);
207
208                 if (first) {
209                         cairo_move_to(cr, x, y);
210                         first = 0;
211                 } else {
212                         cairo_line_to(cr, x, y);
213                 }
214
215         }
216         cairo_stroke(cr);
217 }
218
219 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
220 {
221         char *msg;
222
223         msg = strdup(_("No graphs enabled"));
224
225         cairo_select_font_face(cr,
226                                "sans-serif",
227                                CAIRO_FONT_SLANT_NORMAL,
228                                CAIRO_FONT_WEIGHT_NORMAL);
229         cairo_set_font_size(cr, 18.0);
230
231         cairo_move_to(cr, x, y);
232         cairo_show_text(cr, msg);
233
234         free(msg);
235 }
236
237 void
238 graph_update(struct psensor **sensors,
239              GtkWidget *w_graph,
240              struct config *config,
241              GtkWidget *window)
242 {
243         struct color *fgcolor = config->graph_fgcolor;
244         int et, bt, width, height, g_width, g_height;
245         double min_rpm, max_rpm, mint, maxt;
246         char *strmin, *strmax;
247         /* horizontal and vertical offset of the graph */
248         int g_xoff, g_yoff, no_graphs, min, max;
249         cairo_surface_t *cst;
250         cairo_t *cr, *cr_pixmap;
251         char *str_btime, *str_etime;
252         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
253         struct psensor **sensor_cur, **enabled_sensors;
254         GtkAllocation galloc;
255         GtkStyleContext *style_ctx;
256         GdkRGBA rgba;
257
258         if (!gtk_widget_is_drawable(w_graph))
259                 return ;
260
261         enabled_sensors = psensor_list_filter_graph_enabled(sensors);
262
263         min_rpm = get_min_rpm(enabled_sensors);
264         max_rpm = get_max_rpm(enabled_sensors);
265
266         mint = get_min_temp(enabled_sensors);
267         strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
268                                       mint,
269                                       config->temperature_unit == CELSIUS);
270
271         maxt = get_max_temp(enabled_sensors);
272         strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
273                                       maxt,
274                                       config->temperature_unit == CELSIUS);
275
276         str_btime = time_to_str(get_graph_begin_time_s(config));
277         str_etime = time_to_str(get_graph_end_time_s());
278
279         gtk_widget_get_allocation(w_graph, &galloc);
280         width = galloc.width;
281         height = galloc.height;
282
283         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
284         cr = cairo_create(cst);
285
286         cairo_select_font_face(cr,
287                                "sans-serif",
288                                CAIRO_FONT_SLANT_NORMAL,
289                                CAIRO_FONT_WEIGHT_NORMAL);
290         cairo_set_font_size(cr, 10.0);
291
292         cairo_text_extents(cr, str_etime, &te_etime);
293         cairo_text_extents(cr, str_btime, &te_btime);
294         cairo_text_extents(cr, strmax, &te_max);
295         cairo_text_extents(cr, strmin, &te_min);
296
297         g_yoff = GRAPH_V_PADDING;
298
299         g_height = height - GRAPH_V_PADDING;
300         if (te_etime.height > te_btime.height)
301                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
302         else
303                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
304
305         if (te_min.width > te_max.width)
306                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
307         else
308                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
309
310         g_width = width - g_xoff - GRAPH_H_PADDING;
311
312         draw_graph_background(cr,
313                               g_xoff, g_yoff, g_width, g_height,
314                               width, height, config,
315                               w_graph,
316                               window);
317
318         /** Set the color for text drawing */
319         style_ctx = gtk_widget_get_style_context(window);
320         gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
321         cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
322
323         /* draw graph begin time */
324         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
325         cairo_show_text(cr, str_btime);
326         free(str_btime);
327
328         /* draw graph end time */
329         cairo_move_to(cr,
330                       width - te_etime.width - GRAPH_H_PADDING,
331                       height - GRAPH_V_PADDING);
332         cairo_show_text(cr, str_etime);
333         free(str_etime);
334
335         /* draw min and max temp */
336         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
337         cairo_show_text(cr, strmax);
338         free(strmax);
339
340         cairo_move_to(cr,
341                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
342         cairo_show_text(cr, strmin);
343         free(strmin);
344
345         draw_background_lines(cr, fgcolor,
346                               g_width, g_height,
347                               g_xoff, g_yoff,
348                               mint, maxt);
349
350         /* .. and finaly draws the temperature graphs */
351         bt = get_graph_begin_time_s(config);
352         et = get_graph_end_time_s();
353
354         if (bt && et) {
355                 sensor_cur = enabled_sensors;
356
357                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
358                 cairo_set_line_width(cr, 1);
359                 no_graphs = 1;
360                 while (*sensor_cur) {
361                         struct psensor *s = *sensor_cur;
362
363                         no_graphs = 0;
364                         if (is_fan_type(s->type)) {
365                                 min = min_rpm;
366                                 max = max_rpm;
367                         } else if (s->type & SENSOR_TYPE_CPU_USAGE) {
368                                 min = 0;
369                                 max = get_max_value(enabled_sensors,
370                                                     SENSOR_TYPE_CPU_USAGE);
371                         } else {
372                                 min = mint;
373                                 max = maxt;
374                         }
375
376                         draw_sensor_curve(s, cr,
377                                           min, max,
378                                           bt, et,
379                                           g_width, g_height,
380                                           g_xoff, g_yoff);
381
382                         sensor_cur++;
383                 }
384
385                 if (no_graphs)
386                         display_no_graphs_warning(cr,
387                                                   g_xoff + 12,
388                                                   g_height / 2);
389         }
390
391         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
392
393         if (cr_pixmap) {
394
395                 if (config->alpha_channel_enabled)
396                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
397
398                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
399                 cairo_paint(cr_pixmap);
400         }
401
402         free(enabled_sensors);
403
404         cairo_destroy(cr_pixmap);
405         cairo_surface_destroy(cst);
406         cairo_destroy(cr);
407 }