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;
59 compute_y(double value, double min, double max, int height, int off)
61 double t = value - min;
62 return height - ((double)height * (t / (max - min))) + off;
65 static char *time_to_str(time_t s)
68 /* note: localtime returns a static field, no free required */
69 struct tm *tm = localtime(&s);
75 strftime(str, 6, "%H:%M", tm);
81 draw_graph_background(cairo_t *cr,
82 int g_xoff, int g_yoff,
83 int g_width, int g_height,
84 int width, int height, struct config *config,
88 GtkStyleContext *style_ctx;
89 struct color *bgcolor;
92 bgcolor = config->graph_bgcolor;
94 style_ctx = gtk_widget_get_style_context(window);
95 gtk_style_context_get_background_color(style_ctx,
96 GTK_STATE_FLAG_NORMAL,
99 if (config->alpha_channel_enabled)
100 cairo_set_source_rgba(cr,
104 config->graph_bg_alpha);
106 cairo_set_source_rgb(cr,
111 cairo_rectangle(cr, 0, 0, width, height);
113 if (config->alpha_channel_enabled)
114 cairo_set_source_rgba(cr,
118 config->graph_bg_alpha);
120 cairo_set_source_rgb(cr,
125 cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
129 /* setup dash style */
130 static double dashes[] = {
134 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
136 static void draw_background_lines(cairo_t *cr,
138 int g_width, int g_height,
139 int g_xoff, int g_yoff,
144 /* draw background lines */
145 cairo_set_line_width(cr, 1);
146 cairo_set_dash(cr, dashes, ndash, 0);
147 cairo_set_source_rgb(cr,
148 color->red, color->green, color->blue);
150 /* vertical lines representing time steps */
151 for (i = 0; i <= 5; i++) {
152 int x = i * (g_width / 5) + g_xoff;
153 cairo_move_to(cr, x, g_yoff);
154 cairo_line_to(cr, x, g_yoff + g_height);
158 /* horizontal lines draws a line for each 10C step */
159 for (i = min; i < max; i++) {
161 int y = compute_y(i, min, max, g_height, g_yoff);
163 cairo_move_to(cr, g_xoff, y);
164 cairo_line_to(cr, g_xoff + g_width, y);
169 /* back to normal line style */
170 cairo_set_dash(cr, 0, 0, 0);
173 /* contains the time_t of the measures which have been draw ie not the
174 * bezier control points for each sensor. */
175 static GHashTable *times;
177 static void draw_sensor_smooth_curve(struct psensor *s,
188 int i, dt, vdt, j, k, found;
189 double x[4], y[4], v;
190 time_t t, t0, *stimes;
193 times = g_hash_table_new_full(g_str_hash,
198 stimes = g_hash_table_lookup(times, s->id);
200 cairo_set_source_rgb(cr,
207 while (i < s->values_max_length) {
208 t = s->measures[i].time.tv_sec;
209 v = s->measures[i].value;
212 if (v != UNKNOWN_DBL_VALUE && t) {
215 if (t == stimes[k]) {
230 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
231 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
232 g_hash_table_insert(times, strdup(s->id), stimes);
234 if (i == s->values_max_length)
239 while (i < s->values_max_length) {
242 while (i < s->values_max_length && j < 4) {
243 t = s->measures[i].time.tv_sec;
244 v = s->measures[i].value;
246 if (v == UNKNOWN_DBL_VALUE || !t) {
253 x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
254 y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
265 cairo_move_to(cr, x[0], y[0]);
266 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
275 static void draw_sensor_curve(struct psensor *s,
286 int first, i, t, dt, vdt;
289 cairo_set_source_rgb(cr,
296 for (i = 0; i < s->values_max_length; i++) {
297 t = s->measures[i].time.tv_sec;
298 v = s->measures[i].value;
300 if (v == UNKNOWN_DBL_VALUE || !t)
307 x = ((double)vdt * g_width) / dt + g_xoff;
309 y = compute_y(v, min, max, g_height, g_yoff);
312 cairo_move_to(cr, x, y);
315 cairo_line_to(cr, x, y);
322 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
326 msg = strdup(_("No graphs enabled"));
328 cairo_select_font_face(cr,
330 CAIRO_FONT_SLANT_NORMAL,
331 CAIRO_FONT_WEIGHT_NORMAL);
332 cairo_set_font_size(cr, 18.0);
334 cairo_move_to(cr, x, y);
335 cairo_show_text(cr, msg);
341 graph_update(struct psensor **sensors,
343 struct config *config,
346 struct color *fgcolor = config->graph_fgcolor;
347 int et, bt, width, height, g_width, g_height;
348 double min_rpm, max_rpm, mint, maxt;
349 char *strmin, *strmax;
350 /* horizontal and vertical offset of the graph */
351 int g_xoff, g_yoff, no_graphs, min, max;
352 cairo_surface_t *cst;
353 cairo_t *cr, *cr_pixmap;
354 char *str_btime, *str_etime;
355 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
356 struct psensor **sensor_cur, **enabled_sensors;
357 GtkAllocation galloc;
358 GtkStyleContext *style_ctx;
361 if (!gtk_widget_is_drawable(w_graph))
364 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
366 min_rpm = get_min_rpm(enabled_sensors);
367 max_rpm = get_max_rpm(enabled_sensors);
369 mint = get_min_temp(enabled_sensors);
370 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
372 config->temperature_unit == CELSIUS);
374 maxt = get_max_temp(enabled_sensors);
375 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
377 config->temperature_unit == CELSIUS);
379 str_btime = time_to_str(get_graph_begin_time_s(config));
380 str_etime = time_to_str(get_graph_end_time_s());
382 gtk_widget_get_allocation(w_graph, &galloc);
383 width = galloc.width;
384 height = galloc.height;
386 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
387 cr = cairo_create(cst);
389 cairo_select_font_face(cr,
391 CAIRO_FONT_SLANT_NORMAL,
392 CAIRO_FONT_WEIGHT_NORMAL);
393 cairo_set_font_size(cr, 10.0);
395 cairo_text_extents(cr, str_etime, &te_etime);
396 cairo_text_extents(cr, str_btime, &te_btime);
397 cairo_text_extents(cr, strmax, &te_max);
398 cairo_text_extents(cr, strmin, &te_min);
400 g_yoff = GRAPH_V_PADDING;
402 g_height = height - GRAPH_V_PADDING;
403 if (te_etime.height > te_btime.height)
404 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
406 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
408 if (te_min.width > te_max.width)
409 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
411 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
413 g_width = width - g_xoff - GRAPH_H_PADDING;
415 draw_graph_background(cr,
416 g_xoff, g_yoff, g_width, g_height,
417 width, height, config,
421 /** Set the color for text drawing */
422 style_ctx = gtk_widget_get_style_context(window);
423 gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
424 cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
426 /* draw graph begin time */
427 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
428 cairo_show_text(cr, str_btime);
431 /* draw graph end time */
433 width - te_etime.width - GRAPH_H_PADDING,
434 height - GRAPH_V_PADDING);
435 cairo_show_text(cr, str_etime);
438 /* draw min and max temp */
439 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
440 cairo_show_text(cr, strmax);
444 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
445 cairo_show_text(cr, strmin);
448 draw_background_lines(cr, fgcolor,
453 /* .. and finaly draws the temperature graphs */
454 bt = get_graph_begin_time_s(config);
455 et = get_graph_end_time_s();
458 sensor_cur = enabled_sensors;
460 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
461 cairo_set_line_width(cr, 1);
463 while (*sensor_cur) {
464 struct psensor *s = *sensor_cur;
467 if (s->type & SENSOR_TYPE_RPM) {
470 } else if (s->type & SENSOR_TYPE_PERCENT) {
472 max = get_max_value(enabled_sensors,
473 SENSOR_TYPE_PERCENT);
480 draw_sensor_smooth_curve(s, cr,
486 draw_sensor_curve(s, cr,
496 display_no_graphs_warning(cr,
501 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
504 if (config->alpha_channel_enabled)
505 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
507 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
508 cairo_paint(cr_pixmap);
511 free(enabled_sensors);
513 cairo_destroy(cr_pixmap);
514 cairo_surface_destroy(cst);