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