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