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