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