chiark / gitweb /
internal connection numbers thing which does not work - but much other works
[chiark-utils.git] / cprogs / xacpi-simple.c
index 687127aa031d5212f9aa05532e45c70b3d882bfe..37a2e31d2f4368eaf620ee6212546162903312b0 100644 (file)
@@ -1,16 +1,16 @@
 /*
  * display outputs, per line:
  *
- *    Remaining: | Empty:   | Degraded:
- *     green     |  red     |  black      discharging
- *     blue      |  red     |  black      charging
- *     cyan      |  red     |  black      charged
- *     grey      |  red     |  black      charging&discharching!
- *     green     |  yellow  |  black      discharging - low!
- *     blue      |  yellow  |  black      charging - low
- *     cyan      |  yellow  |  black      charged - low [1]
- *     grey      |  yellow  |  black      charging&discharching! - low [1]
- *       ...  dark grey  ...              no batteries present
+ *   Remaining:        | Empty:        | Degraded:
+ *     blue    |  black        |  dimgrey      discharging
+ *     green   |  black        |  dimgrey      charging
+ *     cyan    |  black        |  dimgrey      charged
+ *     grey    |  black        |  dimgrey      charging&discharching!
+ *     blue    |  red          |  dimgrey      discharging - low!
+ *     green   |  red          |  dimgrey      charging - low
+ *     cyan    |  red          |  dimgrey      charged - low [1]
+ *     grey    |  red          |  dimgrey      charging&discharching, low [1]
+ *       ...  darkmagenta  ...                 no batteries present
  *
  * [1] battery must be quite badly degraded
  */
 #include <unistd.h>
 #include <ctype.h>
 
+#include <sys/poll.h>
 #include <sys/types.h>
 #include <dirent.h>
 
 #include <X11/Xlib.h>
 
+#define TIMEOUT 5000 /* milliseconds */
+
 /*---------- general utility stuff and declarations ----------*/
 
 static void fail(const char *m) {
   fprintf(stderr,"error: %s\n", m);
-  sleep(5);
   exit(-1);
 }
 static void badusage(void) { fail("bad usage"); }
@@ -56,7 +58,7 @@ typedef struct batinfo_field {
 } batinfo_field;
 
 #define QUANTITY_FIELDS                                \
-  QF(info,  design_capacity, "mWh")            \
+  QF(info,  design_capacity,    "mWh")         \
   QF(info,  last_full_capacity, "mWh")         \
   QF(state, present_rate,       "mW")          \
   QF(state, remaining_capacity, "mWh")         \
