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,19 +51,20 @@ 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*
erealloc(void *v, ulong n)
{
v = realloc(v, n);
if (v == nil)
if(v==nil)
sysfatal("out of memory reallocating");
return v;
}
@ -70,9 +73,9 @@ char*
estrdup(char *s)
{
s = strdup(s);
if (s == nil)
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
@ -114,16 +114,16 @@ findname(XWindow w)
Atom net_wm_name;
p = getproperty(w, XA_WM_NAME);
if (p) {
if(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);
if (p) {
if(p){
free(p);
return w;
return(w);
}
oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler);
@ -131,8 +131,7 @@ findname(XWindow w)
XFlush(dpy);
XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler);
if (s == 0) {
if(s == 0) {
if (xwin != NULL)
XFree(xwin);
return 0;
@ -145,7 +144,6 @@ findname(XWindow w)
return w;
}
}
XFree(xwin);
return 0;
@ -208,19 +206,18 @@ refreshwin(void)
XFlush(dpy);
XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler);
if (s == 0) {
if (xwin != NULL)
if(s==0){
if(xwin!=NULL)
XFree(xwin);
return;
}
qsort(xwin, nxwin, sizeof(xwin[0]), wcmp);
nw = 0;
for (i = 0; i < nxwin; i++) {
for(i=0; i<nxwin; i++){
memset(&attr, 0, sizeof attr);
xwin[i] = findname(xwin[i]);
if (xwin[i] == 0)
if(xwin[i]==0)
continue;
oldxerrorhandler = XSetErrorHandler(winwatchxerrorhandler);
@ -228,11 +225,11 @@ refreshwin(void)
XFlush(dpy);
XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler);
if (s == 0)
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);
@ -241,34 +238,32 @@ refreshwin(void)
XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler);
if (s == 0)
if(s==0)
continue;
if (exclude != nil && regexec(exclude, class.res_name, nil, 0)) {
if (exclude!=nil && regexec(exclude, class.res_name, nil, 0)) {
free(class.res_name);
free(class.res_class);
continue;
}
net_wm_name = XInternAtom (dpy, "_NET_WM_NAME", FALSE);
net_wm_name = XInternAtom(dpy, "_NET_WM_NAME", FALSE);
wmname = getproperty(xwin[i], net_wm_name);
if (wmname == nil) {
if(wmname==nil){
wmname = getproperty(xwin[i], XA_WM_NAME);
if (wmname == nil) {
if(wmname==nil){
free(class.res_name);
free(class.res_class);
continue;
}
}
if (showwmnames == 1)
label = wmname;
else
label = class.res_name;
if(showwmnames==1)
label = wmname;
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);
@ -276,12 +271,12 @@ refreshwin(void)
continue;
}
if (nw < nwin) {
if(nw<nwin){
free(win[nw].label);
win[nw].label = nil;
}
if (nw >= mwin) {
if(nw>=mwin){
mwin += 8;
win = erealloc(win, mwin * sizeof(win[0]));
}
@ -301,14 +296,12 @@ refreshwin(void)
XSync(dpy, False);
XSetErrorHandler(oldxerrorhandler);
while (nwin > nw)
while(nwin>nw)
free(win[--nwin].label);
nwin = nw;
if (sortlabels == 1)
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);
@ -345,20 +337,21 @@ 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;
if (ncols != cols) {
if(rows*cols<nwin || rows*cols>=nwin*2){
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);
for (i = 0; i < nwin; i++)
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);
@ -372,22 +365,21 @@ redraw(Image *screen, int all)
int i;
all |= geometry();
if (all)
if(all)
draw(screen, screen->r, lightblue, nil, ZP);
for (i = 0; i < nwin; i++)
if (all || win[i].dirty)
for(i=0; i<nwin; i++)
if(all || win[i].dirty)
drawwin(i);
if (!all)
for (; i < onwin; i++)
if(!all)
for (; i<onwin; i++)
drawnowin(i);
onwin = nwin;
}
void
eresized(int new)
{
if (new && getwindow(display, Refmesg) < 0)
if(new && getwindow(display, Refmesg)<0)
fprint(2, "can't reattach to window");
geometry();
redraw(screen, 1);
@ -414,38 +406,34 @@ selectwin(XWindow win)
XSync(dpy, False);
}
void
click(Mouse m)
{
int i, j;
if (m.buttons == 0 || (m.buttons & ~4))
if(m.buttons==0 || (m.buttons&~4))
return;
for (i = 0; i < nwin; i++)
if (ptinrect(m.xy, win[i].r))
for(i=0; i<nwin; i++)
if(ptinrect(m.xy, win[i].r))
break;
if (i == nwin)
if(i==nwin)
return;
do
m = emouse();
while (m.buttons == 4);
while(m.buttons==4);
if (m.buttons != 0) {
if(m.buttons!=0){
do
m = emouse();
while (m.buttons);
while(m.buttons);
return;
}
for (j = 0; j < nwin; j++)
if (ptinrect(m.xy, win[j].r))
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 {
@ -478,7 +465,7 @@ main(int argc, char **argv)
break;
case 'e':
exclude = regcomp(EARGF(usage()));
if (exclude == nil)
if(exclude==nil)
sysfatal("Bad regexp");
break;
case 's':
@ -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,8 +486,7 @@ main(int argc, char **argv)
Etimer = etimer(0, 1000);
dpy = XOpenDisplay("");
if (dpy == nil)
if(dpy==nil)
sysfatal("open display: %r");
root = DefaultRootWindow(dpy);
@ -507,26 +494,25 @@ main(int argc, char **argv)
initdraw(0, 0, "winwatch");
lightblue = allocimagemix(display, DPalebluegreen, DWhite);
if (lightblue == nil)
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)) {
for(;;){
switch(eread(Emouse|Ekeyboard|Etimer, &e)){
case Ekeyboard:
if (e.kbdc == 0x7F || e.kbdc == 'q')
if(e.kbdc==0x7F || e.kbdc=='q')
exits(0);
break;
case Emouse:
if (e.mouse.buttons)
if(e.mouse.buttons)
click(e.mouse);
/* fall through */
default: /* Etimer */