]> Dogcows Code - chaz/tint2/blob - src/battery/battery.c
added battery code
[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
45 void update_battery(struct batstate *data) {
46 FILE *fp;
47 char tmp[25];
48 int64_t energy_now = 0, energy_full = 0, current_now = 0;
49 int seconds = 0;
50 int8_t new_percentage = 0;
51
52 fp = fopen("/sys/class/power_supply/BAT0/energy_now", "r");
53 if(fp != NULL) {
54 fgets(tmp, sizeof tmp, fp);
55 energy_now = atoi(tmp);
56 fclose(fp);
57 } else printf("ERROR: battery applet can't open energy_now\n");
58
59 fp = fopen("/sys/class/power_supply/BAT0/energy_full", "r");
60 if(fp != NULL) {
61 fgets(tmp, sizeof tmp, fp);
62 energy_full = atoi(tmp);
63 fclose(fp);
64 } else printf("ERROR: battery applet can't open energy_full\n");
65
66 fp = fopen("/sys/class/power_supply/BAT0/current_now", "r");
67 if(fp != NULL) {
68 fgets(tmp, sizeof tmp, fp);
69 current_now = atoi(tmp);
70 fclose(fp);
71 } else printf("ERROR: battery applet can't open current_now\n");
72
73 fp = fopen("/sys/class/power_supply/BAT0/status", "r");
74 if(fp != NULL) {
75 fgets(tmp, sizeof tmp, fp);
76 fclose(fp);
77 } else printf("ERROR: battery applet can't open status");
78
79 data->state = BATTERY_UNKNOWN;
80 if(strcasecmp(tmp, "Charging\n")==0) data->state = BATTERY_CHARGING;
81 if(strcasecmp(tmp, "Discharging\n")==0) data->state = BATTERY_DISCHARGING;
82
83 if(current_now > 0) {
84 switch(data->state) {
85 case BATTERY_CHARGING:
86 seconds = 3600 * (energy_full - energy_now) / current_now;
87 break;
88 case BATTERY_DISCHARGING:
89 seconds = 3600 * energy_now / current_now;
90 break;
91 default:
92 seconds = 0;
93 break;
94 }
95 } else seconds = 0;
96
97 data->time.hours = seconds / 3600;
98 seconds -= 3600 * data->time.hours;
99 data->time.minutes = seconds / 60;
100 seconds -= 60 * data->time.minutes;
101 data->time.seconds = seconds;
102
103 new_percentage = (energy_now*100)/energy_full;
104
105 if(battery_low_status != 0 && battery_low_status == new_percentage && data->percentage > new_percentage) {
106 printf("battery low, executing: %s\n", battery_low_cmd);
107 if(battery_low_cmd) system(battery_low_cmd);
108 }
109
110 data->percentage = new_percentage;
111 }
112
113 void init_battery()
114 {
115 Panel *panel;
116 Battery *battery;
117 int i, bat_percentage_height, bat_percentage_height_ink, bat_time_height, bat_time_height_ink;
118
119 for (i=0 ; i < nb_panel ; i++) {
120 panel = &panel1[i];
121 battery = &panel->battery;
122
123 battery->area.parent = panel;
124 battery->area.panel = panel;
125 battery->area._draw_foreground = draw_battery;
126 battery->area._resize = resize_battery;
127
128 if (!battery->area.on_screen) continue;
129
130 battery->area.posy = panel->area.pix.border.width + panel->area.paddingy;
131 battery->area.height = panel->area.height - (2 * battery->area.posy);
132 battery->area.resize = 1;
133 battery->area.redraw = 1;
134
135 update_battery(&battery_state);
136 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
137 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
138
139 get_text_size(bat1_font_desc, &bat_percentage_height_ink, &bat_percentage_height, panel->area.height, buf_bat_percentage, strlen(buf_bat_percentage));
140 battery->bat1_posy = (battery->area.height - bat_percentage_height) / 2;
141
142 get_text_size(bat2_font_desc, &bat_time_height_ink, &bat_time_height, panel->area.height, buf_bat_time, strlen(buf_bat_time));
143
144 battery->bat1_posy -= ((bat_time_height_ink + 2) / 2);
145 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;
146 }
147 }
148
149
150 void draw_battery (void *obj, cairo_t *c, int active)
151 {
152 Battery *battery = obj;
153 PangoLayout *layout;
154
155 layout = pango_cairo_create_layout (c);
156
157 // draw layout
158 pango_layout_set_font_description(layout, bat1_font_desc);
159 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
160 pango_layout_set_alignment(layout, PANGO_ALIGN_CENTER);
161 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
162
163 cairo_set_source_rgba(c, battery->font.color[0], battery->font.color[1], battery->font.color[2], battery->font.alpha);
164
165 pango_cairo_update_layout(c, layout);
166 cairo_move_to(c, 0, battery->bat1_posy);
167 pango_cairo_show_layout(c, layout);
168
169 pango_layout_set_font_description(layout, bat2_font_desc);
170 pango_layout_set_indent(layout, 0);
171 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
172 pango_layout_set_width(layout, battery->area.width * PANGO_SCALE);
173
174 pango_cairo_update_layout(c, layout);
175 cairo_move_to(c, 0, battery->bat2_posy);
176 pango_cairo_show_layout(c, layout);
177
178 g_object_unref(layout);
179 }
180
181 void resize_battery(void *obj)
182 {
183 Battery *battery = obj;
184 PangoLayout *layout;
185 int percentage_width, time_width, new_width;
186
187 percentage_width = time_width = 0;
188 battery->area.redraw = 1;
189
190 snprintf(buf_bat_percentage, sizeof(buf_bat_percentage), "%d%%", battery_state.percentage);
191 snprintf(buf_bat_time, sizeof(buf_bat_time), "%02d:%02d", battery_state.time.hours, battery_state.time.minutes);
192
193 cairo_surface_t *cs;
194 cairo_t *c;
195 Pixmap pmap;
196 pmap = XCreatePixmap(server.dsp, server.root_win, battery->area.width, battery->area.height, server.depth);
197
198 cs = cairo_xlib_surface_create(server.dsp, pmap, server.visual, battery->area.width, battery->area.height);
199 c = cairo_create(cs);
200 layout = pango_cairo_create_layout(c);
201
202 // check width
203 pango_layout_set_font_description(layout, bat1_font_desc);
204 pango_layout_set_indent(layout, 0);
205 pango_layout_set_text(layout, buf_bat_percentage, strlen(buf_bat_percentage));
206 pango_layout_get_pixel_size(layout, &percentage_width, NULL);
207
208 pango_layout_set_font_description(layout, bat2_font_desc);
209 pango_layout_set_indent(layout, 0);
210 pango_layout_set_text(layout, buf_bat_time, strlen(buf_bat_time));
211 pango_layout_get_pixel_size(layout, &time_width, NULL);
212
213 if(percentage_width > time_width) new_width = percentage_width;
214 else new_width = time_width;
215
216 new_width += (2*battery->area.paddingxlr) + (2*battery->area.pix.border.width);
217
218 if(new_width > battery->area.width || new_width < (battery->area.width-6)) {
219 int i;
220 Panel *panel = ((Area*)obj)->panel;
221
222 printf("battery_width %d, new_width %d\n", battery->area.width, new_width);
223
224 // resize battery
225 // we try to limit the number of resize
226 battery->area.width = new_width + 1;
227 battery->area.posx = panel->area.width - battery->area.width - panel->area.paddingxlr - panel->area.pix.border.width;
228 if (panel->clock.area.on_screen)
229 battery->area.posx -= (panel->clock.area.width + panel->area.paddingx);
230
231 // resize other objects on panel
232 for (i=0 ; i < nb_panel ; i++)
233 panel1[i].area.resize = 1;
234
235 systray.area.resize = 1;
236 panel_refresh = 1;
237 }
238
239 g_object_unref (layout);
240 cairo_destroy (c);
241 cairo_surface_destroy (cs);
242 XFreePixmap (server.dsp, pmap);
243 }
244
This page took 0.050336 seconds and 5 git commands to generate.