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 region (curves) */
43 /* Vertical position of the central region (curves) */
46 /* Height of the drawing canvas */
48 /* Width of the drawing canvas */
51 /* Background color of the current desktop theme */
52 GdkRGBA theme_bg_color;
53 /* Foreground color of the current desktop theme */
54 GdkRGBA theme_fg_color;
58 /* Return the end time of the graph i.e. the more recent measure. If
59 * no measure are available, return 0.
61 static time_t get_graph_end_time_s(struct psensor **sensors)
65 struct measure *measures;
71 measures = s->measures;
73 for (i = s->values_max_length - 1; i >= 0; i--) {
74 if (measures[i].value != UNKNOWN_DBL_VALUE) {
75 t = measures[i].time.tv_sec;
89 static time_t get_graph_begin_time_s(struct config *cfg, time_t etime)
94 return etime - cfg->graph_monitoring_duration * 60;
98 compute_y(double value, double min, double max, int height, int off)
100 double t = value - min;
102 return height - ((double)height * (t / (max - min))) + off;
105 static char *time_to_str(time_t s)
108 /* note: localtime returns a static field, no free required */
109 struct tm *tm = localtime(&s);
115 strftime(str, 6, "%H:%M", tm);
120 static void draw_left_region(cairo_t *cr, struct graph_info *info)
122 cairo_set_source_rgb(cr,
123 info->theme_bg_color.red,
124 info->theme_bg_color.green,
125 info->theme_bg_color.blue);
127 cairo_rectangle(cr, 0, 0, info->g_xoff, info->height);
132 draw_graph_background(cairo_t *cr,
133 int g_width, int g_height,
134 struct config *config,
135 struct graph_info *info)
137 struct color *bgcolor;
139 bgcolor = config->graph_bgcolor;
141 if (config->alpha_channel_enabled)
142 cairo_set_source_rgba(cr,
143 info->theme_bg_color.red,
144 info->theme_bg_color.green,
145 info->theme_bg_color.blue,
146 config->graph_bg_alpha);
148 cairo_set_source_rgb(cr,
149 info->theme_bg_color.red,
150 info->theme_bg_color.green,
151 info->theme_bg_color.blue);
153 cairo_rectangle(cr, info->g_xoff, 0, g_width, info->height);
156 if (config->alpha_channel_enabled)
157 cairo_set_source_rgba(cr,
161 config->graph_bg_alpha);
163 cairo_set_source_rgb(cr,
168 cairo_rectangle(cr, info->g_xoff, info->g_yoff, g_width, g_height);
172 /* setup dash style */
173 static double dashes[] = {
177 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
179 static void draw_background_lines(cairo_t *cr,
181 int g_width, int g_height,
183 struct graph_info *info)
187 /* draw background lines */
188 cairo_set_line_width(cr, 1);
189 cairo_set_dash(cr, dashes, ndash, 0);
190 cairo_set_source_rgb(cr,
191 color->red, color->green, color->blue);
193 /* vertical lines representing time steps */
194 for (i = 0; i <= 5; i++) {
195 int x = i * (g_width / 5) + info->g_xoff;
197 cairo_move_to(cr, x, info->g_yoff);
198 cairo_line_to(cr, x, info->g_yoff + g_height);
202 /* horizontal lines draws a line for each 10C step */
203 for (i = min; i < max; i++) {
205 int y = compute_y(i, min, max, g_height, info->g_yoff);
207 cairo_move_to(cr, info->g_xoff, y);
208 cairo_line_to(cr, info->g_xoff + g_width, y);
213 /* back to normal line style */
214 cairo_set_dash(cr, 0, 0, 0);
217 /* Keys: sensor identifier.
219 * Values: array of time_t. Each time_t is corresponding to a sensor
220 * measure which has been used as the start point of a Bezier curve.
222 static GHashTable *times;
224 static void draw_sensor_smooth_curve(struct psensor *s,
232 struct graph_info *info)
234 int i, dt, vdt, j, k, found;
235 double x[4], y[4], v;
236 time_t t, t0, *stimes;
239 times = g_hash_table_new_full(g_str_hash,
244 stimes = g_hash_table_lookup(times, s->id);
246 cairo_set_source_rgb(cr,
251 /* search the index of the first measure used as a start point
252 * of a Bezier curve. The start and end points of the Bezier
253 * curves must be preserved to ensure the same overall shape
257 while (i < s->values_max_length) {
258 t = s->measures[i].time.tv_sec;
259 v = s->measures[i].value;
262 if (v != UNKNOWN_DBL_VALUE && t) {
265 if (t == stimes[k]) {
280 stimes = malloc((s->values_max_length + 1) * sizeof(time_t));
281 memset(stimes, 0, (s->values_max_length + 1) * sizeof(time_t));
282 g_hash_table_insert(times, strdup(s->id), stimes);
284 if (i == s->values_max_length)
289 while (i < s->values_max_length) {
292 while (i < s->values_max_length && j < 4) {
293 t = s->measures[i].time.tv_sec;
294 v = s->measures[i].value;
296 if (v == UNKNOWN_DBL_VALUE || !t) {
303 x[0 + j] = ((double)vdt * g_width) / dt + info->g_xoff;
304 y[0 + j] = compute_y(v,
318 cairo_move_to(cr, x[0], y[0]);
319 cairo_curve_to(cr, x[1], y[1], x[2], y[3], x[3], y[3]);
328 static void draw_sensor_curve(struct psensor *s,
336 struct graph_info *info)
338 int first, i, t, dt, vdt;
341 cairo_set_source_rgb(cr,
348 for (i = 0; i < s->values_max_length; i++) {
349 t = s->measures[i].time.tv_sec;
350 v = s->measures[i].value;
352 if (v == UNKNOWN_DBL_VALUE || !t)
357 x = ((double)vdt * g_width) / dt + info->g_xoff;
359 y = compute_y(v, min, max, g_height, info->g_yoff);
362 cairo_move_to(cr, x, y);
365 cairo_line_to(cr, x, y);
372 static void display_no_graphs_warning(cairo_t *cr, int x, int y)
376 msg = strdup(_("No graphs enabled"));
378 cairo_select_font_face(cr,
380 CAIRO_FONT_SLANT_NORMAL,
381 CAIRO_FONT_WEIGHT_NORMAL);
382 cairo_set_font_size(cr, 18.0);
384 cairo_move_to(cr, x, y);
385 cairo_show_text(cr, msg);
391 graph_update(struct psensor **sensors,
393 struct config *config,
396 struct color *fgcolor = config->graph_fgcolor;
397 int et, bt, width, height, g_width, g_height;
398 double min_rpm, max_rpm, mint, maxt, min, max;
399 char *strmin, *strmax;
400 /* horizontal and vertical offset of the graph */
401 int g_xoff, g_yoff, no_graphs;
402 cairo_surface_t *cst;
403 cairo_t *cr, *cr_pixmap;
404 char *str_btime, *str_etime;
405 cairo_text_extents_t te_btime, te_etime, te_max, te_min;
406 struct psensor **sensor_cur, **enabled_sensors;
407 GtkAllocation galloc;
408 GtkStyleContext *style_ctx;
409 struct graph_info info;
411 if (!gtk_widget_is_drawable(w_graph))
414 enabled_sensors = psensor_list_filter_graph_enabled(sensors);
416 min_rpm = get_min_rpm(enabled_sensors);
417 max_rpm = get_max_rpm(enabled_sensors);
419 mint = get_min_temp(enabled_sensors);
420 strmin = psensor_value_to_str(SENSOR_TYPE_TEMP,
422 config->temperature_unit == CELSIUS);
424 maxt = get_max_temp(enabled_sensors);
425 strmax = psensor_value_to_str(SENSOR_TYPE_TEMP,
427 config->temperature_unit == CELSIUS);
429 et = get_graph_end_time_s(enabled_sensors);
430 bt = get_graph_begin_time_s(config, et);
432 str_btime = time_to_str(bt);
433 str_etime = time_to_str(et);
435 gtk_widget_get_allocation(w_graph, &galloc);
436 width = galloc.width;
437 info.width = galloc.width;
438 height = galloc.height;
439 info.height = height;
442 cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
443 cr = cairo_create(cst);
445 cairo_select_font_face(cr,
447 CAIRO_FONT_SLANT_NORMAL,
448 CAIRO_FONT_WEIGHT_NORMAL);
449 cairo_set_font_size(cr, 10.0);
451 cairo_text_extents(cr, str_etime, &te_etime);
452 cairo_text_extents(cr, str_btime, &te_btime);
453 cairo_text_extents(cr, strmax, &te_max);
454 cairo_text_extents(cr, strmin, &te_min);
456 g_yoff = GRAPH_V_PADDING;
458 g_height = height - GRAPH_V_PADDING;
459 if (te_etime.height > te_btime.height)
460 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
462 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
464 if (te_min.width > te_max.width)
465 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
467 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
469 info.g_xoff = g_xoff;
470 info.g_yoff = g_yoff;
472 style_ctx = gtk_widget_get_style_context(window);
473 gtk_style_context_get_background_color(style_ctx,
474 GTK_STATE_FLAG_NORMAL,
475 &info.theme_bg_color);
476 gtk_style_context_get_color(style_ctx,
477 GTK_STATE_FLAG_NORMAL,
478 &info.theme_fg_color);
481 g_width = width - g_xoff - GRAPH_H_PADDING;
483 draw_graph_background(cr, g_width, g_height, config, &info);
485 /* Set the color for text drawing */
486 cairo_set_source_rgb(cr,
487 info.theme_fg_color.red,
488 info.theme_fg_color.green,
489 info.theme_fg_color.blue);
491 /* draw graph begin time */
492 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
493 cairo_show_text(cr, str_btime);
496 /* draw graph end time */
498 width - te_etime.width - GRAPH_H_PADDING,
499 height - GRAPH_V_PADDING);
500 cairo_show_text(cr, str_etime);
503 draw_background_lines(cr, fgcolor,
508 /* .. and finaly draws the temperature graphs */
510 sensor_cur = enabled_sensors;
512 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
513 cairo_set_line_width(cr, 1);
515 while (*sensor_cur) {
516 struct psensor *s = *sensor_cur;
519 if (s->type & SENSOR_TYPE_RPM) {
522 } else if (s->type & SENSOR_TYPE_PERCENT) {
524 max = get_max_value(enabled_sensors,
525 SENSOR_TYPE_PERCENT);
531 if (is_smooth_curves_enabled)
532 draw_sensor_smooth_curve(s, cr,
538 draw_sensor_curve(s, cr,
548 display_no_graphs_warning(cr,
553 draw_left_region(cr, &info);
555 /* draw min and max temp */
556 cairo_set_source_rgb(cr,
557 info.theme_fg_color.red,
558 info.theme_fg_color.green,
559 info.theme_fg_color.blue);
561 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
562 cairo_show_text(cr, strmax);
566 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
567 cairo_show_text(cr, strmin);
570 cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
573 if (config->alpha_channel_enabled)
574 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
576 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
577 cairo_paint(cr_pixmap);
580 free(enabled_sensors);
582 cairo_destroy(cr_pixmap);
583 cairo_surface_destroy(cst);
587 int compute_values_max_length(struct config *c)
589 int n, duration, interval;
591 duration = c->graph_monitoring_duration * 60;
592 interval = c->sensor_update_interval;
594 n = 3 + ceil((((double)duration) / interval) + 0.5) + 3;