removed usage of deprecated gtk_get_style
[psensor.git] / src / graph.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <stdlib.h>
20 #include <sys/time.h>
21 #include <gtk/gtk.h>
22
23 #include "cfg.h"
24 #include "psensor.h"
25
26 /* horizontal padding */
27 #define GRAPH_H_PADDING 4
28 /* vertical padding */
29 #define GRAPH_V_PADDING 4
30
31 static time_t get_graph_end_time_s()
32 {
33         struct timeval tv;
34
35         if (gettimeofday(&tv, NULL) == 0)
36                 return tv.tv_sec;
37         else
38                 return 0;
39 }
40
41 static time_t get_graph_begin_time_s(struct config *cfg)
42 {
43         int ct;
44
45         ct = get_graph_end_time_s();
46
47         if (!ct)
48                 return 0;
49
50         return ct - cfg->graph_monitoring_duration * 60;
51 }
52
53 static int compute_y(double value, double min, double max, int height, int off)
54 {
55         double t = value - min;
56         return height - ((double)height * (t / (max - min))) + off;
57 }
58
59 static char *time_to_str(time_t s)
60 {
61         char *str;
62         /* note: localtime returns a static field, no free required */
63         struct tm *tm = localtime(&s);
64
65         if (!tm)
66                 return NULL;
67
68         str = malloc(6);
69         strftime(str, 6, "%H:%M", tm);
70
71         return str;
72 }
73
74 static void
75 draw_graph_background(cairo_t *cr,
76                       int g_xoff, int g_yoff,
77                       int g_width, int g_height,
78                       int width, int height, struct config *config,
79                       GtkWidget *widget)
80 {
81         GtkStyleContext* style_ctx;
82         struct color *bgcolor;
83         GdkRGBA rgba;
84
85         bgcolor = config->graph_bgcolor;
86
87         style_ctx = gtk_widget_get_style_context(widget);
88         gtk_style_context_get_background_color(style_ctx,
89                                                GTK_STATE_FLAG_NORMAL,
90                                                &rgba);
91         if (config->alpha_channel_enabled)
92                 cairo_set_source_rgba(cr,
93                                       rgba.red,
94                                       rgba.green,
95                                       rgba.blue,
96                                       config->graph_bg_alpha);
97         else
98                 cairo_set_source_rgb(cr,
99                                      rgba.red,
100                                      rgba.green,
101                                      rgba.blue);
102
103         cairo_rectangle(cr, 0, 0, width, height);
104         cairo_fill(cr);
105         if (config->alpha_channel_enabled)
106                 cairo_set_source_rgba(cr,
107                                       bgcolor->f_red,
108                                       bgcolor->f_green,
109                                       bgcolor->f_blue, config->graph_bg_alpha);
110         else
111                 cairo_set_source_rgb(cr,
112                                      bgcolor->f_red,
113                                      bgcolor->f_green, bgcolor->f_blue);
114
115
116
117         cairo_rectangle(cr, g_xoff, g_yoff, g_width, g_height);
118         cairo_fill(cr);
119 }
120
121 /* setup dash style */
122 static double dashes[] = {
123         1.0,            /* ink */
124         2.0,            /* skip */
125 };
126 static int ndash = sizeof(dashes) / sizeof(dashes[0]);
127
128 static void draw_background_lines(cairo_t *cr,
129                                   struct color *color,
130                                   int g_width, int g_height,
131                                   int g_xoff, int g_yoff,
132                                   int min, int max)
133 {
134         int i;
135
136         /* draw background lines */
137         cairo_set_line_width(cr, 1);
138         cairo_set_dash(cr, dashes, ndash, 0);
139         cairo_set_source_rgb(cr,
140                              color->f_red, color->f_green, color->f_blue);
141
142         /* vertical lines representing time steps */
143         for (i = 0; i <= 5; i++) {
144                 int x = i * (g_width / 5) + g_xoff;
145                 cairo_move_to(cr, x, g_yoff);
146                 cairo_line_to(cr, x, g_yoff + g_height);
147                 cairo_stroke(cr);
148         }
149
150         /* horizontal lines draws a line for each 10C step */
151         for (i = min; i < max; i++) {
152                 if (i % 10 == 0) {
153                         int y = compute_y(i, min, max, g_height, g_yoff);
154
155                         cairo_move_to(cr, g_xoff, y);
156                         cairo_line_to(cr, g_xoff + g_width, y);
157                         cairo_stroke(cr);
158                 }
159         }
160
161         /* back to normal line style */
162         cairo_set_dash(cr, 0, 0, 0);
163 }
164
165 static void draw_sensor_curve(struct psensor *s,
166                               cairo_t *cr,
167                               double min,
168                               double max,
169                               int bt,
170                               int et,
171                               int g_width,
172                               int g_height,
173                               int g_xoff,
174                               int g_yoff)
175 {
176         int first, i, x, y, t, dt, vdt;
177         double v;
178
179         cairo_set_source_rgb(cr,
180                              s->color->f_red,
181                              s->color->f_green,
182                              s->color->f_blue);
183
184         dt = et - bt;
185         first = 1;
186         for (i = 0; i < s->values_max_length; i++) {
187                 t = s->measures[i].time.tv_sec;
188                 v = s->measures[i].value.d_num;
189
190                 if (v == UNKNOWN_DBL_VALUE || !t)
191                         continue;
192
193                 vdt = t - bt;
194                 if (vdt < 0)
195                         continue;
196
197                 x = vdt * g_width / dt + g_xoff;
198
199                 y = compute_y(v, min, max, g_height, g_yoff);
200
201                 if (first) {
202                         cairo_move_to(cr, x, y);
203                         first = 0;
204                 } else {
205                         cairo_line_to(cr, x, y);
206                 }
207
208         }
209         cairo_stroke(cr);
210 }
211
212
213 void
214 graph_update(struct psensor **sensors,
215              GtkWidget *w_graph,
216              struct config *config)
217 {
218         struct color *fgcolor = config->graph_fgcolor;
219         int et, bt, width, height, g_width, g_height;
220         double min_rpm, max_rpm, mint, maxt;
221         char *strmin, *strmax;
222         /* horizontal and vertical offset of the graph */
223         int g_xoff, g_yoff;
224         cairo_surface_t *cst;
225         cairo_t *cr, *cr_pixmap;
226         char *str_btime, *str_etime;
227         cairo_text_extents_t te_btime, te_etime, te_max, te_min;
228         struct psensor **sensor_cur;
229         GtkAllocation galloc;
230
231         if (!gtk_widget_is_drawable(w_graph))
232                 return ;
233
234         min_rpm = get_min_rpm(sensors);
235         max_rpm = get_max_rpm(sensors);
236
237         mint = get_min_temp(sensors);
238         strmin = psensor_value_to_string(SENSOR_TYPE_TEMP, mint);
239
240         maxt = get_max_temp(sensors);
241         strmax = psensor_value_to_string(SENSOR_TYPE_TEMP, maxt);
242
243         str_btime = time_to_str(get_graph_begin_time_s(config));
244         str_etime = time_to_str(get_graph_end_time_s());
245
246         gtk_widget_get_allocation(w_graph, &galloc);
247         width = galloc.width;
248         height = galloc.height;
249
250         cst = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
251         cr = cairo_create(cst);
252
253         cairo_select_font_face(cr,
254                                "sans-serif",
255                                CAIRO_FONT_SLANT_NORMAL,
256                                CAIRO_FONT_WEIGHT_NORMAL);
257         cairo_set_font_size(cr, 10.0);
258
259         cairo_text_extents(cr, str_etime, &te_etime);
260         cairo_text_extents(cr, str_btime, &te_btime);
261         cairo_text_extents(cr, strmax, &te_max);
262         cairo_text_extents(cr, strmin, &te_min);
263
264         g_yoff = GRAPH_V_PADDING;
265
266         g_height = height - GRAPH_V_PADDING;
267         if (te_etime.height > te_btime.height)
268                 g_height -= GRAPH_V_PADDING + te_etime.height + GRAPH_V_PADDING;
269         else
270                 g_height -= GRAPH_V_PADDING + te_btime.height + GRAPH_V_PADDING;
271
272         if (te_min.width > te_max.width)
273                 g_xoff = (2 * GRAPH_H_PADDING) + te_max.width;
274         else
275                 g_xoff = (2 * GRAPH_H_PADDING) + te_min.width;
276
277         g_width = width - g_xoff - GRAPH_H_PADDING;
278
279         draw_graph_background(cr,
280                               g_xoff, g_yoff, g_width, g_height,
281                               width, height, config,
282                               w_graph);
283
284         cairo_set_source_rgb(cr,
285                              fgcolor->f_red, fgcolor->f_green, fgcolor->f_blue);
286
287         /* draw graph begin time */
288         cairo_move_to(cr, g_xoff, height - GRAPH_V_PADDING);
289         cairo_show_text(cr, str_btime);
290         free(str_btime);
291
292         /* draw graph end time */
293         cairo_move_to(cr,
294                       width - te_etime.width - GRAPH_H_PADDING,
295                       height - GRAPH_V_PADDING);
296         cairo_show_text(cr, str_etime);
297         free(str_etime);
298
299         /* draw min and max temp */
300         cairo_move_to(cr, GRAPH_H_PADDING, te_max.height + GRAPH_V_PADDING);
301         cairo_show_text(cr, strmax);
302         free(strmax);
303
304         cairo_move_to(cr,
305                       GRAPH_H_PADDING, height - (te_min.height / 2) - g_yoff);
306         cairo_show_text(cr, strmin);
307         free(strmin);
308
309         draw_background_lines(cr, fgcolor,
310                               g_width, g_height,
311                               g_xoff, g_yoff,
312                               mint, maxt);
313
314         /* .. and finaly draws the temperature graphs */
315         bt = get_graph_begin_time_s(config);
316         et = get_graph_end_time_s();
317
318         if (bt && et) {
319                 sensor_cur = sensors;
320
321                 cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);
322                 cairo_set_line_width(cr, 1);
323                 while (*sensor_cur) {
324                         struct psensor *s = *sensor_cur;
325
326                         if (s->enabled) {
327                                 double min, max;
328
329                                 if (is_fan_type(s->type)) {
330                                         min = min_rpm;
331                                         max = max_rpm;
332                                 } else if (s->type & SENSOR_TYPE_CPU_USAGE) {
333                                         min = 0;
334                                         max = get_max_value
335                                                 (sensors,
336                                                  SENSOR_TYPE_CPU_USAGE);
337                                 } else {
338                                         min = mint;
339                                         max = maxt;
340                                 }
341
342                                 draw_sensor_curve(s, cr,
343                                                   min, max,
344                                                   bt, et,
345                                                   g_width, g_height,
346                                                   g_xoff, g_yoff);
347                         }
348
349                         sensor_cur++;
350                 }
351         }
352
353         cr_pixmap = gdk_cairo_create(gtk_widget_get_window(w_graph));
354
355         if (cr_pixmap) {
356
357                 if (config->alpha_channel_enabled)
358                         cairo_set_operator(cr_pixmap, CAIRO_OPERATOR_SOURCE);
359
360                 cairo_set_source_surface(cr_pixmap, cst, 0, 0);
361                 cairo_paint(cr_pixmap);
362         }
363
364         cairo_destroy(cr_pixmap);
365         cairo_surface_destroy(cst);
366         cairo_destroy(cr);
367 }