chiark / gitweb /
@@ -2,6 +2,7 @@
[chiark-utils.git] / cprogs / xacpi-simple.c
diff --git a/cprogs/xacpi-simple.c b/cprogs/xacpi-simple.c
new file mode 100644 (file)
index 0000000..96ba105
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ */
+
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <X11/Xlib.h>
+
+static void fail(const char *m) {
+  fprintf(stderr,"error: %s\n", m);
+  sleep(5);
+  exit(-1);
+}
+static void badusage(void) { fail("bad usage"); }
+
+static Display *d;
+static Window w;
+static int width, height;
+static GC gc_green, gc_red, gc_yellow, gc_blue;
+static Colormap cmap;
+static int screen;
+
+#define TOP      60
+#define BOTTOM 3600
+
+static void refresh(void);
+
+static void geometry(void) {
+  int dummy;
+  Window dummyw;
+  
+  XGetGeometry(d,w, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
+  refresh();
+}
+
+static void show(double fill_norm, double ratepersec_norm, int ac) {
+  double elap, then;
+  int i, leftmost_lit;
+
+  for (i=0; 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;
+
+    leftmost_lit= then <= 0 ? -1 :
+      then >= 1.0 ? width :
+      width * then;
+
+fprintf(stderr," width=%d height=%d i=%d elap=%f then=%f leftmost_lit=%d\n",
+       (unsigned long)w,
+       width,height,i,then,leftmost_lit);
+    if (leftmost_lit >= 0)
+      XDrawLine(d, w, ac ? gc_blue : gc_green, 0,i, leftmost_lit,i);
+    if (leftmost_lit < width)
+      XDrawLine(d, w, then >= 0 ? gc_red : gc_yellow,
+               leftmost_lit+1,i, width,i);
+  }
+}
+
+static void refresh(void) {
+  show(0.3, 0.5/3600, 1);
+}
+
+static void colour(GC *gc_r, const char *name) {
+  XColor xc;
+  Status st;
+  XGCValues gcv;
+  
+  st= XAllocNamedColor(d,cmap,name,&xc,&xc);
+  if (!st) fail("couldn't allocate colour");
+  
+  gcv.function= GXcopy;
+  gcv.line_width= 1;
+  gcv.foreground= xc.pixel;
+  *gc_r= XCreateGC(d,w, GCFunction|GCForeground|GCLineWidth, &gcv);
+}
+
+int main(int argc, const char *const *argv) {
+  const char *windowid_string;
+  char *ep;
+  XEvent ev;
+  
+  d= XOpenDisplay(0); if (!d) fail("could not open display");
+
+  if (!argv[0] || argv[1])
+    badusage();
+
+  /*  if (!(windowid_string= getenv("WINDOWID")))
+    badusage();  w= strtoul(windowid_string,&ep,0);
+  if (*ep) badusage();
+
+*/
+
+  screen= DefaultScreen(d);
+
+  w= XCreateSimpleWindow(d,DefaultRootWindow(d),0,0,50,50,0,0,0);
+
+  cmap= DefaultColormap(d,screen);
+  
+  colour(&gc_green,  "green");
+  colour(&gc_red,    "darkred");
+  colour(&gc_yellow, "yellow");
+  colour(&gc_blue,   "blue");
+
+  XSelectInput(d,w, ExposureMask|VisibilityChangeMask);
+  XMapWindow(d,w);
+  
+  geometry();
+  
+  for (;;) {
+    XNextEvent(d,&ev);
+    geometry();
+  }
+}