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
14 * ... yellow ... error
16 * [1] battery must be quite badly degraded
19 * Copyright (C) 2004 Ian Jackson <ian@davenant.greenend.org.uk>
21 * This is free software; you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation; either version 3,
24 * or (at your option) any later version.
26 * This is distributed in the hope that it will be useful, but
27 * WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public
32 * License along with this file; if not, consult the Free Software
33 * Foundation's website at www.fsf.org, or the GNU Project website at
48 #include <sys/types.h>
52 #include <X11/Xutil.h>
53 #include <X11/Xresource.h>
58 #define TIMEOUT 5000 /* milliseconds */
59 #define TIMEOUT_ONERROR 3333 /* milliseconds */
61 static const char program_name[]= "xacpi-simple";
63 /*---------- general utility stuff and declarations ----------*/
65 static void fail(const char *m) {
66 fprintf(stderr,"error: %s\n", m);
69 static void badusage(void) { fail("bad usage"); }
71 typedef struct fileinfo fileinfo;
74 void (*parse)(const fileinfo*);
78 /*---------- structure of and results from /sys/class/power/... ----------*/
79 /* variables thisbat_... are the results from readbattery();
80 * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
83 typedef struct batinfo_field {
86 const char *enumarray[10];
89 #define UEVENT_QUANTITY_FIELDS(q) \
90 q(design_capacity, CHARGE_FULL_DESIGN ) /* uAh */ \
91 q(last_full_capacity, CHARGE_FULL ) /* uAh */ \
92 q(present_rate, CURRENT_NOW ) /* uA */ \
93 q(remaining_capacity, CHARGE_NOW ) /* uAh */ \
94 q(present, PRESENT ) /* boolean */ \
95 q(online, ONLINE ) /* boolean */
97 #define UEVENT_ENUM_FIELDS(e) \
98 e(type, TYPE, "Battery", "Mains" ) \
99 e(state, STATUS, "Discharging", "Charging", "Charged" )
101 #define CHGST_DISCHARGING 0 /* Reflects order in e(state,...) above */
102 #define CHGST_CHARGING 1 /* Also, much code assumes exadtly */
103 #define CHGST_CHARGED 2 /* these three possible states. */
104 #define CHGST_ERROR 8 /* Except that this one is an extra bit. */
106 #define Q_FLD(f,l) { "POWER_SUPPLY_" #l, &thisbat_##f },
107 #define E_FLD(f,l,vl...) { "POWER_SUPPLY_" #l, &thisbat_##f, { vl } },
109 #define ALL_FIELDS(i) \
110 UEVENT_QUANTITY_FIELDS(i) \
111 UEVENT_ENUM_FIELDS(i) \
114 #define F_VAR(f,...) static uint64_t thisbat_##f;
117 static const batinfo_field uevent_fields[]= {
118 UEVENT_QUANTITY_FIELDS(Q_FLD)
119 UEVENT_ENUM_FIELDS(E_FLD)
123 #define VAL_NOTFOUND (~0UL)
125 static const fileinfo files[]= {
126 { "uevent", parse_fields, uevent_fields },
127 { "alarm", parse_boolean, &thisbat_alarm },
131 /*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
133 /* variables private to the parser and its error handlers */
134 static char batlinebuf[1000];
135 static FILE *batfile;
136 static const char *batdirname;
137 static const char *batfilename;
138 static const char *batlinevalue;
140 static int batfailf(const char *why) {
142 fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
143 batdirname,batfilename, batlinebuf,batlinevalue,why);
145 fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
146 batdirname,batfilename, why, batlinebuf);
151 static int batfaile(const char *syscall, const char *target) {
152 fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
153 batdirname ? batdirname : "*", syscall, target, strerror(errno));
157 static void chdir_base(void) {
160 r= chdir("/sys/class/power_supply");
161 if (r) batfaile("chdir","/sys/class/power_supply");
164 static void tidybattery(void) {
165 if (batfile) { fclose(batfile); batfile=0; }
166 if (batdirname) { chdir_base(); batdirname=0; }
169 static void parse_fields(const fileinfo *cfile, char *batlinebuf) {
170 equals= strchr(batlinebuf,'=');
172 return batfailf("line without a equals");
175 for (field=fields; field->file; field++) {
176 if (!strcmp(field->label,batlinebuf))
182 if (!field->enumarray[0]) {
184 *field->valuep= strtoull(equals,&ep,10);
186 batfailf("value number syntax incorrect");
190 for (*field->valuep=0, enumsearch=field->enumarray;
191 *enumsearch && strcmp(*enumsearch,equals);
192 (*field->valuep)++, enumsearch++);
194 batfailf("unknown enum value");
199 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
200 const batinfo_field *field;
201 const fileinfo *cfile;
202 const char *const *enumsearch;
203 char *colon, *ep, *sr, *p;
206 r= chdir(batdirname);
207 if (r) return batfaile("chdir",batdirname);
209 #define V_NOTFOUND(f,...) thisbat_##f = VAL_NOTFOUND;
210 ALL_FIELDS(V_NOTFOUND)
212 for (field=fields; field->file; field++)
213 *field->valuep= VAL_NOTFOUND;
216 (batfilename= cfile->filename);
218 batfile= fopen(batfilename,"r");
219 if (!batfile) return batfaile("open",batfilename);
224 sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
225 if (ferror(batfile)) return batfaile("read",batfilename);
226 if (!sr && feof(batfile)) break;
227 l= strlen(batlinebuf);
229 if (batlinebuf[l-1] != '\n')
230 return batfailf("line too long");
233 cfile->parse(cfile, batlinebuf);
240 if (!(thisbat_alarm_present==0 ||
241 thisbat_state_present==0 || thisbat_info_present==0)) {
242 if (thisbat_alarm_present == VAL_NOTFOUND)
243 thisbat_alarm_present= 1;
245 for (field=fields, missing=0;
248 if (*field->valuep == VAL_NOTFOUND) {
249 fprintf(stderr,"battery/%s/%s: %s: not found\n",
250 batdirname, field->file,field->label);
254 if (missing) return -1;
258 if (r) return batfaile("chdir","..");
264 /*---------- data collection and analysis ----------*/
266 /* These next three variables are the results of the charging state */
267 static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
268 static double nondegraded_norm, fill_norm, ratepersec_norm;
269 static int alarm_level; /* 0=ok, 1=low */
272 static double total_##f##_##l;
276 static void acquiredata(void) {
281 charging_state_mask= 0;
288 di= opendir("."); if (!di) batfaile("opendir","battery");
289 while ((de= readdir(di))) {
290 if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
292 batdirname= de->d_name;
296 if (r) { charging_state_mask |= CHGST_ERROR; continue; }
298 if (!thisbat_info_present || !thisbat_state_present)
301 charging_state_mask |= 1u << thisbat_state_charging_state;
304 total_##f##_##l += thisbat_##f##_##l;
308 if (thisbat_state_charging_state == CHGST_DISCHARGING)
310 total_state_present_rate -= 2.0 * thisbat_state_present_rate;
315 if (total_info_design_capacity < 0.5)
316 total_info_design_capacity= 1.0;
318 if (total_info_last_full_capacity < total_state_remaining_capacity)
319 total_info_last_full_capacity= total_state_remaining_capacity;
320 if (total_info_design_capacity < total_info_last_full_capacity)
321 total_info_design_capacity= total_info_last_full_capacity;
324 (total_state_remaining_capacity < total_alarm_alarm &&
325 !(charging_state_mask & 1u << CHGST_CHARGING));
327 nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
328 fill_norm= total_state_remaining_capacity / total_info_design_capacity;
329 ratepersec_norm= total_state_present_rate
330 / (3600.0 * total_info_design_capacity);
333 static void initacquire(void) {
337 /*---------- argument parsing ----------*/
340 C(blue, discharging) \
346 C(dimgrey, degraded) \
347 C(darkgreen, absent) \
349 C(white, equilibrium) \
354 static XrmDatabase xrm;
355 static Display *disp;
358 static const char defaultresources[]=
361 "*" #u "Color: " #c "\n"
367 #define S(s) ((char*)(s))
368 static const XrmOptionDescRec optiontable[]= {
369 { S("-display"), S("*display"), XrmoptionSepArg },
370 { S("-geometry"), S("*geometry"), XrmoptionSepArg },
373 { S("-" #u "Color"), S("*" #u "Color"), XrmoptionSepArg }, \
374 { S("-" #u "Colour"), S("*" #u "Color"), XrmoptionSepArg },
380 static const char *getresource(const char *want) {
381 char name_buf[256], class_buf[256];
383 char *rep_type_dummy;
386 assert(strlen(want) < 128);
387 sprintf(name_buf,"xacpi-simple.%s",want);
388 sprintf(class_buf,"Xacpi-Simple.%s",want);
390 r= XrmGetResource(xrm, name_buf,class_buf, &rep_type_dummy, &val);
396 static void more_resources(const char *str, const char *why) {
401 more= XrmGetStringDatabase((char*)str);
402 if (!more) fail(why);
403 XrmCombineDatabase(more,&xrm,0);
406 static void parseargs(int argc, char **argv) {
407 Screen *screenscreen;
411 XrmParseCommand(&xrm, (XrmOptionDescRec*)optiontable,
412 sizeof(optiontable)/sizeof(*optiontable),
413 program_name, &argc, argv);
415 if (argc>1) badusage();
417 disp= XOpenDisplay(getresource("display"));
418 if (!disp) fail("could not open display");
420 screen= DefaultScreen(disp);
422 screenscreen= ScreenOfDisplay(disp,screen);
423 if (!screenscreen) fail("screenofdisplay");
424 more_resources(XScreenResourceString(screenscreen), "screen resources");
425 more_resources(XResourceManagerString(disp), "display resources");
426 more_resources(defaultresources, "default resources");
429 /*---------- display ----------*/
432 static int width, height;
433 static Colormap cmap;
434 static unsigned long lastbackground;
438 unsigned long lastfg;
441 #define C(c,u) static unsigned long pix_##c;
442 #define GC(g) static Gcstate gc_##g;
447 static void refresh(void);
449 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
451 static void failr(const char *m, int r) {
452 fprintf(stderr,"error: %s (code %d)\n", m, r);
456 static void setbackground(unsigned long newbg) {
459 if (newbg == lastbackground) return;
460 r= XSetWindowBackground(disp,win,newbg);
461 if (!r) fail("XSetWindowBackground");
462 lastbackground= newbg;
465 static void setforeground(Gcstate *g, unsigned long px) {
469 if (g->lastfg == px) return;
471 memset(&gcv,0,sizeof(gcv));
472 g->lastfg= gcv.foreground= px;
473 r= XChangeGC(disp,g->gc,GCForeground,&gcv);
474 if (!r) fail("XChangeGC");
477 static void show_solid(unsigned long px) {
479 XClearWindow(disp,win);
482 static void show(void) {
484 int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
486 if (!charging_state_mask)
487 return show_solid(pix_darkgreen);
489 if (charging_state_mask & CHGST_ERROR)
490 return show_solid(pix_yellow);
492 setbackground(pix_dimgrey);
493 XClearWindow(disp,win);
495 setforeground(&gc_remain,
496 !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
497 !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
498 charging_state_mask & (1u<<CHGST_CHARGING)
499 ? pix_green : pix_blue);
501 setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
503 for (i=0, first_beyond=1; i<height; i++) {
506 TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
508 then= fill_norm + ratepersec_norm * elap;
511 ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
512 (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
514 if (then <= 0.0) then= 0.0;
515 else if (then >= nondegraded_norm) then= nondegraded_norm;
517 leftmost_lit= width * then;
518 leftmost_nondeg= width * nondegraded_norm;
520 if (beyond && first_beyond) {
521 XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
524 if (leftmost_lit < leftmost_nondeg)
525 XDrawLine(disp, win, gc_empty.gc,
526 leftmost_lit,i, leftmost_nondeg,i);
527 if (leftmost_lit >= 0)
528 XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
533 static void initgc(Gcstate *gc_r) {
536 memset(&gcv,0,sizeof(gcv));
537 gcv.function= GXcopy;
539 gc_r->lastfg= gcv.foreground= pix_white;
540 gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
543 static void colour(unsigned long *pix_r, const char *whichcolour) {
548 name= getresource(whichcolour);
549 if (!name) fail("get colour resource");
551 st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
557 static void initgraphics(int argc, char **argv) {
559 const char *geom_string;
560 XSizeHints *normal_hints;
562 XClassHint *class_hint;
563 int pos_x, pos_y, gravity;
564 char *program_name_silly;
566 program_name_silly= (char*)program_name;
568 normal_hints= XAllocSizeHints();
569 wm_hints= XAllocWMHints();
570 class_hint= XAllocClassHint();
572 if (!normal_hints || !wm_hints || !class_hint)
573 fail("could not alloc hint(s)");
575 geom_string= getresource("geometry");
577 xwmgr= XWMGeometry(disp,screen, geom_string,"128x32", 0,
583 win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),
584 pos_x,pos_y,width,height,0,0,0);
585 cmap= DefaultColormap(disp,screen);
587 #define C(c,u) colour(&pix_##c, #u "Color");
588 #define GC(g) initgc(&gc_##g);
593 r= XSetWindowBackground(disp,win,pix_dimgrey);
594 if (!r) fail("init set background");
595 lastbackground= pix_dimgrey;
597 normal_hints->flags= PWinGravity;
598 normal_hints->win_gravity= gravity;
599 normal_hints->x= pos_x;
600 normal_hints->y= pos_y;
601 normal_hints->width= width;
602 normal_hints->height= height;
603 if ((xwmgr & XValue) || (xwmgr & YValue))
604 normal_hints->flags |= USPosition;
606 wm_hints->flags= InputHint;
607 wm_hints->input= False;
609 class_hint->res_name= program_name_silly;
610 class_hint->res_class= program_name_silly;
612 XmbSetWMProperties(disp,win, program_name,program_name,
613 argv,argc, normal_hints, wm_hints, class_hint);
615 XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
616 XMapWindow(disp,win);
619 static void refresh(void) {
624 static void newgeometry(void) {
628 XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
631 static void eventloop(void) {
642 pfd.fd= ConnectionNumber(disp);
643 pfd.events= POLLIN|POLLERR;
645 timeout= !(charging_state_mask & CHGST_ERROR) ? TIMEOUT : TIMEOUT_ONERROR;
646 r= poll(&pfd,1,timeout);
647 if (r==-1 && errno!=EINTR) failr("poll",errno);
649 while (XPending(disp)) {
650 XNextEvent(disp,&ev);
651 if (ev.type == ConfigureNotify) {
652 XConfigureEvent *ce= (void*)&ev;
661 int main(int argc, char **argv) {
662 parseargs(argc,argv);
664 initgraphics(argc,argv);