2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
12 #include <sys/select.h>
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
19 static int (*xerrorxlib)(Display *, XErrorEvent *);
27 resize(sel, True, TopLeft);
30 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
40 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
41 for(i = 0; i < num; i++) {
42 if(!XGetWindowAttributes(dpy, wins[i], &wa))
44 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
46 if(wa.map_state == IsViewable)
55 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
58 unsigned long res, extra;
61 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
64 if(status != Success || *prop == 0) {
74 * Startup Error handler to check if another window manager
78 xerrorstart(Display *dsply, XErrorEvent *ee)
88 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
90 Atom wmatom[WMLast], netatom[NetLast];
93 Client *clients = NULL;
95 Cursor cursor[CurLast];
108 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L,
109 ((unsigned char **)&protocols));
113 for(i = 0; i < res; i++) {
114 if(protocols[i] == wmatom[WMDelete])
115 protos |= PROTODELWIN;
117 free((char *) protocols);
122 sendevent(Window w, Atom a, long value)
126 e.type = ClientMessage;
127 e.xclient.window = w;
128 e.xclient.message_type = a;
129 e.xclient.format = 32;
130 e.xclient.data.l[0] = value;
131 e.xclient.data.l[1] = CurrentTime;
132 XSendEvent(dpy, w, False, NoEventMask, &e);
143 * There's no way to check accesses to destroyed windows, thus those cases are
144 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
145 * default error handler, which calls exit().
148 xerror(Display *dpy, XErrorEvent *ee)
150 if(ee->error_code == BadWindow
151 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
152 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
153 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
154 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
155 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
156 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
158 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
159 ee->request_code, ee->error_code);
160 return xerrorxlib(dpy, ee); /* may call exit() */
164 main(int argc, char *argv[])
172 XSetWindowAttributes wa;
174 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
175 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
179 eprint("usage: dwm [-v]\n");
181 dpy = XOpenDisplay(0);
183 eprint("dwm: cannot open display\n");
185 xfd = ConnectionNumber(dpy);
186 screen = DefaultScreen(dpy);
187 root = RootWindow(dpy, screen);
190 XSetErrorHandler(xerrorstart);
191 /* this causes an error if some other window manager is running */
192 XSelectInput(dpy, root, SubstructureRedirectMask);
196 eprint("dwm: another window manager is already running\n");
198 XSetErrorHandler(NULL);
199 xerrorxlib = XSetErrorHandler(xerror);
202 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
203 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
204 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
205 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
206 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
207 PropModeReplace, (unsigned char *) netatom, NetLast);
210 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
211 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
212 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
217 for(ntags = 0; tags[ntags]; ntags++);
218 seltag = emallocz(sizeof(Bool) * ntags);
219 seltag[DEFTAG] = True;
222 dc.bg = getcolor(BGCOLOR);
223 dc.fg = getcolor(FGCOLOR);
224 dc.border = getcolor(BORDERCOLOR);
228 sw = DisplayWidth(dpy, screen);
229 sh = DisplayHeight(dpy, screen);
230 mw = (sw * MASTERW) / 100;
232 wa.override_redirect = 1;
233 wa.background_pixmap = ParentRelative;
234 wa.event_mask = ButtonPressMask | ExposureMask;
238 dc.h = bh = dc.font.height + 4;
239 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
240 CopyFromParent, DefaultVisual(dpy, screen),
241 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
242 XDefineCursor(dpy, barwin, cursor[CurNormal]);
243 XMapRaised(dpy, barwin);
245 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
246 dc.gc = XCreateGC(dpy, root, 0, 0);
248 strcpy(stext, "dwm-"VERSION);
251 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
253 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
254 wa.cursor = cursor[CurNormal];
255 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
259 /* main event loop, also reads status text from stdin */
264 FD_SET(STDIN_FILENO, &rd);
267 i = select(xfd + 1, &rd, NULL, NULL, NULL);
268 if(i == -1 && errno == EINTR)
271 eprint("select failed\n");
273 if(FD_ISSET(xfd, &rd)) {
274 while(XPending(dpy)) {
275 XNextEvent(dpy, &ev);
277 (handler[ev.type])(&ev); /* call handler */
280 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
281 readin = NULL != fgets(stext, sizeof(stext), stdin);
283 stext[strlen(stext) - 1] = 0;
285 strcpy(stext, "broken pipe");