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 */
56 /* Background color of the current desktop theme */
57 GdkRGBA theme_bg_color;
58 /* Foreground color of the current desktop theme */
59 GdkRGBA theme_fg_color;
63 /* Return the end time of the graph i.e. the more recent measure. If
64 * no measure are available, return 0.
66 static time_t get_graph_end_time_s(struct psensor **sensors)
70 struct measure *measures;
76 measures = s->measures;
78 for (i = s->values_max_length - 1; i >= 0; i--) {
79 if (measures[i].value != UNKNOWN_DBL_VALUE) {
80 t = measures[i].time.tv_sec;
94 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
99 return etime - cfg->graph_monitoring_duration * 60;
103 compute_y(double value, double min, double max, int height, int off)
105 double t = value - min;
107 return height - ((double)height * (t / (max - min))) + off;
110 static char *time_to_str(time_t s)
113 /* note: localtime returns a static field, no free required */
114 struct tm *tm = localtime(&s);
120 strftime(str, 6, "%H:%M", tm);
125 static void draw_left_region(cairo_t *cr, struct graph_info *info)
127 cairo_set_source_rgb(cr,
128 info->theme_bg_color.red,
129 info->theme_bg_color.green,
130 info->theme_bg_color.blue);
132 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
137 draw_graph_background(cairo_t *cr,
138 struct config *config,
139 struct graph_info *info)
141 struct color *bgcolor;
143 bgcolor = config->graph_bgcolor;
145 if (config->alpha_channel_enabled)
146 cairo_set_source_rgba(cr,
147 info->theme_bg_color.red,
148 info->theme_bg_color.green,
149 info->theme_bg_color.blue,
150 config->graph_bg_alpha);
152 cairo_set_source_rgb(cr,
153 info->theme_bg_color.red,
154 info->theme_bg_color.green,
155 info->theme_bg_color.blue);
157 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
160 if (config->alpha_channel_enabled)
161 cairo_set_source_rgba(cr,
165 config->graph_bg_alpha);
167 cairo_set_source_rgb(cr,
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 struct config *config,
190 struct graph_info *info)
196 color = config->graph_fgcolor;
198 /* draw background lines */
199 cairo_set_line_width(cr, 1);
200 cairo_set_dash(cr, dashes, ndash, 0);
201 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
203 /* vertical lines representing time steps */
204 for (i = 0; i <= 5; i++) {
205 x = i * ((double)info->g_width / 5) + info->g_xoff;
206 cairo_move_to(cr, x, info->g_yoff);
207 cairo_line_to(cr, x, info->g_yoff + info->g_height);
210 /* horizontal lines draws a line for each 10C step */
211 for (i = min; i < max; i++) {
219 cairo_move_to(cr, info->g_xoff, y);
220 cairo_line_to(cr, info->g_xoff + info->g_width, y);
226 /* back to normal line style */
227 cairo_set_dash(cr, 0, 0, 0);
230 /* Keys: sensor identifier.
232 * Values: array of time_t. Each time_t is corresponding to a sensor
233 * measure which has been used as the start point of a Bezier curve.
235 static GHashTable *times;
237 static void draw_sensor_smooth_curve(struct psensor *s,
243 struct graph_info *info)
245 int i, dt, vdt, j, k, found;
246 double x[4], y[4], v;
247 time_t t, t0, *stimes;
250 times = g_hash_table_new_full(g_str_hash,
255 stimes = g_hash_table_lookup(times, s->id);
257 cairo_set_source_rgb(cr,
262 /* search the index of the first measure used as a start point
263 * of a Bezier curve. The start and end points of the Bezier
264 * curves must be preserved to ensure the same overall shape
268 while (i < s->values_max_length) {
269 t = s->measures[i].time.tv_sec;
270 v = s->measures[i].value;
273 if (v != UNKNOWN_DBL_VALUE && t) {
276 if (t == stimes[k]) {
291 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
292 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
293 g_hash_table_insert(times, strdup(s->id), stimes);
295 if (i == s->values_max_length)
300 while (i < s->values_max_length) {
303 while (i < s->values_max_length && j < 4) {
304 t = s->measures[i].time.tv_sec;
305 v = s->measures[i].value;
307 if (v == UNKNOWN_DBL_VALUE || !t) {
314 x[0 + j] = ((double)vdt * info->g_width)
316 y[0 + j] = compute_y(v,
330 cairo_move_to(cr, x[0], y[0]);
331 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
340 static void draw_sensor_curve(struct psensor *s,
346 struct graph_info *info)
348 int first, i, t, dt, vdt;
351 cairo_set_source_rgb(cr,
358 for (i = 0; i < s->values_max_length; i++) {
359 t = s->measures[i].time.tv_sec;
360 v = s->measures[i].value;
362 if (v == UNKNOWN_DBL_VALUE || !t)
367 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
369 y = compute_y(v, min, max, info->g_height, info->g_yoff);
372 cairo_move_to(cr, x, y);
375 cairo_line_to(cr, x, y);
382 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
386 msg = strdup(_("No graphs enabled"));
388 cairo_select_font_face(cr,
390 CAIRO_FONT_SLANT_NORMAL,
391 CAIRO_FONT_WEIGHT_NORMAL);
392 cairo_set_font_size(cr, 18.0);
394 cairo_move_to(cr, x, y);
395 cairo_show_text(cr, msg);
401 graph_update(struct psensor **sensors,
403 struct config *config,
406 int et, bt, width, height, g_width, g_height;
407 double min_rpm, max_rpm, mint, maxt, min, max;
408 char *strmin, *strmax;
409 /* horizontal and vertical offset of the graph */
410 int g_xoff, g_yoff, no_graphs;
411 cairo_surface_t *cst;
412 cairo_t *cr, *cr_pixmap;
413 char *str_btime, *str_etime;
414 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
415 struct psensor **sensor_cur, **enabled_sensors;
416 GtkAllocation galloc;
417 GtkStyleContext *style_ctx;
418 struct graph_info info;
420 if (!gtk_widget_is_drawable(w_graph))
423 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
425 min_rpm = get_min_rpm(enabled_sensors);
426 max_rpm = get_max_rpm(enabled_sensors);
428 mint = get_min_temp(enabled_sensors);
429 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
431 config->temperature_unit == CELSIUS);
433 maxt = get_max_temp(enabled_sensors);
434 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
436 config->temperature_unit == CELSIUS);
438 et = get_graph_end_time_s(enabled_sensors);
439 bt = get_graph_begin_time_s(config, et);
441 str_btime = time_to_str(bt);
442 str_etime = time_to_str(et);
444 gtk_widget_get_allocation(w_graph, &galloc);
445 width = galloc.width;
446 info.width = galloc.width;
447 height = galloc.height;
448 info.height = height;
451 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
452 cr = cairo_create(cst);
454 cairo_select_font_face(cr,
456 CAIRO_FONT_SLANT_NORMAL,
457 CAIRO_FONT_WEIGHT_NORMAL);
458 cairo_set_font_size(cr, 10.0);
460 cairo_text_extents(cr, str_etime, &te_etime);
461 cairo_text_extents(cr, str_btime, &te_btime);
462 cairo_text_extents(cr, strmax, &te_max);
463 cairo_text_extents(cr, strmin, &te_min);
465 g_yoff = GRAPH_V_PADDING;
466 info.g_yoff = g_yoff;
468 g_height = height - GRAPH_V_PADDING;
469 if (te_etime.height > te_btime.height)
470 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
472 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
474 info.g_height = g_height;
476 if (te_min.width > te_max.width)
477 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
479 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
481 info.g_xoff = g_xoff;
483 style_ctx = gtk_widget_get_style_context(window);
484 gtk_style_context_get_background_color(style_ctx,
485 GTK_STATE_FLAG_NORMAL,
486 &info.theme_bg_color);
487 gtk_style_context_get_color(style_ctx,
488 GTK_STATE_FLAG_NORMAL,
489 &info.theme_fg_color);
491 g_width = width - g_xoff - GRAPH_H_PADDING;
492 info.g_width = g_width;
494 draw_graph_background(cr, config, &info);
496 /* Set the color for text drawing */
497 cairo_set_source_rgb(cr,
498 info.theme_fg_color.red,
499 info.theme_fg_color.green,
500 info.theme_fg_color.blue);
502 /* draw graph begin time */
503 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
504 cairo_show_text(cr, str_btime);
507 /* draw graph end time */
509 width - te_etime.width - GRAPH_H_PADDING,
510 height - GRAPH_V_PADDING);
511 cairo_show_text(cr, str_etime);
514 draw_background_lines(cr, mint, maxt, config, &info);
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,
545 draw_sensor_curve(s, cr,
554 display_no_graphs_warning(cr,
559 draw_left_region(cr, &info);
561 /* draw min and max temp */
562 cairo_set_source_rgb(cr,
563 info.theme_fg_color.red,
564 info.theme_fg_color.green,
565 info.theme_fg_color.blue);
567 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
568 cairo_show_text(cr, strmax);
572 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
573 cairo_show_text(cr, strmin);
576 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
579 if (config->alpha_channel_enabled)
580 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
582 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
583 cairo_paint(cr_pixmap);
586 free(enabled_sensors);
588 cairo_destroy(cr_pixmap);
589 cairo_surface_destroy(cst);
593 int compute_values_max_length(struct config *c)
595 int n, duration, interval;
597 duration = c->graph_monitoring_duration * 60;
598 interval = c->sensor_update_interval;
600 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;