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 static const char program_name[]= "xacpi-simple";
41 /*---------- general utility stuff and declarations ----------*/
43 static void fail(const char *m) {
44 fprintf(stderr,"error: %s\n", m);
47 static void badusage(void) { fail("bad usage"); }
49 #define CHGST_DISCHARGING 0 /* Reflects order in E(state,charging_state) */
50 #define CHGST_CHARGING 1 /* in fields table. Also, much code assumes */
51 #define CHGST_CHARGED 2 /* exactly these three possible states. */
53 /*---------- structure of and results from /proc/acpi/battery/... ----------*/
54 /* variables thisbat_... are the results from readbattery();
55 * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
58 typedef struct batinfo_field {
61 unsigned long *valuep;
63 const char *enumarray[10];
66 #define QUANTITY_FIELDS \
67 QF(info, design_capacity, "mWh") \
68 QF(info, last_full_capacity, "mWh") \
69 QF(state, present_rate, "mW") \
70 QF(state, remaining_capacity, "mWh") \
71 QF(alarm, alarm, "mWh")
73 #define QF(f,l,u) static unsigned long thisbat_##f##_##l;
77 static unsigned long thisbat_alarm_present, thisbat_info_present;
78 static unsigned long thisbat_state_present, thisbat_state_charging_state;
80 #define VAL_NOTFOUND (~0UL)
82 static const batinfo_field fields[]= {
83 #define E(f,l) #f, #l, &thisbat_##f##_##l, 0
84 #define QF(f,l,u) { #f, #l, &thisbat_##f##_##l, u },
85 { E(alarm, present), { "no", "yes" } },
86 { E(info, present), { "no", "yes" } },
87 { E(state,present), { "no", "yes" } },
88 { E(state,charging_state), { "discharging", "charging", "charged" } },
89 QUANTITY_FIELDS /* take care re charging_state values order - */
90 { 0 } /* if you must change it, search for CHGST_... */
95 static const char *files[]= { "info", "state", "alarm" };
97 /*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
99 /* variables private to the parser and its error handlers */
100 static char batlinebuf[1000];
101 static FILE *batfile;
102 static const char *batdirname;
103 static const char *batfilename;
104 static const char *batlinevalue;
106 static int batfailf(const char *why) {
108 fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
109 batdirname,batfilename, batlinebuf,batlinevalue,why);
111 fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
112 batdirname,batfilename, why, batlinebuf);
117 static int batfaile(const char *syscall, const char *target) {
118 fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
119 batdirname ? batdirname : "*", syscall, target, strerror(errno));
123 static void chdir_base(void) {
126 r= chdir("/proc/acpi/battery");
127 if (r) batfaile("chdir","/proc/acpi/battery");
130 static void tidybattery(void) {
131 if (batfile) { fclose(batfile); batfile=0; }
132 if (batdirname) { chdir_base(); batdirname=0; }
135 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
136 const batinfo_field *field;
137 const char *const *cfilename, *const *enumsearch;
138 char *colon, *ep, *sr, *p;
141 r= chdir(batdirname);
142 if (r) return batfaile("chdir",batdirname);
144 for (field=fields; field->file; field++)
145 *field->valuep= VAL_NOTFOUND;
147 for (cfilename=files;
148 (batfilename= *cfilename);
150 batfile= fopen(batfilename,"r");
151 if (!batfile) return batfaile("open",batfilename);
156 sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
157 if (ferror(batfile)) return batfaile("read",batfilename);
158 if (!sr && feof(batfile)) break;
159 l= strlen(batlinebuf);
161 if (batlinebuf[l-1] != '\n')
162 return batfailf("line too long");
164 colon= strchr(batlinebuf,':');
166 return batfailf("line without a colon");
169 for (p=batlinebuf; p<colon; p++)
173 for (field=fields; field->file; field++) {
174 if (!strcmp(field->file,batfilename) &&
175 !strcmp(field->label,batlinebuf))
176 goto label_interesting;
181 for (batlinevalue= colon+1;
182 *batlinevalue && isspace((unsigned char)*batlinevalue);
187 *field->valuep= strtoul(batlinevalue,&ep,10);
188 if (ep==batlinevalue || *ep!=' ')
189 batfailf("value number syntax incorrect");
190 if (strcmp(ep+1,field->unit)) batfailf("incorrect unit");
194 for (*field->valuep=0, enumsearch=field->enumarray;
195 *enumsearch && strcmp(*enumsearch,batlinevalue);
196 (*field->valuep)++, enumsearch++);
198 batfailf("unknown enum value");
207 if (!(thisbat_alarm_present==0 ||
208 thisbat_info_present==0 ||
209 thisbat_state_present==0)) {
210 if (thisbat_alarm_present == VAL_NOTFOUND)
211 thisbat_alarm_present= 1;
213 for (field=fields, missing=0;
216 if (*field->valuep == VAL_NOTFOUND) {
217 fprintf(stderr,"battery/%s/%s: %s: not found\n",
218 batdirname, field->file,field->label);
222 if (missing) return -1;
226 if (r) return batfaile("chdir","..");
232 /*---------- data collection and analysis ----------*/
234 /* These next three variables are the results of the charging state */
235 static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
236 static double nondegraded_norm, fill_norm, ratepersec_norm;
237 static int alarm_level; /* 0=ok, 1=low */
240 static double total_##f##_##l;
244 static void acquiredata(void) {
249 charging_state_mask= 0;
256 di= opendir("."); if (!di) batfaile("opendir","battery");
257 while ((de= readdir(di))) {
258 if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
260 batdirname= de->d_name;
266 if (!thisbat_info_present || !thisbat_state_present)
269 charging_state_mask |= 1u << thisbat_state_charging_state;
272 total_##f##_##l += thisbat_##f##_##l;
276 if (thisbat_state_charging_state == CHGST_DISCHARGING)
278 total_state_present_rate -= 2.0 * thisbat_state_present_rate;
283 if (total_info_design_capacity < 0.5)
284 total_info_design_capacity= 1.0;
286 if (total_info_last_full_capacity < total_state_remaining_capacity)
287 total_info_last_full_capacity= total_state_remaining_capacity;
288 if (total_info_design_capacity < total_info_last_full_capacity)
289 total_info_design_capacity= total_info_last_full_capacity;
292 (total_state_remaining_capacity < total_alarm_alarm &&
293 !(charging_state_mask & 1u << CHGST_CHARGING));
295 nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
296 fill_norm= total_state_remaining_capacity / total_info_design_capacity;
297 ratepersec_norm= total_state_present_rate
298 / (3600.0 * total_info_design_capacity);
301 static void initacquire(void) {
305 /*---------- display ----------*/
321 static Display *disp;
323 static int width, height;
324 static Colormap cmap;
326 static unsigned long lastbackground;
330 unsigned long lastfg;
333 #define C(c) static unsigned long pix_##c;
334 #define GC(g) static Gcstate gc_##g;
339 static void refresh(void);
341 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
343 static void failr(const char *m, int r) {
344 fprintf(stderr,"error: %s (code %d)\n", m, r);
348 static void setbackground(unsigned long newbg) {
351 if (newbg == lastbackground) return;
352 r= XSetWindowBackground(disp,win,newbg);
353 if (!r) fail("XSetWindowBackground");
354 lastbackground= newbg;
357 static void setforeground(Gcstate *g, unsigned long px) {
361 if (g->lastfg == px) return;
363 memset(&gcv,0,sizeof(gcv));
364 g->lastfg= gcv.foreground= px;
365 r= XChangeGC(disp,g->gc,GCForeground,&gcv);
366 if (!r) fail("XChangeGC");
369 static void show(void) {
371 int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
373 if (!charging_state_mask) {
374 setbackground(pix_darkgreen);
375 XClearWindow(disp,win);
379 setbackground(pix_dimgrey);
380 XClearWindow(disp,win);
382 setforeground(&gc_remain,
383 !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
384 !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
385 charging_state_mask & (1u<<CHGST_CHARGING)
386 ? pix_green : pix_blue);
388 setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
390 for (i=0, first_beyond=1; i<height; i++) {
393 TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
395 then= fill_norm + ratepersec_norm * elap;
398 ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
399 (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
401 if (then <= 0.0) then= 0.0;
402 else if (then >= nondegraded_norm) then= nondegraded_norm;
404 leftmost_lit= width * then;
405 leftmost_nondeg= width * nondegraded_norm;
407 if (beyond && first_beyond) {
408 XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
411 if (leftmost_lit < leftmost_nondeg)
412 XDrawLine(disp, win, gc_empty.gc,
413 leftmost_lit,i, leftmost_nondeg,i);
414 if (leftmost_lit >= 0)
415 XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
420 static void initgc(Gcstate *gc_r) {
423 memset(&gcv,0,sizeof(gcv));
424 gcv.function= GXcopy;
426 gc_r->lastfg= gcv.foreground= pix_white;
427 gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
430 static void colour(unsigned long *pix_r, const char *name) {
434 st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
440 static void initgraphics(int argc, char **argv) {
443 const char *geom_string;
444 XSizeHints *normal_hints;
446 XClassHint *class_hint;
447 int pos_x, pos_y, gravity;
448 char *program_name_silly;
450 program_name_silly= (char*)program_name;
452 disp= XOpenDisplay(0);
453 if (!disp) fail("could not open display");
455 screen= DefaultScreen(disp);
457 normal_hints= XAllocSizeHints();
458 wm_hints= XAllocWMHints();
459 class_hint= XAllocClassHint();
461 if (!normal_hints || !wm_hints || !class_hint)
462 fail("could not alloc hint(s)");
464 geom_string= argc>=2 ? argv[1] : 0;
466 r= XWMGeometry(disp,screen, geom_string,"128x32", 0,
472 win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),
473 pos_x,pos_y,width,height,0,0,0);
474 cmap= DefaultColormap(disp,screen);
476 #define C(c) colour(&pix_##c, #c);
477 #define GC(g) initgc(&gc_##g);
482 r= XSetWindowBackground(disp,win,pix_dimgrey);
483 if (!r) fail("init set background");
484 lastbackground= pix_dimgrey;
486 normal_hints->flags= PWinGravity;
487 normal_hints->win_gravity= gravity;
489 wm_hints->flags= InputHint;
490 wm_hints->input= False;
492 class_hint->res_name= program_name_silly;
493 class_hint->res_class= program_name_silly;
495 /*r= XStringListToTextProperty(&program_name_silly, 1, &tp);
496 if (!r) fail("XStringListtoTextProperty");*/
498 XmbSetWMProperties(disp,win, program_name,program_name,
499 argv,argc, normal_hints, wm_hints, class_hint);
501 XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
502 XMapWindow(disp,win);
505 static void refresh(void) {
510 static void newgeometry(void) {
514 XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
517 static void eventloop(void) {
528 pfd.fd= ConnectionNumber(disp);
529 pfd.events= POLLIN|POLLERR;
531 r= poll(&pfd,1,TIMEOUT);
532 if (r==-1 && errno!=EINTR) failr("poll",errno);
534 while (XPending(disp)) {
535 XNextEvent(disp,&ev);
536 if (ev.type == ConfigureNotify) {
537 XConfigureEvent *ce= (void*)&ev;
546 int main(int argc, char **argv) {
547 if (argc<1 || argc>2)
551 initgraphics(argc,argv);