Xinqi Bao's Git

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