garbeam@0: /* garbeam@0: * (C)opyright MMVI Anselm R. Garbe garbeam@0: * See LICENSE file for license details. garbeam@0: */ garbeam@0: garbeam@59: #include garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@57: #include garbeam@59: #include garbeam@0: garbeam@0: #include garbeam@0: #include garbeam@0: #include garbeam@0: garbeam@43: #include "dwm.h" garbeam@0: garbeam@31: /********** CUSTOMIZE **********/ garbeam@31: garbeam@31: char *tags[TLast] = { garbeam@31: [Tscratch] = "scratch", garbeam@31: [Tdev] = "dev", garbeam@31: [Twww] = "www", garbeam@31: [Twork] = "work", garbeam@31: }; garbeam@31: garbeam@31: /********** CUSTOMIZE **********/ garbeam@31: garbeam@3: /* X structs */ garbeam@0: Display *dpy; garbeam@5: Window root, barwin; garbeam@13: Atom wm_atom[WMLast], net_atom[NetLast]; garbeam@0: Cursor cursor[CurLast]; garbeam@5: Bool running = True; garbeam@31: Bool issel; garbeam@3: garbeam@31: int tsel = Tdev; /* default tag */ garbeam@57: int screen, sx, sy, sw, sh, bx, by, bw, bh, mw; garbeam@57: char stext[1024]; garbeam@3: garbeam@34: DC dc = {0}; garbeam@50: Client *clients = NULL; garbeam@50: Client *sel = NULL; garbeam@0: garbeam@0: static Bool other_wm_running; garbeam@31: static const char version[] = garbeam@57: "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n"; garbeam@74: static int (*x_xerror) (Display *, XErrorEvent *); garbeam@0: garbeam@0: static void garbeam@34: usage() { error("usage: dwm [-v]\n"); } garbeam@0: garbeam@0: static void garbeam@0: scan_wins() garbeam@0: { garbeam@0: unsigned int i, num; garbeam@0: Window *wins; garbeam@0: XWindowAttributes wa; garbeam@0: Window d1, d2; 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: { garbeam@13: Atom real; garbeam@13: int format; garbeam@13: unsigned long res, extra; garbeam@13: int status; 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@13: int garbeam@74: proto(Window w) garbeam@13: { garbeam@30: unsigned char *protocols; garbeam@13: long res; garbeam@13: int protos = 0; garbeam@13: int i; garbeam@13: garbeam@30: res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols); garbeam@13: if(res <= 0) { garbeam@13: return protos; garbeam@13: } garbeam@13: for(i = 0; i < res; i++) { garbeam@13: if(protocols[i] == wm_atom[WMDelete]) garbeam@13: protos |= WM_PROTOCOL_DELWIN; 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@13: XFlush(dpy); garbeam@13: } garbeam@13: garbeam@0: /* garbeam@0: * There's no way to check accesses to destroyed windows, thus garbeam@0: * those cases are ignored (especially on UnmapNotify's). garbeam@0: * Other types of errors call Xlib's default error handler, which garbeam@0: * calls exit(). garbeam@0: */ garbeam@10: int garbeam@74: xerror(Display *dpy, XErrorEvent *error) garbeam@0: { garbeam@0: if(error->error_code == BadWindow garbeam@0: || (error->request_code == X_SetInputFocus garbeam@0: && error->error_code == BadMatch) garbeam@0: || (error->request_code == X_PolyText8 garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_PolyFillRectangle garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_PolySegment garbeam@0: && error->error_code == BadDrawable) garbeam@0: || (error->request_code == X_ConfigureWindow garbeam@0: && error->error_code == BadMatch) garbeam@0: || (error->request_code == X_GrabKey garbeam@0: && error->error_code == BadAccess)) garbeam@0: return 0; garbeam@34: fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n", garbeam@0: error->request_code, error->error_code); garbeam@74: return x_xerror(dpy, error); /* may call exit() */ garbeam@0: } garbeam@0: garbeam@0: /* garbeam@0: * Startup Error handler to check if another window manager garbeam@0: * is already running. garbeam@0: */ garbeam@0: static int garbeam@74: startup_xerror(Display *dpy, XErrorEvent *error) garbeam@0: { garbeam@0: other_wm_running = True; garbeam@0: return -1; garbeam@0: } garbeam@0: garbeam@0: static void garbeam@0: cleanup() garbeam@0: { garbeam@50: while(sel) { garbeam@52: resize(sel, True); garbeam@50: unmanage(sel); garbeam@50: } garbeam@0: XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime); garbeam@0: } garbeam@0: garbeam@27: void garbeam@49: quit(Arg *arg) garbeam@27: { garbeam@27: running = False; garbeam@27: } garbeam@27: garbeam@0: int garbeam@0: main(int argc, char *argv[]) garbeam@0: { garbeam@59: int i, n; garbeam@59: fd_set rd; garbeam@0: XSetWindowAttributes wa; garbeam@0: unsigned int mask; garbeam@65: Bool readstdin = True; garbeam@0: Window w; garbeam@5: XEvent ev; garbeam@0: garbeam@0: for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) { garbeam@0: switch (argv[i][1]) { garbeam@0: case 'v': garbeam@0: fprintf(stdout, "%s", version); garbeam@0: exit(0); garbeam@0: break; garbeam@0: default: garbeam@0: usage(); garbeam@0: break; garbeam@0: } garbeam@0: } garbeam@0: garbeam@0: dpy = XOpenDisplay(0); garbeam@0: if(!dpy) garbeam@34: error("dwm: cannot connect X server\n"); garbeam@0: garbeam@0: screen = DefaultScreen(dpy); garbeam@0: root = RootWindow(dpy, screen); garbeam@0: garbeam@0: /* check if another WM is already running */ garbeam@0: other_wm_running = False; garbeam@74: XSetErrorHandler(startup_xerror); garbeam@0: /* this causes an error if some other WM is running */ garbeam@0: XSelectInput(dpy, root, SubstructureRedirectMask); garbeam@3: XFlush(dpy); garbeam@0: garbeam@0: if(other_wm_running) garbeam@34: error("dwm: another window manager is already running\n"); garbeam@0: garbeam@0: XSetErrorHandler(0); garbeam@74: x_xerror = XSetErrorHandler(xerror); garbeam@0: garbeam@0: /* init atoms */ garbeam@13: wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False); garbeam@13: wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False); garbeam@0: net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False); garbeam@0: net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False); garbeam@0: XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32, garbeam@0: PropModeReplace, (unsigned char *) net_atom, 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(); garbeam@0: garbeam@5: /* style */ garbeam@74: dc.bg = getcolor(BGCOLOR); garbeam@74: dc.fg = getcolor(FGCOLOR); garbeam@74: 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); garbeam@74: drawstatus(); garbeam@60: garbeam@57: issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask); garbeam@21: garbeam@9: wa.event_mask = SubstructureRedirectMask | EnterWindowMask \ garbeam@9: | LeaveWindowMask; garbeam@0: wa.cursor = cursor[CurNormal]; garbeam@57: garbeam@0: XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa); garbeam@0: garbeam@57: strcpy(stext, "dwm-"VERSION); garbeam@5: scan_wins(); garbeam@3: garbeam@59: /* main event loop, reads status text from stdin as well */ garbeam@61: Mainloop: garbeam@5: while(running) { garbeam@59: FD_ZERO(&rd); garbeam@65: if(readstdin) garbeam@65: FD_SET(STDIN_FILENO, &rd); garbeam@59: FD_SET(ConnectionNumber(dpy), &rd); garbeam@59: garbeam@59: i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0); garbeam@59: if(i == -1 && errno == EINTR) garbeam@59: continue; garbeam@59: if(i < 0) garbeam@59: error("select failed\n"); garbeam@59: else if(i > 0) { garbeam@64: if(FD_ISSET(ConnectionNumber(dpy), &rd)) { garbeam@64: while(XPending(dpy)) { garbeam@64: XNextEvent(dpy, &ev); garbeam@64: if(handler[ev.type]) garbeam@64: (handler[ev.type])(&ev); /* call handler */ garbeam@64: } garbeam@59: } garbeam@65: if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) { garbeam@59: i = n = 0; garbeam@60: for(;;) { garbeam@60: if((i = getchar()) == EOF) { garbeam@65: /* broken pipe/end of producer */ garbeam@65: readstdin = False; garbeam@65: strcpy(stext, "broken pipe"); garbeam@60: goto Mainloop; garbeam@60: } garbeam@60: if(i == '\n' || n >= sizeof(stext) - 1) garbeam@60: break; garbeam@59: stext[n++] = i; garbeam@60: } garbeam@59: stext[n] = 0; garbeam@74: drawstatus(); garbeam@59: } garbeam@59: } garbeam@5: } garbeam@0: garbeam@0: cleanup(); garbeam@0: XCloseDisplay(dpy); garbeam@0: garbeam@0: return 0; garbeam@0: }