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