12 typedef struct Win Win;
21 Reprog *exclude = nil;
36 erealloc(void *v, ulong n)
40 sysfatal("out of memory reallocating %lud", n);
51 sysfatal("out of memory allocating %lud", n);
72 readfile(char *buf, int nbuf, char *file, ...)
78 vsnprint(buf, nbuf, file, arg);
81 if((fd = open(buf, OREAD)) < 0){
85 n = read(fd, buf, nbuf-1);
98 char label[128], wctl[128], *tok[8];
99 int i, fd, n, nr, nw, state;
100 static int mywinid = -1;
104 if(readfile(wctl, sizeof(wctl), "/dev/winid") > 0)
105 mywinid = atoi(wctl);
108 if((fd = open("/dev/wsys", OREAD)) < 0)
112 /* i'd rather read one at a time but rio won't let me */
113 while((nr=dirread(fd, &pd)) > 0){
115 n = atoi(pd[i].name);
118 if(readfile(label, sizeof(label), "/dev/wsys/%d/label", n) <= 0)
120 if(exclude != nil && regexec(exclude,label,nil,0))
122 if(readfile(wctl, sizeof(wctl), "/dev/wsys/%d/wctl", n) <= 0)
124 if(tokenize(wctl, tok, nelem(tok)) != 6)
127 if(strcmp(tok[4], "current") == 0)
129 if(strcmp(tok[5], "visible") == 0)
131 if(nw < nwin && win[nw].n == n && win[nw].state == state &&
132 strcmp(win[nw].label, label)==0){
144 win = erealloc(win, mwin*sizeof(win[0]));
147 win[nw].label = estrdup(label);
148 win[nw].state = state;
150 win[nw].r = Rect(0,0,0,0);
156 free(win[--nwin].label);
166 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
167 r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
168 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
169 draw(screen, insetrect(r, -1), lightblue, nil, ZP);
175 draw(screen, win[i].r, statecol[win[i].state], nil, ZP);
176 _string(screen, addpt(win[i].r.min, Pt(2,0)), display->black, ZP,
177 font, win[i].label, nil, strlen(win[i].label),
178 win[i].r, nil, ZP, SoverD);
179 border(screen, win[i].r, 1, display->black, ZP);
190 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
191 if(rows*cols < nwin || rows*cols >= nwin*2){
192 ncols = nwin <= 0 ? 1 : (nwin+rows-1)/rows;
199 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
200 for(i=0; i<nwin; i++)
201 win[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
202 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
208 redraw(Image *screen, int all)
214 draw(screen, screen->r, lightblue, nil, ZP);
215 for(i=0; i<nwin; i++)
216 if(all || win[i].dirty)
228 if(new && getwindow(display, Refmesg) < 0)
229 fprint(2,"can't reattach to window");
240 if((m.buttons & 7) != 4)
242 for(i=0; i<nwin; i++)
243 if(ptinrect(m.xy, win[i].r))
249 while((m.buttons & 7) == 4);
250 if((m.buttons & 7) || !ptinrect(m.xy, win[i].r))
253 sprint(buf, "/dev/wsys/%d/wctl", win[i].n);
254 if((fd = open(buf, OWRITE)) < 0)
256 if(win[i].state == (CURRENT|VISIBLE))
257 write(fd, "hide\n", 5);
259 write(fd, "unhide\n", 7);
260 write(fd, "top\n", 4);
261 write(fd, "current\n", 8);
270 fprint(2, "usage: winwatch [-e exclude] [-f font]\n");
275 main(int argc, char **argv)
277 char *fontname = nil;
284 fontname = EARGF(usage());
287 exclude = regcomp(EARGF(usage()));
289 sysfatal("Bad regexp");
298 if(initdraw(0, fontname, "winwatch") < 0)
299 sysfatal("initdraw: %r");
300 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
302 statecol[0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
303 statecol[1] = lightblue;
304 statecol[2] = lightblue;
305 statecol[3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
307 for(i=0; i<nelem(statecol); i++)
308 if(statecol[i] == nil)
309 sysfatal("allocimage: %r");
313 einit(Emouse|Ekeyboard);
314 Etimer = etimer(0, 2500);
317 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
319 if(e.kbdc==0x7F || e.kbdc=='q')
323 if(click(e.mouse) == 0)
326 default: /* Etimer */