X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=blobdiff_plain;f=cprogs%2Fxacpi-simple.c;h=f2ba491d93adbc41d8773eedf77b4564f90ece13;hp=20655ac94a7bf6714d1df10839dc5bc33cd2ca2c;hb=6f9f49dbd62f0889fbbbbc249d1ae33f4e91269e;hpb=51e3ad42cb6d9114fe2fb6b277adb59179e1f835 diff --git a/cprogs/xacpi-simple.c b/cprogs/xacpi-simple.c index 20655ac..f2ba491 100644 --- a/cprogs/xacpi-simple.c +++ b/cprogs/xacpi-simple.c @@ -1,15 +1,16 @@ /* * display outputs, per line: * - * Remaining: | Empty: | Degraded: - * blue | black | dimgrey discharging - * green | black | dimgrey charging - * cyan | black | dimgrey charged - * grey | black | dimgrey charging&discharching! - * blue | red | dimgrey discharging - low! - * green | red | dimgrey charging - low - * cyan | red | dimgrey charged - low [1] - * grey | red | dimgrey charging&discharching, low [1] + * Remaining: | Empty: | Degraded: + * blue | black | dimgrey discharging + * green | black | dimgrey charging + * cyan | black | dimgrey charged + * grey | black | dimgrey charging&discharching! + * lightgrey | black | dimgrey none of the above + * blue | red | dimgrey discharging - low! + * green | red | dimgrey charging - low + * cyan | red | dimgrey charged - low [1] + * grey | red | dimgrey charging&discharching, low [1] * ... darkgreen ... no batteries present * ... yellow ... error * @@ -42,6 +43,9 @@ #include #include #include +#include +#include +#include #include #include @@ -58,6 +62,7 @@ #define TIMEOUT_ONERROR 3333 /* milliseconds */ static const char program_name[]= "xacpi-simple"; +static int debug, alarmlevel; /*---------- general utility stuff and declarations ----------*/ @@ -67,58 +72,122 @@ static void fail(const char *m) { } static void badusage(void) { fail("bad usage"); } -#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. */ +typedef uint64_t value; +#define VAL_NOTFOUND (~(value)0) -/*---------- 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 fileinfo fileinfo; +typedef int parser(const fileinfo*); + +static parser parse_uevent; + +struct fileinfo { + const char *filename; + parser *parse; + const void *extra; +}; + +/*---------- structure of and results from /sys/class/power/... ----------*/ +/* variables this_... are the results from readbattery(); + * if readbattery() succeeds the appropriate ones are all valid + * and not VAL_NOTFOUND */ typedef struct batinfo_field { - const char *file; const char *label; - const char *acpi_label; - double acpi_conversion_factor; - unsigned long *valuep; - const char *unit; + value *valuep; const char *enumarray[10]; } batinfo_field; -#define QUANTITY_FIELDS \ - QF(info, design_capacity, "mWh", energy_full_design, 1e-3) \ - QF(info, last_full_capacity, "mWh", energy_full, 1e-3) \ - QF(state, present_rate, "mW", power_now, 1e-3) \ - QF(state, remaining_capacity, "mWh", energy_now, 1e-3) \ - QF(alarm, alarm, "mWh", alarm, 1e-3) - -#define QF(f,l,u,a,ac) 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), 0,0, { "no", "yes" } }, - { E(info, present), 0,0, { "no", "yes" } }, - { E(state,present), 0,0, { "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 +#define BAT_QTYS(_, _ec, EC_, PC_) \ + _(design_capacity##_ec, BATTERY, EC_##FULL_DESIGN ) \ + _(last_full_capacity##_ec, BATTERY, EC_##FULL ) \ + _(remaining_capacity##_ec, BATTERY, EC_##NOW ) \ + _(present_rate##_ec, BATTERY, PC_##NOW ) + /* ENERGY [mWh]; POWER [mW]; CHARGE [uAh]; CURRENT [uA] */ + +#define UEVENT_ESSENTIAL_QUANTITY_FIELDS(_) \ + _(present, BATTERY, PRESENT /* bool */ ) \ + _(online, MAINS, ONLINE /* bool */ ) + +#define UEVENT_FUNKY_QUANTITY_FIELDS(_) \ + BAT_QTYS(_,_energy,ENERGY_,POWER_) \ + BAT_QTYS(_,_charge,CHARGE_,CURRENT_) + +#define UEVENT_OPTIONAL_QUANTITY_FIELDS(_) \ + _(voltage, BATTERY, VOLTAGE_NOW /* uV */ ) + +#define UEVENT_ENUM_FIELDS(_) \ + _(state, BATTERY, STATUS, "Discharging","Charging","Full","Unknown" ) \ + _(type, BOTH, TYPE, "Mains", "Battery" ) + +#define CHGST_DISCHARGING 0 /* Reflects order in _(state,...) above */ +#define CHGST_CHARGING 1 /* Also, much code assumes exactly */ +#define CHGST_CHARGED 2 /* these three possible states. */ +#define CHGST_UNKNOWN 3 /* these three possible states. */ +#define CHGST_ERROR 8 /* Except that this one is an extra bit. */ + +#define TYPE_MAINS 0 /* Reflects order in _(type,...) above */ +#define TYPE_BATTERY 1 /* Also, much code assumes exactly these two */ +#define TYPE_BOTH 100 /* Except this is a magic invalid value. */ + +#define SEPARATE_QUANTITY_FIELDS(_) \ + /* See commit ec6f5f0be800bc5f2a27046833dba04e0c67ffac for + the code needed to use this */ + + +#define ALL_DIRECT_VARS(_) \ + UEVENT_ESSENTIAL_QUANTITY_FIELDS(_) \ + UEVENT_FUNKY_QUANTITY_FIELDS(_) \ + UEVENT_OPTIONAL_QUANTITY_FIELDS(_) \ + UEVENT_ENUM_FIELDS(_) \ + SEPARATE_QUANTITY_FIELDS(_) + +#define ALL_VARS(_) \ + ALL_DIRECT_VARS(_) \ + BAT_QTYS(_,,,) + +#define ALL_NEEDED_FIELDS(_) \ + UEVENT_ESSENTIAL_QUANTITY_FIELDS(_) \ + UEVENT_ENUM_FIELDS(_) \ + SEPARATE_QUANTITY_FIELDS(_) + +#define ALL_PLAIN_ACCUMULATE_FIELDS(_) \ + UEVENT_ESSENTIAL_QUANTITY_FIELDS(_) \ + SEPARATE_QUANTITY_FIELDS(_) + +#define ALL_ACCUMULATE_FIELDS(_) \ + ALL_PLAIN_ACCUMULATE_FIELDS(_) \ + BAT_QTYS(_,,,) + + +#define F_VAR(f,...) \ +static value this_##f; +ALL_VARS(F_VAR) + +#define Q_FLD(f,t,l) { "POWER_SUPPLY_" #l, &this_##f }, +#define E_FLD(f,t,l,vl...) { "POWER_SUPPLY_" #l, &this_##f, { vl } }, + +static const batinfo_field uevent_fields[]= { + UEVENT_ESSENTIAL_QUANTITY_FIELDS(Q_FLD) + UEVENT_FUNKY_QUANTITY_FIELDS(Q_FLD) + UEVENT_OPTIONAL_QUANTITY_FIELDS(Q_FLD) + UEVENT_ENUM_FIELDS(E_FLD) + { 0 } }; -static const char *files[]= { "info", "state", "alarm" }; +#define S_FLD(f,t,fn,vl...) \ +static const batinfo_field bif_##f = { 0, &this_##f, { vl } }; + SEPARATE_QUANTITY_FIELDS(S_FLD) -/*---------- parsing of one battery in /proc/acpi/battery/... ----------*/ +#define S_FILE(f,t,fn,vl...) { fn, parse_separate, &bif_##f }, + +static const fileinfo files[]= { + { "uevent", parse_uevent, uevent_fields }, + SEPARATE_QUANTITY_FIELDS(S_FILE) + { 0 } +}; + +/*---------- parsing of one thingx in /sys/class/power/... ----------*/ /* variables private to the parser and its error handlers */ static char batlinebuf[1000]; @@ -129,50 +198,106 @@ static const char *batlinevalue; static int batfailf(const char *why) { if (batlinevalue) { - fprintf(stderr,"battery/%s/%s: %s value `%s': %s\n", + fprintf(stderr,"%s/%s: %s value `%s': %s\n", batdirname,batfilename, batlinebuf,batlinevalue,why); } else { - fprintf(stderr,"battery/%s/%s: %s: `%s'\n", + fprintf(stderr,"%s/%s: %s: `%s'\n", batdirname,batfilename, why, batlinebuf); } return -1; } +static int batfailc(const char *why) { + fprintf(stderr,"%s/%s: %s\n", + batdirname,batfilename, why); + return -1; +} + static int batfaile(const char *syscall, const char *target) { - fprintf(stderr,"battery/%s: failed to %s %s: %s\n", + fprintf(stderr,"%s: failed to %s %s: %s\n", batdirname ? batdirname : "*", syscall, target, strerror(errno)); return -1; } -static void chdir_base(void) { +static int chdir_base(void) { int r; - r= chdir("/proc/acpi/battery"); - if (r) batfaile("chdir","/proc/acpi/battery"); + r= chdir("/sys/class/power_supply"); + if (r) return batfaile("chdir","/sys/class/power_supply"); + + return 0; } 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 */ +static int parse_value(const fileinfo *cfile, const batinfo_field *field) { + if (*field->valuep != VAL_NOTFOUND) + return batfailf("value specified multiple times"); + + if (!field->enumarray[0]) { + + char *ep; + *field->valuep= strtoull(batlinevalue,&ep,10); + if (*ep) + batfailf("value number syntax incorrect"); + + } else { + + const char *const *enumsearch; + for (*field->valuep=0, enumsearch=field->enumarray; + *enumsearch && strcmp(*enumsearch,batlinevalue); + (*field->valuep)++, enumsearch++); + if (!*enumsearch) + batfailf("unknown enum value"); + + } + return 0; +} + +static int parse_uevent(const fileinfo *cfile) { + char *equals= strchr(batlinebuf,'='); + if (!equals) + return batfailf("line without a equals"); + *equals= 0; + batlinevalue = equals+1; + const batinfo_field *field; - const char *const *cfilename, *const *enumsearch; - char *colon, *ep, *sr, *p; - int r, l, missing; + for (field=cfile->extra; field->label; field++) { + if (!strcmp(field->label,batlinebuf)) + goto found; + } + return 0; + + found: + return parse_value(cfile, field); +} + +static int readbattery(void) { /* 0=>ok, -1=>couldn't */ + + const fileinfo *cfile; + char *sr; + int r, l; + r= chdir_base(); + if (r) return r; + r= chdir(batdirname); if (r) return batfaile("chdir",batdirname); - for (field=fields; field->file; field++) - *field->valuep= VAL_NOTFOUND; +#define V_NOTFOUND(f,...) \ + this_##f = VAL_NOTFOUND; +ALL_VARS(V_NOTFOUND) - for (cfilename=files; - (batfilename= *cfilename); - cfilename++) { + for (cfile=files; + (batfilename= cfile->filename); + cfile++) { batfile= fopen(batfilename,"r"); - if (!batfile) return batfaile("open",batfilename); + if (!batfile) { + if (errno == ENOENT) continue; + return batfaile("open",batfilename); + } for (;;) { batlinevalue= 0; @@ -185,72 +310,37 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */ 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; pfile; 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"); - - } + if (cfile->parse(cfile)) + return -1; } fclose(batfile); batfile= 0; } - if (!(thisbat_alarm_present==0 || - thisbat_state_present==0 || thisbat_info_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; + if (debug) { + printf("%s:\n",batdirname); +#define V_PRINT(f,...) \ + printf(" %-30s = %20"PRId64"\n", #f, (int64_t)this_##f); +ALL_DIRECT_VARS(V_PRINT) } - r= chdir(".."); - if (r) return batfaile("chdir",".."); - batdirname= 0; + int needsfields_MAINS = this_type == TYPE_MAINS; + int needsfields_BATTERY = this_type == TYPE_BATTERY; + int needsfields_BOTH = 1; + + int missing = 0; + +#define V_NEEDED(f,t,...) \ + if (needsfields_##t && this_##f == VAL_NOTFOUND) { \ + fprintf(stderr,"%s: %s: not found\n", \ + batdirname, #f); \ + missing++; \ + } +ALL_NEEDED_FIELDS(V_NEEDED) + + if (missing) return -1; return 0; } @@ -258,28 +348,32 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */ /*---------- data collection and analysis ----------*/ /* These next three variables are the results of the charging state */ -static unsigned charging_state_mask; /* 1u<d_name[0]==0 || de->d_name[0]=='.') continue; @@ -287,60 +381,103 @@ static void acquiredata(void) { r= readbattery(); tidybattery(); - if (r) { charging_state_mask |= CHGST_ERROR; continue; } + if (r) { + bad: + charging_mask |= (1u << CHGST_ERROR); + break; + } - if (!thisbat_info_present || !thisbat_state_present) - continue; + if (this_type == TYPE_BATTERY) { + if (!this_present) + continue; + + charging_mask |= 1u << this_state; + +#define QTY_SUPPLIED(f,...) this_##f != VAL_NOTFOUND && +#define QTY_USE_ENERGY(f,...) this_##f = this_##f##_energy; +#define QTY_USE_CHARGE(f,...) this_##f = this_##f##_charge; + + double funky_multiplier; + if (BAT_QTYS(QTY_SUPPLIED,_energy,,) 1) { + if (debug) printf(" using energy\n"); + BAT_QTYS(QTY_USE_ENERGY,,,); + funky_multiplier = 1.0; + } else if (BAT_QTYS(QTY_SUPPLIED,_charge,,) + this_voltage != VAL_NOTFOUND) { + if (debug) printf(" using charge\n"); + BAT_QTYS(QTY_USE_CHARGE,,,); + funky_multiplier = this_voltage * 1e-6; + } else { + batfailc("neither complete set of energy nor charge"); + continue; + } + if (this_state == CHGST_DISCHARGING) + /* negate it */ + total_present_rate -= 2.0 * this_present_rate * funky_multiplier; - charging_state_mask |= 1u << thisbat_state_charging_state; +#define Q_ACCUMULATE_FUNKY(f,...) \ + total_##f += this_##f * funky_multiplier; +BAT_QTYS(Q_ACCUMULATE_FUNKY,,,) + } -#define QF(f,l,u) \ - total_##f##_##l += thisbat_##f##_##l; - QUANTITY_FIELDS -#undef QF +#define Q_ACCUMULATE_PLAIN(f,t,...) \ + if (this_type == TYPE_##t) \ + total_##f += this_##f; +ALL_PLAIN_ACCUMULATE_FIELDS(Q_ACCUMULATE_PLAIN) - if (thisbat_state_charging_state == CHGST_DISCHARGING) - /* negate it */ - total_state_present_rate -= 2.0 * thisbat_state_present_rate; } - closedir(di); + if (di) closedir(di); + + if (debug) { + printf("TOTAL:\n"); + printf(" %-30s = %#20x\n", "mask", charging_mask); +#define T_PRINT(f,...) \ + printf(" %-30s = %20.6f\n", #f, total_##f); +BAT_QTYS(T_PRINT,,,) +ALL_PLAIN_ACCUMULATE_FIELDS(T_PRINT) + } - if (total_info_design_capacity < 0.5) - total_info_design_capacity= 1.0; + if ((charging_mask & (1u<1) badusage(); + debug= !!getresource("debug"); + + const char *alarmlevel_string= getresource("alarmLevel"); + alarmlevel = alarmlevel_string ? atoi(alarmlevel_string) : 300; + disp= XOpenDisplay(getresource("display")); if (!disp) fail("could not open display"); @@ -432,7 +576,7 @@ typedef struct { unsigned long lastfg; } Gcstate; -#define C(c,u) static unsigned long pix_##c; +#define C(c,u) static unsigned long pix_##u; #define GC(g) static Gcstate gc_##g; COLOURS #undef C @@ -440,7 +584,7 @@ typedef struct { static void refresh(void); -#define CHGMASK_CHG_DIS (1u<=nondegraded_norm)); + ((charging_mask & (1u<=nondegraded_norm)); if (then <= 0.0) then= 0.0; else if (then >= nondegraded_norm) then= nondegraded_norm; @@ -530,7 +676,7 @@ static void initgc(Gcstate *gc_r) { memset(&gcv,0,sizeof(gcv)); gcv.function= GXcopy; gcv.line_width= 1; - gc_r->lastfg= gcv.foreground= pix_white; + gc_r->lastfg= gcv.foreground= pix_equilibrium; gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv); } @@ -578,15 +724,15 @@ static void initgraphics(int argc, char **argv) { pos_x,pos_y,width,height,0,0,0); cmap= DefaultColormap(disp,screen); -#define C(c,u) colour(&pix_##c, #u "Color"); +#define C(c,u) colour(&pix_##u, #u "Color"); #define GC(g) initgc(&gc_##g); COLOURS #undef C #undef GC - r= XSetWindowBackground(disp,win,pix_dimgrey); + r= XSetWindowBackground(disp,win,pix_degraded); if (!r) fail("init set background"); - lastbackground= pix_dimgrey; + lastbackground= pix_degraded; normal_hints->flags= PWinGravity; normal_hints->win_gravity= gravity; @@ -636,7 +782,7 @@ static void eventloop(void) { pfd.fd= ConnectionNumber(disp); pfd.events= POLLIN|POLLERR; - timeout= !(charging_state_mask & CHGST_ERROR) ? TIMEOUT : TIMEOUT_ONERROR; + timeout= !(charging_mask & (1u << CHGST_ERROR)) ? TIMEOUT : TIMEOUT_ONERROR; r= poll(&pfd,1,timeout); if (r==-1 && errno!=EINTR) failr("poll",errno);