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 master, nmaster, 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;
48 XFreeFontSet(dpy, dc.font.set);
50 XFreeFont(dpy, dc.font.xfont);
51 XUngrabKey(dpy, AnyKey, AnyModifier, root);
52 XFreePixmap(dpy, dc.drawable);
54 XDestroyWindow(dpy, barwin);
55 XFreeCursor(dpy, cursor[CurNormal]);
56 XFreeCursor(dpy, cursor[CurResize]);
57 XFreeCursor(dpy, cursor[CurMove]);
58 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
70 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
71 for(i = 0; i < num; i++) {
72 if(!XGetWindowAttributes(dpy, wins[i], &wa))
74 if(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] == XKeysymToKeycode(dpy, XK_Num_Lock))
110 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);
138 bmw = textw(TILESYMBOL) > textw(FLOATSYMBOL) ? textw(TILESYMBOL) : textw(FLOATSYMBOL);
140 dc.h = bh = dc.font.height + 2;
141 wa.override_redirect = 1;
142 wa.background_pixmap = ParentRelative;
143 wa.event_mask = ButtonPressMask | ExposureMask;
144 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
145 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
146 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
147 XDefineCursor(dpy, barwin, cursor[CurNormal]);
148 XMapRaised(dpy, barwin);
149 strcpy(stext, "dwm-"VERSION);
152 way = sy + (TOPBAR ? bh : 0);
155 /* pixmap for everything */
156 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
157 dc.gc = XCreateGC(dpy, root, 0, 0);
158 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
159 /* multihead support */
160 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
164 * Startup Error handler to check if another window manager
165 * is already running.
168 xerrorstart(Display *dsply, XErrorEvent *ee) {
176 sendevent(Window w, Atom a, long value) {
179 e.type = ClientMessage;
180 e.xclient.window = w;
181 e.xclient.message_type = a;
182 e.xclient.format = 32;
183 e.xclient.data.l[0] = value;
184 e.xclient.data.l[1] = CurrentTime;
185 XSendEvent(dpy, w, False, NoEventMask, &e);
191 readin = running = False;
194 /* There's no way to check accesses to destroyed windows, thus those cases are
195 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
196 * default error handler, which may call exit.
199 xerror(Display *dpy, XErrorEvent *ee) {
200 if(ee->error_code == BadWindow
201 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
202 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
203 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
204 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
205 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
206 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
207 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
209 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
210 ee->request_code, ee->error_code);
211 return xerrorxlib(dpy, ee); /* may call exit */
215 main(int argc, char *argv[]) {
220 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
221 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
225 eprint("usage: dwm [-v]\n");
226 setlocale(LC_CTYPE, "");
227 dpy = XOpenDisplay(0);
229 eprint("dwm: cannot open display\n");
230 xfd = ConnectionNumber(dpy);
231 screen = DefaultScreen(dpy);
232 root = RootWindow(dpy, screen);
234 XSetErrorHandler(xerrorstart);
235 /* this causes an error if some other window manager is running */
236 XSelectInput(dpy, root, SubstructureRedirectMask);
239 eprint("dwm: another window manager is already running\n");
242 XSetErrorHandler(NULL);
243 xerrorxlib = XSetErrorHandler(xerror);
249 /* main event loop, also reads status text from stdin */
256 FD_SET(STDIN_FILENO, &rd);
258 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
261 eprint("select failed\n");
263 if(FD_ISSET(STDIN_FILENO, &rd)) {
264 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
266 strncpy(stext, strerror(errno), sizeof stext - 1);
267 stext[sizeof stext - 1] = '\0';
271 strncpy(stext, "EOF", 4);
275 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
276 for(; p >= stext && *p != '\n'; --p);
278 strncpy(stext, p + 1, sizeof stext);
282 if(FD_ISSET(xfd, &rd))