7 typedef struct Win Win;
17 Reprog *exclude = nil;
32 erealloc(void *v, ulong n)
36 sysfatal("out of memory reallocating %lud", n);
47 sysfatal("out of memory allocating %lud", n);
72 int i, fd, lfd, n, nr, nw, m;
75 if((fd = open("/dev/wsys", OREAD)) < 0)
79 /* i'd rather read one at a time but rio won't let me */
80 while((nr=dirread(fd, &pd)) > 0){
83 sprint(label, "/dev/wsys/%d/label", n);
84 if((lfd = open(label, OREAD)) < 0)
86 m = read(lfd, label, sizeof(label)-1);
91 if(exclude != nil && regexec(exclude,label,nil,0))
94 if(nw < nwin && win[nw].n == n && strcmp(win[nw].label, label)==0){
106 win = erealloc(win, mwin*sizeof(win[0]));
109 win[nw].label = estrdup(label);
111 win[nw].r = Rect(0,0,0,0);
117 free(win[--nwin].label);
127 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
128 r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
129 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
130 draw(screen, insetrect(r, -1), lightblue, nil, ZP);
136 draw(screen, win[i].r, lightblue, nil, ZP);
137 _string(screen, addpt(win[i].r.min, Pt(2,0)), display->black, ZP,
138 font, win[i].label, nil, strlen(win[i].label),
139 win[i].r, nil, ZP, SoverD);
140 border(screen, win[i].r, 1, display->black, ZP);
151 rows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
152 if(rows*cols < nwin || rows*cols >= nwin*2){
153 ncols = nwin <= 0 ? 1 : (nwin+rows-1)/rows;
160 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
161 for(i=0; i<nwin; i++)
162 win[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
163 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
169 redraw(Image *screen, int all)
175 draw(screen, screen->r, lightblue, nil, ZP);
176 for(i=0; i<nwin; i++)
177 if(all || win[i].dirty)
189 if(new && getwindow(display, Refmesg) < 0)
190 fprint(2,"can't reattach to window");
201 if(m.buttons == 0 || (m.buttons & ~4))
204 for(i=0; i<nwin; i++)
205 if(ptinrect(m.xy, win[i].r))
212 while(m.buttons == 4);
221 for(j=0; j<nwin; j++)
222 if(ptinrect(m.xy, win[j].r))
227 sprint(buf, "/dev/wsys/%d/wctl", win[i].n);
228 if((fd = open(buf, OWRITE)) < 0)
230 write(fd, "unhide\n", 7);
231 write(fd, "top\n", 4);
232 write(fd, "current\n", 8);
239 fprint(2, "usage: winwatch [-e exclude] [-f font]\n");
244 main(int argc, char **argv)
250 fontname = "/lib/font/bit/lucidasans/unicode.8.font";
253 fontname = EARGF(usage());
256 exclude = regcomp(EARGF(usage()));
258 sysfatal("Bad regexp");
267 initdraw(0, 0, "winwatch");
268 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
270 sysfatal("allocimagemix: %r");
271 if((font = openfont(display, fontname)) == nil)
272 sysfatal("font '%s' not found", fontname);
276 einit(Emouse|Ekeyboard);
277 Etimer = etimer(0, 2500);
280 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
282 if(e.kbdc==0x7F || e.kbdc=='q')
289 default: /* Etimer */