2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
11 #include <X11/cursorfont.h>
12 #include <X11/Xatom.h>
13 #include <X11/Xproto.h>
17 /********** CUSTOMIZE **********/
20 [Tscratch] = "scratch",
27 /********** CUSTOMIZE **********/
32 Atom wm_atom[WMLast], net_atom[NetLast];
33 Cursor cursor[CurLast];
37 int tsel = Tdev; /* default tag */
38 int screen, sx, sy, sw, sh, bx, by, bw, bh, mw;
42 Client *clients = NULL;
45 static Bool other_wm_running;
46 static const char version[] =
47 "dwm-" VERSION ", (C)opyright MMVI Anselm R. Garbe\n";
48 static int (*x_error_handler) (Display *, XErrorEvent *);
51 usage() { error("usage: dwm [-v]\n"); }
61 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
62 for(i = 0; i < num; i++) {
63 if(!XGetWindowAttributes(dpy, wins[i], &wa))
65 if(wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
67 if(wa.map_state == IsViewable)
76 win_property(Window w, Atom a, Atom t, long l, unsigned char **prop)
80 unsigned long res, extra;
83 status = XGetWindowProperty(dpy, w, a, 0L, l, False, t, &real, &format,
86 if(status != Success || *prop == 0) {
98 unsigned char *protocols;
103 res = win_property(w, wm_atom[WMProtocols], XA_ATOM, 20L, &protocols);
107 for(i = 0; i < res; i++) {
108 if(protocols[i] == wm_atom[WMDelete])
109 protos |= WM_PROTOCOL_DELWIN;
111 free((char *) protocols);
116 send_message(Window w, Atom a, long value)
120 e.type = ClientMessage;
121 e.xclient.window = w;
122 e.xclient.message_type = a;
123 e.xclient.format = 32;
124 e.xclient.data.l[0] = value;
125 e.xclient.data.l[1] = CurrentTime;
126 XSendEvent(dpy, w, False, NoEventMask, &e);
131 * There's no way to check accesses to destroyed windows, thus
132 * those cases are ignored (especially on UnmapNotify's).
133 * Other types of errors call Xlib's default error handler, which
137 error_handler(Display *dpy, XErrorEvent *error)
139 if(error->error_code == BadWindow
140 || (error->request_code == X_SetInputFocus
141 && error->error_code == BadMatch)
142 || (error->request_code == X_PolyText8
143 && error->error_code == BadDrawable)
144 || (error->request_code == X_PolyFillRectangle
145 && error->error_code == BadDrawable)
146 || (error->request_code == X_PolySegment
147 && error->error_code == BadDrawable)
148 || (error->request_code == X_ConfigureWindow
149 && error->error_code == BadMatch)
150 || (error->request_code == X_GrabKey
151 && error->error_code == BadAccess))
153 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
154 error->request_code, error->error_code);
155 return x_error_handler(dpy, error); /* may call exit() */
159 * Startup Error handler to check if another window manager
160 * is already running.
163 startup_error_handler(Display *dpy, XErrorEvent *error)
165 other_wm_running = True;
176 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
186 main(int argc, char *argv[])
189 XSetWindowAttributes wa;
194 /* command line args */
195 for(i = 1; (i < argc) && (argv[i][0] == '-'); i++) {
196 switch (argv[i][1]) {
198 fprintf(stdout, "%s", version);
207 dpy = XOpenDisplay(0);
209 error("dwm: cannot connect X server\n");
211 screen = DefaultScreen(dpy);
212 root = RootWindow(dpy, screen);
214 /* check if another WM is already running */
215 other_wm_running = False;
216 XSetErrorHandler(startup_error_handler);
217 /* this causes an error if some other WM is running */
218 XSelectInput(dpy, root, SubstructureRedirectMask);
222 error("dwm: another window manager is already running\n");
225 x_error_handler = XSetErrorHandler(error_handler);
228 wm_atom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
229 wm_atom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
230 net_atom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
231 net_atom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
232 XChangeProperty(dpy, root, net_atom[NetSupported], XA_ATOM, 32,
233 PropModeReplace, (unsigned char *) net_atom, NetLast);
236 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
237 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
238 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
243 dc.bg = initcolor(BGCOLOR);
244 dc.fg = initcolor(FGCOLOR);
245 dc.border = initcolor(BORDERCOLOR);
249 sw = DisplayWidth(dpy, screen);
250 sh = DisplayHeight(dpy, screen);
251 mw = (sw * MASTERW) / 100;
253 wa.override_redirect = 1;
254 wa.background_pixmap = ParentRelative;
255 wa.event_mask = ExposureMask;
259 dc.h = bh = dc.font.height + 4;
260 barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
261 CopyFromParent, DefaultVisual(dpy, screen),
262 CWOverrideRedirect | CWBackPixmap | CWEventMask, &wa);
263 XDefineCursor(dpy, barwin, cursor[CurNormal]);
264 XMapRaised(dpy, barwin);
266 issel = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
268 wa.event_mask = SubstructureRedirectMask | EnterWindowMask \
270 wa.cursor = cursor[CurNormal];
272 XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
274 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
275 dc.gc = XCreateGC(dpy, root, 0, 0);
277 strcpy(stext, "dwm-"VERSION);
282 XNextEvent(dpy, &ev);
284 (handler[ev.type])(&ev); /* call handler */