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