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