From c73ccbf362115934d3d11bd6d86a6163c927e2b7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 4 Jun 2012 20:12:13 +0100 Subject: [PATCH] wip --- cprogs/xacpi-simple.c | 314 +++++++++++++++++++++++++----------------- 1 file changed, 188 insertions(+), 126 deletions(-) diff --git a/cprogs/xacpi-simple.c b/cprogs/xacpi-simple.c index cd2a1c7..034bc78 100644 --- a/cprogs/xacpi-simple.c +++ b/cprogs/xacpi-simple.c @@ -1,3 +1,8 @@ +todo + power vs current + Full + old way of doing alarm + /* * display outputs, per line: * @@ -6,6 +11,7 @@ * green | black | dimgrey charging * cyan | black | dimgrey charged * grey | black | dimgrey charging&discharching! + * darkcyan | black | dimgrey none of the above * blue | red | dimgrey discharging - low! * green | red | dimgrey charging - low * cyan | red | dimgrey charged - low [1] @@ -43,6 +49,7 @@ #include #include #include +#include #include #include @@ -68,51 +75,72 @@ static void fail(const char *m) { } static void badusage(void) { fail("bad usage"); } +typedef uint64_t value; +#define VAL_NOTFOUND (~(value)0) + typedef struct fileinfo fileinfo; +typedef int parser(const fileinfo*); + +static parser parse_uevent, parse_separate; + struct fileinfo { const char *filename; - void (*parse)(const fileinfo*); + parser *parse; const void *extra; }; /*---------- structure of and results from /sys/class/power/... ----------*/ /* variables thisbat_... are the results from readbattery(); - * if readbattery() succeeds they are all valid and not VAL_NOTFOUND + * if readbattery() succeeds the appropriate ones are all valid + * and not VAL_NOTFOUND */ typedef struct batinfo_field { const char *label; - uint64_t *valuep; + value *valuep; const char *enumarray[10]; } batinfo_field; -#define UEVENT_QUANTITY_FIELDS(q) \ - q(design_capacity, CHARGE_FULL_DESIGN ) /* uAh */ \ - q(last_full_capacity, CHARGE_FULL ) /* uAh */ \ - q(present_rate, CURRENT_NOW ) /* uA */ \ - q(remaining_capacity, CHARGE_NOW ) /* uAh */ \ - q(present, PRESENT ) /* boolean */ \ - q(online, ONLINE ) /* boolean */ - -#define UEVENT_ENUM_FIELDS(e) \ - e(type, TYPE, "Battery", "Mains" ) \ - e(state, STATUS, "Discharging", "Charging", "Charged" ) - -#define CHGST_DISCHARGING 0 /* Reflects order in e(state,...) above */ -#define CHGST_CHARGING 1 /* Also, much code assumes exadtly */ -#define CHGST_CHARGED 2 /* these three possible states. */ +#define UEVENT_QUANTITY_FIELDS(f) \ + f(design_capacity, BATTERY, CHARGE_FULL_DESIGN ) /* uAh */ \ + f(last_full_capacity, BATTERY, CHARGE_FULL ) /* uAh */ \ + f(present_rate, BATTERY, CURRENT_NOW ) /* uA */ \ + f(remaining_capacity, BATTERY, CHARGE_NOW ) /* uAh */ \ + f(present, BATTERY, PRESENT ) /* boolean */ \ + f(online, MAINS, ONLINE ) /* boolean */ + +#define UEVENT_ENUM_FIELDS(f) \ + f(state, BATTERY, STATUS, "Discharging","Charging","Charged","Unknown" ) \ + f(type, BOTH, TYPE, "Mains", "Battery" ) + +#define CHGST_DISCHARGING 0 /* Reflects order in f(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 Q_FLD(f,l) { "POWER_SUPPLY_" #l, &thisbat_##f }, -#define E_FLD(f,l,vl...) { "POWER_SUPPLY_" #l, &thisbat_##f, { vl } }, +#define TYPE_MAINS 0 /* Reflects order in f(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(f) \ + f(alarm, BATTERY, "alarm", "0", "1") + +#define ALL_FIELDS(f) \ + UEVENT_QUANTITY_FIELDS(f) \ + UEVENT_ENUM_FIELDS(f) \ + SEPARATE_QUANTITY_FIELDS(f) -#define ALL_FIELDS(i) \ - UEVENT_QUANTITY_FIELDS(i) \ - UEVENT_ENUM_FIELDS(i) \ - i(thisbat_alarm) +#define ALL_QUANTITY_FIELDS(f) \ + UEVENT_QUANTITY_FIELDS(f) \ + SEPARATE_QUANTITY_FIELDS(f) -#define F_VAR(f,...) static uint64_t thisbat_##f; - ALL_FIELDS(F_VAR) +#define F_VAR(f,...) \ +static value thisbat_##f; +ALL_FIELDS(F_VAR) + +#define Q_FLD(f,t,l) { "POWER_SUPPLY_" #l, &thisbat_##f }, +#define E_FLD(f,t,l,vl...) { "POWER_SUPPLY_" #l, &thisbat_##f, { vl } }, static const batinfo_field uevent_fields[]= { UEVENT_QUANTITY_FIELDS(Q_FLD) @@ -120,15 +148,19 @@ static const batinfo_field uevent_fields[]= { { 0 } }; -#define VAL_NOTFOUND (~0UL) +#define S_FLD(f,t,fn,vl...) \ +static const batinfo_field bif_##f = { 0, &thisbat_##f, { vl } }; + SEPARATE_QUANTITY_FIELDS(S_FLD) + +#define S_FILE(f,t,fn,vl...) { fn, parse_separate, &bif_##f }, static const fileinfo files[]= { - { "uevent", parse_fields, uevent_fields }, - { "alarm", parse_boolean, &thisbat_alarm }, + { "uevent", parse_uevent, uevent_fields }, + SEPARATE_QUANTITY_FIELDS(S_FILE) { 0 } }; -/*---------- parsing of one battery in /proc/acpi/battery/... ----------*/ +/*---------- parsing of one thingx in /sys/class/power/... ----------*/ /* variables private to the parser and its error handlers */ static char batlinebuf[1000]; @@ -139,84 +171,105 @@ 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 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("/sys/class/power_supply"); - if (r) batfaile("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 void parse_fields(const fileinfo *cfile, char *batlinebuf) { - equals= strchr(batlinebuf,'='); - if (!equals) - return batfailf("line without a equals"); - *equals++= 0; +static int parse_value(const fileinfo *cfile, const batinfo_field *field) { + if (*field->valuep != VAL_NOTFOUND) + return batfailf("value specified multiple times"); - for (field=fields; field->file; field++) { - if (!strcmp(field->label,batlinebuf)) - goto found; - } - return; - - found: if (!field->enumarray[0]) { - *field->valuep= strtoull(equals,&ep,10); + 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,equals); + *enumsearch && strcmp(*enumsearch,batlinevalue); (*field->valuep)++, enumsearch++); if (!*enumsearch) batfailf("unknown enum value"); } + return 0; } -static int readbattery(void) { /* 0=>ok, -1=>couldn't */ +static int parse_separate(const fileinfo *cfile) { + batlinevalue = batlinebuf; + return parse_value(cfile, cfile->extra); +} + +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; + 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; - const char *const *enumsearch; - char *colon, *ep, *sr, *p; - int r, l, missing; + char *sr; + int r, l; + r= chdir_base(); + if (r) return r; + r= chdir(batdirname); if (r) return batfaile("chdir",batdirname); -#define V_NOTFOUND(f,...) thisbat_##f = VAL_NOTFOUND; - ALL_FIELDS(V_NOTFOUND) - - for (field=fields; field->file; field++) - *field->valuep= VAL_NOTFOUND; +#define V_NOTFOUND(f,...) \ + thisbat_##f = VAL_NOTFOUND; +ALL_FIELDS(V_NOTFOUND) for (cfile=files; (batfilename= cfile->filename); - cfile++++) { + cfile++) { batfile= fopen(batfilename,"r"); - if (!batfile) return batfaile("open",batfilename); + if (!batfile) { + if (errno == ENOENT) continue; + return batfaile("open",batfilename); + } for (;;) { batlinevalue= 0; @@ -230,33 +283,29 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */ return batfailf("line too long"); batlinebuf[l-1]= 0; - cfile->parse(cfile, batlinebuf); + 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; + int needsfields_MAINS = thisbat_type == TYPE_MAINS; + int needsfields_BATTERY = thisbat_type == TYPE_BATTERY; + int needsfields_BOTH = 1; + + int missing = 0; + +#define V_NEEDED(f,t,...) \ + if (needsfields_##t && thisbat_##f == VAL_NOTFOUND) { \ + fprintf(stderr,"%s: %s: not found\n", \ + batdirname, #f); \ + missing++; \ } +ALL_FIELDS(V_NEEDED) - r= chdir(".."); - if (r) return batfaile("chdir",".."); - batdirname= 0; + if (missing) return -1; return 0; } @@ -264,28 +313,29 @@ 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; @@ -293,45 +343,48 @@ 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 (thisbat_type == TYPE_BATTERY) { + if (!thisbat_present) + continue; - charging_state_mask |= 1u << thisbat_state_charging_state; + charging_mask |= 1u << thisbat_state; -#define QF(f,l,u) \ - total_##f##_##l += thisbat_##f##_##l; - QUANTITY_FIELDS -#undef QF + if (thisbat_state == CHGST_DISCHARGING) + /* negate it */ + total_present_rate -= 2.0 * thisbat_present_rate; + } - if (thisbat_state_charging_state == CHGST_DISCHARGING) - /* negate it */ - total_state_present_rate -= 2.0 * thisbat_state_present_rate; +#define Q_TOTALISE(f,t,...) \ + if (thisbat_type == TYPE_##t) \ + total_##f += thisbat_##f; +ALL_QUANTITY_FIELDS(Q_TOTALISE) } closedir(di); - if (total_info_design_capacity < 0.5) - total_info_design_capacity= 1.0; + if (total_design_capacity < 0.5) + total_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; + if (total_last_full_capacity < total_remaining_capacity) + total_last_full_capacity= total_remaining_capacity; + if (total_design_capacity < total_last_full_capacity) + total_design_capacity= total_last_full_capacity; - alarm_level= - (total_state_remaining_capacity < total_alarm_alarm && - !(charging_state_mask & 1u << CHGST_CHARGING)); + alarm_level= total_alarm && (charging_mask & 1u << CHGST_DISCHARGING); - 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); + nondegraded_norm= total_last_full_capacity / total_design_capacity; + fill_norm= total_remaining_capacity / total_design_capacity; + ratepersec_norm= total_present_rate + / (3600.0 * total_design_capacity); } static void initacquire(void) { - chdir_base(); } /*---------- argument parsing ----------*/ @@ -340,6 +393,7 @@ static void initacquire(void) { C(blue, discharging) \ C(green, charging) \ C(cyan, charged) \ + C(darkcyan, notcharging) \ C(grey, confusing) \ C(black, normal) \ C(red, low) \ @@ -446,7 +500,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; @@ -623,9 +679,15 @@ static void refresh(void) { static void newgeometry(void) { int dummy; + unsigned int udummy, gotwidth, gotheight; Window dummyw; - XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy); + XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &gotwidth,&gotheight, + &udummy,&udummy); + assert(gotwidth < INT_MAX); + assert(gotheight < INT_MAX); + width = gotwidth; + height = gotheight; } static void eventloop(void) { @@ -642,7 +704,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); -- 2.30.2