1 /**************************************************************************
5 * Copyright (C) 2009 Sebastian Reichel <elektranox@gmail.com>
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.
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 **************************************************************************/
24 #include <cairo-xlib.h>
25 #include <pango/pangocairo.h>
27 #if defined(__OpenBSD__) || defined(__NetBSD__)
28 #include <machine/apmvar.h>
30 #include <sys/ioctl.h>
34 #if defined(__FreeBSD__)
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
49 PangoFontDescription
*bat1_font_desc
;
50 PangoFontDescription
*bat2_font_desc
;
51 struct batstate battery_state
;
54 static timeout
* battery_timeout
;
56 static char buf_bat_percentage
[10];
57 static char buf_bat_time
[20];
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
;
67 #if defined(__OpenBSD__) || defined(__NetBSD__)
71 void update_batterys(void* arg
)
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;
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;
93 panel1
[i
].battery
.area
.resize
= 1;
97 void default_battery()
100 percentage_hide
= 101;
101 battery_low_cmd_send
= 0;
107 path_energy_full
= 0;
108 path_current_now
= 0;
110 #if defined(__OpenBSD__) || defined(__NetBSD__)
115 void cleanup_battery()
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
);
125 #if defined(__OpenBSD__) || defined(__NetBSD__)
126 if ((apm_fd
!= -1) && (close(apm_fd
) == -1))
127 warn("cannot close /dev/apm");
134 if (!battery_enabled
) return;
136 #if defined(__OpenBSD__) || defined(__NetBSD__)
137 apm_fd
= open("/dev/apm", O_RDONLY
);
139 warn("init_battery: failed to open /dev/apm.");
144 #elif !defined(__FreeBSD__)
147 GError
*error
= NULL
;
148 const char *entryname
;
149 char *battery_dir
= 0;
151 directory
= g_dir_open("/sys/class/power_supply", 0, &error
);
155 while ((entryname
=g_dir_read_name(directory
))) {
156 if (strncmp(entryname
,"AC", 2) == 0) continue;
158 char *path1
= g_build_filename("/sys/class/power_supply", entryname
, "present", NULL
);
159 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
161 battery_dir
= g_build_filename("/sys/class/power_supply", entryname
, NULL
);
168 g_dir_close(directory
);
170 fprintf(stderr
, "ERROR: battery applet can't found power_supply\n");
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
);
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
);
187 fprintf(stderr
, "ERROR: can't found energy_* or charge_*\n");
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
);
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
) {
204 fprintf(stderr
, "ERROR: battery applet can't open energy_now\n");
216 if (battery_enabled
&& battery_timeout
==0)
217 battery_timeout
= add_timeout(10, 10000, update_batterys
, 0);
221 void init_battery_panel(void *p
)
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
;
227 if (!battery_enabled
)
230 battery
->area
.parent
= p
;
231 battery
->area
.panel
= p
;
232 battery
->area
._draw_foreground
= draw_battery
;
233 battery
->area
.size_mode
= SIZE_BY_CONTENT
;
234 battery
->area
._resize
= resize_battery
;
235 battery
->area
.resize
= 1;
236 battery
->area
.redraw
= 1;
237 battery
->area
.on_screen
= 1;
239 update_battery(&battery_state
);
240 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
241 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
243 get_text_size(bat1_font_desc
, &bat_percentage_height_ink
, &bat_percentage_height
, panel
->area
.height
, buf_bat_percentage
, strlen(buf_bat_percentage
));
244 get_text_size(bat2_font_desc
, &bat_time_height_ink
, &bat_time_height
, panel
->area
.height
, buf_bat_time
, strlen(buf_bat_time
));
246 if (panel_horizontal
) {
247 // panel horizonal => fixed height and posy
248 battery
->area
.posy
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
249 battery
->area
.height
= panel
->area
.height
- (2 * battery
->area
.posy
);
252 // panel vertical => fixed width, height, posy and posx
253 battery
->area
.posy
= panel
->clock
.area
.posy
+ panel
->clock
.area
.height
+ panel
->area
.paddingx
;
254 battery
->area
.height
= (2 * battery
->area
.paddingxlr
) + (bat_time_height
+ bat_percentage_height
);
255 battery
->area
.posx
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
256 battery
->area
.width
= panel
->area
.width
- (2 * panel
->area
.bg
->border
.width
) - (2 * panel
->area
.paddingy
);
259 battery
->bat1_posy
= (battery
->area
.height
- bat_percentage_height
) / 2;
260 battery
->bat1_posy
-= ((bat_time_height_ink
+ 2) / 2);
261 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;
265 void update_battery() {
266 #if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__)
267 // unused on OpenBSD, silence compiler warnings
270 int64_t current_now
= 0;
272 #if defined(__FreeBSD__)
276 int64_t energy_now
= 0, energy_full
= 0;
278 int8_t new_percentage
= 0;
280 #if defined(__OpenBSD__) || defined(__NetBSD__)
281 struct apm_power_info info
;
282 if (ioctl(apm_fd
, APM_IOC_GETPOWER
, &(info
)) < 0)
283 warn("power update: APM_IOC_GETPOWER");
285 // best attempt at mapping to linux battery states
286 battery_state
.state
= BATTERY_UNKNOWN
;
287 switch (info
.battery_state
) {
288 case APM_BATT_CHARGING
:
289 battery_state
.state
= BATTERY_CHARGING
;
292 battery_state
.state
= BATTERY_DISCHARGING
;
296 if (info
.battery_life
== 100)
297 battery_state
.state
= BATTERY_FULL
;
299 // no mapping for openbsd really
303 if (info
.minutes_left
!= -1)
304 seconds
= info
.minutes_left
* 60;
308 new_percentage
= info
.battery_life
;
310 #elif defined(__FreeBSD__)
311 len
= sizeof(sysctl_out
);
313 if (sysctlbyname("hw.acpi.battery.state", &sysctl_out
, &len
, NULL
, 0) != 0)
314 fprintf(stderr
, "power update: no such sysctl");
316 // attemp to map the battery state to linux
317 battery_state
.state
= BATTERY_UNKNOWN
;
321 battery_state
.state
= BATTERY_DISCHARGING
;
324 battery_state
.state
= BATTERY_CHARGING
;
327 battery_state
.state
= BATTERY_FULL
;
331 // no mapping for freebsd
335 if (sysctlbyname("hw.acpi.battery.time", &sysctl_out
, &len
, NULL
, 0) != 0)
338 seconds
= sysctl_out
* 60;
344 if (sysctlbyname("hw.acpi.battery.life", &sysctl_out
, &len
, NULL
, 0) != 0)
347 new_percentage
= sysctl_out
;
350 fp
= fopen(path_status
, "r");
352 if (fgets(tmp
, sizeof tmp
, fp
)) {
353 battery_state
.state
= BATTERY_UNKNOWN
;
354 if(strcasecmp(tmp
, "Charging\n")==0) battery_state
.state
= BATTERY_CHARGING
;
355 if(strcasecmp(tmp
, "Discharging\n")==0) battery_state
.state
= BATTERY_DISCHARGING
;
356 if(strcasecmp(tmp
, "Full\n")==0) battery_state
.state
= BATTERY_FULL
;
361 fp
= fopen(path_energy_now
, "r");
363 if (fgets(tmp
, sizeof tmp
, fp
)) energy_now
= atoi(tmp
);
367 fp
= fopen(path_energy_full
, "r");
369 if (fgets(tmp
, sizeof tmp
, fp
)) energy_full
= atoi(tmp
);
373 fp
= fopen(path_current_now
, "r");
375 if (fgets(tmp
, sizeof tmp
, fp
)) current_now
= atoi(tmp
);
379 if(current_now
> 0) {
380 switch(battery_state
.state
) {
381 case BATTERY_CHARGING
:
382 seconds
= 3600 * (energy_full
- energy_now
) / current_now
;
384 case BATTERY_DISCHARGING
:
385 seconds
= 3600 * energy_now
/ current_now
;
394 battery_state
.time
.hours
= seconds
/ 3600;
395 seconds
-= 3600 * battery_state
.time
.hours
;
396 battery_state
.time
.minutes
= seconds
/ 60;
397 seconds
-= 60 * battery_state
.time
.minutes
;
398 battery_state
.time
.seconds
= seconds
;
401 new_percentage
= (energy_now
*100)/energy_full
;
403 if(battery_low_status
> new_percentage
&& battery_state
.state
== BATTERY_DISCHARGING
&& !battery_low_cmd_send
) {
404 tint_exec(battery_low_cmd
);
405 battery_low_cmd_send
= 1;
407 if(battery_low_status
< new_percentage
&& battery_state
.state
== BATTERY_CHARGING
&& battery_low_cmd_send
) {
408 battery_low_cmd_send
= 0;
411 battery_state
.percentage
= new_percentage
;
413 // clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
414 if(battery_state
.percentage
> 100) {
415 battery_state
.percentage
= 100;
420 void draw_battery (void *obj
, cairo_t
*c
)
422 Battery
*battery
= obj
;
425 layout
= pango_cairo_create_layout (c
);
428 pango_layout_set_font_description(layout
, bat1_font_desc
);
429 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
430 pango_layout_set_alignment(layout
, PANGO_ALIGN_CENTER
);
431 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
433 cairo_set_source_rgba(c
, battery
->font
.color
[0], battery
->font
.color
[1], battery
->font
.color
[2], battery
->font
.alpha
);
435 pango_cairo_update_layout(c
, layout
);
436 cairo_move_to(c
, 0, battery
->bat1_posy
);
437 pango_cairo_show_layout(c
, layout
);
439 pango_layout_set_font_description(layout
, bat2_font_desc
);
440 pango_layout_set_indent(layout
, 0);
441 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
442 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
444 pango_cairo_update_layout(c
, layout
);
445 cairo_move_to(c
, 0, battery
->bat2_posy
);
446 pango_cairo_show_layout(c
, layout
);
448 g_object_unref(layout
);
452 void resize_battery(void *obj
)
454 Battery
*battery
= obj
;
456 int percentage_width
, time_width
, new_width
;
458 percentage_width
= time_width
= 0;
459 battery
->area
.redraw
= 1;
461 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
462 if(battery_state
.state
== BATTERY_FULL
) {
463 strcpy(buf_bat_time
, "Full");
465 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
467 // vertical panel doen't adjust width
468 if (!panel_horizontal
) return;
473 pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, battery
->area
.width
, battery
->area
.height
, server
.depth
);
475 cs
= cairo_xlib_surface_create(server
.dsp
, pmap
, server
.visual
, battery
->area
.width
, battery
->area
.height
);
476 c
= cairo_create(cs
);
477 layout
= pango_cairo_create_layout(c
);
480 pango_layout_set_font_description(layout
, bat1_font_desc
);
481 pango_layout_set_indent(layout
, 0);
482 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
483 pango_layout_get_pixel_size(layout
, &percentage_width
, NULL
);
485 pango_layout_set_font_description(layout
, bat2_font_desc
);
486 pango_layout_set_indent(layout
, 0);
487 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
488 pango_layout_get_pixel_size(layout
, &time_width
, NULL
);
490 if(percentage_width
> time_width
) new_width
= percentage_width
;
491 else new_width
= time_width
;
493 new_width
+= (2*battery
->area
.paddingxlr
) + (2*battery
->area
.bg
->border
.width
);
495 int old_width
= battery
->area
.width
;
497 Panel
*panel
= ((Area
*)obj
)->panel
;
498 battery
->area
.width
= new_width
+ 1;
499 battery
->area
.posx
= panel
->area
.width
- battery
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.bg
->border
.width
;
500 if (panel
->clock
.area
.on_screen
)
501 battery
->area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
503 if(new_width
> old_width
|| new_width
< (old_width
-6)) {
504 // refresh and resize other objects on panel
505 // we try to limit the number of refresh
506 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
507 panel
->area
.resize
= 1;
508 systray
.area
.resize
= 1;
512 g_object_unref (layout
);
514 cairo_surface_destroy (cs
);
515 XFreePixmap (server
.dsp
, pmap
);