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 arg@291: #include garbeam@0: #include garbeam@0: #include garbeam@0: arg@333: /* extern */ arg@333: arg@333: char stext[1024]; arg@333: Bool *seltag; arg@333: int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; arg@333: unsigned int ntags, numlockmask; arg@333: Atom wmatom[WMLast], netatom[NetLast]; arg@333: Bool running = True; arg@333: Bool issel = True; arg@333: Client *clients = NULL; arg@333: Client *sel = NULL; arg@333: Cursor cursor[CurLast]; arg@333: Display *dpy; arg@333: DC dc = {0}; arg@333: Window root, barwin; arg@333: garbeam@84: /* static */ garbeam@0: arg@123: static int (*xerrorxlib)(Display *, XErrorEvent *); arg@302: static Bool otherwm, readin; garbeam@0: garbeam@0: static void garbeam@76: cleanup() garbeam@76: { arg@302: close(STDIN_FILENO); garbeam@76: while(sel) { arg@99: resize(sel, True, TopLeft); garbeam@76: unmanage(sel); garbeam@76: } arg@295: if(dc.font.set) arg@295: XFreeFontSet(dpy, dc.font.set); arg@295: else arg@295: XFreeFont(dpy, dc.font.xfont); arg@292: XUngrabKey(dpy, AnyKey, AnyModifier, root); arg@295: XFreePixmap(dpy, dc.drawable); arg@295: XFreeGC(dpy, dc.gc); arg@309: XDestroyWindow(dpy, barwin); garbeam@76: XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); arg@292: XSync(dpy, False); 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: arg@292: wins = NULL; 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: arg@333: static void arg@333: setup() arg@333: { arg@333: int i, j; arg@333: unsigned int mask; arg@333: Window w; arg@333: XModifierKeymap *modmap; arg@333: XSetWindowAttributes wa; arg@333: arg@333: /* init atoms */ arg@333: wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); arg@333: wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); arg@333: netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); arg@333: netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); arg@333: XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32, arg@333: PropModeReplace, (unsigned char *) netatom, NetLast); arg@333: arg@333: /* init cursors */ arg@333: cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr); arg@333: cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing); arg@333: cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur); arg@333: arg@333: modmap = XGetModifierMapping(dpy); arg@333: for (i = 0; i < 8; i++) { arg@333: for (j = 0; j < modmap->max_keypermod; j++) { arg@333: if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) arg@333: numlockmask = (1 << i); arg@333: } arg@333: } arg@333: XFree(modmap); arg@333: arg@333: wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; arg@333: wa.cursor = cursor[CurNormal]; arg@333: XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); arg@333: arg@333: grabkeys(); arg@333: initrregs(); arg@333: arg@333: for(ntags = 0; tags[ntags]; ntags++); arg@333: seltag = emallocz(sizeof(Bool) * ntags); arg@333: seltag[0] = True; arg@333: arg@333: /* style */ arg@352: dc.bg = getcolor(BGCOLOR); arg@352: dc.fg = getcolor(FGCOLOR); arg@352: dc.border = getcolor(BORDERCOLOR); arg@333: setfont(FONT); arg@333: arg@333: sx = sy = 0; arg@333: sw = DisplayWidth(dpy, screen); arg@333: sh = DisplayHeight(dpy, screen); arg@333: mw = (sw * MASTERW) / 100; arg@333: arg@333: bx = by = 0; arg@333: bw = sw; arg@333: dc.h = bh = dc.font.height + 4; arg@333: wa.override_redirect = 1; arg@333: wa.background_pixmap = ParentRelative; arg@333: wa.event_mask = ButtonPressMask | ExposureMask; arg@333: barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen), arg@333: CopyFromParent, DefaultVisual(dpy, screen), arg@333: CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa); arg@333: XDefineCursor(dpy, barwin, cursor[CurNormal]); arg@333: XMapRaised(dpy, barwin); arg@333: arg@333: dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen)); arg@333: dc.gc = XCreateGC(dpy, root, 0, 0); arg@344: XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter); arg@333: arg@333: issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); arg@333: strcpy(stext, "dwm-"VERSION); arg@333: } arg@333: 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@13: int garbeam@75: getproto(Window w) garbeam@13: { arg@329: int i, format, protos, status; arg@328: unsigned long extra, res; arg@328: Atom *protocols, real; garbeam@13: arg@329: protos = 0; arg@329: status = XGetWindowProperty(dpy, w, wmatom[WMProtocols], 0L, 20L, False, arg@329: XA_ATOM, &real, &format, &res, &extra, (unsigned char **)&protocols); arg@328: if(status != Success || protocols == 0) garbeam@13: return protos; arg@329: for(i = 0; i < res; i++) garbeam@77: if(protocols[i] == wmatom[WMDelete]) arg@157: protos |= PROTODELWIN; arg@328: free(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: { arg@302: readin = 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@333: int r, xfd; garbeam@59: fd_set rd; 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: arg@292: XSync(dpy, False); garbeam@78: XSetErrorHandler(NULL); garbeam@75: xerrorxlib = XSetErrorHandler(xerror); arg@275: XSync(dpy, False); garbeam@0: arg@333: setup(); garbeam@74: drawstatus(); garbeam@75: scan(); garbeam@3: arg@214: /* main event loop, also reads status text from stdin */ arg@242: XSync(dpy, False); arg@292: procevent(); arg@302: readin = True; 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); arg@333: r = select(xfd + 1, &rd, NULL, NULL, NULL); arg@333: if((r == -1) && (errno == EINTR)) garbeam@59: continue; arg@333: if(r > 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: } garbeam@59: } arg@333: else if(r < 0) arg@316: eprint("select failed\n"); arg@293: procevent(); garbeam@5: } garbeam@0: cleanup(); garbeam@0: XCloseDisplay(dpy); garbeam@0: garbeam@0: return 0; garbeam@0: }