X-Git-Url: https://jasonwoof.com/gitweb/?p=dwm.git;a=blobdiff_plain;f=main.c;h=39172ee3ef72bdc6b5d7241f95d84b6ae185b7cc;hp=d9ca9f734f8535e5a0baa2170b0ec8410313a789;hb=dba23062bad40afb1a90f60b6897cf9e1ca5035b;hpb=e3fd306ee2aa5b92bf4fba339a1ef0d88a97a5dc diff --git a/main.c b/main.c index d9ca9f7..39172ee 100644 --- a/main.c +++ b/main.c @@ -43,16 +43,16 @@ DC dc = {0}; Client *clients = NULL; Client *sel = NULL; -static Bool other_wm_running; +static Bool otherwm; static const char version[] = "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; -static int (*x_error_handler) (Display *, XErrorEvent *); +static int (*xerrorxlib)(Display *, XErrorEvent *); static void -usage() { error("usage: dwm [-v]\n"); } +usage() { eprint("usage: dwm [-v]\n"); } static void -scan_wins() +scan() { unsigned int i, num; Window *wins; @@ -73,6 +73,22 @@ scan_wins() XFree(wins); } +static void +cleanup() +{ + while(sel) { + resize(sel, True); + unmanage(sel); + } + XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); +} + +void +quit(Arg *arg) +{ + running = False; +} + static int win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) { @@ -94,7 +110,7 @@ win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) } int -win_proto(Window w) +getproto(Window w) { unsigned char *protocols; long res; @@ -114,7 +130,7 @@ win_proto(Window w) } void -send_message(Window w, Atom a, long value) +sendevent(Window w, Atom a, long value) { XEvent e; @@ -129,58 +145,42 @@ send_message(Window w, Atom a, long value) } /* - * There's no way to check accesses to destroyed windows, thus - * those cases are ignored (especially on UnmapNotify's). - * Other types of errors call Xlib's default error handler, which - * calls exit(). - */ -int -error_handler(Display *dpy, XErrorEvent *error) -{ - if(error->error_code == BadWindow - || (error->request_code == X_SetInputFocus - && error->error_code == BadMatch) - || (error->request_code == X_PolyText8 - && error->error_code == BadDrawable) - || (error->request_code == X_PolyFillRectangle - && error->error_code == BadDrawable) - || (error->request_code == X_PolySegment - && error->error_code == BadDrawable) - || (error->request_code == X_ConfigureWindow - && error->error_code == BadMatch) - || (error->request_code == X_GrabKey - && error->error_code == BadAccess)) - return 0; - fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", - error->request_code, error->error_code); - return x_error_handler(dpy, error); /* may call exit() */ -} - -/* * Startup Error handler to check if another window manager * is already running. */ static int -startup_error_handler(Display *dpy, XErrorEvent *error) +xerrorstart(Display *dsply, XErrorEvent *ee) { - other_wm_running = True; + otherwm = True; return -1; } -static void -cleanup() -{ - while(sel) { - resize(sel, True); - unmanage(sel); - } - XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); -} - -void -quit(Arg *arg) +/* + * There's no way to check accesses to destroyed windows, thus + * those cases are ignored (especially on UnmapNotify's). + * Other types of errors call Xlib's default error handler, which + * calls exit(). + */ +int +xerror(Display *dpy, XErrorEvent *ee) { - running = False; + if(ee->error_code == BadWindow + || (ee->request_code == X_SetInputFocus + && ee->error_code == BadMatch) + || (ee->request_code == X_PolyText8 + && ee->error_code == BadDrawable) + || (ee->request_code == X_PolyFillRectangle + && ee->error_code == BadDrawable) + || (ee->request_code == X_PolySegment + && ee->error_code == BadDrawable) + || (ee->request_code == X_ConfigureWindow + && ee->error_code == BadMatch) + || (ee->request_code == X_GrabKey + && ee->error_code == BadAccess)) + return 0; + fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", + ee->request_code, ee->error_code); + return xerrorxlib(dpy, ee); /* may call exit() */ } int @@ -190,6 +190,7 @@ main(int argc, char *argv[]) fd_set rd; XSetWindowAttributes wa; unsigned int mask; + Bool readstdin = True; Window w; XEvent ev; @@ -207,23 +208,23 @@ main(int argc, char *argv[]) dpy = XOpenDisplay(0); if(!dpy) - error("dwm: cannot connect X server\n"); + eprint("dwm: cannot connect X server\n"); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); /* check if another WM is already running */ - other_wm_running = False; - XSetErrorHandler(startup_error_handler); + otherwm = False; + XSetErrorHandler(xerrorstart); /* this causes an error if some other WM is running */ XSelectInput(dpy, root, SubstructureRedirectMask); XFlush(dpy); - if(other_wm_running) - error("dwm: another window manager is already running\n"); + if(otherwm) + eprint("dwm: another window manager is already running\n"); XSetErrorHandler(0); - x_error_handler = XSetErrorHandler(error_handler); + xerrorxlib = XSetErrorHandler(xerror); /* init atoms */ wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); @@ -238,13 +239,13 @@ main(int argc, char *argv[]) cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); - update_keys(); + grabkeys(); /* style */ - dc.bg = initcolor(BGCOLOR); - dc.fg = initcolor(FGCOLOR); - dc.border = initcolor(BORDERCOLOR); - initfont(FONT); + dc.bg = getcolor(BGCOLOR); + dc.fg = getcolor(FGCOLOR); + dc.border = getcolor(BORDERCOLOR); + setfont(FONT); sx = sy = 0; sw = DisplayWidth(dpy, screen); @@ -266,7 +267,7 @@ main(int argc, char *argv[]) dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); dc.gc = XCreateGC(dpy, root, 0, 0); - draw_bar(); + drawstatus(); issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); @@ -277,31 +278,36 @@ main(int argc, char *argv[]) XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); strcpy(stext, "dwm-"VERSION); - scan_wins(); + scan(); /* main event loop, reads status text from stdin as well */ Mainloop: while(running) { FD_ZERO(&rd); - FD_SET(0, &rd); + if(readstdin) + FD_SET(STDIN_FILENO, &rd); FD_SET(ConnectionNumber(dpy), &rd); i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0); if(i == -1 && errno == EINTR) continue; if(i < 0) - error("select failed\n"); + eprint("select failed\n"); else if(i > 0) { - if(FD_ISSET(ConnectionNumber(dpy), &rd) && XPending(dpy) > 0) { - XNextEvent(dpy, &ev); - if(handler[ev.type]) - (handler[ev.type])(&ev); /* call handler */ + if(FD_ISSET(ConnectionNumber(dpy), &rd)) { + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type])(&ev); /* call handler */ + } } - if(FD_ISSET(0, &rd)) { + if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) { i = n = 0; for(;;) { if((i = getchar()) == EOF) { - stext[0] = 0; + /* broken pipe/end of producer */ + readstdin = False; + strcpy(stext, "broken pipe"); goto Mainloop; } if(i == '\n' || n >= sizeof(stext) - 1) @@ -309,7 +315,7 @@ Mainloop: stext[n++] = i; } stext[n] = 0; - draw_bar(); + drawstatus(); } } }