/*
* 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]
+ * ... darkgreen ... no batteries present
*
* [1] battery must be quite badly degraded
*/
#include <unistd.h>
#include <ctype.h>
+#include <sys/poll.h>
#include <sys/types.h>
#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) {
fprintf(stderr,"error: %s\n", m);
- sleep(5);
exit(-1);
}
static void badusage(void) { fail("bad usage"); }
} 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") \
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(info, present), { "no", "yes" } },
- { E(state,present), { "no", "yes" } },
- { E(state,charging_state), { "discharging charging charged" } },
+ { E(alarm, present), { "no", "yes" } },
+ { 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
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);
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");
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;
batfile= 0;
}
- r= chdir(batdirname);
- 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;
+
+ r= chdir("..");
+ if (r) return batfaile("chdir","..");
+ batdirname= 0;
return 0;
}
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;
/*---------- display ----------*/
-#define TOP 60
-#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(darkgreen) \
+ C(white) \
GC(remain) \
+ GC(white) \
GC(empty)
static Display *disp;
#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) fail("XSetWindowBackground");
lastbackground= newbg;
}
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) {
double elap, then;
- int i, leftmost_lit, leftmost_nondeg;
+ int i, leftmost_lit, leftmost_nondeg, beyond, first_beyond;
if (!charging_state_mask) {
- setbackground(pix_darkgrey);
+ setbackground(pix_darkgreen);
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++) {
+ for (i=0, first_beyond=1; 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;
+
+ beyond=
+ ((charging_state_mask & (1u<<CHGST_DISCHARGING) && then <= 0.0) ||
+ (charging_state_mask & (1u<<CHGST_CHARGING) && then>=nondegraded_norm));
+
if (then <= 0.0) then= 0.0;
else if (then >= nondegraded_norm) then= nondegraded_norm;
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);
+ if (beyond && first_beyond) {
+ XDrawLine(disp, win, gc_white.gc, 0,i, leftmost_nondeg,i);
+ first_beyond= 0;
+ } else {
+ if (leftmost_lit < leftmost_nondeg)
+ XDrawLine(disp, win, gc_empty.gc,
+ leftmost_lit,i, leftmost_nondeg,i);
+ if (leftmost_lit >= 0)
+ XDrawLine(disp, win, gc_remain.gc, 0,i, leftmost_lit,i);
+ }
}
}
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_white;
gc_r->gc= XCreateGC(disp,win, GCFunction|GCLineWidth|GCForeground, &gcv);
}
Status st;
st= XAllocNamedColor(disp,cmap,name,&xc,&xc);
- if (!st) fail("couldn't allocate colour");
+ if (!st) fail(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);
#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;
+
+ 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;
- XSelectInput(disp,win, ExposureMask|VisibilityChangeMask);
+ /*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);
}
Window dummyw;
XGetGeometry(disp,win, &dummyw,&dummy,&dummy, &width,&height, &dummy,&dummy);
- refresh();
}
static void eventloop(void) {
XEvent ev;
+ struct pollfd pfd;
+ int r;
newgeometry();
-
+ refresh();
+
for (;;) {
- XNextEvent(disp,&ev);
- newgeometry();
+ XFlush(disp);
+
+ pfd.fd= ConnectionNumber(disp);
+ pfd.events= POLLIN|POLLERR;
+
+ r= poll(&pfd,1,TIMEOUT);
+ if (r==-1 && errno!=EINTR) failr("poll",errno);
+
+ while (XPending(disp)) {
+ XNextEvent(disp,&ev);
+ if (ev.type == ConfigureNotify) {
+ XConfigureEvent *ce= (void*)&ev;
+ width= ce->width;
+ height= ce->height;
+ }
+ }
+ refresh();
}
}
-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;
}