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