]> Dogcows Code - chaz/tint2/blob - src/battery/battery.c
cleanup default value on SIGUSR1
[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 #include "window.h"
28 #include "server.h"
29 #include "area.h"
30 #include "panel.h"
31 #include "taskbar.h"
32 #include "battery.h"
33 #include "clock.h"
34 #include "timer.h"
35
36 PangoFontDescription *bat1_font_desc=0;
37 PangoFontDescription *bat2_font_desc=0;
38 struct batstate battery_state;
39 int battery_enabled;
40 int percentage_hide;
41 static timeout* battery_timeout=0;
42
43 static char buf_bat_percentage[10];
44 static char buf_bat_time[20];
45
46 int8_t battery_low_status;
47 unsigned char battery_low_cmd_send;
48 char *battery_low_cmd=0;
49 char *path_energy_now=0;
50 char *path_energy_full=0;
51 char *path_current_now=0;
52 char *path_status=0;
53
54 void update_batterys(void* arg)
55 {
56 int i;
57 update_battery();
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;
64 panel_refresh = 1;
65 }
66 continue;
67 }
68 else {
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;
73 panel_refresh = 1;
74 }
75 }
76 panel1[i].battery.area.resize = 1;
77 }
78 }
79
80
81 void init_battery()
82 {
83 // check battery
84 GDir *directory = 0;
85 GError *error = NULL;
86 const char *entryname;
87 char *battery_dir = 0;
88
89 if (!battery_enabled) return;
90
91 directory = g_dir_open("/sys/class/power_supply", 0, &error);
92 if (error)
93 g_error_free(error);
94 else {
95 while ((entryname=g_dir_read_name(directory))) {
96 if (strncmp(entryname,"AC", 2) == 0) continue;
97
98 char *path1 = g_build_filename("/sys/class/power_supply", entryname, "present", NULL);
99 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
100 g_free(path1);
101 battery_dir = g_build_filename("/sys/class/power_supply", entryname, NULL);
102 break;
103 }
104 g_free(path1);
105 }
106 }
107 if (directory)
108 g_dir_close(directory);
109 if (!battery_dir) {
110 cleanup_battery();
111 fprintf(stderr, "ERROR: battery applet can't found power_supply\n");
112 return;
113 }
114
115 char *path1 = g_build_filename(battery_dir, "energy_now", NULL);
116 if (g_file_test (path1, G_FILE_TEST_EXISTS)) {
117 path_energy_now = g_build_filename(battery_dir, "energy_now", NULL);
118 path_energy_full = g_build_filename(battery_dir, "energy_full", NULL);
119 }
120 else {
121 char *path2 = g_build_filename(battery_dir, "charge_now", NULL);
122 if (g_file_test (path2, G_FILE_TEST_EXISTS)) {
123 path_energy_now = g_build_filename(battery_dir, "charge_now", NULL);
124 path_energy_full = g_build_filename(battery_dir, "charge_full", NULL);
125 }
126 else {
127 fprintf(stderr, "ERROR: can't found energy_* or charge_*\n");
128 }
129 g_free(path2);
130 }
131 if (path_energy_now && path_energy_full) {
132 path_current_now = g_build_filename(battery_dir, "current_now", NULL);
133 path_status = g_build_filename(battery_dir, "status", NULL);
134
135 // check file
136 FILE *fp1, *fp2, *fp3, *fp4;
137 fp1 = fopen(path_energy_now, "r");
138 fp2 = fopen(path_energy_full, "r");
139 fp3 = fopen(path_current_now, "r");
140 fp4 = fopen(path_status, "r");
141 if (fp1 == NULL || fp2 == NULL || fp3 == NULL || fp4 == NULL) {
142 cleanup_battery();
143 fprintf(stderr, "ERROR: battery applet can't open energy_now\n");
144 }
145 fclose(fp1);
146 fclose(fp2);
147 fclose(fp3);
148 fclose(fp4);
149 }
150
151 g_free(path1);
152 g_free(battery_dir);
153
154 if (battery_enabled && battery_timeout==0)
155 battery_timeout = add_timeout(10, 10000, update_batterys, 0);
156 }
157
158
159 void cleanup_battery()
160 {
161 battery_enabled = 0;
162 percentage_hide = 101;
163 battery_low_cmd_send = 0;
164
165 if (bat1_font_desc) {
166 pango_font_description_free(bat1_font_desc);
167 bat1_font_desc = 0;
168 }
169 if (bat2_font_desc) {
170 pango_font_description_free(bat2_font_desc);
171 bat2_font_desc = 0;
172 }
173 if (path_energy_now) {
174 g_free(path_energy_now);
175 path_energy_now = 0;
176 }
177 if (path_energy_full) {
178 g_free(path_energy_full);
179 path_energy_full = 0;
180 }
181 if (path_current_now) {
182 g_free(path_current_now);
183 path_current_now = 0;
184 }
185 if (path_status) {
186 g_free(path_status);
187 path_status = 0;
188 }
189 if (battery_low_cmd) {
190 g_free(battery_low_cmd);
191 battery_low_cmd = 0;
192 }
193 }
194
195
196 void init_battery_panel(void *p)
197 {
198 Panel *panel = (Panel*)p;
199 Battery *battery = &panel->battery;
200 int bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
201
202 if (!battery_enabled)
203 return;
204
205 battery->area.parent = p;
206 battery->area.panel = p;
207 battery->area._draw_foreground = draw_battery;
208 battery->area._resize = resize_battery;
209 battery->area.resize = 1;
210 battery->area.redraw = 1;
211 battery->area.on_screen = 1;
212
213 update_battery(&battery_state);
214 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
215 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
216
217 get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
218 get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
219
220 if (panel_horizontal) {
221 // panel horizonal => fixed height and posy
222 battery->area.posy = panel->area.bg->border.width + panel->area.paddingy;
223 battery->area.height = panel->area.height - (2 * battery->area.posy);
224 }
225 else {
226 // panel vertical => fixed width, height, posy and posx
227 battery->area.posy = panel->clock.area.posy + panel->clock.area.height + panel->area.paddingx;
228 battery->area.height = (2 * battery->area.paddingxlr) + (bat_time_height + bat_percentage_height);
229 battery->area.posx = panel->area.bg->border.width + panel->area.paddingy;
230 battery->area.width = panel->area.width - (2 * panel->area.bg->border.width) - (2 * panel->area.paddingy);
231 }
232
233 battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
234 battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
235 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;
236 }
237
238
239 void update_battery() {
240 FILE *fp;
241 char tmp[25];
242 int64_t energy_now = 0, energy_full = 0, current_now = 0;
243 int seconds = 0;
244 int8_t new_percentage = 0;
245
246 fp = fopen(path_status, "r");
247 if(fp != NULL) {
248 if (fgets(tmp, sizeof tmp, fp)) {
249 battery_state.state = BATTERY_UNKNOWN;
250 if(strcasecmp(tmp, "Charging\n")==0) battery_state.state = BATTERY_CHARGING;
251 if(strcasecmp(tmp, "Discharging\n")==0) battery_state.state = BATTERY_DISCHARGING;
252 if(strcasecmp(tmp, "Full\n")==0) battery_state.state = BATTERY_FULL;
253 }
254 fclose(fp);
255 }
256
257 fp = fopen(path_energy_now, "r");
258 if(fp != NULL) {
259 if (fgets(tmp, sizeof tmp, fp)) energy_now = atoi(tmp);
260 fclose(fp);
261 }
262
263 fp = fopen(path_energy_full, "r");
264 if(fp != NULL) {
265 if (fgets(tmp, sizeof tmp, fp)) energy_full = atoi(tmp);
266 fclose(fp);
267 }
268
269 fp = fopen(path_current_now, "r");
270 if(fp != NULL) {
271 if (fgets(tmp, sizeof tmp, fp)) current_now = atoi(tmp);
272 fclose(fp);
273 }
274
275 if(current_now > 0) {
276 switch(battery_state.state) {
277 case BATTERY_CHARGING:
278 seconds = 3600 * (energy_full - energy_now) / current_now;
279 break;
280 case BATTERY_DISCHARGING:
281 seconds = 3600 * energy_now / current_now;
282 break;
283 default:
284 seconds = 0;
285 break;
286 }
287 } else seconds = 0;
288
289 battery_state.time.hours = seconds / 3600;
290 seconds -= 3600 * battery_state.time.hours;
291 battery_state.time.minutes = seconds / 60;
292 seconds -= 60 * battery_state.time.minutes;
293 battery_state.time.seconds = seconds;
294
295 if(energy_full > 0)
296 new_percentage = (energy_now*100)/energy_full;
297
298 if(battery_low_status > new_percentage && battery_state.state == BATTERY_DISCHARGING && !battery_low_cmd_send) {
299 system(battery_low_cmd); // return value == -1, since we've set SIGCHLD to SIGIGN
300 battery_low_cmd_send = 1;
301 }
302 if(battery_low_status < new_percentage && battery_state.state == BATTERY_CHARGING && battery_low_cmd_send) {
303 battery_low_cmd_send = 0;
304 }
305
306 battery_state.percentage = new_percentage;
307
308 // clamp percentage to 100 in case battery is misreporting that its current charge is more than its max
309 if(battery_state.percentage > 100) {
310 battery_state.percentage = 100;
311 }
312 }
313
314
315 void draw_battery (void *obj, cairo_t *c)
316 {
317 Battery *battery = obj;
318 PangoLayout *layout;
319
320 layout = pango_cairo_create_layout (c);
321
322 // draw layout
323 pango_layout_set_font_description(layout, bat1_font_desc);
324 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
325 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
326 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
327
328 cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);
329
330 pango_cairo_update_layout(c, layout);
331 cairo_move_to(c, 0, battery->bat1_posy);
332 pango_cairo_show_layout(c, layout);
333
334 pango_layout_set_font_description(layout, bat2_font_desc);
335 pango_layout_set_indent(layout, 0);
336 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
337 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
338
339 pango_cairo_update_layout(c, layout);
340 cairo_move_to(c, 0, battery->bat2_posy);
341 pango_cairo_show_layout(c, layout);
342
343 g_object_unref(layout);
344 }
345
346
347 void resize_battery(void *obj)
348 {
349 Battery *battery = obj;
350 PangoLayout *layout;
351 int percentage_width, time_width, new_width;
352
353 percentage_width = time_width = 0;
354 battery->area.redraw = 1;
355
356 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
357 if(battery_state.state == BATTERY_FULL) {
358 strcpy(buf_bat_time, "Full");
359 } else {
360 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
361 }
362 // vertical panel doen't adjust width
363 if (!panel_horizontal) return;
364
365 cairo_surface_t *cs;
366 cairo_t *c;
367 Pixmap pmap;
368 pmap = XCreatePixmap(server.dsp, server.root_win, battery->area.width, battery->area.height, server.depth);
369
370 cs = cairo_xlib_surface_create(server.dsp, pmap, server.visual, battery->area.width, battery->area.height);
371 c = cairo_create(cs);
372 layout = pango_cairo_create_layout(c);
373
374 // check width
375 pango_layout_set_font_description(layout, bat1_font_desc);
376 pango_layout_set_indent(layout, 0);
377 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
378 pango_layout_get_pixel_size(layout, &percentage_width, NULL);
379
380 pango_layout_set_font_description(layout, bat2_font_desc);
381 pango_layout_set_indent(layout, 0);
382 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
383 pango_layout_get_pixel_size(layout, &time_width, NULL);
384
385 if(percentage_width > time_width) new_width = percentage_width;
386 else new_width = time_width;
387
388 new_width += (2*battery->area.paddingxlr) + (2*battery->area.bg->border.width);
389
390 int old_width = battery->area.width;
391
392 Panel *panel = ((Area*)obj)->panel;
393 battery->area.width = new_width + 1;
394 battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.bg->border.width;
395 if (panel->clock.area.on_screen)
396 battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
397
398 if(new_width > old_width || new_width < (old_width-6)) {
399 // refresh and resize other objects on panel
400 // we try to limit the number of refresh
401 // printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
402 panel->area.resize = 1;
403 systray.area.resize = 1;
404 panel_refresh = 1;
405 }
406
407 g_object_unref (layout);
408 cairo_destroy (c);
409 cairo_surface_destroy (cs);
410 XFreePixmap (server.dsp, pmap);
411 }
412
This page took 0.055497 seconds and 5 git commands to generate.