X-Git-Url: https://jasonwoof.com/gitweb/?p=dwm.git;a=blobdiff_plain;f=main.c;h=1e5817698c63242e760acdca20e4ae32df43e573;hp=a1cf245e34e4006d4194cb346f4f35876599bda6;hb=6651dd7fd9e8e95cfc6c472f1adfeff41735d798;hpb=c0705eeb65733e8c5091e47d5bdc701a0779a949 diff --git a/main.c b/main.c index a1cf245..1e58176 100644 --- a/main.c +++ b/main.c @@ -1,64 +1,68 @@ -/* - * (C)opyright MMVI Anselm R. Garbe +/* (C)opyright MMVI Anselm R. Garbe * See LICENSE file for license details. */ +#include "dwm.h" #include -#include #include #include #include #include - +#include #include +#include #include #include -#include "dwm.h" - -/********** CUSTOMIZE **********/ +/* extern */ -char *tags[TLast] = { - [Tscratch] = "scratch", - [Tdev] = "dev", - [Twww] = "www", - [Twork] = "work", -}; - -/********** CUSTOMIZE **********/ - -/* X structs */ -Display *dpy; -Window root, barwin; -Atom wm_atom[WMLast], net_atom[NetLast]; -Cursor cursor[CurLast]; -Bool running = True; -Bool issel; - -int tsel = Tdev; /* default tag */ -int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; char stext[1024]; - -DC dc = {0}; +Bool *seltag; +int bx, by, bw, bh, bmw, masterd, screen, sx, sy, sw, sh; +unsigned int master, ntags, numlockmask; +Atom wmatom[WMLast], netatom[NetLast]; +Bool running = True; +Bool issel = True; Client *clients = NULL; Client *sel = NULL; +Client *stack = NULL; +Cursor cursor[CurLast]; +Display *dpy; +DC dc = {0}; +Window root, barwin; -static Bool other_wm_running; -static const char version[] = - "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; -static int (*x_xerror) (Display *, XErrorEvent *); +/* static */ + +static int (*xerrorxlib)(Display *, XErrorEvent *); +static Bool otherwm, readin; static void -usage() { error("usage: dwm [-v]\n"); } +cleanup(void) { + close(STDIN_FILENO); + while(sel) { + resize(sel, True, TopLeft); + unmanage(sel); + } + if(dc.font.set) + XFreeFontSet(dpy, dc.font.set); + else + XFreeFont(dpy, dc.font.xfont); + XUngrabKey(dpy, AnyKey, AnyModifier, root); + XFreePixmap(dpy, dc.drawable); + XFreeGC(dpy, dc.gc); + XDestroyWindow(dpy, barwin); + XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); + XSync(dpy, False); + free(seltag); +} static void -scan_wins() -{ +scan(void) { unsigned int i, num; - Window *wins; + Window *wins, d1, d2; XWindowAttributes wa; - Window d1, d2; + wins = NULL; if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { for(i = 0; i < num; i++) { if(!XGetWindowAttributes(dpy, wins[i], &wa)) @@ -73,49 +77,111 @@ scan_wins() XFree(wins); } -static int -win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) -{ - Atom real; - int format; - unsigned long res, extra; - int status; - - status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, - &res, &extra, prop); +static void +setup(void) { + int i, j; + unsigned int mask; + Window w; + XModifierKeymap *modmap; + XSetWindowAttributes wa; - if(status != Success || *prop == 0) { - return 0; - } - if(res == 0) { - free((void *) *prop); + /* init atoms */ + wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); + wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); + netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); + netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); + XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, + PropModeReplace, (unsigned char *) netatom, NetLast); + /* init cursors */ + cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); + cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); + cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); + /* init modifier map */ + modmap = XGetModifierMapping(dpy); + for (i = 0; i < 8; i++) { + for (j = 0; j < modmap->max_keypermod; j++) { + if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) + numlockmask = (1 << i); + } } - return res; + XFree(modmap); + /* select for events */ + wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask + | EnterWindowMask | LeaveWindowMask; + wa.cursor = cursor[CurNormal]; + XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); + grabkeys(); + initrregs(); + for(ntags = 0; tags[ntags]; ntags++); + seltag = emallocz(sizeof(Bool) * ntags); + seltag[0] = True; + /* style */ + dc.norm[ColBG] = getcolor(NORMBGCOLOR); + dc.norm[ColFG] = getcolor(NORMFGCOLOR); + dc.sel[ColBG] = getcolor(SELBGCOLOR); + dc.sel[ColFG] = getcolor(SELFGCOLOR); + dc.status[ColBG] = getcolor(STATUSBGCOLOR); + dc.status[ColFG] = getcolor(STATUSFGCOLOR); + setfont(FONT); + /* geometry */ + bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL); + sx = sy = 0; + sw = DisplayWidth(dpy, screen); + sh = DisplayHeight(dpy, screen); + master = MASTER; + /* bar */ + bx = by = 0; + bw = sw; + dc.h = bh = dc.font.height + 2; + wa.override_redirect = 1; + wa.background_pixmap = ParentRelative; + wa.event_mask = ButtonPressMask | ExposureMask; + barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), + CopyFromParent, DefaultVisual(dpy, screen), + CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); + XDefineCursor(dpy, barwin, cursor[CurNormal]); + XMapRaised(dpy, barwin); + strcpy(stext, "dwm-"VERSION); + /* pixmap for everything */ + dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); + dc.gc = XCreateGC(dpy, root, 0, 0); + XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); + /* multihead support */ + issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); } -int -proto(Window w) -{ - unsigned char *protocols; - long res; - int protos = 0; - int i; +/* + * Startup Error handler to check if another window manager + * is already running. + */ +static int +xerrorstart(Display *dsply, XErrorEvent *ee) { + otherwm = True; + return -1; +} + +/* extern */ - res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols); - if(res <= 0) { +int +getproto(Window w) { + int i, format, protos, status; + unsigned long extra, res; + Atom *protocols, real; + + protos = 0; + status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False, + XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols); + if(status != Success || protocols == 0) return protos; - } - for(i = 0; i < res; i++) { - if(protocols[i] == wm_atom[WMDelete]) - protos |= WM_PROTOCOL_DELWIN; - } - free((char *) protocols); + for(i = 0; i < res; i++) + if(protocols[i] == wmatom[WMDelete]) + protos |= PROTODELWIN; + free(protocols); return protos; } void -sendevent(Window w, Atom a, long value) -{ +sendevent(Window w, Atom a, long value) { XEvent e; e.type = ClientMessage; @@ -125,203 +191,94 @@ sendevent(Window w, Atom a, long value) e.xclient.data.l[0] = value; e.xclient.data.l[1] = CurrentTime; XSendEvent(dpy, w, False, NoEventMask, &e); - XFlush(dpy); + XSync(dpy, False); } -/* - * 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(). +void +quit(Arg *arg) { + readin = running = False; +} + +/* There's no way to check accesses to destroyed windows, thus those cases are + * ignored (especially on UnmapNotify's). Other types of errors call Xlibs + * default error handler, which may call exit. */ int -xerror(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)) +xerror(Display *dpy, XErrorEvent *ee) { + 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) + || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable)) return 0; fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", - error->request_code, error->error_code); - return x_xerror(dpy, error); /* may call exit() */ -} - -/* - * Startup Error handler to check if another window manager - * is already running. - */ -static int -startup_xerror(Display *dpy, XErrorEvent *error) -{ - other_wm_running = True; - return -1; -} - -static void -cleanup() -{ - while(sel) { - resize(sel, True); - unmanage(sel); - } - XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); -} - -void -quit(Arg *arg) -{ - running = False; + ee->request_code, ee->error_code); + return xerrorxlib(dpy, ee); /* may call exit */ } int -main(int argc, char *argv[]) -{ - int i, n; +main(int argc, char *argv[]) { + int r, xfd; fd_set rd; - XSetWindowAttributes wa; - unsigned int mask; - Bool readstdin = True; - Window w; - XEvent ev; - for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { - switch (argv[i][1]) { - case 'v': - fprintf(stdout, "%s", version); - exit(0); - break; - default: - usage(); - break; - } + if(argc == 2 && !strncmp("-v", argv[1], 3)) { + fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); + exit(EXIT_SUCCESS); } - + else if(argc != 1) + eprint("usage: dwm [-v]\n"); dpy = XOpenDisplay(0); if(!dpy) - error("dwm: cannot connect X server\n"); - + eprint("dwm: cannot open display\n"); + xfd = ConnectionNumber(dpy); screen = DefaultScreen(dpy); root = RootWindow(dpy, screen); - - /* check if another WM is already running */ - other_wm_running = False; - XSetErrorHandler(startup_xerror); - /* this causes an error if some other WM is running */ + otherwm = False; + XSetErrorHandler(xerrorstart); + /* this causes an error if some other window manager is running */ XSelectInput(dpy, root, SubstructureRedirectMask); - XFlush(dpy); - - if(other_wm_running) - error("dwm: another window manager is already running\n"); - - XSetErrorHandler(0); - x_xerror = XSetErrorHandler(xerror); - - /* init atoms */ - wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); - wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); - net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); - net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); - XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32, - PropModeReplace, (unsigned char *) net_atom, NetLast); - - /* init cursors */ - cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); - cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); - cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); - - grabkeys(); - - /* style */ - dc.bg = getcolor(BGCOLOR); - dc.fg = getcolor(FGCOLOR); - dc.border = getcolor(BORDERCOLOR); - setfont(FONT); - - sx = sy = 0; - sw = DisplayWidth(dpy, screen); - sh = DisplayHeight(dpy, screen); - mw = (sw * MASTERW) / 100; - - wa.override_redirect = 1; - wa.background_pixmap = ParentRelative; - wa.event_mask = ButtonPressMask | ExposureMask; - - bx = by = 0; - bw = sw; - dc.h = bh = dc.font.height + 4; - barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), - CopyFromParent, DefaultVisual(dpy, screen), - CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); - 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); + XSync(dpy, False); + if(otherwm) + eprint("dwm: another window manager is already running\n"); + + XSync(dpy, False); + XSetErrorHandler(NULL); + xerrorxlib = XSetErrorHandler(xerror); + XSync(dpy, False); + setup(); drawstatus(); + scan(); - issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); - - wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ - | LeaveWindowMask; - wa.cursor = cursor[CurNormal]; - - XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); - - strcpy(stext, "dwm-"VERSION); - scan_wins(); - - /* main event loop, reads status text from stdin as well */ -Mainloop: + /* main event loop, also reads status text from stdin */ + XSync(dpy, False); + procevent(); + readin = True; while(running) { FD_ZERO(&rd); - if(readstdin) + if(readin) FD_SET(STDIN_FILENO, &rd); - FD_SET(ConnectionNumber(dpy), &rd); - - i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0); - if(i == -1 && errno == EINTR) + FD_SET(xfd, &rd); + r = select(xfd + 1, &rd, NULL, NULL, NULL); + if((r == -1) && (errno == EINTR)) continue; - if(i < 0) - error("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; + if(r > 0) { + if(readin && FD_ISSET(STDIN_FILENO, &rd)) { + readin = NULL != fgets(stext, sizeof(stext), stdin); + if(readin) + stext[strlen(stext) - 1] = 0; + else + strcpy(stext, "broken pipe"); drawstatus(); } } + else if(r < 0) + eprint("select failed\n"); + procevent(); } - cleanup(); XCloseDisplay(dpy); - return 0; }