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;
62 /* Return the end time of the graph i.e. the more recent measure. If
63 * no measure are available, return 0.
64 * If Bezier curves are used return the measure n-3 to avoid to
65 * display a part of the curve outside the graph area.
67 static time_t get_graph_end_time_s(struct psensor **sensors)
71 struct measure *measures;
77 measures = s->measures;
79 if (is_smooth_curves_enabled)
84 for (i = s->values_max_length - 1; i >= 0; i--) {
85 if (measures[i].value != UNKNOWN_DBL_VALUE) {
87 t = measures[i].time.tv_sec;
106 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
111 return etime - cfg->graph_monitoring_duration * 60;
115 compute_y(double value, double min, double max, int height, int off)
117 double t = value - min;
119 return height - ((double)height * (t / (max - min))) + off;
122 static char *time_to_str(time_t s)
125 /* note: localtime returns a static field, no free required */
126 struct tm *tm = localtime(&s);
132 strftime(str, 6, "%H:%M", tm);
137 static void draw_left_region(cairo_t *cr, struct graph_info *info)
139 cairo_set_source_rgb(cr,
140 info->theme_bg_color.red,
141 info->theme_bg_color.green,
142 info->theme_bg_color.blue);
144 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
148 static void draw_right_region(cairo_t *cr, struct graph_info *info)
150 cairo_set_source_rgb(cr,
151 info->theme_bg_color.red,
152 info->theme_bg_color.green,
153 info->theme_bg_color.blue);
157 info->g_xoff + info->g_width,
159 info->g_xoff + info->g_width + GRAPH_H_PADDING,
165 draw_graph_background(cairo_t *cr,
166 struct config *config,
167 struct graph_info *info)
169 struct color *bgcolor;
171 bgcolor = config->graph_bgcolor;
173 if (config->alpha_channel_enabled)
174 cairo_set_source_rgba(cr,
175 info->theme_bg_color.red,
176 info->theme_bg_color.green,
177 info->theme_bg_color.blue,
178 config->graph_bg_alpha);
180 cairo_set_source_rgb(cr,
181 info->theme_bg_color.red,
182 info->theme_bg_color.green,
183 info->theme_bg_color.blue);
185 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
188 if (config->alpha_channel_enabled)
189 cairo_set_source_rgba(cr,
193 config->graph_bg_alpha);
195 cairo_set_source_rgb(cr,
208 /* setup dash style */
209 static double dashes[] = {
213 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
215 static void draw_background_lines(cairo_t *cr,
217 struct config *config,
218 struct graph_info *info)
224 color = config->graph_fgcolor;
226 /* draw background lines */
227 cairo_set_line_width(cr, 1);
228 cairo_set_dash(cr, dashes, ndash, 0);
229 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
231 /* vertical lines representing time steps */
232 for (i = 0; i <= 5; i++) {
233 x = i * ((double)info->g_width / 5) + info->g_xoff;
234 cairo_move_to(cr, x, info->g_yoff);
235 cairo_line_to(cr, x, info->g_yoff + info->g_height);
238 /* horizontal lines draws a line for each 10C step */
239 for (i = min; i < max; i++) {
247 cairo_move_to(cr, info->g_xoff, y);
248 cairo_line_to(cr, info->g_xoff + info->g_width, y);
254 /* back to normal line style */
255 cairo_set_dash(cr, 0, 0, 0);
258 /* Keys: sensor identifier.
260 * Values: array of time_t. Each time_t is corresponding to a sensor
261 * measure which has been used as the start point of a Bezier curve.
263 static GHashTable *times;
265 static void draw_sensor_smooth_curve(struct psensor *s,
271 struct graph_info *info)
273 int i, dt, vdt, j, k, found;
274 double x[4], y[4], v;
275 time_t t, t0, *stimes;
278 times = g_hash_table_new_full(g_str_hash,
283 stimes = g_hash_table_lookup(times, s->id);
285 cairo_set_source_rgb(cr,
290 /* search the index of the first measure used as a start point
291 * of a Bezier curve. The start and end points of the Bezier
292 * curves must be preserved to ensure the same overall shape
296 while (i < s->values_max_length) {
297 t = s->measures[i].time.tv_sec;
298 v = s->measures[i].value;
301 if (v != UNKNOWN_DBL_VALUE && t) {
304 if (t == stimes[k]) {
319 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
320 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
321 g_hash_table_insert(times, strdup(s->id), stimes);
323 if (i == s->values_max_length)
328 while (i < s->values_max_length) {
331 while (i < s->values_max_length && j < 4) {
332 t = s->measures[i].time.tv_sec;
333 v = s->measures[i].value;
335 if (v == UNKNOWN_DBL_VALUE || !t) {
342 x[0 + j] = ((double)vdt * info->g_width)
344 y[0 + j] = compute_y(v,
358 cairo_move_to(cr, x[0], y[0]);
359 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
368 static void draw_sensor_curve(struct psensor *s,
374 struct graph_info *info)
376 int first, i, t, dt, vdt;
379 cairo_set_source_rgb(cr,
386 for (i = 0; i < s->values_max_length; i++) {
387 t = s->measures[i].time.tv_sec;
388 v = s->measures[i].value;
390 if (v == UNKNOWN_DBL_VALUE || !t)
395 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
397 y = compute_y(v, min, max, info->g_height, info->g_yoff);
400 cairo_move_to(cr, x, y);
403 cairo_line_to(cr, x, y);
410 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
414 msg = strdup(_("No graphs enabled"));
416 cairo_select_font_face(cr,
418 CAIRO_FONT_SLANT_NORMAL,
419 CAIRO_FONT_WEIGHT_NORMAL);
420 cairo_set_font_size(cr, 18.0);
422 cairo_move_to(cr, x, y);
423 cairo_show_text(cr, msg);
429 graph_update(struct psensor **sensors,
431 struct config *config,
434 int et, bt, width, height, g_width, g_height;
435 double min_rpm, max_rpm, mint, maxt, min, max;
436 char *strmin, *strmax;
437 /* horizontal and vertical offset of the graph */
438 int g_xoff, g_yoff, no_graphs;
439 cairo_surface_t *cst;
440 cairo_t *cr, *cr_pixmap;
441 char *str_btime, *str_etime;
442 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
443 struct psensor **sensor_cur, **enabled_sensors;
444 GtkAllocation galloc;
445 GtkStyleContext *style_ctx;
446 struct graph_info info;
448 if (!gtk_widget_is_drawable(w_graph))
451 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
453 min_rpm = get_min_rpm(enabled_sensors);
454 max_rpm = get_max_rpm(enabled_sensors);
456 mint = get_min_temp(enabled_sensors);
457 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
459 config->temperature_unit == CELSIUS);
461 maxt = get_max_temp(enabled_sensors);
462 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
464 config->temperature_unit == CELSIUS);
466 et = get_graph_end_time_s(enabled_sensors);
467 bt = get_graph_begin_time_s(config, et);
469 str_btime = time_to_str(bt);
470 str_etime = time_to_str(et);
472 gtk_widget_get_allocation(w_graph, &galloc);
473 width = galloc.width;
474 info.width = galloc.width;
475 height = galloc.height;
476 info.height = height;
479 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
480 cr = cairo_create(cst);
482 cairo_select_font_face(cr,
484 CAIRO_FONT_SLANT_NORMAL,
485 CAIRO_FONT_WEIGHT_NORMAL);
486 cairo_set_font_size(cr, 10.0);
488 cairo_text_extents(cr, str_etime, &te_etime);
489 cairo_text_extents(cr, str_btime, &te_btime);
490 cairo_text_extents(cr, strmax, &te_max);
491 cairo_text_extents(cr, strmin, &te_min);
493 g_yoff = GRAPH_V_PADDING;
494 info.g_yoff = g_yoff;
496 g_height = height - GRAPH_V_PADDING;
497 if (te_etime.height > te_btime.height)
498 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
500 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
502 info.g_height = g_height;
504 if (te_min.width > te_max.width)
505 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
507 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
509 info.g_xoff = g_xoff;
511 style_ctx = gtk_widget_get_style_context(window);
512 gtk_style_context_get_background_color(style_ctx,
513 GTK_STATE_FLAG_NORMAL,
514 &info.theme_bg_color);
515 gtk_style_context_get_color(style_ctx,
516 GTK_STATE_FLAG_NORMAL,
517 &info.theme_fg_color);
519 g_width = width - g_xoff - GRAPH_H_PADDING;
520 info.g_width = g_width;
522 draw_graph_background(cr, config, &info);
524 /* Set the color for text drawing */
525 cairo_set_source_rgb(cr,
526 info.theme_fg_color.red,
527 info.theme_fg_color.green,
528 info.theme_fg_color.blue);
530 /* draw graph begin time */
531 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
532 cairo_show_text(cr, str_btime);
535 /* draw graph end time */
537 width - te_etime.width - GRAPH_H_PADDING,
538 height - GRAPH_V_PADDING);
539 cairo_show_text(cr, str_etime);
542 draw_background_lines(cr, mint, maxt, config, &info);
544 /* .. and finaly draws the temperature graphs */
546 sensor_cur = enabled_sensors;
548 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
549 cairo_set_line_width(cr, 1);
551 while (*sensor_cur) {
552 struct psensor *s = *sensor_cur;
555 if (s->type & SENSOR_TYPE_RPM) {
558 } else if (s->type & SENSOR_TYPE_PERCENT) {
560 max = get_max_value(enabled_sensors,
561 SENSOR_TYPE_PERCENT);
567 if (is_smooth_curves_enabled)
568 draw_sensor_smooth_curve(s, cr,
573 draw_sensor_curve(s, cr,
582 display_no_graphs_warning(cr,
587 draw_left_region(cr, &info);
588 draw_right_region(cr, &info);
590 /* draw min and max temp */
591 cairo_set_source_rgb(cr,
592 info.theme_fg_color.red,
593 info.theme_fg_color.green,
594 info.theme_fg_color.blue);
596 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
597 cairo_show_text(cr, strmax);
601 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
602 cairo_show_text(cr, strmin);
605 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
608 if (config->alpha_channel_enabled)
609 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
611 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
612 cairo_paint(cr_pixmap);
615 free(enabled_sensors);
617 cairo_destroy(cr_pixmap);
618 cairo_surface_destroy(cst);
622 int compute_values_max_length(struct config *c)
624 int n, duration, interval;
626 duration = c->graph_monitoring_duration * 60;
627 interval = c->sensor_update_interval;
629 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;