@@ -74,9 +76,9 @@ static unsigned long thisbat_state_charging_state;
 static const batinfo_field fields[]= {
 #define E(f,l)       #f, #l, &thisbat_##f##_##l, 0
 #define QF(f,l,u)  { #f, #l, &thisbat_##f##_##l, u },
-  { E(info, present),            { "no", "yes" }                    },
-  { E(state,present),            { "no", "yes" }                    },
-  { E(state,charging_state),     { "discharging charging charged" } },
+  { E(info, present),            { "no", "yes" }                          },
+  { E(state,present),            { "no", "yes" }                          },
+  { E(state,charging_state),     { "discharging", "charging", "charged" } },
   QUANTITY_FIELDS           /* take care re charging_state values order -  */
   { 0 }                     /* if you must change it, search for CHGST_... */
 #undef E
@@ -126,7 +128,7 @@ static void tidybattery(void) {
 static int readbattery(void) { /* 0=>ok, -1=>couldn't */
   const batinfo_field *field;
   const char *const *cfilename, *const *enumsearch;
-  char *colon, *ep;
+  char *colon, *ep, *sr, *p;
   int r, l, missing;
   
   r= chdir(batdirname);
@@ -144,10 +146,10 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */
     for (;;) {
       batlinevalue= 0;
       
-      fgets(batlinebuf,sizeof(batlinebuf),batfile);
+      sr= fgets(batlinebuf,sizeof(batlinebuf),batfile);
       if (ferror(batfile)) return batfaile("read",batfilename);
+      if (!sr && feof(batfile)) break;
       l= strlen(batlinebuf);
-      if (l==0 && feof(batfile)) break;
       assert(l>0);
       if (batlinebuf[l-1] != '\n')
        return batfailf("line too long");
@@ -157,7 +159,11 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */
        return batfailf("line without a colon");
       *colon= 0;
 
-      for (field=fields; ; field++) {
+      for (p=batlinebuf; p<colon; p++)
+       if (*p == ' ')
+         *p= '_';
+
+      for (field=fields; field->file; field++) {
        if (!strcmp(field->file,batfilename) &&
            !strcmp(field->label,batlinebuf))
          goto label_interesting;
@@ -191,7 +197,7 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */
     batfile= 0;
   }
 
-  r= chdir(batdirname);
+  r= chdir("..");
   if (r) return batfaile("chdir","..");
   batdirname= 0;
 
@@ -290,15 +296,17 @@ static void initacquire(void) {
 #define BOTTOM 3600
 
 #define COLOURS                                        \
+  C(dimgrey)                                   \
+  C(blue)                                      \
   C(black)                                     \
-  C(green)                                     \
   C(red)                                       \
-  C(yellow)                                    \
-  C(blue)                                      \
+  C(green)                                     \
   C(cyan)                                      \
   C(grey)                                      \
-  C(darkgrey)                                  \
+  C(darkmagenta)                               \
+  C(white)                                     \
   GC(remain)                                   \
+  GC(white)                                    \
   GC(empty)
 
 static Display *disp;
@@ -323,12 +331,17 @@ static void refresh(void);
 
 #define CHGMASK_CHG_DIS (1u<<CHGST_CHARGING | 1u<<CHGST_DISCHARGING)
 
+static void failr(const char *m, int r) {
+  fprintf(stderr,"error: %s (code %d)\n", m, r);
+  exit(-1);
+}
+
 static void setbackground(unsigned long newbg) {
   int r;
   
   if (newbg == lastbackground) return;
   r= XSetWindowBackground(disp,win,newbg);
-  if (r) fail("XSetWindowBackground");
+  if (r) failr("XSetWindowBackground",r);
   lastbackground= newbg;
 }
 
@@ -341,47 +354,57 @@ static void setforeground(Gcstate *g, unsigned long px) {
   memset(&gcv,0,sizeof(gcv));
   g->lastfg= gcv.foreground= px;
   r= XChangeGC(disp,g->gc,GCForeground,&gcv);
-  if (r) fail("XChangeGC");
+  if (!r) fail("XChangeGC");
 }
 
 static void show(void) {
   double elap, then;
-  int i, leftmost_lit, leftmost_nondeg;
+  int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
 
   if (!charging_state_mask) {
-    setbackground(pix_darkgrey);
+    setbackground(pix_darkmagenta);
     XClearWindow(disp,win);
     return;
   }
 
-  setbackground(pix_black);
+  setbackground(pix_dimgrey);
   XClearWindow(disp,win);
   
   setforeground(&gc_remain,
                !(charging_state_mask & CHGMASK_CHG_DIS) ? pix_cyan :
                !(~charging_state_mask & CHGMASK_CHG_DIS) ? pix_grey :
                charging_state_mask & (1u<<CHGST_CHARGING)
-               ? pix_blue : pix_green);
+               ? pix_green : pix_blue);
                
-  setforeground(&gc_empty, alarm_level ? pix_yellow : pix_red);
+  setforeground(&gc_empty, alarm_level ? pix_red : pix_black);
 
-  for (i=0; i<height; i++) {
+  for (i=0, first_beyond=1; i<height; i++) {
     elap= !i ? 0 :
       height==2 ? BOTTOM :
       TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
     
     then= fill_norm + ratepersec_norm * elap;
+
+    beyond=
+      ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
+       (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
+
     if (then <= 0.0) then= 0.0;
     else if (then >= nondegraded_norm) then= nondegraded_norm;
 
     leftmost_lit= width * then;
     leftmost_nondeg= width * nondegraded_norm;
 
-    if (leftmost_lit >= 0)
-      XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
-    if (leftmost_lit < leftmost_nondeg)
-      XDrawLine(disp, win, gc_empty.gc,
-               leftmost_lit+1,i, leftmost_nondeg,i);
+    if (beyond && first_beyond) {
+      XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
+      first_beyond= 0;
+    } else {
+      if (leftmost_lit < leftmost_nondeg)
+       XDrawLine(disp, win, gc_empty.gc,
+                 leftmost_lit,i, leftmost_nondeg,i);
+      if (leftmost_lit >= 0)
+       XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
+    }
   }
 }
 
@@ -391,7 +414,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_black;
+  gc_r->lastfg= gcv.foreground= pix_white;
   gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
 }
 
@@ -400,7 +423,7 @@ static void colour(unsigned long *pix_r, const char *name) {
   Status st;
   
   st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
-  if (!st) fail("couldn't allocate colour");
+  if (!st) fail(name);
   
   *pix_r= xc.pixel;
 }
@@ -420,9 +443,9 @@ static void initgraphics(void) {
 #undef C
 #undef GC
 
-  r= XSetWindowBackground(disp,win,pix_black);
-  if (r) fail("init set background");
-  lastbackground= pix_black;
+  r= XSetWindowBackground(disp,win,pix_dimgrey);
+  if (!r) fail("init set background");
+  lastbackground= pix_dimgrey;
 
   XSelectInput(disp,win, ExposureMask|VisibilityChangeMask);
   XMapWindow(disp,win);
@@ -438,17 +461,51 @@ static void newgeometry(void) {
   Window dummyw;
   
   XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
-  refresh();
 }
 
 static void eventloop(void) {
   XEvent ev;
+  int *fds, nfds, npfds, *fdp, i, r;
+  struct pollfd *pfds, *pfd;
   
   newgeometry();
-  
+
+  npfds= 0;
+  pfds= 0;
   for (;;) {
-    XNextEvent(disp,&ev);
-    newgeometry();
+    r= XInternalConnectionNumbers(disp, &fds, &nfds);
+    if (!r) fail("XInternalConnectionNumbers");
+
+    if (npfds != nfds) {
+      pfds= realloc(pfds, sizeof(*pfds) * nfds);
+      if (!pfds) failr("realloc for pollfds",errno);
+      npfds= nfds;
+    }
+    for (i=0, pfd=pfds, fdp=fds;
+        i<nfds;
+        i++, pfd++, fdp++) {
+      pfd->fd= *fdp;
+      pfd->events= POLLIN|POLLERR;
+    }
+    XFree(fds);
+
+    r= poll(pfds,npfds,TIMEOUT);
+    if (r==-1) failr("poll",errno);
+
+    for (i=0, pfd=pfds;
+        i<nfds;
+        i++, pfd++) {
+      if (pfd->revents)
+       XProcessInternalConnection(disp,pfd->fd);
+    }
+
+    while (XPending(disp)) {
+      XNextEvent(disp,&ev);
+      if (ev.type == ConfigureNotify)
+       newgeometry();
+    }
+    
+    refresh();
   }
 }