14 typedef struct Win Win;
23 Reprog *exclude = nil;
38 erealloc(void *v, ulong n)
42 sysfatal("out of memory reallocating %lud", n);
53 sysfatal("out of memory allocating %lud", n);
74 readfile(char *buf, int nbuf, char *file, ...)
80 vsnprint(buf, nbuf, file, arg);
83 if((fd = open(buf, OREAD)) < 0){
87 n = read(fd, buf, nbuf-1);
100 char label[128], wctl[128], *tok[8];
101 int i, fd, n, nr, nw, state;
102 static int mywinid = -1;
106 if(readfile(wctl, sizeof(wctl), "/dev/winid") > 0)
107 mywinid = atoi(wctl);
110 if((fd = open("/dev/wsys", OREAD)) < 0)
114 /* i'd rather read one at a time but rio won't let me */
115 while((nr=dirread(fd, &pd)) > 0){
117 n = atoi(pd[i].name);
120 if(readfile(label, sizeof(label), "/dev/wsys/%d/label", n) < 0)
122 if(exclude != nil && regexec(exclude,label,nil,0))
124 if(readfile(wctl, sizeof(wctl), "/dev/wsys/%d/wctl", n) <= 0)
126 if(tokenize(wctl, tok, nelem(tok)) != 6)
129 if(strcmp(tok[4], "current") == 0)
131 if(strcmp(tok[5], "visible") == 0)
133 if(nw < nwin && win[nw].n == n && win[nw].state == state &&
134 strcmp(win[nw].label, label)==0){
146 win = erealloc(win, mwin*sizeof(win[0]));
149 win[nw].label = estrdup(label);
150 win[nw].state = state;
152 win[nw].r = Rect(0,0,0,0);
158 free(win[--nwin].label);
168 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
169 r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
170 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
171 draw(screen, insetrect(r, -1), lightblue, nil, ZP);
177 draw(screen, win[i].r, statecol[win[i].state], nil, ZP);
178 _string(screen, addpt(win[i].r.min, Pt(2,0)), display->black, ZP,
179 font, win[i].label, nil, strlen(win[i].label),
180 win[i].r, nil, ZP, SoverD);
181 border(screen, win[i].r, 1, display->black, ZP);
192 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
195 if(rows*cols < nwin || rows*cols >= nwin*2){
196 ncols = nwin <= 0 ? 1 : (nwin+rows-1)/rows;
203 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
204 for(i=0; i<nwin; i++)
205 win[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
206 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
212 redraw(Image *screen, int all)
218 draw(screen, screen->r, lightblue, nil, ZP);
219 for(i=0; i<nwin; i++)
220 if(all || win[i].dirty)
232 if(new && getwindow(display, Refmesg) < 0)
233 fprint(2,"can't reattach to window");
239 label(Win w, Mouse m)
241 char buf[512], fname[128];
244 snprint(buf, sizeof(buf), "%s", w.label);
245 n = eenter(nil, buf, sizeof(buf), &m);
248 sprint(fname, "/dev/wsys/%d/label", w.n);
249 if((fd = open(fname, OWRITE)) < 0)
264 sprint(buf, "/dev/wsys/%d/wctl", w.n);
265 if((fd = open(buf, OWRITE)) < 0)
267 if(w.state == (CURRENT|VISIBLE))
268 write(fd, "hide\n", 5);
270 write(fd, "unhide\n", 7);
271 write(fd, "top\n", 4);
272 write(fd, "current\n", 8);
286 for(i=0; i<nwin; i++)
287 if(ptinrect(m.xy, win[i].r))
293 while((m.buttons & 7) == b);
294 if((m.buttons & 7) || !ptinrect(m.xy, win[i].r))
299 return label(win[i], m);
301 return unhide(win[i]);
310 fprint(2, "usage: winwatch [-e exclude] [-f font]\n");
315 main(int argc, char **argv)
317 char *fontname = nil;
324 fontname = EARGF(usage());
327 exclude = regcomp(EARGF(usage()));
329 sysfatal("Bad regexp");
338 if(initdraw(0, fontname, "winwatch") < 0)
339 sysfatal("initdraw: %r");
340 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
342 statecol[0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
343 statecol[1] = lightblue;
344 statecol[2] = lightblue;
345 statecol[3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
347 for(i=0; i<nelem(statecol); i++)
348 if(statecol[i] == nil)
349 sysfatal("allocimage: %r");
353 einit(Emouse|Ekeyboard);
354 Etimer = etimer(0, 2500);
357 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
359 if(e.kbdc==Kdel || e.kbdc=='q')
363 if(click(e.mouse) == 0)
366 default: /* Etimer */