Xinqi Bao's Git

drastically changed updategeom() handling, see comment in updategeom() for details
[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, NetClientList, 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 void updategeom(void);
239 static void updatebarpos(Monitor *m);
240 static void updatebars(void);
241 static void updateclientlist(void);
242 static void updatenumlockmask(void);
243 static void updatesizehints(Client *c);
244 static void updatestatus(void);
245 static void updatewindowtype(Client *c);
246 static void updatetitle(Client *c);
247 static void updatewmhints(Client *c);
248 static void view(const Arg *arg);
249 static Client *wintoclient(Window w);
250 static Monitor *wintomon(Window w);
251 static int xerror(Display *dpy, XErrorEvent *ee);
252 static int xerrordummy(Display *dpy, XErrorEvent *ee);
253 static int xerrorstart(Display *dpy, XErrorEvent *ee);
254 static void zoom(const Arg *arg);
255
256 /* variables */
257 static const char broken[] = "broken";
258 static char stext[256];
259 static int screen;
260 static int sw, sh; /* X display screen geometry width, height */
261 static int bh, blw = 0; /* bar geometry */
262 static int (*xerrorxlib)(Display *, XErrorEvent *);
263 static unsigned int numlockmask = 0;
264 static void (*handler[LASTEvent]) (XEvent *) = {
265 [ButtonPress] = buttonpress,
266 [ClientMessage] = clientmessage,
267 [ConfigureRequest] = configurerequest,
268 [ConfigureNotify] = configurenotify,
269 [DestroyNotify] = destroynotify,
270 [EnterNotify] = enternotify,
271 [Expose] = expose,
272 [FocusIn] = focusin,
273 [KeyPress] = keypress,
274 [MappingNotify] = mappingnotify,
275 [MapRequest] = maprequest,
276 [MotionNotify] = motionnotify,
277 [PropertyNotify] = propertynotify,
278 [UnmapNotify] = unmapnotify
279 };
280 static Atom wmatom[WMLast], netatom[NetLast];
281 static Bool running = True;
282 static Cursor cursor[CurLast];
283 static Display *dpy;
284 static DC dc;
285 static Monitor *mons = NULL, *selmon = NULL;
286 static Window root;
287
288 /* configuration, allows nested code to access above variables */
289 #include "config.h"
290
291 /* compile-time check if all tags fit into an unsigned int bit array. */
292 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
293
294 /* function implementations */
295 void
296 applyrules(Client *c) {
297 const char *class, *instance;
298 unsigned int i;
299 const Rule *r;
300 Monitor *m;
301 XClassHint ch = { NULL, NULL };
302
303 /* rule matching */
304 c->isfloating = c->tags = 0;
305 XGetClassHint(dpy, c->win, &ch);
306 class = ch.res_class ? ch.res_class : broken;
307 instance = ch.res_name ? ch.res_name : broken;
308
309 for(i = 0; i < LENGTH(rules); i++) {
310 r = &rules[i];
311 if((!r->title || strstr(c->name, r->title))
312 && (!r->class || strstr(class, r->class))
313 && (!r->instance || strstr(instance, r->instance)))
314 {
315 c->isfloating = r->isfloating;
316 c->tags |= r->tags;
317 for(m = mons; m && m->num != r->monitor; m = m->next);
318 if(m)
319 c->mon = m;
320 }
321 }
322 if(ch.res_class)
323 XFree(ch.res_class);
324 if(ch.res_name)
325 XFree(ch.res_name);
326 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
327 }
328
329 Bool
330 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
331 Bool baseismin;
332 Monitor *m = c->mon;
333
334 /* set minimum possible */
335 *w = MAX(1, *w);
336 *h = MAX(1, *h);
337 if(interact) {
338 if(*x > sw)
339 *x = sw - WIDTH(c);
340 if(*y > sh)
341 *y = sh - HEIGHT(c);
342 if(*x + *w + 2 * c->bw < 0)
343 *x = 0;
344 if(*y + *h + 2 * c->bw < 0)
345 *y = 0;
346 }
347 else {
348 if(*x >= m->wx + m->ww)
349 *x = m->wx + m->ww - WIDTH(c);
350 if(*y >= m->wy + m->wh)
351 *y = m->wy + m->wh - HEIGHT(c);
352 if(*x + *w + 2 * c->bw <= m->wx)
353 *x = m->wx;
354 if(*y + *h + 2 * c->bw <= m->wy)
355 *y = m->wy;
356 }
357 if(*h < bh)
358 *h = bh;
359 if(*w < bh)
360 *w = bh;
361 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
362 /* see last two sentences in ICCCM 4.1.2.3 */
363 baseismin = c->basew == c->minw && c->baseh == c->minh;
364 if(!baseismin) { /* temporarily remove base dimensions */
365 *w -= c->basew;
366 *h -= c->baseh;
367 }
368 /* adjust for aspect limits */
369 if(c->mina > 0 && c->maxa > 0) {
370 if(c->maxa < (float)*w / *h)
371 *w = *h * c->maxa + 0.5;
372 else if(c->mina < (float)*h / *w)
373 *h = *w * c->mina + 0.5;
374 }
375 if(baseismin) { /* increment calculation requires this */
376 *w -= c->basew;
377 *h -= c->baseh;
378 }
379 /* adjust for increment value */
380 if(c->incw)
381 *w -= *w % c->incw;
382 if(c->inch)
383 *h -= *h % c->inch;
384 /* restore base dimensions */
385 *w = MAX(*w + c->basew, c->minw);
386 *h = MAX(*h + c->baseh, c->minh);
387 if(c->maxw)
388 *w = MIN(*w, c->maxw);
389 if(c->maxh)
390 *h = MIN(*h, c->maxh);
391 }
392 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
393 }
394
395 void
396 arrange(Monitor *m) {
397 if(m)
398 showhide(m->stack);
399 else for(m = mons; m; m = m->next)
400 showhide(m->stack);
401 if(m) {
402 arrangemon(m);
403 restack(m);
404 } else for(m = mons; m; m = m->next)
405 arrangemon(m);
406 }
407
408 void
409 arrangemon(Monitor *m) {
410 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
411 if(m->lt[m->sellt]->arrange)
412 m->lt[m->sellt]->arrange(m);
413 }
414
415 void
416 attach(Client *c) {
417 c->next = c->mon->clients;
418 c->mon->clients = c;
419 }
420
421 void
422 attachstack(Client *c) {
423 c->snext = c->mon->stack;
424 c->mon->stack = c;
425 }
426
427 void
428 buttonpress(XEvent *e) {
429 unsigned int i, x, click;
430 Arg arg = {0};
431 Client *c;
432 Monitor *m;
433 XButtonPressedEvent *ev = &e->xbutton;
434
435 click = ClkRootWin;
436 /* focus monitor if necessary */
437 if((m = wintomon(ev->window)) && m != selmon) {
438 unfocus(selmon->sel, True);
439 selmon = m;
440 focus(NULL);
441 }
442 if(ev->window == selmon->barwin) {
443 i = x = 0;
444 do
445 x += TEXTW(tags[i]);
446 while(ev->x >= x && ++i < LENGTH(tags));
447 if(i < LENGTH(tags)) {
448 click = ClkTagBar;
449 arg.ui = 1 << i;
450 }
451 else if(ev->x < x + blw)
452 click = ClkLtSymbol;
453 else if(ev->x > selmon->ww - TEXTW(stext))
454 click = ClkStatusText;
455 else
456 click = ClkWinTitle;
457 }
458 else if((c = wintoclient(ev->window))) {
459 focus(c);
460 click = ClkClientWin;
461 }
462 for(i = 0; i < LENGTH(buttons); i++)
463 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
464 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
465 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
466 }
467
468 void
469 checkotherwm(void) {
470 xerrorxlib = XSetErrorHandler(xerrorstart);
471 /* this causes an error if some other window manager is running */
472 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
473 XSync(dpy, False);
474 XSetErrorHandler(xerror);
475 XSync(dpy, False);
476 }
477
478 void
479 cleanup(void) {
480 Arg a = {.ui = ~0};
481 Layout foo = { "", NULL };
482 Monitor *m;
483
484 view(&a);
485 selmon->lt[selmon->sellt] = &foo;
486 for(m = mons; m; m = m->next)
487 while(m->stack)
488 unmanage(m->stack, False);
489 if(dc.font.set)
490 XFreeFontSet(dpy, dc.font.set);
491 else
492 XFreeFont(dpy, dc.font.xfont);
493 XUngrabKey(dpy, AnyKey, AnyModifier, root);
494 XFreePixmap(dpy, dc.drawable);
495 XFreeGC(dpy, dc.gc);
496 XFreeCursor(dpy, cursor[CurNormal]);
497 XFreeCursor(dpy, cursor[CurResize]);
498 XFreeCursor(dpy, cursor[CurMove]);
499 while(mons)
500 cleanupmon(mons);
501 XSync(dpy, False);
502 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
503 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
504 }
505
506 void
507 cleanupmon(Monitor *mon) {
508 Monitor *m;
509
510 if(mon == mons)
511 mons = mons->next;
512 else {
513 for(m = mons; m && m->next != mon; m = m->next);
514 m->next = mon->next;
515 }
516 XUnmapWindow(dpy, mon->barwin);
517 XDestroyWindow(dpy, mon->barwin);
518 free(mon);
519 }
520
521 void
522 clearurgent(Client *c) {
523 XWMHints *wmh;
524
525 c->isurgent = False;
526 if(!(wmh = XGetWMHints(dpy, c->win)))
527 return;
528 wmh->flags &= ~XUrgencyHint;
529 XSetWMHints(dpy, c->win, wmh);
530 XFree(wmh);
531 }
532
533 void
534 clientmessage(XEvent *e) {
535 XClientMessageEvent *cme = &e->xclient;
536 Client *c = wintoclient(cme->window);
537
538 if(!c)
539 return;
540 if(cme->message_type == netatom[NetWMState]) {
541 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
542 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
543 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
544 }
545 else if(cme->message_type == netatom[NetActiveWindow]) {
546 if(!ISVISIBLE(c)) {
547 c->mon->seltags ^= 1;
548 c->mon->tagset[c->mon->seltags] = c->tags;
549 }
550 pop(c);
551 }
552 }
553
554 void
555 configure(Client *c) {
556 XConfigureEvent ce;
557
558 ce.type = ConfigureNotify;
559 ce.display = dpy;
560 ce.event = c->win;
561 ce.window = c->win;
562 ce.x = c->x;
563 ce.y = c->y;
564 ce.width = c->w;
565 ce.height = c->h;
566 ce.border_width = c->bw;
567 ce.above = None;
568 ce.override_redirect = False;
569 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
570 }
571
572 void
573 configurenotify(XEvent *e) {
574 Monitor *m;
575 XConfigureEvent *ev = &e->xconfigure;
576
577 if(ev->window == root) {
578 sw = ev->width;
579 sh = ev->height;
580 if(dc.drawable != 0)
581 XFreePixmap(dpy, dc.drawable);
582 dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
583 updatebars();
584 for(m = mons; m; m = m->next)
585 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
586 focus(NULL);
587 arrange(NULL);
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 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
862 }
863 selmon->sel = c;
864 drawbars();
865 }
866
867 void
868 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
869 XFocusChangeEvent *ev = &e->xfocus;
870
871 if(selmon->sel && ev->window != selmon->sel->win)
872 setfocus(selmon->sel);
873 }
874
875 void
876 focusmon(const Arg *arg) {
877 Monitor *m;
878
879 if(!mons->next)
880 return;
881 if((m = dirtomon(arg->i)) == selmon)
882 return;
883 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
884 in gedit and anjuta */
885 selmon = m;
886 focus(NULL);
887 }
888
889 void
890 focusstack(const Arg *arg) {
891 Client *c = NULL, *i;
892
893 if(!selmon->sel)
894 return;
895 if(arg->i > 0) {
896 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
897 if(!c)
898 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
899 }
900 else {
901 for(i = selmon->clients; i != selmon->sel; i = i->next)
902 if(ISVISIBLE(i))
903 c = i;
904 if(!c)
905 for(; i; i = i->next)
906 if(ISVISIBLE(i))
907 c = i;
908 }
909 if(c) {
910 focus(c);
911 restack(selmon);
912 }
913 }
914
915 Atom
916 getatomprop(Client *c, Atom prop) {
917 int di;
918 unsigned long dl;
919 unsigned char *p = NULL;
920 Atom da, atom = None;
921
922 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
923 &da, &di, &dl, &dl, &p) == Success && p) {
924 atom = *(Atom *)p;
925 XFree(p);
926 }
927 return atom;
928 }
929
930 unsigned long
931 getcolor(const char *colstr) {
932 Colormap cmap = DefaultColormap(dpy, screen);
933 XColor color;
934
935 if(!XAllocNamedColor(dpy, cmap, colstr, &color, &color))
936 die("error, cannot allocate color '%s'\n", colstr);
937 return color.pixel;
938 }
939
940 Bool
941 getrootptr(int *x, int *y) {
942 int di;
943 unsigned int dui;
944 Window dummy;
945
946 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
947 }
948
949 long
950 getstate(Window w) {
951 int format;
952 long result = -1;
953 unsigned char *p = NULL;
954 unsigned long n, extra;
955 Atom real;
956
957 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
958 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
959 return -1;
960 if(n != 0)
961 result = *p;
962 XFree(p);
963 return result;
964 }
965
966 Bool
967 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
968 char **list = NULL;
969 int n;
970 XTextProperty name;
971
972 if(!text || size == 0)
973 return False;
974 text[0] = '\0';
975 XGetTextProperty(dpy, w, &name, atom);
976 if(!name.nitems)
977 return False;
978 if(name.encoding == XA_STRING)
979 strncpy(text, (char *)name.value, size - 1);
980 else {
981 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
982 strncpy(text, *list, size - 1);
983 XFreeStringList(list);
984 }
985 }
986 text[size - 1] = '\0';
987 XFree(name.value);
988 return True;
989 }
990
991 void
992 grabbuttons(Client *c, Bool focused) {
993 updatenumlockmask();
994 {
995 unsigned int i, j;
996 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
997 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
998 if(focused) {
999 for(i = 0; i < LENGTH(buttons); i++)
1000 if(buttons[i].click == ClkClientWin)
1001 for(j = 0; j < LENGTH(modifiers); j++)
1002 XGrabButton(dpy, buttons[i].button,
1003 buttons[i].mask | modifiers[j],
1004 c->win, False, BUTTONMASK,
1005 GrabModeAsync, GrabModeSync, None, None);
1006 }
1007 else
1008 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1009 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1010 }
1011 }
1012
1013 void
1014 grabkeys(void) {
1015 updatenumlockmask();
1016 {
1017 unsigned int i, j;
1018 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1019 KeyCode code;
1020
1021 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1022 for(i = 0; i < LENGTH(keys); i++)
1023 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1024 for(j = 0; j < LENGTH(modifiers); j++)
1025 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1026 True, GrabModeAsync, GrabModeAsync);
1027 }
1028 }
1029
1030 void
1031 incnmaster(const Arg *arg) {
1032 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1033 arrange(selmon);
1034 }
1035
1036 void
1037 initfont(const char *fontstr) {
1038 char *def, **missing;
1039 int n;
1040
1041 dc.font.set = XCreateFontSet(dpy, fontstr, &missing, &n, &def);
1042 if(missing) {
1043 while(n--)
1044 fprintf(stderr, "dwm: missing fontset: %s\n", missing[n]);
1045 XFreeStringList(missing);
1046 }
1047 if(dc.font.set) {
1048 XFontStruct **xfonts;
1049 char **font_names;
1050
1051 dc.font.ascent = dc.font.descent = 0;
1052 XExtentsOfFontSet(dc.font.set);
1053 n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
1054 while(n--) {
1055 dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
1056 dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
1057 xfonts++;
1058 }
1059 }
1060 else {
1061 if(!(dc.font.xfont = XLoadQueryFont(dpy, fontstr))
1062 && !(dc.font.xfont = XLoadQueryFont(dpy, "fixed")))
1063 die("error, cannot load font: '%s'\n", fontstr);
1064 dc.font.ascent = dc.font.xfont->ascent;
1065 dc.font.descent = dc.font.xfont->descent;
1066 }
1067 dc.font.height = dc.font.ascent + dc.font.descent;
1068 }
1069
1070 #ifdef XINERAMA
1071 static Bool
1072 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1073 while(n--)
1074 /* treat origin (x, y) as fixpoint for uniqueness only, first screen wins */
1075 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org)
1076 return False;
1077 return True;
1078 }
1079 #endif /* XINERAMA */
1080
1081 void
1082 keypress(XEvent *e) {
1083 unsigned int i;
1084 KeySym keysym;
1085 XKeyEvent *ev;
1086
1087 ev = &e->xkey;
1088 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1089 for(i = 0; i < LENGTH(keys); i++)
1090 if(keysym == keys[i].keysym
1091 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1092 && keys[i].func)
1093 keys[i].func(&(keys[i].arg));
1094 }
1095
1096 void
1097 killclient(const Arg *arg) {
1098 if(!selmon->sel)
1099 return;
1100 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1101 XGrabServer(dpy);
1102 XSetErrorHandler(xerrordummy);
1103 XSetCloseDownMode(dpy, DestroyAll);
1104 XKillClient(dpy, selmon->sel->win);
1105 XSync(dpy, False);
1106 XSetErrorHandler(xerror);
1107 XUngrabServer(dpy);
1108 }
1109 }
1110
1111 void
1112 manage(Window w, XWindowAttributes *wa) {
1113 Client *c, *t = NULL;
1114 Window trans = None;
1115 XWindowChanges wc;
1116
1117 if(!(c = calloc(1, sizeof(Client))))
1118 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1119 c->win = w;
1120 updatetitle(c);
1121 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1122 c->mon = t->mon;
1123 c->tags = t->tags;
1124 }
1125 else {
1126 c->mon = selmon;
1127 applyrules(c);
1128 }
1129 /* geometry */
1130 c->x = c->oldx = wa->x;
1131 c->y = c->oldy = wa->y;
1132 c->w = c->oldw = wa->width;
1133 c->h = c->oldh = wa->height;
1134 c->oldbw = wa->border_width;
1135
1136 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1137 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1138 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1139 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1140 c->x = MAX(c->x, c->mon->mx);
1141 /* only fix client y-offset, if the client center might cover the bar */
1142 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1143 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1144 c->bw = borderpx;
1145
1146 wc.border_width = c->bw;
1147 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1148 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
1149 configure(c); /* propagates border_width, if size doesn't change */
1150 updatewindowtype(c);
1151 updatesizehints(c);
1152 updatewmhints(c);
1153 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1154 grabbuttons(c, False);
1155 if(!c->isfloating)
1156 c->isfloating = c->oldstate = trans != None || c->isfixed;
1157 if(c->isfloating)
1158 XRaiseWindow(dpy, c->win);
1159 attach(c);
1160 attachstack(c);
1161 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1162 (unsigned char *) &(c->win), 1);
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 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1235 return;
1236 restack(selmon);
1237 ocx = c->x;
1238 ocy = c->y;
1239 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1240 None, cursor[CurMove], CurrentTime) != GrabSuccess)
1241 return;
1242 if(!getrootptr(&x, &y))
1243 return;
1244 do {
1245 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1246 switch(ev.type) {
1247 case ConfigureRequest:
1248 case Expose:
1249 case MapRequest:
1250 handler[ev.type](&ev);
1251 break;
1252 case MotionNotify:
1253 nx = ocx + (ev.xmotion.x - x);
1254 ny = ocy + (ev.xmotion.y - y);
1255 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1256 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1257 if(abs(selmon->wx - nx) < snap)
1258 nx = selmon->wx;
1259 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1260 nx = selmon->wx + selmon->ww - WIDTH(c);
1261 if(abs(selmon->wy - ny) < snap)
1262 ny = selmon->wy;
1263 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1264 ny = selmon->wy + selmon->wh - HEIGHT(c);
1265 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1266 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1267 togglefloating(NULL);
1268 }
1269 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1270 resize(c, nx, ny, c->w, c->h, True);
1271 break;
1272 }
1273 } while(ev.type != ButtonRelease);
1274 XUngrabPointer(dpy, CurrentTime);
1275 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1276 sendmon(c, m);
1277 selmon = m;
1278 focus(NULL);
1279 }
1280 }
1281
1282 Client *
1283 nexttiled(Client *c) {
1284 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1285 return c;
1286 }
1287
1288 void
1289 pop(Client *c) {
1290 detach(c);
1291 attach(c);
1292 focus(c);
1293 arrange(c->mon);
1294 }
1295
1296 void
1297 propertynotify(XEvent *e) {
1298 Client *c;
1299 Window trans;
1300 XPropertyEvent *ev = &e->xproperty;
1301
1302 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1303 updatestatus();
1304 else if(ev->state == PropertyDelete)
1305 return; /* ignore */
1306 else if((c = wintoclient(ev->window))) {
1307 switch(ev->atom) {
1308 default: break;
1309 case XA_WM_TRANSIENT_FOR:
1310 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1311 (c->isfloating = (wintoclient(trans)) != NULL))
1312 arrange(c->mon);
1313 break;
1314 case XA_WM_NORMAL_HINTS:
1315 updatesizehints(c);
1316 break;
1317 case XA_WM_HINTS:
1318 updatewmhints(c);
1319 drawbars();
1320 break;
1321 }
1322 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1323 updatetitle(c);
1324 if(c == c->mon->sel)
1325 drawbar(c->mon);
1326 }
1327 if(ev->atom == netatom[NetWMWindowType])
1328 updatewindowtype(c);
1329 }
1330 }
1331
1332 void
1333 quit(const Arg *arg) {
1334 running = False;
1335 }
1336
1337 Monitor *
1338 recttomon(int x, int y, int w, int h) {
1339 Monitor *m, *r = selmon;
1340 int a, area = 0;
1341
1342 for(m = mons; m; m = m->next)
1343 if((a = INTERSECT(x, y, w, h, m)) > area) {
1344 area = a;
1345 r = m;
1346 }
1347 return r;
1348 }
1349
1350 void
1351 resize(Client *c, int x, int y, int w, int h, Bool interact) {
1352 if(applysizehints(c, &x, &y, &w, &h, interact))
1353 resizeclient(c, x, y, w, h);
1354 }
1355
1356 void
1357 resizeclient(Client *c, int x, int y, int w, int h) {
1358 XWindowChanges wc;
1359
1360 c->oldx = c->x; c->x = wc.x = x;
1361 c->oldy = c->y; c->y = wc.y = y;
1362 c->oldw = c->w; c->w = wc.width = w;
1363 c->oldh = c->h; c->h = wc.height = h;
1364 wc.border_width = c->bw;
1365 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1366 configure(c);
1367 XSync(dpy, False);
1368 }
1369
1370 void
1371 resizemouse(const Arg *arg) {
1372 int ocx, ocy;
1373 int nw, nh;
1374 Client *c;
1375 Monitor *m;
1376 XEvent ev;
1377
1378 if(!(c = selmon->sel))
1379 return;
1380 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1381 return;
1382 restack(selmon);
1383 ocx = c->x;
1384 ocy = c->y;
1385 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1386 None, cursor[CurResize], CurrentTime) != GrabSuccess)
1387 return;
1388 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1389 do {
1390 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1391 switch(ev.type) {
1392 case ConfigureRequest:
1393 case Expose:
1394 case MapRequest:
1395 handler[ev.type](&ev);
1396 break;
1397 case MotionNotify:
1398 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1399 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1400 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1401 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1402 {
1403 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1404 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1405 togglefloating(NULL);
1406 }
1407 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1408 resize(c, c->x, c->y, nw, nh, True);
1409 break;
1410 }
1411 } while(ev.type != ButtonRelease);
1412 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1413 XUngrabPointer(dpy, CurrentTime);
1414 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1415 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1416 sendmon(c, m);
1417 selmon = m;
1418 focus(NULL);
1419 }
1420 }
1421
1422 void
1423 restack(Monitor *m) {
1424 Client *c;
1425 XEvent ev;
1426 XWindowChanges wc;
1427
1428 drawbar(m);
1429 if(!m->sel)
1430 return;
1431 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1432 XRaiseWindow(dpy, m->sel->win);
1433 if(m->lt[m->sellt]->arrange) {
1434 wc.stack_mode = Below;
1435 wc.sibling = m->barwin;
1436 for(c = m->stack; c; c = c->snext)
1437 if(!c->isfloating && ISVISIBLE(c)) {
1438 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1439 wc.sibling = c->win;
1440 }
1441 }
1442 XSync(dpy, False);
1443 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1444 }
1445
1446 void
1447 run(void) {
1448 XEvent ev;
1449 /* main event loop */
1450 XSync(dpy, False);
1451 while(running && !XNextEvent(dpy, &ev))
1452 if(handler[ev.type])
1453 handler[ev.type](&ev); /* call handler */
1454 }
1455
1456 void
1457 scan(void) {
1458 unsigned int i, num;
1459 Window d1, d2, *wins = NULL;
1460 XWindowAttributes wa;
1461
1462 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1463 for(i = 0; i < num; i++) {
1464 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1465 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1466 continue;
1467 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1468 manage(wins[i], &wa);
1469 }
1470 for(i = 0; i < num; i++) { /* now the transients */
1471 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1472 continue;
1473 if(XGetTransientForHint(dpy, wins[i], &d1)
1474 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1475 manage(wins[i], &wa);
1476 }
1477 if(wins)
1478 XFree(wins);
1479 }
1480 }
1481
1482 void
1483 sendmon(Client *c, Monitor *m) {
1484 if(c->mon == m)
1485 return;
1486 unfocus(c, True);
1487 detach(c);
1488 detachstack(c);
1489 c->mon = m;
1490 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1491 attach(c);
1492 attachstack(c);
1493 focus(NULL);
1494 arrange(NULL);
1495 }
1496
1497 void
1498 setclientstate(Client *c, long state) {
1499 long data[] = { state, None };
1500
1501 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1502 PropModeReplace, (unsigned char *)data, 2);
1503 }
1504
1505 Bool
1506 sendevent(Client *c, Atom proto) {
1507 int n;
1508 Atom *protocols;
1509 Bool exists = False;
1510 XEvent ev;
1511
1512 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1513 while(!exists && n--)
1514 exists = protocols[n] == proto;
1515 XFree(protocols);
1516 }
1517 if(exists) {
1518 ev.type = ClientMessage;
1519 ev.xclient.window = c->win;
1520 ev.xclient.message_type = wmatom[WMProtocols];
1521 ev.xclient.format = 32;
1522 ev.xclient.data.l[0] = proto;
1523 ev.xclient.data.l[1] = CurrentTime;
1524 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1525 }
1526 return exists;
1527 }
1528
1529 void
1530 setfocus(Client *c) {
1531 if(!c->neverfocus) {
1532 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1533 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1534 XA_WINDOW, 32, PropModeReplace,
1535 (unsigned char *) &(c->win), 1);
1536 }
1537 sendevent(c, wmatom[WMTakeFocus]);
1538 }
1539
1540 void
1541 setfullscreen(Client *c, Bool fullscreen) {
1542 if(fullscreen) {
1543 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1544 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1545 c->isfullscreen = True;
1546 c->oldstate = c->isfloating;
1547 c->oldbw = c->bw;
1548 c->bw = 0;
1549 c->isfloating = True;
1550 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1551 XRaiseWindow(dpy, c->win);
1552 }
1553 else {
1554 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1555 PropModeReplace, (unsigned char*)0, 0);
1556 c->isfullscreen = False;
1557 c->isfloating = c->oldstate;
1558 c->bw = c->oldbw;
1559 c->x = c->oldx;
1560 c->y = c->oldy;
1561 c->w = c->oldw;
1562 c->h = c->oldh;
1563 resizeclient(c, c->x, c->y, c->w, c->h);
1564 arrange(c->mon);
1565 }
1566 }
1567
1568 void
1569 setlayout(const Arg *arg) {
1570 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1571 selmon->sellt ^= 1;
1572 if(arg && arg->v)
1573 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1574 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1575 if(selmon->sel)
1576 arrange(selmon);
1577 else
1578 drawbar(selmon);
1579 }
1580
1581 /* arg > 1.0 will set mfact absolutly */
1582 void
1583 setmfact(const Arg *arg) {
1584 float f;
1585
1586 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1587 return;
1588 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1589 if(f < 0.1 || f > 0.9)
1590 return;
1591 selmon->mfact = f;
1592 arrange(selmon);
1593 }
1594
1595 void
1596 setup(void) {
1597 XSetWindowAttributes wa;
1598
1599 /* clean up any zombies immediately */
1600 sigchld(0);
1601
1602 /* init screen */
1603 screen = DefaultScreen(dpy);
1604 root = RootWindow(dpy, screen);
1605 initfont(font);
1606 sw = DisplayWidth(dpy, screen);
1607 sh = DisplayHeight(dpy, screen);
1608 bh = dc.h = dc.font.height + 2;
1609 updategeom();
1610 /* init atoms */
1611 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1612 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1613 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1614 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1615 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1616 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1617 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1618 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1619 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1620 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1621 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1622 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1623 /* init cursors */
1624 cursor[CurNormal] = XCreateFontCursor(dpy, XC_left_ptr);
1625 cursor[CurResize] = XCreateFontCursor(dpy, XC_sizing);
1626 cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
1627 /* init appearance */
1628 dc.norm[ColBorder] = getcolor(normbordercolor);
1629 dc.norm[ColBG] = getcolor(normbgcolor);
1630 dc.norm[ColFG] = getcolor(normfgcolor);
1631 dc.sel[ColBorder] = getcolor(selbordercolor);
1632 dc.sel[ColBG] = getcolor(selbgcolor);
1633 dc.sel[ColFG] = getcolor(selfgcolor);
1634 dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
1635 dc.gc = XCreateGC(dpy, root, 0, NULL);
1636 XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
1637 if(!dc.font.set)
1638 XSetFont(dpy, dc.gc, dc.font.xfont->fid);
1639 /* init bars */
1640 updatebars();
1641 updatestatus();
1642 /* EWMH support per view */
1643 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1644 PropModeReplace, (unsigned char *) netatom, NetLast);
1645 XDeleteProperty(dpy, root, netatom[NetClientList]);
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 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1757 return;
1758 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1759 if(selmon->sel->isfloating)
1760 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1761 selmon->sel->w, selmon->sel->h, 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 updateclientlist();
1824 arrange(m);
1825 }
1826
1827 void
1828 unmapnotify(XEvent *e) {
1829 Client *c;
1830 XUnmapEvent *ev = &e->xunmap;
1831
1832 if((c = wintoclient(ev->window))) {
1833 if(ev->send_event)
1834 setclientstate(c, WithdrawnState);
1835 else
1836 unmanage(c, False);
1837 }
1838 }
1839
1840 void
1841 updatebars(void) {
1842 Monitor *m;
1843 XSetWindowAttributes wa = {
1844 .override_redirect = True,
1845 .background_pixmap = ParentRelative,
1846 .event_mask = ButtonPressMask|ExposureMask
1847 };
1848 for(m = mons; m; m = m->next) {
1849 if (m->barwin)
1850 continue;
1851 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1852 CopyFromParent, DefaultVisual(dpy, screen),
1853 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1854 XDefineCursor(dpy, m->barwin, cursor[CurNormal]);
1855 XMapRaised(dpy, m->barwin);
1856 }
1857 }
1858
1859 void
1860 updatebarpos(Monitor *m) {
1861 m->wy = m->my;
1862 m->wh = m->mh;
1863 if(m->showbar) {
1864 m->wh -= bh;
1865 m->by = m->topbar ? m->wy : m->wy + m->wh;
1866 m->wy = m->topbar ? m->wy + bh : m->wy;
1867 }
1868 else
1869 m->by = -bh;
1870 }
1871
1872 void
1873 updateclientlist() {
1874 Client *c;
1875 Monitor *m;
1876
1877 XDeleteProperty(dpy, root, netatom[NetClientList]);
1878 for(m = mons; m; m = m->next)
1879 for(c = m->clients; c; c = c->next)
1880 XChangeProperty(dpy, root, netatom[NetClientList],
1881 XA_WINDOW, 32, PropModeAppend,
1882 (unsigned char *) &(c->win), 1);
1883 }
1884
1885 void
1886 updategeom(void) {
1887 /* Starting with dwm 6.1 this function uses a new (simpler) strategy:
1888 * whenever screen changes are reported, we destroy all monitors
1889 * and recreate all unique origin monitors and add all clients to
1890 * the first monitor, only. In several circumstances this may suck,
1891 * but dealing with all corner-cases sucks even more.*/
1892
1893 #ifdef XINERAMA
1894 if(XineramaIsActive(dpy)) {
1895 int i, j, n;
1896 Client *c;
1897 Monitor *m, *oldmons = mons;
1898 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &n);
1899 XineramaScreenInfo *unique = NULL;
1900
1901 /* only consider unique geometries as separate screens */
1902 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * n)))
1903 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * n);
1904 for(i = 0, j = 0; i < n; i++)
1905 if(isuniquegeom(unique, j, &info[i]))
1906 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1907 XFree(info);
1908 /* create new monitor structure */
1909 n = j;
1910 mons = m = createmon(); /* new first monitor */
1911 for(i = 1; i < n; i++) {
1912 m->next = createmon();
1913 m = m->next;
1914 }
1915 for(i = 0, m = mons; i < n && m; m = m->next, i++) {
1916 m->num = i;
1917 m->mx = m->wx = unique[i].x_org;
1918 m->my = m->wy = unique[i].y_org;
1919 m->mw = m->ww = unique[i].width;
1920 m->mh = m->wh = unique[i].height;
1921 updatebarpos(m);
1922 }
1923 free(unique);
1924 /* re-attach old clients and cleanup old monitor structure */
1925 while(oldmons) {
1926 m = oldmons;
1927 while(m->clients) {
1928 c = m->clients;
1929 m->clients = c->next;
1930 detachstack(c);
1931 c->mon = mons;
1932 attach(c);
1933 attachstack(c);
1934 }
1935 oldmons = m->next;
1936 cleanupmon(m);
1937 }
1938 }
1939 else
1940 #endif /* XINERAMA */
1941 /* default monitor setup */
1942 {
1943 if(!mons) /* only true if !XINERAMA compile flag */
1944 mons = createmon();
1945 if(mons->mw != sw || mons->mh != sh) {
1946 mons->mw = mons->ww = sw;
1947 mons->mh = mons->wh = sh;
1948 updatebarpos(mons);
1949 }
1950 }
1951 selmon = mons;
1952 selmon = wintomon(root);
1953 }
1954
1955 void
1956 updatenumlockmask(void) {
1957 unsigned int i, j;
1958 XModifierKeymap *modmap;
1959
1960 numlockmask = 0;
1961 modmap = XGetModifierMapping(dpy);
1962 for(i = 0; i < 8; i++)
1963 for(j = 0; j < modmap->max_keypermod; j++)
1964 if(modmap->modifiermap[i * modmap->max_keypermod + j]
1965 == XKeysymToKeycode(dpy, XK_Num_Lock))
1966 numlockmask = (1 << i);
1967 XFreeModifiermap(modmap);
1968 }
1969
1970 void
1971 updatesizehints(Client *c) {
1972 long msize;
1973 XSizeHints size;
1974
1975 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
1976 /* size is uninitialized, ensure that size.flags aren't used */
1977 size.flags = PSize;
1978 if(size.flags & PBaseSize) {
1979 c->basew = size.base_width;
1980 c->baseh = size.base_height;
1981 }
1982 else if(size.flags & PMinSize) {
1983 c->basew = size.min_width;
1984 c->baseh = size.min_height;
1985 }
1986 else
1987 c->basew = c->baseh = 0;
1988 if(size.flags & PResizeInc) {
1989 c->incw = size.width_inc;
1990 c->inch = size.height_inc;
1991 }
1992 else
1993 c->incw = c->inch = 0;
1994 if(size.flags & PMaxSize) {
1995 c->maxw = size.max_width;
1996 c->maxh = size.max_height;
1997 }
1998 else
1999 c->maxw = c->maxh = 0;
2000 if(size.flags & PMinSize) {
2001 c->minw = size.min_width;
2002 c->minh = size.min_height;
2003 }
2004 else if(size.flags & PBaseSize) {
2005 c->minw = size.base_width;
2006 c->minh = size.base_height;
2007 }
2008 else
2009 c->minw = c->minh = 0;
2010 if(size.flags & PAspect) {
2011 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2012 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2013 }
2014 else
2015 c->maxa = c->mina = 0.0;
2016 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2017 && c->maxw == c->minw && c->maxh == c->minh);
2018 }
2019
2020 void
2021 updatetitle(Client *c) {
2022 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2023 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2024 if(c->name[0] == '\0') /* hack to mark broken clients */
2025 strcpy(c->name, broken);
2026 }
2027
2028 void
2029 updatestatus(void) {
2030 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2031 strcpy(stext, "dwm-"VERSION);
2032 drawbar(selmon);
2033 }
2034
2035 void
2036 updatewindowtype(Client *c) {
2037 Atom state = getatomprop(c, netatom[NetWMState]);
2038 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2039
2040 if(state == netatom[NetWMFullscreen])
2041 setfullscreen(c, True);
2042 if(wtype == netatom[NetWMWindowTypeDialog])
2043 c->isfloating = True;
2044 }
2045
2046 void
2047 updatewmhints(Client *c) {
2048 XWMHints *wmh;
2049
2050 if((wmh = XGetWMHints(dpy, c->win))) {
2051 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2052 wmh->flags &= ~XUrgencyHint;
2053 XSetWMHints(dpy, c->win, wmh);
2054 }
2055 else
2056 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2057 if(wmh->flags & InputHint)
2058 c->neverfocus = !wmh->input;
2059 else
2060 c->neverfocus = False;
2061 XFree(wmh);
2062 }
2063 }
2064
2065 void
2066 view(const Arg *arg) {
2067 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2068 return;
2069 selmon->seltags ^= 1; /* toggle sel tagset */
2070 if(arg->ui & TAGMASK)
2071 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2072 focus(NULL);
2073 arrange(selmon);
2074 }
2075
2076 Client *
2077 wintoclient(Window w) {
2078 Client *c;
2079 Monitor *m;
2080
2081 for(m = mons; m; m = m->next)
2082 for(c = m->clients; c; c = c->next)
2083 if(c->win == w)
2084 return c;
2085 return NULL;
2086 }
2087
2088 Monitor *
2089 wintomon(Window w) {
2090 int x, y;
2091 Client *c;
2092 Monitor *m;
2093
2094 if(w == root && getrootptr(&x, &y))
2095 return recttomon(x, y, 1, 1);
2096 for(m = mons; m; m = m->next)
2097 if(w == m->barwin)
2098 return m;
2099 if((c = wintoclient(w)))
2100 return c->mon;
2101 return selmon;
2102 }
2103
2104 /* There's no way to check accesses to destroyed windows, thus those cases are
2105 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2106 * default error handler, which may call exit. */
2107 int
2108 xerror(Display *dpy, XErrorEvent *ee) {
2109 if(ee->error_code == BadWindow
2110 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2111 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2112 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2113 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2114 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2115 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2116 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2117 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2118 return 0;
2119 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2120 ee->request_code, ee->error_code);
2121 return xerrorxlib(dpy, ee); /* may call exit */
2122 }
2123
2124 int
2125 xerrordummy(Display *dpy, XErrorEvent *ee) {
2126 return 0;
2127 }
2128
2129 /* Startup Error handler to check if another window manager
2130 * is already running. */
2131 int
2132 xerrorstart(Display *dpy, XErrorEvent *ee) {
2133 die("dwm: another window manager is already running\n");
2134 return -1;
2135 }
2136
2137 void
2138 zoom(const Arg *arg) {
2139 Client *c = selmon->sel;
2140
2141 if(!selmon->lt[selmon->sellt]->arrange
2142 || (selmon->sel && selmon->sel->isfloating))
2143 return;
2144 if(c == nexttiled(selmon->clients))
2145 if(!c || !(c = nexttiled(c->next)))
2146 return;
2147 pop(c);
2148 }
2149
2150 int
2151 main(int argc, char *argv[]) {
2152 if(argc == 2 && !strcmp("-v", argv[1]))
2153 die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
2154 else if(argc != 1)
2155 die("usage: dwm [-v]\n");
2156 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2157 fputs("warning: no locale support\n", stderr);
2158 if(!(dpy = XOpenDisplay(NULL)))
2159 die("dwm: cannot open display\n");
2160 checkotherwm();
2161 setup();
2162 scan();
2163 run();
2164 cleanup();
2165 XCloseDisplay(dpy);
2166 return EXIT_SUCCESS;
2167 }