From: ianmdlvl Date: Sun, 12 Dec 2004 18:06:02 +0000 (+0000) Subject: x resources and command-line arguments X-Git-Tag: debian_version_4_0_99_0_12~3 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=chiark-utils.git;a=commitdiff_plain;h=004f87de1499fa37d381633dab590558257596bf x resources and command-line arguments --- diff --git a/cprogs/xacpi-simple.c b/cprogs/xacpi-simple.c index be9b2cc..d18624f 100644 --- a/cprogs/xacpi-simple.c +++ b/cprogs/xacpi-simple.c @@ -31,6 +31,7 @@ #include #include +#include #define TOP 60 #define BOTTOM 3600 @@ -308,23 +309,78 @@ static void initacquire(void) { chdir_base(); } -/*---------- display ----------*/ +/*---------- argument parsing ----------*/ #define COLOURS \ - C(dimgrey) \ - C(blue) \ - C(black) \ - C(red) \ - C(green) \ - C(cyan) \ - C(grey) \ - C(darkgreen) \ - C(yellow) \ - C(white) \ + 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 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 void parseargs(int argc, char **argv) { + XrmInitialize(); + + xrm= XrmGetStringDatabase((char*)defaultresources); + if (!xrm) fail("xrmgetstringdatabase"); + + XrmParseCommand(&xrm, (XrmOptionDescRec*)optiontable, + sizeof(optiontable)/sizeof(*optiontable), + program_name, &argc, argv); + + /* if (argc>0) badusage();*/ +} + +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; +} + +/*---------- display ----------*/ + static Display *disp; static Window win; static int width, height; @@ -337,7 +393,7 @@ typedef struct { unsigned long lastfg; } Gcstate; -#define C(c) static unsigned long pix_##c; +#define C(c,u) static unsigned long pix_##c; #define GC(g) static Gcstate gc_##g; COLOURS #undef C @@ -439,9 +495,13 @@ static void initgc(Gcstate *gc_r) { gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv); } -static void colour(unsigned long *pix_r, const char *name) { +static void colour(unsigned long *pix_r, const char *whichcolour) { XColor xc; + const char *name; Status st; + + name= getresource(whichcolour); + if (!name) fail("get colour resource"); st= XAllocNamedColor(disp,cmap,name,&xc,&xc); if (!st) fail(name); @@ -460,7 +520,7 @@ static void initgraphics(int argc, char **argv) { program_name_silly= (char*)program_name; - disp= XOpenDisplay(0); + disp= XOpenDisplay(getresource("display")); if (!disp) fail("could not open display"); screen= DefaultScreen(disp); @@ -472,7 +532,7 @@ static void initgraphics(int argc, char **argv) { if (!normal_hints || !wm_hints || !class_hint) fail("could not alloc hint(s)"); - geom_string= argc>=2 ? argv[1] : 0; + geom_string= getresource("geometry"); r= XWMGeometry(disp,screen, geom_string,"128x32", 0, normal_hints, @@ -484,7 +544,7 @@ static void initgraphics(int argc, char **argv) { pos_x,pos_y,width,height,0,0,0); cmap= DefaultColormap(disp,screen); -#define C(c) colour(&pix_##c, #c); +#define C(c,u) colour(&pix_##c, #u "Color"); #define GC(g) initgc(&gc_##g); COLOURS #undef C @@ -558,9 +618,7 @@ static void eventloop(void) { } int main(int argc, char **argv) { - if (argc<1 || argc>2) - badusage(); - + parseargs(argc,argv); initacquire(); initgraphics(argc,argv); eventloop();