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 screen, sx, sy, sw, sh, wax, way, waw, wah;
22 unsigned int bh, 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);
65 textnw(const char *text, unsigned int len) {
69 XmbTextExtents(dc.font.set, text, len, NULL, &r);
72 return XTextWidth(dc.font.xfont, text, len);
76 drawtext(const char *text, unsigned long col[ColLast], Bool filledsquare, Bool emptysquare) {
79 unsigned int len, olen;
81 XRectangle r = { dc.x, dc.y, dc.w, dc.h };
83 XSetForeground(dpy, dc.gc, col[ColBG]);
84 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
88 olen = len = strlen(text);
91 memcpy(buf, text, len);
93 h = dc.font.ascent + dc.font.descent;
94 y = dc.y + (dc.h / 2) - (h / 2) + dc.font.ascent;
96 /* shorten text if necessary */
97 while(len && (w = textnw(buf, len)) > dc.w - h)
108 return; /* too long */
109 gcv.foreground = col[ColFG];
111 XChangeGC(dpy, dc.gc, GCForeground, &gcv);
112 XmbDrawString(dpy, dc.drawable, dc.font.set, dc.gc, x, y, buf, len);
115 gcv.font = dc.font.xfont->fid;
116 XChangeGC(dpy, dc.gc, GCForeground | GCFont, &gcv);
117 XDrawString(dpy, dc.drawable, dc.gc, x, y, buf, len);
123 r.width = r.height = x + 1;
124 XFillRectangles(dpy, dc.drawable, dc.gc, &r, 1);
126 else if(emptysquare) {
127 r.width = r.height = x;
128 XDrawRectangles(dpy, dc.drawable, dc.gc, &r, 1);
133 getcolor(const char *colstr) {
134 Colormap cmap = DefaultColormap(dpy, screen);
137 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
138 eprint("error, cannot allocate color '%s'\n", colstr);
143 isoccupied(unsigned int t) {
146 for(c = clients; c; c = c->next)
155 Window *wins, d1, d2;
156 XWindowAttributes wa;
159 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
160 for(i = 0; i < num; i++) {
161 if(!XGetWindowAttributes(dpy, wins[i], &wa)
162 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
164 if(wa.map_state == IsViewable)
165 manage(wins[i], &wa);
173 setfont(const char *fontstr) {
174 char *def, **missing;
179 XFreeFontSet(dpy, dc.font.set);
180 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
183 fprintf(stderr, "missing fontset: %s\n", missing[n]);
184 XFreeStringList(missing);
187 XFontSetExtents *font_extents;
188 XFontStruct **xfonts;
190 dc.font.ascent = dc.font.descent = 0;
191 font_extents = XExtentsOfFontSet(dc.font.set);
192 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
193 for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
194 if(dc.font.ascent < (*xfonts)->ascent)
195 dc.font.ascent = (*xfonts)->ascent;
196 if(dc.font.descent < (*xfonts)->descent)
197 dc.font.descent = (*xfonts)->descent;
203 XFreeFont(dpy, dc.font.xfont);
204 dc.font.xfont = NULL;
205 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr)))
206 eprint("error, cannot load font: '%s'\n", fontstr);
207 dc.font.ascent = dc.font.xfont->ascent;
208 dc.font.descent = dc.font.xfont->descent;
210 dc.font.height = dc.font.ascent + dc.font.descent;
218 XModifierKeymap *modmap;
219 XSetWindowAttributes wa;
222 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
223 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
224 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
225 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
226 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
227 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
228 PropModeReplace, (unsigned char *) netatom, NetLast);
230 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
231 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
232 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
233 /* init modifier map */
235 modmap = XGetModifierMapping(dpy);
236 for (i = 0; i < 8; i++)
237 for (j = 0; j < modmap->max_keypermod; j++) {
238 if(modmap->modifiermap[i * modmap->max_keypermod + j]
239 == XKeysymToKeycode(dpy, XK_Num_Lock))
240 numlockmask = (1 << i);
242 XFreeModifiermap(modmap);
243 /* select for events */
244 wa.event_mask = SubstructureRedirectMask | SubstructureNotifyMask
245 | EnterWindowMask | LeaveWindowMask;
246 wa.cursor = cursor[CurNormal];
247 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
250 for(ntags = 0; tags[ntags]; ntags++);
251 seltag = emallocz(sizeof(Bool) * ntags);
254 dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
255 dc.norm[ColBG] = getcolor(NORMBGCOLOR);
256 dc.norm[ColFG] = getcolor(NORMFGCOLOR);
257 dc.sel[ColBorder] = getcolor(SELBORDERCOLOR);
258 dc.sel[ColBG] = getcolor(SELBGCOLOR);
259 dc.sel[ColFG] = getcolor(SELFGCOLOR);
263 sw = DisplayWidth(dpy, screen);
264 sh = DisplayHeight(dpy, screen);
267 dc.h = bh = dc.font.height + 2;
268 wa.override_redirect = 1;
269 wa.background_pixmap = ParentRelative;
270 wa.event_mask = ButtonPressMask | ExposureMask;
271 barwin = XCreateWindow(dpy, root, sx, sy + (TOPBAR ? 0 : sh - bh), sw, bh, 0,
272 DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen),
273 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
274 XDefineCursor(dpy, barwin, cursor[CurNormal]);
275 XMapRaised(dpy, barwin);
276 strcpy(stext, "dwm-"VERSION);
279 way = sy + (TOPBAR ? bh : 0);
282 /* pixmap for everything */
283 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
284 dc.gc = XCreateGC(dpy, root, 0, 0);
285 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
286 /* multihead support */
287 selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
291 * Startup Error handler to check if another window manager
292 * is already running.
295 xerrorstart(Display *dsply, XErrorEvent *ee) {
307 for(i = 0; i < ntags; i++) {
308 dc.w = textw(tags[i]);
310 drawtext(tags[i], dc.sel, sel && sel->tags[i], isoccupied(i));
312 drawtext(tags[i], dc.norm, sel && sel->tags[i], isoccupied(i));
316 drawtext(lt->symbol, dc.norm, False, False);
324 drawtext(stext, dc.norm, False, False);
325 if((dc.w = dc.x - x) > bh) {
327 drawtext(sel ? sel->name : NULL, sel ? dc.sel : dc.norm, False, False);
329 XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, sw, bh, 0, 0);
334 sendevent(Window w, Atom a, long value) {
337 e.type = ClientMessage;
338 e.xclient.window = w;
339 e.xclient.message_type = a;
340 e.xclient.format = 32;
341 e.xclient.data.l[0] = value;
342 e.xclient.data.l[1] = CurrentTime;
343 XSendEvent(dpy, w, False, NoEventMask, &e);
348 textw(const char *text) {
349 return textnw(text, strlen(text)) + dc.font.height;
354 readin = running = False;
357 /* There's no way to check accesses to destroyed windows, thus those cases are
358 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
359 * default error handler, which may call exit.
362 xerror(Display *dpy, XErrorEvent *ee) {
363 if(ee->error_code == BadWindow
364 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
365 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
366 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
367 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
368 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
369 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
370 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
372 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
373 ee->request_code, ee->error_code);
374 return xerrorxlib(dpy, ee); /* may call exit */
378 main(int argc, char *argv[]) {
384 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
385 fputs("dwm-"VERSION", (C)opyright MMVI-MMVII Anselm R. Garbe\n", stdout);
389 eprint("usage: dwm [-v]\n");
390 setlocale(LC_CTYPE, "");
391 dpy = XOpenDisplay(0);
393 eprint("dwm: cannot open display\n");
394 xfd = ConnectionNumber(dpy);
395 screen = DefaultScreen(dpy);
396 root = RootWindow(dpy, screen);
398 XSetErrorHandler(xerrorstart);
399 /* this causes an error if some other window manager is running */
400 XSelectInput(dpy, root, SubstructureRedirectMask);
403 eprint("dwm: another window manager is already running\n");
406 XSetErrorHandler(NULL);
407 xerrorxlib = XSetErrorHandler(xerror);
413 /* main event loop, also reads status text from stdin */
419 FD_SET(STDIN_FILENO, &rd);
421 if(select(xfd + 1, &rd, NULL, NULL, NULL) == -1) {
424 eprint("select failed\n");
426 if(FD_ISSET(STDIN_FILENO, &rd)) {
427 switch(r = read(STDIN_FILENO, stext, sizeof stext - 1)) {
429 strncpy(stext, strerror(errno), sizeof stext - 1);
430 stext[sizeof stext - 1] = '\0';
434 strncpy(stext, "EOF", 4);
438 for(stext[r] = '\0', p = stext + strlen(stext) - 1; p >= stext && *p == '\n'; *p-- = '\0');
439 for(; p >= stext && *p != '\n'; --p);
441 strncpy(stext, p + 1, sizeof stext);
445 if(FD_ISSET(xfd, &rd))
446 while(XPending(dpy)) {
447 XNextEvent(dpy, &ev);
449 (handler[ev.type])(&ev); /* call handler */