2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
13 #include <X11/cursorfont.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xproto.h>
19 Atom wm_atom[WMLast], net_atom[NetLast];
20 Cursor cursor[CurLast];
24 int tsel = Tdev; /* default tag */
25 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
29 Client *clients = NULL;
33 static const char version[] =
34 "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
35 static int (*xerrorxlib)(Display *, XErrorEvent *);
37 /* static functions */
46 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
57 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
58 for(i = 0; i < num; i++) {
59 if(!XGetWindowAttributes(dpy, wins[i], &wa))
61 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
63 if(wa.map_state == IsViewable)
72 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
76 unsigned long res, extra;
79 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
82 if(status != Success || *prop == 0) {
92 * Startup Error handler to check if another window manager
96 xerrorstart(Display *dsply, XErrorEvent *ee)
102 /* extern functions */
107 unsigned char *protocols;
112 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
116 for(i = 0; i < res; i++) {
117 if(protocols[i] == wm_atom[WMDelete])
118 protos |= WM_PROTOCOL_DELWIN;
120 free((char *) protocols);
125 sendevent(Window w, Atom a, long value)
129 e.type = ClientMessage;
130 e.xclient.window = w;
131 e.xclient.message_type = a;
132 e.xclient.format = 32;
133 e.xclient.data.l[0] = value;
134 e.xclient.data.l[1] = CurrentTime;
135 XSendEvent(dpy, w, False, NoEventMask, &e);
146 * There's no way to check accesses to destroyed windows, thus
147 * those cases are ignored (especially on UnmapNotify's).
148 * Other types of errors call Xlib's default error handler, which
152 xerror(Display *dpy, XErrorEvent *ee)
154 if(ee->error_code == BadWindow
155 || (ee->request_code == X_SetInputFocus
156 && ee->error_code == BadMatch)
157 || (ee->request_code == X_PolyText8
158 && ee->error_code == BadDrawable)
159 || (ee->request_code == X_PolyFillRectangle
160 && ee->error_code == BadDrawable)
161 || (ee->request_code == X_PolySegment
162 && ee->error_code == BadDrawable)
163 || (ee->request_code == X_ConfigureWindow
164 && ee->error_code == BadMatch)
165 || (ee->request_code == X_GrabKey
166 && ee->error_code == BadAccess))
168 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
169 ee->request_code, ee->error_code);
170 return xerrorxlib(dpy, ee); /* may call exit() */
174 main(int argc, char *argv[])
178 XSetWindowAttributes wa;
180 Bool readstdin = True;
184 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
185 switch (argv[i][1]) {
187 fprintf(stdout, "%s", version);
191 eprint("usage: dwm [-v]\n");
196 dpy = XOpenDisplay(0);
198 eprint("dwm: cannot connect X server\n");
200 screen = DefaultScreen(dpy);
201 root = RootWindow(dpy, screen);
203 /* check if another WM is already running */
205 XSetErrorHandler(xerrorstart);
206 /* this causes an error if some other WM is running */
207 XSelectInput(dpy, root, SubstructureRedirectMask);
211 eprint("dwm: another window manager is already running\n");
214 xerrorxlib = XSetErrorHandler(xerror);
217 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
218 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
219 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
220 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
221 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
222 PropModeReplace, (unsigned char *) net_atom, NetLast);
225 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
226 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
227 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
232 dc.bg = getcolor(BGCOLOR);
233 dc.fg = getcolor(FGCOLOR);
234 dc.border = getcolor(BORDERCOLOR);
238 sw = DisplayWidth(dpy, screen);
239 sh = DisplayHeight(dpy, screen);
240 mw = (sw * MASTERW) / 100;
242 wa.override_redirect = 1;
243 wa.background_pixmap = ParentRelative;
244 wa.event_mask = ButtonPressMask | ExposureMask;
248 dc.h = bh = dc.font.height + 4;
249 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
250 CopyFromParent, DefaultVisual(dpy, screen),
251 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
252 XDefineCursor(dpy, barwin, cursor[CurNormal]);
253 XMapRaised(dpy, barwin);
255 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
256 dc.gc = XCreateGC(dpy, root, 0, 0);
259 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
261 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
263 wa.cursor = cursor[CurNormal];
265 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
267 strcpy(stext, "dwm-"VERSION);
270 /* main event loop, reads status text from stdin as well */
275 FD_SET(STDIN_FILENO, &rd);
276 FD_SET(ConnectionNumber(dpy), &rd);
278 i = select(ConnectionNumber(dpy) + 1, &rd, 0, 0, 0);
279 if(i == -1 && errno == EINTR)
282 eprint("select failed\n");
284 if(FD_ISSET(ConnectionNumber(dpy), &rd)) {
285 while(XPending(dpy)) {
286 XNextEvent(dpy, &ev);
288 (handler[ev.type])(&ev); /* call handler */
291 if(readstdin && FD_ISSET(STDIN_FILENO, &rd)) {
294 if((i = getchar()) == EOF) {
295 /* broken pipe/end of producer */
297 strcpy(stext, "broken pipe");
300 if(i == '\n' || n >= sizeof(stext) - 1)