+#define CHGST_DISCHARGING 0 /* Reflects order in E(state,charging_state) */
+#define CHGST_CHARGING 1 /* in fields table. Also, much code assumes */
+#define CHGST_CHARGED 2 /* exactly these three possible states. */
+#define CHGST_ERROR 8 /* Except that this one is an extra bit. */
+
+/*---------- structure of and results from /proc/acpi/battery/... ----------*/
+/* variables thisbat_... are the results from readbattery();
+ * if readbattery() succeeds they are all valid and not VAL_NOTFOUND
+ */
+
+typedef struct batinfo_field {
+ const char *file;
+ const char *label;
+ unsigned long *valuep;
+ const char *unit;
+ const char *enumarray[10];
+} batinfo_field;
+
+#define QUANTITY_FIELDS \
+ QF(info, design_capacity, "mWh") \
+ QF(info, last_full_capacity, "mWh") \
+ QF(state, present_rate, "mW") \
+ QF(state, remaining_capacity, "mWh") \
+ QF(alarm, alarm, "mWh")
+
+#define QF(f,l,u) static unsigned long thisbat_##f##_##l;
+ QUANTITY_FIELDS
+#undef QF
+
+static unsigned long thisbat_alarm_present, thisbat_info_present;
+static unsigned long thisbat_state_present, thisbat_state_charging_state;
+
+#define VAL_NOTFOUND (~0UL)
+
+static const batinfo_field fields[]= {
+#define E(f,l) #f, #l, &thisbat_##f##_##l, 0
+#define QF(f,l,u) { #f, #l, &thisbat_##f##_##l, u },
+ { E(alarm, present), { "no", "yes" } },
+ { E(info, present), { "no", "yes" } },
+ { E(state,present), { "no", "yes" } },
+ { E(state,charging_state), { "discharging", "charging", "charged" } },
+ QUANTITY_FIELDS /* take care re charging_state values order - */
+ { 0 } /* if you must change it, search for CHGST_... */
+#undef E
+#undef QF
+};
+
+static const char *files[]= { "info", "state", "alarm" };
+
+/*---------- parsing of one battery in /proc/acpi/battery/... ----------*/
+
+/* variables private to the parser and its error handlers */
+static char batlinebuf[1000];
+static FILE *batfile;
+static const char *batdirname;
+static const char *batfilename;
+static const char *batlinevalue;
+
+static int batfailf(const char *why) {
+ if (batlinevalue) {
+ fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n",
+ batdirname,batfilename, batlinebuf,batlinevalue,why);
+ } else {
+ fprintf(stderr,"battery/%s/%s: %s: `%s'\n",
+ batdirname,batfilename, why, batlinebuf);
+ }
+ return -1;
+}
+
+static int batfaile(const char *syscall, const char *target) {
+ fprintf(stderr,"battery/%s: failed to %s %s: %s\n",
+ batdirname ? batdirname : "*", syscall, target, strerror(errno));
+ return -1;
+}
+
+static void chdir_base(void) {
+ int r;
+
+ r= chdir("/proc/acpi/battery");
+ if (r) batfaile("chdir","/proc/acpi/battery");
+}
+
+static void tidybattery(void) {
+ if (batfile) { fclose(batfile); batfile=0; }
+ if (batdirname) { chdir_base(); batdirname=0; }
+}
+
+static int readbattery(void) { /* 0=>ok, -1=>couldn't */
+ const batinfo_field *field;
+ const char *const *cfilename, *const *enumsearch;
+ char *colon, *ep, *sr, *p;
+ int r, l, missing;
+
+ r= chdir(batdirname);
+ if (r) return batfaile("chdir",batdirname);
+
+ for (field=fields; field->file; field++)
+ *field->valuep= VAL_NOTFOUND;
+
+ for (cfilename=files;
+ (batfilename= *cfilename);
+ cfilename++) {
+ batfile= fopen(batfilename,"r");
+ if (!batfile) return batfaile("open",batfilename);
+
+ for (;;) {
+ batlinevalue= 0;
+
+ sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
+ if (ferror(batfile)) return batfaile("read",batfilename);
+ if (!sr && feof(batfile)) break;
+ l= strlen(batlinebuf);
+ assert(l>0);
+ if (batlinebuf[l-1] != '\n')
+ return batfailf("line too long");
+ batlinebuf[l-1]= 0;
+ colon= strchr(batlinebuf,':');
+ if (!colon)
+ return batfailf("line without a colon");
+ *colon= 0;
+
+ for (batlinevalue= colon+1;
+ *batlinevalue && isspace((unsigned char)*batlinevalue);
+ batlinevalue++);
+
+ if (!strcmp(batlinebuf,"ERROR"))
+ return batfailf("kernel reports error");
+
+ for (p=batlinebuf; p<colon; p++)
+ if (*p == ' ')
+ *p= '_';
+
+ for (field=fields; field->file; field++) {
+ if (!strcmp(field->file,batfilename) &&
+ !strcmp(field->label,batlinebuf))
+ goto label_interesting;
+ }
+ continue;
+
+ label_interesting:
+ if (field->unit) {
+
+ *field->valuep= strtoul(batlinevalue,&ep,10);
+ if (ep==batlinevalue || *ep!=' ')
+ batfailf("value number syntax incorrect");
+ if (strcmp(ep+1,field->unit)) batfailf("incorrect unit");
+
+ } else {
+
+ for (*field->valuep=0, enumsearch=field->enumarray;
+ *enumsearch && strcmp(*enumsearch,batlinevalue);
+ (*field->valuep)++, enumsearch++);
+ if (!*enumsearch)
+ batfailf("unknown enum value");
+
+ }
+ }
+
+ fclose(batfile);
+ batfile= 0;
+ }
+
+ if (!(thisbat_alarm_present==0 ||
+ thisbat_info_present==0 ||
+ thisbat_state_present==0)) {
+ if (thisbat_alarm_present == VAL_NOTFOUND)
+ thisbat_alarm_present= 1;
+
+ for (field=fields, missing=0;
+ field->file;
+ field++) {
+ if (*field->valuep == VAL_NOTFOUND) {
+ fprintf(stderr,"battery/%s/%s: %s: not found\n",
+ batdirname, field->file,field->label);
+ missing++;
+ }
+ }
+ if (missing) return -1;
+ }
+
+ r= chdir("..");
+ if (r) return batfaile("chdir","..");
+ batdirname= 0;
+
+ return 0;
+}
+
+/*---------- data collection and analysis ----------*/
+
+/* These next three variables are the results of the charging state */
+static unsigned charging_state_mask; /* 1u<<CHGST_* | ... */
+static double nondegraded_norm, fill_norm, ratepersec_norm;
+static int alarm_level; /* 0=ok, 1=low */
+
+#define QF(f,l,u) \
+ static double total_##f##_##l;
+ QUANTITY_FIELDS
+#undef QF
+
+static void acquiredata(void) {
+ DIR *di;
+ struct dirent *de;
+ int r;
+
+ charging_state_mask= 0;
+
+#define QF(f,l,u) \
+ total_##f##_##l= 0;
+ QUANTITY_FIELDS
+#undef QF
+
+ di= opendir("."); if (!di) batfaile("opendir","battery");
+ while ((de= readdir(di))) {
+ if (de->d_name[0]==0 || de->d_name[0]=='.') continue;
+
+ batdirname= de->d_name;
+ r= readbattery();
+ tidybattery();
+
+ if (r) { charging_state_mask |= CHGST_ERROR; continue; }
+
+ if (!thisbat_info_present || !thisbat_state_present)
+ continue;
+
+ charging_state_mask |= 1u << thisbat_state_charging_state;
+
+#define QF(f,l,u) \
+ total_##f##_##l += thisbat_##f##_##l;
+ QUANTITY_FIELDS
+#undef QF
+
+ if (thisbat_state_charging_state == CHGST_DISCHARGING)
+ /* negate it */
+ total_state_present_rate -= 2.0 * thisbat_state_present_rate;
+
+ }
+ closedir(di);
+
+ if (total_info_design_capacity < 0.5)
+ total_info_design_capacity= 1.0;
+
+ if (total_info_last_full_capacity < total_state_remaining_capacity)
+ total_info_last_full_capacity= total_state_remaining_capacity;
+ if (total_info_design_capacity < total_info_last_full_capacity)
+ total_info_design_capacity= total_info_last_full_capacity;
+
+ alarm_level=
+ (total_state_remaining_capacity < total_alarm_alarm &&
+ !(charging_state_mask & 1u << CHGST_CHARGING));
+
+ nondegraded_norm= total_info_last_full_capacity / total_info_design_capacity;
+ fill_norm= total_state_remaining_capacity / total_info_design_capacity;
+ ratepersec_norm= total_state_present_rate
+ / (3600.0 * total_info_design_capacity);
+}
+
+static void initacquire(void) {
+ chdir_base();
+}
+
+/*---------- argument parsing ----------*/
+
+#define COLOURS \
+ C(blue, discharging) \
+ C(green, charging) \
+ C(cyan, charged) \
+ C(grey, confusing) \
+ C(black, normal) \
+ C(red, low) \
+ C(dimgrey, degraded) \
+ C(darkgreen, absent) \
+ C(yellow, error) \
+ C(white, equilibrium) \
+ GC(remain) \
+ GC(white) \
+ GC(empty)
+
+static XrmDatabase xrm;
+static Display *disp;
+static int screen;
+
+static const char defaultresources[]=
+#define GC(g)
+#define C(c,u) \
+ "*" #u "Color: " #c "\n"
+ COLOURS
+#undef GC
+#undef C
+ ;
+
+#define S(s) ((char*)(s))
+static const XrmOptionDescRec optiontable[]= {
+ { S("-display"), S("*display"), XrmoptionSepArg },
+ { S("-geometry"), S("*geometry"), XrmoptionSepArg },
+#define GC(g)
+#define C(c,u) \
+ { S("-" #u "Color"), S("*" #u "Color"), XrmoptionSepArg }, \
+ { S("-" #u "Colour"), S("*" #u "Color"), XrmoptionSepArg },
+ COLOURS
+#undef GC
+#undef C
+};
+
+static const char *getresource(const char *want) {
+ char name_buf[256], class_buf[256];
+ XrmValue val;
+ char *rep_type_dummy;
+ int r;
+
+ assert(strlen(want) < 128);
+ sprintf(name_buf,"xacpi-simple.%s",want);
+ sprintf(class_buf,"Xacpi-Simple.%s",want);
+
+ r= XrmGetResource(xrm, name_buf,class_buf, &rep_type_dummy, &val);
+ if (!r) return 0;
+
+ return val.addr;
+}
+
+static void more_resources(const char *str, const char *why) {
+ XrmDatabase more;
+
+ if (!str) return;
+
+ more= XrmGetStringDatabase((char*)str);
+ if (!more) fail(why);
+ XrmCombineDatabase(more,&xrm,0);
+}
+
+static void parseargs(int argc, char **argv) {
+ Screen *screenscreen;
+
+ XrmInitialize();
+
+ XrmParseCommand(&xrm, (XrmOptionDescRec*)optiontable,
+ sizeof(optiontable)/sizeof(*optiontable),
+ program_name, &argc, argv);
+
+ if (argc>1) badusage();
+
+ disp= XOpenDisplay(getresource("display"));
+ if (!disp) fail("could not open display");
+
+ screen= DefaultScreen(disp);
+
+ screenscreen= ScreenOfDisplay(disp,screen);
+ if (!screenscreen) fail("screenofdisplay");
+ more_resources(XScreenResourceString(screenscreen), "screen resources");
+ more_resources(XResourceManagerString(disp), "display resources");
+ more_resources(defaultresources, "default resources");
+}
+
+/*---------- display ----------*/
+
+static Window win;