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