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>
34 /* horizontal padding */
35 static const int GRAPH_H_PADDING = 4;
36 /* vertical padding */
37 static const int GRAPH_V_PADDING = 4;
39 bool is_smooth_curves_enabled;
42 /* Horizontal position of the central area (curves) */
44 /* Vertical position of the central area (curves) */
47 /* Width of the central area (curves) */
49 /* Height of the central area (curves) */
52 /* Height of the drawing canvas */
54 /* Width of the drawing canvas */
57 /* Background color of the current desktop theme */
58 GdkRGBA theme_bg_color;
59 /* Foreground color of the current desktop theme */
60 GdkRGBA theme_fg_color;
63 static struct psensor **list_filter_graph_enabled(struct psensor **sensors)
66 struct psensor **result, **cur, *s;
71 n = psensor_list_size(sensors);
72 result = malloc((n+1) * sizeof(struct psensor *));
74 for (cur = sensors, i = 0; *cur; cur++) {
77 if (config_is_sensor_graph_enabled(s->id))
86 /* Return the end time of the graph i.e. the more recent measure. If
87 * no measure are available, return 0.
88 * If Bezier curves are used return the measure n-3 to avoid to
89 * display a part of the curve outside the graph area.
91 static time_t get_graph_end_time_s(struct psensor **sensors)
95 struct measure *measures;
101 measures = s->measures;
103 if (is_smooth_curves_enabled)
108 for (i = s->values_max_length - 1; i >= 0; i--) {
109 if (measures[i].value != UNKNOWN_DBL_VALUE) {
111 t = measures[i].time.tv_sec;
130 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
135 return etime - cfg->graph_monitoring_duration * 60;
139 compute_y(double value, double min, double max, int height, int off)
141 double t = value - min;
143 return height - ((double)height * (t / (max - min))) + off;
146 static char *time_to_str(time_t s)
149 /* note: localtime returns a static field, no free required */
150 struct tm *tm = localtime(&s);
156 strftime(str, 6, "%H:%M", tm);
161 static void draw_left_region(cairo_t *cr, struct graph_info *info)
163 cairo_set_source_rgb(cr,
164 info->theme_bg_color.red,
165 info->theme_bg_color.green,
166 info->theme_bg_color.blue);
168 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
172 static void draw_right_region(cairo_t *cr, struct graph_info *info)
174 cairo_set_source_rgb(cr,
175 info->theme_bg_color.red,
176 info->theme_bg_color.green,
177 info->theme_bg_color.blue);
181 info->g_xoff + info->g_width,
183 info->g_xoff + info->g_width + GRAPH_H_PADDING,
189 draw_graph_background(cairo_t *cr,
190 struct config *config,
191 struct graph_info *info)
193 struct color *bgcolor;
195 bgcolor = config->graph_bgcolor;
197 if (config->alpha_channel_enabled)
198 cairo_set_source_rgba(cr,
199 info->theme_bg_color.red,
200 info->theme_bg_color.green,
201 info->theme_bg_color.blue,
202 config->graph_bg_alpha);
204 cairo_set_source_rgb(cr,
205 info->theme_bg_color.red,
206 info->theme_bg_color.green,
207 info->theme_bg_color.blue);
209 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
212 if (config->alpha_channel_enabled)
213 cairo_set_source_rgba(cr,
217 config->graph_bg_alpha);
219 cairo_set_source_rgb(cr,
232 /* setup dash style */
233 static double dashes[] = {
237 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
239 static void draw_background_lines(cairo_t *cr,
241 struct config *config,
242 struct graph_info *info)
248 color = config->graph_fgcolor;
250 /* draw background lines */
251 cairo_set_line_width(cr, 1);
252 cairo_set_dash(cr, dashes, ndash, 0);
253 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
255 /* vertical lines representing time steps */
256 for (i = 0; i <= 5; i++) {
257 x = i * ((double)info->g_width / 5) + info->g_xoff;
258 cairo_move_to(cr, x, info->g_yoff);
259 cairo_line_to(cr, x, info->g_yoff + info->g_height);
262 /* horizontal lines draws a line for each 10C step */
263 for (i = min; i < max; i++) {
271 cairo_move_to(cr, info->g_xoff, y);
272 cairo_line_to(cr, info->g_xoff + info->g_width, y);
278 /* back to normal line style */
279 cairo_set_dash(cr, NULL, 0, 0);
282 /* Keys: sensor identifier.
284 * Values: array of time_t. Each time_t is corresponding to a sensor
285 * measure which has been used as the start point of a Bezier curve.
287 static GHashTable *times;
289 static void draw_sensor_smooth_curve(struct psensor *s,
295 struct graph_info *info)
297 int i, dt, vdt, j, k, found;
298 double x[4], y[4], v;
299 time_t t, t0, *stimes;
303 times = g_hash_table_new_full(g_str_hash,
308 stimes = g_hash_table_lookup(times, s->id);
310 color = config_get_sensor_color(s->id);
312 cairo_set_source_rgb(cr,
316 gdk_rgba_free(color);
318 /* search the index of the first measure used as a start point
319 * of a Bezier curve. The start and end points of the Bezier
320 * curves must be preserved to ensure the same overall shape
324 while (i < s->values_max_length) {
325 t = s->measures[i].time.tv_sec;
326 v = s->measures[i].value;
329 if (v != UNKNOWN_DBL_VALUE && t) {
332 if (t == stimes[k]) {
347 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
348 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
349 g_hash_table_insert(times, strdup(s->id), stimes);
351 if (i == s->values_max_length)
356 while (i < s->values_max_length) {
359 while (i < s->values_max_length && j < 4) {
360 t = s->measures[i].time.tv_sec;
361 v = s->measures[i].value;
363 if (v == UNKNOWN_DBL_VALUE || !t) {
370 x[0 + j] = ((double)vdt * info->g_width)
372 y[0 + j] = compute_y(v,
386 cairo_move_to(cr, x[0], y[0]);
387 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
396 static void draw_sensor_curve(struct psensor *s,
402 struct graph_info *info)
404 int first, i, t, dt, vdt;
408 color = config_get_sensor_color(s->id);
409 cairo_set_source_rgb(cr,
413 gdk_rgba_free(color);
417 for (i = 0; i < s->values_max_length; i++) {
418 t = s->measures[i].time.tv_sec;
419 v = s->measures[i].value;
421 if (v == UNKNOWN_DBL_VALUE || !t)
426 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
428 y = compute_y(v, min, max, info->g_height, info->g_yoff);
431 cairo_move_to(cr, x, y);
434 cairo_line_to(cr, x, y);
441 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
445 msg = strdup(_("No graphs enabled"));
447 cairo_select_font_face(cr,
449 CAIRO_FONT_SLANT_NORMAL,
450 CAIRO_FONT_WEIGHT_NORMAL);
451 cairo_set_font_size(cr, 18.0);
453 cairo_move_to(cr, x, y);
454 cairo_show_text(cr, msg);
460 graph_update(struct psensor **sensors,
462 struct config *config,
465 int et, bt, width, height, g_width, g_height;
466 double min_rpm, max_rpm, mint, maxt, min, max;
467 char *strmin, *strmax;
468 /* horizontal and vertical offset of the graph */
469 int g_xoff, g_yoff, no_graphs;
470 cairo_surface_t *cst;
471 cairo_t *cr, *cr_pixmap;
472 char *str_btime, *str_etime;
473 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
474 struct psensor **sensor_cur, **enabled_sensors;
475 GtkAllocation galloc;
476 GtkStyleContext *style_ctx;
477 struct graph_info info;
479 if (!gtk_widget_is_drawable(w_graph))
482 enabled_sensors = list_filter_graph_enabled(sensors);
484 min_rpm = get_min_rpm(enabled_sensors);
485 max_rpm = get_max_rpm(enabled_sensors);
487 mint = get_min_temp(enabled_sensors);
488 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
490 config->temperature_unit == CELSIUS);
492 maxt = get_max_temp(enabled_sensors);
493 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
495 config->temperature_unit == CELSIUS);
497 et = get_graph_end_time_s(enabled_sensors);
498 bt = get_graph_begin_time_s(config, et);
500 str_btime = time_to_str(bt);
501 str_etime = time_to_str(et);
503 gtk_widget_get_allocation(w_graph, &galloc);
504 width = galloc.width;
505 info.width = galloc.width;
506 height = galloc.height;
507 info.height = height;
510 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
511 cr = cairo_create(cst);
513 cairo_select_font_face(cr,
515 CAIRO_FONT_SLANT_NORMAL,
516 CAIRO_FONT_WEIGHT_NORMAL);
517 cairo_set_font_size(cr, 10.0);
519 cairo_text_extents(cr, str_etime, &te_etime);
520 cairo_text_extents(cr, str_btime, &te_btime);
521 cairo_text_extents(cr, strmax, &te_max);
522 cairo_text_extents(cr, strmin, &te_min);
524 g_yoff = GRAPH_V_PADDING;
525 info.g_yoff = g_yoff;
527 g_height = height - GRAPH_V_PADDING;
528 if (te_etime.height > te_btime.height)
529 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
531 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
533 info.g_height = g_height;
535 if (te_min.width > te_max.width)
536 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
538 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
540 info.g_xoff = g_xoff;
542 style_ctx = gtk_widget_get_style_context(window);
543 gtk_style_context_get_background_color(style_ctx,
544 GTK_STATE_FLAG_NORMAL,
545 &info.theme_bg_color);
546 gtk_style_context_get_color(style_ctx,
547 GTK_STATE_FLAG_NORMAL,
548 &info.theme_fg_color);
550 g_width = width - g_xoff - GRAPH_H_PADDING;
551 info.g_width = g_width;
553 draw_graph_background(cr, config, &info);
555 /* Set the color for text drawing */
556 cairo_set_source_rgb(cr,
557 info.theme_fg_color.red,
558 info.theme_fg_color.green,
559 info.theme_fg_color.blue);
561 /* draw graph begin time */
562 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
563 cairo_show_text(cr, str_btime);
566 /* draw graph end time */
568 width - te_etime.width - GRAPH_H_PADDING,
569 height - GRAPH_V_PADDING);
570 cairo_show_text(cr, str_etime);
573 draw_background_lines(cr, mint, maxt, config, &info);
575 /* .. and finaly draws the temperature graphs */
577 sensor_cur = enabled_sensors;
579 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
580 cairo_set_line_width(cr, 1);
582 while (*sensor_cur) {
583 struct psensor *s = *sensor_cur;
586 if (s->type & SENSOR_TYPE_RPM) {
589 } else if (s->type & SENSOR_TYPE_PERCENT) {
591 max = get_max_value(enabled_sensors,
592 SENSOR_TYPE_PERCENT);
598 if (is_smooth_curves_enabled)
599 draw_sensor_smooth_curve(s, cr,
604 draw_sensor_curve(s, cr,
613 display_no_graphs_warning(cr,
618 draw_left_region(cr, &info);
619 draw_right_region(cr, &info);
621 /* draw min and max temp */
622 cairo_set_source_rgb(cr,
623 info.theme_fg_color.red,
624 info.theme_fg_color.green,
625 info.theme_fg_color.blue);
627 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
628 cairo_show_text(cr, strmax);
632 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
633 cairo_show_text(cr, strmin);
636 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
639 if (config->alpha_channel_enabled)
640 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
642 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
643 cairo_paint(cr_pixmap);
646 free(enabled_sensors);
648 cairo_destroy(cr_pixmap);
649 cairo_surface_destroy(cst);
653 int compute_values_max_length(struct config *c)
655 int n, duration, interval;
657 duration = c->graph_monitoring_duration * 60;
658 interval = c->sensor_update_interval;
660 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;