style
[psensor.git] / src / graph.c
1 /*
2  * Copyright (C) 2010-2011 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 width, int height, struct config *config)
77 {
78         struct color *bgcolor = config->graph_bgcolor;
79
80         /* draw background */
81         if (config->alpha_channel_enabled)
82                 cairo_set_source_rgba(cr,
83                                       bgcolor->f_red,
84                                       bgcolor->f_green,
85                                       bgcolor->f_blue, config->graph_bg_alpha);
86         else
87                 cairo_set_source_rgb(cr,
88                                      bgcolor->f_red,
89                                      bgcolor->f_green, bgcolor->f_blue);
90
91         cairo_rectangle(cr, 0, 0, width, height);
92         cairo_fill(cr);
93 }
94
95 /* setup dash style */
96 static double dashes[] = {
97         1.0,            /* ink */
98         1.0,            /* skip */
99         1.0,            /* ink */
100         1.0             /* skip */
101 };
102 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
103
104 static void draw_background_lines(cairo_t *cr,
105                                   struct color *color,
106                                   int g_width, int g_height,
107                                   int g_xoff, int g_yoff,
108                                   int min, int max)
109 {
110         int i;
111
112         /* draw background lines */
113         cairo_set_dash(cr, dashes, ndash, 0);
114         cairo_set_line_width(cr, 1);
115         cairo_set_source_rgb(cr,
116                              color->f_red, color->f_green, color->f_blue);
117
118         /* vertical lines representing time steps */
119         for (i = 0; i <= 5; i++) {
120                 int x = i * (g_width / 5) + g_xoff;
121                 cairo_move_to(cr, x, g_yoff);
122                 cairo_line_to(cr, x, g_yoff + g_height);
123                 cairo_stroke(cr);
124         }
125
126         /* horizontal lines draws a line for each 10C step */
127         for (i = min; i < max; i++) {
128                 if (i % 10 == 0) {
129                         int y = compute_y(i, min, max, g_height, g_yoff);
130
131                         cairo_move_to(cr, g_xoff, y);
132                         cairo_line_to(cr, g_xoff + g_width, y);
133                         cairo_stroke(cr);
134                 }
135         }
136
137         /* back to normal line style */
138         cairo_set_dash(cr, 0, 0, 0);
139
140 }
141
142 static void draw_sensor_curve(struct psensor *s,
143                               cairo_t *cr,
144                               double min,
145                               double max,
146                               int bt,
147                               int et,
148                               int g_width,
149                               int g_height,
150                               int g_xoff,
151                               int g_yoff)
152 {
153         int first = 1;
154         int i;
155
156         cairo_set_source_rgb(cr,
157                              s->color->f_red,
158                              s->color->f_green,
159                              s->color->f_blue);
160         cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
161         cairo_set_line_width(cr, 1);
162
163         for (i = 0; i < s->values_max_length; i++) {
164                 int x, y, t;
165                 double v;
166
167                 t = s->measures[i].time.tv_sec;
168                 v = s->measures[i].value.d_num;
169
170                 if (v == UNKNOWN_DBL_VALUE || !t || (t - bt) < 0)
171                         continue;
172
173                 x = (t - bt) * g_width / (et - bt) + g_xoff;
174
175                 y = compute_y(v, min, max, g_height, g_yoff);
176
177                 if (first) {
178                         cairo_move_to(cr, x, y);
179                         first = 0;
180                 } else {
181                         cairo_line_to(cr, x, y);
182                 }
183
184         }
185         cairo_stroke(cr);
186 }
187
188
189 void
190 graph_update(struct psensor **sensors,
191              GtkWidget *w_graph,
192              struct config *config)
193 {
194         struct color *fgcolor = config->graph_fgcolor;
195         int et, bt, width, height, g_width, g_height;
196         double min_rpm, max_rpm, mint, maxt;
197         char *strmin, *strmax;
198         /* horizontal and vertical offset of the graph */
199         int g_xoff, g_yoff;
200         cairo_surface_t *cst;
201         cairo_t *cr, *cr_pixmap;
202         char *str_btime, *str_etime;
203         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
204         struct psensor **sensor_cur;
205         GtkAllocation galloc;
206
207         if (!gtk_widget_is_drawable(w_graph))
208                 return ;
209
210         min_rpm = get_min_rpm(sensors);
211         max_rpm = get_max_rpm(sensors);
212
213         mint = get_min_temp(sensors);
214         strmin = psensor_value_to_string(SENSOR_TYPE_TEMP, mint);
215
216         maxt = get_max_temp(sensors);
217         strmax = psensor_value_to_string(SENSOR_TYPE_TEMP, maxt);
218
219         str_btime = time_to_str(get_graph_begin_time_s(config));
220         str_etime = time_to_str(get_graph_end_time_s());
221
222         gtk_widget_get_allocation(w_graph, &galloc);
223         width = galloc.width;
224         height = galloc.height;
225
226         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
227         cr = cairo_create(cst);
228
229         draw_graph_background(cr, width, height, config);
230
231         cairo_select_font_face(cr,
232                                "sans-serif",
233                                CAIRO_FONT_SLANT_NORMAL,
234                                CAIRO_FONT_WEIGHT_NORMAL);
235         cairo_set_font_size(cr, 10.0);
236
237         cairo_text_extents(cr, str_etime, &te_etime);
238         cairo_text_extents(cr, str_btime, &te_btime);
239         cairo_text_extents(cr, strmax, &te_max);
240         cairo_text_extents(cr, strmin, &te_min);
241
242         g_yoff = GRAPH_V_PADDING;
243
244         g_height = height - GRAPH_V_PADDING;
245         if (te_etime.height > te_btime.height)
246                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
247         else
248                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
249
250         if (te_min.width > te_max.width)
251                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
252         else
253                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
254
255         g_width = width - g_xoff - GRAPH_H_PADDING;
256
257         cairo_set_source_rgb(cr,
258                              fgcolor->f_red, fgcolor->f_green, fgcolor->f_blue);
259
260         /* draw graph begin time */
261         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
262         cairo_show_text(cr, str_btime);
263         free(str_btime);
264
265         /* draw graph end time */
266         cairo_move_to(cr,
267                       width - te_etime.width - GRAPH_H_PADDING,
268                       height - GRAPH_V_PADDING);
269         cairo_show_text(cr, str_etime);
270         free(str_etime);
271
272         /* draw min and max temp */
273         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
274         cairo_show_text(cr, strmax);
275         free(strmax);
276
277         cairo_move_to(cr,
278                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
279         cairo_show_text(cr, strmin);
280         free(strmin);
281
282         draw_background_lines(cr, fgcolor,
283                               g_width, g_height,
284                               g_xoff, g_yoff,
285                               mint, maxt);
286
287         /* .. and finaly draws the temperature graphs */
288         bt = get_graph_begin_time_s(config);
289         et = get_graph_end_time_s();
290
291         if (bt && et) {
292                 sensor_cur = sensors;
293                 while (*sensor_cur) {
294                         struct psensor *s = *sensor_cur;
295
296                         if (s->enabled) {
297                                 double min, max;
298
299                                 if (is_fan_type(s->type)) {
300                                         min = min_rpm;
301                                         max = max_rpm;
302                                 } else if (s->type & SENSOR_TYPE_CPU_USAGE) {
303                                         min = 0;
304                                         max = get_max_value
305                                                 (sensors,
306                                                  SENSOR_TYPE_CPU_USAGE);
307                                 } else {
308                                         min = mint;
309                                         max = maxt;
310                                 }
311
312                                 draw_sensor_curve(s, cr,
313                                                   min, max,
314                                                   bt, et,
315                                                   g_width, g_height,
316                                                   g_xoff, g_yoff);
317                         }
318
319                         sensor_cur++;
320                 }
321         }
322
323         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
324
325         if (cr_pixmap) {
326
327                 if (config->alpha_channel_enabled)
328                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
329
330                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
331                 cairo_paint(cr_pixmap);
332         }
333
334         cairo_destroy(cr_pixmap);
335         cairo_surface_destroy(cst);
336         cairo_destroy(cr);
337 }