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);
188 int i, nrows, ncols, z;
192 nrows = (Dy(screen->r)-2*MARGIN+PAD)/(font->height+PAD);
199 ncols = nwin <= 0 ? 1 : (nwin+rows-1)/rows;
205 r = Rect(0,0,(Dx(screen->r)-2*MARGIN+PAD)/cols-PAD, font->height);
206 for(i=0; i<nwin; i++)
207 win[i].r = rectaddpt(rectaddpt(r, Pt(MARGIN+(PAD+Dx(r))*(i/rows),
208 MARGIN+(PAD+Dy(r))*(i%rows))), screen->r.min);
214 redraw(Image *screen, int all)
220 draw(screen, screen->r, lightblue, nil, ZP);
221 for(i=0; i<nwin; i++)
222 if(all || win[i].dirty)
234 if(new && getwindow(display, Refmesg) < 0)
235 fprint(2,"can't reattach to window");
241 label(Win w, Mouse m)
243 char buf[512], fname[128];
246 snprint(buf, sizeof(buf), "%s", w.label);
247 n = eenter(nil, buf, sizeof(buf), &m);
250 sprint(fname, "/dev/wsys/%d/label", w.n);
251 if((fd = open(fname, OWRITE)) < 0)
266 sprint(buf, "/dev/wsys/%d/wctl", w.n);
267 if((fd = open(buf, OWRITE)) < 0)
269 if(w.state == (CURRENT|VISIBLE))
270 write(fd, "hide\n", 5);
272 write(fd, "unhide\n", 7);
273 write(fd, "top\n", 4);
274 write(fd, "current\n", 8);
288 for(i=0; i<nwin; i++)
289 if(ptinrect(m.xy, win[i].r))
295 while((m.buttons & 7) == b);
296 if((m.buttons & 7) || !ptinrect(m.xy, win[i].r))
301 return label(win[i], m);
303 return unhide(win[i]);
312 fprint(2, "usage: winwatch [-e exclude] [-f font]\n");
317 main(int argc, char **argv)
319 char *fontname = nil;
326 fontname = EARGF(usage());
329 exclude = regcomp(EARGF(usage()));
331 sysfatal("Bad regexp");
340 if(initdraw(0, fontname, "winwatch") < 0)
341 sysfatal("initdraw: %r");
342 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
344 statecol[0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
345 statecol[1] = lightblue;
346 statecol[2] = lightblue;
347 statecol[3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, DPalegreygreen);
349 for(i=0; i<nelem(statecol); i++)
350 if(statecol[i] == nil)
351 sysfatal("allocimage: %r");
355 einit(Emouse|Ekeyboard);
356 Etimer = etimer(0, 2500);
359 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
361 if(e.kbdc==Kdel || e.kbdc=='q')
365 if(click(e.mouse) == 0)
368 default: /* Etimer */