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