2 * display outputs, per line:
4 * Remaining: | Empty: | Degraded:
5 * blue | black | dimgrey discharging
6 * green | black | dimgrey charging
7 * cyan | black | dimgrey charged
8 * grey | black | dimgrey charging&discharching!
9 * blue | red | dimgrey discharging - low!
10 * green | red | dimgrey charging - low
11 * cyan | red | dimgrey charged - low [1]
12 * grey | red | dimgrey charging&discharching, low [1]
13 * ... darkmagenta ... no batteries present
15 * [1] battery must be quite badly degraded
27 #include <sys/types.h>
32 /*---------- general utility stuff and declarations ----------*/
34 static void fail(const char *m) {
35 fprintf(stderr,"error: %s\n", m);
38 static void badusage(void) { fail("bad usage"); }
40 #define CHGST_DISCHARGING 0 /* Reflects order in E(state,charging_state) */
41 #define CHGST_CHARGING 1 /* in fields table. Also, much code assumes */
42 #define CHGST_CHARGED 2 /* exactly these three possible states. */
44 /*---------- structure of and results from /proc/acpi/battery/... ----------*/
45 /* variables thisbat_... are the results from readbattery();
46 * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
49 typedef struct batinfo_field {
52 unsigned long *valuep;
54 const char *enumarray[10];
57 #define QUANTITY_FIELDS \
58 QF(info, design_capacity, "mWh") \
59 QF(info, last_full_capacity, "mWh") \
60 QF(state, present_rate, "mW") \
61 QF(state, remaining_capacity, "mWh") \
62 QF(alarm, alarm, "mWh")
64 #define QF(f,l,u) static unsigned long thisbat_##f##_##l;
68 static unsigned long thisbat_info_present, thisbat_state_present;
69 static unsigned long thisbat_state_charging_state;
71 #define VAL_NOTFOUND (~0UL)
73 static const batinfo_field fields[]= {
74 #define E(f,l) #f, #l, &thisbat_##f##_##l, 0
75 #define QF(f,l,u) { #f, #l, &thisbat_##f##_##l, u },
76 { E(info, present), { "no", "yes" } },
77 { E(state,present), { "no", "yes" } },
78 { E(state,charging_state), { "discharging", "charging", "charged" } },
79 QUANTITY_FIELDS /* take care re charging_state values order - */
80 { 0 } /* if you must change it, search for CHGST_... */
85 static const char *files[]= { "info", "state", "alarm" };
87 /*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
89 /* variables private to the parser and its error handlers */
90 static char batlinebuf[1000];
92 static const char *batdirname;
93 static const char *batfilename;
94 static const char *batlinevalue;
96 static int batfailf(const char *why) {
98 fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
99 batdirname,batfilename, batlinebuf,batlinevalue,why);
101 fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
102 batdirname,batfilename, why, batlinebuf);
107 static int batfaile(const char *syscall, const char *target) {
108 fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
109 batdirname ? batdirname : "*", syscall, target, strerror(errno));
113 static void chdir_base(void) {
116 r= chdir("/proc/acpi/battery");
117 if (r) batfaile("chdir","/proc/acpi/battery");
120 static void tidybattery(void) {
121 if (batfile) { fclose(batfile); batfile=0; }
122 if (batdirname) { chdir_base(); batdirname=0; }
125 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
126 const batinfo_field *field;
127 const char *const *cfilename, *const *enumsearch;
128 char *colon, *ep, *sr, *p;
131 r= chdir(batdirname);
132 if (r) return batfaile("chdir",batdirname);
134 for (field=fields; field->file; field++)
135 *field->valuep= VAL_NOTFOUND;
137 for (cfilename=files;
138 (batfilename= *cfilename);
140 batfile= fopen(batfilename,"r");
141 if (!batfile) return batfaile("open",batfilename);
146 sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
147 if (ferror(batfile)) return batfaile("read",batfilename);
148 if (!sr && feof(batfile)) break;
149 l= strlen(batlinebuf);
151 if (batlinebuf[l-1] != '\n')
152 return batfailf("line too long");
154 colon= strchr(batlinebuf,':');
156 return batfailf("line without a colon");
159 for (p=batlinebuf; p<colon; p++)
163 for (field=fields; field->file; field++) {
164 if (!strcmp(field->file,batfilename) &&
165 !strcmp(field->label,batlinebuf))
166 goto label_interesting;
171 for (batlinevalue= colon+1;
172 *batlinevalue && isspace((unsigned char)*batlinevalue);
177 *field->valuep= strtoul(batlinevalue,&ep,10);
178 if (ep==batlinevalue || *ep!=' ')
179 batfailf("value number syntax incorrect");
180 if (strcmp(ep+1,field->unit)) batfailf("incorrect unit");
184 for (*field->valuep=0, enumsearch=field->enumarray;
185 *enumsearch && strcmp(*enumsearch,batlinevalue);
186 (*field->valuep)++, enumsearch++);
188 batfailf("unknown enum value");
198 if (r) return batfaile("chdir","..");
201 for (field=fields, missing=0;
204 if (*field->valuep == VAL_NOTFOUND) {
205 fprintf(stderr,"battery/%s/%s: %s: not found\n",
206 batdirname, field->file,field->label);
210 if (missing) return -1;
215 /*---------- data collection and analysis ----------*/
217 /* These next three variables are the results of the charging state */
218 static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
219 static double nondegraded_norm, fill_norm, ratepersec_norm;
220 static int alarm_level; /* 0=ok, 1=low */
223 static double total_##f##_##l;
227 static void acquiredata(void) {
232 charging_state_mask= 0;
239 di= opendir("."); if (!di) batfaile("opendir","battery");
240 while ((de= readdir(di))) {
241 if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
243 batdirname= de->d_name;
249 if (!thisbat_info_present || !thisbat_state_present) {
250 printf("battery/%s not present\n",de->d_name);
254 charging_state_mask |= 1u << thisbat_state_charging_state;
257 total_##f##_##l += thisbat_##f##_##l;
261 if (thisbat_state_charging_state == CHGST_DISCHARGING)
263 total_state_present_rate -= 2.0 * thisbat_state_present_rate;
268 if (total_info_design_capacity < 0.5)
269 total_info_design_capacity= 1.0;
271 if (total_info_last_full_capacity < total_state_remaining_capacity)
272 total_info_last_full_capacity= total_state_remaining_capacity;
273 if (total_info_design_capacity < total_info_last_full_capacity)
274 total_info_design_capacity= total_info_last_full_capacity;
277 (total_state_remaining_capacity < total_alarm_alarm &&
278 !(charging_state_mask & 1u << CHGST_CHARGING));
280 nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
281 fill_norm= total_state_remaining_capacity / total_info_design_capacity;
282 ratepersec_norm= total_state_present_rate
283 / (3600.0 * total_info_design_capacity);
286 static void initacquire(void) {
290 /*---------- display ----------*/
307 static Display *disp;
309 static int width, height;
310 static Colormap cmap;
312 static unsigned long lastbackground;
316 unsigned long lastfg;
319 #define C(c) static unsigned long pix_##c;
320 #define GC(g) static Gcstate gc_##g;
325 static void refresh(void);
327 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
329 static void failr(const char *m, int r) {
330 fprintf(stderr,"error: %s (code %d)\n", m, r);
334 static void setbackground(unsigned long newbg) {
337 if (newbg == lastbackground) return;
338 r= XSetWindowBackground(disp,win,newbg);
339 if (r) failr("XSetWindowBackground",r);
340 lastbackground= newbg;
343 static void setforeground(Gcstate *g, unsigned long px) {
347 if (g->lastfg == px) return;
349 memset(&gcv,0,sizeof(gcv));
350 g->lastfg= gcv.foreground= px;
351 r= XChangeGC(disp,g->gc,GCForeground,&gcv);
352 if (!r) fail("XChangeGC");
355 static void show(void) {
357 int i, leftmost_lit, leftmost_nondeg;
359 if (!charging_state_mask) {
360 setbackground(pix_darkmagenta);
361 XClearWindow(disp,win);
365 setbackground(pix_dimgrey);
366 XClearWindow(disp,win);
368 setforeground(&gc_remain,
369 !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
370 !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
371 charging_state_mask & (1u<<CHGST_CHARGING)
372 ? pix_green : pix_blue);
374 setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
376 for (i=0; i<height; i++) {
379 TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
381 then= fill_norm + ratepersec_norm * elap;
382 if (then <= 0.0) then= 0.0;
383 else if (then >= nondegraded_norm) then= nondegraded_norm;
385 leftmost_lit= width * then;
386 leftmost_nondeg= width * nondegraded_norm;
388 if (leftmost_lit < leftmost_nondeg)
389 XDrawLine(disp, win, gc_empty.gc,
390 leftmost_lit,i, leftmost_nondeg,i);
391 if (leftmost_lit >= 0)
392 XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
396 static void initgc(Gcstate *gc_r) {
399 memset(&gcv,0,sizeof(gcv));
400 gcv.function= GXcopy;
402 gc_r->lastfg= gcv.foreground= pix_dimgrey;
403 gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
406 static void colour(unsigned long *pix_r, const char *name) {
410 st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
416 static void initgraphics(void) {
419 disp= XOpenDisplay(0); if (!disp) fail("could not open display");
421 screen= DefaultScreen(disp);
422 win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),0,0,100,20,0,0,0);
423 cmap= DefaultColormap(disp,screen);
425 #define C(c) colour(&pix_##c, #c);
426 #define GC(g) initgc(&gc_##g);
431 r= XSetWindowBackground(disp,win,pix_dimgrey);
432 if (!r) fail("init set background");
433 lastbackground= pix_dimgrey;
435 XSelectInput(disp,win, ExposureMask|VisibilityChangeMask);
436 XMapWindow(disp,win);
439 static void refresh(void) {
444 static void newgeometry(void) {
448 XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
452 static void eventloop(void) {
458 XNextEvent(disp,&ev);
463 int main(int argc, const char *const *argv) {
464 if (!argv[0] || argv[1])