1 /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
2 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
3 * © 2007 Premysl Hruby <dfenze at gmail dot com>
4 * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
5 * See LICENSE file for license details. */
13 #include <sys/select.h>
14 #include <X11/cursorfont.h>
15 #include <X11/keysym.h>
16 #include <X11/Xatom.h>
17 #include <X11/Xproto.h>
22 int screen, sx, sy, sw, sh, wax, way, waw, wah;
23 unsigned int bh, bpos, ntags, numlockmask;
24 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;
39 static Bool running = True;
46 XMoveWindow(dpy, stack->win, stack->x, stack->y);
50 XFreeFontSet(dpy, dc.font.set);
52 XFreeFont(dpy, dc.font.xfont);
53 XUngrabKey(dpy, AnyKey, AnyModifier, root);
54 XFreePixmap(dpy, dc.drawable);
56 XDestroyWindow(dpy, barwin);
57 XFreeCursor(dpy, cursor[CurNormal]);
58 XFreeCursor(dpy, cursor[CurResize]);
59 XFreeCursor(dpy, cursor[CurMove]);
60 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
66 initcolor(const char *colstr) {
67 Colormap cmap = DefaultColormap(dpy, screen);
70 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
71 eprint("error, cannot allocate color '%s'\n", colstr);
76 initfont(const char *fontstr) {
82 XFreeFontSet(dpy, dc.font.set);
83 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
86 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
87 XFreeStringList(missing);
90 XFontSetExtents *font_extents;
93 dc.font.ascent = dc.font.descent = 0;
94 font_extents = XExtentsOfFontSet(dc.font.set);
95 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
96 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
97 if(dc.font.ascent < (*xfonts)->ascent)
98 dc.font.ascent = (*xfonts)->ascent;
99 if(dc.font.descent < (*xfonts)->descent)
100 dc.font.descent = (*xfonts)->descent;
106 XFreeFont(dpy, dc.font.xfont);
107 dc.font.xfont = NULL;
108 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
109 eprint("error, cannot load font: '%s'\n", fontstr);
110 dc.font.ascent = dc.font.xfont->ascent;
111 dc.font.descent = dc.font.xfont->descent;
113 dc.font.height = dc.font.ascent + dc.font.descent;
119 Window *wins, d1, d2;
120 XWindowAttributes wa;
123 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
124 for(i = 0; i < num; i++) {
125 if(!XGetWindowAttributes(dpy, wins[i], &wa)
126 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
128 if(wa.map_state == IsViewable)
129 manage(wins[i], &wa);
141 XModifierKeymap *modmap;
142 XSetWindowAttributes wa;
145 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
146 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
147 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
148 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
149 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
150 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
151 PropModeReplace, (unsigned char *) netatom, NetLast);
153 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
154 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
155 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
156 /* init modifier map */
158 modmap = XGetModifierMapping(dpy);
159 for (i = 0; i < 8; i++)
160 for (j = 0; j < modmap->max_keypermod; j++) {
161 if(modmap->modifiermap[i * modmap->max_keypermod + j]
162 == XKeysymToKeycode(dpy, XK_Num_Lock))
163 numlockmask = (1 << i);
165 XFreeModifiermap(modmap);
166 /* select for events */
167 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
168 | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
169 wa.cursor = cursor[CurNormal];
170 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
171 XSelectInput(dpy, root, wa.event_mask);
174 for(ntags = 0; tags[ntags]; ntags++);
175 seltag = emallocz(sizeof(Bool) * ntags);
178 dc.norm[ColBorder] = initcolor(NORMBORDERCOLOR);
179 dc.norm[ColBG] = initcolor(NORMBGCOLOR);
180 dc.norm[ColFG] = initcolor(NORMFGCOLOR);
181 dc.sel[ColBorder] = initcolor(SELBORDERCOLOR);
182 dc.sel[ColBG] = initcolor(SELBGCOLOR);
183 dc.sel[ColFG] = initcolor(SELFGCOLOR);
187 sw = DisplayWidth(dpy, screen);
188 sh = DisplayHeight(dpy, screen);
191 dc.h = bh = dc.font.height + 2;
192 wa.override_redirect = 1;
193 wa.background_pixmap = ParentRelative;
194 wa.event_mask = ButtonPressMask | ExposureMask;
195 barwin = XCreateWindow(dpy, root, sx, sy, sw, bh, 0,
196 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
197 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
198 XDefineCursor(dpy, barwin, cursor[CurNormal]);
201 XMapRaised(dpy, barwin);
202 strcpy(stext, "dwm-"VERSION);
203 /* pixmap for everything */
204 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
205 dc.gc = XCreateGC(dpy, root, 0, 0);
206 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
208 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
209 /* multihead support */
210 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
214 * Startup Error handler to check if another window manager
215 * is already running.
218 xerrorstart(Display *dsply, XErrorEvent *ee) {
226 quit(const char *arg) {
227 readin = running = False;
242 XMoveWindow(dpy, barwin, sx, sy);
246 XMoveWindow(dpy, barwin, sx, sy + wah);
249 XMoveWindow(dpy, barwin, sx, sy - bh);
253 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
256 /* There's no way to check accesses to destroyed windows, thus those cases are
257 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
258 * default error handler, which may call exit.
261 xerror(Display *dpy, XErrorEvent *ee) {
262 if(ee->error_code == BadWindow
263 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
264 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
265 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
266 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
267 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
268 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
269 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
271 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
272 ee->request_code, ee->error_code);
273 return xerrorxlib(dpy, ee); /* may call exit */
277 main(int argc, char *argv[]) {
283 if(argc == 2 && !strcmp("-v", argv[1]))
284 eprint("dwm-"VERSION", © 2004-2007 Anselm R. Garbe, Sander van Dijk, Premysl Hruby, Szabolcs Nagy\n");
286 eprint("usage: dwm [-v]\n");
287 setlocale(LC_CTYPE, "");
288 if(!(dpy = XOpenDisplay(0)))
289 eprint("dwm: cannot open display\n");
290 xfd = ConnectionNumber(dpy);
291 screen = DefaultScreen(dpy);
292 root = RootWindow(dpy, screen);
294 XSetErrorHandler(xerrorstart);
295 /* this causes an error if some other window manager is running */
296 XSelectInput(dpy, root, SubstructureRedirectMask);
299 eprint("dwm: another window manager is already running\n");
302 XSetErrorHandler(NULL);
303 xerrorxlib = XSetErrorHandler(xerror);
309 /* main event loop, also reads status text from stdin */
315 FD_SET(STDIN_FILENO, &rd);
317 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
320 eprint("select failed\n");
322 if(FD_ISSET(STDIN_FILENO, &rd)) {
323 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
325 strncpy(stext, strerror(errno), sizeof stext - 1);
326 stext[sizeof stext - 1] = '\0';
330 strncpy(stext, "EOF", 4);
334 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
335 for(; p >= stext && *p != '\n'; --p);
337 strncpy(stext, p + 1, sizeof stext);
341 while(XPending(dpy)) {
342 XNextEvent(dpy, &ev);
344 (handler[ev.type])(&ev); /* call handler */