chiark / gitweb /
consolidate changelog
[chiark-utils.git] / cprogs / xacpi-simple.c
index be9b2cc21f5653089b8c792de795ea2023a2d9fa..4adbfc12f91bb8da75b608756b25408de4cae6b9 100644 (file)
@@ -31,6 +31,7 @@
 
 #include <X11/Xlib.h>
 #include <X11/Xutil.h>
+#include <X11/Xresource.h>
 
 #define TOP      60
 #define BOTTOM 3600
@@ -308,28 +309,103 @@ 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 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 {
@@ -337,7 +413,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 +515,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);
@@ -450,7 +530,7 @@ static void colour(unsigned long *pix_r, const char *name) {
 }
 
 static void initgraphics(int argc, char **argv) {
-  int r;
+  int xwmgr, r;
   const char *geom_string;
   XSizeHints *normal_hints;
   XWMHints *wm_hints;
@@ -460,11 +540,6 @@ static void initgraphics(int argc, char **argv) {
   
   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();
@@ -472,9 +547,9 @@ 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,
+  xwmgr= XWMGeometry(disp,screen, geom_string,"128x32", 0,
                 normal_hints,
                 &pos_x, &pos_y,
                 &width, &height,
@@ -484,7 +559,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
@@ -500,7 +575,8 @@ static void initgraphics(int argc, char **argv) {
   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;
@@ -558,9 +634,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();