fixed >80col
[psensor.git] / src / graph.c
1 /*
2  * Copyright (C) 2010-2012 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 "log.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;
254         GtkAllocation galloc;
255         GtkStyleContext *style_ctx;
256         GdkRGBA rgba;
257
258         if (!gtk_widget_is_drawable(w_graph))
259                 return ;
260
261         min_rpm = get_min_rpm(sensors);
262         max_rpm = get_max_rpm(sensors);
263
264         mint = get_min_temp(sensors);
265
266         strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
267                                       mint,
268                                       config->temperature_unit == CELCIUS);
269
270         maxt = get_max_temp(sensors);
271         strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
272                                       maxt,
273                                       config->temperature_unit == CELCIUS);
274
275         str_btime = time_to_str(get_graph_begin_time_s(config));
276         str_etime = time_to_str(get_graph_end_time_s());
277
278         gtk_widget_get_allocation(w_graph, &galloc);
279         width = galloc.width;
280         height = galloc.height;
281
282         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
283         cr = cairo_create(cst);
284
285         cairo_select_font_face(cr,
286                                "sans-serif",
287                                CAIRO_FONT_SLANT_NORMAL,
288                                CAIRO_FONT_WEIGHT_NORMAL);
289         cairo_set_font_size(cr, 10.0);
290
291         cairo_text_extents(cr, str_etime, &te_etime);
292         cairo_text_extents(cr, str_btime, &te_btime);
293         cairo_text_extents(cr, strmax, &te_max);
294         cairo_text_extents(cr, strmin, &te_min);
295
296         g_yoff = GRAPH_V_PADDING;
297
298         g_height = height - GRAPH_V_PADDING;
299         if (te_etime.height > te_btime.height)
300                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
301         else
302                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
303
304         if (te_min.width > te_max.width)
305                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
306         else
307                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
308
309         g_width = width - g_xoff - GRAPH_H_PADDING;
310
311         draw_graph_background(cr,
312                               g_xoff, g_yoff, g_width, g_height,
313                               width, height, config,
314                               w_graph,
315                               window);
316
317         /** Set the color for text drawing */
318         style_ctx = gtk_widget_get_style_context(window);
319         gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
320         cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
321
322         /* draw graph begin time */
323         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
324         cairo_show_text(cr, str_btime);
325         free(str_btime);
326
327         /* draw graph end time */
328         cairo_move_to(cr,
329                       width - te_etime.width - GRAPH_H_PADDING,
330                       height - GRAPH_V_PADDING);
331         cairo_show_text(cr, str_etime);
332         free(str_etime);
333
334         /* draw min and max temp */
335         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
336         cairo_show_text(cr, strmax);
337         free(strmax);
338
339         cairo_move_to(cr,
340                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
341         cairo_show_text(cr, strmin);
342         free(strmin);
343
344         draw_background_lines(cr, fgcolor,
345                               g_width, g_height,
346                               g_xoff, g_yoff,
347                               mint, maxt);
348
349         /* .. and finaly draws the temperature graphs */
350         bt = get_graph_begin_time_s(config);
351         et = get_graph_end_time_s();
352
353         if (bt && et) {
354                 sensor_cur = sensors;
355
356                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
357                 cairo_set_line_width(cr, 1);
358                 no_graphs = 1;
359                 while (*sensor_cur) {
360                         struct psensor *s = *sensor_cur;
361
362                         if (s->enabled) {
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
370                                                 (sensors,
371                                                  SENSOR_TYPE_CPU_USAGE);
372                                 } else {
373                                         min = mint;
374                                         max = maxt;
375                                 }
376
377                                 draw_sensor_curve(s, cr,
378                                                   min, max,
379                                                   bt, et,
380                                                   g_width, g_height,
381                                                   g_xoff, g_yoff);
382                         }
383
384                         sensor_cur++;
385                 }
386
387                 if (no_graphs)
388                         display_no_graphs_warning(cr,
389                                                   g_xoff + 12,
390                                                   g_height / 2);
391         }
392
393         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
394
395         if (cr_pixmap) {
396
397                 if (config->alpha_channel_enabled)
398                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
399
400                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
401                 cairo_paint(cr_pixmap);
402         }
403
404         cairo_destroy(cr_pixmap);
405         cairo_surface_destroy(cst);
406         cairo_destroy(cr);
407 }