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 bool is_smooth_curves_enabled;
38 static time_t get_graph_end_time_s()
42 if (gettimeofday(&tv, NULL) == 0)
48 static time_t get_graph_begin_time_s(struct config *cfg)
52 ct = get_graph_end_time_s();
57 return ct - cfg->graph_monitoring_duration * 60;
61 compute_y(double value, double min, double max, int height, int off)
63 double t = value - min;
64 return height - ((double)height * (t / (max - min))) + off;
67 static char *time_to_str(time_t s)
70 /* note: localtime returns a static field, no free required */
71 struct tm *tm = localtime(&s);
77 strftime(str, 6, "%H:%M", tm);
83 draw_graph_background(cairo_t *cr,
84 int g_xoff, int g_yoff,
85 int g_width, int g_height,
86 int width, int height, struct config *config,
90 GtkStyleContext *style_ctx;
91 struct color *bgcolor;
94 bgcolor = config->graph_bgcolor;
96 style_ctx = gtk_widget_get_style_context(window);
97 gtk_style_context_get_background_color(style_ctx,
98 GTK_STATE_FLAG_NORMAL,
101 if (config->alpha_channel_enabled)
102 cairo_set_source_rgba(cr,
106 config->graph_bg_alpha);
108 cairo_set_source_rgb(cr,
113 cairo_rectangle(cr, 0, 0, width, height);
115 if (config->alpha_channel_enabled)
116 cairo_set_source_rgba(cr,
120 config->graph_bg_alpha);
122 cairo_set_source_rgb(cr,
127 cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
131 /* setup dash style */
132 static double dashes[] = {
136 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
138 static void draw_background_lines(cairo_t *cr,
140 int g_width, int g_height,
141 int g_xoff, int g_yoff,
146 /* draw background lines */
147 cairo_set_line_width(cr, 1);
148 cairo_set_dash(cr, dashes, ndash, 0);
149 cairo_set_source_rgb(cr,
150 color->red, color->green, color->blue);
152 /* vertical lines representing time steps */
153 for (i = 0; i <= 5; i++) {
154 int x = i * (g_width / 5) + g_xoff;
155 cairo_move_to(cr, x, g_yoff);
156 cairo_line_to(cr, x, g_yoff + g_height);
160 /* horizontal lines draws a line for each 10C step */
161 for (i = min; i < max; i++) {
163 int y = compute_y(i, min, max, g_height, g_yoff);
165 cairo_move_to(cr, g_xoff, y);
166 cairo_line_to(cr, g_xoff + g_width, y);
171 /* back to normal line style */
172 cairo_set_dash(cr, 0, 0, 0);
175 /* Keys: sensor identifier.
177 * Values: array of time_t. Each time_t is corresponding to a sensor
178 * measure which has been used as the start point of a Bezier curve.
180 static GHashTable *times;
182 static void draw_sensor_smooth_curve(struct psensor *s,
193 int i, dt, vdt, j, k, found;
194 double x[4], y[4], v;
195 time_t t, t0, *stimes;
198 times = g_hash_table_new_full(g_str_hash,
203 stimes = g_hash_table_lookup(times, s->id);
205 cairo_set_source_rgb(cr,
210 /* search the index of the first measure used as a start point
211 * of a Bezier curve. The start and end points of the Bezier
212 * curves must be preserved to ensure the same overall shape
216 while (i < s->values_max_length) {
217 t = s->measures[i].time.tv_sec;
218 v = s->measures[i].value;
221 if (v != UNKNOWN_DBL_VALUE && t) {
224 if (t == stimes[k]) {
239 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
240 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
241 g_hash_table_insert(times, strdup(s->id), stimes);
243 if (i == s->values_max_length)
248 while (i < s->values_max_length) {
251 while (i < s->values_max_length && j < 4) {
252 t = s->measures[i].time.tv_sec;
253 v = s->measures[i].value;
255 if (v == UNKNOWN_DBL_VALUE || !t) {
262 x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
263 y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
273 cairo_move_to(cr, x[0], y[0]);
274 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
283 static void draw_sensor_curve(struct psensor *s,
294 int first, i, t, dt, vdt;
297 cairo_set_source_rgb(cr,
304 for (i = 0; i < s->values_max_length; i++) {
305 t = s->measures[i].time.tv_sec;
306 v = s->measures[i].value;
308 if (v == UNKNOWN_DBL_VALUE || !t)
315 x = ((double)vdt * g_width) / dt + g_xoff;
317 y = compute_y(v, min, max, g_height, g_yoff);
320 cairo_move_to(cr, x, y);
323 cairo_line_to(cr, x, y);
330 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
334 msg = strdup(_("No graphs enabled"));
336 cairo_select_font_face(cr,
338 CAIRO_FONT_SLANT_NORMAL,
339 CAIRO_FONT_WEIGHT_NORMAL);
340 cairo_set_font_size(cr, 18.0);
342 cairo_move_to(cr, x, y);
343 cairo_show_text(cr, msg);
349 graph_update(struct psensor **sensors,
351 struct config *config,
354 struct color *fgcolor = config->graph_fgcolor;
355 int et, bt, width, height, g_width, g_height;
356 double min_rpm, max_rpm, mint, maxt, min, max;
357 char *strmin, *strmax;
358 /* horizontal and vertical offset of the graph */
359 int g_xoff, g_yoff, no_graphs;
360 cairo_surface_t *cst;
361 cairo_t *cr, *cr_pixmap;
362 char *str_btime, *str_etime;
363 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
364 struct psensor **sensor_cur, **enabled_sensors;
365 GtkAllocation galloc;
366 GtkStyleContext *style_ctx;
369 if (!gtk_widget_is_drawable(w_graph))
372 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
374 min_rpm = get_min_rpm(enabled_sensors);
375 max_rpm = get_max_rpm(enabled_sensors);
377 mint = get_min_temp(enabled_sensors);
378 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
380 config->temperature_unit == CELSIUS);
382 maxt = get_max_temp(enabled_sensors);
383 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
385 config->temperature_unit == CELSIUS);
387 str_btime = time_to_str(get_graph_begin_time_s(config));
388 str_etime = time_to_str(get_graph_end_time_s());
390 gtk_widget_get_allocation(w_graph, &galloc);
391 width = galloc.width;
392 height = galloc.height;
394 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
395 cr = cairo_create(cst);
397 cairo_select_font_face(cr,
399 CAIRO_FONT_SLANT_NORMAL,
400 CAIRO_FONT_WEIGHT_NORMAL);
401 cairo_set_font_size(cr, 10.0);
403 cairo_text_extents(cr, str_etime, &te_etime);
404 cairo_text_extents(cr, str_btime, &te_btime);
405 cairo_text_extents(cr, strmax, &te_max);
406 cairo_text_extents(cr, strmin, &te_min);
408 g_yoff = GRAPH_V_PADDING;
410 g_height = height - GRAPH_V_PADDING;
411 if (te_etime.height > te_btime.height)
412 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
414 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
416 if (te_min.width > te_max.width)
417 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
419 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
421 g_width = width - g_xoff - GRAPH_H_PADDING;
423 draw_graph_background(cr,
424 g_xoff, g_yoff, g_width, g_height,
425 width, height, config,
429 /** Set the color for text drawing */
430 style_ctx = gtk_widget_get_style_context(window);
431 gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
432 cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
434 /* draw graph begin time */
435 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
436 cairo_show_text(cr, str_btime);
439 /* draw graph end time */
441 width - te_etime.width - GRAPH_H_PADDING,
442 height - GRAPH_V_PADDING);
443 cairo_show_text(cr, str_etime);
446 /* draw min and max temp */
447 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
448 cairo_show_text(cr, strmax);
452 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
453 cairo_show_text(cr, strmin);
456 draw_background_lines(cr, fgcolor,
461 /* .. and finaly draws the temperature graphs */
462 bt = get_graph_begin_time_s(config);
463 et = get_graph_end_time_s();
466 sensor_cur = enabled_sensors;
468 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
469 cairo_set_line_width(cr, 1);
471 while (*sensor_cur) {
472 struct psensor *s = *sensor_cur;
475 if (s->type & SENSOR_TYPE_RPM) {
478 } else if (s->type & SENSOR_TYPE_PERCENT) {
480 max = get_max_value(enabled_sensors,
481 SENSOR_TYPE_PERCENT);
487 if (is_smooth_curves_enabled)
488 draw_sensor_smooth_curve(s, cr,
494 draw_sensor_curve(s, cr,
504 display_no_graphs_warning(cr,
509 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
512 if (config->alpha_channel_enabled)
513 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
515 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
516 cairo_paint(cr_pixmap);
519 free(enabled_sensors);
521 cairo_destroy(cr_pixmap);
522 cairo_surface_destroy(cst);