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