chiark / gitweb /
wip
[chiark-utils.git] / cprogs / xacpi-simple.c
index 48c2ff4c37ad2423d19c792dab86d4cc8b69cf17..73bea7f508973b40338751e7888ec435db4a095e 100644 (file)
@@ -1,16 +1,16 @@
 /*
  * 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
  *
@@ -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,13 +357,15 @@ static double total_##f;
   ALL_ACCUMULATE_FIELDS(Q_VAR)
 
 static void acquiredata(void) {
-  DIR *di;
+  DIR *di = 0;
   struct dirent *de;
   int r;
   
   charging_mask= 0;
   alarmed = 0;
 
+  if (debug) printf("\n");
+
 #define Q_ZERO(f,t,...) \
   total_##f= 0;
 ALL_ACCUMULATE_FIELDS(Q_ZERO)
@@ -425,9 +427,11 @@ ALL_PLAIN_ACCUMULATE_FIELDS(Q_ACCUMULATE_PLAIN)
 
       
   }
-  closedir(di);
+  if (di) closedir(di);
 
-  printf("TOTAL:\n");
+  if (debug) {
+    printf("TOTAL:\n");
+    printf(" %-30s = %#20x\n", "mask", charging_mask);
 #define T_PRINT(f,...)                                 \
     printf(" %-30s = %20.6f\n", #f, total_##f);
 BAT_QTYS(T_PRINT,,,)
@@ -463,17 +467,17 @@ static void initacquire(void) {
 /*---------- argument parsing ----------*/
 
 #define COLOURS                                        \
-  C(blue,      discharging)                    \
-  C(green,     charging)                       \
-  C(cyan,      charged)                                \
-  C(darkcyan,  notcharging)                    \
-  C(grey,      confusing)                      \
-  C(black,     normal)                         \
-  C(red,       low)                            \
-  C(dimgrey,   degraded)                       \
-  C(darkgreen, absent)                         \
-  C(yellow,    error)                          \
-  C(white,     equilibrium)                    \
+  C(blue,           discharging)               \
+  C(green,         charging)                   \
+  C(cyan,           charged)                   \
+  C(lightgrey,      notcharging)               \
+  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)
@@ -481,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)
@@ -497,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 }, \
@@ -522,6 +530,28 @@ 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 */
+
+  if (*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;
+      }
+    }
+  }
+  return *cache= result;
+}
+
 static void more_resources(const char *str, const char *why) {
   XrmDatabase more;
 
@@ -543,11 +573,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");
 
@@ -572,7 +604,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
@@ -618,23 +650,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<<CHGST_CHARGING)
-               ? pix_green : pix_blue);
+               ? pix_charging : pix_discharging);
                
-  setforeground(&gc_empty, alarmed ? pix_red : pix_black);
+  setforeground(&gc_empty, alarmed ? pix_low : pix_normal);
 
   for (i=0, first_beyond=1; i<height; i++) {
     elap= !i ? 0 :
@@ -672,7 +704,7 @@ static void initgc(Gcstate *gc_r) {
   memset(&gcv,0,sizeof(gcv));
   gcv.function= GXcopy;
   gcv.line_width= 1;
-  gc_r->lastfg= gcv.foreground= pix_white;
+  gc_r->lastfg= gcv.foreground= pix_equilibrium;
   gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
 }
 
@@ -716,19 +748,25 @@ 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);
   
-#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;
@@ -741,6 +779,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;
@@ -759,15 +800,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) {