Xinqi Bao's Git

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