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 */
58 static GtkStyleContext *style;
59 /* Foreground color of the current desktop theme */
60 static GdkRGBA theme_fg_color;
61 /* Background color of the current desktop theme */
62 static GdkRGBA theme_bg_color;
64 static void update_theme(GtkWidget *w)
66 style = gtk_widget_get_style_context(w);
68 gtk_style_context_get_background_color(style,
69 GTK_STATE_FLAG_NORMAL,
71 gtk_style_context_get_color(style,
72 GTK_STATE_FLAG_NORMAL,
76 static struct psensor **list_filter_graph_enabled(struct psensor **sensors)
79 struct psensor **result, **cur, *s;
84 n = psensor_list_size(sensors);
85 result = malloc((n+1) * sizeof(struct psensor *));
87 for (cur = sensors, i = 0; *cur; cur++) {
90 if (config_is_sensor_graph_enabled(s->id))
99 /* Return the end time of the graph i.e. the more recent measure. If
100 * no measure are available, return 0.
101 * If Bezier curves are used return the measure n-3 to avoid to
102 * display a part of the curve outside the graph area.
104 static time_t get_graph_end_time_s(struct psensor **sensors)
108 struct measure *measures;
114 measures = s->measures;
116 if (is_smooth_curves_enabled)
121 for (i = s->values_max_length - 1; i >= 0; i--) {
122 if (measures[i].value != UNKNOWN_DBL_VALUE) {
124 t = measures[i].time.tv_sec;
143 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
148 return etime - cfg->graph_monitoring_duration * 60;
152 compute_y(double value, double min, double max, int height, int off)
154 double t = value - min;
156 return height - ((double)height * (t / (max - min))) + off;
159 static char *time_to_str(time_t s)
162 /* note: localtime returns a static field, no free required */
163 struct tm *tm = localtime(&s);
169 strftime(str, 6, "%H:%M", tm);
174 static void draw_left_region(cairo_t *cr, struct graph_info *info)
176 cairo_set_source_rgb(cr,
178 theme_bg_color.green,
179 theme_bg_color.blue);
181 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
185 static void draw_right_region(cairo_t *cr, struct graph_info *info)
187 cairo_set_source_rgb(cr,
189 theme_bg_color.green,
190 theme_bg_color.blue);
194 info->g_xoff + info->g_width,
196 info->g_xoff + info->g_width + GRAPH_H_PADDING,
202 draw_graph_background(cairo_t *cr,
203 struct config *config,
204 struct graph_info *info)
206 struct color *bgcolor;
208 bgcolor = config->graph_bgcolor;
210 if (config->alpha_channel_enabled)
211 cairo_set_source_rgba(cr,
213 theme_bg_color.green,
215 config->graph_bg_alpha);
217 cairo_set_source_rgb(cr,
219 theme_bg_color.green,
220 theme_bg_color.blue);
222 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
225 if (config->alpha_channel_enabled)
226 cairo_set_source_rgba(cr,
230 config->graph_bg_alpha);
232 cairo_set_source_rgb(cr,
245 /* setup dash style */
246 static double dashes[] = {
250 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
252 static void draw_background_lines(cairo_t *cr,
254 struct config *config,
255 struct graph_info *info)
261 color = config->graph_fgcolor;
263 /* draw background lines */
264 cairo_set_line_width(cr, 1);
265 cairo_set_dash(cr, dashes, ndash, 0);
266 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
268 /* vertical lines representing time steps */
269 for (i = 0; i <= 5; i++) {
270 x = i * ((double)info->g_width / 5) + info->g_xoff;
271 cairo_move_to(cr, x, info->g_yoff);
272 cairo_line_to(cr, x, info->g_yoff + info->g_height);
275 /* horizontal lines draws a line for each 10C step */
276 for (i = min; i < max; i++) {
284 cairo_move_to(cr, info->g_xoff, y);
285 cairo_line_to(cr, info->g_xoff + info->g_width, y);
291 /* back to normal line style */
292 cairo_set_dash(cr, NULL, 0, 0);
295 /* Keys: sensor identifier.
297 * Values: array of time_t. Each time_t is corresponding to a sensor
298 * measure which has been used as the start point of a Bezier curve.
300 static GHashTable *times;
302 static void draw_sensor_smooth_curve(struct psensor *s,
308 struct graph_info *info)
310 int i, dt, vdt, j, k, found;
311 double x[4], y[4], v;
312 time_t t, t0, *stimes;
316 times = g_hash_table_new_full(g_str_hash,
321 stimes = g_hash_table_lookup(times, s->id);
323 color = config_get_sensor_color(s->id);
325 cairo_set_source_rgb(cr,
329 gdk_rgba_free(color);
331 /* search the index of the first measure used as a start point
332 * of a Bezier curve. The start and end points of the Bezier
333 * curves must be preserved to ensure the same overall shape
337 while (i < s->values_max_length) {
338 t = s->measures[i].time.tv_sec;
339 v = s->measures[i].value;
342 if (v != UNKNOWN_DBL_VALUE && t) {
345 if (t == stimes[k]) {
360 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
361 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
362 g_hash_table_insert(times, strdup(s->id), stimes);
364 if (i == s->values_max_length)
369 while (i < s->values_max_length) {
372 while (i < s->values_max_length && j < 4) {
373 t = s->measures[i].time.tv_sec;
374 v = s->measures[i].value;
376 if (v == UNKNOWN_DBL_VALUE || !t) {
383 x[0 + j] = ((double)vdt * info->g_width)
385 y[0 + j] = compute_y(v,
399 cairo_move_to(cr, x[0], y[0]);
400 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
409 static void draw_sensor_curve(struct psensor *s,
415 struct graph_info *info)
417 int first, i, t, dt, vdt;
421 color = config_get_sensor_color(s->id);
422 cairo_set_source_rgb(cr,
426 gdk_rgba_free(color);
430 for (i = 0; i < s->values_max_length; i++) {
431 t = s->measures[i].time.tv_sec;
432 v = s->measures[i].value;
434 if (v == UNKNOWN_DBL_VALUE || !t)
439 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
441 y = compute_y(v, min, max, info->g_height, info->g_yoff);
444 cairo_move_to(cr, x, y);
447 cairo_line_to(cr, x, y);
454 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
458 msg = strdup(_("No graphs enabled"));
460 cairo_select_font_face(cr,
462 CAIRO_FONT_SLANT_NORMAL,
463 CAIRO_FONT_WEIGHT_NORMAL);
464 cairo_set_font_size(cr, 18.0);
466 cairo_move_to(cr, x, y);
467 cairo_show_text(cr, msg);
473 graph_update(struct psensor **sensors,
475 struct config *config,
478 int et, bt, width, height, g_width, g_height;
479 double min_rpm, max_rpm, mint, maxt, min, max;
480 char *strmin, *strmax;
481 /* horizontal and vertical offset of the graph */
482 int g_xoff, g_yoff, no_graphs, use_celsius;
483 cairo_surface_t *cst;
484 cairo_t *cr, *cr_pixmap;
485 char *str_btime, *str_etime;
486 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
487 struct psensor **sensor_cur, **enabled_sensors;
488 GtkAllocation galloc;
489 struct graph_info info;
491 if (!gtk_widget_is_drawable(w_graph))
495 update_theme(window);
497 enabled_sensors = list_filter_graph_enabled(sensors);
499 min_rpm = get_min_rpm(enabled_sensors);
500 max_rpm = get_max_rpm(enabled_sensors);
502 if (config_get_temperature_unit() == CELSIUS)
507 mint = get_min_temp(enabled_sensors);
508 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP, mint, use_celsius);
510 maxt = get_max_temp(enabled_sensors);
511 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP, maxt, use_celsius);
513 et = get_graph_end_time_s(enabled_sensors);
514 bt = get_graph_begin_time_s(config, et);
516 str_btime = time_to_str(bt);
517 str_etime = time_to_str(et);
519 gtk_widget_get_allocation(w_graph, &galloc);
520 width = galloc.width;
521 info.width = galloc.width;
522 height = galloc.height;
523 info.height = height;
526 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
527 cr = cairo_create(cst);
529 cairo_select_font_face(cr,
531 CAIRO_FONT_SLANT_NORMAL,
532 CAIRO_FONT_WEIGHT_NORMAL);
533 cairo_set_font_size(cr, 10.0);
535 cairo_text_extents(cr, str_etime, &te_etime);
536 cairo_text_extents(cr, str_btime, &te_btime);
537 cairo_text_extents(cr, strmax, &te_max);
538 cairo_text_extents(cr, strmin, &te_min);
540 g_yoff = GRAPH_V_PADDING;
541 info.g_yoff = g_yoff;
543 g_height = height - GRAPH_V_PADDING;
544 if (te_etime.height > te_btime.height)
545 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
547 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
549 info.g_height = g_height;
551 if (te_min.width > te_max.width)
552 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
554 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
556 info.g_xoff = g_xoff;
558 g_width = width - g_xoff - GRAPH_H_PADDING;
559 info.g_width = g_width;
561 draw_graph_background(cr, config, &info);
563 /* Set the color for text drawing */
564 cairo_set_source_rgb(cr,
566 theme_fg_color.green,
567 theme_fg_color.blue);
569 /* draw graph begin time */
570 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
571 cairo_show_text(cr, str_btime);
574 /* draw graph end time */
576 width - te_etime.width - GRAPH_H_PADDING,
577 height - GRAPH_V_PADDING);
578 cairo_show_text(cr, str_etime);
581 draw_background_lines(cr, mint, maxt, config, &info);
583 /* .. and finaly draws the temperature graphs */
585 sensor_cur = enabled_sensors;
587 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
588 cairo_set_line_width(cr, 1);
590 while (*sensor_cur) {
591 struct psensor *s = *sensor_cur;
594 if (s->type & SENSOR_TYPE_RPM) {
597 } else if (s->type & SENSOR_TYPE_PERCENT) {
599 max = get_max_value(enabled_sensors,
600 SENSOR_TYPE_PERCENT);
606 if (is_smooth_curves_enabled)
607 draw_sensor_smooth_curve(s, cr,
612 draw_sensor_curve(s, cr,
621 display_no_graphs_warning(cr,
626 draw_left_region(cr, &info);
627 draw_right_region(cr, &info);
629 /* draw min and max temp */
630 cairo_set_source_rgb(cr,
632 theme_fg_color.green,
633 theme_fg_color.blue);
635 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
636 cairo_show_text(cr, strmax);
640 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
641 cairo_show_text(cr, strmin);
644 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
647 if (config->alpha_channel_enabled)
648 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
650 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
651 cairo_paint(cr_pixmap);
654 free(enabled_sensors);
656 cairo_destroy(cr_pixmap);
657 cairo_surface_destroy(cst);
661 int compute_values_max_length(struct config *c)
663 int n, duration, interval;
665 duration = c->graph_monitoring_duration * 60;
666 interval = c->sensor_update_interval;
668 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;