various configuration
[azarus-dwm.git] / dwm.c
bloba491276f4befe828a8b81164f3ffea6a4bb9b312
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 + gappx)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw + gappx)
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;
441 x += TEXTW(tags[i]);
442 while (ev->x >= x && ++i < LENGTH(tags));
443 if (i < LENGTH(tags)) {
444 click = ClkTagBar;
445 arg.ui = 1 << i;
446 } else if (ev->x < x + blw)
447 click = ClkLtSymbol;
448 else if (ev->x > selmon->ww - TEXTW(stext))
449 click = ClkStatusText;
450 else
451 click = ClkWinTitle;
452 } else if ((c = wintoclient(ev->window))) {
453 focus(c);
454 click = ClkClientWin;
456 for (i = 0; i < LENGTH(buttons); i++)
457 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
458 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
459 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
462 void
463 checkotherwm(void)
465 xerrorxlib = XSetErrorHandler(xerrorstart);
466 /* this causes an error if some other window manager is running */
467 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
468 XSync(dpy, False);
469 XSetErrorHandler(xerror);
470 XSync(dpy, False);
473 void
474 cleanup(void)
476 Arg a = {.ui = ~0};
477 Layout foo = { "", NULL };
478 Monitor *m;
479 size_t i;
481 view(&a);
482 selmon->lt[selmon->sellt] = &foo;
483 for (m = mons; m; m = m->next)
484 while (m->stack)
485 unmanage(m->stack, 0);
486 XUngrabKey(dpy, AnyKey, AnyModifier, root);
487 while (mons)
488 cleanupmon(mons);
489 for (i = 0; i < CurLast; i++)
490 drw_cur_free(drw, cursor[i]);
491 for (i = 0; i < SchemeLast; i++) {
492 drw_clr_free(scheme[i].border);
493 drw_clr_free(scheme[i].bg);
494 drw_clr_free(scheme[i].fg);
496 drw_free(drw);
497 XSync(dpy, False);
498 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
499 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
502 void
503 cleanupmon(Monitor *mon)
505 Monitor *m;
507 if (mon == mons)
508 mons = mons->next;
509 else {
510 for (m = mons; m && m->next != mon; m = m->next);
511 m->next = mon->next;
513 XUnmapWindow(dpy, mon->barwin);
514 XDestroyWindow(dpy, mon->barwin);
515 free(mon);
518 void
519 clearurgent(Client *c)
521 XWMHints *wmh;
523 c->isurgent = 0;
524 if (!(wmh = XGetWMHints(dpy, c->win)))
525 return;
526 wmh->flags &= ~XUrgencyHint;
527 XSetWMHints(dpy, c->win, wmh);
528 XFree(wmh);
531 void
532 clientmessage(XEvent *e)
534 XClientMessageEvent *cme = &e->xclient;
535 Client *c = wintoclient(cme->window);
537 if (!c)
538 return;
539 if (cme->message_type == netatom[NetWMState]) {
540 if (cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
541 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
542 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
543 } else if (cme->message_type == netatom[NetActiveWindow]) {
544 if (!ISVISIBLE(c)) {
545 c->mon->seltags ^= 1;
546 c->mon->tagset[c->mon->seltags] = c->tags;
548 pop(c);
552 void
553 configure(Client *c)
555 XConfigureEvent ce;
557 ce.type = ConfigureNotify;
558 ce.display = dpy;
559 ce.event = c->win;
560 ce.window = c->win;
561 ce.x = c->x;
562 ce.y = c->y;
563 ce.width = c->w;
564 ce.height = c->h;
565 ce.border_width = c->bw;
566 ce.above = None;
567 ce.override_redirect = False;
568 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
571 void
572 configurenotify(XEvent *e)
574 Monitor *m;
575 XConfigureEvent *ev = &e->xconfigure;
576 int dirty;
578 /* TODO: updategeom handling sucks, needs to be simplified */
579 if (ev->window == root) {
580 dirty = (sw != ev->width || sh != ev->height);
581 sw = ev->width;
582 sh = ev->height;
583 if (updategeom() || dirty) {
584 drw_resize(drw, sw, bh);
585 updatebars();
586 for (m = mons; m; m = m->next)
587 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
588 focus(NULL);
589 arrange(NULL);
594 void
595 configurerequest(XEvent *e)
597 Client *c;
598 Monitor *m;
599 XConfigureRequestEvent *ev = &e->xconfigurerequest;
600 XWindowChanges wc;
602 if ((c = wintoclient(ev->window))) {
603 if (ev->value_mask & CWBorderWidth)
604 c->bw = ev->border_width;
605 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
606 m = c->mon;
607 if (ev->value_mask & CWX) {
608 c->oldx = c->x;
609 c->x = m->mx + ev->x;
611 if (ev->value_mask & CWY) {
612 c->oldy = c->y;
613 c->y = m->my + ev->y;
615 if (ev->value_mask & CWWidth) {
616 c->oldw = c->w;
617 c->w = ev->width;
619 if (ev->value_mask & CWHeight) {
620 c->oldh = c->h;
621 c->h = ev->height;
623 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
624 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
625 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
626 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
627 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
628 configure(c);
629 if (ISVISIBLE(c))
630 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
631 } else
632 configure(c);
633 } else {
634 wc.x = ev->x;
635 wc.y = ev->y;
636 wc.width = ev->width;
637 wc.height = ev->height;
638 wc.border_width = ev->border_width;
639 wc.sibling = ev->above;
640 wc.stack_mode = ev->detail;
641 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
643 XSync(dpy, False);
646 Monitor *
647 createmon(void)
649 Monitor *m;
651 m = ecalloc(1, sizeof(Monitor));
652 m->tagset[0] = m->tagset[1] = 1;
653 m->mfact = mfact;
654 m->nmaster = nmaster;
655 m->showbar = showbar;
656 m->topbar = topbar;
657 m->lt[0] = &layouts[0];
658 m->lt[1] = &layouts[1 % LENGTH(layouts)];
659 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
660 return m;
663 void
664 destroynotify(XEvent *e)
666 Client *c;
667 XDestroyWindowEvent *ev = &e->xdestroywindow;
669 if ((c = wintoclient(ev->window)))
670 unmanage(c, 1);
673 void
674 detach(Client *c)
676 Client **tc;
678 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
679 *tc = c->next;
682 void
683 detachstack(Client *c)
685 Client **tc, *t;
687 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
688 *tc = c->snext;
690 if (c == c->mon->sel) {
691 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
692 c->mon->sel = t;
696 Monitor *
697 dirtomon(int dir)
699 Monitor *m = NULL;
701 if (dir > 0) {
702 if (!(m = selmon->next))
703 m = mons;
704 } else if (selmon == mons)
705 for (m = mons; m->next; m = m->next);
706 else
707 for (m = mons; m->next != selmon; m = m->next);
708 return m;
711 void
712 drawbar(Monitor *m)
714 int x, xx, w, dx;
715 unsigned int i, occ = 0, urg = 0;
716 Client *c;
718 dx = (drw->fonts[0]->ascent + drw->fonts[0]->descent + 2) / 4;
720 for (c = m->clients; c; c = c->next) {
721 occ |= c->tags;
722 if (c->isurgent)
723 urg |= c->tags;
725 x = 0;
726 for (i = 0; i < LENGTH(tags); i++) {
727 w = TEXTW(tags[i]);
728 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
729 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
730 drw_rect(drw, x + 1, 1, dx, dx, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
731 occ & 1 << i, urg & 1 << i);
732 x += w;
734 w = blw = TEXTW(m->ltsymbol);
735 drw_setscheme(drw, &scheme[SchemeNorm]);
736 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
737 x += w;
738 xx = x;
739 if (m == selmon) { /* status is only drawn on selected monitor */
740 w = TEXTW(stext);
741 x = m->ww - w;
742 if (x < xx) {
743 x = xx;
744 w = m->ww - xx;
746 drw_text(drw, x, 0, w, bh, stext, 0);
747 } else
748 x = m->ww;
749 if ((w = x - xx) > bh) {
750 x = xx;
751 if (m->sel) {
752 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
753 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
754 drw_rect(drw, x + 1, 1, dx, dx, m->sel->isfixed, m->sel->isfloating, 0);
755 } else {
756 drw_setscheme(drw, &scheme[SchemeNorm]);
757 drw_rect(drw, x, 0, w, bh, 1, 0, 1);
760 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
763 void
764 drawbars(void)
766 Monitor *m;
768 for (m = mons; m; m = m->next)
769 drawbar(m);
772 void
773 enternotify(XEvent *e)
775 Client *c;
776 Monitor *m;
777 XCrossingEvent *ev = &e->xcrossing;
779 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
780 return;
781 c = wintoclient(ev->window);
782 m = c ? c->mon : wintomon(ev->window);
783 if (m != selmon) {
784 unfocus(selmon->sel, 1);
785 selmon = m;
786 } else if (!c || c == selmon->sel)
787 return;
788 focus(c);
791 void
792 expose(XEvent *e)
794 Monitor *m;
795 XExposeEvent *ev = &e->xexpose;
797 if (ev->count == 0 && (m = wintomon(ev->window)))
798 drawbar(m);
801 void
802 focus(Client *c)
804 if (!c || !ISVISIBLE(c))
805 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
806 /* was if (selmon->sel) */
807 if (selmon->sel && selmon->sel != c)
808 unfocus(selmon->sel, 0);
809 if (c) {
810 if (c->mon != selmon)
811 selmon = c->mon;
812 if (c->isurgent)
813 clearurgent(c);
814 detachstack(c);
815 attachstack(c);
816 grabbuttons(c, 1);
817 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->pix);
818 setfocus(c);
819 } else {
820 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
821 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
823 selmon->sel = c;
824 drawbars();
827 /* there are some broken focus acquiring clients */
828 void
829 focusin(XEvent *e)
831 XFocusChangeEvent *ev = &e->xfocus;
833 if (selmon->sel && ev->window != selmon->sel->win)
834 setfocus(selmon->sel);
837 void
838 focusmon(const Arg *arg)
840 Monitor *m;
842 if (!mons->next)
843 return;
844 if ((m = dirtomon(arg->i)) == selmon)
845 return;
846 unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
847 in gedit and anjuta */
848 selmon = m;
849 focus(NULL);
852 void
853 focusstack(const Arg *arg)
855 Client *c = NULL, *i;
857 if (!selmon->sel)
858 return;
859 if (arg->i > 0) {
860 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
861 if (!c)
862 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
863 } else {
864 for (i = selmon->clients; i != selmon->sel; i = i->next)
865 if (ISVISIBLE(i))
866 c = i;
867 if (!c)
868 for (; i; i = i->next)
869 if (ISVISIBLE(i))
870 c = i;
872 if (c) {
873 focus(c);
874 restack(selmon);
878 Atom
879 getatomprop(Client *c, Atom prop)
881 int di;
882 unsigned long dl;
883 unsigned char *p = NULL;
884 Atom da, atom = None;
886 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
887 &da, &di, &dl, &dl, &p) == Success && p) {
888 atom = *(Atom *)p;
889 XFree(p);
891 return atom;
895 getrootptr(int *x, int *y)
897 int di;
898 unsigned int dui;
899 Window dummy;
901 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
904 long
905 getstate(Window w)
907 int format;
908 long result = -1;
909 unsigned char *p = NULL;
910 unsigned long n, extra;
911 Atom real;
913 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
914 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
915 return -1;
916 if (n != 0)
917 result = *p;
918 XFree(p);
919 return result;
923 gettextprop(Window w, Atom atom, char *text, unsigned int size)
925 char **list = NULL;
926 int n;
927 XTextProperty name;
929 if (!text || size == 0)
930 return 0;
931 text[0] = '\0';
932 XGetTextProperty(dpy, w, &name, atom);
933 if (!name.nitems)
934 return 0;
935 if (name.encoding == XA_STRING)
936 strncpy(text, (char *)name.value, size - 1);
937 else {
938 if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
939 strncpy(text, *list, size - 1);
940 XFreeStringList(list);
943 text[size - 1] = '\0';
944 XFree(name.value);
945 return 1;
948 void
949 grabbuttons(Client *c, int focused)
951 updatenumlockmask();
953 unsigned int i, j;
954 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
955 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
956 if (focused) {
957 for (i = 0; i < LENGTH(buttons); i++)
958 if (buttons[i].click == ClkClientWin)
959 for (j = 0; j < LENGTH(modifiers); j++)
960 XGrabButton(dpy, buttons[i].button,
961 buttons[i].mask | modifiers[j],
962 c->win, False, BUTTONMASK,
963 GrabModeAsync, GrabModeSync, None, None);
964 } else
965 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
966 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
970 void
971 grabkeys(void)
973 updatenumlockmask();
975 unsigned int i, j;
976 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
977 KeyCode code;
979 XUngrabKey(dpy, AnyKey, AnyModifier, root);
980 for (i = 0; i < LENGTH(keys); i++)
981 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
982 for (j = 0; j < LENGTH(modifiers); j++)
983 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
984 True, GrabModeAsync, GrabModeAsync);
988 void
989 incnmaster(const Arg *arg)
991 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
992 arrange(selmon);
995 #ifdef XINERAMA
996 static int
997 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
999 while (n--)
1000 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1001 && unique[n].width == info->width && unique[n].height == info->height)
1002 return 0;
1003 return 1;
1005 #endif /* XINERAMA */
1007 void
1008 keypress(XEvent *e)
1010 unsigned int i;
1011 KeySym keysym;
1012 XKeyEvent *ev;
1014 ev = &e->xkey;
1015 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1016 for (i = 0; i < LENGTH(keys); i++)
1017 if (keysym == keys[i].keysym
1018 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1019 && keys[i].func)
1020 keys[i].func(&(keys[i].arg));
1023 void
1024 killclient(const Arg *arg)
1026 if (!selmon->sel)
1027 return;
1028 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1029 XGrabServer(dpy);
1030 XSetErrorHandler(xerrordummy);
1031 XSetCloseDownMode(dpy, DestroyAll);
1032 XKillClient(dpy, selmon->sel->win);
1033 XSync(dpy, False);
1034 XSetErrorHandler(xerror);
1035 XUngrabServer(dpy);
1039 void
1040 manage(Window w, XWindowAttributes *wa)
1042 Client *c, *t = NULL;
1043 Window trans = None;
1044 XWindowChanges wc;
1046 c = ecalloc(1, sizeof(Client));
1047 c->win = w;
1048 updatetitle(c);
1049 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1050 c->mon = t->mon;
1051 c->tags = t->tags;
1052 } else {
1053 c->mon = selmon;
1054 applyrules(c);
1056 /* geometry */
1057 c->x = c->oldx = wa->x;
1058 c->y = c->oldy = wa->y;
1059 c->w = c->oldw = wa->width;
1060 c->h = c->oldh = wa->height;
1061 c->oldbw = wa->border_width;
1063 if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1064 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1065 if (c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1066 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1067 c->x = MAX(c->x, c->mon->mx);
1068 /* only fix client y-offset, if the client center might cover the bar */
1069 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1070 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1071 c->bw = borderpx;
1073 wc.border_width = c->bw;
1074 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1075 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->pix);
1076 configure(c); /* propagates border_width, if size doesn't change */
1077 updatewindowtype(c);
1078 updatesizehints(c);
1079 updatewmhints(c);
1080 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1081 grabbuttons(c, 0);
1082 if (!c->isfloating)
1083 c->isfloating = c->oldstate = trans != None || c->isfixed;
1084 if (c->isfloating)
1085 XRaiseWindow(dpy, c->win);
1086 attach(c);
1087 attachstack(c);
1088 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1089 (unsigned char *) &(c->win), 1);
1090 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1091 setclientstate(c, NormalState);
1092 if (c->mon == selmon)
1093 unfocus(selmon->sel, 0);
1094 c->mon->sel = c;
1095 arrange(c->mon);
1096 XMapWindow(dpy, c->win);
1097 focus(NULL);
1100 void
1101 mappingnotify(XEvent *e)
1103 XMappingEvent *ev = &e->xmapping;
1105 XRefreshKeyboardMapping(ev);
1106 if (ev->request == MappingKeyboard)
1107 grabkeys();
1110 void
1111 maprequest(XEvent *e)
1113 static XWindowAttributes wa;
1114 XMapRequestEvent *ev = &e->xmaprequest;
1116 if (!XGetWindowAttributes(dpy, ev->window, &wa))
1117 return;
1118 if (wa.override_redirect)
1119 return;
1120 if (!wintoclient(ev->window))
1121 manage(ev->window, &wa);
1124 void
1125 monocle(Monitor *m)
1127 unsigned int n = 0;
1128 Client *c;
1130 for (c = m->clients; c; c = c->next)
1131 if (ISVISIBLE(c))
1132 n++;
1133 if (n > 0) /* override layout symbol */
1134 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1135 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1136 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1139 void
1140 motionnotify(XEvent *e)
1142 static Monitor *mon = NULL;
1143 Monitor *m;
1144 XMotionEvent *ev = &e->xmotion;
1146 if (ev->window != root)
1147 return;
1148 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1149 unfocus(selmon->sel, 1);
1150 selmon = m;
1151 focus(NULL);
1153 mon = m;
1156 void
1157 movemouse(const Arg *arg)
1159 int x, y, ocx, ocy, nx, ny;
1160 Client *c;
1161 Monitor *m;
1162 XEvent ev;
1163 Time lasttime = 0;
1165 if (!(c = selmon->sel))
1166 return;
1167 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1168 return;
1169 restack(selmon);
1170 ocx = c->x;
1171 ocy = c->y;
1172 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1173 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1174 return;
1175 if (!getrootptr(&x, &y))
1176 return;
1177 do {
1178 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1179 switch(ev.type) {
1180 case ConfigureRequest:
1181 case Expose:
1182 case MapRequest:
1183 handler[ev.type](&ev);
1184 break;
1185 case MotionNotify:
1186 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1187 continue;
1188 lasttime = ev.xmotion.time;
1190 nx = ocx + (ev.xmotion.x - x);
1191 ny = ocy + (ev.xmotion.y - y);
1192 if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1193 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1194 if (abs(selmon->wx - nx) < snap)
1195 nx = selmon->wx;
1196 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1197 nx = selmon->wx + selmon->ww - WIDTH(c);
1198 if (abs(selmon->wy - ny) < snap)
1199 ny = selmon->wy;
1200 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1201 ny = selmon->wy + selmon->wh - HEIGHT(c);
1202 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1203 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1204 togglefloating(NULL);
1206 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1207 resize(c, nx, ny, c->w, c->h, 1);
1208 break;
1210 } while (ev.type != ButtonRelease);
1211 XUngrabPointer(dpy, CurrentTime);
1212 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1213 sendmon(c, m);
1214 selmon = m;
1215 focus(NULL);
1219 Client *
1220 nexttiled(Client *c)
1222 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1223 return c;
1226 void
1227 pop(Client *c)
1229 detach(c);
1230 attach(c);
1231 focus(c);
1232 arrange(c->mon);
1235 void
1236 propertynotify(XEvent *e)
1238 Client *c;
1239 Window trans;
1240 XPropertyEvent *ev = &e->xproperty;
1242 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1243 updatestatus();
1244 else if (ev->state == PropertyDelete)
1245 return; /* ignore */
1246 else if ((c = wintoclient(ev->window))) {
1247 switch(ev->atom) {
1248 default: break;
1249 case XA_WM_TRANSIENT_FOR:
1250 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1251 (c->isfloating = (wintoclient(trans)) != NULL))
1252 arrange(c->mon);
1253 break;
1254 case XA_WM_NORMAL_HINTS:
1255 updatesizehints(c);
1256 break;
1257 case XA_WM_HINTS:
1258 updatewmhints(c);
1259 drawbars();
1260 break;
1262 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1263 updatetitle(c);
1264 if (c == c->mon->sel)
1265 drawbar(c->mon);
1267 if (ev->atom == netatom[NetWMWindowType])
1268 updatewindowtype(c);
1272 void
1273 quit(const Arg *arg)
1275 running = 0;
1278 Monitor *
1279 recttomon(int x, int y, int w, int h)
1281 Monitor *m, *r = selmon;
1282 int a, area = 0;
1284 for (m = mons; m; m = m->next)
1285 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1286 area = a;
1287 r = m;
1289 return r;
1292 void
1293 resize(Client *c, int x, int y, int w, int h, int interact)
1295 if (applysizehints(c, &x, &y, &w, &h, interact))
1296 resizeclient(c, x, y, w, h);
1299 void
1300 resizeclient(Client *c, int x, int y, int w, int h)
1302 XWindowChanges wc;
1303 unsigned int n;
1304 unsigned int gapoffset;
1305 unsigned int gapincr;
1306 Client *nbc;
1308 wc.border_width = c->bw;
1310 /* Get number of clients for the selected monitor */
1311 for (n = 0, nbc = nexttiled(selmon->clients); nbc; nbc = nexttiled(nbc->next), n++);
1313 /* Do nothing if layout is floating */
1314 if (c->isfloating || selmon->lt[selmon->sellt]->arrange == NULL) {
1315 gapincr = gapoffset = 0;
1316 } else {
1317 /* Remove border and gap if layout is monocle or only one client */
1318 if (selmon->lt[selmon->sellt]->arrange == monocle || n == 1) {
1319 gapoffset = 0;
1320 gapincr = -2 * borderpx;
1321 wc.border_width = 0;
1322 } else {
1323 gapoffset = gappx;
1324 gapincr = 2 * gappx;
1328 c->oldx = c->x; c->x = wc.x = x + gapoffset;
1329 c->oldy = c->y; c->y = wc.y = y + gapoffset;
1330 c->oldw = c->w; c->w = wc.width = w - gapincr;
1331 c->oldh = c->h; c->h = wc.height = h - gapincr;
1333 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1334 configure(c);
1335 XSync(dpy, False);
1338 void
1339 resizemouse(const Arg *arg)
1341 int ocx, ocy, nw, nh;
1342 Client *c;
1343 Monitor *m;
1344 XEvent ev;
1345 Time lasttime = 0;
1347 if (!(c = selmon->sel))
1348 return;
1349 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1350 return;
1351 restack(selmon);
1352 ocx = c->x;
1353 ocy = c->y;
1354 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1355 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1356 return;
1357 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1358 do {
1359 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1360 switch(ev.type) {
1361 case ConfigureRequest:
1362 case Expose:
1363 case MapRequest:
1364 handler[ev.type](&ev);
1365 break;
1366 case MotionNotify:
1367 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1368 continue;
1369 lasttime = ev.xmotion.time;
1371 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1372 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1373 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1374 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1376 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1377 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1378 togglefloating(NULL);
1380 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1381 resize(c, c->x, c->y, nw, nh, 1);
1382 break;
1384 } while (ev.type != ButtonRelease);
1385 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1386 XUngrabPointer(dpy, CurrentTime);
1387 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1388 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1389 sendmon(c, m);
1390 selmon = m;
1391 focus(NULL);
1395 void
1396 restack(Monitor *m)
1398 Client *c;
1399 XEvent ev;
1400 XWindowChanges wc;
1402 drawbar(m);
1403 if (!m->sel)
1404 return;
1405 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1406 XRaiseWindow(dpy, m->sel->win);
1407 if (m->lt[m->sellt]->arrange) {
1408 wc.stack_mode = Below;
1409 wc.sibling = m->barwin;
1410 for (c = m->stack; c; c = c->snext)
1411 if (!c->isfloating && ISVISIBLE(c)) {
1412 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1413 wc.sibling = c->win;
1416 XSync(dpy, False);
1417 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1420 void
1421 run(void)
1423 XEvent ev;
1424 /* main event loop */
1425 XSync(dpy, False);
1426 while (running && !XNextEvent(dpy, &ev))
1427 if (handler[ev.type])
1428 handler[ev.type](&ev); /* call handler */
1431 void
1432 scan(void)
1434 unsigned int i, num;
1435 Window d1, d2, *wins = NULL;
1436 XWindowAttributes wa;
1438 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1439 for (i = 0; i < num; i++) {
1440 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1441 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1442 continue;
1443 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1444 manage(wins[i], &wa);
1446 for (i = 0; i < num; i++) { /* now the transients */
1447 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1448 continue;
1449 if (XGetTransientForHint(dpy, wins[i], &d1)
1450 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1451 manage(wins[i], &wa);
1453 if (wins)
1454 XFree(wins);
1458 void
1459 sendmon(Client *c, Monitor *m)
1461 if (c->mon == m)
1462 return;
1463 unfocus(c, 1);
1464 detach(c);
1465 detachstack(c);
1466 c->mon = m;
1467 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1468 attach(c);
1469 attachstack(c);
1470 focus(NULL);
1471 arrange(NULL);
1474 void
1475 setclientstate(Client *c, long state)
1477 long data[] = { state, None };
1479 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1480 PropModeReplace, (unsigned char *)data, 2);
1484 sendevent(Client *c, Atom proto)
1486 int n;
1487 Atom *protocols;
1488 int exists = 0;
1489 XEvent ev;
1491 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1492 while (!exists && n--)
1493 exists = protocols[n] == proto;
1494 XFree(protocols);
1496 if (exists) {
1497 ev.type = ClientMessage;
1498 ev.xclient.window = c->win;
1499 ev.xclient.message_type = wmatom[WMProtocols];
1500 ev.xclient.format = 32;
1501 ev.xclient.data.l[0] = proto;
1502 ev.xclient.data.l[1] = CurrentTime;
1503 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1505 return exists;
1508 void
1509 setfocus(Client *c)
1511 if (!c->neverfocus) {
1512 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1513 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1514 XA_WINDOW, 32, PropModeReplace,
1515 (unsigned char *) &(c->win), 1);
1517 sendevent(c, wmatom[WMTakeFocus]);
1520 void
1521 setfullscreen(Client *c, int fullscreen)
1523 if (fullscreen && !c->isfullscreen) {
1524 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1525 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1526 c->isfullscreen = 1;
1527 c->oldstate = c->isfloating;
1528 c->oldbw = c->bw;
1529 c->bw = 0;
1530 c->isfloating = 1;
1531 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1532 XRaiseWindow(dpy, c->win);
1533 } else if (!fullscreen && c->isfullscreen){
1534 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1535 PropModeReplace, (unsigned char*)0, 0);
1536 c->isfullscreen = 0;
1537 c->isfloating = c->oldstate;
1538 c->bw = c->oldbw;
1539 c->x = c->oldx;
1540 c->y = c->oldy;
1541 c->w = c->oldw;
1542 c->h = c->oldh;
1543 resizeclient(c, c->x, c->y, c->w, c->h);
1544 arrange(c->mon);
1548 void
1549 setlayout(const Arg *arg)
1551 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1552 selmon->sellt ^= 1;
1553 if (arg && arg->v)
1554 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1555 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1556 if (selmon->sel)
1557 arrange(selmon);
1558 else
1559 drawbar(selmon);
1562 /* arg > 1.0 will set mfact absolutly */
1563 void
1564 setmfact(const Arg *arg)
1566 float f;
1568 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1569 return;
1570 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1571 if (f < 0.1 || f > 0.9)
1572 return;
1573 selmon->mfact = f;
1574 arrange(selmon);
1577 void
1578 setup(void)
1580 XSetWindowAttributes wa;
1582 /* clean up any zombies immediately */
1583 sigchld(0);
1585 /* init screen */
1586 screen = DefaultScreen(dpy);
1587 sw = DisplayWidth(dpy, screen);
1588 sh = DisplayHeight(dpy, screen);
1589 root = RootWindow(dpy, screen);
1590 xinitvisual();
1591 drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
1592 drw_load_fonts(drw, fonts, LENGTH(fonts));
1593 if (!drw->fontcount)
1594 die("no fonts could be loaded.\n");
1595 bh = drw->fonts[0]->h + 2;
1596 updategeom();
1597 /* init atoms */
1598 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1599 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1600 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1601 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1602 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1603 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1604 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1605 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1606 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1607 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1608 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1609 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1610 /* init cursors */
1611 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1612 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1613 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1614 /* init appearance */
1615 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor, borderalpha);
1616 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor, baralpha);
1617 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor, OPAQUE);
1618 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor, borderalpha);
1619 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor, baralpha);
1620 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor, OPAQUE);
1621 /* init bars */
1622 updatebars();
1623 updatestatus();
1624 /* EWMH support per view */
1625 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1626 PropModeReplace, (unsigned char *) netatom, NetLast);
1627 XDeleteProperty(dpy, root, netatom[NetClientList]);
1628 /* select for events */
1629 wa.cursor = cursor[CurNormal]->cursor;
1630 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1631 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1632 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1633 XSelectInput(dpy, root, wa.event_mask);
1634 grabkeys();
1635 focus(NULL);
1638 void
1639 showhide(Client *c)
1641 if (!c)
1642 return;
1643 if (ISVISIBLE(c)) {
1644 /* show clients top down */
1645 XMoveWindow(dpy, c->win, c->x, c->y);
1646 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1647 resize(c, c->x, c->y, c->w, c->h, 0);
1648 showhide(c->snext);
1649 } else {
1650 /* hide clients bottom up */
1651 showhide(c->snext);
1652 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1656 void
1657 sigchld(int unused)
1659 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1660 die("can't install SIGCHLD handler:");
1661 while (0 < waitpid(-1, NULL, WNOHANG));
1664 void
1665 spawn(const Arg *arg)
1667 if (arg->v == dmenucmd)
1668 dmenumon[0] = '0' + selmon->num;
1669 if (fork() == 0) {
1670 if (dpy)
1671 close(ConnectionNumber(dpy));
1672 setsid();
1673 execvp(((char **)arg->v)[0], (char **)arg->v);
1674 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1675 perror(" failed");
1676 exit(EXIT_SUCCESS);
1680 void
1681 tag(const Arg *arg)
1683 if (selmon->sel && arg->ui & TAGMASK) {
1684 selmon->sel->tags = arg->ui & TAGMASK;
1685 focus(NULL);
1686 arrange(selmon);
1690 void
1691 tagmon(const Arg *arg)
1693 if (!selmon->sel || !mons->next)
1694 return;
1695 sendmon(selmon->sel, dirtomon(arg->i));
1698 void
1699 tile(Monitor *m)
1701 unsigned int i, n, h, mw, my, ty;
1702 Client *c;
1704 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1705 if (n == 0)
1706 return;
1708 if (n > m->nmaster)
1709 mw = m->nmaster ? m->ww * m->mfact : 0;
1710 else
1711 mw = m->ww;
1712 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1713 if (i < m->nmaster) {
1714 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1715 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1716 my += HEIGHT(c);
1717 } else {
1718 h = (m->wh - ty) / (n - i);
1719 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1720 ty += HEIGHT(c);
1724 void
1725 togglebar(const Arg *arg)
1727 selmon->showbar = !selmon->showbar;
1728 updatebarpos(selmon);
1729 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1730 arrange(selmon);
1733 void
1734 togglefloating(const Arg *arg)
1736 if (!selmon->sel)
1737 return;
1738 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1739 return;
1740 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1741 if (selmon->sel->isfloating)
1742 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1743 selmon->sel->w, selmon->sel->h, 0);
1744 arrange(selmon);
1747 void
1748 toggletag(const Arg *arg)
1750 unsigned int newtags;
1752 if (!selmon->sel)
1753 return;
1754 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1755 if (newtags) {
1756 selmon->sel->tags = newtags;
1757 focus(NULL);
1758 arrange(selmon);
1762 void
1763 toggleview(const Arg *arg)
1765 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1767 if (newtagset) {
1768 selmon->tagset[selmon->seltags] = newtagset;
1769 focus(NULL);
1770 arrange(selmon);
1774 void
1775 unfocus(Client *c, int setfocus)
1777 if (!c)
1778 return;
1779 grabbuttons(c, 0);
1780 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->pix);
1781 if (setfocus) {
1782 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1783 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1787 void
1788 unmanage(Client *c, int destroyed)
1790 Monitor *m = c->mon;
1791 XWindowChanges wc;
1793 /* The server grab construct avoids race conditions. */
1794 detach(c);
1795 detachstack(c);
1796 if (!destroyed) {
1797 wc.border_width = c->oldbw;
1798 XGrabServer(dpy);
1799 XSetErrorHandler(xerrordummy);
1800 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1801 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1802 setclientstate(c, WithdrawnState);
1803 XSync(dpy, False);
1804 XSetErrorHandler(xerror);
1805 XUngrabServer(dpy);
1807 free(c);
1808 focus(NULL);
1809 updateclientlist();
1810 arrange(m);
1813 void
1814 unmapnotify(XEvent *e)
1816 Client *c;
1817 XUnmapEvent *ev = &e->xunmap;
1819 if ((c = wintoclient(ev->window))) {
1820 if (ev->send_event)
1821 setclientstate(c, WithdrawnState);
1822 else
1823 unmanage(c, 0);
1827 void
1828 updatebars(void)
1830 Monitor *m;
1831 XSetWindowAttributes wa = {
1832 .override_redirect = True,
1833 .background_pixel = 0,
1834 .border_pixel = 0,
1835 .colormap = cmap,
1836 .event_mask = ButtonPressMask|ExposureMask
1838 for (m = mons; m; m = m->next) {
1839 if (m->barwin)
1840 continue;
1841 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
1842 InputOutput, visual,
1843 CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
1844 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1845 XMapRaised(dpy, m->barwin);
1849 void
1850 updatebarpos(Monitor *m)
1852 m->wy = m->my;
1853 m->wh = m->mh;
1854 if (m->showbar) {
1855 m->wh -= bh;
1856 m->by = m->topbar ? m->wy : m->wy + m->wh;
1857 m->wy = m->topbar ? m->wy + bh : m->wy;
1858 } else
1859 m->by = -bh;
1862 void
1863 updateclientlist()
1865 Client *c;
1866 Monitor *m;
1868 XDeleteProperty(dpy, root, netatom[NetClientList]);
1869 for (m = mons; m; m = m->next)
1870 for (c = m->clients; c; c = c->next)
1871 XChangeProperty(dpy, root, netatom[NetClientList],
1872 XA_WINDOW, 32, PropModeAppend,
1873 (unsigned char *) &(c->win), 1);
1877 updategeom(void)
1879 int dirty = 0;
1881 #ifdef XINERAMA
1882 if (XineramaIsActive(dpy)) {
1883 int i, j, n, nn;
1884 Client *c;
1885 Monitor *m;
1886 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1887 XineramaScreenInfo *unique = NULL;
1889 for (n = 0, m = mons; m; m = m->next, n++);
1890 /* only consider unique geometries as separate screens */
1891 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1892 for (i = 0, j = 0; i < nn; i++)
1893 if (isuniquegeom(unique, j, &info[i]))
1894 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1895 XFree(info);
1896 nn = j;
1897 if (n <= nn) {
1898 for (i = 0; i < (nn - n); i++) { /* new monitors available */
1899 for (m = mons; m && m->next; m = m->next);
1900 if (m)
1901 m->next = createmon();
1902 else
1903 mons = createmon();
1905 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
1906 if (i >= n
1907 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1908 || unique[i].width != m->mw || unique[i].height != m->mh))
1910 dirty = 1;
1911 m->num = i;
1912 m->mx = m->wx = unique[i].x_org;
1913 m->my = m->wy = unique[i].y_org;
1914 m->mw = m->ww = unique[i].width;
1915 m->mh = m->wh = unique[i].height;
1916 updatebarpos(m);
1918 } else {
1919 /* less monitors available nn < n */
1920 for (i = nn; i < n; i++) {
1921 for (m = mons; m && m->next; m = m->next);
1922 while (m->clients) {
1923 dirty = 1;
1924 c = m->clients;
1925 m->clients = c->next;
1926 detachstack(c);
1927 c->mon = mons;
1928 attach(c);
1929 attachstack(c);
1931 if (m == selmon)
1932 selmon = mons;
1933 cleanupmon(m);
1936 free(unique);
1937 } else
1938 #endif /* XINERAMA */
1939 /* default monitor setup */
1941 if (!mons)
1942 mons = createmon();
1943 if (mons->mw != sw || mons->mh != sh) {
1944 dirty = 1;
1945 mons->mw = mons->ww = sw;
1946 mons->mh = mons->wh = sh;
1947 updatebarpos(mons);
1950 if (dirty) {
1951 selmon = mons;
1952 selmon = wintomon(root);
1954 return dirty;
1957 void
1958 updatenumlockmask(void)
1960 unsigned int i, j;
1961 XModifierKeymap *modmap;
1963 numlockmask = 0;
1964 modmap = XGetModifierMapping(dpy);
1965 for (i = 0; i < 8; i++)
1966 for (j = 0; j < modmap->max_keypermod; j++)
1967 if (modmap->modifiermap[i * modmap->max_keypermod + j]
1968 == XKeysymToKeycode(dpy, XK_Num_Lock))
1969 numlockmask = (1 << i);
1970 XFreeModifiermap(modmap);
1973 void
1974 updatesizehints(Client *c)
1976 long msize;
1977 XSizeHints size;
1979 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
1980 /* size is uninitialized, ensure that size.flags aren't used */
1981 size.flags = PSize;
1982 if (size.flags & PBaseSize) {
1983 c->basew = size.base_width;
1984 c->baseh = size.base_height;
1985 } else if (size.flags & PMinSize) {
1986 c->basew = size.min_width;
1987 c->baseh = size.min_height;
1988 } else
1989 c->basew = c->baseh = 0;
1990 if (size.flags & PResizeInc) {
1991 c->incw = size.width_inc;
1992 c->inch = size.height_inc;
1993 } else
1994 c->incw = c->inch = 0;
1995 if (size.flags & PMaxSize) {
1996 c->maxw = size.max_width;
1997 c->maxh = size.max_height;
1998 } else
1999 c->maxw = c->maxh = 0;
2000 if (size.flags & PMinSize) {
2001 c->minw = size.min_width;
2002 c->minh = size.min_height;
2003 } else if (size.flags & PBaseSize) {
2004 c->minw = size.base_width;
2005 c->minh = size.base_height;
2006 } else
2007 c->minw = c->minh = 0;
2008 if (size.flags & PAspect) {
2009 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2010 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2011 } else
2012 c->maxa = c->mina = 0.0;
2013 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2014 && c->maxw == c->minw && c->maxh == c->minh);
2017 void
2018 updatetitle(Client *c)
2020 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2021 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2022 if (c->name[0] == '\0') /* hack to mark broken clients */
2023 strcpy(c->name, broken);
2026 void
2027 updatestatus(void)
2029 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2030 strcpy(stext, "dwm-"VERSION);
2031 drawbar(selmon);
2034 void
2035 updatewindowtype(Client *c)
2037 Atom state = getatomprop(c, netatom[NetWMState]);
2038 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2040 if (state == netatom[NetWMFullscreen])
2041 setfullscreen(c, 1);
2042 if (wtype == netatom[NetWMWindowTypeDialog])
2043 c->isfloating = 1;
2046 void
2047 updatewmhints(Client *c)
2049 XWMHints *wmh;
2051 if ((wmh = XGetWMHints(dpy, c->win))) {
2052 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2053 wmh->flags &= ~XUrgencyHint;
2054 XSetWMHints(dpy, c->win, wmh);
2055 } else
2056 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2057 if (wmh->flags & InputHint)
2058 c->neverfocus = !wmh->input;
2059 else
2060 c->neverfocus = 0;
2061 XFree(wmh);
2065 void
2066 view(const Arg *arg)
2068 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2069 return;
2070 selmon->seltags ^= 1; /* toggle sel tagset */
2071 if (arg->ui & TAGMASK)
2072 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2073 focus(NULL);
2074 arrange(selmon);
2077 Client *
2078 wintoclient(Window w)
2080 Client *c;
2081 Monitor *m;
2083 for (m = mons; m; m = m->next)
2084 for (c = m->clients; c; c = c->next)
2085 if (c->win == w)
2086 return c;
2087 return NULL;
2090 Monitor *
2091 wintomon(Window w)
2093 int x, y;
2094 Client *c;
2095 Monitor *m;
2097 if (w == root && getrootptr(&x, &y))
2098 return recttomon(x, y, 1, 1);
2099 for (m = mons; m; m = m->next)
2100 if (w == m->barwin)
2101 return m;
2102 if ((c = wintoclient(w)))
2103 return c->mon;
2104 return selmon;
2107 /* There's no way to check accesses to destroyed windows, thus those cases are
2108 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2109 * default error handler, which may call exit. */
2111 xerror(Display *dpy, XErrorEvent *ee)
2113 if (ee->error_code == BadWindow
2114 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2115 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2116 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2117 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2118 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2119 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2120 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2121 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2122 return 0;
2123 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2124 ee->request_code, ee->error_code);
2125 return xerrorxlib(dpy, ee); /* may call exit */
2129 xerrordummy(Display *dpy, XErrorEvent *ee)
2131 return 0;
2134 /* Startup Error handler to check if another window manager
2135 * is already running. */
2137 xerrorstart(Display *dpy, XErrorEvent *ee)
2139 die("dwm: another window manager is already running\n");
2140 return -1;
2143 void
2144 xinitvisual()
2146 XVisualInfo *infos;
2147 XRenderPictFormat *fmt;
2148 int nitems;
2149 int i;
2151 XVisualInfo tpl = {
2152 .screen = screen,
2153 .depth = 32,
2154 .class = TrueColor
2156 long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
2158 infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
2159 visual = NULL;
2160 for(i = 0; i < nitems; i ++) {
2161 fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
2162 if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
2163 visual = infos[i].visual;
2164 depth = infos[i].depth;
2165 cmap = XCreateColormap(dpy, root, visual, AllocNone);
2166 useargb = 1;
2167 break;
2171 XFree(infos);
2173 if (! visual) {
2174 visual = DefaultVisual(dpy, screen);
2175 depth = DefaultDepth(dpy, screen);
2176 cmap = DefaultColormap(dpy, screen);
2180 void
2181 zoom(const Arg *arg)
2183 Client *c = selmon->sel;
2185 if (!selmon->lt[selmon->sellt]->arrange
2186 || (selmon->sel && selmon->sel->isfloating))
2187 return;
2188 if (c == nexttiled(selmon->clients))
2189 if (!c || !(c = nexttiled(c->next)))
2190 return;
2191 pop(c);
2195 main(int argc, char *argv[])
2197 if (argc == 2 && !strcmp("-v", argv[1]))
2198 die("dwm-"VERSION "\n");
2199 else if (argc != 1)
2200 die("usage: dwm [-v]\n");
2201 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2202 fputs("warning: no locale support\n", stderr);
2203 if (!(dpy = XOpenDisplay(NULL)))
2204 die("dwm: cannot open display\n");
2205 checkotherwm();
2206 setup();
2207 scan();
2208 run();
2209 cleanup();
2210 XCloseDisplay(dpy);
2211 return EXIT_SUCCESS;