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 * ... darkgreen ... no batteries present
15 * [1] battery must be quite badly degraded
28 #include <sys/types.h>
36 #define TIMEOUT 5000 /* milliseconds */
38 /*---------- general utility stuff and declarations ----------*/
40 static void fail(const char *m) {
41 fprintf(stderr,"error: %s\n", m);
44 static void badusage(void) { fail("bad usage"); }
46 #define CHGST_DISCHARGING 0 /* Reflects order in E(state,charging_state) */
47 #define CHGST_CHARGING 1 /* in fields table. Also, much code assumes */
48 #define CHGST_CHARGED 2 /* exactly these three possible states. */
50 /*---------- structure of and results from /proc/acpi/battery/... ----------*/
51 /* variables thisbat_... are the results from readbattery();
52 * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
55 typedef struct batinfo_field {
58 unsigned long *valuep;
60 const char *enumarray[10];
63 #define QUANTITY_FIELDS \
64 QF(info, design_capacity, "mWh") \
65 QF(info, last_full_capacity, "mWh") \
66 QF(state, present_rate, "mW") \
67 QF(state, remaining_capacity, "mWh") \
68 QF(alarm, alarm, "mWh")
70 #define QF(f,l,u) static unsigned long thisbat_##f##_##l;
74 static unsigned long thisbat_alarm_present, thisbat_info_present;
75 static unsigned long thisbat_state_present, thisbat_state_charging_state;
77 #define VAL_NOTFOUND (~0UL)
79 static const batinfo_field fields[]= {
80 #define E(f,l) #f, #l, &thisbat_##f##_##l, 0
81 #define QF(f,l,u) { #f, #l, &thisbat_##f##_##l, u },
82 { E(alarm, present), { "no", "yes" } },
83 { E(info, present), { "no", "yes" } },
84 { E(state,present), { "no", "yes" } },
85 { E(state,charging_state), { "discharging", "charging", "charged" } },
86 QUANTITY_FIELDS /* take care re charging_state values order - */
87 { 0 } /* if you must change it, search for CHGST_... */
92 static const char *files[]= { "info", "state", "alarm" };
94 /*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
96 /* variables private to the parser and its error handlers */
97 static char batlinebuf[1000];
99 static const char *batdirname;
100 static const char *batfilename;
101 static const char *batlinevalue;
103 static int batfailf(const char *why) {
105 fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
106 batdirname,batfilename, batlinebuf,batlinevalue,why);
108 fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
109 batdirname,batfilename, why, batlinebuf);
114 static int batfaile(const char *syscall, const char *target) {
115 fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
116 batdirname ? batdirname : "*", syscall, target, strerror(errno));
120 static void chdir_base(void) {
123 r= chdir("/proc/acpi/battery");
124 if (r) batfaile("chdir","/proc/acpi/battery");
127 static void tidybattery(void) {
128 if (batfile) { fclose(batfile); batfile=0; }
129 if (batdirname) { chdir_base(); batdirname=0; }
132 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
133 const batinfo_field *field;
134 const char *const *cfilename, *const *enumsearch;
135 char *colon, *ep, *sr, *p;
138 r= chdir(batdirname);
139 if (r) return batfaile("chdir",batdirname);
141 for (field=fields; field->file; field++)
142 *field->valuep= VAL_NOTFOUND;
144 for (cfilename=files;
145 (batfilename= *cfilename);
147 batfile= fopen(batfilename,"r");
148 if (!batfile) return batfaile("open",batfilename);
153 sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
154 if (ferror(batfile)) return batfaile("read",batfilename);
155 if (!sr && feof(batfile)) break;
156 l= strlen(batlinebuf);
158 if (batlinebuf[l-1] != '\n')
159 return batfailf("line too long");
161 colon= strchr(batlinebuf,':');
163 return batfailf("line without a colon");
166 for (p=batlinebuf; p<colon; p++)
170 for (field=fields; field->file; field++) {
171 if (!strcmp(field->file,batfilename) &&
172 !strcmp(field->label,batlinebuf))
173 goto label_interesting;
178 for (batlinevalue= colon+1;
179 *batlinevalue && isspace((unsigned char)*batlinevalue);
184 *field->valuep= strtoul(batlinevalue,&ep,10);
185 if (ep==batlinevalue || *ep!=' ')
186 batfailf("value number syntax incorrect");
187 if (strcmp(ep+1,field->unit)) batfailf("incorrect unit");
191 for (*field->valuep=0, enumsearch=field->enumarray;
192 *enumsearch && strcmp(*enumsearch,batlinevalue);
193 (*field->valuep)++, enumsearch++);
195 batfailf("unknown enum value");
205 if (r) return batfaile("chdir","..");
208 if (!(thisbat_alarm_present==0 ||
209 thisbat_info_present==0 ||
210 thisbat_state_present==0)) {
211 if (thisbat_alarm_present == VAL_NOTFOUND)
212 thisbat_alarm_present= 1;
214 for (field=fields, missing=0;
217 if (*field->valuep == VAL_NOTFOUND) {
218 fprintf(stderr,"battery/%s/%s: %s: not found\n",
219 batdirname, field->file,field->label);
223 if (missing) return -1;
229 /*---------- data collection and analysis ----------*/
231 /* These next three variables are the results of the charging state */
232 static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
233 static double nondegraded_norm, fill_norm, ratepersec_norm;
234 static int alarm_level; /* 0=ok, 1=low */
237 static double total_##f##_##l;
241 static void acquiredata(void) {
246 charging_state_mask= 0;
253 di= opendir("."); if (!di) batfaile("opendir","battery");
254 while ((de= readdir(di))) {
255 if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
257 batdirname= de->d_name;
263 if (!thisbat_info_present || !thisbat_state_present)
266 charging_state_mask |= 1u << thisbat_state_charging_state;
269 total_##f##_##l += thisbat_##f##_##l;
273 if (thisbat_state_charging_state == CHGST_DISCHARGING)
275 total_state_present_rate -= 2.0 * thisbat_state_present_rate;
280 if (total_info_design_capacity < 0.5)
281 total_info_design_capacity= 1.0;
283 if (total_info_last_full_capacity < total_state_remaining_capacity)
284 total_info_last_full_capacity= total_state_remaining_capacity;
285 if (total_info_design_capacity < total_info_last_full_capacity)
286 total_info_design_capacity= total_info_last_full_capacity;
289 (total_state_remaining_capacity < total_alarm_alarm &&
290 !(charging_state_mask & 1u << CHGST_CHARGING));
292 nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
293 fill_norm= total_state_remaining_capacity / total_info_design_capacity;
294 ratepersec_norm= total_state_present_rate
295 / (3600.0 * total_info_design_capacity);
298 static void initacquire(void) {
302 /*---------- display ----------*/
318 static Display *disp;
320 static int width, height;
321 static Colormap cmap;
323 static unsigned long lastbackground;
327 unsigned long lastfg;
330 #define C(c) static unsigned long pix_##c;
331 #define GC(g) static Gcstate gc_##g;
336 static void refresh(void);
338 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
340 static void failr(const char *m, int r) {
341 fprintf(stderr,"error: %s (code %d)\n", m, r);
345 static void setbackground(unsigned long newbg) {
348 if (newbg == lastbackground) return;
349 r= XSetWindowBackground(disp,win,newbg);
350 if (!r) fail("XSetWindowBackground");
351 lastbackground= newbg;
354 static void setforeground(Gcstate *g, unsigned long px) {
358 if (g->lastfg == px) return;
360 memset(&gcv,0,sizeof(gcv));
361 g->lastfg= gcv.foreground= px;
362 r= XChangeGC(disp,g->gc,GCForeground,&gcv);
363 if (!r) fail("XChangeGC");
366 static void show(void) {
368 int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
370 if (!charging_state_mask) {
371 setbackground(pix_darkgreen);
372 XClearWindow(disp,win);
376 setbackground(pix_dimgrey);
377 XClearWindow(disp,win);
379 setforeground(&gc_remain,
380 !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
381 !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
382 charging_state_mask & (1u<<CHGST_CHARGING)
383 ? pix_green : pix_blue);
385 setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
387 for (i=0, first_beyond=1; i<height; i++) {
390 TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
392 then= fill_norm + ratepersec_norm * elap;
395 ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
396 (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
398 if (then <= 0.0) then= 0.0;
399 else if (then >= nondegraded_norm) then= nondegraded_norm;
401 leftmost_lit= width * then;
402 leftmost_nondeg= width * nondegraded_norm;
404 if (beyond && first_beyond) {
405 XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
408 if (leftmost_lit < leftmost_nondeg)
409 XDrawLine(disp, win, gc_empty.gc,
410 leftmost_lit,i, leftmost_nondeg,i);
411 if (leftmost_lit >= 0)
412 XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
417 static void initgc(Gcstate *gc_r) {
420 memset(&gcv,0,sizeof(gcv));
421 gcv.function= GXcopy;
423 gc_r->lastfg= gcv.foreground= pix_white;
424 gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
427 static void colour(unsigned long *pix_r, const char *name) {
431 st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
437 static void initgraphics(void) {
440 disp= XOpenDisplay(0); if (!disp) fail("could not open display");
442 screen= DefaultScreen(disp);
443 win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),0,0,100,20,0,0,0);
444 cmap= DefaultColormap(disp,screen);
446 #define C(c) colour(&pix_##c, #c);
447 #define GC(g) initgc(&gc_##g);
452 r= XSetWindowBackground(disp,win,pix_dimgrey);
453 if (!r) fail("init set background");
454 lastbackground= pix_dimgrey;
456 XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
457 XMapWindow(disp,win);
460 static void refresh(void) {
465 static void newgeometry(void) {
469 XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
472 static void eventloop(void) {
483 pfd.fd= ConnectionNumber(disp);
484 pfd.events= POLLIN|POLLERR;
486 r= poll(&pfd,1,TIMEOUT);
487 if (r==-1 && errno!=EINTR) failr("poll",errno);
489 while (XPending(disp)) {
490 XNextEvent(disp,&ev);
491 if (ev.type == ConfigureNotify) {
492 XConfigureEvent *ce= (void*)&ev;
501 int main(int argc, const char *const *argv) {
502 if (!argv[0] || argv[1])