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 #define GRAPH_H_PADDING 4
35 /* vertical padding */
36 #define GRAPH_V_PADDING 4
38 bool is_smooth_curves_enabled;
41 /* Horizontal position of the central region (curves) */
43 /* Vertical position of the central region (curves) */
46 /* Height of the drawing canvas */
49 /* Background color of the current desktop theme */
50 GdkRGBA theme_bg_color;
52 /* Foreground color of the current desktop theme */
53 GdkRGBA theme_fg_color;
57 /* Return the end time of the graph i.e. the more recent measure. If
58 * no measure are available, return 0.
60 static time_t get_graph_end_time_s(struct psensor **sensors)
64 struct measure *measures;
70 measures = s->measures;
72 for (i = s->values_max_length - 1; i >= 0; i--) {
73 if (measures[i].value != UNKNOWN_DBL_VALUE) {
74 t = measures[i].time.tv_sec;
88 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
93 return etime - cfg->graph_monitoring_duration * 60;
97 compute_y(double value, double min, double max, int height, int off)
99 double t = value - min;
101 return height - ((double)height * (t / (max - min))) + off;
104 static char *time_to_str(time_t s)
107 /* note: localtime returns a static field, no free required */
108 struct tm *tm = localtime(&s);
114 strftime(str, 6, "%H:%M", tm);
119 static void draw_left_region(cairo_t *cr, struct graph_info *info)
121 cairo_set_source_rgb(cr,
122 info->theme_bg_color.red,
123 info->theme_bg_color.green,
124 info->theme_bg_color.blue);
126 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
131 draw_graph_background(cairo_t *cr,
132 int g_xoff, int g_yoff,
133 int g_width, int g_height,
134 int width, int height, struct config *config,
138 GtkStyleContext *style_ctx;
139 struct color *bgcolor;
142 bgcolor = config->graph_bgcolor;
144 style_ctx = gtk_widget_get_style_context(window);
145 gtk_style_context_get_background_color(style_ctx,
146 GTK_STATE_FLAG_NORMAL,
149 if (config->alpha_channel_enabled)
150 cairo_set_source_rgba(cr,
154 config->graph_bg_alpha);
156 cairo_set_source_rgb(cr,
161 cairo_rectangle(cr, g_xoff, 0, g_width, height);
164 if (config->alpha_channel_enabled)
165 cairo_set_source_rgba(cr,
169 config->graph_bg_alpha);
171 cairo_set_source_rgb(cr,
176 cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
180 /* setup dash style */
181 static double dashes[] = {
185 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
187 static void draw_background_lines(cairo_t *cr,
189 int g_width, int g_height,
190 int g_xoff, int g_yoff,
195 /* draw background lines */
196 cairo_set_line_width(cr, 1);
197 cairo_set_dash(cr, dashes, ndash, 0);
198 cairo_set_source_rgb(cr,
199 color->red, color->green, color->blue);
201 /* vertical lines representing time steps */
202 for (i = 0; i <= 5; i++) {
203 int x = i * (g_width / 5) + g_xoff;
205 cairo_move_to(cr, x, g_yoff);
206 cairo_line_to(cr, x, g_yoff + g_height);
210 /* horizontal lines draws a line for each 10C step */
211 for (i = min; i < max; i++) {
213 int y = compute_y(i, min, max, g_height, g_yoff);
215 cairo_move_to(cr, g_xoff, y);
216 cairo_line_to(cr, g_xoff + g_width, y);
221 /* back to normal line style */
222 cairo_set_dash(cr, 0, 0, 0);
225 /* Keys: sensor identifier.
227 * Values: array of time_t. Each time_t is corresponding to a sensor
228 * measure which has been used as the start point of a Bezier curve.
230 static GHashTable *times;
232 static void draw_sensor_smooth_curve(struct psensor *s,
243 int i, dt, vdt, j, k, found;
244 double x[4], y[4], v;
245 time_t t, t0, *stimes;
248 times = g_hash_table_new_full(g_str_hash,
253 stimes = g_hash_table_lookup(times, s->id);
255 cairo_set_source_rgb(cr,
260 /* search the index of the first measure used as a start point
261 * of a Bezier curve. The start and end points of the Bezier
262 * curves must be preserved to ensure the same overall shape
266 while (i < s->values_max_length) {
267 t = s->measures[i].time.tv_sec;
268 v = s->measures[i].value;
271 if (v != UNKNOWN_DBL_VALUE && t) {
274 if (t == stimes[k]) {
289 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
290 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
291 g_hash_table_insert(times, strdup(s->id), stimes);
293 if (i == s->values_max_length)
298 while (i < s->values_max_length) {
301 while (i < s->values_max_length && j < 4) {
302 t = s->measures[i].time.tv_sec;
303 v = s->measures[i].value;
305 if (v == UNKNOWN_DBL_VALUE || !t) {
312 x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
313 y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
323 cairo_move_to(cr, x[0], y[0]);
324 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
333 static void draw_sensor_curve(struct psensor *s,
344 int first, i, t, dt, vdt;
347 cairo_set_source_rgb(cr,
354 for (i = 0; i < s->values_max_length; i++) {
355 t = s->measures[i].time.tv_sec;
356 v = s->measures[i].value;
358 if (v == UNKNOWN_DBL_VALUE || !t)
363 x = ((double)vdt * g_width) / dt + g_xoff;
365 y = compute_y(v, min, max, g_height, g_yoff);
368 cairo_move_to(cr, x, y);
371 cairo_line_to(cr, x, y);
378 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
382 msg = strdup(_("No graphs enabled"));
384 cairo_select_font_face(cr,
386 CAIRO_FONT_SLANT_NORMAL,
387 CAIRO_FONT_WEIGHT_NORMAL);
388 cairo_set_font_size(cr, 18.0);
390 cairo_move_to(cr, x, y);
391 cairo_show_text(cr, msg);
397 graph_update(struct psensor **sensors,
399 struct config *config,
402 struct color *fgcolor = config->graph_fgcolor;
403 int et, bt, width, height, g_width, g_height;
404 double min_rpm, max_rpm, mint, maxt, min, max;
405 char *strmin, *strmax;
406 /* horizontal and vertical offset of the graph */
407 int g_xoff, g_yoff, no_graphs;
408 cairo_surface_t *cst;
409 cairo_t *cr, *cr_pixmap;
410 char *str_btime, *str_etime;
411 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
412 struct psensor **sensor_cur, **enabled_sensors;
413 GtkAllocation galloc;
414 GtkStyleContext *style_ctx;
415 struct graph_info info;
417 if (!gtk_widget_is_drawable(w_graph))
420 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
422 min_rpm = get_min_rpm(enabled_sensors);
423 max_rpm = get_max_rpm(enabled_sensors);
425 mint = get_min_temp(enabled_sensors);
426 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
428 config->temperature_unit == CELSIUS);
430 maxt = get_max_temp(enabled_sensors);
431 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
433 config->temperature_unit == CELSIUS);
435 et = get_graph_end_time_s(enabled_sensors);
436 bt = get_graph_begin_time_s(config, et);
438 str_btime = time_to_str(bt);
439 str_etime = time_to_str(et);
441 gtk_widget_get_allocation(w_graph, &galloc);
442 width = galloc.width;
443 height = galloc.height;
445 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
446 cr = cairo_create(cst);
448 cairo_select_font_face(cr,
450 CAIRO_FONT_SLANT_NORMAL,
451 CAIRO_FONT_WEIGHT_NORMAL);
452 cairo_set_font_size(cr, 10.0);
454 cairo_text_extents(cr, str_etime, &te_etime);
455 cairo_text_extents(cr, str_btime, &te_btime);
456 cairo_text_extents(cr, strmax, &te_max);
457 cairo_text_extents(cr, strmin, &te_min);
459 g_yoff = GRAPH_V_PADDING;
461 g_height = height - GRAPH_V_PADDING;
462 if (te_etime.height > te_btime.height)
463 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
465 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
467 if (te_min.width > te_max.width)
468 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
470 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
472 info.g_xoff = g_xoff;
473 info.g_yoff = g_yoff;
474 info.height = height;
476 style_ctx = gtk_widget_get_style_context(window);
477 gtk_style_context_get_background_color(style_ctx,
478 GTK_STATE_FLAG_NORMAL,
479 &info.theme_bg_color);
480 gtk_style_context_get_color(style_ctx,
481 GTK_STATE_FLAG_NORMAL,
482 &info.theme_fg_color);
485 g_width = width - g_xoff - GRAPH_H_PADDING;
487 draw_graph_background(cr,
488 g_xoff, g_yoff, g_width, g_height,
489 width, height, config,
493 /* Set the color for text drawing */
494 cairo_set_source_rgb(cr,
495 info.theme_fg_color.red,
496 info.theme_fg_color.green,
497 info.theme_fg_color.blue);
499 /* draw graph begin time */
500 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
501 cairo_show_text(cr, str_btime);
504 /* draw graph end time */
506 width - te_etime.width - GRAPH_H_PADDING,
507 height - GRAPH_V_PADDING);
508 cairo_show_text(cr, str_etime);
511 draw_background_lines(cr, fgcolor,
516 /* .. and finaly draws the temperature graphs */
518 sensor_cur = enabled_sensors;
520 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
521 cairo_set_line_width(cr, 1);
523 while (*sensor_cur) {
524 struct psensor *s = *sensor_cur;
527 if (s->type & SENSOR_TYPE_RPM) {
530 } else if (s->type & SENSOR_TYPE_PERCENT) {
532 max = get_max_value(enabled_sensors,
533 SENSOR_TYPE_PERCENT);
539 if (is_smooth_curves_enabled)
540 draw_sensor_smooth_curve(s, cr,
546 draw_sensor_curve(s, cr,
556 display_no_graphs_warning(cr,
561 draw_left_region(cr, &info);
563 /* draw min and max temp */
564 cairo_set_source_rgb(cr,
565 info.theme_fg_color.red,
566 info.theme_fg_color.green,
567 info.theme_fg_color.blue);
569 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
570 cairo_show_text(cr, strmax);
574 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
575 cairo_show_text(cr, strmin);
578 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
581 if (config->alpha_channel_enabled)
582 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
584 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
585 cairo_paint(cr_pixmap);
588 free(enabled_sensors);
590 cairo_destroy(cr_pixmap);
591 cairo_surface_destroy(cst);
595 int compute_values_max_length(struct config *c)
597 int n, duration, interval;
599 duration = c->graph_monitoring_duration * 60;
600 interval = c->sensor_update_interval;
602 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;