Xinqi Bao's Git

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