garbeam@0: /* garbeam@0: * (C)opyright MMVI Anselm R. Garbe garbeam@0: * See LICENSE file for license details. garbeam@0: */ garbeam@0: garbeam@76: #include "dwm.h" garbeam@59: #include garbeam@0: #include garbeam@0: #include garbeam@57: #include garbeam@59: #include arg@138: #include garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@84: /* static */ garbeam@0: arg@123: static int (*xerrorxlib)(Display *, XErrorEvent *); garbeam@75: static Bool otherwm; garbeam@0: garbeam@0: static void garbeam@76: cleanup() garbeam@76: { garbeam@76: while(sel) { arg@99: resize(sel, True, TopLeft); garbeam@76: unmanage(sel); garbeam@76: } garbeam@76: XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); garbeam@76: } garbeam@0: garbeam@0: static void garbeam@75: scan() garbeam@0: { garbeam@0: unsigned int i, num; arg@123: Window *wins, d1, d2; garbeam@0: XWindowAttributes wa; garbeam@0: garbeam@0: if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) { garbeam@0: for(i = 0; i < num; i++) { garbeam@0: if(!XGetWindowAttributes(dpy, wins[i], &wa)) garbeam@0: continue; garbeam@0: if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1)) garbeam@0: continue; garbeam@0: if(wa.map_state == IsViewable) garbeam@10: manage(wins[i], &wa); garbeam@0: } garbeam@0: } garbeam@0: if(wins) garbeam@0: XFree(wins); garbeam@0: } garbeam@0: garbeam@13: static int garbeam@13: win_property(Window w, Atom a, Atom t, long l, unsigned char **prop) garbeam@13: { arg@123: int status, format; arg@123: unsigned long res, extra; garbeam@13: Atom real; garbeam@13: garbeam@13: status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format, garbeam@13: &res, &extra, prop); garbeam@13: garbeam@13: if(status != Success || *prop == 0) { garbeam@13: return 0; garbeam@13: } garbeam@13: if(res == 0) { garbeam@13: free((void *) *prop); garbeam@13: } garbeam@13: return res; garbeam@13: } garbeam@13: garbeam@76: /* garbeam@76: * Startup Error handler to check if another window manager garbeam@76: * is already running. garbeam@76: */ garbeam@76: static int garbeam@76: xerrorstart(Display *dsply, XErrorEvent *ee) garbeam@76: { garbeam@76: otherwm = True; garbeam@76: return -1; garbeam@76: } garbeam@76: garbeam@84: /* extern */ garbeam@84: garbeam@84: char stext[1024]; arg@262: Bool *seltag; garbeam@84: int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; arg@178: unsigned int ntags; garbeam@84: Atom wmatom[WMLast], netatom[NetLast]; garbeam@84: Bool running = True; garbeam@84: Bool issel = True; garbeam@84: Client *clients = NULL; garbeam@84: Client *sel = NULL; garbeam@84: Cursor cursor[CurLast]; garbeam@84: Display *dpy; garbeam@84: DC dc = {0}; garbeam@84: Window root, barwin; garbeam@76: garbeam@13: int garbeam@75: getproto(Window w) garbeam@13: { garbeam@13: int protos = 0; garbeam@13: int i; arg@123: long res; arg@203: Atom *protocols; garbeam@13: arg@203: res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, arg@203: ((unsigned char **)&protocols)); garbeam@13: if(res <= 0) { garbeam@13: return protos; garbeam@13: } garbeam@13: for(i = 0; i < res; i++) { garbeam@77: if(protocols[i] == wmatom[WMDelete]) arg@157: protos |= PROTODELWIN; garbeam@13: } garbeam@13: free((char *) protocols); garbeam@13: return protos; garbeam@13: } garbeam@13: garbeam@13: void garbeam@74: sendevent(Window w, Atom a, long value) garbeam@13: { garbeam@13: XEvent e; garbeam@13: garbeam@13: e.type = ClientMessage; garbeam@13: e.xclient.window = w; garbeam@13: e.xclient.message_type = a; garbeam@13: e.xclient.format = 32; garbeam@13: e.xclient.data.l[0] = value; garbeam@13: e.xclient.data.l[1] = CurrentTime; garbeam@13: XSendEvent(dpy, w, False, NoEventMask, &e); garbeam@79: XSync(dpy, False); garbeam@13: } garbeam@13: garbeam@76: void garbeam@76: quit(Arg *arg) garbeam@75: { garbeam@76: running = False; garbeam@75: } garbeam@75: garbeam@75: /* garbeam@84: * There's no way to check accesses to destroyed windows, thus those cases are garbeam@84: * ignored (especially on UnmapNotify's). Other types of errors call Xlibs garbeam@84: * default error handler, which calls exit(). garbeam@0: */ garbeam@10: int garbeam@75: xerror(Display *dpy, XErrorEvent *ee) garbeam@0: { garbeam@75: if(ee->error_code == BadWindow arg@123: || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch) arg@123: || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable) arg@123: || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable) arg@123: || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable) arg@123: || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch) arg@123: || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)) garbeam@0: return 0; garbeam@34: fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", arg@123: ee->request_code, ee->error_code); garbeam@75: return xerrorxlib(dpy, ee); /* may call exit() */ garbeam@27: } garbeam@27: garbeam@0: int garbeam@0: main(int argc, char *argv[]) garbeam@0: { arg@265: int i, xfd; arg@123: unsigned int mask; garbeam@59: fd_set rd; arg@164: Bool readin = True; garbeam@0: Window w; garbeam@5: XEvent ev; arg@123: XSetWindowAttributes wa; garbeam@0: arg@137: if(argc == 2 && !strncmp("-v", argv[1], 3)) { arg@137: fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout); arg@137: exit(EXIT_SUCCESS); garbeam@0: } arg@137: else if(argc != 1) arg@137: eprint("usage: dwm [-v]\n"); garbeam@0: garbeam@0: dpy = XOpenDisplay(0); garbeam@0: if(!dpy) arg@197: eprint("dwm: cannot open display\n"); garbeam@0: arg@265: xfd = ConnectionNumber(dpy); garbeam@0: screen = DefaultScreen(dpy); garbeam@0: root = RootWindow(dpy, screen); garbeam@0: garbeam@75: otherwm = False; garbeam@75: XSetErrorHandler(xerrorstart); arg@197: /* this causes an error if some other window manager is running */ garbeam@0: XSelectInput(dpy, root, SubstructureRedirectMask); garbeam@78: XSync(dpy, False); garbeam@0: garbeam@75: if(otherwm) garbeam@75: eprint("dwm: another window manager is already running\n"); garbeam@0: garbeam@78: XSetErrorHandler(NULL); garbeam@75: xerrorxlib = XSetErrorHandler(xerror); arg@275: XSync(dpy, False); garbeam@0: garbeam@0: /* init atoms */ garbeam@77: wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); garbeam@77: wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); garbeam@77: netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); garbeam@77: netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); garbeam@77: XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, garbeam@77: PropModeReplace, (unsigned char *) netatom, NetLast); garbeam@0: garbeam@0: /* init cursors */ garbeam@0: cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); garbeam@0: cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); garbeam@0: cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); garbeam@0: garbeam@73: grabkeys(); arg@191: initrregs(); garbeam@0: arg@178: for(ntags = 0; tags[ntags]; ntags++); arg@262: seltag = emallocz(sizeof(Bool) * ntags); arg@262: seltag[DEFTAG] = True; arg@178: garbeam@5: /* style */ garbeam@74: dc.bg = getcolor(BGCOLOR); garbeam@74: dc.fg = getcolor(FGCOLOR); arg@237: dc.border = getcolor(BORDERCOLOR); garbeam@74: setfont(FONT); garbeam@5: garbeam@57: sx = sy = 0; garbeam@57: sw = DisplayWidth(dpy, screen); garbeam@57: sh = DisplayHeight(dpy, screen); garbeam@57: mw = (sw * MASTERW) / 100; garbeam@5: garbeam@57: wa.override_redirect = 1; garbeam@57: wa.background_pixmap = ParentRelative; garbeam@58: wa.event_mask = ButtonPressMask | ExposureMask; garbeam@57: garbeam@57: bx = by = 0; garbeam@57: bw = sw; garbeam@57: dc.h = bh = dc.font.height + 4; garbeam@57: barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), garbeam@57: CopyFromParent, DefaultVisual(dpy, screen), garbeam@57: CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); garbeam@57: XDefineCursor(dpy, barwin, cursor[CurNormal]); garbeam@57: XMapRaised(dpy, barwin); garbeam@57: garbeam@60: dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); garbeam@60: dc.gc = XCreateGC(dpy, root, 0, 0); arg@222: arg@222: strcpy(stext, "dwm-"VERSION); garbeam@74: drawstatus(); garbeam@60: garbeam@57: issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); garbeam@21: arg@123: wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask; garbeam@0: wa.cursor = cursor[CurNormal]; garbeam@0: XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); garbeam@0: garbeam@75: scan(); garbeam@3: arg@214: /* main event loop, also reads status text from stdin */ arg@242: XSync(dpy, False); garbeam@5: while(running) { garbeam@59: FD_ZERO(&rd); arg@164: if(readin) arg@164: FD_SET(STDIN_FILENO, &rd); arg@265: FD_SET(xfd, &rd); garbeam@59: arg@266: i = select(xfd + 1, &rd, NULL, NULL, NULL); garbeam@59: if(i == -1 && errno == EINTR) garbeam@59: continue; garbeam@59: if(i < 0) garbeam@75: eprint("select failed\n"); garbeam@59: else if(i > 0) { arg@164: if(readin && FD_ISSET(STDIN_FILENO, &rd)) { arg@164: readin = NULL != fgets(stext, sizeof(stext), stdin); arg@164: if(readin) arg@164: stext[strlen(stext) - 1] = 0; arg@162: else arg@164: strcpy(stext, "broken pipe"); garbeam@74: drawstatus(); garbeam@59: } arg@275: if(FD_ISSET(xfd, &rd)) { arg@275: while(XPending(dpy)) { arg@275: XNextEvent(dpy, &ev); arg@275: if(handler[ev.type]) arg@275: (handler[ev.type])(&ev); /* call handler */ arg@275: } arg@275: } garbeam@59: } garbeam@5: } garbeam@0: cleanup(); garbeam@0: XCloseDisplay(dpy); garbeam@0: garbeam@0: return 0; garbeam@0: }