Xinqi Bao's Git

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