chiark / gitweb /
draws things
authorianmdlvl <ianmdlvl>
Sat, 11 Dec 2004 23:23:38 +0000 (23:23 +0000)
committerianmdlvl <ianmdlvl>
Sat, 11 Dec 2004 23:23:38 +0000 (23:23 +0000)
cprogs/xacpi-simple.c

index 687127a..1b80df7 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
  */
@@ -33,7 +33,6 @@
 
 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 +55,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 +73,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 +125,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 +143,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 +156,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 +194,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,14 +293,14 @@ 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)                                       \
   GC(remain)                                   \
   GC(empty)
 
@@ -323,12 +326,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,7 +349,7 @@ 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) {
@@ -349,21 +357,21 @@ static void show(void) {
   int i, leftmost_lit, leftmost_nondeg;
 
   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++) {
     elap= !i ? 0 :
@@ -377,11 +385,11 @@ static void show(void) {
     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);
+               leftmost_lit,i, leftmost_nondeg,i);
+    if (leftmost_lit >= 0)
+      XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
   }
 }
 
@@ -391,7 +399,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_dimgrey;
   gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
 }
 
@@ -400,7 +408,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 +428,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);