X-Git-Url: https://jasonwoof.com/gitweb/?p=dwm.git;a=blobdiff_plain;f=main.c;h=39172ee3ef72bdc6b5d7241f95d84b6ae185b7cc;hp=99e9f49cb3ef797165f41386e1c1b4f61763d930;hb=dba23062bad40afb1a90f60b6897cf9e1ca5035b;hpb=9cd686c93a80b4095d4ee0960bef320ccd9ea02c diff --git a/main.c b/main.c index 99e9f49..39172ee 100644 --- a/main.c +++ b/main.c @@ -3,10 +3,12 @@ * See LICENSE file for license details. */ +#include #include #include #include #include +#include #include #include @@ -19,7 +21,6 @@ char *tags[TLast] = { [Tscratch] = "scratch", [Tdev] = "dev", - [Tirc] = "irc", [Twww] = "www", [Twork] = "work", }; @@ -42,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; @@ -72,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) { @@ -93,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; @@ -113,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; @@ -128,70 +145,55 @@ 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 main(int argc, char *argv[]) { - int i; + int i, n; + fd_set rd; XSetWindowAttributes wa; unsigned int mask; + Bool readstdin = True; Window w; XEvent ev; - /* command line args */ for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { switch (argv[i][1]) { case 'v': @@ -206,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); @@ -237,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); @@ -252,7 +254,7 @@ main(int argc, char *argv[]) wa.override_redirect = 1; wa.background_pixmap = ParentRelative; - wa.event_mask = ExposureMask; + wa.event_mask = ButtonPressMask | ExposureMask; bx = by = 0; bw = sw; @@ -263,6 +265,10 @@ main(int argc, char *argv[]) XDefineCursor(dpy, barwin, cursor[CurNormal]); XMapRaised(dpy, barwin); + dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); + dc.gc = XCreateGC(dpy, root, 0, 0); + drawstatus(); + issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ @@ -271,17 +277,47 @@ main(int argc, char *argv[]) XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); - dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); - dc.gc = XCreateGC(dpy, root, 0, 0); - strcpy(stext, "dwm-"VERSION); - scan_wins(); - draw_bar(); + scan(); + /* main event loop, reads status text from stdin as well */ +Mainloop: while(running) { - XNextEvent(dpy, &ev); - if(handler[ev.type]) - (handler[ev.type])(&ev); /* call handler */ + FD_ZERO(&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) + eprint("select failed\n"); + else if(i > 0) { + if(FD_ISSET(ConnectionNumber(dpy), &rd)) { + while(XPending(dpy)) { + XNextEvent(dpy, &ev); + if(handler[ev.type]) + (handler[ev.type])(&ev); /* call handler */ + } + } + if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) { + i = n = 0; + for(;;) { + if((i = getchar()) == EOF) { + /* broken pipe/end of producer */ + readstdin = False; + strcpy(stext, "broken pipe"); + goto Mainloop; + } + if(i == '\n' || n >= sizeof(stext) - 1) + break; + stext[n++] = i; + } + stext[n] = 0; + drawstatus(); + } + } } cleanup();