Xinqi Bao's Git

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