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 if (is_smooth_curves_enabled)
83 for (i = s->values_max_length - 1; i >= 0; i--) {
84 if (measures[i].value != UNKNOWN_DBL_VALUE) {
86 t = measures[i].time.tv_sec;
103 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
108 return etime - cfg->graph_monitoring_duration * 60;
112 compute_y(double value, double min, double max, int height, int off)
114 double t = value - min;
116 return height - ((double)height * (t / (max - min))) + off;
119 static char *time_to_str(time_t s)
122 /* note: localtime returns a static field, no free required */
123 struct tm *tm = localtime(&s);
129 strftime(str, 6, "%H:%M", tm);
134 static void draw_left_region(cairo_t *cr, struct graph_info *info)
136 cairo_set_source_rgb(cr,
137 info->theme_bg_color.red,
138 info->theme_bg_color.green,
139 info->theme_bg_color.blue);
141 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
145 static void draw_right_region(cairo_t *cr, struct graph_info *info)
147 cairo_set_source_rgb(cr,
148 info->theme_bg_color.red,
149 info->theme_bg_color.green,
150 info->theme_bg_color.blue);
154 info->g_xoff + info->g_width,
156 info->g_xoff + info->g_width + GRAPH_H_PADDING,
162 draw_graph_background(cairo_t *cr,
163 struct config *config,
164 struct graph_info *info)
166 struct color *bgcolor;
168 bgcolor = config->graph_bgcolor;
170 if (config->alpha_channel_enabled)
171 cairo_set_source_rgba(cr,
172 info->theme_bg_color.red,
173 info->theme_bg_color.green,
174 info->theme_bg_color.blue,
175 config->graph_bg_alpha);
177 cairo_set_source_rgb(cr,
178 info->theme_bg_color.red,
179 info->theme_bg_color.green,
180 info->theme_bg_color.blue);
182 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
185 if (config->alpha_channel_enabled)
186 cairo_set_source_rgba(cr,
190 config->graph_bg_alpha);
192 cairo_set_source_rgb(cr,
205 /* setup dash style */
206 static double dashes[] = {
210 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
212 static void draw_background_lines(cairo_t *cr,
214 struct config *config,
215 struct graph_info *info)
221 color = config->graph_fgcolor;
223 /* draw background lines */
224 cairo_set_line_width(cr, 1);
225 cairo_set_dash(cr, dashes, ndash, 0);
226 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
228 /* vertical lines representing time steps */
229 for (i = 0; i <= 5; i++) {
230 x = i * ((double)info->g_width / 5) + info->g_xoff;
231 cairo_move_to(cr, x, info->g_yoff);
232 cairo_line_to(cr, x, info->g_yoff + info->g_height);
235 /* horizontal lines draws a line for each 10C step */
236 for (i = min; i < max; i++) {
244 cairo_move_to(cr, info->g_xoff, y);
245 cairo_line_to(cr, info->g_xoff + info->g_width, y);
251 /* back to normal line style */
252 cairo_set_dash(cr, 0, 0, 0);
255 /* Keys: sensor identifier.
257 * Values: array of time_t. Each time_t is corresponding to a sensor
258 * measure which has been used as the start point of a Bezier curve.
260 static GHashTable *times;
262 static void draw_sensor_smooth_curve(struct psensor *s,
268 struct graph_info *info)
270 int i, dt, vdt, j, k, found;
271 double x[4], y[4], v;
272 time_t t, t0, *stimes;
275 times = g_hash_table_new_full(g_str_hash,
280 stimes = g_hash_table_lookup(times, s->id);
282 cairo_set_source_rgb(cr,
287 /* search the index of the first measure used as a start point
288 * of a Bezier curve. The start and end points of the Bezier
289 * curves must be preserved to ensure the same overall shape
293 while (i < s->values_max_length) {
294 t = s->measures[i].time.tv_sec;
295 v = s->measures[i].value;
298 if (v != UNKNOWN_DBL_VALUE && t) {
301 if (t == stimes[k]) {
316 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
317 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
318 g_hash_table_insert(times, strdup(s->id), stimes);
320 if (i == s->values_max_length)
325 while (i < s->values_max_length) {
328 while (i < s->values_max_length && j < 4) {
329 t = s->measures[i].time.tv_sec;
330 v = s->measures[i].value;
332 if (v == UNKNOWN_DBL_VALUE || !t) {
339 x[0 + j] = ((double)vdt * info->g_width)
341 y[0 + j] = compute_y(v,
355 cairo_move_to(cr, x[0], y[0]);
356 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
365 static void draw_sensor_curve(struct psensor *s,
371 struct graph_info *info)
373 int first, i, t, dt, vdt;
376 cairo_set_source_rgb(cr,
383 for (i = 0; i < s->values_max_length; i++) {
384 t = s->measures[i].time.tv_sec;
385 v = s->measures[i].value;
387 if (v == UNKNOWN_DBL_VALUE || !t)
392 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
394 y = compute_y(v, min, max, info->g_height, info->g_yoff);
397 cairo_move_to(cr, x, y);
400 cairo_line_to(cr, x, y);
407 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
411 msg = strdup(_("No graphs enabled"));
413 cairo_select_font_face(cr,
415 CAIRO_FONT_SLANT_NORMAL,
416 CAIRO_FONT_WEIGHT_NORMAL);
417 cairo_set_font_size(cr, 18.0);
419 cairo_move_to(cr, x, y);
420 cairo_show_text(cr, msg);
426 graph_update(struct psensor **sensors,
428 struct config *config,
431 int et, bt, width, height, g_width, g_height;
432 double min_rpm, max_rpm, mint, maxt, min, max;
433 char *strmin, *strmax;
434 /* horizontal and vertical offset of the graph */
435 int g_xoff, g_yoff, no_graphs;
436 cairo_surface_t *cst;
437 cairo_t *cr, *cr_pixmap;
438 char *str_btime, *str_etime;
439 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
440 struct psensor **sensor_cur, **enabled_sensors;
441 GtkAllocation galloc;
442 GtkStyleContext *style_ctx;
443 struct graph_info info;
445 if (!gtk_widget_is_drawable(w_graph))
448 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
450 min_rpm = get_min_rpm(enabled_sensors);
451 max_rpm = get_max_rpm(enabled_sensors);
453 mint = get_min_temp(enabled_sensors);
454 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
456 config->temperature_unit == CELSIUS);
458 maxt = get_max_temp(enabled_sensors);
459 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
461 config->temperature_unit == CELSIUS);
463 et = get_graph_end_time_s(enabled_sensors);
464 bt = get_graph_begin_time_s(config, et);
466 str_btime = time_to_str(bt);
467 str_etime = time_to_str(et);
469 gtk_widget_get_allocation(w_graph, &galloc);
470 width = galloc.width;
471 info.width = galloc.width;
472 height = galloc.height;
473 info.height = height;
476 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
477 cr = cairo_create(cst);
479 cairo_select_font_face(cr,
481 CAIRO_FONT_SLANT_NORMAL,
482 CAIRO_FONT_WEIGHT_NORMAL);
483 cairo_set_font_size(cr, 10.0);
485 cairo_text_extents(cr, str_etime, &te_etime);
486 cairo_text_extents(cr, str_btime, &te_btime);
487 cairo_text_extents(cr, strmax, &te_max);
488 cairo_text_extents(cr, strmin, &te_min);
490 g_yoff = GRAPH_V_PADDING;
491 info.g_yoff = g_yoff;
493 g_height = height - GRAPH_V_PADDING;
494 if (te_etime.height > te_btime.height)
495 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
497 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
499 info.g_height = g_height;
501 if (te_min.width > te_max.width)
502 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
504 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
506 info.g_xoff = g_xoff;
508 style_ctx = gtk_widget_get_style_context(window);
509 gtk_style_context_get_background_color(style_ctx,
510 GTK_STATE_FLAG_NORMAL,
511 &info.theme_bg_color);
512 gtk_style_context_get_color(style_ctx,
513 GTK_STATE_FLAG_NORMAL,
514 &info.theme_fg_color);
516 g_width = width - g_xoff - GRAPH_H_PADDING;
517 info.g_width = g_width;
519 draw_graph_background(cr, config, &info);
521 /* Set the color for text drawing */
522 cairo_set_source_rgb(cr,
523 info.theme_fg_color.red,
524 info.theme_fg_color.green,
525 info.theme_fg_color.blue);
527 /* draw graph begin time */
528 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
529 cairo_show_text(cr, str_btime);
532 /* draw graph end time */
534 width - te_etime.width - GRAPH_H_PADDING,
535 height - GRAPH_V_PADDING);
536 cairo_show_text(cr, str_etime);
539 draw_background_lines(cr, mint, maxt, config, &info);
541 /* .. and finaly draws the temperature graphs */
543 sensor_cur = enabled_sensors;
545 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
546 cairo_set_line_width(cr, 1);
548 while (*sensor_cur) {
549 struct psensor *s = *sensor_cur;
552 if (s->type & SENSOR_TYPE_RPM) {
555 } else if (s->type & SENSOR_TYPE_PERCENT) {
557 max = get_max_value(enabled_sensors,
558 SENSOR_TYPE_PERCENT);
564 if (is_smooth_curves_enabled)
565 draw_sensor_smooth_curve(s, cr,
570 draw_sensor_curve(s, cr,
579 display_no_graphs_warning(cr,
584 draw_left_region(cr, &info);
585 draw_right_region(cr, &info);
587 /* draw min and max temp */
588 cairo_set_source_rgb(cr,
589 info.theme_fg_color.red,
590 info.theme_fg_color.green,
591 info.theme_fg_color.blue);
593 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
594 cairo_show_text(cr, strmax);
598 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
599 cairo_show_text(cr, strmin);
602 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
605 if (config->alpha_channel_enabled)
606 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
608 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
609 cairo_paint(cr_pixmap);
612 free(enabled_sensors);
614 cairo_destroy(cr_pixmap);
615 cairo_surface_destroy(cst);
619 int compute_values_max_length(struct config *c)
621 int n, duration, interval;
623 duration = c->graph_monitoring_duration * 60;
624 interval = c->sensor_update_interval;
626 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;