2 * Copyright (C) 2010-2011 jeanfi@gmail.com
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.
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.
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
26 /* horizontal padding */
27 #define GRAPH_H_PADDING 4
28 /* vertical padding */
29 #define GRAPH_V_PADDING 4
31 static time_t get_graph_end_time_s()
35 if (gettimeofday(&tv, NULL) == 0)
41 static time_t get_graph_begin_time_s(struct config *cfg)
45 ct = get_graph_end_time_s();
50 return ct - cfg->graph_monitoring_duration * 60;
53 static int compute_y(double value, double min, double max, int height, int off)
55 double t = value - min;
56 return height - ((double)height * (t / (max - min))) + off;
59 static char *time_to_str(time_t s)
62 /* note: localtime returns a static field, no free required */
63 struct tm *tm = localtime(&s);
69 strftime(str, 6, "%H:%M", tm);
75 draw_graph_background(cairo_t *cr,
76 int width, int height, struct config *config)
78 struct color *bgcolor = config->graph_bgcolor;
81 if (config->alpha_channel_enabled)
82 cairo_set_source_rgba(cr,
85 bgcolor->f_blue, config->graph_bg_alpha);
87 cairo_set_source_rgb(cr,
89 bgcolor->f_green, bgcolor->f_blue);
91 cairo_rectangle(cr, 0, 0, width, height);
95 /* setup dash style */
96 static double dashes[] = {
102 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
104 static void draw_background_lines(cairo_t *cr,
106 int g_width, int g_height,
107 int g_xoff, int g_yoff,
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);
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);
126 /* horizontal lines draws a line for each 10C step */
127 for (i = min; i < max; i++) {
129 int y = compute_y(i, min, max, g_height, g_yoff);
131 cairo_move_to(cr, g_xoff, y);
132 cairo_line_to(cr, g_xoff + g_width, y);
137 /* back to normal line style */
138 cairo_set_dash(cr, 0, 0, 0);
142 static void draw_sensor_curve(struct psensor *s,
156 cairo_set_source_rgb(cr,
160 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
161 cairo_set_line_width(cr, 1);
163 for (i = 0; i < s->values_max_length; i++) {
167 t = s->measures[i].time.tv_sec;
168 v = s->measures[i].value.d_num;
170 if (v == UNKNOWN_DBL_VALUE || !t || (t - bt) < 0)
173 x = (t - bt) * g_width / (et - bt) + g_xoff;
175 y = compute_y(v, min, max, g_height, g_yoff);
178 cairo_move_to(cr, x, y);
181 cairo_line_to(cr, x, y);
190 graph_update(struct psensor **sensors,
192 struct config *config)
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 */
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;
207 if (!gtk_widget_is_drawable(w_graph))
210 min_rpm = get_min_rpm(sensors);
211 max_rpm = get_max_rpm(sensors);
213 mint = get_min_temp(sensors);
214 strmin = psensor_value_to_string(SENSOR_TYPE_TEMP, mint);
216 maxt = get_max_temp(sensors);
217 strmax = psensor_value_to_string(SENSOR_TYPE_TEMP, maxt);
219 str_btime = time_to_str(get_graph_begin_time_s(config));
220 str_etime = time_to_str(get_graph_end_time_s());
222 gtk_widget_get_allocation(w_graph, &galloc);
223 width = galloc.width;
224 height = galloc.height;
226 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
227 cr = cairo_create(cst);
229 draw_graph_background(cr, width, height, config);
231 cairo_select_font_face(cr,
233 CAIRO_FONT_SLANT_NORMAL,
234 CAIRO_FONT_WEIGHT_NORMAL);
235 cairo_set_font_size(cr, 10.0);
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);
242 g_yoff = GRAPH_V_PADDING;
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;
248 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
250 if (te_min.width > te_max.width)
251 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
253 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
255 g_width = width - g_xoff - GRAPH_H_PADDING;
257 cairo_set_source_rgb(cr,
258 fgcolor->f_red, fgcolor->f_green, fgcolor->f_blue);
260 /* draw graph begin time */
261 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
262 cairo_show_text(cr, str_btime);
265 /* draw graph end time */
267 width - te_etime.width - GRAPH_H_PADDING,
268 height - GRAPH_V_PADDING);
269 cairo_show_text(cr, str_etime);
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);
278 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
279 cairo_show_text(cr, strmin);
282 draw_background_lines(cr, fgcolor,
287 /* .. and finaly draws the temperature graphs */
288 bt = get_graph_begin_time_s(config);
289 et = get_graph_end_time_s();
292 sensor_cur = sensors;
293 while (*sensor_cur) {
294 struct psensor *s = *sensor_cur;
299 if (is_fan_type(s->type)) {
302 } else if (s->type & SENSOR_TYPE_CPU_USAGE) {
306 SENSOR_TYPE_CPU_USAGE);
312 draw_sensor_curve(s, cr,
323 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
327 if (config->alpha_channel_enabled)
328 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
330 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
331 cairo_paint(cr_pixmap);
334 cairo_destroy(cr_pixmap);
335 cairo_surface_destroy(cst);