2 * Copyright (C) 2010-2014 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
24 #include <glib/gi18n.h>
31 /* horizontal padding */
32 #define GRAPH_H_PADDING 4
33 /* vertical padding */
34 #define GRAPH_V_PADDING 4
36 static time_t get_graph_end_time_s()
40 if (gettimeofday(&tv, NULL) == 0)
46 static time_t get_graph_begin_time_s(struct config *cfg)
50 ct = get_graph_end_time_s();
55 return ct - cfg->graph_monitoring_duration * 60;
58 static int compute_y(double value, double min, double max, int height, int off)
60 double t = value - min;
61 return height - ((double)height * (t / (max - min))) + off;
64 static char *time_to_str(time_t s)
67 /* note: localtime returns a static field, no free required */
68 struct tm *tm = localtime(&s);
74 strftime(str, 6, "%H:%M", tm);
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,
87 GtkStyleContext *style_ctx;
88 struct color *bgcolor;
91 bgcolor = config->graph_bgcolor;
93 style_ctx = gtk_widget_get_style_context(window);
94 gtk_style_context_get_background_color(style_ctx,
95 GTK_STATE_FLAG_NORMAL,
98 if (config->alpha_channel_enabled)
99 cairo_set_source_rgba(cr,
103 config->graph_bg_alpha);
105 cairo_set_source_rgb(cr,
110 cairo_rectangle(cr, 0, 0, width, height);
112 if (config->alpha_channel_enabled)
113 cairo_set_source_rgba(cr,
117 config->graph_bg_alpha);
119 cairo_set_source_rgb(cr,
124 cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
128 /* setup dash style */
129 static double dashes[] = {
133 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
135 static void draw_background_lines(cairo_t *cr,
137 int g_width, int g_height,
138 int g_xoff, int g_yoff,
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);
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);
157 /* horizontal lines draws a line for each 10C step */
158 for (i = min; i < max; i++) {
160 int y = compute_y(i, min, max, g_height, g_yoff);
162 cairo_move_to(cr, g_xoff, y);
163 cairo_line_to(cr, g_xoff + g_width, y);
168 /* back to normal line style */
169 cairo_set_dash(cr, 0, 0, 0);
172 static void draw_sensor_curve(struct psensor *s,
183 int first, i, x, y, t, dt, vdt;
186 cairo_set_source_rgb(cr,
193 for (i = 0; i < s->values_max_length; i++) {
194 t = s->measures[i].time.tv_sec;
195 v = s->measures[i].value;
197 if (v == UNKNOWN_DBL_VALUE || !t)
204 x = vdt * g_width / dt + g_xoff;
206 y = compute_y(v, min, max, g_height, g_yoff);
209 cairo_move_to(cr, x, y);
212 cairo_line_to(cr, x, y);
219 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
223 msg = strdup(_("No graphs enabled"));
225 cairo_select_font_face(cr,
227 CAIRO_FONT_SLANT_NORMAL,
228 CAIRO_FONT_WEIGHT_NORMAL);
229 cairo_set_font_size(cr, 18.0);
231 cairo_move_to(cr, x, y);
232 cairo_show_text(cr, msg);
238 graph_update(struct psensor **sensors,
240 struct config *config,
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, **enabled_sensors;
254 GtkAllocation galloc;
255 GtkStyleContext *style_ctx;
258 if (!gtk_widget_is_drawable(w_graph))
261 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
263 min_rpm = get_min_rpm(enabled_sensors);
264 max_rpm = get_max_rpm(enabled_sensors);
266 mint = get_min_temp(enabled_sensors);
267 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
269 config->temperature_unit == CELSIUS);
271 maxt = get_max_temp(enabled_sensors);
272 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
274 config->temperature_unit == CELSIUS);
276 str_btime = time_to_str(get_graph_begin_time_s(config));
277 str_etime = time_to_str(get_graph_end_time_s());
279 gtk_widget_get_allocation(w_graph, &galloc);
280 width = galloc.width;
281 height = galloc.height;
283 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
284 cr = cairo_create(cst);
286 cairo_select_font_face(cr,
288 CAIRO_FONT_SLANT_NORMAL,
289 CAIRO_FONT_WEIGHT_NORMAL);
290 cairo_set_font_size(cr, 10.0);
292 cairo_text_extents(cr, str_etime, &te_etime);
293 cairo_text_extents(cr, str_btime, &te_btime);
294 cairo_text_extents(cr, strmax, &te_max);
295 cairo_text_extents(cr, strmin, &te_min);
297 g_yoff = GRAPH_V_PADDING;
299 g_height = height - GRAPH_V_PADDING;
300 if (te_etime.height > te_btime.height)
301 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
303 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
305 if (te_min.width > te_max.width)
306 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
308 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
310 g_width = width - g_xoff - GRAPH_H_PADDING;
312 draw_graph_background(cr,
313 g_xoff, g_yoff, g_width, g_height,
314 width, height, config,
318 /** Set the color for text drawing */
319 style_ctx = gtk_widget_get_style_context(window);
320 gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
321 cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
323 /* draw graph begin time */
324 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
325 cairo_show_text(cr, str_btime);
328 /* draw graph end time */
330 width - te_etime.width - GRAPH_H_PADDING,
331 height - GRAPH_V_PADDING);
332 cairo_show_text(cr, str_etime);
335 /* draw min and max temp */
336 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
337 cairo_show_text(cr, strmax);
341 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
342 cairo_show_text(cr, strmin);
345 draw_background_lines(cr, fgcolor,
350 /* .. and finaly draws the temperature graphs */
351 bt = get_graph_begin_time_s(config);
352 et = get_graph_end_time_s();
355 sensor_cur = enabled_sensors;
357 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
358 cairo_set_line_width(cr, 1);
360 while (*sensor_cur) {
361 struct psensor *s = *sensor_cur;
364 if (is_fan_type(s->type)) {
367 } else if (s->type & SENSOR_TYPE_CPU_USAGE) {
369 max = get_max_value(enabled_sensors,
370 SENSOR_TYPE_CPU_USAGE);
376 draw_sensor_curve(s, cr,
386 display_no_graphs_warning(cr,
391 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
395 if (config->alpha_channel_enabled)
396 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
398 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
399 cairo_paint(cr_pixmap);
402 free(enabled_sensors);
404 cairo_destroy(cr_pixmap);
405 cairo_surface_destroy(cst);