Xinqi Bao's Git

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