chiark / gitweb /
bb5cb651c16ff2861eac2990ae62b2c367ac29eb
[chiark-utils.git] / cprogs / xacpi-simple.c
1 /*
2  */
3
4 #include <stdio.h>
5 #include <math.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
9 #include <X11/Xlib.h>
10
11 static void fail(const char *m) {
12   fprintf(stderr,"error: %s\n", m);
13   sleep(5);
14   exit(-1);
15 }
16 static void badusage(void) { fail("bad usage"); }
17
18 static Display *d;
19 static Window w;
20 static int width, height;
21 static GC gc_green, gc_red, gc_yellow, gc_blue;
22 static Colormap cmap;
23 static int screen;
24
25 #define TOP      60
26 #define BOTTOM 3600
27
28 static void refresh(void);
29
30 static void geometry(void) {
31   int dummy;
32   Window dummyw;
33   
34   XGetGeometry(d,w, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
35   refresh();
36 }
37
38 static void show(double fill_norm, double ratepersec_norm, int ac) {
39   double elap, then;
40   int i, leftmost_lit;
41
42   for (i=0; i<height; i++) {
43     elap= !i ? 0 :
44       height==2 ? BOTTOM :
45       TOP * exp( (double)i / (height-2) * log( (double)BOTTOM/TOP ) );
46     
47     then= fill_norm + ratepersec_norm * elap;
48
49     leftmost_lit= then <= 0 ? -1 :
50       then >= 1.0 ? width :
51       width * then;
52
53     if (leftmost_lit >= 0)
54       XDrawLine(d, w, ac ? gc_blue : gc_green, 0,i, leftmost_lit,i);
55     if (leftmost_lit < width)
56       XDrawLine(d, w, then >= 0 ? gc_red : gc_yellow,
57                 leftmost_lit+1,i, width,i);
58   }
59 }
60
61 static void refresh(void) {
62   show(0.3, 0.5/3600, 1);
63 }
64
65 static void colour(GC *gc_r, const char *name) {
66   XColor xc;
67   Status st;
68   XGCValues gcv;
69   
70   st= XAllocNamedColor(d,cmap,name,&xc,&xc);
71   if (!st) fail("couldn't allocate colour");
72   
73   gcv.function= GXcopy;
74   gcv.line_width= 1;
75   gcv.foreground= xc.pixel;
76   *gc_r= XCreateGC(d,w, GCFunction|GCForeground|GCLineWidth, &gcv);
77 }
78
79 int main(int argc, const char *const *argv) {
80   XEvent ev;
81   
82   d= XOpenDisplay(0); if (!d) fail("could not open display");
83
84   if (!argv[0] || argv[1])
85     badusage();
86
87   screen= DefaultScreen(d);
88
89   w= XCreateSimpleWindow(d,DefaultRootWindow(d),0,0,100,20,0,0,0);
90
91   cmap= DefaultColormap(d,screen);
92   
93   colour(&gc_green,  "green");
94   colour(&gc_red,    "red");
95   colour(&gc_yellow, "yellow");
96   colour(&gc_blue,   "blue");
97
98   XSelectInput(d,w, ExposureMask|VisibilityChangeMask);
99   XMapWindow(d,w);
100   
101   geometry();
102   
103   for (;;) {
104     XNextEvent(d,&ev);
105     geometry();
106   }
107 }