2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
13 #include <sys/select.h>
14 #include <X11/cursorfont.h>
15 #include <X11/Xatom.h>
16 #include <X11/Xproto.h>
21 static int (*xerrorxlib)(Display *, XErrorEvent *);
28 resize(sel, True, TopLeft);
31 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
41 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
42 for(i = 0; i < num; i++) {
43 if(!XGetWindowAttributes(dpy, wins[i], &wa))
45 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
47 if(wa.map_state == IsViewable)
56 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
59 unsigned long res, extra;
62 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
65 if(status != Success || *prop == 0) {
75 * Startup Error handler to check if another window manager
79 xerrorstart(Display *dsply, XErrorEvent *ee)
89 int tsel = Tdev; /* default tag */
91 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
92 Atom wmatom[WMLast], netatom[NetLast];
95 Client *clients = NULL;
97 Cursor cursor[CurLast];
108 unsigned char *protocols;
110 res = win_property(w, wmatom[WMProtocols], XA_ATOM, 20L, &protocols);
114 for(i = 0; i < res; i++) {
115 if(protocols[i] == wmatom[WMDelete])
116 protos |= WM_PROTOCOL_DELWIN;
118 free((char *) protocols);
123 sendevent(Window w, Atom a, long value)
127 e.type = ClientMessage;
128 e.xclient.window = w;
129 e.xclient.message_type = a;
130 e.xclient.format = 32;
131 e.xclient.data.l[0] = value;
132 e.xclient.data.l[1] = CurrentTime;
133 XSendEvent(dpy, w, False, NoEventMask, &e);
144 * There's no way to check accesses to destroyed windows, thus those cases are
145 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
146 * default error handler, which calls exit().
149 xerror(Display *dpy, XErrorEvent *ee)
151 if(ee->error_code == BadWindow
152 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
153 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
154 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
155 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
156 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
157 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess))
159 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
160 ee->request_code, ee->error_code);
161 return xerrorxlib(dpy, ee); /* may call exit() */
165 main(int argc, char *argv[])
173 XSetWindowAttributes wa;
175 if(argc == 2 && !strncmp("-v", argv[1], 3)) {
176 fputs("dwm-"VERSION", (C)opyright MMVI Anselm R. Garbe\n", stdout);
180 eprint("usage: dwm [-v]\n");
182 dpy = XOpenDisplay(0);
184 eprint("dwm: cannot connect X server\n");
186 screen = DefaultScreen(dpy);
187 root = RootWindow(dpy, screen);
189 /* check if another WM is already running */
191 XSetErrorHandler(xerrorstart);
192 /* this causes an error if some other WM is running */
193 XSelectInput(dpy, root, SubstructureRedirectMask);
197 eprint("dwm: another window manager is already running\n");
199 XSetErrorHandler(NULL);
200 xerrorxlib = XSetErrorHandler(xerror);
203 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
204 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
205 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
206 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
207 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
208 PropModeReplace, (unsigned char *) netatom, NetLast);
211 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
212 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
213 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
218 dc.bg = getcolor(BGCOLOR);
219 dc.fg = getcolor(FGCOLOR);
220 dc.border = getcolor(BORDERCOLOR);
224 sw = DisplayWidth(dpy, screen);
225 sh = DisplayHeight(dpy, screen);
226 mw = (sw * MASTERW) / 100;
228 wa.override_redirect = 1;
229 wa.background_pixmap = ParentRelative;
230 wa.event_mask = ButtonPressMask | ExposureMask;
234 dc.h = bh = dc.font.height + 4;
235 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
236 CopyFromParent, DefaultVisual(dpy, screen),
237 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
238 XDefineCursor(dpy, barwin, cursor[CurNormal]);
239 XMapRaised(dpy, barwin);
241 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
242 dc.gc = XCreateGC(dpy, root, 0, 0);
245 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
247 wa.event_mask = SubstructureRedirectMask | EnterWindowMask | LeaveWindowMask;
248 wa.cursor = cursor[CurNormal];
249 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
251 strcpy(stext, "dwm-"VERSION);
254 /* main event loop, reads status text from stdin as well */
258 FD_SET(STDIN_FILENO, &rd);
259 FD_SET(ConnectionNumber(dpy), &rd);
261 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
262 if(i == -1 && errno == EINTR)
265 eprint("select failed\n");
267 if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
268 while(XPending(dpy)) {
269 XNextEvent(dpy, &ev);
271 (handler[ev.type])(&ev); /* call handler */
274 if(readin && FD_ISSET(STDIN_FILENO, &rd)) {
275 readin = NULL != fgets(stext, sizeof(stext), stdin);
277 stext[strlen(stext) - 1] = 0;
279 strcpy(stext, "broken pipe");