Xinqi Bao's Git

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