Xinqi Bao's Git

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