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>
33 /* horizontal padding */
34 const int GRAPH_H_PADDING = 4;
35 /* vertical padding */
36 const int GRAPH_V_PADDING = 4;
38 bool is_smooth_curves_enabled;
41 /* Horizontal position of the central area (curves) */
43 /* Vertical position of the central area (curves) */
46 /* Width of the central area (curves) */
48 /* Height of the central area (curves) */
51 /* Height of the drawing canvas */
53 /* Width of the drawing canvas */
57 static GtkStyleContext *style;
58 /* Foreground color of the current desktop theme */
59 static GdkRGBA theme_fg_color;
60 /* Background color of the current desktop theme */
61 static GdkRGBA theme_bg_color;
63 static void update_theme(GtkWidget *w)
65 style = gtk_widget_get_style_context(w);
67 gtk_style_context_get_background_color(style,
68 GTK_STATE_FLAG_NORMAL,
70 gtk_style_context_get_color(style,
71 GTK_STATE_FLAG_NORMAL,
75 /* Return the end time of the graph i.e. the more recent measure. If
76 * no measure are available, return 0.
77 * If Bezier curves are used return the measure n-3 to avoid to
78 * display a part of the curve outside the graph area.
80 static time_t get_graph_end_time_s(struct psensor **sensors)
84 struct measure *measures;
90 measures = s->measures;
92 if (is_smooth_curves_enabled)
97 for (i = s->values_max_length - 1; i >= 0; i--) {
98 if (measures[i].value != UNKNOWN_DBL_VALUE) {
100 t = measures[i].time.tv_sec;
119 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
124 return etime - cfg->graph_monitoring_duration * 60;
128 compute_y(double value, double min, double max, int height, int off)
130 double t = value - min;
132 return height - ((double)height * (t / (max - min))) + off;
135 static char *time_to_str(time_t s)
138 /* note: localtime returns a static field, no free required */
139 struct tm *tm = localtime(&s);
145 strftime(str, 6, "%H:%M", tm);
150 static void draw_left_region(cairo_t *cr, struct graph_info *info)
152 cairo_set_source_rgb(cr,
154 theme_bg_color.green,
155 theme_bg_color.blue);
157 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
161 static void draw_right_region(cairo_t *cr, struct graph_info *info)
163 cairo_set_source_rgb(cr,
165 theme_bg_color.green,
166 theme_bg_color.blue);
170 info->g_xoff + info->g_width,
172 info->g_xoff + info->g_width + GRAPH_H_PADDING,
178 draw_graph_background(cairo_t *cr,
179 struct config *config,
180 struct graph_info *info)
182 struct color *bgcolor;
184 bgcolor = config->graph_bgcolor;
186 if (config->alpha_channel_enabled)
187 cairo_set_source_rgba(cr,
189 theme_bg_color.green,
191 config->graph_bg_alpha);
193 cairo_set_source_rgb(cr,
195 theme_bg_color.green,
196 theme_bg_color.blue);
198 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
201 if (config->alpha_channel_enabled)
202 cairo_set_source_rgba(cr,
206 config->graph_bg_alpha);
208 cairo_set_source_rgb(cr,
221 /* setup dash style */
222 static double dashes[] = {
226 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
228 static void draw_background_lines(cairo_t *cr,
230 struct config *config,
231 struct graph_info *info)
237 color = config->graph_fgcolor;
239 /* draw background lines */
240 cairo_set_line_width(cr, 1);
241 cairo_set_dash(cr, dashes, ndash, 0);
242 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
244 /* vertical lines representing time steps */
245 for (i = 0; i <= 5; i++) {
246 x = i * ((double)info->g_width / 5) + info->g_xoff;
247 cairo_move_to(cr, x, info->g_yoff);
248 cairo_line_to(cr, x, info->g_yoff + info->g_height);
251 /* horizontal lines draws a line for each 10C step */
252 for (i = min; i < max; i++) {
260 cairo_move_to(cr, info->g_xoff, y);
261 cairo_line_to(cr, info->g_xoff + info->g_width, y);
267 /* back to normal line style */
268 cairo_set_dash(cr, 0, 0, 0);
271 /* Keys: sensor identifier.
273 * Values: array of time_t. Each time_t is corresponding to a sensor
274 * measure which has been used as the start point of a Bezier curve.
276 static GHashTable *times;
278 static void draw_sensor_smooth_curve(struct psensor *s,
284 struct graph_info *info)
286 int i, dt, vdt, j, k, found;
287 double x[4], y[4], v;
288 time_t t, t0, *stimes;
291 times = g_hash_table_new_full(g_str_hash,
296 stimes = g_hash_table_lookup(times, s->id);
298 cairo_set_source_rgb(cr,
303 /* search the index of the first measure used as a start point
304 * of a Bezier curve. The start and end points of the Bezier
305 * curves must be preserved to ensure the same overall shape
309 while (i < s->values_max_length) {
310 t = s->measures[i].time.tv_sec;
311 v = s->measures[i].value;
314 if (v != UNKNOWN_DBL_VALUE && t) {
317 if (t == stimes[k]) {
332 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
333 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
334 g_hash_table_insert(times, strdup(s->id), stimes);
336 if (i == s->values_max_length)
341 while (i < s->values_max_length) {
344 while (i < s->values_max_length && j < 4) {
345 t = s->measures[i].time.tv_sec;
346 v = s->measures[i].value;
348 if (v == UNKNOWN_DBL_VALUE || !t) {
355 x[0 + j] = ((double)vdt * info->g_width)
357 y[0 + j] = compute_y(v,
371 cairo_move_to(cr, x[0], y[0]);
372 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
381 static void draw_sensor_curve(struct psensor *s,
387 struct graph_info *info)
389 int first, i, t, dt, vdt;
392 cairo_set_source_rgb(cr,
399 for (i = 0; i < s->values_max_length; i++) {
400 t = s->measures[i].time.tv_sec;
401 v = s->measures[i].value;
403 if (v == UNKNOWN_DBL_VALUE || !t)
408 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
410 y = compute_y(v, min, max, info->g_height, info->g_yoff);
413 cairo_move_to(cr, x, y);
416 cairo_line_to(cr, x, y);
423 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
427 msg = strdup(_("No graphs enabled"));
429 cairo_select_font_face(cr,
431 CAIRO_FONT_SLANT_NORMAL,
432 CAIRO_FONT_WEIGHT_NORMAL);
433 cairo_set_font_size(cr, 18.0);
435 cairo_move_to(cr, x, y);
436 cairo_show_text(cr, msg);
442 graph_update(struct psensor **sensors,
444 struct config *config,
447 int et, bt, width, height, g_width, g_height;
448 double min_rpm, max_rpm, mint, maxt, min, max;
449 char *strmin, *strmax;
450 /* horizontal and vertical offset of the graph */
451 int g_xoff, g_yoff, no_graphs;
452 cairo_surface_t *cst;
453 cairo_t *cr, *cr_pixmap;
454 char *str_btime, *str_etime;
455 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
456 struct psensor **sensor_cur, **enabled_sensors;
457 GtkAllocation galloc;
458 GtkStyleContext *style_ctx;
459 struct graph_info info;
461 if (!gtk_widget_is_drawable(w_graph))
465 update_theme(window);
467 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
469 min_rpm = get_min_rpm(enabled_sensors);
470 max_rpm = get_max_rpm(enabled_sensors);
472 mint = get_min_temp(enabled_sensors);
473 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
475 config->temperature_unit == CELSIUS);
477 maxt = get_max_temp(enabled_sensors);
478 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
480 config->temperature_unit == CELSIUS);
482 et = get_graph_end_time_s(enabled_sensors);
483 bt = get_graph_begin_time_s(config, et);
485 str_btime = time_to_str(bt);
486 str_etime = time_to_str(et);
488 gtk_widget_get_allocation(w_graph, &galloc);
489 width = galloc.width;
490 info.width = galloc.width;
491 height = galloc.height;
492 info.height = height;
495 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
496 cr = cairo_create(cst);
498 cairo_select_font_face(cr,
500 CAIRO_FONT_SLANT_NORMAL,
501 CAIRO_FONT_WEIGHT_NORMAL);
502 cairo_set_font_size(cr, 10.0);
504 cairo_text_extents(cr, str_etime, &te_etime);
505 cairo_text_extents(cr, str_btime, &te_btime);
506 cairo_text_extents(cr, strmax, &te_max);
507 cairo_text_extents(cr, strmin, &te_min);
509 g_yoff = GRAPH_V_PADDING;
510 info.g_yoff = g_yoff;
512 g_height = height - GRAPH_V_PADDING;
513 if (te_etime.height > te_btime.height)
514 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
516 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
518 info.g_height = g_height;
520 if (te_min.width > te_max.width)
521 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
523 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
525 info.g_xoff = g_xoff;
527 g_width = width - g_xoff - GRAPH_H_PADDING;
528 info.g_width = g_width;
530 draw_graph_background(cr, config, &info);
532 /* Set the color for text drawing */
533 cairo_set_source_rgb(cr,
535 theme_fg_color.green,
536 theme_fg_color.blue);
538 /* draw graph begin time */
539 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
540 cairo_show_text(cr, str_btime);
543 /* draw graph end time */
545 width - te_etime.width - GRAPH_H_PADDING,
546 height - GRAPH_V_PADDING);
547 cairo_show_text(cr, str_etime);
550 draw_background_lines(cr, mint, maxt, config, &info);
552 /* .. and finaly draws the temperature graphs */
554 sensor_cur = enabled_sensors;
556 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
557 cairo_set_line_width(cr, 1);
559 while (*sensor_cur) {
560 struct psensor *s = *sensor_cur;
563 if (s->type & SENSOR_TYPE_RPM) {
566 } else if (s->type & SENSOR_TYPE_PERCENT) {
568 max = get_max_value(enabled_sensors,
569 SENSOR_TYPE_PERCENT);
575 if (is_smooth_curves_enabled)
576 draw_sensor_smooth_curve(s, cr,
581 draw_sensor_curve(s, cr,
590 display_no_graphs_warning(cr,
595 draw_left_region(cr, &info);
596 draw_right_region(cr, &info);
598 /* draw min and max temp */
599 cairo_set_source_rgb(cr,
601 theme_fg_color.green,
602 theme_fg_color.blue);
604 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
605 cairo_show_text(cr, strmax);
609 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
610 cairo_show_text(cr, strmin);
613 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
616 if (config->alpha_channel_enabled)
617 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
619 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
620 cairo_paint(cr_pixmap);
623 free(enabled_sensors);
625 cairo_destroy(cr_pixmap);
626 cairo_surface_destroy(cst);
630 int compute_values_max_length(struct config *c)
632 int n, duration, interval;
634 duration = c->graph_monitoring_duration * 60;
635 interval = c->sensor_update_interval;
637 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;