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 /* Keys: sensor identifier.
175 * Values: array of time_t. Each time_t is corresponding to a sensor
176 * measure which has been used as the start point of a Bezier curve.
178 static GHashTable *times;
180 static void draw_sensor_smooth_curve(struct psensor *s,
191 int i, dt, vdt, j, k, found;
192 double x[4], y[4], v;
193 time_t t, t0, *stimes;
196 times = g_hash_table_new_full(g_str_hash,
201 stimes = g_hash_table_lookup(times, s->id);
203 cairo_set_source_rgb(cr,
208 /* search the index of the first measure used as a start point
209 * of a Bezier curve. The start and end points of the Bezier
210 * curves must be preserved to ensure the same overall shape
214 while (i < s->values_max_length) {
215 t = s->measures[i].time.tv_sec;
216 v = s->measures[i].value;
219 if (v != UNKNOWN_DBL_VALUE && t) {
222 if (t == stimes[k]) {
237 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
238 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
239 g_hash_table_insert(times, strdup(s->id), stimes);
241 if (i == s->values_max_length)
246 while (i < s->values_max_length) {
249 while (i < s->values_max_length && j < 4) {
250 t = s->measures[i].time.tv_sec;
251 v = s->measures[i].value;
253 if (v == UNKNOWN_DBL_VALUE || !t) {
260 x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
261 y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
271 cairo_move_to(cr, x[0], y[0]);
272 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
281 static void draw_sensor_curve(struct psensor *s,
292 int first, i, t, dt, vdt;
295 cairo_set_source_rgb(cr,
302 for (i = 0; i < s->values_max_length; i++) {
303 t = s->measures[i].time.tv_sec;
304 v = s->measures[i].value;
306 if (v == UNKNOWN_DBL_VALUE || !t)
313 x = ((double)vdt * g_width) / dt + g_xoff;
315 y = compute_y(v, min, max, g_height, g_yoff);
318 cairo_move_to(cr, x, y);
321 cairo_line_to(cr, x, y);
328 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
332 msg = strdup(_("No graphs enabled"));
334 cairo_select_font_face(cr,
336 CAIRO_FONT_SLANT_NORMAL,
337 CAIRO_FONT_WEIGHT_NORMAL);
338 cairo_set_font_size(cr, 18.0);
340 cairo_move_to(cr, x, y);
341 cairo_show_text(cr, msg);
347 graph_update(struct psensor **sensors,
349 struct config *config,
352 struct color *fgcolor = config->graph_fgcolor;
353 int et, bt, width, height, g_width, g_height;
354 double min_rpm, max_rpm, mint, maxt;
355 char *strmin, *strmax;
356 /* horizontal and vertical offset of the graph */
357 int g_xoff, g_yoff, no_graphs, min, max;
358 cairo_surface_t *cst;
359 cairo_t *cr, *cr_pixmap;
360 char *str_btime, *str_etime;
361 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
362 struct psensor **sensor_cur, **enabled_sensors;
363 GtkAllocation galloc;
364 GtkStyleContext *style_ctx;
367 if (!gtk_widget_is_drawable(w_graph))
370 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
372 min_rpm = get_min_rpm(enabled_sensors);
373 max_rpm = get_max_rpm(enabled_sensors);
375 mint = get_min_temp(enabled_sensors);
376 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
378 config->temperature_unit == CELSIUS);
380 maxt = get_max_temp(enabled_sensors);
381 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
383 config->temperature_unit == CELSIUS);
385 str_btime = time_to_str(get_graph_begin_time_s(config));
386 str_etime = time_to_str(get_graph_end_time_s());
388 gtk_widget_get_allocation(w_graph, &galloc);
389 width = galloc.width;
390 height = galloc.height;
392 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
393 cr = cairo_create(cst);
395 cairo_select_font_face(cr,
397 CAIRO_FONT_SLANT_NORMAL,
398 CAIRO_FONT_WEIGHT_NORMAL);
399 cairo_set_font_size(cr, 10.0);
401 cairo_text_extents(cr, str_etime, &te_etime);
402 cairo_text_extents(cr, str_btime, &te_btime);
403 cairo_text_extents(cr, strmax, &te_max);
404 cairo_text_extents(cr, strmin, &te_min);
406 g_yoff = GRAPH_V_PADDING;
408 g_height = height - GRAPH_V_PADDING;
409 if (te_etime.height > te_btime.height)
410 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
412 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
414 if (te_min.width > te_max.width)
415 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
417 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
419 g_width = width - g_xoff - GRAPH_H_PADDING;
421 draw_graph_background(cr,
422 g_xoff, g_yoff, g_width, g_height,
423 width, height, config,
427 /** Set the color for text drawing */
428 style_ctx = gtk_widget_get_style_context(window);
429 gtk_style_context_get_color(style_ctx, GTK_STATE_FLAG_NORMAL, &rgba);
430 cairo_set_source_rgb(cr, rgba.red, rgba.green, rgba.blue);
432 /* draw graph begin time */
433 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
434 cairo_show_text(cr, str_btime);
437 /* draw graph end time */
439 width - te_etime.width - GRAPH_H_PADDING,
440 height - GRAPH_V_PADDING);
441 cairo_show_text(cr, str_etime);
444 /* draw min and max temp */
445 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
446 cairo_show_text(cr, strmax);
450 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
451 cairo_show_text(cr, strmin);
454 draw_background_lines(cr, fgcolor,
459 /* .. and finaly draws the temperature graphs */
460 bt = get_graph_begin_time_s(config);
461 et = get_graph_end_time_s();
464 sensor_cur = enabled_sensors;
466 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
467 cairo_set_line_width(cr, 1);
469 while (*sensor_cur) {
470 struct psensor *s = *sensor_cur;
473 if (s->type & SENSOR_TYPE_RPM) {
476 } else if (s->type & SENSOR_TYPE_PERCENT) {
478 max = get_max_value(enabled_sensors,
479 SENSOR_TYPE_PERCENT);
486 draw_sensor_smooth_curve(s, cr,
492 draw_sensor_curve(s, cr,
502 display_no_graphs_warning(cr,
507 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
510 if (config->alpha_channel_enabled)
511 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
513 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
514 cairo_paint(cr_pixmap);
517 free(enabled_sensors);
519 cairo_destroy(cr_pixmap);
520 cairo_surface_destroy(cst);