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 /* Return the end time of the graph i.e. the more recent measure. If
64 * no measure are available, return 0.
65 * If Bezier curves are used return the measure n-3 to avoid to
66 * display a part of the curve outside the graph area.
68 static time_t get_graph_end_time_s(struct psensor **sensors)
72 struct measure *measures;
78 measures = s->measures;
80 if (is_smooth_curves_enabled)
85 for (i = s->values_max_length - 1; i >= 0; i--) {
86 if (measures[i].value != UNKNOWN_DBL_VALUE) {
88 t = measures[i].time.tv_sec;
107 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
112 return etime - cfg->graph_monitoring_duration * 60;
116 compute_y(double value, double min, double max, int height, int off)
118 double t = value - min;
120 return height - ((double)height * (t / (max - min))) + off;
123 static char *time_to_str(time_t s)
126 /* note: localtime returns a static field, no free required */
127 struct tm *tm = localtime(&s);
133 strftime(str, 6, "%H:%M", tm);
138 static void draw_left_region(cairo_t *cr, struct graph_info *info)
140 cairo_set_source_rgb(cr,
141 info->theme_bg_color.red,
142 info->theme_bg_color.green,
143 info->theme_bg_color.blue);
145 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
149 static void draw_right_region(cairo_t *cr, struct graph_info *info)
151 cairo_set_source_rgb(cr,
152 info->theme_bg_color.red,
153 info->theme_bg_color.green,
154 info->theme_bg_color.blue);
158 info->g_xoff + info->g_width,
160 info->g_xoff + info->g_width + GRAPH_H_PADDING,
166 draw_graph_background(cairo_t *cr,
167 struct config *config,
168 struct graph_info *info)
170 struct color *bgcolor;
172 bgcolor = config->graph_bgcolor;
174 if (config->alpha_channel_enabled)
175 cairo_set_source_rgba(cr,
176 info->theme_bg_color.red,
177 info->theme_bg_color.green,
178 info->theme_bg_color.blue,
179 config->graph_bg_alpha);
181 cairo_set_source_rgb(cr,
182 info->theme_bg_color.red,
183 info->theme_bg_color.green,
184 info->theme_bg_color.blue);
186 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
189 if (config->alpha_channel_enabled)
190 cairo_set_source_rgba(cr,
194 config->graph_bg_alpha);
196 cairo_set_source_rgb(cr,
209 /* setup dash style */
210 static double dashes[] = {
214 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
216 static void draw_background_lines(cairo_t *cr,
218 struct config *config,
219 struct graph_info *info)
225 color = config->graph_fgcolor;
227 /* draw background lines */
228 cairo_set_line_width(cr, 1);
229 cairo_set_dash(cr, dashes, ndash, 0);
230 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
232 /* vertical lines representing time steps */
233 for (i = 0; i <= 5; i++) {
234 x = i * ((double)info->g_width / 5) + info->g_xoff;
235 cairo_move_to(cr, x, info->g_yoff);
236 cairo_line_to(cr, x, info->g_yoff + info->g_height);
239 /* horizontal lines draws a line for each 10C step */
240 for (i = min; i < max; i++) {
248 cairo_move_to(cr, info->g_xoff, y);
249 cairo_line_to(cr, info->g_xoff + info->g_width, y);
255 /* back to normal line style */
256 cairo_set_dash(cr, NULL, 0, 0);
259 /* Keys: sensor identifier.
261 * Values: array of time_t. Each time_t is corresponding to a sensor
262 * measure which has been used as the start point of a Bezier curve.
264 static GHashTable *times;
266 static void draw_sensor_smooth_curve(struct psensor *s,
272 struct graph_info *info)
274 int i, dt, vdt, j, k, found;
275 double x[4], y[4], v;
276 time_t t, t0, *stimes;
279 times = g_hash_table_new_full(g_str_hash,
284 stimes = g_hash_table_lookup(times, s->id);
286 cairo_set_source_rgb(cr,
291 /* search the index of the first measure used as a start point
292 * of a Bezier curve. The start and end points of the Bezier
293 * curves must be preserved to ensure the same overall shape
297 while (i < s->values_max_length) {
298 t = s->measures[i].time.tv_sec;
299 v = s->measures[i].value;
302 if (v != UNKNOWN_DBL_VALUE && t) {
305 if (t == stimes[k]) {
320 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
321 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
322 g_hash_table_insert(times, strdup(s->id), stimes);
324 if (i == s->values_max_length)
329 while (i < s->values_max_length) {
332 while (i < s->values_max_length && j < 4) {
333 t = s->measures[i].time.tv_sec;
334 v = s->measures[i].value;
336 if (v == UNKNOWN_DBL_VALUE || !t) {
343 x[0 + j] = ((double)vdt * info->g_width)
345 y[0 + j] = compute_y(v,
359 cairo_move_to(cr, x[0], y[0]);
360 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
369 static void draw_sensor_curve(struct psensor *s,
375 struct graph_info *info)
377 int first, i, t, dt, vdt;
380 cairo_set_source_rgb(cr,
387 for (i = 0; i < s->values_max_length; i++) {
388 t = s->measures[i].time.tv_sec;
389 v = s->measures[i].value;
391 if (v == UNKNOWN_DBL_VALUE || !t)
396 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
398 y = compute_y(v, min, max, info->g_height, info->g_yoff);
401 cairo_move_to(cr, x, y);
404 cairo_line_to(cr, x, y);
411 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
415 msg = strdup(_("No graphs enabled"));
417 cairo_select_font_face(cr,
419 CAIRO_FONT_SLANT_NORMAL,
420 CAIRO_FONT_WEIGHT_NORMAL);
421 cairo_set_font_size(cr, 18.0);
423 cairo_move_to(cr, x, y);
424 cairo_show_text(cr, msg);
430 graph_update(struct psensor **sensors,
432 struct config *config,
435 int et, bt, width, height, g_width, g_height;
436 double min_rpm, max_rpm, mint, maxt, min, max;
437 char *strmin, *strmax;
438 /* horizontal and vertical offset of the graph */
439 int g_xoff, g_yoff, no_graphs;
440 cairo_surface_t *cst;
441 cairo_t *cr, *cr_pixmap;
442 char *str_btime, *str_etime;
443 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
444 struct psensor **sensor_cur, **enabled_sensors;
445 GtkAllocation galloc;
446 GtkStyleContext *style_ctx;
447 struct graph_info info;
449 if (!gtk_widget_is_drawable(w_graph))
452 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
454 min_rpm = get_min_rpm(enabled_sensors);
455 max_rpm = get_max_rpm(enabled_sensors);
457 mint = get_min_temp(enabled_sensors);
458 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
460 config->temperature_unit == CELSIUS);
462 maxt = get_max_temp(enabled_sensors);
463 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
465 config->temperature_unit == CELSIUS);
467 et = get_graph_end_time_s(enabled_sensors);
468 bt = get_graph_begin_time_s(config, et);
470 str_btime = time_to_str(bt);
471 str_etime = time_to_str(et);
473 gtk_widget_get_allocation(w_graph, &galloc);
474 width = galloc.width;
475 info.width = galloc.width;
476 height = galloc.height;
477 info.height = height;
480 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
481 cr = cairo_create(cst);
483 cairo_select_font_face(cr,
485 CAIRO_FONT_SLANT_NORMAL,
486 CAIRO_FONT_WEIGHT_NORMAL);
487 cairo_set_font_size(cr, 10.0);
489 cairo_text_extents(cr, str_etime, &te_etime);
490 cairo_text_extents(cr, str_btime, &te_btime);
491 cairo_text_extents(cr, strmax, &te_max);
492 cairo_text_extents(cr, strmin, &te_min);
494 g_yoff = GRAPH_V_PADDING;
495 info.g_yoff = g_yoff;
497 g_height = height - GRAPH_V_PADDING;
498 if (te_etime.height > te_btime.height)
499 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
501 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
503 info.g_height = g_height;
505 if (te_min.width > te_max.width)
506 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
508 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
510 info.g_xoff = g_xoff;
512 style_ctx = gtk_widget_get_style_context(window);
513 gtk_style_context_get_background_color(style_ctx,
514 GTK_STATE_FLAG_NORMAL,
515 &info.theme_bg_color);
516 gtk_style_context_get_color(style_ctx,
517 GTK_STATE_FLAG_NORMAL,
518 &info.theme_fg_color);
520 g_width = width - g_xoff - GRAPH_H_PADDING;
521 info.g_width = g_width;
523 draw_graph_background(cr, config, &info);
525 /* Set the color for text drawing */
526 cairo_set_source_rgb(cr,
527 info.theme_fg_color.red,
528 info.theme_fg_color.green,
529 info.theme_fg_color.blue);
531 /* draw graph begin time */
532 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
533 cairo_show_text(cr, str_btime);
536 /* draw graph end time */
538 width - te_etime.width - GRAPH_H_PADDING,
539 height - GRAPH_V_PADDING);
540 cairo_show_text(cr, str_etime);
543 draw_background_lines(cr, mint, maxt, config, &info);
545 /* .. and finaly draws the temperature graphs */
547 sensor_cur = enabled_sensors;
549 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
550 cairo_set_line_width(cr, 1);
552 while (*sensor_cur) {
553 struct psensor *s = *sensor_cur;
556 if (s->type & SENSOR_TYPE_RPM) {
559 } else if (s->type & SENSOR_TYPE_PERCENT) {
561 max = get_max_value(enabled_sensors,
562 SENSOR_TYPE_PERCENT);
568 if (is_smooth_curves_enabled)
569 draw_sensor_smooth_curve(s, cr,
574 draw_sensor_curve(s, cr,
583 display_no_graphs_warning(cr,
588 draw_left_region(cr, &info);
589 draw_right_region(cr, &info);
591 /* draw min and max temp */
592 cairo_set_source_rgb(cr,
593 info.theme_fg_color.red,
594 info.theme_fg_color.green,
595 info.theme_fg_color.blue);
597 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
598 cairo_show_text(cr, strmax);
602 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
603 cairo_show_text(cr, strmin);
606 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
609 if (config->alpha_channel_enabled)
610 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
612 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
613 cairo_paint(cr_pixmap);
616 free(enabled_sensors);
618 cairo_destroy(cr_pixmap);
619 cairo_surface_destroy(cst);
623 int compute_values_max_length(struct config *c)
625 int n, duration, interval;
627 duration = c->graph_monitoring_duration * 60;
628 interval = c->sensor_update_interval;
630 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;