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