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.
65 static time_t get_graph_end_time_s(struct psensor **sensors)
69 struct measure *measures;
75 measures = s->measures;
77 if (is_smooth_curves_enabled)
82 for (i = s->values_max_length - 1; i >= 0; i--) {
83 if (measures[i].value != UNKNOWN_DBL_VALUE) {
85 t = measures[i].time.tv_sec;
102 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
107 return etime - cfg->graph_monitoring_duration * 60;
111 compute_y(double value, double min, double max, int height, int off)
113 double t = value - min;
115 return height - ((double)height * (t / (max - min))) + off;
118 static char *time_to_str(time_t s)
121 /* note: localtime returns a static field, no free required */
122 struct tm *tm = localtime(&s);
128 strftime(str, 6, "%H:%M", tm);
133 static void draw_left_region(cairo_t *cr, struct graph_info *info)
135 cairo_set_source_rgb(cr,
136 info->theme_bg_color.red,
137 info->theme_bg_color.green,
138 info->theme_bg_color.blue);
140 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
144 static void draw_right_region(cairo_t *cr, struct graph_info *info)
146 cairo_set_source_rgb(cr,
147 info->theme_bg_color.red,
148 info->theme_bg_color.green,
149 info->theme_bg_color.blue);
153 info->g_xoff + info->g_width,
155 info->g_xoff + info->g_width + GRAPH_H_PADDING,
161 draw_graph_background(cairo_t *cr,
162 struct config *config,
163 struct graph_info *info)
165 struct color *bgcolor;
167 bgcolor = config->graph_bgcolor;
169 if (config->alpha_channel_enabled)
170 cairo_set_source_rgba(cr,
171 info->theme_bg_color.red,
172 info->theme_bg_color.green,
173 info->theme_bg_color.blue,
174 config->graph_bg_alpha);
176 cairo_set_source_rgb(cr,
177 info->theme_bg_color.red,
178 info->theme_bg_color.green,
179 info->theme_bg_color.blue);
181 cairo_rectangle(cr, info->g_xoff, 0, info->g_width, info->height);
184 if (config->alpha_channel_enabled)
185 cairo_set_source_rgba(cr,
189 config->graph_bg_alpha);
191 cairo_set_source_rgb(cr,
204 /* setup dash style */
205 static double dashes[] = {
209 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
211 static void draw_background_lines(cairo_t *cr,
213 struct config *config,
214 struct graph_info *info)
220 color = config->graph_fgcolor;
222 /* draw background lines */
223 cairo_set_line_width(cr, 1);
224 cairo_set_dash(cr, dashes, ndash, 0);
225 cairo_set_source_rgb(cr, color->red, color->green, color->blue);
227 /* vertical lines representing time steps */
228 for (i = 0; i <= 5; i++) {
229 x = i * ((double)info->g_width / 5) + info->g_xoff;
230 cairo_move_to(cr, x, info->g_yoff);
231 cairo_line_to(cr, x, info->g_yoff + info->g_height);
234 /* horizontal lines draws a line for each 10C step */
235 for (i = min; i < max; i++) {
243 cairo_move_to(cr, info->g_xoff, y);
244 cairo_line_to(cr, info->g_xoff + info->g_width, y);
250 /* back to normal line style */
251 cairo_set_dash(cr, 0, 0, 0);
254 /* Keys: sensor identifier.
256 * Values: array of time_t. Each time_t is corresponding to a sensor
257 * measure which has been used as the start point of a Bezier curve.
259 static GHashTable *times;
261 static void draw_sensor_smooth_curve(struct psensor *s,
267 struct graph_info *info)
269 int i, dt, vdt, j, k, found;
270 double x[4], y[4], v;
271 time_t t, t0, *stimes;
274 times = g_hash_table_new_full(g_str_hash,
279 stimes = g_hash_table_lookup(times, s->id);
281 cairo_set_source_rgb(cr,
286 /* search the index of the first measure used as a start point
287 * of a Bezier curve. The start and end points of the Bezier
288 * curves must be preserved to ensure the same overall shape
292 while (i < s->values_max_length) {
293 t = s->measures[i].time.tv_sec;
294 v = s->measures[i].value;
297 if (v != UNKNOWN_DBL_VALUE && t) {
300 if (t == stimes[k]) {
315 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
316 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
317 g_hash_table_insert(times, strdup(s->id), stimes);
319 if (i == s->values_max_length)
324 while (i < s->values_max_length) {
327 while (i < s->values_max_length && j < 4) {
328 t = s->measures[i].time.tv_sec;
329 v = s->measures[i].value;
331 if (v == UNKNOWN_DBL_VALUE || !t) {
338 x[0 + j] = ((double)vdt * info->g_width)
340 y[0 + j] = compute_y(v,
354 cairo_move_to(cr, x[0], y[0]);
355 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
364 static void draw_sensor_curve(struct psensor *s,
370 struct graph_info *info)
372 int first, i, t, dt, vdt;
375 cairo_set_source_rgb(cr,
382 for (i = 0; i < s->values_max_length; i++) {
383 t = s->measures[i].time.tv_sec;
384 v = s->measures[i].value;
386 if (v == UNKNOWN_DBL_VALUE || !t)
391 x = ((double)vdt * info->g_width) / dt + info->g_xoff;
393 y = compute_y(v, min, max, info->g_height, info->g_yoff);
396 cairo_move_to(cr, x, y);
399 cairo_line_to(cr, x, y);
406 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
410 msg = strdup(_("No graphs enabled"));
412 cairo_select_font_face(cr,
414 CAIRO_FONT_SLANT_NORMAL,
415 CAIRO_FONT_WEIGHT_NORMAL);
416 cairo_set_font_size(cr, 18.0);
418 cairo_move_to(cr, x, y);
419 cairo_show_text(cr, msg);
425 graph_update(struct psensor **sensors,
427 struct config *config,
430 int et, bt, width, height, g_width, g_height;
431 double min_rpm, max_rpm, mint, maxt, min, max;
432 char *strmin, *strmax;
433 /* horizontal and vertical offset of the graph */
434 int g_xoff, g_yoff, no_graphs;
435 cairo_surface_t *cst;
436 cairo_t *cr, *cr_pixmap;
437 char *str_btime, *str_etime;
438 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
439 struct psensor **sensor_cur, **enabled_sensors;
440 GtkAllocation galloc;
441 GtkStyleContext *style_ctx;
442 struct graph_info info;
444 if (!gtk_widget_is_drawable(w_graph))
447 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
449 min_rpm = get_min_rpm(enabled_sensors);
450 max_rpm = get_max_rpm(enabled_sensors);
452 mint = get_min_temp(enabled_sensors);
453 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
455 config->temperature_unit == CELSIUS);
457 maxt = get_max_temp(enabled_sensors);
458 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
460 config->temperature_unit == CELSIUS);
462 et = get_graph_end_time_s(enabled_sensors);
463 bt = get_graph_begin_time_s(config, et);
465 str_btime = time_to_str(bt);
466 str_etime = time_to_str(et);
468 gtk_widget_get_allocation(w_graph, &galloc);
469 width = galloc.width;
470 info.width = galloc.width;
471 height = galloc.height;
472 info.height = height;
475 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
476 cr = cairo_create(cst);
478 cairo_select_font_face(cr,
480 CAIRO_FONT_SLANT_NORMAL,
481 CAIRO_FONT_WEIGHT_NORMAL);
482 cairo_set_font_size(cr, 10.0);
484 cairo_text_extents(cr, str_etime, &te_etime);
485 cairo_text_extents(cr, str_btime, &te_btime);
486 cairo_text_extents(cr, strmax, &te_max);
487 cairo_text_extents(cr, strmin, &te_min);
489 g_yoff = GRAPH_V_PADDING;
490 info.g_yoff = g_yoff;
492 g_height = height - GRAPH_V_PADDING;
493 if (te_etime.height > te_btime.height)
494 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
496 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
498 info.g_height = g_height;
500 if (te_min.width > te_max.width)
501 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
503 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
505 info.g_xoff = g_xoff;
507 style_ctx = gtk_widget_get_style_context(window);
508 gtk_style_context_get_background_color(style_ctx,
509 GTK_STATE_FLAG_NORMAL,
510 &info.theme_bg_color);
511 gtk_style_context_get_color(style_ctx,
512 GTK_STATE_FLAG_NORMAL,
513 &info.theme_fg_color);
515 g_width = width - g_xoff - GRAPH_H_PADDING;
516 info.g_width = g_width;
518 draw_graph_background(cr, config, &info);
520 /* Set the color for text drawing */
521 cairo_set_source_rgb(cr,
522 info.theme_fg_color.red,
523 info.theme_fg_color.green,
524 info.theme_fg_color.blue);
526 /* draw graph begin time */
527 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
528 cairo_show_text(cr, str_btime);
531 /* draw graph end time */
533 width - te_etime.width - GRAPH_H_PADDING,
534 height - GRAPH_V_PADDING);
535 cairo_show_text(cr, str_etime);
538 draw_background_lines(cr, mint, maxt, config, &info);
540 /* .. and finaly draws the temperature graphs */
542 sensor_cur = enabled_sensors;
544 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
545 cairo_set_line_width(cr, 1);
547 while (*sensor_cur) {
548 struct psensor *s = *sensor_cur;
551 if (s->type & SENSOR_TYPE_RPM) {
554 } else if (s->type & SENSOR_TYPE_PERCENT) {
556 max = get_max_value(enabled_sensors,
557 SENSOR_TYPE_PERCENT);
563 if (is_smooth_curves_enabled)
564 draw_sensor_smooth_curve(s, cr,
569 draw_sensor_curve(s, cr,
578 display_no_graphs_warning(cr,
583 draw_left_region(cr, &info);
584 draw_right_region(cr, &info);
586 /* draw min and max temp */
587 cairo_set_source_rgb(cr,
588 info.theme_fg_color.red,
589 info.theme_fg_color.green,
590 info.theme_fg_color.blue);
592 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
593 cairo_show_text(cr, strmax);
597 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
598 cairo_show_text(cr, strmin);
601 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
604 if (config->alpha_channel_enabled)
605 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
607 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
608 cairo_paint(cr_pixmap);
611 free(enabled_sensors);
613 cairo_destroy(cr_pixmap);
614 cairo_surface_destroy(cst);
618 int compute_values_max_length(struct config *c)
620 int n, duration, interval;
622 duration = c->graph_monitoring_duration * 60;
623 interval = c->sensor_update_interval;
625 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;