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