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>
35 PangoFontDescription
*bat1_font_desc
;
36 PangoFontDescription
*bat2_font_desc
;
37 struct batstate battery_state
;
39 static char buf_bat_percentage
[10];
40 static char buf_bat_time
[20];
42 int8_t battery_low_status
;
43 char *battery_low_cmd
;
44 char *path_energy_now
, *path_energy_full
, *path_current_now
, *path_status
;
52 const char *entryname
;
53 char *battery_dir
= 0;
55 path_energy_now
= path_energy_full
= path_current_now
= path_status
= 0;
56 directory
= g_dir_open("/sys/class/power_supply", 0, &error
);
60 while ((entryname
=g_dir_read_name(directory
))) {
61 if (strncmp(entryname
,"AC", 2) == 0) continue;
63 char *path1
= g_build_filename("/sys/class/power_supply", entryname
, "present", NULL
);
64 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
66 battery_dir
= g_build_filename("/sys/class/power_supply", entryname
, NULL
);
72 if (battery_dir
!= 0) {
73 char *path1
= g_build_filename(battery_dir
, "energy_now", NULL
);
74 if (g_file_test (path1
, G_FILE_TEST_EXISTS
)) {
75 path_energy_now
= g_build_filename(battery_dir
, "energy_now", NULL
);
76 path_energy_full
= g_build_filename(battery_dir
, "energy_full", NULL
);
79 char *path2
= g_build_filename(battery_dir
, "charge_now", NULL
);
80 if (g_file_test (path2
, G_FILE_TEST_EXISTS
)) {
81 path_energy_now
= g_build_filename(battery_dir
, "charge_now", NULL
);
82 path_energy_full
= g_build_filename(battery_dir
, "charge_full", NULL
);
87 fprintf(stderr
, "ERROR: can't found energy_* or charge_*\n");
91 path_current_now
= g_build_filename(battery_dir
, "current_now", NULL
);
92 path_status
= g_build_filename(battery_dir
, "status", NULL
);
99 int i
, bat_percentage_height
, bat_percentage_height_ink
, bat_time_height
, bat_time_height_ink
;
101 for (i
=0 ; i
< nb_panel
; i
++) {
103 battery
= &panel
->battery
;
105 if (battery_dir
== 0) battery
->area
.on_screen
= 0;
106 if (!battery
->area
.on_screen
) continue;
108 battery
->area
.parent
= panel
;
109 battery
->area
.panel
= panel
;
110 battery
->area
._draw_foreground
= draw_battery
;
111 battery
->area
._resize
= resize_battery
;
112 battery
->area
.resize
= 1;
113 battery
->area
.redraw
= 1;
115 if((fp
= fopen(path_energy_now
, "r")) == NULL
) {
116 fprintf(stderr
, "ERROR: battery applet can't open energy_now\n");
117 panel
->battery
.area
.on_screen
= 0;
121 if((fp
= fopen(path_energy_full
, "r")) == NULL
) {
122 fprintf(stderr
, "ERROR: battery applet can't open energy_full\n");
123 panel
->battery
.area
.on_screen
= 0;
127 if((fp
= fopen(path_current_now
, "r")) == NULL
) {
128 fprintf(stderr
, "ERROR: battery applet can't open current_now\n");
129 panel
->battery
.area
.on_screen
= 0;
133 if((fp
= fopen(path_status
, "r")) == NULL
) {
134 fprintf(stderr
, "ERROR: battery applet can't open status");
135 panel
->battery
.area
.on_screen
= 0;
140 update_battery(&battery_state
);
141 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
142 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
144 get_text_size(bat1_font_desc
, &bat_percentage_height_ink
, &bat_percentage_height
, panel
->area
.height
, buf_bat_percentage
, strlen(buf_bat_percentage
));
145 get_text_size(bat2_font_desc
, &bat_time_height_ink
, &bat_time_height
, panel
->area
.height
, buf_bat_time
, strlen(buf_bat_time
));
147 if (panel_horizontal
) {
148 // panel horizonal => fixed height and posy
149 battery
->area
.posy
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
150 battery
->area
.height
= panel
->area
.height
- (2 * battery
->area
.posy
);
153 // panel vertical => fixed width, height, posy and posx
154 battery
->area
.posy
= panel
->clock
.area
.posy
+ panel
->clock
.area
.height
+ panel
->area
.paddingx
;
155 battery
->area
.height
= (2 * battery
->area
.paddingxlr
) + (bat_time_height
+ bat_percentage_height
);
156 battery
->area
.posx
= panel
->area
.pix
.border
.width
+ panel
->area
.paddingy
;
157 battery
->area
.width
= panel
->area
.width
- (2 * panel
->area
.pix
.border
.width
) - (2 * panel
->area
.paddingy
);
160 battery
->bat1_posy
= (battery
->area
.height
- bat_percentage_height
) / 2;
161 battery
->bat1_posy
-= ((bat_time_height_ink
+ 2) / 2);
162 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;
167 void update_battery() {
170 int64_t energy_now
= 0, energy_full
= 0, current_now
= 0;
172 int8_t new_percentage
= 0;
174 fp
= fopen(path_energy_now
, "r");
176 fgets(tmp
, sizeof tmp
, fp
);
177 energy_now
= atoi(tmp
);
181 fp
= fopen(path_energy_full
, "r");
183 fgets(tmp
, sizeof tmp
, fp
);
184 energy_full
= atoi(tmp
);
188 fp
= fopen(path_current_now
, "r");
190 fgets(tmp
, sizeof tmp
, fp
);
191 current_now
= atoi(tmp
);
195 fp
= fopen(path_status
, "r");
197 fgets(tmp
, sizeof tmp
, fp
);
201 battery_state
.state
= BATTERY_UNKNOWN
;
202 if(strcasecmp(tmp
, "Charging\n")==0) battery_state
.state
= BATTERY_CHARGING
;
203 if(strcasecmp(tmp
, "Discharging\n")==0) battery_state
.state
= BATTERY_DISCHARGING
;
205 if(current_now
> 0) {
206 switch(battery_state
.state
) {
207 case BATTERY_CHARGING
:
208 seconds
= 3600 * (energy_full
- energy_now
) / current_now
;
210 case BATTERY_DISCHARGING
:
211 seconds
= 3600 * energy_now
/ current_now
;
219 battery_state
.time
.hours
= seconds
/ 3600;
220 seconds
-= 3600 * battery_state
.time
.hours
;
221 battery_state
.time
.minutes
= seconds
/ 60;
222 seconds
-= 60 * battery_state
.time
.minutes
;
223 battery_state
.time
.seconds
= seconds
;
226 new_percentage
= (energy_now
*100)/energy_full
;
228 if(battery_low_status
!= 0 && battery_low_status
== new_percentage
&& battery_state
.percentage
> new_percentage
) {
229 //printf("battery low, executing: %s\n", battery_low_cmd);
230 if (battery_low_cmd
) system(battery_low_cmd
);
233 battery_state
.percentage
= new_percentage
;
237 void draw_battery (void *obj
, cairo_t
*c
, int active
)
239 Battery
*battery
= obj
;
242 layout
= pango_cairo_create_layout (c
);
245 pango_layout_set_font_description(layout
, bat1_font_desc
);
246 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
247 pango_layout_set_alignment(layout
, PANGO_ALIGN_CENTER
);
248 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
250 cairo_set_source_rgba(c
, battery
->font
.color
[0], battery
->font
.color
[1], battery
->font
.color
[2], battery
->font
.alpha
);
252 pango_cairo_update_layout(c
, layout
);
253 cairo_move_to(c
, 0, battery
->bat1_posy
);
254 pango_cairo_show_layout(c
, layout
);
256 pango_layout_set_font_description(layout
, bat2_font_desc
);
257 pango_layout_set_indent(layout
, 0);
258 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
259 pango_layout_set_width(layout
, battery
->area
.width
* PANGO_SCALE
);
261 pango_cairo_update_layout(c
, layout
);
262 cairo_move_to(c
, 0, battery
->bat2_posy
);
263 pango_cairo_show_layout(c
, layout
);
265 g_object_unref(layout
);
269 void resize_battery(void *obj
)
271 Battery
*battery
= obj
;
273 int percentage_width
, time_width
, new_width
;
275 percentage_width
= time_width
= 0;
276 battery
->area
.redraw
= 1;
278 snprintf(buf_bat_percentage
, sizeof(buf_bat_percentage
), "%d%%", battery_state
.percentage
);
279 snprintf(buf_bat_time
, sizeof(buf_bat_time
), "%02d:%02d", battery_state
.time
.hours
, battery_state
.time
.minutes
);
280 // vertical panel doen't adjust width
281 if (!panel_horizontal
) return;
286 pmap
= XCreatePixmap(server
.dsp
, server
.root_win
, battery
->area
.width
, battery
->area
.height
, server
.depth
);
288 cs
= cairo_xlib_surface_create(server
.dsp
, pmap
, server
.visual
, battery
->area
.width
, battery
->area
.height
);
289 c
= cairo_create(cs
);
290 layout
= pango_cairo_create_layout(c
);
293 pango_layout_set_font_description(layout
, bat1_font_desc
);
294 pango_layout_set_indent(layout
, 0);
295 pango_layout_set_text(layout
, buf_bat_percentage
, strlen(buf_bat_percentage
));
296 pango_layout_get_pixel_size(layout
, &percentage_width
, NULL
);
298 pango_layout_set_font_description(layout
, bat2_font_desc
);
299 pango_layout_set_indent(layout
, 0);
300 pango_layout_set_text(layout
, buf_bat_time
, strlen(buf_bat_time
));
301 pango_layout_get_pixel_size(layout
, &time_width
, NULL
);
303 if(percentage_width
> time_width
) new_width
= percentage_width
;
304 else new_width
= time_width
;
306 new_width
+= (2*battery
->area
.paddingxlr
) + (2*battery
->area
.pix
.border
.width
);
308 int old_width
= battery
->area
.width
;
310 Panel
*panel
= ((Area
*)obj
)->panel
;
311 battery
->area
.width
= new_width
+ 1;
312 battery
->area
.posx
= panel
->area
.width
- battery
->area
.width
- panel
->area
.paddingxlr
- panel
->area
.pix
.border
.width
;
313 if (panel
->clock
.area
.on_screen
)
314 battery
->area
.posx
-= (panel
->clock
.area
.width
+ panel
->area
.paddingx
);
316 if(new_width
> old_width
|| new_width
< (old_width
-6)) {
317 // refresh and resize other objects on panel
318 // we try to limit the number of refresh
319 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
320 panel
->area
.resize
= 1;
321 systray
.area
.resize
= 1;
325 g_object_unref (layout
);
327 cairo_surface_destroy (cs
);
328 XFreePixmap (server
.dsp
, pmap
);