2 Copyright (C) 2010-2011 wpitchoune@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
27 /* horizontal padding */
28 #define GRAPH_H_PADDING 4
29 /* vertical padding */
30 #define GRAPH_V_PADDING 4
32 time_t get_graph_end_time_s()
36 if (gettimeofday(&tv, NULL) == 0)
42 time_t get_graph_begin_time_s(struct config *cfg)
46 ct = get_graph_end_time_s();
51 return ct - cfg->graph_monitoring_duration * 60;
54 int compute_y(float temp, int mint, int maxt, int width, int height)
56 double t = temp - mint;
57 return height - ((double)height * (t / (maxt - mint)));
60 char *time_to_str(time_t s)
63 /* note: localtime returns a static field, no free required */
64 struct tm *tm = localtime(&s);
70 strftime(str, 6, "%H:%M", tm);
76 draw_graph_background(cairo_t *cr,
77 int width, int height, struct config *config)
79 struct color *bgcolor = config->graph_bgcolor;
82 if (config->alpha_channel_enabled)
83 cairo_set_source_rgba(cr,
86 bgcolor->f_blue, config->graph_bg_alpha);
88 cairo_set_source_rgb(cr,
90 bgcolor->f_green, bgcolor->f_blue);
92 cairo_rectangle(cr, 0, 0, width, height);
97 graph_update(struct psensor **sensors,
98 GtkWidget *w_graph, struct config *config)
100 struct color *fgcolor = config->graph_fgcolor;
102 int min_rpm = get_min_rpm(sensors);
103 int max_rpm = get_max_rpm(sensors);
105 int mint = get_min_temp(sensors);
106 char *strmin = psensor_value_to_string(SENSOR_TYPE_TEMP, mint);
108 int maxt = get_max_temp(sensors);
109 char *strmax = psensor_value_to_string(SENSOR_TYPE_TEMP, maxt);
111 int width = w_graph->allocation.width;
112 int height = w_graph->allocation.height;
116 /* horizontal and vertical offset of the graph */
121 cairo_surface_t *cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
124 cairo_t *cr = cairo_create(cst);
127 char *str_btime = time_to_str(get_graph_begin_time_s(config));
128 cairo_text_extents_t te_btime;
130 char *str_etime = time_to_str(get_graph_end_time_s());
131 cairo_text_extents_t te_etime;
133 /* setup dash style */
140 int ndash = sizeof(dashes) / sizeof(dashes[0]);
144 cairo_text_extents_t te_max, te_min;
146 struct psensor **sensor_cur;
148 draw_graph_background(cr, width, height, config);
150 cairo_select_font_face(cr,
152 CAIRO_FONT_SLANT_NORMAL,
153 CAIRO_FONT_WEIGHT_NORMAL);
154 cairo_set_font_size(cr, 10.0);
156 cairo_text_extents(cr, str_etime, &te_etime);
157 cairo_text_extents(cr, str_btime, &te_btime);
158 cairo_text_extents(cr, strmax, &te_max);
159 cairo_text_extents(cr, strmin, &te_min);
161 if (te_etime.height > te_btime.height)
162 g_yoff = GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
164 g_yoff = GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
166 if (te_min.width > te_max.width)
167 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
169 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
171 g_width = width - g_xoff;
173 cairo_set_source_rgb(cr,
174 fgcolor->f_red, fgcolor->f_green, fgcolor->f_blue);
176 g_height = height - g_yoff - GRAPH_V_PADDING;
178 /* draw graph begin time */
179 cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
180 cairo_show_text(cr, str_btime);
183 /* draw graph end time */
185 width - te_etime.width - GRAPH_H_PADDING,
186 height - GRAPH_V_PADDING);
187 cairo_show_text(cr, str_etime);
190 /* draw min and max temp */
191 cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
192 cairo_show_text(cr, strmax);
196 GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
197 cairo_show_text(cr, strmin);
200 /* draw background lines */
201 cairo_set_dash(cr, dashes, ndash, 0);
202 cairo_set_line_width(cr, 1);
203 cairo_set_source_rgb(cr,
204 fgcolor->f_red, fgcolor->f_green, fgcolor->f_blue);
206 /* vertical lines representing time steps */
207 for (i = 0; i <= 5; i++) {
208 int x = i * (g_width / 5) + g_xoff;
209 cairo_move_to(cr, x, GRAPH_V_PADDING);
210 cairo_line_to(cr, x, height - g_yoff);
214 /* horizontal lines draws a line for each 10C step */
215 for (i = mint; i < maxt; i++) {
217 int y = compute_y(i, mint, maxt, width, g_height)
219 cairo_move_to(cr, g_xoff, y);
220 cairo_line_to(cr, width - GRAPH_H_PADDING, y);
225 /* back to normal line style */
226 cairo_set_dash(cr, 0, 0, 0);
228 /* .. and finaly draws the temperature graphs */
229 bt = get_graph_begin_time_s(config);
230 et = get_graph_end_time_s();
232 sensor_cur = sensors;
233 while (*sensor_cur) {
234 struct psensor *s = *sensor_cur;
240 cairo_set_source_rgb(cr,
244 cairo_set_line_width(cr, 1);
246 for (i = 0; i < s->values_max_length; i++) {
247 int min, max, x, y, t, v;
249 t = s->measures[i].time.tv_sec;
250 v = s->measures[i].value;
252 if (!v || !t || !bt || !et || (t - bt) < 0)
255 if (is_fan_type(s->type)) {
263 x = (t - bt) * g_width / (et - bt) + g_xoff;
265 y = compute_y(v, min, max, g_width, g_height)
269 cairo_move_to(cr, x, y);
272 cairo_line_to(cr, x, y);
282 cr_pixmap = gdk_cairo_create(w_graph->window);
286 if (config->alpha_channel_enabled)
287 cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
289 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
290 cairo_paint(cr_pixmap);
293 cairo_destroy(cr_pixmap);
294 cairo_surface_destroy(cst);