#include <X11/Xlib.h>
#include <X11/Xutil.h>
+#include <X11/Xresource.h>
#define TOP 60
#define BOTTOM 3600
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 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;
static int width, height;
static Colormap cmap;
-static int screen;
static unsigned long lastbackground;
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
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);
}
static void initgraphics(int argc, char **argv) {
- int r;
+ int xwmgr, r;
const char *geom_string;
XSizeHints *normal_hints;
XWMHints *wm_hints;
program_name_silly= (char*)program_name;
- disp= XOpenDisplay(0);
- if (!disp) fail("could not open display");
-
- screen= DefaultScreen(disp);
-
normal_hints= XAllocSizeHints();
wm_hints= XAllocWMHints();
class_hint= XAllocClassHint();
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,
+ xwmgr= XWMGeometry(disp,screen, geom_string,"128x32", 0,
normal_hints,
&pos_x, &pos_y,
&width, &height,
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
normal_hints->y= pos_y;
normal_hints->width= width;
normal_hints->height= height;
- if (geom_string) normal_hints->flags |= USPosition;
+ if ((xwmgr & XValue) || (xwmgr & YValue))
+ normal_hints->flags |= USPosition;
wm_hints->flags= InputHint;
wm_hints->input= False;
}
int main(int argc, char **argv) {
- if (argc<1 || argc>2)
- badusage();
-
+ parseargs(argc,argv);
initacquire();
initgraphics(argc,argv);
eventloop();