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>
46 PangoFontDescription
*bat1_font_desc
;
47 PangoFontDescription
*bat2_font_desc
;
48 struct batstate battery_state
;
51 static timeout
* battery_timeout
;
53 static char buf_bat_percentage
[10];
54 static char buf_bat_time
[20];
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
;
64 #if defined(__OpenBSD__) || defined(__NetBSD__)
68 void update_batterys(void* arg
)
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;
81 if (panel1
[i
].battery
.area
.on_screen
== 0) {
82 panel1
[i
].battery
.area
.on_screen
= 1;
86 panel1
[i
].battery
.area
.resize
= 1;
90 void default_battery()
93 percentage_hide
= 101;
94 battery_low_cmd_send
= 0;
100 path_energy_full
= 0;
101 path_current_now
= 0;
103 #if defined(__OpenBSD__) || defined(__NetBSD__)
108 void cleanup_battery()
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
);
118 #if defined(__OpenBSD__) || defined(__NetBSD__)
119 if ((apm_fd
!= -1) && (close(apm_fd
) == -1))
120 warn("cannot close /dev/apm");
127 if (!battery_enabled
) return;
129 #if defined(__OpenBSD__) || defined(__NetBSD__)
130 apm_fd
= open("/dev/apm", O_RDONLY
);
132 warn("init_battery: failed to open /dev/apm.");
137 #elif !defined(__FreeBSD__)
140 GError
*error
= NULL
;
141 const char *entryname
;
142 char *battery_dir
= 0;
144 directory
= g_dir_open("/sys/class/power_supply", 0, &error
);
148 while ((entryname
=g_dir_read_name(directory
))) {
149 if (strncmp(entryname
,"AC", 2) == 0) continue;
151 char *path1
= g_build_filename("/sys/class/power_supply", entryname
, "present", NULL
);
152 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
154 battery_dir
= g_build_filename("/sys/class/power_supply", entryname
, NULL
);
161 g_dir_close(directory
);
163 fprintf(stderr
, "ERROR: battery applet can't found power_supply\n");
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
);
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
);
180 fprintf(stderr
, "ERROR: can't found energy_* or charge_*\n");
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
);
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
) {
197 fprintf(stderr
, "ERROR: battery applet can't open energy_now\n");
209 if (battery_enabled
&& battery_timeout
==0)
210 battery_timeout
= add_timeout(10, 10000, update_batterys
, 0);
214 void init_battery_panel(void *p
)
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
;
220 if (!battery_enabled
)
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;
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
);
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
));
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
);
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
);
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;
256 void update_battery() {
257 #if !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__FreeBSD__)
258 // unused on OpenBSD, silence compiler warnings
261 int64_t current_now
= 0;
263 #if defined(__FreeBSD__)
267 int64_t energy_now
= 0, energy_full
= 0;
269 int8_t new_percentage
= 0;
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");
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
;
283 battery_state
.state
= BATTERY_DISCHARGING
;
287 if (info
.battery_life
== 100)
288 battery_state
.state
= BATTERY_FULL
;
290 // no mapping for openbsd really
294 if (info
.minutes_left
!= -1)
295 seconds
= info
.minutes_left
* 60;
299 new_percentage
= info
.battery_life
;
301 #elif defined(__FreeBSD__)
302 len
= sizeof(sysctl_out
);
304 if (sysctlbyname("hw.acpi.battery.state", &sysctl_out
, &len
, NULL
, 0) != 0)
305 fprintf(stderr
, "power update: no such sysctl");
307 // attemp to map the battery state to linux
308 battery_state
.state
= BATTERY_UNKNOWN
;
312 battery_state
.state
= BATTERY_DISCHARGING
;
315 battery_state
.state
= BATTERY_CHARGING
;
318 battery_state
.state
= BATTERY_FULL
;
322 // no mapping for freebsd
326 if (sysctlbyname("hw.acpi.battery.time", &sysctl_out
, &len
, NULL
, 0) != 0)
329 seconds
= sysctl_out
* 60;
335 if (sysctlbyname("hw.acpi.battery.life", &sysctl_out
, &len
, NULL
, 0) != 0)
338 new_percentage
= sysctl_out
;
341 fp
= fopen(path_status
, "r");
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
;
352 fp
= fopen(path_energy_now
, "r");
354 if (fgets(tmp
, sizeof tmp
, fp
)) energy_now
= atoi(tmp
);
358 fp
= fopen(path_energy_full
, "r");
360 if (fgets(tmp
, sizeof tmp
, fp
)) energy_full
= atoi(tmp
);
364 fp
= fopen(path_current_now
, "r");
366 if (fgets(tmp
, sizeof tmp
, fp
)) current_now
= atoi(tmp
);
370 if(current_now
> 0) {
371 switch(battery_state
.state
) {
372 case BATTERY_CHARGING
:
373 seconds
= 3600 * (energy_full
- energy_now
) / current_now
;
375 case BATTERY_DISCHARGING
:
376 seconds
= 3600 * energy_now
/ current_now
;
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
;
392 new_percentage
= (energy_now
*100)/energy_full
;
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;
398 if(battery_low_status
< new_percentage
&& battery_state
.state
== BATTERY_CHARGING
&& battery_low_cmd_send
) {
399 battery_low_cmd_send
= 0;
402 battery_state
.percentage
= new_percentage
;
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;
411 void draw_battery (void *obj
, cairo_t
*c
)
413 Battery
*battery
= obj
;
416 layout
= pango_cairo_create_layout (c
);
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
));
424 cairo_set_source_rgba(c
, battery
->font
.color
[0], battery
->font
.color
[1], battery
->font
.color
[2], battery
->font
.alpha
);
426 pango_cairo_update_layout(c
, layout
);
427 cairo_move_to(c
, 0, battery
->bat1_posy
);
428 pango_cairo_show_layout(c
, layout
);
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
);
435 pango_cairo_update_layout(c
, layout
);
436 cairo_move_to(c
, 0, battery
->bat2_posy
);
437 pango_cairo_show_layout(c
, layout
);
439 g_object_unref(layout
);
443 int resize_battery(void *obj
)
445 Battery
*battery
= obj
;
447 int percentage_width
, time_width
, new_width
, ret
= 0;
449 percentage_width
= time_width
= 0;
450 battery
->area
.redraw
= 1;
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");
456 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
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);
468 pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, battery
->area
.width
, battery
->area
.height
, server
.depth
);
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
);
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
);
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
);
485 if(percentage_width
> time_width
) new_width
= percentage_width
;
486 else new_width
= time_width
;
488 int old_width
= battery
->area
.width
;
490 new_width
+= (2*battery
->area
.paddingxlr
) + (2*battery
->area
.bg
->border
.width
);
491 battery
->area
.width
= new_width
+ 1;
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);
500 g_object_unref (layout
);
502 cairo_surface_destroy (cs
);
503 XFreePixmap (server
.dsp
, pmap
);