2 * Copyright (C) 2010-2016 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>
35 /* horizontal padding */
36 static const int GRAPH_H_PADDING = 4;
37 /* vertical padding */
38 static const int GRAPH_V_PADDING = 4;
40 bool is_smooth_curves_enabled;
43 /* Horizontal position of the central area (curves) */
45 /* Vertical position of the central area (curves) */
48 /* Width of the central area (curves) */
50 /* Height of the central area (curves) */
53 /* Height of the drawing canvas */
55 /* Width of the drawing canvas */
59 static GtkStyleContext *style;
60 /* Foreground color of the current desktop theme */
61 static GdkRGBA theme_fg_color;
62 /* Background color of the current desktop theme */
63 static GdkRGBA theme_bg_color;
65 static void update_theme(GtkWidget *w)
67 style = gtk_widget_get_style_context(w);
69 gtk_style_context_get_background_color(style,
70 GTK_STATE_FLAG_NORMAL,
72 gtk_style_context_get_color(style,
73 GTK_STATE_FLAG_NORMAL,
77 static struct psensor **list_filter_graph_enabled(struct psensor **sensors)
80 struct psensor **result, **cur, *s;
85 n = psensor_list_size(sensors);
86 result = malloc((n+1) * sizeof(struct psensor *));
88 for (cur = sensors, i = 0; *cur; cur++) {
91 if (config_is_sensor_graph_enabled(s->id))
100 /* Return the end time of the graph i.e. the more recent measure. If
101 * no measure are available, return 0.
102 * If Bezier curves are used return the measure n-3 to avoid to
103 * display a part of the curve outside the graph area.
105 static time_t get_graph_end_time_s(struct psensor **sensors)
109 struct measure *measures;
115 measures = s->measures;
117 if (is_smooth_curves_enabled)
122 for (i = s->values_max_length - 1; i >= 0; i--) {
123 if (measures[i].value != UNKNOWN_DBL_VALUE) {
125 t = measures[i].time.tv_sec;
144 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
149 return etime - cfg->graph_monitoring_duration * 60;
153 compute_y(double value, double min, double max, int height, int off)
155 double t = value - min;
157 return height - ((double)height * (t / (max - min))) + off;
160 static char *time_to_str(time_t s)
163 /* note: localtime returns a static field, no free required */
164 struct tm *tm = localtime(&s);
170 strftime(str, 6, "%H:%M", tm);
175 static void draw_left_region(cairo_t *cr, struct graph_info *info)
177 cairo_set_source_rgb(cr,
179 theme_bg_color.green,
180 theme_bg_color.blue);
182 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
186 static void draw_right_region(cairo_t *cr, struct graph_info *info)
188 cairo_set_source_rgb(cr,
190 theme_bg_color.green,
191 theme_bg_color.blue);
195 info->g_xoff + info->g_width,
197 info->g_xoff + info->g_width + GRAPH_H_PADDING,
203 draw_graph_background(cairo_t *cr,
204 struct config *config,
205 struct graph_info *info)
207 struct color *bgcolor;
209 bgcolor = config->graph_bgcolor;
211 if (config->alpha_channel_enabled)
212 cairo_set_source_rgba(cr,
214 theme_bg_color.green,
216 config->graph_bg_alpha);
218 cairo_set_source_rgb(cr,
220 theme_bg_color.green,
221 theme_bg_color.blue);
223 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
226 if (config->alpha_channel_enabled)
227 cairo_set_source_rgba(cr,
231 config->graph_bg_alpha);
233 cairo_set_source_rgb(cr,
246 /* setup dash style */
247 static double dashes[] = {
251 static int ndash = ARRAY_SIZE(dashes);
253 static void draw_background_lines(cairo_t *cr,
255 struct config *config,
256 struct graph_info *info)
262 color = config->graph_fgcolor;
264 /* draw background lines */
265 cairo_set_line_width(cr, 1);
266 cairo_set_dash(cr, dashes, ndash, 0);
267 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
269 /* vertical lines representing time steps */
270 for (i = 0; i <= 5; i++) {
271 x = i * ((double)info->g_width / 5) + info->g_xoff;
272 cairo_move_to(cr, x, info->g_yoff);
273 cairo_line_to(cr, x, info->g_yoff + info->g_height);
276 /* horizontal lines draws a line for each 10C step */
277 for (i = min; i < max; i++) {
285 cairo_move_to(cr, info->g_xoff, y);
286 cairo_line_to(cr, info->g_xoff + info->g_width, y);
292 /* back to normal line style */
293 cairo_set_dash(cr, NULL, 0, 0);
296 /* Keys: sensor identifier.
298 * Values: array of time_t. Each time_t is corresponding to a sensor
299 * measure which has been used as the start point of a Bezier curve.
301 static GHashTable *times;
303 static void draw_sensor_smooth_curve(struct psensor *s,
309 struct graph_info *info)
311 int i, dt, vdt, j, k, found;
312 double x[4], y[4], v;
313 time_t t, t0, *stimes;
317 times = g_hash_table_new_full(g_str_hash,
322 stimes = g_hash_table_lookup(times, s->id);
324 color = config_get_sensor_color(s->id);
326 cairo_set_source_rgb(cr,
330 gdk_rgba_free(color);
332 /* search the index of the first measure used as a start point
333 * of a Bezier curve. The start and end points of the Bezier
334 * curves must be preserved to ensure the same overall shape
339 while (i < s->values_max_length) {
340 t = s->measures[i].time.tv_sec;
341 v = s->measures[i].value;
344 if (v != UNKNOWN_DBL_VALUE && t) {
347 if (t == stimes[k]) {
362 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
363 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
364 g_hash_table_insert(times, strdup(s->id), stimes);
366 if (i == s->values_max_length)
371 while (i < s->values_max_length) {
374 while (i < s->values_max_length && j < 4) {
375 t = s->measures[i].time.tv_sec;
376 v = s->measures[i].value;
378 if (v == UNKNOWN_DBL_VALUE || !t) {
385 x[0 + j] = ((double)vdt * info->g_width)
387 y[0 + j] = compute_y(v,
401 cairo_move_to(cr, x[0], y[0]);
402 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
411 static void draw_sensor_curve(struct psensor *s,
417 struct graph_info *info)
419 int first, i, t, dt, vdt;
423 color = config_get_sensor_color(s->id);
424 cairo_set_source_rgb(cr,
428 gdk_rgba_free(color);
432 for (i = 0; i < s->values_max_length; i++) {
433 t = s->measures[i].time.tv_sec;
434 v = s->measures[i].value;
436 if (v == UNKNOWN_DBL_VALUE || !t)
441 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
443 y = compute_y(v, min, max, info->g_height, info->g_yoff);
446 cairo_move_to(cr, x, y);
449 cairo_line_to(cr, x, y);
456 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
460 msg = strdup(_("No graphs enabled"));
462 cairo_select_font_face(cr,
464 CAIRO_FONT_SLANT_NORMAL,
465 CAIRO_FONT_WEIGHT_NORMAL);
466 cairo_set_font_size(cr, 18.0);
468 cairo_move_to(cr, x, y);
469 cairo_show_text(cr, msg);
475 graph_update(struct psensor **sensors,
477 struct config *config,
480 int et, bt, width, height, g_width, g_height;
481 double min_rpm, max_rpm, mint, maxt, min, max;
482 char *strmin, *strmax;
483 /* horizontal and vertical offset of the graph */
484 int g_xoff, g_yoff, no_graphs, use_celsius;
485 cairo_surface_t *cst;
486 cairo_t *cr, *cr_pixmap;
487 char *str_btime, *str_etime;
488 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
489 struct psensor **sensor_cur, **enabled_sensors;
490 GtkAllocation galloc;
491 struct graph_info info;
493 if (!gtk_widget_is_drawable(w_graph))
497 update_theme(window);
499 enabled_sensors = list_filter_graph_enabled(sensors);
501 min_rpm = get_min_rpm(enabled_sensors);
502 max_rpm = get_max_rpm(enabled_sensors);
504 if (config_get_temperature_unit() == CELSIUS)
509 mint = get_min_temp(enabled_sensors);
510 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP, mint, use_celsius);
512 maxt = get_max_temp(enabled_sensors);
513 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP, maxt, use_celsius);
515 et = get_graph_end_time_s(enabled_sensors);
516 bt = get_graph_begin_time_s(config, et);
518 str_btime = time_to_str(bt);
519 str_etime = time_to_str(et);
521 gtk_widget_get_allocation(w_graph, &galloc);
522 width = galloc.width;
523 info.width = galloc.width;
524 height = galloc.height;
525 info.height = height;
528 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
529 cr = cairo_create(cst);
531 cairo_select_font_face(cr,
533 CAIRO_FONT_SLANT_NORMAL,
534 CAIRO_FONT_WEIGHT_NORMAL);
535 cairo_set_font_size(cr, 10.0);
537 cairo_text_extents(cr, str_etime, &te_etime);
538 cairo_text_extents(cr, str_btime, &te_btime);
539 cairo_text_extents(cr, strmax, &te_max);
540 cairo_text_extents(cr, strmin, &te_min);
542 g_yoff = GRAPH_V_PADDING;
543 info.g_yoff = g_yoff;
545 g_height = height - GRAPH_V_PADDING;
546 if (te_etime.height > te_btime.height)
547 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
549 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
551 info.g_height = g_height;
553 if (te_min.width > te_max.width)
554 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
556 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
558 info.g_xoff = g_xoff;
560 g_width = width - g_xoff - GRAPH_H_PADDING;
561 info.g_width = g_width;
563 draw_graph_background(cr, config, &info);
565 /* Set the color for text drawing */
566 cairo_set_source_rgb(cr,
568 theme_fg_color.green,
569 theme_fg_color.blue);
571 /* draw graph begin time */
572 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
573 cairo_show_text(cr, str_btime);
576 /* draw graph end time */
578 width - te_etime.width - GRAPH_H_PADDING,
579 height - GRAPH_V_PADDING);
580 cairo_show_text(cr, str_etime);
583 draw_background_lines(cr, mint, maxt, config, &info);
585 /* .. and finaly draws the temperature graphs */
587 sensor_cur = enabled_sensors;
589 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
590 cairo_set_line_width(cr, 1);
592 while (*sensor_cur) {
593 struct psensor *s = *sensor_cur;
596 if (s->type & SENSOR_TYPE_RPM) {
599 } else if (s->type & SENSOR_TYPE_PERCENT) {
601 max = get_max_value(enabled_sensors,
602 SENSOR_TYPE_PERCENT);
608 if (is_smooth_curves_enabled)
609 draw_sensor_smooth_curve(s, cr,
614 draw_sensor_curve(s, cr,
623 display_no_graphs_warning(cr,
628 draw_left_region(cr, &info);
629 draw_right_region(cr, &info);
631 /* draw min and max temp */
632 cairo_set_source_rgb(cr,
634 theme_fg_color.green,
635 theme_fg_color.blue);
637 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
638 cairo_show_text(cr, strmax);
642 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
643 cairo_show_text(cr, strmin);
646 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
649 if (config->alpha_channel_enabled)
650 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
652 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
653 cairo_paint(cr_pixmap);
656 free(enabled_sensors);
658 cairo_destroy(cr_pixmap);
659 cairo_surface_destroy(cst);
663 int compute_values_max_length(struct config *c)
665 int n, duration, interval;
667 duration = c->graph_monitoring_duration * 60;
668 interval = c->sensor_update_interval;
670 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;