my changes
[azarus-dwm.git] / dwm.c
blob17fe373d8102fa1466d63f268c529f3ec318a6b5
1 /* See LICENSE file for copyright and license details.
3 * dynamic window manager is designed like any other X client as well. It is
4 * driven through handling X events. In contrast to other X clients, a window
5 * manager selects for SubstructureRedirectMask on the root window, to receive
6 * events about window (dis-)appearance. Only one X connection at a time is
7 * allowed to select for this event mask.
9 * The event handlers of dwm are organized in an array which is accessed
10 * whenever a new event has been fetched. This allows event dispatching
11 * in O(1) time.
13 * Each child of the root window is called a client, except windows which have
14 * set the override_redirect flag. Clients are organized in a linked client
15 * list on each monitor, the focus history is remembered through a stack list
16 * on each monitor. Each client contains a bit array to indicate the tags of a
17 * client.
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
23 #include <errno.h>
24 #include <locale.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
36 #include <X11/Xlib.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
39 #ifdef XINERAMA
40 #include <X11/extensions/Xinerama.h>
41 #endif /* XINERAMA */
42 #include <X11/Xft/Xft.h>
44 #include "drw.h"
45 #include "util.h"
47 /* macros */
48 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
49 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
50 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
51 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
52 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
53 #define LENGTH(X) (sizeof X / sizeof X[0])
54 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
55 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
56 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
57 #define TAGMASK ((1 << LENGTH(tags)) - 1)
58 #define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
60 #define OPAQUE 0xffU
62 /* enums */
63 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
64 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
65 enum { NetSupported, NetWMName, NetWMState,
66 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
67 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
68 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
69 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
70 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
72 typedef union {
73 int i;
74 unsigned int ui;
75 float f;
76 const void *v;
77 } Arg;
79 typedef struct {
80 unsigned int click;
81 unsigned int mask;
82 unsigned int button;
83 void (*func)(const Arg *arg);
84 const Arg arg;
85 } Button;
87 typedef struct Monitor Monitor;
88 typedef struct Client Client;
89 struct Client {
90 char name[256];
91 float mina, maxa;
92 int x, y, w, h;
93 int oldx, oldy, oldw, oldh;
94 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 int bw, oldbw;
96 unsigned int tags;
97 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
98 Client *next;
99 Client *snext;
100 Monitor *mon;
101 Window win;
104 typedef struct {
105 unsigned int mod;
106 KeySym keysym;
107 void (*func)(const Arg *);
108 const Arg arg;
109 } Key;
111 typedef struct {
112 const char *symbol;
113 void (*arrange)(Monitor *);
114 } Layout;
116 struct Monitor {
117 char ltsymbol[16];
118 float mfact;
119 int nmaster;
120 int num;
121 int by; /* bar geometry */
122 int mx, my, mw, mh; /* screen size */
123 int wx, wy, ww, wh; /* window area */
124 unsigned int seltags;
125 unsigned int sellt;
126 unsigned int tagset[2];
127 int showbar;
128 int topbar;
129 Client *clients;
130 Client *sel;
131 Client *stack;
132 Monitor *next;
133 Window barwin;
134 const Layout *lt[2];
137 typedef struct {
138 const char *class;
139 const char *instance;
140 const char *title;
141 unsigned int tags;
142 int isfloating;
143 int monitor;
144 } Rule;
146 /* function declarations */
147 static void applyrules(Client *c);
148 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
149 static void arrange(Monitor *m);
150 static void arrangemon(Monitor *m);
151 static void attach(Client *c);
152 static void attachstack(Client *c);
153 static void buttonpress(XEvent *e);
154 static void checkotherwm(void);
155 static void cleanup(void);
156 static void cleanupmon(Monitor *mon);
157 static void clearurgent(Client *c);
158 static void clientmessage(XEvent *e);
159 static void configure(Client *c);
160 static void configurenotify(XEvent *e);
161 static void configurerequest(XEvent *e);
162 static Monitor *createmon(void);
163 static void destroynotify(XEvent *e);
164 static void detach(Client *c);
165 static void detachstack(Client *c);
166 static Monitor *dirtomon(int dir);
167 static void drawbar(Monitor *m);
168 static void drawbars(void);
169 static void enternotify(XEvent *e);
170 static void expose(XEvent *e);
171 static void focus(Client *c);
172 static void focusin(XEvent *e);
173 static void focusmon(const Arg *arg);
174 static void focusstack(const Arg *arg);
175 static int getrootptr(int *x, int *y);
176 static long getstate(Window w);
177 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
178 static void grabbuttons(Client *c, int focused);
179 static void grabkeys(void);
180 static void incnmaster(const Arg *arg);
181 static void keypress(XEvent *e);
182 static void killclient(const Arg *arg);
183 static void manage(Window w, XWindowAttributes *wa);
184 static void mappingnotify(XEvent *e);
185 static void maprequest(XEvent *e);
186 static void monocle(Monitor *m);
187 static void motionnotify(XEvent *e);
188 static void movemouse(const Arg *arg);
189 static Client *nexttiled(Client *c);
190 static void pop(Client *);
191 static void propertynotify(XEvent *e);
192 static void quit(const Arg *arg);
193 static Monitor *recttomon(int x, int y, int w, int h);
194 static void resize(Client *c, int x, int y, int w, int h, int interact);
195 static void resizeclient(Client *c, int x, int y, int w, int h);
196 static void resizemouse(const Arg *arg);
197 static void restack(Monitor *m);
198 static void run(void);
199 static void scan(void);
200 static int sendevent(Client *c, Atom proto);
201 static void sendmon(Client *c, Monitor *m);
202 static void setclientstate(Client *c, long state);
203 static void setfocus(Client *c);
204 static void setfullscreen(Client *c, int fullscreen);
205 static void setlayout(const Arg *arg);
206 static void setmfact(const Arg *arg);
207 static void setup(void);
208 static void showhide(Client *c);
209 static void sigchld(int unused);
210 static void spawn(const Arg *arg);
211 static void tag(const Arg *arg);
212 static void tagmon(const Arg *arg);
213 static void tile(Monitor *);
214 static void togglebar(const Arg *arg);
215 static void togglefloating(const Arg *arg);
216 static void toggletag(const Arg *arg);
217 static void toggleview(const Arg *arg);
218 static void unfocus(Client *c, int setfocus);
219 static void unmanage(Client *c, int destroyed);
220 static void unmapnotify(XEvent *e);
221 static int updategeom(void);
222 static void updatebarpos(Monitor *m);
223 static void updatebars(void);
224 static void updateclientlist(void);
225 static void updatenumlockmask(void);
226 static void updatesizehints(Client *c);
227 static void updatestatus(void);
228 static void updatewindowtype(Client *c);
229 static void updatetitle(Client *c);
230 static void updatewmhints(Client *c);
231 static void view(const Arg *arg);
232 static Client *wintoclient(Window w);
233 static Monitor *wintomon(Window w);
234 static int xerror(Display *dpy, XErrorEvent *ee);
235 static int xerrordummy(Display *dpy, XErrorEvent *ee);
236 static int xerrorstart(Display *dpy, XErrorEvent *ee);
237 static void xinitvisual();
238 static void zoom(const Arg *arg);
240 /* variables */
241 static const char broken[] = "broken";
242 static char stext[256];
243 static int screen;
244 static int sw, sh; /* X display screen geometry width, height */
245 static int bh, blw = 0; /* bar geometry */
246 static int (*xerrorxlib)(Display *, XErrorEvent *);
247 static unsigned int numlockmask = 0;
248 static void (*handler[LASTEvent]) (XEvent *) = {
249 [ButtonPress] = buttonpress,
250 [ClientMessage] = clientmessage,
251 [ConfigureRequest] = configurerequest,
252 [ConfigureNotify] = configurenotify,
253 [DestroyNotify] = destroynotify,
254 [EnterNotify] = enternotify,
255 [Expose] = expose,
256 [FocusIn] = focusin,
257 [KeyPress] = keypress,
258 [MappingNotify] = mappingnotify,
259 [MapRequest] = maprequest,
260 [MotionNotify] = motionnotify,
261 [PropertyNotify] = propertynotify,
262 [UnmapNotify] = unmapnotify
264 static Atom wmatom[WMLast], netatom[NetLast];
265 static int running = 1;
266 static Cur *cursor[CurLast];
267 static ClrScheme scheme[SchemeLast];
268 static Display *dpy;
269 static Drw *drw;
270 static Monitor *mons, *selmon;
271 static Window root;
273 static int useargb = 0;
274 static Visual *visual;
275 static int depth;
276 static Colormap cmap;
278 /* configuration, allows nested code to access above variables */
279 #include "config.h"
281 /* compile-time check if all tags fit into an unsigned int bit array. */
282 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
284 /* function implementations */
285 void
286 applyrules(Client *c)
288 const char *class, *instance;
289 unsigned int i;
290 const Rule *r;
291 Monitor *m;
292 XClassHint ch = { NULL, NULL };
294 /* rule matching */
295 c->isfloating = 0;
296 c->tags = 0;
297 XGetClassHint(dpy, c->win, &ch);
298 class = ch.res_class ? ch.res_class : broken;
299 instance = ch.res_name ? ch.res_name : broken;
301 for (i = 0; i < LENGTH(rules); i++) {
302 r = &rules[i];
303 if ((!r->title || strstr(c->name, r->title))
304 && (!r->class || strstr(class, r->class))
305 && (!r->instance || strstr(instance, r->instance)))
307 c->isfloating = r->isfloating;
308 c->tags |= r->tags;
309 for (m = mons; m && m->num != r->monitor; m = m->next);
310 if (m)
311 c->mon = m;
314 if (ch.res_class)
315 XFree(ch.res_class);
316 if (ch.res_name)
317 XFree(ch.res_name);
318 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
322 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
324 int baseismin;
325 Monitor *m = c->mon;
327 /* set minimum possible */
328 *w = MAX(1, *w);
329 *h = MAX(1, *h);
330 if (interact) {
331 if (*x > sw)
332 *x = sw - WIDTH(c);
333 if (*y > sh)
334 *y = sh - HEIGHT(c);
335 if (*x + *w + 2 * c->bw < 0)
336 *x = 0;
337 if (*y + *h + 2 * c->bw < 0)
338 *y = 0;
339 } else {
340 if (*x >= m->wx + m->ww)
341 *x = m->wx + m->ww - WIDTH(c);
342 if (*y >= m->wy + m->wh)
343 *y = m->wy + m->wh - HEIGHT(c);
344 if (*x + *w + 2 * c->bw <= m->wx)
345 *x = m->wx;
346 if (*y + *h + 2 * c->bw <= m->wy)
347 *y = m->wy;
349 if (*h < bh)
350 *h = bh;
351 if (*w < bh)
352 *w = bh;
353 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
354 /* see last two sentences in ICCCM 4.1.2.3 */
355 baseismin = c->basew == c->minw && c->baseh == c->minh;
356 if (!baseismin) { /* temporarily remove base dimensions */
357 *w -= c->basew;
358 *h -= c->baseh;
360 /* adjust for aspect limits */
361 if (c->mina > 0 && c->maxa > 0) {
362 if (c->maxa < (float)*w / *h)
363 *w = *h * c->maxa + 0.5;
364 else if (c->mina < (float)*h / *w)
365 *h = *w * c->mina + 0.5;
367 if (baseismin) { /* increment calculation requires this */
368 *w -= c->basew;
369 *h -= c->baseh;
371 /* adjust for increment value */
372 if (c->incw)
373 *w -= *w % c->incw;
374 if (c->inch)
375 *h -= *h % c->inch;
376 /* restore base dimensions */
377 *w = MAX(*w + c->basew, c->minw);
378 *h = MAX(*h + c->baseh, c->minh);
379 if (c->maxw)
380 *w = MIN(*w, c->maxw);
381 if (c->maxh)
382 *h = MIN(*h, c->maxh);
384 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
387 void
388 arrange(Monitor *m)
390 if (m)
391 showhide(m->stack);
392 else for (m = mons; m; m = m->next)
393 showhide(m->stack);
394 if (m) {
395 arrangemon(m);
396 restack(m);
397 } else for (m = mons; m; m = m->next)
398 arrangemon(m);
401 void
402 arrangemon(Monitor *m)
404 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
405 if (m->lt[m->sellt]->arrange)
406 m->lt[m->sellt]->arrange(m);
409 void
410 attach(Client *c)
412 c->next = c->mon->clients;
413 c->mon->clients = c;
416 void
417 attachstack(Client *c)
419 c->snext = c->mon->stack;
420 c->mon->stack = c;
423 void
424 buttonpress(XEvent *e)
426 unsigned int i, x, click;
427 Arg arg = {0};
428 Client *c;
429 Monitor *m;
430 XButtonPressedEvent *ev = &e->xbutton;
432 click = ClkRootWin;
433 /* focus monitor if necessary */
434 if ((m = wintomon(ev->window)) && m != selmon) {
435 unfocus(selmon->sel, 1);
436 selmon = m;
437 focus(NULL);
439 if (ev->window == selmon->barwin) {
440 i = x = 0;
442 x += TEXTW(tags[i]);
443 while (ev->x >= x && ++i < LENGTH(tags));
444 if (i < LENGTH(tags)) {
445 click = ClkTagBar;
446 arg.ui = 1 << i;
447 } else if (ev->x < x + blw)
448 click = ClkLtSymbol;
449 else if (ev->x > selmon->ww - TEXTW(stext))
450 click = ClkStatusText;
451 else
452 click = ClkWinTitle;
453 } else if ((c = wintoclient(ev->window))) {
454 focus(c);
455 click = ClkClientWin;
457 for (i = 0; i < LENGTH(buttons); i++)
458 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
459 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
460 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
463 void
464 checkotherwm(void)
466 xerrorxlib = XSetErrorHandler(xerrorstart);
467 /* this causes an error if some other window manager is running */
468 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
469 XSync(dpy, False);
470 XSetErrorHandler(xerror);
471 XSync(dpy, False);
474 void
475 cleanup(void)
477 Arg a = {.ui = ~0};
478 Layout foo = { "", NULL };
479 Monitor *m;
480 size_t i;
482 view(&a);
483 selmon->lt[selmon->sellt] = &foo;
484 for (m = mons; m; m = m->next)
485 while (m->stack)
486 unmanage(m->stack, 0);
487 XUngrabKey(dpy, AnyKey, AnyModifier, root);
488 while (mons)
489 cleanupmon(mons);
490 for (i = 0; i < CurLast; i++)
491 drw_cur_free(drw, cursor[i]);
492 for (i = 0; i < SchemeLast; i++) {
493 drw_clr_free(scheme[i].border);
494 drw_clr_free(scheme[i].bg);
495 drw_clr_free(scheme[i].fg);
497 drw_free(drw);
498 XSync(dpy, False);
499 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
500 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
503 void
504 cleanupmon(Monitor *mon)
506 Monitor *m;
508 if (mon == mons)
509 mons = mons->next;
510 else {
511 for (m = mons; m && m->next != mon; m = m->next);
512 m->next = mon->next;
514 XUnmapWindow(dpy, mon->barwin);
515 XDestroyWindow(dpy, mon->barwin);
516 free(mon);
519 void
520 clearurgent(Client *c)
522 XWMHints *wmh;
524 c->isurgent = 0;
525 if (!(wmh = XGetWMHints(dpy, c->win)))
526 return;
527 wmh->flags &= ~XUrgencyHint;
528 XSetWMHints(dpy, c->win, wmh);
529 XFree(wmh);
532 void
533 clientmessage(XEvent *e)
535 XClientMessageEvent *cme = &e->xclient;
536 Client *c = wintoclient(cme->window);
538 if (!c)
539 return;
540 if (cme->message_type == netatom[NetWMState]) {
541 if (cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
542 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
543 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
544 } else if (cme->message_type == netatom[NetActiveWindow]) {
545 if (!ISVISIBLE(c)) {
546 c->mon->seltags ^= 1;
547 c->mon->tagset[c->mon->seltags] = c->tags;
549 pop(c);
553 void
554 configure(Client *c)
556 XConfigureEvent ce;
558 ce.type = ConfigureNotify;
559 ce.display = dpy;
560 ce.event = c->win;
561 ce.window = c->win;
562 ce.x = c->x;
563 ce.y = c->y;
564 ce.width = c->w;
565 ce.height = c->h;
566 ce.border_width = c->bw;
567 ce.above = None;
568 ce.override_redirect = False;
569 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
572 void
573 configurenotify(XEvent *e)
575 Monitor *m;
576 XConfigureEvent *ev = &e->xconfigure;
577 int dirty;
579 /* TODO: updategeom handling sucks, needs to be simplified */
580 if (ev->window == root) {
581 dirty = (sw != ev->width || sh != ev->height);
582 sw = ev->width;
583 sh = ev->height;
584 if (updategeom() || dirty) {
585 drw_resize(drw, sw, bh);
586 updatebars();
587 for (m = mons; m; m = m->next)
588 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
589 focus(NULL);
590 arrange(NULL);
595 void
596 configurerequest(XEvent *e)
598 Client *c;
599 Monitor *m;
600 XConfigureRequestEvent *ev = &e->xconfigurerequest;
601 XWindowChanges wc;
603 if ((c = wintoclient(ev->window))) {
604 if (ev->value_mask & CWBorderWidth)
605 c->bw = ev->border_width;
606 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
607 m = c->mon;
608 if (ev->value_mask & CWX) {
609 c->oldx = c->x;
610 c->x = m->mx + ev->x;
612 if (ev->value_mask & CWY) {
613 c->oldy = c->y;
614 c->y = m->my + ev->y;
616 if (ev->value_mask & CWWidth) {
617 c->oldw = c->w;
618 c->w = ev->width;
620 if (ev->value_mask & CWHeight) {
621 c->oldh = c->h;
622 c->h = ev->height;
624 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
625 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
626 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
627 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
628 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
629 configure(c);
630 if (ISVISIBLE(c))
631 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
632 } else
633 configure(c);
634 } else {
635 wc.x = ev->x;
636 wc.y = ev->y;
637 wc.width = ev->width;
638 wc.height = ev->height;
639 wc.border_width = ev->border_width;
640 wc.sibling = ev->above;
641 wc.stack_mode = ev->detail;
642 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
644 XSync(dpy, False);
647 Monitor *
648 createmon(void)
650 Monitor *m;
652 m = ecalloc(1, sizeof(Monitor));
653 m->tagset[0] = m->tagset[1] = 1;
654 m->mfact = mfact;
655 m->nmaster = nmaster;
656 m->showbar = showbar;
657 m->topbar = topbar;
658 m->lt[0] = &layouts[0];
659 m->lt[1] = &layouts[1 % LENGTH(layouts)];
660 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
661 return m;
664 void
665 destroynotify(XEvent *e)
667 Client *c;
668 XDestroyWindowEvent *ev = &e->xdestroywindow;
670 if ((c = wintoclient(ev->window)))
671 unmanage(c, 1);
674 void
675 detach(Client *c)
677 Client **tc;
679 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
680 *tc = c->next;
683 void
684 detachstack(Client *c)
686 Client **tc, *t;
688 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
689 *tc = c->snext;
691 if (c == c->mon->sel) {
692 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
693 c->mon->sel = t;
697 Monitor *
698 dirtomon(int dir)
700 Monitor *m = NULL;
702 if (dir > 0) {
703 if (!(m = selmon->next))
704 m = mons;
705 } else if (selmon == mons)
706 for (m = mons; m->next; m = m->next);
707 else
708 for (m = mons; m->next != selmon; m = m->next);
709 return m;
712 void
713 drawbar(Monitor *m)
715 int x, xx, w, dx;
716 unsigned int i, occ = 0, urg = 0;
717 Client *c;
719 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
721 for (c = m->clients; c; c = c->next) {
722 occ |= c->tags;
723 if (c->isurgent)
724 urg |= c->tags;
726 x = 0;
727 for (i = 0; i < LENGTH(tags); i++) {
728 w = TEXTW(tags[i]);
729 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
730 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
731 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
732 occ & 1 << i, urg & 1 << i);
733 x += w;
735 w = blw = TEXTW(m->ltsymbol);
736 drw_setscheme(drw, &scheme[SchemeNorm]);
737 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
738 x += w;
739 xx = x;
740 if (m == selmon) { /* status is only drawn on selected monitor */
741 w = TEXTW(stext);
742 x = m->ww - w;
743 if (x < xx) {
744 x = xx;
745 w = m->ww - xx;
747 drw_text(drw, x, 0, w, bh, stext, 0);
748 } else
749 x = m->ww;
750 if ((w = x - xx) > bh) {
751 x = xx;
752 if (m->sel) {
753 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
754 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
755 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
756 } else {
757 drw_setscheme(drw, &scheme[SchemeNorm]);
758 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
761 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
764 void
765 drawbars(void)
767 Monitor *m;
769 for (m = mons; m; m = m->next)
770 drawbar(m);
773 void
774 enternotify(XEvent *e)
776 Client *c;
777 Monitor *m;
778 XCrossingEvent *ev = &e->xcrossing;
780 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
781 return;
782 c = wintoclient(ev->window);
783 m = c ? c->mon : wintomon(ev->window);
784 if (m != selmon) {
785 unfocus(selmon->sel, 1);
786 selmon = m;
787 } else if (!c || c == selmon->sel)
788 return;
789 focus(c);
792 void
793 expose(XEvent *e)
795 Monitor *m;
796 XExposeEvent *ev = &e->xexpose;
798 if (ev->count == 0 && (m = wintomon(ev->window)))
799 drawbar(m);
802 void
803 focus(Client *c)
805 if (!c || !ISVISIBLE(c))
806 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
807 /* was if (selmon->sel) */
808 if (selmon->sel && selmon->sel != c)
809 unfocus(selmon->sel, 0);
810 if (c) {
811 if (c->mon != selmon)
812 selmon = c->mon;
813 if (c->isurgent)
814 clearurgent(c);
815 detachstack(c);
816 attachstack(c);
817 grabbuttons(c, 1);
818 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
819 setfocus(c);
820 } else {
821 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
822 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
824 selmon->sel = c;
825 drawbars();
828 /* there are some broken focus acquiring clients */
829 void
830 focusin(XEvent *e)
832 XFocusChangeEvent *ev = &e->xfocus;
834 if (selmon->sel && ev->window != selmon->sel->win)
835 setfocus(selmon->sel);
838 void
839 focusmon(const Arg *arg)
841 Monitor *m;
843 if (!mons->next)
844 return;
845 if ((m = dirtomon(arg->i)) == selmon)
846 return;
847 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
848 in gedit and anjuta */
849 selmon = m;
850 focus(NULL);
853 void
854 focusstack(const Arg *arg)
856 Client *c = NULL, *i;
858 if (!selmon->sel)
859 return;
860 if (arg->i > 0) {
861 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
862 if (!c)
863 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
864 } else {
865 for (i = selmon->clients; i != selmon->sel; i = i->next)
866 if (ISVISIBLE(i))
867 c = i;
868 if (!c)
869 for (; i; i = i->next)
870 if (ISVISIBLE(i))
871 c = i;
873 if (c) {
874 focus(c);
875 restack(selmon);
879 Atom
880 getatomprop(Client *c, Atom prop)
882 int di;
883 unsigned long dl;
884 unsigned char *p = NULL;
885 Atom da, atom = None;
887 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
888 &da, &di, &dl, &dl, &p) == Success && p) {
889 atom = *(Atom *)p;
890 XFree(p);
892 return atom;
896 getrootptr(int *x, int *y)
898 int di;
899 unsigned int dui;
900 Window dummy;
902 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
905 long
906 getstate(Window w)
908 int format;
909 long result = -1;
910 unsigned char *p = NULL;
911 unsigned long n, extra;
912 Atom real;
914 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
915 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
916 return -1;
917 if (n != 0)
918 result = *p;
919 XFree(p);
920 return result;
924 gettextprop(Window w, Atom atom, char *text, unsigned int size)
926 char **list = NULL;
927 int n;
928 XTextProperty name;
930 if (!text || size == 0)
931 return 0;
932 text[0] = '\0';
933 XGetTextProperty(dpy, w, &name, atom);
934 if (!name.nitems)
935 return 0;
936 if (name.encoding == XA_STRING)
937 strncpy(text, (char *)name.value, size - 1);
938 else {
939 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
940 strncpy(text, *list, size - 1);
941 XFreeStringList(list);
944 text[size - 1] = '\0';
945 XFree(name.value);
946 return 1;
949 void
950 grabbuttons(Client *c, int focused)
952 updatenumlockmask();
954 unsigned int i, j;
955 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
956 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
957 if (focused) {
958 for (i = 0; i < LENGTH(buttons); i++)
959 if (buttons[i].click == ClkClientWin)
960 for (j = 0; j < LENGTH(modifiers); j++)
961 XGrabButton(dpy, buttons[i].button,
962 buttons[i].mask | modifiers[j],
963 c->win, False, BUTTONMASK,
964 GrabModeAsync, GrabModeSync, None, None);
965 } else
966 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
967 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
971 void
972 grabkeys(void)
974 updatenumlockmask();
976 unsigned int i, j;
977 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
978 KeyCode code;
980 XUngrabKey(dpy, AnyKey, AnyModifier, root);
981 for (i = 0; i < LENGTH(keys); i++)
982 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
983 for (j = 0; j < LENGTH(modifiers); j++)
984 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
985 True, GrabModeAsync, GrabModeAsync);
989 void
990 incnmaster(const Arg *arg)
992 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
993 arrange(selmon);
996 #ifdef XINERAMA
997 static int
998 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1000 while (n--)
1001 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1002 && unique[n].width == info->width && unique[n].height == info->height)
1003 return 0;
1004 return 1;
1006 #endif /* XINERAMA */
1008 void
1009 keypress(XEvent *e)
1011 unsigned int i;
1012 KeySym keysym;
1013 XKeyEvent *ev;
1015 ev = &e->xkey;
1016 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1017 for (i = 0; i < LENGTH(keys); i++)
1018 if (keysym == keys[i].keysym
1019 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1020 && keys[i].func)
1021 keys[i].func(&(keys[i].arg));
1024 void
1025 killclient(const Arg *arg)
1027 if (!selmon->sel)
1028 return;
1029 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1030 XGrabServer(dpy);
1031 XSetErrorHandler(xerrordummy);
1032 XSetCloseDownMode(dpy, DestroyAll);
1033 XKillClient(dpy, selmon->sel->win);
1034 XSync(dpy, False);
1035 XSetErrorHandler(xerror);
1036 XUngrabServer(dpy);
1040 void
1041 manage(Window w, XWindowAttributes *wa)
1043 Client *c, *t = NULL;
1044 Window trans = None;
1045 XWindowChanges wc;
1047 c = ecalloc(1, sizeof(Client));
1048 c->win = w;
1049 updatetitle(c);
1050 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1051 c->mon = t->mon;
1052 c->tags = t->tags;
1053 } else {
1054 c->mon = selmon;
1055 applyrules(c);
1057 /* geometry */
1058 c->x = c->oldx = wa->x;
1059 c->y = c->oldy = wa->y;
1060 c->w = c->oldw = wa->width;
1061 c->h = c->oldh = wa->height;
1062 c->oldbw = wa->border_width;
1064 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1065 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1066 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1067 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1068 c->x = MAX(c->x, c->mon->mx);
1069 /* only fix client y-offset, if the client center might cover the bar */
1070 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1071 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1072 c->bw = borderpx;
1074 wc.border_width = c->bw;
1075 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1076 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1077 configure(c); /* propagates border_width, if size doesn't change */
1078 updatewindowtype(c);
1079 updatesizehints(c);
1080 updatewmhints(c);
1081 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1082 grabbuttons(c, 0);
1083 if (!c->isfloating)
1084 c->isfloating = c->oldstate = trans != None || c->isfixed;
1085 if (c->isfloating)
1086 XRaiseWindow(dpy, c->win);
1087 attach(c);
1088 attachstack(c);
1089 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1090 (unsigned char *) &(c->win), 1);
1091 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1092 setclientstate(c, NormalState);
1093 if (c->mon == selmon)
1094 unfocus(selmon->sel, 0);
1095 c->mon->sel = c;
1096 arrange(c->mon);
1097 XMapWindow(dpy, c->win);
1098 focus(NULL);
1101 void
1102 mappingnotify(XEvent *e)
1104 XMappingEvent *ev = &e->xmapping;
1106 XRefreshKeyboardMapping(ev);
1107 if (ev->request == MappingKeyboard)
1108 grabkeys();
1111 void
1112 maprequest(XEvent *e)
1114 static XWindowAttributes wa;
1115 XMapRequestEvent *ev = &e->xmaprequest;
1117 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1118 return;
1119 if (wa.override_redirect)
1120 return;
1121 if (!wintoclient(ev->window))
1122 manage(ev->window, &wa);
1125 void
1126 monocle(Monitor *m)
1128 unsigned int n = 0;
1129 Client *c;
1131 for (c = m->clients; c; c = c->next)
1132 if (ISVISIBLE(c))
1133 n++;
1134 if (n > 0) /* override layout symbol */
1135 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1136 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1137 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1140 void
1141 motionnotify(XEvent *e)
1143 static Monitor *mon = NULL;
1144 Monitor *m;
1145 XMotionEvent *ev = &e->xmotion;
1147 if (ev->window != root)
1148 return;
1149 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1150 unfocus(selmon->sel, 1);
1151 selmon = m;
1152 focus(NULL);
1154 mon = m;
1157 void
1158 movemouse(const Arg *arg)
1160 int x, y, ocx, ocy, nx, ny;
1161 Client *c;
1162 Monitor *m;
1163 XEvent ev;
1164 Time lasttime = 0;
1166 if (!(c = selmon->sel))
1167 return;
1168 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1169 return;
1170 restack(selmon);
1171 ocx = c->x;
1172 ocy = c->y;
1173 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1174 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1175 return;
1176 if (!getrootptr(&x, &y))
1177 return;
1178 do {
1179 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1180 switch(ev.type) {
1181 case ConfigureRequest:
1182 case Expose:
1183 case MapRequest:
1184 handler[ev.type](&ev);
1185 break;
1186 case MotionNotify:
1187 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1188 continue;
1189 lasttime = ev.xmotion.time;
1191 nx = ocx + (ev.xmotion.x - x);
1192 ny = ocy + (ev.xmotion.y - y);
1193 if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1194 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1195 if (abs(selmon->wx - nx) < snap)
1196 nx = selmon->wx;
1197 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1198 nx = selmon->wx + selmon->ww - WIDTH(c);
1199 if (abs(selmon->wy - ny) < snap)
1200 ny = selmon->wy;
1201 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1202 ny = selmon->wy + selmon->wh - HEIGHT(c);
1203 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1204 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1205 togglefloating(NULL);
1207 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1208 resize(c, nx, ny, c->w, c->h, 1);
1209 break;
1211 } while (ev.type != ButtonRelease);
1212 XUngrabPointer(dpy, CurrentTime);
1213 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1214 sendmon(c, m);
1215 selmon = m;
1216 focus(NULL);
1220 Client *
1221 nexttiled(Client *c)
1223 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1224 return c;
1227 void
1228 pop(Client *c)
1230 detach(c);
1231 attach(c);
1232 focus(c);
1233 arrange(c->mon);
1236 void
1237 propertynotify(XEvent *e)
1239 Client *c;
1240 Window trans;
1241 XPropertyEvent *ev = &e->xproperty;
1243 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1244 updatestatus();
1245 else if (ev->state == PropertyDelete)
1246 return; /* ignore */
1247 else if ((c = wintoclient(ev->window))) {
1248 switch(ev->atom) {
1249 default: break;
1250 case XA_WM_TRANSIENT_FOR:
1251 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1252 (c->isfloating = (wintoclient(trans)) != NULL))
1253 arrange(c->mon);
1254 break;
1255 case XA_WM_NORMAL_HINTS:
1256 updatesizehints(c);
1257 break;
1258 case XA_WM_HINTS:
1259 updatewmhints(c);
1260 drawbars();
1261 break;
1263 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1264 updatetitle(c);
1265 if (c == c->mon->sel)
1266 drawbar(c->mon);
1268 if (ev->atom == netatom[NetWMWindowType])
1269 updatewindowtype(c);
1273 void
1274 quit(const Arg *arg)
1276 running = 0;
1279 Monitor *
1280 recttomon(int x, int y, int w, int h)
1282 Monitor *m, *r = selmon;
1283 int a, area = 0;
1285 for (m = mons; m; m = m->next)
1286 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1287 area = a;
1288 r = m;
1290 return r;
1293 void
1294 resize(Client *c, int x, int y, int w, int h, int interact)
1296 if (applysizehints(c, &x, &y, &w, &h, interact))
1297 resizeclient(c, x, y, w, h);
1300 void
1301 resizeclient(Client *c, int x, int y, int w, int h)
1303 XWindowChanges wc;
1305 c->oldx = c->x; c->x = wc.x = x;
1306 c->oldy = c->y; c->y = wc.y = y;
1307 c->oldw = c->w; c->w = wc.width = w;
1308 c->oldh = c->h; c->h = wc.height = h;
1309 wc.border_width = c->bw;
1310 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1311 configure(c);
1312 XSync(dpy, False);
1315 void
1316 resizemouse(const Arg *arg)
1318 int ocx, ocy, nw, nh;
1319 Client *c;
1320 Monitor *m;
1321 XEvent ev;
1322 Time lasttime = 0;
1324 if (!(c = selmon->sel))
1325 return;
1326 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1327 return;
1328 restack(selmon);
1329 ocx = c->x;
1330 ocy = c->y;
1331 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1332 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1333 return;
1334 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1335 do {
1336 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1337 switch(ev.type) {
1338 case ConfigureRequest:
1339 case Expose:
1340 case MapRequest:
1341 handler[ev.type](&ev);
1342 break;
1343 case MotionNotify:
1344 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1345 continue;
1346 lasttime = ev.xmotion.time;
1348 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1349 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1350 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1351 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1353 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1354 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1355 togglefloating(NULL);
1357 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1358 resize(c, c->x, c->y, nw, nh, 1);
1359 break;
1361 } while (ev.type != ButtonRelease);
1362 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1363 XUngrabPointer(dpy, CurrentTime);
1364 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1365 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1366 sendmon(c, m);
1367 selmon = m;
1368 focus(NULL);
1372 void
1373 restack(Monitor *m)
1375 Client *c;
1376 XEvent ev;
1377 XWindowChanges wc;
1379 drawbar(m);
1380 if (!m->sel)
1381 return;
1382 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1383 XRaiseWindow(dpy, m->sel->win);
1384 if (m->lt[m->sellt]->arrange) {
1385 wc.stack_mode = Below;
1386 wc.sibling = m->barwin;
1387 for (c = m->stack; c; c = c->snext)
1388 if (!c->isfloating && ISVISIBLE(c)) {
1389 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1390 wc.sibling = c->win;
1393 XSync(dpy, False);
1394 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1397 void
1398 run(void)
1400 XEvent ev;
1401 /* main event loop */
1402 XSync(dpy, False);
1403 while (running && !XNextEvent(dpy, &ev))
1404 if (handler[ev.type])
1405 handler[ev.type](&ev); /* call handler */
1408 void
1409 scan(void)
1411 unsigned int i, num;
1412 Window d1, d2, *wins = NULL;
1413 XWindowAttributes wa;
1415 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1416 for (i = 0; i < num; i++) {
1417 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1418 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1419 continue;
1420 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1421 manage(wins[i], &wa);
1423 for (i = 0; i < num; i++) { /* now the transients */
1424 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1425 continue;
1426 if (XGetTransientForHint(dpy, wins[i], &d1)
1427 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1428 manage(wins[i], &wa);
1430 if (wins)
1431 XFree(wins);
1435 void
1436 sendmon(Client *c, Monitor *m)
1438 if (c->mon == m)
1439 return;
1440 unfocus(c, 1);
1441 detach(c);
1442 detachstack(c);
1443 c->mon = m;
1444 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1445 attach(c);
1446 attachstack(c);
1447 focus(NULL);
1448 arrange(NULL);
1451 void
1452 setclientstate(Client *c, long state)
1454 long data[] = { state, None };
1456 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1457 PropModeReplace, (unsigned char *)data, 2);
1461 sendevent(Client *c, Atom proto)
1463 int n;
1464 Atom *protocols;
1465 int exists = 0;
1466 XEvent ev;
1468 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1469 while (!exists && n--)
1470 exists = protocols[n] == proto;
1471 XFree(protocols);
1473 if (exists) {
1474 ev.type = ClientMessage;
1475 ev.xclient.window = c->win;
1476 ev.xclient.message_type = wmatom[WMProtocols];
1477 ev.xclient.format = 32;
1478 ev.xclient.data.l[0] = proto;
1479 ev.xclient.data.l[1] = CurrentTime;
1480 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1482 return exists;
1485 void
1486 setfocus(Client *c)
1488 if (!c->neverfocus) {
1489 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1490 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1491 XA_WINDOW, 32, PropModeReplace,
1492 (unsigned char *) &(c->win), 1);
1494 sendevent(c, wmatom[WMTakeFocus]);
1497 void
1498 setfullscreen(Client *c, int fullscreen)
1500 if (fullscreen && !c->isfullscreen) {
1501 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1502 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1503 c->isfullscreen = 1;
1504 c->oldstate = c->isfloating;
1505 c->oldbw = c->bw;
1506 c->bw = 0;
1507 c->isfloating = 1;
1508 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1509 XRaiseWindow(dpy, c->win);
1510 } else if (!fullscreen && c->isfullscreen){
1511 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1512 PropModeReplace, (unsigned char*)0, 0);
1513 c->isfullscreen = 0;
1514 c->isfloating = c->oldstate;
1515 c->bw = c->oldbw;
1516 c->x = c->oldx;
1517 c->y = c->oldy;
1518 c->w = c->oldw;
1519 c->h = c->oldh;
1520 resizeclient(c, c->x, c->y, c->w, c->h);
1521 arrange(c->mon);
1525 void
1526 setlayout(const Arg *arg)
1528 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1529 selmon->sellt ^= 1;
1530 if (arg && arg->v)
1531 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1532 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1533 if (selmon->sel)
1534 arrange(selmon);
1535 else
1536 drawbar(selmon);
1539 /* arg > 1.0 will set mfact absolutly */
1540 void
1541 setmfact(const Arg *arg)
1543 float f;
1545 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1546 return;
1547 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1548 if (f < 0.1 || f > 0.9)
1549 return;
1550 selmon->mfact = f;
1551 arrange(selmon);
1554 void
1555 setup(void)
1557 XSetWindowAttributes wa;
1559 /* clean up any zombies immediately */
1560 sigchld(0);
1562 /* init screen */
1563 screen = DefaultScreen(dpy);
1564 sw = DisplayWidth(dpy, screen);
1565 sh = DisplayHeight(dpy, screen);
1566 root = RootWindow(dpy, screen);
1567 xinitvisual();
1568 drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
1569 drw_load_fonts(drw, fonts, LENGTH(fonts));
1570 if (!drw->fontcount)
1571 die("no fonts could be loaded.\n");
1572 bh = drw->fonts[0]->h + 2;
1573 updategeom();
1574 /* init atoms */
1575 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1576 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1577 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1578 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1579 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1580 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1581 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1582 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1583 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1584 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1585 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1586 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1587 /* init cursors */
1588 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1589 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1590 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1591 /* init appearance */
1592 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor, borderalpha);
1593 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor, baralpha);
1594 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor, OPAQUE);
1595 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor, borderalpha);
1596 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor, baralpha);
1597 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor, OPAQUE);
1598 /* init bars */
1599 updatebars();
1600 updatestatus();
1601 /* EWMH support per view */
1602 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1603 PropModeReplace, (unsigned char *) netatom, NetLast);
1604 XDeleteProperty(dpy, root, netatom[NetClientList]);
1605 /* select for events */
1606 wa.cursor = cursor[CurNormal]->cursor;
1607 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1608 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1609 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1610 XSelectInput(dpy, root, wa.event_mask);
1611 grabkeys();
1612 focus(NULL);
1615 void
1616 showhide(Client *c)
1618 if (!c)
1619 return;
1620 if (ISVISIBLE(c)) {
1621 /* show clients top down */
1622 XMoveWindow(dpy, c->win, c->x, c->y);
1623 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1624 resize(c, c->x, c->y, c->w, c->h, 0);
1625 showhide(c->snext);
1626 } else {
1627 /* hide clients bottom up */
1628 showhide(c->snext);
1629 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1633 void
1634 sigchld(int unused)
1636 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1637 die("can't install SIGCHLD handler:");
1638 while (0 < waitpid(-1, NULL, WNOHANG));
1641 void
1642 spawn(const Arg *arg)
1644 if (arg->v == dmenucmd)
1645 dmenumon[0] = '0' + selmon->num;
1646 if (fork() == 0) {
1647 if (dpy)
1648 close(ConnectionNumber(dpy));
1649 setsid();
1650 execvp(((char **)arg->v)[0], (char **)arg->v);
1651 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1652 perror(" failed");
1653 exit(EXIT_SUCCESS);
1657 void
1658 tag(const Arg *arg)
1660 if (selmon->sel && arg->ui & TAGMASK) {
1661 selmon->sel->tags = arg->ui & TAGMASK;
1662 focus(NULL);
1663 arrange(selmon);
1667 void
1668 tagmon(const Arg *arg)
1670 if (!selmon->sel || !mons->next)
1671 return;
1672 sendmon(selmon->sel, dirtomon(arg->i));
1675 void
1676 tile(Monitor *m)
1678 unsigned int i, n, h, mw, my, ty;
1679 Client *c;
1681 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1682 if (n == 0)
1683 return;
1685 if (n > m->nmaster)
1686 mw = m->nmaster ? m->ww * m->mfact : 0;
1687 else
1688 mw = m->ww;
1689 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1690 if (i < m->nmaster) {
1691 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1692 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1693 my += HEIGHT(c);
1694 } else {
1695 h = (m->wh - ty) / (n - i);
1696 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1697 ty += HEIGHT(c);
1701 void
1702 togglebar(const Arg *arg)
1704 selmon->showbar = !selmon->showbar;
1705 updatebarpos(selmon);
1706 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1707 arrange(selmon);
1710 void
1711 togglefloating(const Arg *arg)
1713 if (!selmon->sel)
1714 return;
1715 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1716 return;
1717 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1718 if (selmon->sel->isfloating)
1719 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1720 selmon->sel->w, selmon->sel->h, 0);
1721 arrange(selmon);
1724 void
1725 toggletag(const Arg *arg)
1727 unsigned int newtags;
1729 if (!selmon->sel)
1730 return;
1731 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1732 if (newtags) {
1733 selmon->sel->tags = newtags;
1734 focus(NULL);
1735 arrange(selmon);
1739 void
1740 toggleview(const Arg *arg)
1742 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1744 if (newtagset) {
1745 selmon->tagset[selmon->seltags] = newtagset;
1746 focus(NULL);
1747 arrange(selmon);
1751 void
1752 unfocus(Client *c, int setfocus)
1754 if (!c)
1755 return;
1756 grabbuttons(c, 0);
1757 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1758 if (setfocus) {
1759 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1760 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1764 void
1765 unmanage(Client *c, int destroyed)
1767 Monitor *m = c->mon;
1768 XWindowChanges wc;
1770 /* The server grab construct avoids race conditions. */
1771 detach(c);
1772 detachstack(c);
1773 if (!destroyed) {
1774 wc.border_width = c->oldbw;
1775 XGrabServer(dpy);
1776 XSetErrorHandler(xerrordummy);
1777 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1778 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1779 setclientstate(c, WithdrawnState);
1780 XSync(dpy, False);
1781 XSetErrorHandler(xerror);
1782 XUngrabServer(dpy);
1784 free(c);
1785 focus(NULL);
1786 updateclientlist();
1787 arrange(m);
1790 void
1791 unmapnotify(XEvent *e)
1793 Client *c;
1794 XUnmapEvent *ev = &e->xunmap;
1796 if ((c = wintoclient(ev->window))) {
1797 if (ev->send_event)
1798 setclientstate(c, WithdrawnState);
1799 else
1800 unmanage(c, 0);
1804 void
1805 updatebars(void)
1807 Monitor *m;
1808 XSetWindowAttributes wa = {
1809 .override_redirect = True,
1810 .background_pixel = 0,
1811 .border_pixel = 0,
1812 .colormap = cmap,
1813 .event_mask = ButtonPressMask|ExposureMask
1815 for (m = mons; m; m = m->next) {
1816 if (m->barwin)
1817 continue;
1818 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
1819 InputOutput, visual,
1820 CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
1821 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1822 XMapRaised(dpy, m->barwin);
1826 void
1827 updatebarpos(Monitor *m)
1829 m->wy = m->my;
1830 m->wh = m->mh;
1831 if (m->showbar) {
1832 m->wh -= bh;
1833 m->by = m->topbar ? m->wy : m->wy + m->wh;
1834 m->wy = m->topbar ? m->wy + bh : m->wy;
1835 } else
1836 m->by = -bh;
1839 void
1840 updateclientlist()
1842 Client *c;
1843 Monitor *m;
1845 XDeleteProperty(dpy, root, netatom[NetClientList]);
1846 for (m = mons; m; m = m->next)
1847 for (c = m->clients; c; c = c->next)
1848 XChangeProperty(dpy, root, netatom[NetClientList],
1849 XA_WINDOW, 32, PropModeAppend,
1850 (unsigned char *) &(c->win), 1);
1854 updategeom(void)
1856 int dirty = 0;
1858 #ifdef XINERAMA
1859 if (XineramaIsActive(dpy)) {
1860 int i, j, n, nn;
1861 Client *c;
1862 Monitor *m;
1863 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1864 XineramaScreenInfo *unique = NULL;
1866 for (n = 0, m = mons; m; m = m->next, n++);
1867 /* only consider unique geometries as separate screens */
1868 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1869 for (i = 0, j = 0; i < nn; i++)
1870 if (isuniquegeom(unique, j, &info[i]))
1871 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1872 XFree(info);
1873 nn = j;
1874 if (n <= nn) {
1875 for (i = 0; i < (nn - n); i++) { /* new monitors available */
1876 for (m = mons; m && m->next; m = m->next);
1877 if (m)
1878 m->next = createmon();
1879 else
1880 mons = createmon();
1882 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1883 if (i >= n
1884 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1885 || unique[i].width != m->mw || unique[i].height != m->mh))
1887 dirty = 1;
1888 m->num = i;
1889 m->mx = m->wx = unique[i].x_org;
1890 m->my = m->wy = unique[i].y_org;
1891 m->mw = m->ww = unique[i].width;
1892 m->mh = m->wh = unique[i].height;
1893 updatebarpos(m);
1895 } else {
1896 /* less monitors available nn < n */
1897 for (i = nn; i < n; i++) {
1898 for (m = mons; m && m->next; m = m->next);
1899 while (m->clients) {
1900 dirty = 1;
1901 c = m->clients;
1902 m->clients = c->next;
1903 detachstack(c);
1904 c->mon = mons;
1905 attach(c);
1906 attachstack(c);
1908 if (m == selmon)
1909 selmon = mons;
1910 cleanupmon(m);
1913 free(unique);
1914 } else
1915 #endif /* XINERAMA */
1916 /* default monitor setup */
1918 if (!mons)
1919 mons = createmon();
1920 if (mons->mw != sw || mons->mh != sh) {
1921 dirty = 1;
1922 mons->mw = mons->ww = sw;
1923 mons->mh = mons->wh = sh;
1924 updatebarpos(mons);
1927 if (dirty) {
1928 selmon = mons;
1929 selmon = wintomon(root);
1931 return dirty;
1934 void
1935 updatenumlockmask(void)
1937 unsigned int i, j;
1938 XModifierKeymap *modmap;
1940 numlockmask = 0;
1941 modmap = XGetModifierMapping(dpy);
1942 for (i = 0; i < 8; i++)
1943 for (j = 0; j < modmap->max_keypermod; j++)
1944 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1945 == XKeysymToKeycode(dpy, XK_Num_Lock))
1946 numlockmask = (1 << i);
1947 XFreeModifiermap(modmap);
1950 void
1951 updatesizehints(Client *c)
1953 long msize;
1954 XSizeHints size;
1956 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1957 /* size is uninitialized, ensure that size.flags aren't used */
1958 size.flags = PSize;
1959 if (size.flags & PBaseSize) {
1960 c->basew = size.base_width;
1961 c->baseh = size.base_height;
1962 } else if (size.flags & PMinSize) {
1963 c->basew = size.min_width;
1964 c->baseh = size.min_height;
1965 } else
1966 c->basew = c->baseh = 0;
1967 if (size.flags & PResizeInc) {
1968 c->incw = size.width_inc;
1969 c->inch = size.height_inc;
1970 } else
1971 c->incw = c->inch = 0;
1972 if (size.flags & PMaxSize) {
1973 c->maxw = size.max_width;
1974 c->maxh = size.max_height;
1975 } else
1976 c->maxw = c->maxh = 0;
1977 if (size.flags & PMinSize) {
1978 c->minw = size.min_width;
1979 c->minh = size.min_height;
1980 } else if (size.flags & PBaseSize) {
1981 c->minw = size.base_width;
1982 c->minh = size.base_height;
1983 } else
1984 c->minw = c->minh = 0;
1985 if (size.flags & PAspect) {
1986 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1987 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1988 } else
1989 c->maxa = c->mina = 0.0;
1990 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1991 && c->maxw == c->minw && c->maxh == c->minh);
1994 void
1995 updatetitle(Client *c)
1997 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
1998 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
1999 if (c->name[0] == '\0') /* hack to mark broken clients */
2000 strcpy(c->name, broken);
2003 void
2004 updatestatus(void)
2006 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2007 strcpy(stext, "dwm-"VERSION);
2008 drawbar(selmon);
2011 void
2012 updatewindowtype(Client *c)
2014 Atom state = getatomprop(c, netatom[NetWMState]);
2015 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2017 if (state == netatom[NetWMFullscreen])
2018 setfullscreen(c, 1);
2019 if (wtype == netatom[NetWMWindowTypeDialog])
2020 c->isfloating = 1;
2023 void
2024 updatewmhints(Client *c)
2026 XWMHints *wmh;
2028 if ((wmh = XGetWMHints(dpy, c->win))) {
2029 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2030 wmh->flags &= ~XUrgencyHint;
2031 XSetWMHints(dpy, c->win, wmh);
2032 } else
2033 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2034 if (wmh->flags & InputHint)
2035 c->neverfocus = !wmh->input;
2036 else
2037 c->neverfocus = 0;
2038 XFree(wmh);
2042 void
2043 view(const Arg *arg)
2045 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2046 return;
2047 selmon->seltags ^= 1; /* toggle sel tagset */
2048 if (arg->ui & TAGMASK)
2049 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2050 focus(NULL);
2051 arrange(selmon);
2054 Client *
2055 wintoclient(Window w)
2057 Client *c;
2058 Monitor *m;
2060 for (m = mons; m; m = m->next)
2061 for (c = m->clients; c; c = c->next)
2062 if (c->win == w)
2063 return c;
2064 return NULL;
2067 Monitor *
2068 wintomon(Window w)
2070 int x, y;
2071 Client *c;
2072 Monitor *m;
2074 if (w == root && getrootptr(&x, &y))
2075 return recttomon(x, y, 1, 1);
2076 for (m = mons; m; m = m->next)
2077 if (w == m->barwin)
2078 return m;
2079 if ((c = wintoclient(w)))
2080 return c->mon;
2081 return selmon;
2084 /* There's no way to check accesses to destroyed windows, thus those cases are
2085 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2086 * default error handler, which may call exit. */
2088 xerror(Display *dpy, XErrorEvent *ee)
2090 if (ee->error_code == BadWindow
2091 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2092 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2093 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2094 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2095 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2096 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2097 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2098 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2099 return 0;
2100 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2101 ee->request_code, ee->error_code);
2102 return xerrorxlib(dpy, ee); /* may call exit */
2106 xerrordummy(Display *dpy, XErrorEvent *ee)
2108 return 0;
2111 /* Startup Error handler to check if another window manager
2112 * is already running. */
2114 xerrorstart(Display *dpy, XErrorEvent *ee)
2116 die("dwm: another window manager is already running\n");
2117 return -1;
2120 void
2121 xinitvisual()
2123 XVisualInfo *infos;
2124 XRenderPictFormat *fmt;
2125 int nitems;
2126 int i;
2128 XVisualInfo tpl = {
2129 .screen = screen,
2130 .depth = 32,
2131 .class = TrueColor
2133 long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
2135 infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
2136 visual = NULL;
2137 for(i = 0; i < nitems; i ++) {
2138 fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
2139 if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
2140 visual = infos[i].visual;
2141 depth = infos[i].depth;
2142 cmap = XCreateColormap(dpy, root, visual, AllocNone);
2143 useargb = 1;
2144 break;
2148 XFree(infos);
2150 if (! visual) {
2151 visual = DefaultVisual(dpy, screen);
2152 depth = DefaultDepth(dpy, screen);
2153 cmap = DefaultColormap(dpy, screen);
2157 void
2158 zoom(const Arg *arg)
2160 Client *c = selmon->sel;
2162 if (!selmon->lt[selmon->sellt]->arrange
2163 || (selmon->sel && selmon->sel->isfloating))
2164 return;
2165 if (c == nexttiled(selmon->clients))
2166 if (!c || !(c = nexttiled(c->next)))
2167 return;
2168 pop(c);
2172 main(int argc, char *argv[])
2174 if (argc == 2 && !strcmp("-v", argv[1]))
2175 die("dwm-"VERSION "\n");
2176 else if (argc != 1)
2177 die("usage: dwm [-v]\n");
2178 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2179 fputs("warning: no locale support\n", stderr);
2180 if (!(dpy = XOpenDisplay(NULL)))
2181 die("dwm: cannot open display\n");
2182 checkotherwm();
2183 setup();
2184 scan();
2185 run();
2186 cleanup();
2187 XCloseDisplay(dpy);
2188 return EXIT_SUCCESS;