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>
36 PangoFontDescription
*bat1_font_desc
;
37 PangoFontDescription
*bat2_font_desc
;
38 struct batstate battery_state
;
41 static timeout
* battery_timeout
;
43 static char buf_bat_percentage
[10];
44 static char buf_bat_time
[20];
46 int8_t battery_low_status
;
47 unsigned char battery_low_cmd_send
;
48 char *battery_low_cmd
;
49 char *path_energy_now
;
50 char *path_energy_full
;
51 char *path_current_now
;
54 void update_batterys(void* arg
)
58 for (i
=0 ; i
< nb_panel
; i
++) {
59 if (battery_state
.percentage
>= percentage_hide
) {
60 if (panel1
[i
].battery
.area
.on_screen
== 1) {
61 panel1
[i
].battery
.area
.on_screen
= 0;
62 // force resize on panel
63 panel1
[i
].area
.resize
= 1;
69 if (panel1
[i
].battery
.area
.on_screen
== 0) {
70 panel1
[i
].battery
.area
.on_screen
= 1;
71 // force resize on panel
72 panel1
[i
].area
.resize
= 1;
76 panel1
[i
].battery
.area
.resize
= 1;
80 void default_battery()
83 percentage_hide
= 101;
84 battery_low_cmd_send
= 0;
95 void cleanup_battery()
97 if (bat1_font_desc
) pango_font_description_free(bat1_font_desc
);
98 if (bat2_font_desc
) pango_font_description_free(bat2_font_desc
);
99 if (path_energy_now
) g_free(path_energy_now
);
100 if (path_energy_full
) g_free(path_energy_full
);
101 if (path_current_now
) g_free(path_current_now
);
102 if (path_status
) g_free(path_status
);
103 if (battery_low_cmd
) g_free(battery_low_cmd
);
111 GError
*error
= NULL
;
112 const char *entryname
;
113 char *battery_dir
= 0;
115 if (!battery_enabled
) return;
117 directory
= g_dir_open("/sys/class/power_supply", 0, &error
);
121 while ((entryname
=g_dir_read_name(directory
))) {
122 if (strncmp(entryname
,"AC", 2) == 0) continue;
124 char *path1
= g_build_filename("/sys/class/power_supply", entryname
, "present", NULL
);
125 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
127 battery_dir
= g_build_filename("/sys/class/power_supply", entryname
, NULL
);
134 g_dir_close(directory
);
137 fprintf(stderr
, "ERROR: battery applet can't found power_supply\n");
141 char *path1
= g_build_filename(battery_dir
, "energy_now", NULL
);
142 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
143 path_energy_now
= g_build_filename(battery_dir
, "energy_now", NULL
);
144 path_energy_full
= g_build_filename(battery_dir
, "energy_full", NULL
);
147 char *path2
= g_build_filename(battery_dir
, "charge_now", NULL
);
148 if (g_file_test (path2
, G_FILE_TEST_EXISTS
)) {
149 path_energy_now
= g_build_filename(battery_dir
, "charge_now", NULL
);
150 path_energy_full
= g_build_filename(battery_dir
, "charge_full", NULL
);
153 fprintf(stderr
, "ERROR: can't found energy_* or charge_*\n");
157 if (path_energy_now
&& path_energy_full
) {
158 path_current_now
= g_build_filename(battery_dir
, "current_now", NULL
);
159 path_status
= g_build_filename(battery_dir
, "status", NULL
);
162 FILE *fp1
, *fp2
, *fp3
, *fp4
;
163 fp1
= fopen(path_energy_now
, "r");
164 fp2
= fopen(path_energy_full
, "r");
165 fp3
= fopen(path_current_now
, "r");
166 fp4
= fopen(path_status
, "r");
167 if (fp1
== NULL
|| fp2
== NULL
|| fp3
== NULL
|| fp4
== NULL
) {
169 fprintf(stderr
, "ERROR: battery applet can't open energy_now\n");
180 if (battery_enabled
&& battery_timeout
==0)
181 battery_timeout
= add_timeout(10, 10000, update_batterys
, 0);
185 void init_battery_panel(void *p
)
187 Panel
*panel
= (Panel
*)p
;
188 Battery
*battery
= &panel
->battery
;
189 int bat_percentage_height
, bat_percentage_height_ink
, bat_time_height
, bat_time_height_ink
;
191 if (!battery_enabled
)
194 battery
->area
.parent
= p
;
195 battery
->area
.panel
= p
;
196 battery
->area
._draw_foreground
= draw_battery
;
197 battery
->area
._resize
= resize_battery
;
198 battery
->area
.resize
= 1;
199 battery
->area
.redraw
= 1;
200 battery
->area
.on_screen
= 1;
202 update_battery(&battery_state
);
203 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
204 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
206 get_text_size(bat1_font_desc
, &bat_percentage_height_ink
, &bat_percentage_height
, panel
->area
.height
, buf_bat_percentage
, strlen(buf_bat_percentage
));
207 get_text_size(bat2_font_desc
, &bat_time_height_ink
, &bat_time_height
, panel
->area
.height
, buf_bat_time
, strlen(buf_bat_time
));
209 if (panel_horizontal
) {
210 // panel horizonal => fixed height and posy
211 battery
->area
.posy
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
212 battery
->area
.height
= panel
->area
.height
- (2 * battery
->area
.posy
);
215 // panel vertical => fixed width, height, posy and posx
216 battery
->area
.posy
= panel
->clock
.area
.posy
+ panel
->clock
.area
.height
+ panel
->area
.paddingx
;
217 battery
->area
.height
= (2 * battery
->area
.paddingxlr
) + (bat_time_height
+ bat_percentage_height
);
218 battery
->area
.posx
= panel
->area
.bg
->border
.width
+ panel
->area
.paddingy
;
219 battery
->area
.width
= panel
->area
.width
- (2 * panel
->area
.bg
->border
.width
) - (2 * panel
->area
.paddingy
);
222 battery
->bat1_posy
= (battery
->area
.height
- bat_percentage_height
) / 2;
223 battery
->bat1_posy
-= ((bat_time_height_ink
+ 2) / 2);
224 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;
228 void update_battery() {
231 int64_t energy_now
= 0, energy_full
= 0, current_now
= 0;
233 int8_t new_percentage
= 0;
235 fp
= fopen(path_status
, "r");
237 if (fgets(tmp
, sizeof tmp
, fp
)) {
238 battery_state
.state
= BATTERY_UNKNOWN
;
239 if(strcasecmp(tmp
, "Charging\n")==0) battery_state
.state
= BATTERY_CHARGING
;
240 if(strcasecmp(tmp
, "Discharging\n")==0) battery_state
.state
= BATTERY_DISCHARGING
;
241 if(strcasecmp(tmp
, "Full\n")==0) battery_state
.state
= BATTERY_FULL
;
246 fp
= fopen(path_energy_now
, "r");
248 if (fgets(tmp
, sizeof tmp
, fp
)) energy_now
= atoi(tmp
);
252 fp
= fopen(path_energy_full
, "r");
254 if (fgets(tmp
, sizeof tmp
, fp
)) energy_full
= atoi(tmp
);
258 fp
= fopen(path_current_now
, "r");
260 if (fgets(tmp
, sizeof tmp
, fp
)) current_now
= atoi(tmp
);
264 if(current_now
> 0) {
265 switch(battery_state
.state
) {
266 case BATTERY_CHARGING
:
267 seconds
= 3600 * (energy_full
- energy_now
) / current_now
;
269 case BATTERY_DISCHARGING
:
270 seconds
= 3600 * energy_now
/ current_now
;
278 battery_state
.time
.hours
= seconds
/ 3600;
279 seconds
-= 3600 * battery_state
.time
.hours
;
280 battery_state
.time
.minutes
= seconds
/ 60;
281 seconds
-= 60 * battery_state
.time
.minutes
;
282 battery_state
.time
.seconds
= seconds
;
285 new_percentage
= (energy_now
*100)/energy_full
;
287 if(battery_low_status
> new_percentage
&& battery_state
.state
== BATTERY_DISCHARGING
&& !battery_low_cmd_send
) {
288 system(battery_low_cmd
); // return value == -1, since we've set SIGCHLD to SIGIGN
289 battery_low_cmd_send
= 1;
291 if(battery_low_status
< new_percentage
&& battery_state
.state
== BATTERY_CHARGING
&& battery_low_cmd_send
) {
292 battery_low_cmd_send
= 0;
295 battery_state
.percentage
= new_percentage
;
297 // clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
298 if(battery_state
.percentage
> 100) {
299 battery_state
.percentage
= 100;
304 void draw_battery (void *obj
, cairo_t
*c
)
306 Battery
*battery
= obj
;
309 layout
= pango_cairo_create_layout (c
);
312 pango_layout_set_font_description(layout
, bat1_font_desc
);
313 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
314 pango_layout_set_alignment(layout
, PANGO_ALIGN_CENTER
);
315 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
317 cairo_set_source_rgba(c
, battery
->font
.color
[0], battery
->font
.color
[1], battery
->font
.color
[2], battery
->font
.alpha
);
319 pango_cairo_update_layout(c
, layout
);
320 cairo_move_to(c
, 0, battery
->bat1_posy
);
321 pango_cairo_show_layout(c
, layout
);
323 pango_layout_set_font_description(layout
, bat2_font_desc
);
324 pango_layout_set_indent(layout
, 0);
325 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
326 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
328 pango_cairo_update_layout(c
, layout
);
329 cairo_move_to(c
, 0, battery
->bat2_posy
);
330 pango_cairo_show_layout(c
, layout
);
332 g_object_unref(layout
);
336 void resize_battery(void *obj
)
338 Battery
*battery
= obj
;
340 int percentage_width
, time_width
, new_width
;
342 percentage_width
= time_width
= 0;
343 battery
->area
.redraw
= 1;
345 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
346 if(battery_state
.state
== BATTERY_FULL
) {
347 strcpy(buf_bat_time
, "Full");
349 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
351 // vertical panel doen't adjust width
352 if (!panel_horizontal
) return;
357 pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, battery
->area
.width
, battery
->area
.height
, server
.depth
);
359 cs
= cairo_xlib_surface_create(server
.dsp
, pmap
, server
.visual
, battery
->area
.width
, battery
->area
.height
);
360 c
= cairo_create(cs
);
361 layout
= pango_cairo_create_layout(c
);
364 pango_layout_set_font_description(layout
, bat1_font_desc
);
365 pango_layout_set_indent(layout
, 0);
366 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
367 pango_layout_get_pixel_size(layout
, &percentage_width
, NULL
);
369 pango_layout_set_font_description(layout
, bat2_font_desc
);
370 pango_layout_set_indent(layout
, 0);
371 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
372 pango_layout_get_pixel_size(layout
, &time_width
, NULL
);
374 if(percentage_width
> time_width
) new_width
= percentage_width
;
375 else new_width
= time_width
;
377 new_width
+= (2*battery
->area
.paddingxlr
) + (2*battery
->area
.bg
->border
.width
);
379 int old_width
= battery
->area
.width
;
381 Panel
*panel
= ((Area
*)obj
)->panel
;
382 battery
->area
.width
= new_width
+ 1;
383 battery
->area
.posx
= panel
->area
.width
- battery
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.bg
->border
.width
;
384 if (panel
->clock
.area
.on_screen
)
385 battery
->area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
387 if(new_width
> old_width
|| new_width
< (old_width
-6)) {
388 // refresh and resize other objects on panel
389 // we try to limit the number of refresh
390 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
391 panel
->area
.resize
= 1;
392 systray
.area
.resize
= 1;
396 g_object_unref (layout
);
398 cairo_surface_destroy (cs
);
399 XFreePixmap (server
.dsp
, pmap
);