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