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