Xinqi Bao's Git

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