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