2 * slightly modified from
3 * https://github.com/fhs/misc/blob/master/cmd/winwatch/winwatch.c
4 * so as to deal with memory leaks and certain X errors
13 #include "../devdraw/x11-inc.h"
17 typedef struct Win Win;
27 Atom net_active_window;
28 Reprog *exclude = nil;
47 winwatchxerrorhandler(XDisplay *disp, XErrorEvent *xe)
51 XGetErrorText(disp, xe->error_code, buf, 100);
52 fprint(2, "winwatch: X error %s, request code %d\n",
53 buf, xe->request_code);
58 erealloc(void *v, ulong n)
62 sysfatal("out of memory reallocating");
71 sysfatal("out of memory allocating");
76 getproperty(XWindow w, Atom a)
86 s = XGetWindowProperty(dpy, w, a, 0, 100L, 0,
87 AnyPropertyType, &type, &fmt, &n, &dummy, &p);
102 XWindow dw1, dw2, *xwin, rwin;
107 p = getproperty(w, XA_WM_NAME);
113 net_wm_name = XInternAtom(dpy, "_NET_WM_NAME", FALSE);
114 p = getproperty(w, net_wm_name);
122 s = XQueryTree(dpy, w, &dw1, &dw2, &xwin, &nxwin);
125 for (i = 0; i < nxwin; i++){
126 w = findname(xwin[i]);
139 wcmp(const void *w1, const void *w2)
141 return *(XWindow *) w1 - *(XWindow *) w2;
144 /* unicode-aware case-insensitive strcmp, taken from golang’s gc/subr.c */
147 _cistrcmp(char *p, char *q)
156 p += chartorune(&rp, p);
157 q += chartorune(&rq, q);
158 rp = tolowerrune(rp);
159 rq = tolowerrune(rq);
169 winlabelcmp(const void *w1, const void *w2)
171 const Win *p1 = (Win *) w1;
172 const Win *p2 = (Win *) w2;
173 return _cistrcmp(p1->label, p2->label);
179 XWindow dw1, dw2, *xwin;
181 XWindowAttributes attr;
189 s = XQueryTree(dpy, root, &dw1, &dw2, &xwin, &nxwin);
196 qsort(xwin, nxwin, sizeof(xwin[0]), wcmp);
199 for(i=0; i<nxwin; i++){
200 memset(&attr, 0, sizeof attr);
201 xwin[i] = findname(xwin[i]);
205 s = XGetWindowAttributes(dpy, xwin[i], &attr);
209 if (attr.width <= 0 ||
210 attr.override_redirect ||
211 attr.map_state != IsViewable)
214 s = XGetClassHint(dpy, xwin[i], &class);
219 if (exclude!=nil && regexec(exclude, class.res_name, nil, 0)) {
220 free(class.res_name);
221 free(class.res_class);
225 net_wm_name = XInternAtom(dpy, "_NET_WM_NAME", FALSE);
226 wmname = getproperty(xwin[i], net_wm_name);
229 wmname = getproperty(xwin[i], XA_WM_NAME);
231 free(class.res_name);
232 free(class.res_class);
237 label = class.res_name;
241 if(nw<nwin && win[nw].n==xwin[i] && strcmp(win[nw].label, label)==0) {
244 free(class.res_name);
245 free(class.res_class);
256 win = erealloc(win, mwin * sizeof(win[0]));
259 win[nw].label = estrdup(label);
261 win[nw].r = Rect(0, 0, 0, 0);
263 free(class.res_name);
264 free(class.res_class);
271 free(win[--nwin].label);
275 qsort(win, nwin, sizeof(struct Win), winlabelcmp);
283 r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, font->height);
286 Pt(MARGIN + (PAD + Dx(r)) * (i / rows),
287 MARGIN + (PAD + Dy(r)) * (i % rows))),
289 draw(screen, insetrect(r, -1), lightblue, nil, ZP);
295 draw(screen, win[i].r, lightblue, nil, ZP);
296 _string(screen, addpt(win[i].r.min, Pt(2, 0)), display->black, ZP,
297 font, win[i].label, nil, strlen(win[i].label),
298 win[i].r, nil, ZP, SoverD);
299 border(screen, win[i].r, 1, display->black, ZP);
310 rows = (Dy(screen->r) - 2 * MARGIN + PAD) / (font->height + PAD);
311 if(rows*cols<nwin || rows*cols>=nwin*2){
314 ncols = (nwin + rows - 1) / rows;
321 r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, font->height);
322 for(i=0; i<nwin; i++)
326 Pt(MARGIN + (PAD + Dx(r)) * (i / rows),
327 MARGIN + (PAD + Dy(r)) * (i % rows))),
334 redraw(Image *screen, int all)
340 draw(screen, screen->r, lightblue, nil, ZP);
341 for(i=0; i<nwin; i++)
342 if(all || win[i].dirty)
353 if(new && getwindow(display, Refmesg)<0)
354 fprint(2, "can't reattach to window");
361 selectwin(XWindow win)
366 memset(&ev, 0, sizeof ev);
367 ev.xclient.type = ClientMessage;
368 ev.xclient.serial = 0;
369 ev.xclient.send_event = True;
370 ev.xclient.message_type = net_active_window;
371 ev.xclient.window = win;
372 ev.xclient.format = 32;
373 mask = SubstructureRedirectMask | SubstructureNotifyMask;
375 XSendEvent(dpy, root, False, mask, &ev);
376 XMapRaised(dpy, win);
385 if(m.buttons==0 || (m.buttons&~4))
388 for(i=0; i<nwin; i++)
389 if(ptinrect(m.xy, win[i].r))
404 for(j=0; j<nwin; j++)
405 if(ptinrect(m.xy, win[j].r))
415 "usage: winwatch [-e exclude] [-W winsize] [-f font] [-n] [-s]\n");
420 main(int argc, char **argv)
428 fontname = "/lib/font/bit/lucsans/unicode.8.font";
432 winsize = EARGF(usage());
435 fontname = EARGF(usage());
438 exclude = regcomp(EARGF(usage()));
440 sysfatal("Bad regexp");
455 einit(Emouse | Ekeyboard);
456 Etimer = etimer(0, 1000);
458 dpy = XOpenDisplay("");
460 sysfatal("open display: %r");
462 root = DefaultRootWindow(dpy);
463 net_active_window = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
465 initdraw(0, 0, "winwatch");
466 lightblue = allocimagemix(display, DPalebluegreen, DWhite);
468 sysfatal("allocimagemix: %r");
469 font = openfont(display, fontname);
471 sysfatal("font '%s' not found", fontname);
473 XSetErrorHandler(winwatchxerrorhandler);
478 switch(eread(Emouse|Ekeyboard|Etimer, &e)){
480 if(e.kbdc==0x7F || e.kbdc=='q')
487 default: /* Etimer */