chiark / gitweb /
set our name, actually
[chiark-utils.git] / cprogs / xacpi-simple.c
index 37a2e31d2f4368eaf620ee6212546162903312b0..671ad6ab4ce2a869e709a02390ec52fe2c6875ba 100644 (file)
@@ -10,7 +10,7 @@
  *     green   |  red          |  dimgrey      charging - low
  *     cyan    |  red          |  dimgrey      charged - low [1]
  *     grey    |  red          |  dimgrey      charging&discharching, low [1]
- *       ...  darkmagenta  ...                 no batteries present
+ *       ...  darkgreen  ...                   no batteries present
  *
  * [1] battery must be quite badly degraded
  */
 #include <dirent.h>
 
 #include <X11/Xlib.h>
+#include <X11/Xutil.h>
+
+#define TOP      60
+#define BOTTOM 3600
 
 #define TIMEOUT 5000 /* milliseconds */
 
@@ -68,14 +72,15 @@ typedef struct batinfo_field {
   QUANTITY_FIELDS
 #undef QF
 
-static unsigned long thisbat_info_present, thisbat_state_present;
-static unsigned long thisbat_state_charging_state;
+static unsigned long thisbat_alarm_present, thisbat_info_present;
+static unsigned long thisbat_state_present, thisbat_state_charging_state;
 
 #define VAL_NOTFOUND (~0UL)
 
 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(alarm, present),           { "no", "yes" }                          },
   { E(info, present),            { "no", "yes" }                          },
   { E(state,present),            { "no", "yes" }                          },
   { E(state,charging_state),     { "discharging", "charging", "charged" } },
@@ -201,16 +206,23 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */
   if (r) return batfaile("chdir","..");
   batdirname= 0;
 
-  for (field=fields, missing=0;
-       field->file;
-       field++) {
-    if (*field->valuep == VAL_NOTFOUND) {
-      fprintf(stderr,"battery/%s/%s: %s: not found\n",
-             batdirname, field->file,field->label);
-      missing++;
+  if (!(thisbat_alarm_present==0 ||
+       thisbat_info_present==0 ||
+       thisbat_state_present==0)) {
+    if (thisbat_alarm_present == VAL_NOTFOUND)
+      thisbat_alarm_present= 1;
+    
+    for (field=fields, missing=0;
+        field->file;
+        field++) {
+      if (*field->valuep == VAL_NOTFOUND) {
+       fprintf(stderr,"battery/%s/%s: %s: not found\n",
+               batdirname, field->file,field->label);
+       missing++;
+      }
     }
+    if (missing) return -1;
   }
-  if (missing) return -1;
 
   return 0;
 }   
@@ -249,10 +261,8 @@ static void acquiredata(void) {
 
     if (r) continue;
 
-    if (!thisbat_info_present || !thisbat_state_present) {
-      printf("battery/%s not present\n",de->d_name);
+    if (!thisbat_info_present || !thisbat_state_present)
       continue;
-    }
 
     charging_state_mask |= 1u << thisbat_state_charging_state;
 
@@ -292,9 +302,6 @@ static void initacquire(void) {
 
 /*---------- display ----------*/
 
-#define TOP      60
-#define BOTTOM 3600
-
 #define COLOURS                                        \
   C(dimgrey)                                   \
   C(blue)                                      \
@@ -303,7 +310,7 @@ static void initacquire(void) {
   C(green)                                     \
   C(cyan)                                      \
   C(grey)                                      \
-  C(darkmagenta)                               \
+  C(darkgreen)                                 \
   C(white)                                     \
   GC(remain)                                   \
   GC(white)                                    \
@@ -341,7 +348,7 @@ static void setbackground(unsigned long newbg) {
   
   if (newbg == lastbackground) return;
   r= XSetWindowBackground(disp,win,newbg);
-  if (r) failr("XSetWindowBackground",r);
+  if (!r) fail("XSetWindowBackground");
   lastbackground= newbg;
 }
 
@@ -362,7 +369,7 @@ static void show(void) {
   int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
 
   if (!charging_state_mask) {
-    setbackground(pix_darkmagenta);
+    setbackground(pix_darkgreen);
     XClearWindow(disp,win);
     return;
   }
@@ -430,6 +437,8 @@ static void colour(unsigned long *pix_r, const char *name) {
 
 static void initgraphics(void) {
   int r;
+  XTextProperty tp;
+  const char *program_name= "xacpi-simple";
   
   disp= XOpenDisplay(0); if (!disp) fail("could not open display");
 
@@ -447,7 +456,11 @@ static void initgraphics(void) {
   if (!r) fail("init set background");
   lastbackground= pix_dimgrey;
 
-  XSelectInput(disp,win, ExposureMask|VisibilityChangeMask);
+  r= XStringListToTextProperty((char**)&program_name, 1, &tp);
+  if (!r) fail("XStringListtoTextProperty");
+  XSetWMName(disp,win,&tp);
+  
+  XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
   XMapWindow(disp,win);
 }
  
@@ -465,46 +478,29 @@ static void newgeometry(void) {
 
 static void eventloop(void) {
   XEvent ev;
-  int *fds, nfds, npfds, *fdp, i, r;
-  struct pollfd *pfds, *pfd;
+  struct pollfd pfd;
+  int r;
   
   newgeometry();
+  refresh();
 
-  npfds= 0;
-  pfds= 0;
   for (;;) {
-    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);
+    XFlush(disp);
 
-    r= poll(pfds,npfds,TIMEOUT);
-    if (r==-1) failr("poll",errno);
+    pfd.fd= ConnectionNumber(disp);
+    pfd.events= POLLIN|POLLERR;
 
-    for (i=0, pfd=pfds;
-        i<nfds;
-        i++, pfd++) {
-      if (pfd->revents)
-       XProcessInternalConnection(disp,pfd->fd);
-    }
+    r= poll(&pfd,1,TIMEOUT);
+    if (r==-1 && errno!=EINTR) failr("poll",errno);
 
     while (XPending(disp)) {
       XNextEvent(disp,&ev);
-      if (ev.type == ConfigureNotify)
-       newgeometry();
+      if (ev.type == ConfigureNotify) {
+       XConfigureEvent *ce= (void*)&ev;
+       width= ce->width;
+       height= ce->height;
+      }
     }
-    
     refresh();
   }
 }