chiark / gitweb /
tries to set geometry but only works for size it seems
[chiark-utils.git] / cprogs / xacpi-simple.c
index 50b7965bee5dfdcc016fde3dfdb63187b21205ef..157ed59356d46b6b08bbba65c60312a6cfbc9d24 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 */
 
+static const char program_name[]= "xacpi-simple";
+
 /*---------- general utility stuff and declarations ----------*/
 
 static void fail(const char *m) {
@@ -71,14 +74,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" } },
@@ -200,21 +204,28 @@ static int readbattery(void) { /* 0=>ok, -1=>couldn't */
     batfile= 0;
   }
 
+  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;
+  }
+
   r= chdir("..");
   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 (missing) return -1;
-
   return 0;
 }   
 
@@ -252,10 +263,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;
 
@@ -303,7 +312,7 @@ static void initacquire(void) {
   C(green)                                     \
   C(cyan)                                      \
   C(grey)                                      \
-  C(darkmagenta)                               \
+  C(darkgreen)                                 \
   C(white)                                     \
   GC(remain)                                   \
   GC(white)                                    \
@@ -341,7 +350,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 +371,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;
   }
@@ -428,13 +437,40 @@ static void colour(unsigned long *pix_r, const char *name) {
   *pix_r= xc.pixel;
 }
 
-static void initgraphics(void) {
+static void initgraphics(int argc, char **argv) {
   int r;
+  XTextProperty tp;
+  const char *geom_string;
+  XSizeHints *normal_hints;
+  XWMHints *wm_hints;
+  XClassHint *class_hint;
+  int pos_x, pos_y, gravity;
+  char *program_name_silly;
   
-  disp= XOpenDisplay(0); if (!disp) fail("could not open display");
+  program_name_silly= (char*)program_name;
+
+  disp= XOpenDisplay(0);
+  if (!disp) fail("could not open display");
 
   screen= DefaultScreen(disp);
-  win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),0,0,100,20,0,0,0);
+
+  normal_hints= XAllocSizeHints();
+  wm_hints= XAllocWMHints();
+  class_hint= XAllocClassHint();
+
+  if (!normal_hints || !wm_hints || !class_hint)
+    fail("could not alloc hint(s)");
+
+  geom_string= argc>=2 ? argv[1] : 0;
+
+  r= XWMGeometry(disp,screen, geom_string,"128x32", 0,
+                normal_hints,
+                &pos_x, &pos_y,
+                &width, &height,
+                &gravity);
+
+  win= XCreateSimpleWindow(disp,DefaultRootWindow(disp),
+                          pos_x,pos_y,width,height,0,0,0);
   cmap= DefaultColormap(disp,screen);
   
 #define C(c) colour(&pix_##c, #c);
@@ -447,6 +483,21 @@ static void initgraphics(void) {
   if (!r) fail("init set background");
   lastbackground= pix_dimgrey;
 
+  normal_hints->flags= PWinGravity;
+  normal_hints->win_gravity= gravity;
+
+  wm_hints->flags= InputHint;
+  wm_hints->input= False;
+
+  class_hint->res_name= program_name_silly;
+  class_hint->res_class= program_name_silly;
+
+  /*r= XStringListToTextProperty(&program_name_silly, 1, &tp);
+    if (!r) fail("XStringListtoTextProperty");*/
+
+  XmbSetWMProperties(disp,win, program_name,program_name,
+                    argv,argc, normal_hints, wm_hints, class_hint);
+
   XSelectInput(disp,win, ExposureMask|StructureNotifyMask);
   XMapWindow(disp,win);
 }
@@ -472,6 +523,8 @@ static void eventloop(void) {
   refresh();
 
   for (;;) {
+    XFlush(disp);
+
     pfd.fd= ConnectionNumber(disp);
     pfd.events= POLLIN|POLLERR;
 
@@ -486,18 +539,16 @@ static void eventloop(void) {
        height= ce->height;
       }
     }
-    
     refresh();
-    XFlush(disp);
   }
 }
 
-int main(int argc, const char *const *argv) {
-  if (!argv[0] || argv[1])
+int main(int argc, char **argv) {
+  if (argc<1 || argc>2)
     badusage();
 
   initacquire();
-  initgraphics();
+  initgraphics(argc,argv);
   eventloop();
   return 0;
 }