chiark / gitweb /
bool cache optional
[chiark-utils.git] / cprogs / xacpi-simple.c
index a7b584cf012f053edea01e59b30cee5d1a750bf5..eb761a290635ca85ab8f80db11b6d26444c26dca 100644 (file)
@@ -62,7 +62,7 @@
 #define TIMEOUT_ONERROR 3333 /* milliseconds */
 
 static const char program_name[]= "xacpi-simple";
-static int debug, alarmlevel;
+static int debug=-1, alarmlevel;
 
 /*---------- general utility stuff and declarations ----------*/
 
@@ -357,7 +357,7 @@ static double total_##f;
   ALL_ACCUMULATE_FIELDS(Q_VAR)
 
 static void acquiredata(void) {
-  DIR *di;
+  DIR *di = 0;
   struct dirent *de;
   int r;
   
@@ -427,7 +427,7 @@ ALL_PLAIN_ACCUMULATE_FIELDS(Q_ACCUMULATE_PLAIN)
 
       
   }
-  closedir(di);
+  if (di) closedir(di);
 
   if (debug) {
     printf("TOTAL:\n");
@@ -485,6 +485,7 @@ static void initacquire(void) {
 static XrmDatabase xrm;
 static Display *disp;
 static int screen;
+static const char *parentwindow;
 
 static const char defaultresources[]=
 #define GC(g)
@@ -501,6 +502,9 @@ static const XrmOptionDescRec optiontable[]= {
   { S("-warningTime"),  S("*warningTime"),  XrmoptionSepArg },
   { S("-display"),      S("*display"),      XrmoptionSepArg },
   { S("-geometry"),     S("*geometry"),     XrmoptionSepArg },
+  { S("-into"),         S("*parentWindow"), XrmoptionSepArg },
+  { S("-icon"),         S("*icon"),         XrmoptionIsArg },
+  { S("-withdrawn"),    S("*withdrawn"),    XrmoptionIsArg },
 #define GC(g)
 #define C(c,u)                                                 \
   { S("-" #u "Color"),  S("*" #u "Color"),  XrmoptionSepArg }, \
@@ -526,6 +530,32 @@ static const char *getresource(const char *want) {
   return val.addr;
 }
 
+static int getresource_bool(const char *want, int def, int *cache) {
+  /* *cache should be initialised to -1 and will be set to !!value
+   * alternatively cache==0 is allowed */
+
+  if (cache && *cache >= 0) return *cache;
+
+  const char *str= getresource(want);
+  int result = def;
+  if (str && str[0]) {
+    char *ep;
+    long l= strtol(str,&ep,0);
+    if (!*ep) {
+      result = l > 0;
+    } else {
+      switch (str[0]) {
+      case 't': case 'T': case 'y': case 'Y':         result= 1;  break;
+      case 'f': case 'F': case 'n': case 'N':         result= 0;  break;
+      case '-': /* option name from XrmoptionIsArg */ result= 1;  break;
+      }
+    }
+  }
+
+  if (cache) *cache= result;
+  return result;
+}
+
 static void more_resources(const char *str, const char *why) {
   XrmDatabase more;
 
@@ -547,11 +577,13 @@ static void parseargs(int argc, char **argv) {
 
   if (argc>1) badusage();
 
-  debug= !!getresource("debug");
+  getresource_bool("debug",0,&debug);
 
   const char *alarmlevel_string= getresource("alarmLevel");
   alarmlevel = alarmlevel_string ? atoi(alarmlevel_string) : 300;
 
+  parentwindow = getresource("parentWindow");
+
   disp= XOpenDisplay(getresource("display"));
   if (!disp) fail("could not open display");
 
@@ -720,7 +752,13 @@ static void initgraphics(int argc, char **argv) {
                 &width, &height,
                 &gravity);
 
-  win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),
+  unsigned long parentwindowid;
+  if (parentwindow)
+    parentwindowid = strtoul(parentwindow,0,0);
+  else
+    parentwindowid = DefaultRootWindow(disp);
+
+  win= XCreateSimpleWindow(disp,parentwindowid,
                           pos_x,pos_y,width,height,0,0,0);
   cmap= DefaultColormap(disp,screen);
   
@@ -745,6 +783,9 @@ static void initgraphics(int argc, char **argv) {
 
   wm_hints->flags= InputHint;
   wm_hints->input= False;
+  wm_hints->initial_state= (getresource("withdrawn") ? WithdrawnState :
+                           getresource("icon") ? IconicState
+                           : NormalState);
 
   class_hint->res_name= program_name_silly;
   class_hint->res_class= program_name_silly;
@@ -763,15 +804,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) {