Xinqi Bao's Git

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