2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/cursorfont.h>
11 #include <X11/Xatom.h>
12 #include <X11/Xproto.h>
16 /********** CUSTOMIZE **********/
19 [Tscratch] = "scratch",
26 /********** CUSTOMIZE **********/
31 Atom wm_atom[WMLast], net_atom[NetLast];
32 Cursor cursor[CurLast];
36 int tsel = Tdev; /* default tag */
37 int screen, sx, sy, sw, sh, mw, th;
40 Client *clients = NULL;
43 static Bool other_wm_running;
44 static const char version[] =
45 "dwm - " VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
46 static int (*x_error_handler) (Display *, XErrorEvent *);
49 usage() { error("usage: dwm [-v]\n"); }
59 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
60 for(i = 0; i < num; i++) {
61 if(!XGetWindowAttributes(dpy, wins[i], &wa))
63 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
65 if(wa.map_state == IsViewable)
74 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
78 unsigned long res, extra;
81 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
84 if(status != Success || *prop == 0) {
96 unsigned char *protocols;
101 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
105 for(i = 0; i < res; i++) {
106 if(protocols[i] == wm_atom[WMDelete])
107 protos |= WM_PROTOCOL_DELWIN;
109 free((char *) protocols);
114 send_message(Window w, Atom a, long value)
118 e.type = ClientMessage;
119 e.xclient.window = w;
120 e.xclient.message_type = a;
121 e.xclient.format = 32;
122 e.xclient.data.l[0] = value;
123 e.xclient.data.l[1] = CurrentTime;
124 XSendEvent(dpy, w, False, NoEventMask, &e);
129 * There's no way to check accesses to destroyed windows, thus
130 * those cases are ignored (especially on UnmapNotify's).
131 * Other types of errors call Xlib's default error handler, which
135 error_handler(Display *dpy, XErrorEvent *error)
137 if(error->error_code == BadWindow
138 || (error->request_code == X_SetInputFocus
139 && error->error_code == BadMatch)
140 || (error->request_code == X_PolyText8
141 && error->error_code == BadDrawable)
142 || (error->request_code == X_PolyFillRectangle
143 && error->error_code == BadDrawable)
144 || (error->request_code == X_PolySegment
145 && error->error_code == BadDrawable)
146 || (error->request_code == X_ConfigureWindow
147 && error->error_code == BadMatch)
148 || (error->request_code == X_GrabKey
149 && error->error_code == BadAccess))
151 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
152 error->request_code, error->error_code);
153 return x_error_handler(dpy, error); /* may call exit() */
157 * Startup Error handler to check if another window manager
158 * is already running.
161 startup_error_handler(Display *dpy, XErrorEvent *error)
163 other_wm_running = True;
174 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
184 main(int argc, char *argv[])
187 XSetWindowAttributes wa;
192 /* command line args */
193 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
194 switch (argv[i][1]) {
196 fprintf(stdout, "%s", version);
205 dpy = XOpenDisplay(0);
207 error("dwm: cannot connect X server\n");
209 screen = DefaultScreen(dpy);
210 root = RootWindow(dpy, screen);
212 /* check if another WM is already running */
213 other_wm_running = False;
214 XSetErrorHandler(startup_error_handler);
215 /* this causes an error if some other WM is running */
216 XSelectInput(dpy, root, SubstructureRedirectMask);
220 error("dwm: another window manager is already running\n");
223 sw = DisplayWidth(dpy, screen);
224 sh = DisplayHeight(dpy, screen);
225 mw = (sw * MASTERW) / 100;
226 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
229 x_error_handler = XSetErrorHandler(error_handler);
232 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
233 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
234 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
235 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
237 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
238 PropModeReplace, (unsigned char *) net_atom, NetLast);
242 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
243 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
244 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
249 dc.bg = initcolor(BGCOLOR);
250 dc.fg = initcolor(FGCOLOR);
251 dc.border = initcolor(BORDERCOLOR);
254 th = dc.font.height + 4;
256 dc.drawable = XCreatePixmap(dpy, root, sw, th, DefaultDepth(dpy, screen));
257 dc.gc = XCreateGC(dpy, root, 0, 0);
259 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
261 wa.cursor = cursor[CurNormal];
262 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
267 XNextEvent(dpy, &ev);
269 (handler[ev.type])(&ev); /* call handler */