fix double free bug, simplify error handling, reduce X11 calls

This commit is contained in:
MvA 2022-09-11 15:04:39 +02:00 committed by Dan Cross
parent 3e764832bc
commit 846f724983

View file

@ -24,7 +24,6 @@ struct Win {
XDisplay *dpy; XDisplay *dpy;
XWindow root; XWindow root;
Atom net_active_window; Atom net_active_window;
Reprog *exclude = nil; Reprog *exclude = nil;
Win *win; Win *win;
@ -38,8 +37,7 @@ Font *font;
Image *lightblue; Image *lightblue;
enum enum {
{
PAD = 3, PAD = 3,
MARGIN = 5 MARGIN = 5
}; };
@ -71,7 +69,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*
@ -90,10 +88,10 @@ getproperty(XWindow w, Atom a)
if(s!=0){ if(s!=0){
XFree(p); XFree(p);
return nil; return(nil);
} }
return (char*)p; return((char*)p);
} }
XWindow XWindow
@ -109,14 +107,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);
} }
rwin = 0; rwin = 0;
@ -144,6 +142,7 @@ wcmp(const void *w1, const void *w2)
} }
/* unicode-aware case-insensitive strcmp, taken from golangs gc/subr.c */ /* unicode-aware case-insensitive strcmp, taken from golangs gc/subr.c */
int int
_cistrcmp(char *p, char *q) _cistrcmp(char *p, char *q)
{ {
@ -194,7 +193,6 @@ refreshwin(void)
XFree(xwin); XFree(xwin);
return; return;
} }
qsort(xwin, nxwin, sizeof(xwin[0]), wcmp); qsort(xwin, nxwin, sizeof(xwin[0]), wcmp);
nw = 0; nw = 0;
@ -257,7 +255,6 @@ refreshwin(void)
mwin += 8; mwin += 8;
win = erealloc(win, mwin * sizeof(win[0])); win = erealloc(win, mwin * sizeof(win[0]));
} }
win[nw].n = xwin[i]; win[nw].n = xwin[i];
win[nw].label = estrdup(label); win[nw].label = estrdup(label);
win[nw].dirty = 1; win[nw].dirty = 1;
@ -455,6 +452,8 @@ main(int argc, char **argv)
if(argc) if(argc)
usage(); usage();
einit(Emouse | Ekeyboard);
Etimer = etimer(0, 1000);
dpy = XOpenDisplay(""); dpy = XOpenDisplay("");
if(dpy==nil) if(dpy==nil)
@ -471,14 +470,10 @@ main(int argc, char **argv)
if(font==nil) if(font==nil)
sysfatal("font '%s' not found", fontname); sysfatal("font '%s' not found", fontname);
einit(Emouse | Ekeyboard);
Etimer = etimer(0, 1000);
XSetErrorHandler(winwatchxerrorhandler); XSetErrorHandler(winwatchxerrorhandler);
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: