]> Dogcows Code - chaz/tint2/blob - src/battery/battery.c
*fix* execute an external command by calling fork/execl and do not ignore SIGCHLD...
[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 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
28 #include <machine/apmvar.h>
29 #include <err.h>
30 #include <sys/ioctl.h>
31 #include <unistd.h>
32 #endif
33
34 #include "window.h"
35 #include "server.h"
36 #include "area.h"
37 #include "panel.h"
38 #include "taskbar.h"
39 #include "battery.h"
40 #include "clock.h"
41 #include "timer.h"
42 #include "common.h"
43
44 PangoFontDescription *bat1_font_desc;
45 PangoFontDescription *bat2_font_desc;
46 struct batstate battery_state;
47 int battery_enabled;
48 int percentage_hide;
49 static timeout* battery_timeout;
50
51 static char buf_bat_percentage[10];
52 static char buf_bat_time[20];
53
54 int8_t battery_low_status;
55 unsigned char battery_low_cmd_send;
56 char *battery_low_cmd;
57 char *path_energy_now;
58 char *path_energy_full;
59 char *path_current_now;
60 char *path_status;
61
62 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
63 int apm_fd;
64 #endif
65
66
67 void update_batterys(void* arg)
68 {
69 int i;
70 update_battery();
71 for (i=0 ; i < nb_panel ; i++) {
72 if (battery_state.percentage >= percentage_hide) {
73 if (panel1[i].battery.area.on_screen == 1) {
74 panel1[i].battery.area.on_screen = 0;
75 // force resize on panel
76 panel1[i].area.resize = 1;
77 panel_refresh = 1;
78 }
79 continue;
80 }
81 else {
82 if (panel1[i].battery.area.on_screen == 0) {
83 panel1[i].battery.area.on_screen = 1;
84 // force resize on panel
85 panel1[i].area.resize = 1;
86 panel_refresh = 1;
87 }
88 }
89 panel1[i].battery.area.resize = 1;
90 }
91 }
92
93 void default_battery()
94 {
95 battery_enabled = 0;
96 percentage_hide = 101;
97 battery_low_cmd_send = 0;
98 battery_timeout = 0;
99 bat1_font_desc = 0;
100 bat2_font_desc = 0;
101 battery_low_cmd = 0;
102 path_energy_now = 0;
103 path_energy_full = 0;
104 path_current_now = 0;
105 path_status = 0;
106 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
107 apm_fd = -1;
108 #endif
109 }
110
111 void cleanup_battery()
112 {
113 if (bat1_font_desc) pango_font_description_free(bat1_font_desc);
114 if (bat2_font_desc) pango_font_description_free(bat2_font_desc);
115 if (path_energy_now) g_free(path_energy_now);
116 if (path_energy_full) g_free(path_energy_full);
117 if (path_current_now) g_free(path_current_now);
118 if (path_status) g_free(path_status);
119 if (battery_low_cmd) g_free(battery_low_cmd);
120
121 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
122 if ((apm_fd != -1) && (close(apm_fd) == -1))
123 warn("cannot close /dev/apm");
124 #endif
125 }
126
127
128 void init_battery()
129 {
130 if (!battery_enabled) return;
131
132 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
133 apm_fd = open("/dev/apm", O_RDONLY);
134 if (apm_fd < 0) {
135 warn("init_battery: failed to open /dev/apm.");
136 battery_enabled = 0;
137 return;
138 }
139
140 #else
141 // check battery
142 GDir *directory = 0;
143 GError *error = NULL;
144 const char *entryname;
145 char *battery_dir = 0;
146
147 directory = g_dir_open("/sys/class/power_supply", 0, &error);
148 if (error)
149 g_error_free(error);
150 else {
151 while ((entryname=g_dir_read_name(directory))) {
152 if (strncmp(entryname,"AC", 2) == 0) continue;
153
154 char *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
155 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
156 g_free(path1);
157 battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
158 break;
159 }
160 g_free(path1);
161 }
162 }
163 if (directory)
164 g_dir_close(directory);
165 if (!battery_dir) {
166 fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
167 default_battery();
168 return;
169 }
170
171 char *path1 = g_build_filename(battery_dir, "energy_now", NULL);
172 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
173 path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
174 path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
175 }
176 else {
177 char *path2 = g_build_filename(battery_dir, "charge_now", NULL);
178 if (g_file_test (path2, G_FILE_TEST_EXISTS)) {
179 path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
180 path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
181 }
182 else {
183 fprintf(stderr, "ERROR: can't found energy_* or charge_*\n");
184 }
185 g_free(path2);
186 }
187 if (path_energy_now && path_energy_full) {
188 path_current_now = g_build_filename(battery_dir, "current_now", NULL);
189 path_status = g_build_filename(battery_dir, "status", NULL);
190
191 // check file
192 FILE *fp1, *fp2, *fp3, *fp4;
193 fp1 = fopen(path_energy_now, "r");
194 fp2 = fopen(path_energy_full, "r");
195 fp3 = fopen(path_current_now, "r");
196 fp4 = fopen(path_status, "r");
197 if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
198 cleanup_battery();
199 default_battery();
200 fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
201 }
202 fclose(fp1);
203 fclose(fp2);
204 fclose(fp3);
205 fclose(fp4);
206 }
207
208 g_free(path1);
209 g_free(battery_dir);
210 #endif
211
212 if (battery_enabled && battery_timeout==0)
213 battery_timeout = add_timeout(10, 10000, update_batterys, 0);
214 }
215
216
217 void init_battery_panel(void *p)
218 {
219 Panel *panel = (Panel*)p;
220 Battery *battery = &panel->battery;
221 int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
222
223 if (!battery_enabled)
224 return;
225
226 battery->area.parent = p;
227 battery->area.panel = p;
228 battery->area._draw_foreground = draw_battery;
229 battery->area._resize = resize_battery;
230 battery->area.resize = 1;
231 battery->area.redraw = 1;
232 battery->area.on_screen = 1;
233
234 update_battery(&battery_state);
235 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
236 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
237
238 get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
239 get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
240
241 if (panel_horizontal) {
242 // panel horizonal => fixed height and posy
243 battery->area.posy = panel->area.bg->border.width + panel->area.paddingy;
244 battery->area.height = panel->area.height - (2 * battery->area.posy);
245 }
246 else {
247 // panel vertical => fixed width, height, posy and posx
248 battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
249 battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
250 battery->area.posx = panel->area.bg->border.width + panel->area.paddingy;
251 battery->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
252 }
253
254 battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
255 battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
256 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;
257 }
258
259
260 void update_battery() {
261 #if !defined(__OpenBSD__) && !defined(__FreeBSD__) && !defined(__NetBSD__)
262 // unused on OpenBSD, silence compiler warnings
263 FILE *fp;
264 char tmp[25];
265 int64_t current_now = 0;
266 #endif
267 int64_t energy_now = 0, energy_full = 0;
268 int seconds = 0;
269 int8_t new_percentage = 0;
270
271 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__)
272 struct apm_power_info info;
273 if (ioctl(apm_fd, APM_IOC_GETPOWER, &(info)) < 0)
274 warn("power update: APM_IOC_GETPOWER");
275
276 // best attempt at mapping to linux battery states
277 battery_state.state = BATTERY_UNKNOWN;
278 switch (info.battery_state) {
279 case APM_BATT_CHARGING:
280 battery_state.state = BATTERY_CHARGING;
281 break;
282 default:
283 battery_state.state = BATTERY_DISCHARGING;
284 break;
285 }
286
287 if (info.battery_life == 100)
288 battery_state.state = BATTERY_FULL;
289
290 // no mapping for openbsd really
291 energy_full = 0;
292 energy_now = 0;
293
294 if (info.minutes_left != -1)
295 seconds = info.minutes_left * 60;
296 else
297 seconds = -1;
298
299 new_percentage = info.battery_life;
300
301 #else
302 fp = fopen(path_status, "r");
303 if(fp != NULL) {
304 if (fgets(tmp, sizeof tmp, fp)) {
305 battery_state.state = BATTERY_UNKNOWN;
306 if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
307 if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
308 if(strcasecmp(tmp, "Full\n")==0) battery_state.state = BATTERY_FULL;
309 }
310 fclose(fp);
311 }
312
313 fp = fopen(path_energy_now, "r");
314 if(fp != NULL) {
315 if (fgets(tmp, sizeof tmp, fp)) energy_now = atoi(tmp);
316 fclose(fp);
317 }
318
319 fp = fopen(path_energy_full, "r");
320 if(fp != NULL) {
321 if (fgets(tmp, sizeof tmp, fp)) energy_full = atoi(tmp);
322 fclose(fp);
323 }
324
325 fp = fopen(path_current_now, "r");
326 if(fp != NULL) {
327 if (fgets(tmp, sizeof tmp, fp)) current_now = atoi(tmp);
328 fclose(fp);
329 }
330
331 if(current_now > 0) {
332 switch(battery_state.state) {
333 case BATTERY_CHARGING:
334 seconds = 3600 * (energy_full - energy_now) / current_now;
335 break;
336 case BATTERY_DISCHARGING:
337 seconds = 3600 * energy_now / current_now;
338 break;
339 default:
340 seconds = 0;
341 break;
342 }
343 } else seconds = 0;
344 #endif
345
346 battery_state.time.hours = seconds / 3600;
347 seconds -= 3600 * battery_state.time.hours;
348 battery_state.time.minutes = seconds / 60;
349 seconds -= 60 * battery_state.time.minutes;
350 battery_state.time.seconds = seconds;
351
352 if(energy_full > 0)
353 new_percentage = (energy_now*100)/energy_full;
354
355 if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
356 tint_exec(battery_low_cmd);
357 battery_low_cmd_send = 1;
358 }
359 if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
360 battery_low_cmd_send = 0;
361 }
362
363 battery_state.percentage = new_percentage;
364
365 // clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
366 if(battery_state.percentage > 100) {
367 battery_state.percentage = 100;
368 }
369 }
370
371
372 void draw_battery (void *obj, cairo_t *c)
373 {
374 Battery *battery = obj;
375 PangoLayout *layout;
376
377 layout = pango_cairo_create_layout (c);
378
379 // draw layout
380 pango_layout_set_font_description(layout, bat1_font_desc);
381 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
382 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
383 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
384
385 cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);
386
387 pango_cairo_update_layout(c, layout);
388 cairo_move_to(c, 0, battery->bat1_posy);
389 pango_cairo_show_layout(c, layout);
390
391 pango_layout_set_font_description(layout, bat2_font_desc);
392 pango_layout_set_indent(layout, 0);
393 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
394 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
395
396 pango_cairo_update_layout(c, layout);
397 cairo_move_to(c, 0, battery->bat2_posy);
398 pango_cairo_show_layout(c, layout);
399
400 g_object_unref(layout);
401 }
402
403
404 void resize_battery(void *obj)
405 {
406 Battery *battery = obj;
407 PangoLayout *layout;
408 int percentage_width, time_width, new_width;
409
410 percentage_width = time_width = 0;
411 battery->area.redraw = 1;
412
413 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
414 if(battery_state.state == BATTERY_FULL) {
415 strcpy(buf_bat_time, "Full");
416 } else {
417 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
418 }
419 // vertical panel doen't adjust width
420 if (!panel_horizontal) return;
421
422 cairo_surface_t *cs;
423 cairo_t *c;
424 Pixmap pmap;
425 pmap = XCreatePixmap(server.dsp, server.root_win, battery->area.width, battery->area.height, server.depth);
426
427 cs = cairo_xlib_surface_create(server.dsp, pmap, server.visual, battery->area.width, battery->area.height);
428 c = cairo_create(cs);
429 layout = pango_cairo_create_layout(c);
430
431 // check width
432 pango_layout_set_font_description(layout, bat1_font_desc);
433 pango_layout_set_indent(layout, 0);
434 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
435 pango_layout_get_pixel_size(layout, &percentage_width, NULL);
436
437 pango_layout_set_font_description(layout, bat2_font_desc);
438 pango_layout_set_indent(layout, 0);
439 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
440 pango_layout_get_pixel_size(layout, &time_width, NULL);
441
442 if(percentage_width > time_width) new_width = percentage_width;
443 else new_width = time_width;
444
445 new_width += (2*battery->area.paddingxlr) + (2*battery->area.bg->border.width);
446
447 int old_width = battery->area.width;
448
449 Panel *panel = ((Area*)obj)->panel;
450 battery->area.width = new_width + 1;
451 battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.bg->border.width;
452 if (panel->clock.area.on_screen)
453 battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
454
455 if(new_width > old_width || new_width < (old_width-6)) {
456 // refresh and resize other objects on panel
457 // we try to limit the number of refresh
458 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
459 panel->area.resize = 1;
460 systray.area.resize = 1;
461 panel_refresh = 1;
462 }
463
464 g_object_unref (layout);
465 cairo_destroy (c);
466 cairo_surface_destroy (cs);
467 XFreePixmap (server.dsp, pmap);
468 }
469
This page took 0.055036 seconds and 5 git commands to generate.