1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
12 #include <sys/select.h>
13 #include <X11/cursorfont.h>
14 #include <X11/keysym.h>
15 #include <X11/Xatom.h>
16 #include <X11/Xproto.h>
21 int bh, bmw, screen, sx, sy, sw, sh, wax, way, waw, wah;
22 unsigned int ntags, numlockmask;
23 Atom wmatom[WMLast], netatom[NetLast];
26 Bool selscreen = True;
27 Client *clients = NULL;
30 Cursor cursor[CurLast];
37 static int (*xerrorxlib)(Display *, XErrorEvent *);
38 static Bool otherwm, readin;
45 XMoveWindow(dpy, stack->win, stack->x, stack->y);
49 XFreeFontSet(dpy, dc.font.set);
51 XFreeFont(dpy, dc.font.xfont);
52 XUngrabKey(dpy, AnyKey, AnyModifier, root);
53 XFreePixmap(dpy, dc.drawable);
55 XDestroyWindow(dpy, barwin);
56 XFreeCursor(dpy, cursor[CurNormal]);
57 XFreeCursor(dpy, cursor[CurResize]);
58 XFreeCursor(dpy, cursor[CurMove]);
59 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
71 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
72 for(i = 0; i < num; i++) {
73 if(!XGetWindowAttributes(dpy, wins[i], &wa)
74 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
76 if(wa.map_state == IsViewable)
89 XModifierKeymap *modmap;
90 XSetWindowAttributes wa;
93 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
94 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
95 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
96 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
97 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
98 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
99 PropModeReplace, (unsigned char *) netatom, NetLast);
101 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
102 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
103 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
104 /* init modifier map */
106 modmap = XGetModifierMapping(dpy);
107 for (i = 0; i < 8; i++)
108 for (j = 0; j < modmap->max_keypermod; j++) {
109 if(modmap->modifiermap[i * modmap->max_keypermod + j]
110 == XKeysymToKeycode(dpy, XK_Num_Lock))
111 numlockmask = (1 << i);
113 XFreeModifiermap(modmap);
114 /* select for events */
115 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
116 | EnterWindowMask | LeaveWindowMask;
117 wa.cursor = cursor[CurNormal];
118 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
121 for(ntags = 0; tags[ntags]; ntags++);
122 seltag = emallocz(sizeof(Bool) * ntags);
125 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
126 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
127 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
128 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
129 dc.sel[ColBG] = getcolor(SELBGCOLOR);
130 dc.sel[ColFG] = getcolor(SELFGCOLOR);
134 sw = DisplayWidth(dpy, screen);
135 sh = DisplayHeight(dpy, screen);
136 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
138 dc.h = bh = dc.font.height + 2;
139 wa.override_redirect = 1;
140 wa.background_pixmap = ParentRelative;
141 wa.event_mask = ButtonPressMask | ExposureMask;
142 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
143 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
144 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
145 XDefineCursor(dpy, barwin, cursor[CurNormal]);
146 XMapRaised(dpy, barwin);
147 strcpy(stext, "dwm-"VERSION);
150 way = sy + (TOPBAR ? bh : 0);
153 /* pixmap for everything */
154 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
155 dc.gc = XCreateGC(dpy, root, 0, 0);
156 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
157 /* multihead support */
158 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
162 * Startup Error handler to check if another window manager
163 * is already running.
166 xerrorstart(Display *dsply, XErrorEvent *ee) {
174 sendevent(Window w, Atom a, long value) {
177 e.type = ClientMessage;
178 e.xclient.window = w;
179 e.xclient.message_type = a;
180 e.xclient.format = 32;
181 e.xclient.data.l[0] = value;
182 e.xclient.data.l[1] = CurrentTime;
183 XSendEvent(dpy, w, False, NoEventMask, &e);
189 readin = running = False;
192 /* There's no way to check accesses to destroyed windows, thus those cases are
193 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
194 * default error handler, which may call exit.
197 xerror(Display *dpy, XErrorEvent *ee) {
198 if(ee->error_code == BadWindow
199 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
200 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
201 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
202 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
203 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
204 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
205 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
207 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
208 ee->request_code, ee->error_code);
209 return xerrorxlib(dpy, ee); /* may call exit */
213 main(int argc, char *argv[]) {
218 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
219 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
223 eprint("usage: dwm [-v]\n");
224 setlocale(LC_CTYPE, "");
225 dpy = XOpenDisplay(0);
227 eprint("dwm: cannot open display\n");
228 xfd = ConnectionNumber(dpy);
229 screen = DefaultScreen(dpy);
230 root = RootWindow(dpy, screen);
232 XSetErrorHandler(xerrorstart);
233 /* this causes an error if some other window manager is running */
234 XSelectInput(dpy, root, SubstructureRedirectMask);
237 eprint("dwm: another window manager is already running\n");
240 XSetErrorHandler(NULL);
241 xerrorxlib = XSetErrorHandler(xerror);
247 /* main event loop, also reads status text from stdin */
254 FD_SET(STDIN_FILENO, &rd);
256 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
259 eprint("select failed\n");
261 if(FD_ISSET(STDIN_FILENO, &rd)) {
262 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
264 strncpy(stext, strerror(errno), sizeof stext - 1);
265 stext[sizeof stext - 1] = '\0';
269 strncpy(stext, "EOF", 4);
273 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
274 for(; p >= stext && *p != '\n'; --p);
276 strncpy(stext, p + 1, sizeof stext);
280 if(FD_ISSET(xfd, &rd))