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,
135 struct graph_info *info)
137 struct color *bgcolor;
139 bgcolor = config->graph_bgcolor;
141 if (config->alpha_channel_enabled)
142 cairo_set_source_rgba(cr,
143 info->theme_bg_color.red,
144 info->theme_bg_color.green,
145 info->theme_bg_color.blue,
146 config->graph_bg_alpha);
148 cairo_set_source_rgb(cr,
149 info->theme_bg_color.red,
150 info->theme_bg_color.green,
151 info->theme_bg_color.blue);
153 cairo_rectangle(cr, g_xoff, 0, g_width, height);
156 if (config->alpha_channel_enabled)
157 cairo_set_source_rgba(cr,
161 config->graph_bg_alpha);
163 cairo_set_source_rgb(cr,
168 cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
172 /* setup dash style */
173 static double dashes[] = {
177 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
179 static void draw_background_lines(cairo_t *cr,
181 int g_width, int g_height,
182 int g_xoff, int g_yoff,
187 /* draw background lines */
188 cairo_set_line_width(cr, 1);
189 cairo_set_dash(cr, dashes, ndash, 0);
190 cairo_set_source_rgb(cr,
191 color->red, color->green, color->blue);
193 /* vertical lines representing time steps */
194 for (i = 0; i <= 5; i++) {
195 int x = i * (g_width / 5) + g_xoff;
197 cairo_move_to(cr, x, g_yoff);
198 cairo_line_to(cr, x, g_yoff + g_height);
202 /* horizontal lines draws a line for each 10C step */
203 for (i = min; i < max; i++) {
205 int y = compute_y(i, min, max, g_height, g_yoff);
207 cairo_move_to(cr, g_xoff, y);
208 cairo_line_to(cr, g_xoff + g_width, y);
213 /* back to normal line style */
214 cairo_set_dash(cr, 0, 0, 0);
217 /* Keys: sensor identifier.
219 * Values: array of time_t. Each time_t is corresponding to a sensor
220 * measure which has been used as the start point of a Bezier curve.
222 static GHashTable *times;
224 static void draw_sensor_smooth_curve(struct psensor *s,
235 int i, dt, vdt, j, k, found;
236 double x[4], y[4], v;
237 time_t t, t0, *stimes;
240 times = g_hash_table_new_full(g_str_hash,
245 stimes = g_hash_table_lookup(times, s->id);
247 cairo_set_source_rgb(cr,
252 /* search the index of the first measure used as a start point
253 * of a Bezier curve. The start and end points of the Bezier
254 * curves must be preserved to ensure the same overall shape
258 while (i < s->values_max_length) {
259 t = s->measures[i].time.tv_sec;
260 v = s->measures[i].value;
263 if (v != UNKNOWN_DBL_VALUE && t) {
266 if (t == stimes[k]) {
281 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
282 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
283 g_hash_table_insert(times, strdup(s->id), stimes);
285 if (i == s->values_max_length)
290 while (i < s->values_max_length) {
293 while (i < s->values_max_length && j < 4) {
294 t = s->measures[i].time.tv_sec;
295 v = s->measures[i].value;
297 if (v == UNKNOWN_DBL_VALUE || !t) {
304 x[0 + j] = ((double)vdt * g_width) / dt + g_xoff;
305 y[0 + j] = compute_y(v, min, max, g_height, g_yoff);
315 cairo_move_to(cr, x[0], y[0]);
316 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
325 static void draw_sensor_curve(struct psensor *s,
336 int first, i, t, dt, vdt;
339 cairo_set_source_rgb(cr,
346 for (i = 0; i < s->values_max_length; i++) {
347 t = s->measures[i].time.tv_sec;
348 v = s->measures[i].value;
350 if (v == UNKNOWN_DBL_VALUE || !t)
355 x = ((double)vdt * g_width) / dt + g_xoff;
357 y = compute_y(v, min, max, g_height, g_yoff);
360 cairo_move_to(cr, x, y);
363 cairo_line_to(cr, x, y);
370 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
374 msg = strdup(_("No graphs enabled"));
376 cairo_select_font_face(cr,
378 CAIRO_FONT_SLANT_NORMAL,
379 CAIRO_FONT_WEIGHT_NORMAL);
380 cairo_set_font_size(cr, 18.0);
382 cairo_move_to(cr, x, y);
383 cairo_show_text(cr, msg);
389 graph_update(struct psensor **sensors,
391 struct config *config,
394 struct color *fgcolor = config->graph_fgcolor;
395 int et, bt, width, height, g_width, g_height;
396 double min_rpm, max_rpm, mint, maxt, min, max;
397 char *strmin, *strmax;
398 /* horizontal and vertical offset of the graph */
399 int g_xoff, g_yoff, no_graphs;
400 cairo_surface_t *cst;
401 cairo_t *cr, *cr_pixmap;
402 char *str_btime, *str_etime;
403 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
404 struct psensor **sensor_cur, **enabled_sensors;
405 GtkAllocation galloc;
406 GtkStyleContext *style_ctx;
407 struct graph_info info;
409 if (!gtk_widget_is_drawable(w_graph))
412 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
414 min_rpm = get_min_rpm(enabled_sensors);
415 max_rpm = get_max_rpm(enabled_sensors);
417 mint = get_min_temp(enabled_sensors);
418 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
420 config->temperature_unit == CELSIUS);
422 maxt = get_max_temp(enabled_sensors);
423 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
425 config->temperature_unit == CELSIUS);
427 et = get_graph_end_time_s(enabled_sensors);
428 bt = get_graph_begin_time_s(config, et);
430 str_btime = time_to_str(bt);
431 str_etime = time_to_str(et);
433 gtk_widget_get_allocation(w_graph, &galloc);
434 width = galloc.width;
435 height = galloc.height;
437 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
438 cr = cairo_create(cst);
440 cairo_select_font_face(cr,
442 CAIRO_FONT_SLANT_NORMAL,
443 CAIRO_FONT_WEIGHT_NORMAL);
444 cairo_set_font_size(cr, 10.0);
446 cairo_text_extents(cr, str_etime, &te_etime);
447 cairo_text_extents(cr, str_btime, &te_btime);
448 cairo_text_extents(cr, strmax, &te_max);
449 cairo_text_extents(cr, strmin, &te_min);
451 g_yoff = GRAPH_V_PADDING;
453 g_height = height - GRAPH_V_PADDING;
454 if (te_etime.height > te_btime.height)
455 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
457 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
459 if (te_min.width > te_max.width)
460 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
462 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
464 info.g_xoff = g_xoff;
465 info.g_yoff = g_yoff;
466 info.height = height;
468 style_ctx = gtk_widget_get_style_context(window);
469 gtk_style_context_get_background_color(style_ctx,
470 GTK_STATE_FLAG_NORMAL,
471 &info.theme_bg_color);
472 gtk_style_context_get_color(style_ctx,
473 GTK_STATE_FLAG_NORMAL,
474 &info.theme_fg_color);
477 g_width = width - g_xoff - GRAPH_H_PADDING;
479 draw_graph_background(cr,
480 g_xoff, g_yoff, g_width, g_height,
481 width, height, config,
484 /* Set the color for text drawing */
485 cairo_set_source_rgb(cr,
486 info.theme_fg_color.red,
487 info.theme_fg_color.green,
488 info.theme_fg_color.blue);
490 /* draw graph begin time */
491 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
492 cairo_show_text(cr, str_btime);
495 /* draw graph end time */
497 width - te_etime.width - GRAPH_H_PADDING,
498 height - GRAPH_V_PADDING);
499 cairo_show_text(cr, str_etime);
502 draw_background_lines(cr, fgcolor,
507 /* .. and finaly draws the temperature graphs */
509 sensor_cur = enabled_sensors;
511 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
512 cairo_set_line_width(cr, 1);
514 while (*sensor_cur) {
515 struct psensor *s = *sensor_cur;
518 if (s->type & SENSOR_TYPE_RPM) {
521 } else if (s->type & SENSOR_TYPE_PERCENT) {
523 max = get_max_value(enabled_sensors,
524 SENSOR_TYPE_PERCENT);
530 if (is_smooth_curves_enabled)
531 draw_sensor_smooth_curve(s, cr,
537 draw_sensor_curve(s, cr,
547 display_no_graphs_warning(cr,
552 draw_left_region(cr, &info);
554 /* draw min and max temp */
555 cairo_set_source_rgb(cr,
556 info.theme_fg_color.red,
557 info.theme_fg_color.green,
558 info.theme_fg_color.blue);
560 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
561 cairo_show_text(cr, strmax);
565 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
566 cairo_show_text(cr, strmin);
569 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
572 if (config->alpha_channel_enabled)
573 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
575 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
576 cairo_paint(cr_pixmap);
579 free(enabled_sensors);
581 cairo_destroy(cr_pixmap);
582 cairo_surface_destroy(cst);
586 int compute_values_max_length(struct config *c)
588 int n, duration, interval;
590 duration = c->graph_monitoring_duration * 60;
591 interval = c->sensor_update_interval;
593 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;