winwatch: Plan 9-ify.

This is new code, and custom to plan9port.  Make it
conform more closely to plan9 style.

Signed-off-by: Dan Cross <cross@gajendra.net>
This commit is contained in:
Dan Cross 2020-01-15 14:47:39 +00:00 committed by Dan Cross
parent 0ac4bfee32
commit 6510a2d353

View file

@ -1,13 +1,15 @@
/* slightly modified from /*
https://github.com/fhs/misc/blob/master/cmd/winwatch/winwatch.c * slightly modified from
so as to deal with memory leaks and certain X errors */ * https://github.com/fhs/misc/blob/master/cmd/winwatch/winwatch.c
* so as to deal with memory leaks and certain X errors
*/
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include <draw.h> #include <draw.h>
#include <event.h> #include <event.h>
#include <regexp.h> #include <regexp.h>
#include <stdio.h> #include <fmt.h>
#include "../devdraw/x11-inc.h" #include "../devdraw/x11-inc.h"
AUTOLIB(X11); AUTOLIB(X11);
@ -49,12 +51,13 @@ winwatchxerrorhandler(XDisplay *disp, XErrorEvent *xe)
char buf[100]; char buf[100];
XGetErrorText(disp, xe->error_code, buf, 100); XGetErrorText(disp, xe->error_code, buf, 100);
fprintf(stderr, "winwatch: X error %s, request code %d\n", buf, fprint(2, "winwatch: X error %s, request code %d\n",
xe->request_code); buf, xe->request_code);
XFlush(disp); XFlush(disp);
XSync(disp, False); XSync(disp, False);
XSetErrorHandler(oldxerrorhandler); XSetErrorHandler(oldxerrorhandler);
longjmp(savebuf, 1); longjmp(savebuf, 1);
return(0); /* Not reached */
} }
void* void*
@ -72,7 +75,7 @@ estrdup(char *s)
s = strdup(s); s = strdup(s);
if(s==nil) if(s==nil)
sysfatal("out of memory allocating"); sysfatal("out of memory allocating");
return s; return(s);
} }
char* char*
@ -86,21 +89,18 @@ getproperty(XWindow w, Atom a)
n = 100; n = 100;
p = nil; p = nil;
oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler); oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler);
s = XGetWindowProperty(dpy, w, a, 0, 100L, 0, s = XGetWindowProperty(dpy, w, a, 0, 100L, 0,
AnyPropertyType, &type, &fmt, &n, &dummy, &p); AnyPropertyType, &type, &fmt, &n, &dummy, &p);
XFlush(dpy); XFlush(dpy);
XSync(dpy, False); XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler); XSetErrorHandler(oldxerrorhandler);
if(s!=0){
XFree(p);
if (s == 0) return(nil);
return (char *) p;
else {
free(p);
return nil;
} }
return((char*)p);
} }
XWindow XWindow
@ -116,14 +116,14 @@ findname(XWindow w)
p = getproperty(w, XA_WM_NAME); p = getproperty(w, XA_WM_NAME);
if(p){ if(p){
free(p); free(p);
return w; return(w);
} }
net_wm_name = XInternAtom(dpy, "_NET_WM_NAME", FALSE); net_wm_name = XInternAtom(dpy, "_NET_WM_NAME", FALSE);
p = getproperty(w, net_wm_name); p = getproperty(w, net_wm_name);
if(p){ if(p){
free(p); free(p);
return w; return(w);
} }
oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler); oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler);
@ -131,7 +131,6 @@ findname(XWindow w)
XFlush(dpy); XFlush(dpy);
XSync(dpy, False); XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler); XSetErrorHandler(oldxerrorhandler);
if(s == 0) { if(s == 0) {
if (xwin != NULL) if (xwin != NULL)
XFree(xwin); XFree(xwin);
@ -145,7 +144,6 @@ findname(XWindow w)
return w; return w;
} }
} }
XFree(xwin); XFree(xwin);
return 0; return 0;
@ -208,7 +206,6 @@ refreshwin(void)
XFlush(dpy); XFlush(dpy);
XSync(dpy, False); XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler); XSetErrorHandler(oldxerrorhandler);
if(s==0){ if(s==0){
if(xwin!=NULL) if(xwin!=NULL)
XFree(xwin); XFree(xwin);
@ -228,11 +225,11 @@ refreshwin(void)
XFlush(dpy); XFlush(dpy);
XSync(dpy, False); XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler); XSetErrorHandler(oldxerrorhandler);
if(s==0) if(s==0)
continue; continue;
if (attr.width <= 0 || attr.override_redirect if (attr.width <= 0 ||
|| attr.map_state != IsViewable) attr.override_redirect ||
attr.map_state != IsViewable)
continue; continue;
oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler); oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler);
@ -262,13 +259,11 @@ refreshwin(void)
} }
} }
label = class.res_name;
if(showwmnames==1) if(showwmnames==1)
label = wmname; label = wmname;
else
label = class.res_name;
if (nw < nwin && win[nw].n == xwin[i] if(nw<nwin && win[nw].n==xwin[i] && strcmp(win[nw].label, label)==0) {
&& strcmp(win[nw].label, label) == 0) {
nw++; nw++;
free(wmname); free(wmname);
free(class.res_name); free(class.res_name);
@ -307,8 +302,6 @@ refreshwin(void)
if(sortlabels==1) if(sortlabels==1)
qsort(win, nwin, sizeof(struct Win), winlabelcmp); qsort(win, nwin, sizeof(struct Win), winlabelcmp);
return;
} }
void void
@ -316,10 +309,9 @@ drawnowin(int i)
{ {
Rectangle r; Rectangle r;
r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, font->height);
font->height); r = rectaddpt(
r = rectaddpt(rectaddpt rectaddpt(r,
(r,
Pt(MARGIN + (PAD + Dx(r)) * (i / rows), Pt(MARGIN + (PAD + Dx(r)) * (i / rows),
MARGIN + (PAD + Dy(r)) * (i % rows))), MARGIN + (PAD + Dy(r)) * (i % rows))),
screen->r.min); screen->r.min);
@ -346,19 +338,20 @@ geometry(void)
z = 0; z = 0;
rows = (Dy(screen->r) - 2 * MARGIN + PAD) / (font->height + PAD); rows = (Dy(screen->r) - 2 * MARGIN + PAD) / (font->height + PAD);
if(rows*cols<nwin || rows*cols>=nwin*2){ if(rows*cols<nwin || rows*cols>=nwin*2){
ncols = nwin <= 0 ? 1 : (nwin + rows - 1) / rows; ncols = 1;
if(nwin>0)
ncols = (nwin + rows - 1) / rows;
if(ncols!=cols){ if(ncols!=cols){
cols = ncols; cols = ncols;
z = 1; z = 1;
} }
} }
r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, r = Rect(0, 0, (Dx(screen->r) - 2 * MARGIN + PAD) / cols - PAD, font->height);
font->height);
for(i=0; i<nwin; i++) for(i=0; i<nwin; i++)
win[i].r = win[i].r =
rectaddpt(rectaddpt rectaddpt(
(r, rectaddpt(r,
Pt(MARGIN + (PAD + Dx(r)) * (i / rows), Pt(MARGIN + (PAD + Dx(r)) * (i / rows),
MARGIN + (PAD + Dy(r)) * (i % rows))), MARGIN + (PAD + Dy(r)) * (i % rows))),
screen->r.min); screen->r.min);
@ -380,7 +373,6 @@ redraw(Image *screen, int all)
if(!all) if(!all)
for (; i<onwin; i++) for (; i<onwin; i++)
drawnowin(i); drawnowin(i);
onwin = nwin; onwin = nwin;
} }
@ -414,7 +406,6 @@ selectwin(XWindow win)
XSync(dpy, False); XSync(dpy, False);
} }
void void
click(Mouse m) click(Mouse m)
{ {
@ -439,13 +430,10 @@ click(Mouse m)
while(m.buttons); while(m.buttons);
return; return;
} }
for(j=0; j<nwin; j++) for(j=0; j<nwin; j++)
if(ptinrect(m.xy, win[j].r)) if(ptinrect(m.xy, win[j].r))
break; break;
if (j != i) if(j==i)
return;
selectwin(win[i].n); selectwin(win[i].n);
} }
@ -466,7 +454,6 @@ main(int argc, char **argv)
sortlabels = 0; sortlabels = 0;
showwmnames = 0; showwmnames = 0;
fontname = "/lib/font/bit/lucsans/unicode.8.font"; fontname = "/lib/font/bit/lucsans/unicode.8.font";
ARGBEGIN { ARGBEGIN {
@ -489,8 +476,9 @@ main(int argc, char **argv)
break; break;
default: default:
usage(); usage();
} } ARGEND;
ARGEND if (argc)
if(argc)
usage(); usage();
/* moved up from original winwatch.c for p9p because there can be only one but we want to restart when needed */ /* moved up from original winwatch.c for p9p because there can be only one but we want to restart when needed */
@ -498,7 +486,6 @@ main(int argc, char **argv)
Etimer = etimer(0, 1000); Etimer = etimer(0, 1000);
dpy = XOpenDisplay(""); dpy = XOpenDisplay("");
if(dpy==nil) if(dpy==nil)
sysfatal("open display: %r"); sysfatal("open display: %r");
@ -509,16 +496,15 @@ main(int argc, char **argv)
lightblue = allocimagemix(display, DPalebluegreen, DWhite); lightblue = allocimagemix(display, DPalebluegreen, DWhite);
if(lightblue==nil) if(lightblue==nil)
sysfatal("allocimagemix: %r"); sysfatal("allocimagemix: %r");
if ((font = openfont(display, fontname)) == nil) font = openfont(display, fontname);
if(font==nil)
sysfatal("font '%s' not found", fontname); sysfatal("font '%s' not found", fontname);
/* reentry point upon X server errors */ /* reentry point upon X server errors */
setjmp(savebuf); setjmp(savebuf);
refreshwin(); refreshwin();
redraw(screen, 1); redraw(screen, 1);
for(;;){ for(;;){
switch(eread(Emouse|Ekeyboard|Etimer, &e)){ switch(eread(Emouse|Ekeyboard|Etimer, &e)){
case Ekeyboard: case Ekeyboard: