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