13 typedef struct Win Win;
22 Reprog *exclude = nil;
37 erealloc(void *v, ulong n)
41 sysfatal("out of memory reallocating %lud", n);
52 sysfatal("out of memory allocating %lud", n);
73 readfile(char *buf, int nbuf, char *file, ...)
79 vsnprint(buf, nbuf, file, arg);
82 if((fd = open(buf, OREAD)) < 0){
86 n = read(fd, buf, nbuf-1);
99 char label[128], wctl[128], *tok[8];
100 int i, fd, n, nr, nw, state;
101 static int mywinid = -1;
105 if(readfile(wctl, sizeof(wctl), "/dev/winid") > 0)
106 mywinid = atoi(wctl);
109 if((fd = open("/dev/wsys", OREAD)) < 0)
113 /* i'd rather read one at a time but rio won't let me */
114 while((nr=dirread(fd, &pd)) > 0){
116 n = atoi(pd[i].name);
119 if(readfile(label, sizeof(label), "/dev/wsys/%d/label", n) <= 0)
121 if(exclude != nil && regexec(exclude,label,nil,0))
123 if(readfile(wctl, sizeof(wctl), "/dev/wsys/%d/wctl", n) <= 0)
125 if(tokenize(wctl, tok, nelem(tok)) != 6)
128 if(strcmp(tok[4], "current") == 0)
130 if(strcmp(tok[5], "visible") == 0)
132 if(nw < nwin && win[nw].n == n && win[nw].state == state &&
133 strcmp(win[nw].label, label)==0){
145 win = erealloc(win, mwin*sizeof(win[0]));
148 win[nw].label = estrdup(label);
149 win[nw].state = state;
151 win[nw].r = Rect(0,0,0,0);
157 free(win[--nwin].label);
167 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
168 r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
169 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
170 draw(screen, insetrect(r, -1), lightblue, nil, ZP);
176 draw(screen, win[i].r, statecol[win[i].state], nil, ZP);
177 _string(screen, addpt(win[i].r.min, Pt(2,0)), display->black, ZP,
178 font, win[i].label, nil, strlen(win[i].label),
179 win[i].r, nil, ZP, SoverD);
180 border(screen, win[i].r, 1, display->black, ZP);
191 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
194 if(rows*cols < nwin || rows*cols >= nwin*2){
195 ncols = nwin <= 0 ? 1 : (nwin+rows-1)/rows;
202 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
203 for(i=0; i<nwin; i++)
204 win[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
205 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
211 redraw(Image *screen, int all)
217 draw(screen, screen->r, lightblue, nil, ZP);
218 for(i=0; i<nwin; i++)
219 if(all || win[i].dirty)
231 if(new && getwindow(display, Refmesg) < 0)
232 fprint(2,"can't reattach to window");
243 if((m.buttons & 7) != 4)
245 for(i=0; i<nwin; i++)
246 if(ptinrect(m.xy, win[i].r))
252 while((m.buttons & 7) == 4);
253 if((m.buttons & 7) || !ptinrect(m.xy, win[i].r))
256 sprint(buf, "/dev/wsys/%d/wctl", win[i].n);
257 if((fd = open(buf, OWRITE)) < 0)
259 if(win[i].state == (CURRENT|VISIBLE))
260 write(fd, "hide\n", 5);
262 write(fd, "unhide\n", 7);
263 write(fd, "top\n", 4);
264 write(fd, "current\n", 8);
273 fprint(2, "usage: winwatch [-e exclude] [-f font]\n");
278 main(int argc, char **argv)
280 char *fontname = nil;
287 fontname = EARGF(usage());
290 exclude = regcomp(EARGF(usage()));
292 sysfatal("Bad regexp");
301 if(initdraw(0, fontname, "winwatch") < 0)
302 sysfatal("initdraw: %r");
303 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
305 statecol[0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
306 statecol[1] = lightblue;
307 statecol[2] = lightblue;
308 statecol[3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
310 for(i=0; i<nelem(statecol); i++)
311 if(statecol[i] == nil)
312 sysfatal("allocimage: %r");
316 einit(Emouse|Ekeyboard);
317 Etimer = etimer(0, 2500);
320 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
322 if(e.kbdc==Kdel || e.kbdc=='q')
326 if(click(e.mouse) == 0)
329 default: /* Etimer */