various fixes
[azarus-dwm.git] / dwm.c
blob78dc4e9744ec96dc37b401e4176c70fba51b25d0
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 <X11/Xatom.h>
24 #include <X11/Xlib.h>
25 #include <X11/Xproto.h>
26 #include <X11/Xutil.h>
27 #include <X11/cursorfont.h>
28 #include <X11/keysym.h>
29 #include <errno.h>
30 #include <locale.h>
31 #include <signal.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <unistd.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"
46 /* macros */
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_text(drw, 0, 0, 0, 0, (X), 0) + drw->fonts[0]->h)
59 #define OPAQUE 0xffU
61 /* enums */
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState,
65 NetWMFullscreen, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
69 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
71 typedef union {
72 int i;
73 unsigned int ui;
74 float f;
75 const void *v;
76 } Arg;
78 typedef struct {
79 unsigned int click;
80 unsigned int mask;
81 unsigned int button;
82 void (*func)(const Arg *arg);
83 const Arg arg;
84 } Button;
86 typedef struct Monitor Monitor;
87 typedef struct Client Client;
88 struct Client {
89 char name[256];
90 float mina, maxa;
91 int x, y, w, h;
92 int oldx, oldy, oldw, oldh;
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
94 int bw, oldbw;
95 unsigned int tags;
96 int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
97 Client *next;
98 Client *snext;
99 Monitor *mon;
100 Window win;
103 typedef struct {
104 unsigned int mod;
105 KeySym keysym;
106 void (*func)(const Arg *);
107 const Arg arg;
108 } Key;
110 typedef struct {
111 const char *symbol;
112 void (*arrange)(Monitor *);
113 } Layout;
115 struct Monitor {
116 char ltsymbol[16];
117 float mfact;
118 int nmaster;
119 int num;
120 int by; /* bar geometry */
121 int mx, my, mw, mh; /* screen size */
122 int wx, wy, ww, wh; /* window area */
123 unsigned int seltags;
124 unsigned int sellt;
125 unsigned int tagset[2];
126 int showbar;
127 int topbar;
128 Client *clients;
129 Client *sel;
130 Client *stack;
131 Monitor *next;
132 Window barwin;
133 const Layout *lt[2];
136 typedef struct {
137 const char *class;
138 const char *instance;
139 const char *title;
140 unsigned int tags;
141 int isfloating;
142 int monitor;
143 } Rule;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact);
148 static void arrange(Monitor *m);
149 static void arrangemon(Monitor *m);
150 static void attach(Client *c);
151 static void attachstack(Client *c);
152 static void buttonpress(XEvent *e);
153 static void checkotherwm(void);
154 static void cleanup(void);
155 static void cleanupmon(Monitor *mon);
156 static void clearurgent(Client *c);
157 static void clientmessage(XEvent *e);
158 static void configure(Client *c);
159 static void configurenotify(XEvent *e);
160 static void configurerequest(XEvent *e);
161 static Monitor *createmon(void);
162 static void destroynotify(XEvent *e);
163 static void detach(Client *c);
164 static void detachstack(Client *c);
165 static Monitor *dirtomon(int dir);
166 static void drawbar(Monitor *m);
167 static void drawbars(void);
168 static void enternotify(XEvent *e);
169 static void expose(XEvent *e);
170 static void focus(Client *c);
171 static void focusin(XEvent *e);
172 static void focusmon(const Arg *arg);
173 static void focusstack(const Arg *arg);
174 static int getrootptr(int *x, int *y);
175 static long getstate(Window w);
176 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
177 static void grabbuttons(Client *c, int focused);
178 static void grabkeys(void);
179 static void incnmaster(const Arg *arg);
180 static void keypress(XEvent *e);
181 static void killclient(const Arg *arg);
182 static void manage(Window w, XWindowAttributes *wa);
183 static void mappingnotify(XEvent *e);
184 static void maprequest(XEvent *e);
185 static void monocle(Monitor *m);
186 static void motionnotify(XEvent *e);
187 static void movemouse(const Arg *arg);
188 static Client *nexttiled(Client *c);
189 static void pop(Client *);
190 static void propertynotify(XEvent *e);
191 static void quit(const Arg *arg);
192 static Monitor *recttomon(int x, int y, int w, int h);
193 static void resize(Client *c, int x, int y, int w, int h, int interact);
194 static void resizeclient(Client *c, int x, int y, int w, int h);
195 static void resizemouse(const Arg *arg);
196 static void restack(Monitor *m);
197 static void run(void);
198 static void scan(void);
199 static int sendevent(Client *c, Atom proto);
200 static void sendmon(Client *c, Monitor *m);
201 static void setclientstate(Client *c, long state);
202 static void setfocus(Client *c);
203 static void setfullscreen(Client *c, int fullscreen);
204 static void setlayout(const Arg *arg);
205 static void setmfact(const Arg *arg);
206 static void setup(void);
207 static void showhide(Client *c);
208 static void sigchld(int unused);
209 static void spawn(const Arg *arg);
210 static void tag(const Arg *arg);
211 static void tagmon(const Arg *arg);
212 static void tile(Monitor *);
213 static void togglebar(const Arg *arg);
214 static void togglefloating(const Arg *arg);
215 static void toggletag(const Arg *arg);
216 static void toggleview(const Arg *arg);
217 static void unfocus(Client *c, int setfocus);
218 static void unmanage(Client *c, int destroyed);
219 static void unmapnotify(XEvent *e);
220 static int updategeom(void);
221 static void updatebarpos(Monitor *m);
222 static void updatebars(void);
223 static void updateclientlist(void);
224 static void updatenumlockmask(void);
225 static void updatesizehints(Client *c);
226 static void updatestatus(void);
227 static void updatewindowtype(Client *c);
228 static void updatetitle(Client *c);
229 static void updatewmhints(Client *c);
230 static void view(const Arg *arg);
231 static Client *wintoclient(Window w);
232 static Monitor *wintomon(Window w);
233 static int xerror(Display *dpy, XErrorEvent *ee);
234 static int xerrordummy(Display *dpy, XErrorEvent *ee);
235 static int xerrorstart(Display *dpy, XErrorEvent *ee);
236 static void xinitvisual();
237 static void zoom(const Arg *arg);
239 /* variables */
240 static const char broken[] = "broken";
241 static char stext[256];
242 static int screen;
243 static int sw, sh; /* X display screen geometry width, height */
244 static int bh, blw = 0; /* bar geometry */
245 static int (*xerrorxlib)(Display *, XErrorEvent *);
246 static unsigned int numlockmask = 0;
247 static void (*handler[LASTEvent]) (XEvent *) = {
248 [ButtonPress] = buttonpress,
249 [ClientMessage] = clientmessage,
250 [ConfigureRequest] = configurerequest,
251 [ConfigureNotify] = configurenotify,
252 [DestroyNotify] = destroynotify,
253 [EnterNotify] = enternotify,
254 [Expose] = expose,
255 [FocusIn] = focusin,
256 [KeyPress] = keypress,
257 [MappingNotify] = mappingnotify,
258 [MapRequest] = maprequest,
259 [MotionNotify] = motionnotify,
260 [PropertyNotify] = propertynotify,
261 [UnmapNotify] = unmapnotify
263 static Atom wmatom[WMLast], netatom[NetLast];
264 static int running = 1;
265 static Cur *cursor[CurLast];
266 static ClrScheme scheme[SchemeLast];
267 static Display *dpy;
268 static Drw *drw;
269 static Monitor *mons, *selmon;
270 static Window root;
272 static int useargb = 0;
273 static Visual *visual;
274 static int depth;
275 static Colormap cmap;
277 /* configuration, allows nested code to access above variables */
278 #include "config.h"
280 /* compile-time check if all tags fit into an unsigned int bit array. */
281 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
283 /* function implementations */
284 void
285 applyrules(Client *c)
287 const char *class, *instance;
288 unsigned int i;
289 const Rule *r;
290 Monitor *m;
291 XClassHint ch = { NULL, NULL };
293 /* rule matching */
294 c->isfloating = 0;
295 c->tags = 0;
296 XGetClassHint(dpy, c->win, &ch);
297 class = ch.res_class ? ch.res_class : broken;
298 instance = ch.res_name ? ch.res_name : broken;
300 for (i = 0; i < LENGTH(rules); i++) {
301 r = &rules[i];
302 if ((!r->title || strstr(c->name, r->title))
303 && (!r->class || strstr(class, r->class))
304 && (!r->instance || strstr(instance, r->instance)))
306 c->isfloating = r->isfloating;
307 c->tags |= r->tags;
308 for (m = mons; m && m->num != r->monitor; m = m->next);
309 if (m)
310 c->mon = m;
313 if (ch.res_class)
314 XFree(ch.res_class);
315 if (ch.res_name)
316 XFree(ch.res_name);
317 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
321 applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
323 int baseismin;
324 Monitor *m = c->mon;
326 /* set minimum possible */
327 *w = MAX(1, *w);
328 *h = MAX(1, *h);
329 if (interact) {
330 if (*x > sw)
331 *x = sw - WIDTH(c);
332 if (*y > sh)
333 *y = sh - HEIGHT(c);
334 if (*x + *w + 2 * c->bw < 0)
335 *x = 0;
336 if (*y + *h + 2 * c->bw < 0)
337 *y = 0;
338 } else {
339 if (*x >= m->wx + m->ww)
340 *x = m->wx + m->ww - WIDTH(c);
341 if (*y >= m->wy + m->wh)
342 *y = m->wy + m->wh - HEIGHT(c);
343 if (*x + *w + 2 * c->bw <= m->wx)
344 *x = m->wx;
345 if (*y + *h + 2 * c->bw <= m->wy)
346 *y = m->wy;
348 if (*h < bh)
349 *h = bh;
350 if (*w < bh)
351 *w = bh;
352 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
353 /* see last two sentences in ICCCM 4.1.2.3 */
354 baseismin = c->basew == c->minw && c->baseh == c->minh;
355 if (!baseismin) { /* temporarily remove base dimensions */
356 *w -= c->basew;
357 *h -= c->baseh;
359 /* adjust for aspect limits */
360 if (c->mina > 0 && c->maxa > 0) {
361 if (c->maxa < (float)*w / *h)
362 *w = *h * c->maxa + 0.5;
363 else if (c->mina < (float)*h / *w)
364 *h = *w * c->mina + 0.5;
366 if (baseismin) { /* increment calculation requires this */
367 *w -= c->basew;
368 *h -= c->baseh;
370 /* adjust for increment value */
371 if (c->incw)
372 *w -= *w % c->incw;
373 if (c->inch)
374 *h -= *h % c->inch;
375 /* restore base dimensions */
376 *w = MAX(*w + c->basew, c->minw);
377 *h = MAX(*h + c->baseh, c->minh);
378 if (c->maxw)
379 *w = MIN(*w, c->maxw);
380 if (c->maxh)
381 *h = MIN(*h, c->maxh);
383 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
386 void
387 arrange(Monitor *m)
389 if (m)
390 showhide(m->stack);
391 else for (m = mons; m; m = m->next)
392 showhide(m->stack);
393 if (m) {
394 arrangemon(m);
395 restack(m);
396 } else for (m = mons; m; m = m->next)
397 arrangemon(m);
400 void
401 arrangemon(Monitor *m)
403 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
404 if (m->lt[m->sellt]->arrange)
405 m->lt[m->sellt]->arrange(m);
408 void
409 attach(Client *c)
411 c->next = c->mon->clients;
412 c->mon->clients = c;
415 void
416 attachstack(Client *c)
418 c->snext = c->mon->stack;
419 c->mon->stack = c;
422 void
423 buttonpress(XEvent *e)
425 unsigned int i, x, click;
426 Arg arg = {0};
427 Client *c;
428 Monitor *m;
429 XButtonPressedEvent *ev = &e->xbutton;
431 click = ClkRootWin;
432 /* focus monitor if necessary */
433 if ((m = wintomon(ev->window)) && m != selmon) {
434 unfocus(selmon->sel, 1);
435 selmon = m;
436 focus(NULL);
438 if (ev->window == selmon->barwin) {
439 i = x = 0;
440 unsigned int occ = 0;
441 for(c = m->clients; c; c = c->next)
442 occ |= c->tags;
443 do {
444 /* do not reserve space for vacant tags */
445 if(!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
446 continue;
447 x += TEXTW(tags[i]);
448 } while (ev->x >= x && ++i < LENGTH(tags));
449 if (i < LENGTH(tags)) {
450 click = ClkTagBar;
451 arg.ui = 1 << i;
452 } else if (ev->x < x + blw)
453 click = ClkLtSymbol;
454 else if (ev->x > selmon->ww - TEXTW(stext))
455 click = ClkStatusText;
456 else
457 click = ClkWinTitle;
458 } else if ((c = wintoclient(ev->window))) {
459 focus(c);
460 click = ClkClientWin;
462 for (i = 0; i < LENGTH(buttons); i++)
463 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
464 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
465 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
468 void
469 checkotherwm(void)
471 xerrorxlib = XSetErrorHandler(xerrorstart);
472 /* this causes an error if some other window manager is running */
473 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
474 XSync(dpy, False);
475 XSetErrorHandler(xerror);
476 XSync(dpy, False);
479 void
480 cleanup(void)
482 Arg a = {.ui = ~0};
483 Layout foo = { "", NULL };
484 Monitor *m;
485 size_t i;
487 view(&a);
488 selmon->lt[selmon->sellt] = &foo;
489 for (m = mons; m; m = m->next)
490 while (m->stack)
491 unmanage(m->stack, 0);
492 XUngrabKey(dpy, AnyKey, AnyModifier, root);
493 while (mons)
494 cleanupmon(mons);
495 for (i = 0; i < CurLast; i++)
496 drw_cur_free(drw, cursor[i]);
497 for (i = 0; i < SchemeLast; i++) {
498 drw_clr_free(scheme[i].border);
499 drw_clr_free(scheme[i].bg);
500 drw_clr_free(scheme[i].fg);
502 drw_free(drw);
503 XSync(dpy, False);
504 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
505 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
508 void
509 cleanupmon(Monitor *mon)
511 Monitor *m;
513 if (mon == mons)
514 mons = mons->next;
515 else {
516 for (m = mons; m && m->next != mon; m = m->next);
517 m->next = mon->next;
519 XUnmapWindow(dpy, mon->barwin);
520 XDestroyWindow(dpy, mon->barwin);
521 free(mon);
524 void
525 clearurgent(Client *c)
527 XWMHints *wmh;
529 c->isurgent = 0;
530 if (!(wmh = XGetWMHints(dpy, c->win)))
531 return;
532 wmh->flags &= ~XUrgencyHint;
533 XSetWMHints(dpy, c->win, wmh);
534 XFree(wmh);
537 void
538 clientmessage(XEvent *e)
540 XClientMessageEvent *cme = &e->xclient;
541 Client *c = wintoclient(cme->window);
543 if (!c)
544 return;
545 if (cme->message_type == netatom[NetWMState]) {
546 if (cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
547 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
548 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
549 } else if (cme->message_type == netatom[NetActiveWindow]) {
550 if (!ISVISIBLE(c)) {
551 c->mon->seltags ^= 1;
552 c->mon->tagset[c->mon->seltags] = c->tags;
554 pop(c);
558 void
559 configure(Client *c)
561 XConfigureEvent ce;
563 ce.type = ConfigureNotify;
564 ce.display = dpy;
565 ce.event = c->win;
566 ce.window = c->win;
567 ce.x = c->x;
568 ce.y = c->y;
569 ce.width = c->w;
570 ce.height = c->h;
571 ce.border_width = c->bw;
572 ce.above = None;
573 ce.override_redirect = False;
574 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
577 void
578 configurenotify(XEvent *e)
580 Monitor *m;
581 XConfigureEvent *ev = &e->xconfigure;
582 int dirty;
584 /* TODO: updategeom handling sucks, needs to be simplified */
585 if (ev->window == root) {
586 dirty = (sw != ev->width || sh != ev->height);
587 sw = ev->width;
588 sh = ev->height;
589 if (updategeom() || dirty) {
590 drw_resize(drw, sw, bh);
591 updatebars();
592 for (m = mons; m; m = m->next)
593 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
594 focus(NULL);
595 arrange(NULL);
600 void
601 configurerequest(XEvent *e)
603 Client *c;
604 Monitor *m;
605 XConfigureRequestEvent *ev = &e->xconfigurerequest;
606 XWindowChanges wc;
608 if ((c = wintoclient(ev->window))) {
609 if (ev->value_mask & CWBorderWidth)
610 c->bw = ev->border_width;
611 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
612 m = c->mon;
613 if (ev->value_mask & CWX) {
614 c->oldx = c->x;
615 c->x = m->mx + ev->x;
617 if (ev->value_mask & CWY) {
618 c->oldy = c->y;
619 c->y = m->my + ev->y;
621 if (ev->value_mask & CWWidth) {
622 c->oldw = c->w;
623 c->w = ev->width;
625 if (ev->value_mask & CWHeight) {
626 c->oldh = c->h;
627 c->h = ev->height;
629 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
630 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
631 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
632 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
633 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
634 configure(c);
635 if (ISVISIBLE(c))
636 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
637 } else
638 configure(c);
639 } else {
640 wc.x = ev->x;
641 wc.y = ev->y;
642 wc.width = ev->width;
643 wc.height = ev->height;
644 wc.border_width = ev->border_width;
645 wc.sibling = ev->above;
646 wc.stack_mode = ev->detail;
647 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
649 XSync(dpy, False);
652 Monitor *
653 createmon(void)
655 Monitor *m;
657 m = ecalloc(1, sizeof(Monitor));
658 m->tagset[0] = m->tagset[1] = 1;
659 m->mfact = mfact;
660 m->nmaster = nmaster;
661 m->showbar = showbar;
662 m->topbar = topbar;
663 m->lt[0] = &layouts[0];
664 m->lt[1] = &layouts[1 % LENGTH(layouts)];
665 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
666 return m;
669 void
670 destroynotify(XEvent *e)
672 Client *c;
673 XDestroyWindowEvent *ev = &e->xdestroywindow;
675 if ((c = wintoclient(ev->window)))
676 unmanage(c, 1);
679 void
680 detach(Client *c)
682 Client **tc;
684 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
685 *tc = c->next;
688 void
689 detachstack(Client *c)
691 Client **tc, *t;
693 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
694 *tc = c->snext;
696 if (c == c->mon->sel) {
697 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
698 c->mon->sel = t;
702 Monitor *
703 dirtomon(int dir)
705 Monitor *m = NULL;
707 if (dir > 0) {
708 if (!(m = selmon->next))
709 m = mons;
710 } else if (selmon == mons)
711 for (m = mons; m->next; m = m->next);
712 else
713 for (m = mons; m->next != selmon; m = m->next);
714 return m;
717 void
718 drawbar(Monitor *m)
720 int x, xx, w, dx;
721 unsigned int i, occ = 0, urg = 0;
722 Client *c;
724 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
726 for (c = m->clients; c; c = c->next) {
727 occ |= c->tags;
728 if (c->isurgent)
729 urg |= c->tags;
731 x = 0;
732 for (i = 0; i < LENGTH(tags); i++) {
733 /* do not draw vacant tags */
734 if(!(occ & 1 << i || m->tagset[m->seltags] & 1 << i))
735 continue;
736 w = TEXTW(tags[i]);
737 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
738 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
739 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
740 0, urg & 1 << i);
741 x += w;
743 w = blw = TEXTW(m->ltsymbol);
744 drw_setscheme(drw, &scheme[SchemeNorm]);
745 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
746 x += w;
747 xx = x;
748 if (m == selmon) { /* status is only drawn on selected monitor */
749 w = TEXTW(stext);
750 x = m->ww - w;
751 if (x < xx) {
752 x = xx;
753 w = m->ww - xx;
755 drw_text(drw, x, 0, w, bh, stext, 0);
756 } else
757 x = m->ww;
758 if ((w = x - xx) > bh) {
759 x = xx;
760 if (m->sel) {
761 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
762 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
763 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
764 } else {
765 drw_setscheme(drw, &scheme[SchemeNorm]);
766 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
769 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
772 void
773 drawbars(void)
775 Monitor *m;
777 for (m = mons; m; m = m->next)
778 drawbar(m);
781 void
782 enternotify(XEvent *e)
784 Client *c;
785 Monitor *m;
786 XCrossingEvent *ev = &e->xcrossing;
788 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
789 return;
790 c = wintoclient(ev->window);
791 m = c ? c->mon : wintomon(ev->window);
792 if (m != selmon) {
793 unfocus(selmon->sel, 1);
794 selmon = m;
795 } else if (!c || c == selmon->sel)
796 return;
797 focus(c);
800 void
801 expose(XEvent *e)
803 Monitor *m;
804 XExposeEvent *ev = &e->xexpose;
806 if (ev->count == 0 && (m = wintomon(ev->window)))
807 drawbar(m);
810 void
811 focus(Client *c)
813 if (!c || !ISVISIBLE(c))
814 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
815 /* was if (selmon->sel) */
816 if (selmon->sel && selmon->sel != c)
817 unfocus(selmon->sel, 0);
818 if (c) {
819 if (c->mon != selmon)
820 selmon = c->mon;
821 if (c->isurgent)
822 clearurgent(c);
823 detachstack(c);
824 attachstack(c);
825 grabbuttons(c, 1);
826 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
827 setfocus(c);
828 } else {
829 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
830 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
832 selmon->sel = c;
833 drawbars();
836 /* there are some broken focus acquiring clients */
837 void
838 focusin(XEvent *e)
840 XFocusChangeEvent *ev = &e->xfocus;
842 if (selmon->sel && ev->window != selmon->sel->win)
843 setfocus(selmon->sel);
846 void
847 focusmon(const Arg *arg)
849 Monitor *m;
851 if (!mons->next)
852 return;
853 if ((m = dirtomon(arg->i)) == selmon)
854 return;
855 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
856 in gedit and anjuta */
857 selmon = m;
858 focus(NULL);
861 void
862 focusstack(const Arg *arg)
864 Client *c = NULL, *i;
866 if (!selmon->sel)
867 return;
868 if (arg->i > 0) {
869 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
870 if (!c)
871 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
872 } else {
873 for (i = selmon->clients; i != selmon->sel; i = i->next)
874 if (ISVISIBLE(i))
875 c = i;
876 if (!c)
877 for (; i; i = i->next)
878 if (ISVISIBLE(i))
879 c = i;
881 if (c) {
882 focus(c);
883 restack(selmon);
887 Atom
888 getatomprop(Client *c, Atom prop)
890 int di;
891 unsigned long dl;
892 unsigned char *p = NULL;
893 Atom da, atom = None;
895 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
896 &da, &di, &dl, &dl, &p) == Success && p) {
897 atom = *(Atom *)p;
898 XFree(p);
900 return atom;
904 getrootptr(int *x, int *y)
906 int di;
907 unsigned int dui;
908 Window dummy;
910 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
913 long
914 getstate(Window w)
916 int format;
917 long result = -1;
918 unsigned char *p = NULL;
919 unsigned long n, extra;
920 Atom real;
922 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
923 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
924 return -1;
925 if (n != 0)
926 result = *p;
927 XFree(p);
928 return result;
932 gettextprop(Window w, Atom atom, char *text, unsigned int size)
934 char **list = NULL;
935 int n;
936 XTextProperty name;
938 if (!text || size == 0)
939 return 0;
940 text[0] = '\0';
941 XGetTextProperty(dpy, w, &name, atom);
942 if (!name.nitems)
943 return 0;
944 if (name.encoding == XA_STRING)
945 strncpy(text, (char *)name.value, size - 1);
946 else {
947 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
948 strncpy(text, *list, size - 1);
949 XFreeStringList(list);
952 text[size - 1] = '\0';
953 XFree(name.value);
954 return 1;
957 void
958 grabbuttons(Client *c, int focused)
960 updatenumlockmask();
962 unsigned int i, j;
963 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
964 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
965 if (focused) {
966 for (i = 0; i < LENGTH(buttons); i++)
967 if (buttons[i].click == ClkClientWin)
968 for (j = 0; j < LENGTH(modifiers); j++)
969 XGrabButton(dpy, buttons[i].button,
970 buttons[i].mask | modifiers[j],
971 c->win, False, BUTTONMASK,
972 GrabModeAsync, GrabModeSync, None, None);
973 } else
974 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
975 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
979 void
980 grabkeys(void)
982 updatenumlockmask();
984 unsigned int i, j;
985 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
986 KeyCode code;
988 XUngrabKey(dpy, AnyKey, AnyModifier, root);
989 for (i = 0; i < LENGTH(keys); i++)
990 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
991 for (j = 0; j < LENGTH(modifiers); j++)
992 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
993 True, GrabModeAsync, GrabModeAsync);
997 void
998 incnmaster(const Arg *arg)
1000 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1001 arrange(selmon);
1004 #ifdef XINERAMA
1005 static int
1006 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1008 while (n--)
1009 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1010 && unique[n].width == info->width && unique[n].height == info->height)
1011 return 0;
1012 return 1;
1014 #endif /* XINERAMA */
1016 void
1017 keypress(XEvent *e)
1019 unsigned int i;
1020 KeySym keysym;
1021 XKeyEvent *ev;
1023 ev = &e->xkey;
1024 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1025 for (i = 0; i < LENGTH(keys); i++)
1026 if (keysym == keys[i].keysym
1027 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1028 && keys[i].func)
1029 keys[i].func(&(keys[i].arg));
1032 void
1033 killclient(const Arg *arg)
1035 if (!selmon->sel)
1036 return;
1037 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1038 XGrabServer(dpy);
1039 XSetErrorHandler(xerrordummy);
1040 XSetCloseDownMode(dpy, DestroyAll);
1041 XKillClient(dpy, selmon->sel->win);
1042 XSync(dpy, False);
1043 XSetErrorHandler(xerror);
1044 XUngrabServer(dpy);
1048 void
1049 manage(Window w, XWindowAttributes *wa)
1051 Client *c, *t = NULL;
1052 Window trans = None;
1053 XWindowChanges wc;
1055 c = ecalloc(1, sizeof(Client));
1056 c->win = w;
1057 updatetitle(c);
1058 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1059 c->mon = t->mon;
1060 c->tags = t->tags;
1061 } else {
1062 c->mon = selmon;
1063 applyrules(c);
1065 /* geometry */
1066 c->x = c->oldx = wa->x;
1067 c->y = c->oldy = wa->y;
1068 c->w = c->oldw = wa->width;
1069 c->h = c->oldh = wa->height;
1070 c->oldbw = wa->border_width;
1072 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1073 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1074 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1075 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1076 c->x = MAX(c->x, c->mon->mx);
1077 /* only fix client y-offset, if the client center might cover the bar */
1078 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1079 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1080 c->bw = borderpx;
1082 wc.border_width = c->bw;
1083 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1084 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1085 configure(c); /* propagates border_width, if size doesn't change */
1086 updatewindowtype(c);
1087 updatesizehints(c);
1088 updatewmhints(c);
1089 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1090 grabbuttons(c, 0);
1091 if (!c->isfloating)
1092 c->isfloating = c->oldstate = trans != None || c->isfixed;
1093 if (c->isfloating)
1094 XRaiseWindow(dpy, c->win);
1095 attach(c);
1096 attachstack(c);
1097 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1098 (unsigned char *) &(c->win), 1);
1099 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1100 setclientstate(c, NormalState);
1101 if (c->mon == selmon)
1102 unfocus(selmon->sel, 0);
1103 c->mon->sel = c;
1104 arrange(c->mon);
1105 XMapWindow(dpy, c->win);
1106 focus(NULL);
1109 void
1110 mappingnotify(XEvent *e)
1112 XMappingEvent *ev = &e->xmapping;
1114 XRefreshKeyboardMapping(ev);
1115 if (ev->request == MappingKeyboard)
1116 grabkeys();
1119 void
1120 maprequest(XEvent *e)
1122 static XWindowAttributes wa;
1123 XMapRequestEvent *ev = &e->xmaprequest;
1125 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1126 return;
1127 if (wa.override_redirect)
1128 return;
1129 if (!wintoclient(ev->window))
1130 manage(ev->window, &wa);
1133 void
1134 monocle(Monitor *m)
1136 unsigned int n = 0;
1137 Client *c;
1139 for (c = m->clients; c; c = c->next)
1140 if (ISVISIBLE(c))
1141 n++;
1142 if (n > 0) /* override layout symbol */
1143 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1144 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1145 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1148 void
1149 motionnotify(XEvent *e)
1151 static Monitor *mon = NULL;
1152 Monitor *m;
1153 XMotionEvent *ev = &e->xmotion;
1155 if (ev->window != root)
1156 return;
1157 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1158 unfocus(selmon->sel, 1);
1159 selmon = m;
1160 focus(NULL);
1162 mon = m;
1165 void
1166 movemouse(const Arg *arg)
1168 int x, y, ocx, ocy, nx, ny;
1169 Client *c;
1170 Monitor *m;
1171 XEvent ev;
1172 Time lasttime = 0;
1174 if (!(c = selmon->sel))
1175 return;
1176 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1177 return;
1178 restack(selmon);
1179 ocx = c->x;
1180 ocy = c->y;
1181 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1182 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1183 return;
1184 if (!getrootptr(&x, &y))
1185 return;
1186 do {
1187 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1188 switch(ev.type) {
1189 case ConfigureRequest:
1190 case Expose:
1191 case MapRequest:
1192 handler[ev.type](&ev);
1193 break;
1194 case MotionNotify:
1195 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1196 continue;
1197 lasttime = ev.xmotion.time;
1199 nx = ocx + (ev.xmotion.x - x);
1200 ny = ocy + (ev.xmotion.y - y);
1201 if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1202 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1203 if (abs(selmon->wx - nx) < snap)
1204 nx = selmon->wx;
1205 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1206 nx = selmon->wx + selmon->ww - WIDTH(c);
1207 if (abs(selmon->wy - ny) < snap)
1208 ny = selmon->wy;
1209 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1210 ny = selmon->wy + selmon->wh - HEIGHT(c);
1211 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1212 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1213 togglefloating(NULL);
1215 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1216 resize(c, nx, ny, c->w, c->h, 1);
1217 break;
1219 } while (ev.type != ButtonRelease);
1220 XUngrabPointer(dpy, CurrentTime);
1221 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1222 sendmon(c, m);
1223 selmon = m;
1224 focus(NULL);
1228 Client *
1229 nexttiled(Client *c)
1231 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1232 return c;
1235 void
1236 pop(Client *c)
1238 detach(c);
1239 attach(c);
1240 focus(c);
1241 arrange(c->mon);
1244 void
1245 propertynotify(XEvent *e)
1247 Client *c;
1248 Window trans;
1249 XPropertyEvent *ev = &e->xproperty;
1251 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1252 updatestatus();
1253 else if (ev->state == PropertyDelete)
1254 return; /* ignore */
1255 else if ((c = wintoclient(ev->window))) {
1256 switch(ev->atom) {
1257 default: break;
1258 case XA_WM_TRANSIENT_FOR:
1259 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1260 (c->isfloating = (wintoclient(trans)) != NULL))
1261 arrange(c->mon);
1262 break;
1263 case XA_WM_NORMAL_HINTS:
1264 updatesizehints(c);
1265 break;
1266 case XA_WM_HINTS:
1267 updatewmhints(c);
1268 drawbars();
1269 break;
1271 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1272 updatetitle(c);
1273 if (c == c->mon->sel)
1274 drawbar(c->mon);
1276 if (ev->atom == netatom[NetWMWindowType])
1277 updatewindowtype(c);
1281 void
1282 quit(const Arg *arg)
1284 running = 0;
1287 Monitor *
1288 recttomon(int x, int y, int w, int h)
1290 Monitor *m, *r = selmon;
1291 int a, area = 0;
1293 for (m = mons; m; m = m->next)
1294 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1295 area = a;
1296 r = m;
1298 return r;
1301 void
1302 resize(Client *c, int x, int y, int w, int h, int interact)
1304 if (applysizehints(c, &x, &y, &w, &h, interact))
1305 resizeclient(c, x, y, w, h);
1308 void
1309 resizeclient(Client *c, int x, int y, int w, int h)
1311 XWindowChanges wc;
1313 c->oldx = c->x; c->x = wc.x = x;
1314 c->oldy = c->y; c->y = wc.y = y;
1315 c->oldw = c->w; c->w = wc.width = w;
1316 c->oldh = c->h; c->h = wc.height = h;
1317 wc.border_width = c->bw;
1318 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1319 configure(c);
1320 XSync(dpy, False);
1323 void
1324 resizemouse(const Arg *arg)
1326 int ocx, ocy, nw, nh;
1327 Client *c;
1328 Monitor *m;
1329 XEvent ev;
1330 Time lasttime = 0;
1332 if (!(c = selmon->sel))
1333 return;
1334 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1335 return;
1336 restack(selmon);
1337 ocx = c->x;
1338 ocy = c->y;
1339 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1340 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1341 return;
1342 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1343 do {
1344 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1345 switch(ev.type) {
1346 case ConfigureRequest:
1347 case Expose:
1348 case MapRequest:
1349 handler[ev.type](&ev);
1350 break;
1351 case MotionNotify:
1352 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1353 continue;
1354 lasttime = ev.xmotion.time;
1356 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1357 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1358 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1359 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1361 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1362 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1363 togglefloating(NULL);
1365 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1366 resize(c, c->x, c->y, nw, nh, 1);
1367 break;
1369 } while (ev.type != ButtonRelease);
1370 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1371 XUngrabPointer(dpy, CurrentTime);
1372 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1373 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1374 sendmon(c, m);
1375 selmon = m;
1376 focus(NULL);
1380 void
1381 restack(Monitor *m)
1383 Client *c;
1384 XEvent ev;
1385 XWindowChanges wc;
1387 drawbar(m);
1388 if (!m->sel)
1389 return;
1390 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1391 XRaiseWindow(dpy, m->sel->win);
1392 if (m->lt[m->sellt]->arrange) {
1393 wc.stack_mode = Below;
1394 wc.sibling = m->barwin;
1395 for (c = m->stack; c; c = c->snext)
1396 if (!c->isfloating && ISVISIBLE(c)) {
1397 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1398 wc.sibling = c->win;
1401 XSync(dpy, False);
1402 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1405 void
1406 run(void)
1408 XEvent ev;
1409 /* main event loop */
1410 XSync(dpy, False);
1411 while (running && !XNextEvent(dpy, &ev))
1412 if (handler[ev.type])
1413 handler[ev.type](&ev); /* call handler */
1416 void
1417 scan(void)
1419 unsigned int i, num;
1420 Window d1, d2, *wins = NULL;
1421 XWindowAttributes wa;
1423 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1424 for (i = 0; i < num; i++) {
1425 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1426 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1427 continue;
1428 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1429 manage(wins[i], &wa);
1431 for (i = 0; i < num; i++) { /* now the transients */
1432 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1433 continue;
1434 if (XGetTransientForHint(dpy, wins[i], &d1)
1435 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1436 manage(wins[i], &wa);
1438 if (wins)
1439 XFree(wins);
1443 void
1444 sendmon(Client *c, Monitor *m)
1446 if (c->mon == m)
1447 return;
1448 unfocus(c, 1);
1449 detach(c);
1450 detachstack(c);
1451 c->mon = m;
1452 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1453 attach(c);
1454 attachstack(c);
1455 focus(NULL);
1456 arrange(NULL);
1459 void
1460 setclientstate(Client *c, long state)
1462 long data[] = { state, None };
1464 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1465 PropModeReplace, (unsigned char *)data, 2);
1469 sendevent(Client *c, Atom proto)
1471 int n;
1472 Atom *protocols;
1473 int exists = 0;
1474 XEvent ev;
1476 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1477 while (!exists && n--)
1478 exists = protocols[n] == proto;
1479 XFree(protocols);
1481 if (exists) {
1482 ev.type = ClientMessage;
1483 ev.xclient.window = c->win;
1484 ev.xclient.message_type = wmatom[WMProtocols];
1485 ev.xclient.format = 32;
1486 ev.xclient.data.l[0] = proto;
1487 ev.xclient.data.l[1] = CurrentTime;
1488 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1490 return exists;
1493 void
1494 setfocus(Client *c)
1496 if (!c->neverfocus) {
1497 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1498 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1499 XA_WINDOW, 32, PropModeReplace,
1500 (unsigned char *) &(c->win), 1);
1502 sendevent(c, wmatom[WMTakeFocus]);
1505 void
1506 setfullscreen(Client *c, int fullscreen)
1508 if (fullscreen && !c->isfullscreen) {
1509 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1510 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1511 c->isfullscreen = 1;
1512 c->oldstate = c->isfloating;
1513 c->oldbw = c->bw;
1514 c->bw = 0;
1515 c->isfloating = 1;
1516 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1517 XRaiseWindow(dpy, c->win);
1518 } else if (!fullscreen && c->isfullscreen){
1519 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1520 PropModeReplace, (unsigned char*)0, 0);
1521 c->isfullscreen = 0;
1522 c->isfloating = c->oldstate;
1523 c->bw = c->oldbw;
1524 c->x = c->oldx;
1525 c->y = c->oldy;
1526 c->w = c->oldw;
1527 c->h = c->oldh;
1528 resizeclient(c, c->x, c->y, c->w, c->h);
1529 arrange(c->mon);
1533 void
1534 setlayout(const Arg *arg)
1536 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1537 selmon->sellt ^= 1;
1538 if (arg && arg->v)
1539 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1540 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1541 if (selmon->sel)
1542 arrange(selmon);
1543 else
1544 drawbar(selmon);
1547 /* arg > 1.0 will set mfact absolutly */
1548 void
1549 setmfact(const Arg *arg)
1551 float f;
1553 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1554 return;
1555 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1556 if (f < 0.1 || f > 0.9)
1557 return;
1558 selmon->mfact = f;
1559 arrange(selmon);
1562 void
1563 setup(void)
1565 XSetWindowAttributes wa;
1567 /* clean up any zombies immediately */
1568 sigchld(0);
1570 /* init screen */
1571 screen = DefaultScreen(dpy);
1572 sw = DisplayWidth(dpy, screen);
1573 sh = DisplayHeight(dpy, screen);
1574 root = RootWindow(dpy, screen);
1575 xinitvisual();
1576 drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
1577 drw_load_fonts(drw, fonts, LENGTH(fonts));
1578 if (!drw->fontcount)
1579 die("no fonts could be loaded.\n");
1580 bh = drw->fonts[0]->h + 2;
1581 updategeom();
1582 /* init atoms */
1583 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1584 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1585 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1586 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1587 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1588 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1589 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1590 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1591 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1592 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1593 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1594 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1595 /* init cursors */
1596 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1597 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1598 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1599 /* init appearance */
1600 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor, borderalpha);
1601 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor, baralpha);
1602 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor, OPAQUE);
1603 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor, borderalpha);
1604 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor, baralpha);
1605 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor, OPAQUE);
1606 /* init bars */
1607 updatebars();
1608 updatestatus();
1609 /* EWMH support per view */
1610 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1611 PropModeReplace, (unsigned char *) netatom, NetLast);
1612 XDeleteProperty(dpy, root, netatom[NetClientList]);
1613 /* select for events */
1614 wa.cursor = cursor[CurNormal]->cursor;
1615 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1616 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1617 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1618 XSelectInput(dpy, root, wa.event_mask);
1619 grabkeys();
1620 focus(NULL);
1623 void
1624 showhide(Client *c)
1626 if (!c)
1627 return;
1628 if (ISVISIBLE(c)) {
1629 /* show clients top down */
1630 XMoveWindow(dpy, c->win, c->x, c->y);
1631 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1632 resize(c, c->x, c->y, c->w, c->h, 0);
1633 showhide(c->snext);
1634 } else {
1635 /* hide clients bottom up */
1636 showhide(c->snext);
1637 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1641 void
1642 sigchld(int unused)
1644 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1645 die("can't install SIGCHLD handler:");
1646 while (0 < waitpid(-1, NULL, WNOHANG));
1649 void
1650 spawn(const Arg *arg)
1652 if (arg->v == dmenucmd)
1653 dmenumon[0] = '0' + selmon->num;
1654 if (fork() == 0) {
1655 if (dpy)
1656 close(ConnectionNumber(dpy));
1657 setsid();
1658 execvp(((char **)arg->v)[0], (char **)arg->v);
1659 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1660 perror(" failed");
1661 exit(EXIT_SUCCESS);
1665 void
1666 tag(const Arg *arg)
1668 if (selmon->sel && arg->ui & TAGMASK) {
1669 selmon->sel->tags = arg->ui & TAGMASK;
1670 focus(NULL);
1671 arrange(selmon);
1675 void
1676 tagmon(const Arg *arg)
1678 if (!selmon->sel || !mons->next)
1679 return;
1680 sendmon(selmon->sel, dirtomon(arg->i));
1683 void
1684 tile(Monitor *m)
1686 unsigned int i, n, h, mw, my, ty;
1687 Client *c;
1689 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1690 if (n == 0)
1691 return;
1693 if (n > m->nmaster)
1694 mw = m->nmaster ? m->ww * m->mfact : 0;
1695 else
1696 mw = m->ww;
1697 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1698 if (i < m->nmaster) {
1699 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1700 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1701 my += HEIGHT(c);
1702 } else {
1703 h = (m->wh - ty) / (n - i);
1704 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1705 ty += HEIGHT(c);
1709 void
1710 togglebar(const Arg *arg)
1712 selmon->showbar = !selmon->showbar;
1713 updatebarpos(selmon);
1714 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1715 arrange(selmon);
1718 void
1719 togglefloating(const Arg *arg)
1721 if (!selmon->sel)
1722 return;
1723 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1724 return;
1725 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1726 if (selmon->sel->isfloating)
1727 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1728 selmon->sel->w, selmon->sel->h, 0);
1729 arrange(selmon);
1732 void
1733 toggletag(const Arg *arg)
1735 unsigned int newtags;
1737 if (!selmon->sel)
1738 return;
1739 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1740 if (newtags) {
1741 selmon->sel->tags = newtags;
1742 focus(NULL);
1743 arrange(selmon);
1747 void
1748 toggleview(const Arg *arg)
1750 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1752 if (newtagset) {
1753 selmon->tagset[selmon->seltags] = newtagset;
1754 focus(NULL);
1755 arrange(selmon);
1759 void
1760 unfocus(Client *c, int setfocus)
1762 if (!c)
1763 return;
1764 grabbuttons(c, 0);
1765 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1766 if (setfocus) {
1767 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1768 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1772 void
1773 unmanage(Client *c, int destroyed)
1775 Monitor *m = c->mon;
1776 XWindowChanges wc;
1778 /* The server grab construct avoids race conditions. */
1779 detach(c);
1780 detachstack(c);
1781 if (!destroyed) {
1782 wc.border_width = c->oldbw;
1783 XGrabServer(dpy);
1784 XSetErrorHandler(xerrordummy);
1785 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1786 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1787 setclientstate(c, WithdrawnState);
1788 XSync(dpy, False);
1789 XSetErrorHandler(xerror);
1790 XUngrabServer(dpy);
1792 free(c);
1793 focus(NULL);
1794 updateclientlist();
1795 arrange(m);
1798 void
1799 unmapnotify(XEvent *e)
1801 Client *c;
1802 XUnmapEvent *ev = &e->xunmap;
1804 if ((c = wintoclient(ev->window))) {
1805 if (ev->send_event)
1806 setclientstate(c, WithdrawnState);
1807 else
1808 unmanage(c, 0);
1812 void
1813 updatebars(void)
1815 Monitor *m;
1816 XSetWindowAttributes wa = {
1817 .override_redirect = True,
1818 .background_pixel = 0,
1819 .border_pixel = 0,
1820 .colormap = cmap,
1821 .event_mask = ButtonPressMask|ExposureMask
1823 for (m = mons; m; m = m->next) {
1824 if (m->barwin)
1825 continue;
1826 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
1827 InputOutput, visual,
1828 CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
1829 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1830 XMapRaised(dpy, m->barwin);
1834 void
1835 updatebarpos(Monitor *m)
1837 m->wy = m->my;
1838 m->wh = m->mh;
1839 if (m->showbar) {
1840 m->wh -= bh;
1841 m->by = m->topbar ? m->wy : m->wy + m->wh;
1842 m->wy = m->topbar ? m->wy + bh : m->wy;
1843 } else
1844 m->by = -bh;
1847 void
1848 updateclientlist()
1850 Client *c;
1851 Monitor *m;
1853 XDeleteProperty(dpy, root, netatom[NetClientList]);
1854 for (m = mons; m; m = m->next)
1855 for (c = m->clients; c; c = c->next)
1856 XChangeProperty(dpy, root, netatom[NetClientList],
1857 XA_WINDOW, 32, PropModeAppend,
1858 (unsigned char *) &(c->win), 1);
1862 updategeom(void)
1864 int dirty = 0;
1866 #ifdef XINERAMA
1867 if (XineramaIsActive(dpy)) {
1868 int i, j, n, nn;
1869 Client *c;
1870 Monitor *m;
1871 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1872 XineramaScreenInfo *unique = NULL;
1874 for (n = 0, m = mons; m; m = m->next, n++);
1875 /* only consider unique geometries as separate screens */
1876 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1877 for (i = 0, j = 0; i < nn; i++)
1878 if (isuniquegeom(unique, j, &info[i]))
1879 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1880 XFree(info);
1881 nn = j;
1882 if (n <= nn) {
1883 for (i = 0; i < (nn - n); i++) { /* new monitors available */
1884 for (m = mons; m && m->next; m = m->next);
1885 if (m)
1886 m->next = createmon();
1887 else
1888 mons = createmon();
1890 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1891 if (i >= n
1892 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1893 || unique[i].width != m->mw || unique[i].height != m->mh))
1895 dirty = 1;
1896 m->num = i;
1897 m->mx = m->wx = unique[i].x_org;
1898 m->my = m->wy = unique[i].y_org;
1899 m->mw = m->ww = unique[i].width;
1900 m->mh = m->wh = unique[i].height;
1901 updatebarpos(m);
1903 } else {
1904 /* less monitors available nn < n */
1905 for (i = nn; i < n; i++) {
1906 for (m = mons; m && m->next; m = m->next);
1907 while (m->clients) {
1908 dirty = 1;
1909 c = m->clients;
1910 m->clients = c->next;
1911 detachstack(c);
1912 c->mon = mons;
1913 attach(c);
1914 attachstack(c);
1916 if (m == selmon)
1917 selmon = mons;
1918 cleanupmon(m);
1921 free(unique);
1922 } else
1923 #endif /* XINERAMA */
1924 /* default monitor setup */
1926 if (!mons)
1927 mons = createmon();
1928 if (mons->mw != sw || mons->mh != sh) {
1929 dirty = 1;
1930 mons->mw = mons->ww = sw;
1931 mons->mh = mons->wh = sh;
1932 updatebarpos(mons);
1935 if (dirty) {
1936 selmon = mons;
1937 selmon = wintomon(root);
1939 return dirty;
1942 void
1943 updatenumlockmask(void)
1945 unsigned int i, j;
1946 XModifierKeymap *modmap;
1948 numlockmask = 0;
1949 modmap = XGetModifierMapping(dpy);
1950 for (i = 0; i < 8; i++)
1951 for (j = 0; j < modmap->max_keypermod; j++)
1952 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1953 == XKeysymToKeycode(dpy, XK_Num_Lock))
1954 numlockmask = (1 << i);
1955 XFreeModifiermap(modmap);
1958 void
1959 updatesizehints(Client *c)
1961 long msize;
1962 XSizeHints size;
1964 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1965 /* size is uninitialized, ensure that size.flags aren't used */
1966 size.flags = PSize;
1967 if (size.flags & PBaseSize) {
1968 c->basew = size.base_width;
1969 c->baseh = size.base_height;
1970 } else if (size.flags & PMinSize) {
1971 c->basew = size.min_width;
1972 c->baseh = size.min_height;
1973 } else
1974 c->basew = c->baseh = 0;
1975 if (size.flags & PResizeInc) {
1976 c->incw = size.width_inc;
1977 c->inch = size.height_inc;
1978 } else
1979 c->incw = c->inch = 0;
1980 if (size.flags & PMaxSize) {
1981 c->maxw = size.max_width;
1982 c->maxh = size.max_height;
1983 } else
1984 c->maxw = c->maxh = 0;
1985 if (size.flags & PMinSize) {
1986 c->minw = size.min_width;
1987 c->minh = size.min_height;
1988 } else if (size.flags & PBaseSize) {
1989 c->minw = size.base_width;
1990 c->minh = size.base_height;
1991 } else
1992 c->minw = c->minh = 0;
1993 if (size.flags & PAspect) {
1994 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
1995 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
1996 } else
1997 c->maxa = c->mina = 0.0;
1998 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
1999 && c->maxw == c->minw && c->maxh == c->minh);
2002 void
2003 updatetitle(Client *c)
2005 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2006 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2007 if (c->name[0] == '\0') /* hack to mark broken clients */
2008 strcpy(c->name, broken);
2011 void
2012 updatestatus(void)
2014 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2015 strcpy(stext, "dwm-"VERSION);
2016 drawbar(selmon);
2019 void
2020 updatewindowtype(Client *c)
2022 Atom state = getatomprop(c, netatom[NetWMState]);
2023 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2025 if (state == netatom[NetWMFullscreen])
2026 setfullscreen(c, 1);
2027 if (wtype == netatom[NetWMWindowTypeDialog])
2028 c->isfloating = 1;
2031 void
2032 updatewmhints(Client *c)
2034 XWMHints *wmh;
2036 if ((wmh = XGetWMHints(dpy, c->win))) {
2037 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2038 wmh->flags &= ~XUrgencyHint;
2039 XSetWMHints(dpy, c->win, wmh);
2040 } else
2041 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2042 if (wmh->flags & InputHint)
2043 c->neverfocus = !wmh->input;
2044 else
2045 c->neverfocus = 0;
2046 XFree(wmh);
2050 void
2051 view(const Arg *arg)
2053 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2054 return;
2055 selmon->seltags ^= 1; /* toggle sel tagset */
2056 if (arg->ui & TAGMASK)
2057 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2058 focus(NULL);
2059 arrange(selmon);
2062 Client *
2063 wintoclient(Window w)
2065 Client *c;
2066 Monitor *m;
2068 for (m = mons; m; m = m->next)
2069 for (c = m->clients; c; c = c->next)
2070 if (c->win == w)
2071 return c;
2072 return NULL;
2075 Monitor *
2076 wintomon(Window w)
2078 int x, y;
2079 Client *c;
2080 Monitor *m;
2082 if (w == root && getrootptr(&x, &y))
2083 return recttomon(x, y, 1, 1);
2084 for (m = mons; m; m = m->next)
2085 if (w == m->barwin)
2086 return m;
2087 if ((c = wintoclient(w)))
2088 return c->mon;
2089 return selmon;
2092 /* There's no way to check accesses to destroyed windows, thus those cases are
2093 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2094 * default error handler, which may call exit. */
2096 xerror(Display *dpy, XErrorEvent *ee)
2098 if (ee->error_code == BadWindow
2099 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2100 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2101 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2102 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2103 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2104 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2105 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2106 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2107 return 0;
2108 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2109 ee->request_code, ee->error_code);
2110 return xerrorxlib(dpy, ee); /* may call exit */
2114 xerrordummy(Display *dpy, XErrorEvent *ee)
2116 return 0;
2119 /* Startup Error handler to check if another window manager
2120 * is already running. */
2122 xerrorstart(Display *dpy, XErrorEvent *ee)
2124 die("dwm: another window manager is already running\n");
2125 return -1;
2128 void
2129 xinitvisual()
2131 XVisualInfo *infos;
2132 XRenderPictFormat *fmt;
2133 int nitems;
2134 int i;
2136 XVisualInfo tpl = {
2137 .screen = screen,
2138 .depth = 32,
2139 .class = TrueColor
2141 long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
2143 infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
2144 visual = NULL;
2145 for(i = 0; i < nitems; i ++) {
2146 fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
2147 if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
2148 visual = infos[i].visual;
2149 depth = infos[i].depth;
2150 cmap = XCreateColormap(dpy, root, visual, AllocNone);
2151 useargb = 1;
2152 break;
2156 XFree(infos);
2158 if (! visual) {
2159 visual = DefaultVisual(dpy, screen);
2160 depth = DefaultDepth(dpy, screen);
2161 cmap = DefaultColormap(dpy, screen);
2165 void
2166 zoom(const Arg *arg)
2168 Client *c = selmon->sel;
2170 if (!selmon->lt[selmon->sellt]->arrange
2171 || (selmon->sel && selmon->sel->isfloating))
2172 return;
2173 if (c == nexttiled(selmon->clients))
2174 if (!c || !(c = nexttiled(c->next)))
2175 return;
2176 pop(c);
2180 main(int argc, char *argv[])
2182 if (argc == 2 && !strcmp("-v", argv[1]))
2183 die("dwm-"VERSION "\n");
2184 else if (argc != 1)
2185 die("usage: dwm [-v]\n");
2186 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2187 fputs("warning: no locale support\n", stderr);
2188 if (!(dpy = XOpenDisplay(NULL)))
2189 die("dwm: cannot open display\n");
2190 checkotherwm();
2191 setup();
2192 scan();
2193 run();
2194 cleanup();
2195 XCloseDisplay(dpy);
2196 return EXIT_SUCCESS;