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