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>
32 #include <X11/Xutil.h>
37 #define TIMEOUT 5000 /* milliseconds */
39 /*---------- general utility stuff and declarations ----------*/
41 static void fail(const char *m) {
42 fprintf(stderr,"error: %s\n", m);
45 static void badusage(void) { fail("bad usage"); }
47 #define CHGST_DISCHARGING 0 /* Reflects order in E(state,charging_state) */
48 #define CHGST_CHARGING 1 /* in fields table. Also, much code assumes */
49 #define CHGST_CHARGED 2 /* exactly these three possible states. */
51 /*---------- structure of and results from /proc/acpi/battery/... ----------*/
52 /* variables thisbat_... are the results from readbattery();
53 * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
56 typedef struct batinfo_field {
59 unsigned long *valuep;
61 const char *enumarray[10];
64 #define QUANTITY_FIELDS \
65 QF(info, design_capacity, "mWh") \
66 QF(info, last_full_capacity, "mWh") \
67 QF(state, present_rate, "mW") \
68 QF(state, remaining_capacity, "mWh") \
69 QF(alarm, alarm, "mWh")
71 #define QF(f,l,u) static unsigned long thisbat_##f##_##l;
75 static unsigned long thisbat_alarm_present, thisbat_info_present;
76 static unsigned long thisbat_state_present, thisbat_state_charging_state;
78 #define VAL_NOTFOUND (~0UL)
80 static const batinfo_field fields[]= {
81 #define E(f,l) #f, #l, &thisbat_##f##_##l, 0
82 #define QF(f,l,u) { #f, #l, &thisbat_##f##_##l, u },
83 { E(alarm, present), { "no", "yes" } },
84 { E(info, present), { "no", "yes" } },
85 { E(state,present), { "no", "yes" } },
86 { E(state,charging_state), { "discharging", "charging", "charged" } },
87 QUANTITY_FIELDS /* take care re charging_state values order - */
88 { 0 } /* if you must change it, search for CHGST_... */
93 static const char *files[]= { "info", "state", "alarm" };
95 /*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
97 /* variables private to the parser and its error handlers */
98 static char batlinebuf[1000];
100 static const char *batdirname;
101 static const char *batfilename;
102 static const char *batlinevalue;
104 static int batfailf(const char *why) {
106 fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
107 batdirname,batfilename, batlinebuf,batlinevalue,why);
109 fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
110 batdirname,batfilename, why, batlinebuf);
115 static int batfaile(const char *syscall, const char *target) {
116 fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
117 batdirname ? batdirname : "*", syscall, target, strerror(errno));
121 static void chdir_base(void) {
124 r= chdir("/proc/acpi/battery");
125 if (r) batfaile("chdir","/proc/acpi/battery");
128 static void tidybattery(void) {
129 if (batfile) { fclose(batfile); batfile=0; }
130 if (batdirname) { chdir_base(); batdirname=0; }
133 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
134 const batinfo_field *field;
135 const char *const *cfilename, *const *enumsearch;
136 char *colon, *ep, *sr, *p;
139 r= chdir(batdirname);
140 if (r) return batfaile("chdir",batdirname);
142 for (field=fields; field->file; field++)
143 *field->valuep= VAL_NOTFOUND;
145 for (cfilename=files;
146 (batfilename= *cfilename);
148 batfile= fopen(batfilename,"r");
149 if (!batfile) return batfaile("open",batfilename);
154 sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
155 if (ferror(batfile)) return batfaile("read",batfilename);
156 if (!sr && feof(batfile)) break;
157 l= strlen(batlinebuf);
159 if (batlinebuf[l-1] != '\n')
160 return batfailf("line too long");
162 colon= strchr(batlinebuf,':');
164 return batfailf("line without a colon");
167 for (p=batlinebuf; p<colon; p++)
171 for (field=fields; field->file; field++) {
172 if (!strcmp(field->file,batfilename) &&
173 !strcmp(field->label,batlinebuf))
174 goto label_interesting;
179 for (batlinevalue= colon+1;
180 *batlinevalue && isspace((unsigned char)*batlinevalue);
185 *field->valuep= strtoul(batlinevalue,&ep,10);
186 if (ep==batlinevalue || *ep!=' ')
187 batfailf("value number syntax incorrect");
188 if (strcmp(ep+1,field->unit)) batfailf("incorrect unit");
192 for (*field->valuep=0, enumsearch=field->enumarray;
193 *enumsearch && strcmp(*enumsearch,batlinevalue);
194 (*field->valuep)++, enumsearch++);
196 batfailf("unknown enum value");
206 if (r) return batfaile("chdir","..");
209 if (!(thisbat_alarm_present==0 ||
210 thisbat_info_present==0 ||
211 thisbat_state_present==0)) {
212 if (thisbat_alarm_present == VAL_NOTFOUND)
213 thisbat_alarm_present= 1;
215 for (field=fields, missing=0;
218 if (*field->valuep == VAL_NOTFOUND) {
219 fprintf(stderr,"battery/%s/%s: %s: not found\n",
220 batdirname, field->file,field->label);
224 if (missing) return -1;
230 /*---------- data collection and analysis ----------*/
232 /* These next three variables are the results of the charging state */
233 static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
234 static double nondegraded_norm, fill_norm, ratepersec_norm;
235 static int alarm_level; /* 0=ok, 1=low */
238 static double total_##f##_##l;
242 static void acquiredata(void) {
247 charging_state_mask= 0;
254 di= opendir("."); if (!di) batfaile("opendir","battery");
255 while ((de= readdir(di))) {
256 if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
258 batdirname= de->d_name;
264 if (!thisbat_info_present || !thisbat_state_present)
267 charging_state_mask |= 1u << thisbat_state_charging_state;
270 total_##f##_##l += thisbat_##f##_##l;
274 if (thisbat_state_charging_state == CHGST_DISCHARGING)
276 total_state_present_rate -= 2.0 * thisbat_state_present_rate;
281 if (total_info_design_capacity < 0.5)
282 total_info_design_capacity= 1.0;
284 if (total_info_last_full_capacity < total_state_remaining_capacity)
285 total_info_last_full_capacity= total_state_remaining_capacity;
286 if (total_info_design_capacity < total_info_last_full_capacity)
287 total_info_design_capacity= total_info_last_full_capacity;
290 (total_state_remaining_capacity < total_alarm_alarm &&
291 !(charging_state_mask & 1u << CHGST_CHARGING));
293 nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
294 fill_norm= total_state_remaining_capacity / total_info_design_capacity;
295 ratepersec_norm= total_state_present_rate
296 / (3600.0 * total_info_design_capacity);
299 static void initacquire(void) {
303 /*---------- display ----------*/
319 static Display *disp;
321 static int width, height;
322 static Colormap cmap;
324 static unsigned long lastbackground;
328 unsigned long lastfg;
331 #define C(c) static unsigned long pix_##c;
332 #define GC(g) static Gcstate gc_##g;
337 static void refresh(void);
339 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
341 static void failr(const char *m, int r) {
342 fprintf(stderr,"error: %s (code %d)\n", m, r);
346 static void setbackground(unsigned long newbg) {
349 if (newbg == lastbackground) return;
350 r= XSetWindowBackground(disp,win,newbg);
351 if (!r) fail("XSetWindowBackground");
352 lastbackground= newbg;
355 static void setforeground(Gcstate *g, unsigned long px) {
359 if (g->lastfg == px) return;
361 memset(&gcv,0,sizeof(gcv));
362 g->lastfg= gcv.foreground= px;
363 r= XChangeGC(disp,g->gc,GCForeground,&gcv);
364 if (!r) fail("XChangeGC");
367 static void show(void) {
369 int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
371 if (!charging_state_mask) {
372 setbackground(pix_darkgreen);
373 XClearWindow(disp,win);
377 setbackground(pix_dimgrey);
378 XClearWindow(disp,win);
380 setforeground(&gc_remain,
381 !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
382 !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
383 charging_state_mask & (1u<<CHGST_CHARGING)
384 ? pix_green : pix_blue);
386 setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
388 for (i=0, first_beyond=1; i<height; i++) {
391 TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
393 then= fill_norm + ratepersec_norm * elap;
396 ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
397 (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
399 if (then <= 0.0) then= 0.0;
400 else if (then >= nondegraded_norm) then= nondegraded_norm;
402 leftmost_lit= width * then;
403 leftmost_nondeg= width * nondegraded_norm;
405 if (beyond && first_beyond) {
406 XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
409 if (leftmost_lit < leftmost_nondeg)
410 XDrawLine(disp, win, gc_empty.gc,
411 leftmost_lit,i, leftmost_nondeg,i);
412 if (leftmost_lit >= 0)
413 XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
418 static void initgc(Gcstate *gc_r) {
421 memset(&gcv,0,sizeof(gcv));
422 gcv.function= GXcopy;
424 gc_r->lastfg= gcv.foreground= pix_white;
425 gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
428 static void colour(unsigned long *pix_r, const char *name) {
432 st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
438 static void initgraphics(void) {
441 const char *program_name= "xacpi-simple";
443 disp= XOpenDisplay(0); if (!disp) fail("could not open display");
445 screen= DefaultScreen(disp);
446 win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),0,0,100,20,0,0,0);
447 cmap= DefaultColormap(disp,screen);
449 #define C(c) colour(&pix_##c, #c);
450 #define GC(g) initgc(&gc_##g);
455 r= XSetWindowBackground(disp,win,pix_dimgrey);
456 if (!r) fail("init set background");
457 lastbackground= pix_dimgrey;
459 r= XStringListToTextProperty((char**)&program_name, 1, &tp);
460 if (!r) fail("XStringListtoTextProperty");
461 XSetWMName(disp,win,&tp);
463 XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
464 XMapWindow(disp,win);
467 static void refresh(void) {
472 static void newgeometry(void) {
476 XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
479 static void eventloop(void) {
490 pfd.fd= ConnectionNumber(disp);
491 pfd.events= POLLIN|POLLERR;
493 r= poll(&pfd,1,TIMEOUT);
494 if (r==-1 && errno!=EINTR) failr("poll",errno);
496 while (XPending(disp)) {
497 XNextEvent(disp,&ev);
498 if (ev.type == ConfigureNotify) {
499 XConfigureEvent *ce= (void*)&ev;
508 int main(int argc, const char *const *argv) {
509 if (!argv[0] || argv[1])