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