#include #include #include #include void panic(char *s) { fprint(2, "error: %s: %r\n", s); exits("error"); } int setimage(char *name, Image *cimg) { int n; Image *img; name = smprint("th_%s", name); img = namedimage(display, name); if(img == nil){ if(nameimage(cimg, name, 1) == 0) panic("couldn't name image\n"); n = 1; }else{ if(eqrect(img->r, cimg->r)) draw(img, img->r, cimg, nil, ZP); else print("%s unchanged\n", name); freeimage(cimg); n = 0; } free(name); return n; } int setcolor(char *name, ulong col) { Image *cimg; cimg = allocimage(display, Rect(0,0,1,1), RGB24, 1, col); return setimage(name, cimg); } int setfile(char *name, char *path) { int fd; Image *cimg; fd = open(path, OREAD); if(fd < 0) return 0; cimg = readimage(display, fd, 0); close(fd); if(cimg == nil) return 0; return setimage(name, cimg); } void main(int argc, char *[]) { int nnew; char *s; Biobuf b; // NB: no full initdraw here because we don't actually // want to draw anything display = initdisplay("/dev", nil, nil); if(display == nil) panic("initdraw"); nnew = 0; Binit(&b, 0, OREAD); while(s = Brdstr(&b, '\n', 1)){ char *ss[2], *p; p = strchr(s, '#'); if(p) *p = '\0'; int n = tokenize(s, ss, 2); if(n == 2){ if(ss[1][0] == '/') nnew += setfile(ss[0], ss[1]); else{ ulong col = strtoul(ss[1], nil, 16); col = (col<<8) | 0xFF; nnew += setcolor(ss[0], col); } } free(s); } Bterm(&b); flushimage(display, 1); int fd = open("/dev/wctl", OWRITE); if(fd >= 0){ write(fd, "refresh", 7); close(fd); } if(nnew > 0 && argc > 1){ switch(rfork(RFPROC)){ case -1: panic("can't fork"); case 0: for(;;) sleep(10); default: break; } } exits(nil); }