]> Dogcows Code - chaz/tint2/blob - src/battery/battery.c
fixed some segfault and memleak
[chaz/tint2] / src / battery / battery.c
1 /**************************************************************************
2 *
3 * Tint2 : battery
4 *
5 * Copyright (C) 2009 Sebastian Reichel <elektranox@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * or any later version as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 **************************************************************************/
19
20 #include <string.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <cairo.h>
24 #include <cairo-xlib.h>
25 #include <pango/pangocairo.h>
26
27 #include "window.h"
28 #include "server.h"
29 #include "taskbar.h"
30 #include "panel.h"
31 #include "area.h"
32 #include "battery.h"
33 #include "clock.h"
34
35 PangoFontDescription *bat1_font_desc;
36 PangoFontDescription *bat2_font_desc;
37 struct batstate battery_state;
38
39 static char buf_bat_percentage[10];
40 static char buf_bat_time[20];
41
42 int8_t battery_low_status;
43 char *battery_low_cmd;
44 char *path_energy_now, *path_energy_full, *path_current_now, *path_status;
45
46
47 void init_battery()
48 {
49 // check battery
50 GDir *directory = 0;
51 GError *error = NULL;
52 const char *entryname;
53 char *battery_dir = 0;
54
55 path_energy_now = path_energy_full = path_current_now = path_status = 0;
56 directory = g_dir_open("/sys/class/power_supply", 0, &error);
57 if (error)
58 g_error_free(error);
59 else {
60 while ((entryname=g_dir_read_name(directory))) {
61 if (strncmp(entryname,"AC", 2) == 0) continue;
62
63 char *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
64 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
65 g_free(path1);
66 battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
67 break;
68 }
69 g_free(path1);
70 }
71 }
72 if (directory)
73 g_dir_close(directory);
74 if (battery_dir) {
75 char *path1 = g_build_filename(battery_dir, "energy_now", NULL);
76 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
77 path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
78 path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
79 }
80 else {
81 char *path2 = g_build_filename(battery_dir, "charge_now", NULL);
82 if (g_file_test (path2, G_FILE_TEST_EXISTS)) {
83 path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
84 path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
85 }
86 else {
87 g_free(battery_dir);
88 battery_dir = 0;
89 fprintf(stderr, "ERROR: can't found energy_* or charge_*\n");
90 }
91 g_free(path2);
92 }
93 path_current_now = g_build_filename(battery_dir, "current_now", NULL);
94 path_status = g_build_filename(battery_dir, "status", NULL);
95 g_free(path1);
96 }
97
98 FILE *fp;
99 Panel *panel;
100 Battery *battery;
101 int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
102
103 for (i=0 ; i < nb_panel ; i++) {
104 panel = &panel1[i];
105 battery = &panel->battery;
106
107 if (battery_dir == 0) battery->area.on_screen = 0;
108 if (!battery->area.on_screen) continue;
109
110 battery->area.parent = panel;
111 battery->area.panel = panel;
112 battery->area._draw_foreground = draw_battery;
113 battery->area._resize = resize_battery;
114 battery->area.resize = 1;
115 battery->area.redraw = 1;
116
117 if((fp = fopen(path_energy_now, "r")) == NULL) {
118 fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
119 panel->battery.area.on_screen = 0;
120 continue;
121 }
122 fclose(fp);
123 if((fp = fopen(path_energy_full, "r")) == NULL) {
124 fprintf(stderr, "ERROR: battery applet can't open energy_full\n");
125 panel->battery.area.on_screen = 0;
126 continue;
127 }
128 fclose(fp);
129 if((fp = fopen(path_current_now, "r")) == NULL) {
130 fprintf(stderr, "ERROR: battery applet can't open current_now\n");
131 panel->battery.area.on_screen = 0;
132 continue;
133 }
134 fclose(fp);
135 if((fp = fopen(path_status, "r")) == NULL) {
136 fprintf(stderr, "ERROR: battery applet can't open status");
137 panel->battery.area.on_screen = 0;
138 continue;
139 }
140 fclose(fp);
141
142 update_battery(&battery_state);
143 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
144 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
145
146 get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
147 get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
148
149 if (panel_horizontal) {
150 // panel horizonal => fixed height and posy
151 battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
152 battery->area.height = panel->area.height - (2 * battery->area.posy);
153 }
154 else {
155 // panel vertical => fixed width, height, posy and posx
156 battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
157 battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
158 battery->area.posx = panel->area.pix.border.width + panel->area.paddingy;
159 battery->area.width = panel->area.width - (2 * panel->area.pix.border.width) - (2 * panel->area.paddingy);
160 }
161
162 battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
163 battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
164 battery->bat2_posy = battery->bat1_posy + bat_percentage_height + 2 - (bat_percentage_height - bat_percentage_height_ink)/2 - (bat_time_height - bat_time_height_ink)/2;
165 }
166
167 if (battery_dir)
168 g_free(battery_dir);
169 }
170
171
172 void update_battery() {
173 FILE *fp;
174 char tmp[25];
175 int64_t energy_now = 0, energy_full = 0, current_now = 0;
176 int seconds = 0;
177 int8_t new_percentage = 0;
178
179 fp = fopen(path_energy_now, "r");
180 if(fp != NULL) {
181 fgets(tmp, sizeof tmp, fp);
182 energy_now = atoi(tmp);
183 fclose(fp);
184 }
185
186 fp = fopen(path_energy_full, "r");
187 if(fp != NULL) {
188 fgets(tmp, sizeof tmp, fp);
189 energy_full = atoi(tmp);
190 fclose(fp);
191 }
192
193 fp = fopen(path_current_now, "r");
194 if(fp != NULL) {
195 fgets(tmp, sizeof tmp, fp);
196 current_now = atoi(tmp);
197 fclose(fp);
198 }
199
200 fp = fopen(path_status, "r");
201 if(fp != NULL) {
202 fgets(tmp, sizeof tmp, fp);
203 fclose(fp);
204 }
205
206 battery_state.state = BATTERY_UNKNOWN;
207 if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
208 if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
209
210 if(current_now > 0) {
211 switch(battery_state.state) {
212 case BATTERY_CHARGING:
213 seconds = 3600 * (energy_full - energy_now) / current_now;
214 break;
215 case BATTERY_DISCHARGING:
216 seconds = 3600 * energy_now / current_now;
217 break;
218 default:
219 seconds = 0;
220 break;
221 }
222 } else seconds = 0;
223
224 battery_state.time.hours = seconds / 3600;
225 seconds -= 3600 * battery_state.time.hours;
226 battery_state.time.minutes = seconds / 60;
227 seconds -= 60 * battery_state.time.minutes;
228 battery_state.time.seconds = seconds;
229
230 if(energy_full > 0)
231 new_percentage = (energy_now*100)/energy_full;
232
233 if(battery_low_status != 0 && battery_low_status == new_percentage && battery_state.percentage > new_percentage) {
234 //printf("battery low, executing: %s\n", battery_low_cmd);
235 if (battery_low_cmd) system(battery_low_cmd);
236 }
237
238 battery_state.percentage = new_percentage;
239 }
240
241
242 void draw_battery (void *obj, cairo_t *c, int active)
243 {
244 Battery *battery = obj;
245 PangoLayout *layout;
246
247 layout = pango_cairo_create_layout (c);
248
249 // draw layout
250 pango_layout_set_font_description(layout, bat1_font_desc);
251 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
252 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
253 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
254
255 cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);
256
257 pango_cairo_update_layout(c, layout);
258 cairo_move_to(c, 0, battery->bat1_posy);
259 pango_cairo_show_layout(c, layout);
260
261 pango_layout_set_font_description(layout, bat2_font_desc);
262 pango_layout_set_indent(layout, 0);
263 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
264 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
265
266 pango_cairo_update_layout(c, layout);
267 cairo_move_to(c, 0, battery->bat2_posy);
268 pango_cairo_show_layout(c, layout);
269
270 g_object_unref(layout);
271 }
272
273
274 void resize_battery(void *obj)
275 {
276 Battery *battery = obj;
277 PangoLayout *layout;
278 int percentage_width, time_width, new_width;
279
280 percentage_width = time_width = 0;
281 battery->area.redraw = 1;
282
283 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
284 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
285 // vertical panel doen't adjust width
286 if (!panel_horizontal) return;
287
288 cairo_surface_t *cs;
289 cairo_t *c;
290 Pixmap pmap;
291 pmap = XCreatePixmap(server.dsp, server.root_win, battery->area.width, battery->area.height, server.depth);
292
293 cs = cairo_xlib_surface_create(server.dsp, pmap, server.visual, battery->area.width, battery->area.height);
294 c = cairo_create(cs);
295 layout = pango_cairo_create_layout(c);
296
297 // check width
298 pango_layout_set_font_description(layout, bat1_font_desc);
299 pango_layout_set_indent(layout, 0);
300 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
301 pango_layout_get_pixel_size(layout, &percentage_width, NULL);
302
303 pango_layout_set_font_description(layout, bat2_font_desc);
304 pango_layout_set_indent(layout, 0);
305 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
306 pango_layout_get_pixel_size(layout, &time_width, NULL);
307
308 if(percentage_width > time_width) new_width = percentage_width;
309 else new_width = time_width;
310
311 new_width += (2*battery->area.paddingxlr) + (2*battery->area.pix.border.width);
312
313 int old_width = battery->area.width;
314
315 Panel *panel = ((Area*)obj)->panel;
316 battery->area.width = new_width + 1;
317 battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
318 if (panel->clock.area.on_screen)
319 battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
320
321 if(new_width > old_width || new_width < (old_width-6)) {
322 // refresh and resize other objects on panel
323 // we try to limit the number of refresh
324 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
325 panel->area.resize = 1;
326 systray.area.resize = 1;
327 panel_refresh = 1;
328 }
329
330 g_object_unref (layout);
331 cairo_destroy (c);
332 cairo_surface_destroy (cs);
333 XFreePixmap (server.dsp, pmap);
334 }
335
This page took 0.061321 seconds and 5 git commands to generate.