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=034bc78df05c482dce54fb6edad7345a9a2577b8;hb=6f9f49dbd62f0889fbbbbc249d1ae33f4e91269e;hpb=c73ccbf362115934d3d11bd6d86a6163c927e2b7 diff --git a/cprogs/xacpi-simple.c b/cprogs/xacpi-simple.c index 034bc78..f2ba491 100644 --- a/cprogs/xacpi-simple.c +++ b/cprogs/xacpi-simple.c @@ -1,21 +1,16 @@ -todo - power vs current - Full - old way of doing alarm - /* * display outputs, per line: * - * Remaining: | Empty: | Degraded: - * blue | black | dimgrey discharging - * 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] - * 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 * @@ -50,6 +45,7 @@ todo #include #include #include +#include #include #include @@ -66,6 +62,7 @@ todo #define TIMEOUT_ONERROR 3333 /* milliseconds */ static const char program_name[]= "xacpi-simple"; +static int debug, alarmlevel; /*---------- general utility stuff and declarations ----------*/ @@ -81,7 +78,7 @@ typedef uint64_t value; typedef struct fileinfo fileinfo; typedef int parser(const fileinfo*); -static parser parse_uevent, parse_separate; +static parser parse_uevent; struct fileinfo { const char *filename; @@ -90,7 +87,7 @@ struct fileinfo { }; /*---------- structure of and results from /sys/class/power/... ----------*/ -/* variables thisbat_... are the results from readbattery(); +/* variables this_... are the results from readbattery(); * if readbattery() succeeds the appropriate ones are all valid * and not VAL_NOTFOUND */ @@ -101,55 +98,85 @@ typedef struct batinfo_field { const char *enumarray[10]; } batinfo_field; -#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 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(f) \ - f(state, BATTERY, STATUS, "Discharging","Charging","Charged","Unknown" ) \ - f(type, BOTH, TYPE, "Mains", "Battery" ) +#define UEVENT_ENUM_FIELDS(_) \ + _(state, BATTERY, STATUS, "Discharging","Charging","Full","Unknown" ) \ + _(type, BOTH, TYPE, "Mains", "Battery" ) -#define CHGST_DISCHARGING 0 /* Reflects order in f(state,...) above */ +#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 f(type,...) above */ +#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(f) \ - f(alarm, BATTERY, "alarm", "0", "1") +#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_FIELDS(f) \ - UEVENT_QUANTITY_FIELDS(f) \ - UEVENT_ENUM_FIELDS(f) \ - SEPARATE_QUANTITY_FIELDS(f) +#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 ALL_QUANTITY_FIELDS(f) \ - UEVENT_QUANTITY_FIELDS(f) \ - SEPARATE_QUANTITY_FIELDS(f) #define F_VAR(f,...) \ -static value thisbat_##f; -ALL_FIELDS(F_VAR) +static value this_##f; +ALL_VARS(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 } }, +#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_QUANTITY_FIELDS(Q_FLD) + UEVENT_ESSENTIAL_QUANTITY_FIELDS(Q_FLD) + UEVENT_FUNKY_QUANTITY_FIELDS(Q_FLD) + UEVENT_OPTIONAL_QUANTITY_FIELDS(Q_FLD) UEVENT_ENUM_FIELDS(E_FLD) { 0 } }; #define S_FLD(f,t,fn,vl...) \ -static const batinfo_field bif_##f = { 0, &thisbat_##f, { vl } }; +static const batinfo_field bif_##f = { 0, &this_##f, { vl } }; SEPARATE_QUANTITY_FIELDS(S_FLD) #define S_FILE(f,t,fn,vl...) { fn, parse_separate, &bif_##f }, @@ -180,6 +207,12 @@ static int batfailf(const char *why) { 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,"%s: failed to %s %s: %s\n", batdirname ? batdirname : "*", syscall, target, strerror(errno)); @@ -223,11 +256,6 @@ static int parse_value(const fileinfo *cfile, const batinfo_field *field) { return 0; } -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) @@ -259,8 +287,8 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */ if (r) return batfaile("chdir",batdirname); #define V_NOTFOUND(f,...) \ - thisbat_##f = VAL_NOTFOUND; -ALL_FIELDS(V_NOTFOUND) + this_##f = VAL_NOTFOUND; +ALL_VARS(V_NOTFOUND) for (cfile=files; (batfilename= cfile->filename); @@ -291,19 +319,26 @@ ALL_FIELDS(V_NOTFOUND) batfile= 0; } - int needsfields_MAINS = thisbat_type == TYPE_MAINS; - int needsfields_BATTERY = thisbat_type == TYPE_BATTERY; + 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) + } + + 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 && thisbat_##f == VAL_NOTFOUND) { \ + if (needsfields_##t && this_##f == VAL_NOTFOUND) { \ fprintf(stderr,"%s: %s: not found\n", \ batdirname, #f); \ missing++; \ } -ALL_FIELDS(V_NEEDED) +ALL_NEEDED_FIELDS(V_NEEDED) if (missing) return -1; @@ -315,22 +350,25 @@ ALL_FIELDS(V_NEEDED) /* These next three variables are the results of the charging state */ static unsigned 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"); @@ -492,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 @@ -538,23 +622,23 @@ static void show(void) { int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond; if (!charging_mask) - return show_solid(pix_darkgreen); + return show_solid(pix_absent); if (charging_mask & (1u << CHGST_ERROR)) - return show_solid(pix_yellow); + return show_solid(pix_error); - setbackground(pix_dimgrey); + setbackground(pix_degraded); XClearWindow(disp,win); setforeground(&gc_remain, !(charging_mask & CHGMASK_CHG_DIS) ? (~charging_mask & (1u << CHGST_CHARGED) ? - pix_darkcyan : pix_cyan) : - !(~charging_mask & CHGMASK_CHG_DIS) ? pix_grey : + pix_notcharging : pix_charged) : + !(~charging_mask & CHGMASK_CHG_DIS) ? pix_confusing : charging_mask & (1u<lastfg= gcv.foreground= pix_white; + gc_r->lastfg= gcv.foreground= pix_equilibrium; gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv); } @@ -640,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; @@ -679,15 +763,9 @@ static void refresh(void) { static void newgeometry(void) { int dummy; - unsigned int udummy, gotwidth, gotheight; Window dummyw; - XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &gotwidth,&gotheight, - &udummy,&udummy); - assert(gotwidth < INT_MAX); - assert(gotheight < INT_MAX); - width = gotwidth; - height = gotheight; + XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy); } static void eventloop(void) {