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: 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: 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@291: unsigned int ntags, numlockmask; 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: { 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@291: int i, j, xfd; arg@123: unsigned int mask; garbeam@59: fd_set rd; garbeam@0: Window w; arg@291: XModifierKeymap *modmap; 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: arg@292: XSync(dpy, False); 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: arg@291: modmap = XGetModifierMapping(dpy); arg@291: for (i = 0; i < 8; i++) { arg@291: for (j = 0; j < modmap->max_keypermod; j++) { arg@291: if(modmap->modifiermap[i * modmap->max_keypermod + j] == XKeysymToKeycode(dpy, XK_Num_Lock)) arg@291: numlockmask = (1 << i); arg@291: } arg@291: } arg@291: XFree(modmap); arg@291: arg@291: wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask | EnterWindowMask | LeaveWindowMask; arg@281: wa.cursor = cursor[CurNormal]; arg@281: XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); arg@281: garbeam@73: grabkeys(); arg@191: initrregs(); garbeam@0: arg@178: for(ntags = 0; tags[ntags]; ntags++); arg@262: seltag = emallocz(sizeof(Bool) * ntags); arg@326: seltag[0] = 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: arg@291: bx = by = 0; arg@291: bw = sw; arg@291: dc.h = bh = dc.font.height + 4; garbeam@57: wa.override_redirect = 1; garbeam@57: wa.background_pixmap = ParentRelative; garbeam@58: wa.event_mask = ButtonPressMask | ExposureMask; 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: 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@266: i = select(xfd + 1, &rd, NULL, NULL, NULL); arg@316: if((i == -1) && (errno == EINTR)) garbeam@59: continue; arg@316: 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: } garbeam@59: } arg@316: else if(i < 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: }