Added ability to select multiple tags for viewing/assignment.
[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);
355 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
356}
357
358int
359applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact)
360{
361 int baseismin;
362 Monitor *m = c->mon;
363
364 /* set minimum possible */
365 *w = MAX(1, *w);
366 *h = MAX(1, *h);
367 if (interact) {
368 if (*x > sw)
369 *x = sw - WIDTH(c);
370 if (*y > sh)
371 *y = sh - HEIGHT(c);
372 if (*x + *w + 2 * c->bw < 0)
373 *x = 0;
374 if (*y + *h + 2 * c->bw < 0)
375 *y = 0;
376 } else {
377 if (*x >= m->wx + m->ww)
378 *x = m->wx + m->ww - WIDTH(c);
379 if (*y >= m->wy + m->wh)
380 *y = m->wy + m->wh - HEIGHT(c);
381 if (*x + *w + 2 * c->bw <= m->wx)
382 *x = m->wx;
383 if (*y + *h + 2 * c->bw <= m->wy)
384 *y = m->wy;
385 }
386 if (*h < bh)
387 *h = bh;
388 if (*w < bh)
389 *w = bh;
390 if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
391 if (!c->hintsvalid)
392 updatesizehints(c);
393 /* see last two sentences in ICCCM 4.1.2.3 */
394 baseismin = c->basew == c->minw && c->baseh == c->minh;
395 if (!baseismin) { /* temporarily remove base dimensions */
396 *w -= c->basew;
397 *h -= c->baseh;
398 }
399 /* adjust for aspect limits */
400 if (c->mina > 0 && c->maxa > 0) {
401 if (c->maxa < (float)*w / *h)
402 *w = *h * c->maxa + 0.5;
403 else if (c->mina < (float)*h / *w)
404 *h = *w * c->mina + 0.5;
405 }
406 if (baseismin) { /* increment calculation requires this */
407 *w -= c->basew;
408 *h -= c->baseh;
409 }
410 /* adjust for increment value */
411 if (c->incw)
412 *w -= *w % c->incw;
413 if (c->inch)
414 *h -= *h % c->inch;
415 /* restore base dimensions */
416 *w = MAX(*w + c->basew, c->minw);
417 *h = MAX(*h + c->baseh, c->minh);
418 if (c->maxw)
419 *w = MIN(*w, c->maxw);
420 if (c->maxh)
421 *h = MIN(*h, c->maxh);
422 }
423 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
424}
425
426void
427arrange(Monitor *m)
428{
429 if (m)
430 showhide(m->stack);
431 else for (m = mons; m; m = m->next)
432 showhide(m->stack);
433 if (m) {
434 arrangemon(m);
435 restack(m);
436 } else for (m = mons; m; m = m->next)
437 arrangemon(m);
438}
439
440void
441arrangemon(Monitor *m)
442{
443 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
444 if (m->lt[m->sellt]->arrange)
445 m->lt[m->sellt]->arrange(m);
446}
447
448void
449attach(Client *c)
450{
451 c->next = c->mon->clients;
452 c->mon->clients = c;
453}
454
455void
456attachstack(Client *c)
457{
458 c->snext = c->mon->stack;
459 c->mon->stack = c;
460}
461
462void
463buttonpress(XEvent *e)
464{
465 unsigned int i, x, click;
466 Arg arg = {0};
467 Client *c;
468 Monitor *m;
469 XButtonPressedEvent *ev = &e->xbutton;
470
471 click = ClkRootWin;
472 /* focus monitor if necessary */
473 if ((m = wintomon(ev->window)) && m != selmon) {
474 unfocus(selmon->sel, 1);
475 selmon = m;
476 focus(NULL);
477 }
478 if (ev->window == selmon->barwin) {
479 i = x = 0;
480 do
481 x += TEXTW(tags[i]);
482 while (ev->x >= x && ++i < LENGTH(tags));
483 if (i < LENGTH(tags)) {
484 click = ClkTagBar;
485 arg.ui = 1 << i;
486 } else if (ev->x < x + TEXTW(selmon->ltsymbol))
487 click = ClkLtSymbol;
488 else if (ev->x > selmon->ww - (int)TEXTW(stext))
489 click = ClkStatusText;
490 else
491 click = ClkWinTitle;
492 } else if ((c = wintoclient(ev->window))) {
493 focus(c);
494 restack(selmon);
495 XAllowEvents(dpy, ReplayPointer, CurrentTime);
496 click = ClkClientWin;
497 }
498 for (i = 0; i < LENGTH(buttons); i++)
499 if (click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
500 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
501 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
502}
503
504void
505checkotherwm(void)
506{
507 xerrorxlib = XSetErrorHandler(xerrorstart);
508 /* this causes an error if some other window manager is running */
509 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
510 XSync(dpy, False);
511 XSetErrorHandler(xerror);
512 XSync(dpy, False);
513}
514
515void
516cleanup(void)
517{
518 Arg a = {.ui = ~0};
519 Layout foo = { "", NULL };
520 Monitor *m;
521 size_t i;
522
523 view(&a);
524 selmon->lt[selmon->sellt] = &foo;
525 for (m = mons; m; m = m->next)
526 while (m->stack)
527 unmanage(m->stack, 0);
528 XUngrabKey(dpy, AnyKey, AnyModifier, root);
529 while (mons)
530 cleanupmon(mons);
531 for (i = 0; i < CurLast; i++)
532 drw_cur_free(drw, cursor[i]);
533 for (i = 0; i < LENGTH(colors); i++)
534 free(scheme[i]);
535 free(scheme);
536 XDestroyWindow(dpy, wmcheckwin);
537 drw_free(drw);
538 XSync(dpy, False);
539 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
540 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
541}
542
543void
544cleanupmon(Monitor *mon)
545{
546 Monitor *m;
547
548 if (mon == mons)
549 mons = mons->next;
550 else {
551 for (m = mons; m && m->next != mon; m = m->next);
552 m->next = mon->next;
553 }
554 XUnmapWindow(dpy, mon->barwin);
555 XDestroyWindow(dpy, mon->barwin);
556 free(mon);
557}
558
559void
560clientmessage(XEvent *e)
561{
562 XClientMessageEvent *cme = &e->xclient;
563 Client *c = wintoclient(cme->window);
564
565 if (!c)
566 return;
567 if (cme->message_type == netatom[NetWMState]) {
568 if (cme->data.l[1] == netatom[NetWMFullscreen]
569 || cme->data.l[2] == netatom[NetWMFullscreen])
570 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
571 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
572 } else if (cme->message_type == netatom[NetActiveWindow]) {
573 if (c != selmon->sel && !c->isurgent)
574 seturgent(c, 1);
575 }
576}
577
578void
579configure(Client *c)
580{
581 XConfigureEvent ce;
582
583 ce.type = ConfigureNotify;
584 ce.display = dpy;
585 ce.event = c->win;
586 ce.window = c->win;
587 ce.x = c->x;
588 ce.y = c->y;
589 ce.width = c->w;
590 ce.height = c->h;
591 ce.border_width = c->bw;
592 ce.above = None;
593 ce.override_redirect = False;
594 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
595}
596
597void
598configurenotify(XEvent *e)
599{
600 Monitor *m;
601 Client *c;
602 XConfigureEvent *ev = &e->xconfigure;
603 int dirty;
604
605 /* TODO: updategeom handling sucks, needs to be simplified */
606 if (ev->window == root) {
607 dirty = (sw != ev->width || sh != ev->height);
608 sw = ev->width;
609 sh = ev->height;
610 if (updategeom() || dirty) {
611 drw_resize(drw, sw, bh);
612 updatebars();
613 for (m = mons; m; m = m->next) {
614 for (c = m->clients; c; c = c->next)
615 if (c->isfullscreen)
616 resizeclient(c, m->mx, m->my, m->mw, m->mh);
617 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
618 }
619 focus(NULL);
620 arrange(NULL);
621 }
622 }
623}
624
625void
626configurerequest(XEvent *e)
627{
628 Client *c;
629 Monitor *m;
630 XConfigureRequestEvent *ev = &e->xconfigurerequest;
631 XWindowChanges wc;
632
633 if ((c = wintoclient(ev->window))) {
634 if (ev->value_mask & CWBorderWidth)
635 c->bw = ev->border_width;
636 else if (c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
637 m = c->mon;
638 if (ev->value_mask & CWX) {
639 c->oldx = c->x;
640 c->x = m->mx + ev->x;
641 }
642 if (ev->value_mask & CWY) {
643 c->oldy = c->y;
644 c->y = m->my + ev->y;
645 }
646 if (ev->value_mask & CWWidth) {
647 c->oldw = c->w;
648 c->w = ev->width;
649 }
650 if (ev->value_mask & CWHeight) {
651 c->oldh = c->h;
652 c->h = ev->height;
653 }
654 if ((c->x + c->w) > m->mx + m->mw && c->isfloating)
655 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
656 if ((c->y + c->h) > m->my + m->mh && c->isfloating)
657 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
658 if ((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
659 configure(c);
660 if (ISVISIBLE(c))
661 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
662 } else
663 configure(c);
664 } else {
665 wc.x = ev->x;
666 wc.y = ev->y;
667 wc.width = ev->width;
668 wc.height = ev->height;
669 wc.border_width = ev->border_width;
670 wc.sibling = ev->above;
671 wc.stack_mode = ev->detail;
672 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
673 }
674 XSync(dpy, False);
675}
676
677Monitor *
678createmon(void)
679{
680 Monitor *m;
681
682 m = ecalloc(1, sizeof(Monitor));
683 m->tagset[0] = m->tagset[1] = 1;
684 m->mfact = mfact;
685 m->nmaster = nmaster;
686 m->showbar = showbar;
687 m->topbar = topbar;
688 m->lt[0] = &layouts[0];
689 m->lt[1] = &layouts[1 % LENGTH(layouts)];
690 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
691 return m;
692}
693
694void
695destroynotify(XEvent *e)
696{
697 Client *c;
698 XDestroyWindowEvent *ev = &e->xdestroywindow;
699
700 if ((c = wintoclient(ev->window)))
701 unmanage(c, 1);
702}
703
704void
705detach(Client *c)
706{
707 Client **tc;
708
709 for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
710 *tc = c->next;
711}
712
713void
714detachstack(Client *c)
715{
716 Client **tc, *t;
717
718 for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
719 *tc = c->snext;
720
721 if (c == c->mon->sel) {
722 for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
723 c->mon->sel = t;
724 }
725}
726
727Monitor *
728dirtomon(int dir)
729{
730 Monitor *m = NULL;
731
732 if (dir > 0) {
733 if (!(m = selmon->next))
734 m = mons;
735 } else if (selmon == mons)
736 for (m = mons; m->next; m = m->next);
737 else
738 for (m = mons; m->next != selmon; m = m->next);
739 return m;
740}
741
742void
743drawbar(Monitor *m)
744{
745 int x, w, tw = 0;
746 int boxs = drw->fonts->h / 9;
747 int boxw = drw->fonts->h / 6 + 2;
748 unsigned int i, occ = 0, urg = 0;
749 Client *c;
750
751 if (!m->showbar)
752 return;
753
754 /* draw status first so it can be overdrawn by tags later */
755 if (m == selmon) { /* status is only drawn on selected monitor */
756 drw_setscheme(drw, scheme[SchemeNorm]);
757 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
758 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
759 }
760
761 for (c = m->clients; c; c = c->next) {
762 occ |= c->tags;
763 if (c->isurgent)
764 urg |= c->tags;
765 }
766 x = 0;
767 for (i = 0; i < LENGTH(tags); i++) {
768 w = TEXTW(tags[i]);
769 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
770 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
771 if (occ & 1 << i)
772 drw_rect(drw, x + boxs, boxs, boxw, boxw,
773 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
774 urg & 1 << i);
775 x += w;
776 }
777 w = TEXTW(m->ltsymbol);
778 drw_setscheme(drw, scheme[SchemeNorm]);
779 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
780
781 if ((w = m->ww - tw - x) > bh) {
782 if (m->sel) {
516dd383
AT
783 /* fix overflow when window name is bigger than window width */
784 int mid = (m->ww - (int)TEXTW(m->sel->name)) / 2 - x;
785 /* make sure name will not overlap on tags even when it is very long */
786 mid = mid >= lrpad / 2 ? mid : lrpad / 2;
5715edf5 787 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
516dd383 788 drw_text(drw, x, 0, w, bh, mid, m->sel->name, 0);
5715edf5
AT
789 if (m->sel->isfloating)
790 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
791 } else {
792 drw_setscheme(drw, scheme[SchemeNorm]);
793 drw_rect(drw, x, 0, w, bh, 1, 1);
794 }
795 }
796 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
797}
798
799void
800drawbars(void)
801{
802 Monitor *m;
803
804 for (m = mons; m; m = m->next)
805 drawbar(m);
806}
807
808void
809enternotify(XEvent *e)
810{
811 Client *c;
812 Monitor *m;
813 XCrossingEvent *ev = &e->xcrossing;
814
815 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
816 return;
817 c = wintoclient(ev->window);
818 m = c ? c->mon : wintomon(ev->window);
819 if (m != selmon) {
820 unfocus(selmon->sel, 1);
821 selmon = m;
822 } else if (!c || c == selmon->sel)
823 return;
824 focus(c);
825}
826
827void
828expose(XEvent *e)
829{
830 Monitor *m;
831 XExposeEvent *ev = &e->xexpose;
832
833 if (ev->count == 0 && (m = wintomon(ev->window)))
834 drawbar(m);
835}
836
837void
838focus(Client *c)
839{
840 if (!c || !ISVISIBLE(c))
841 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
842 if (selmon->sel && selmon->sel != c)
843 unfocus(selmon->sel, 0);
844 if (c) {
845 if (c->mon != selmon)
846 selmon = c->mon;
847 if (c->isurgent)
848 seturgent(c, 0);
849 detachstack(c);
850 attachstack(c);
851 grabbuttons(c, 1);
852 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
853 setfocus(c);
854 } else {
855 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
856 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
857 }
858 selmon->sel = c;
859 drawbars();
860}
861
862/* there are some broken focus acquiring clients needing extra handling */
863void
864focusin(XEvent *e)
865{
866 XFocusChangeEvent *ev = &e->xfocus;
867
868 if (selmon->sel && ev->window != selmon->sel->win)
869 setfocus(selmon->sel);
870}
871
872void
873focusmon(const Arg *arg)
874{
875 Monitor *m;
876
877 if (!mons->next)
878 return;
879 if ((m = dirtomon(arg->i)) == selmon)
880 return;
881 unfocus(selmon->sel, 0);
882 selmon = m;
883 focus(NULL);
884}
885
886void
887focusstack(const Arg *arg)
888{
889 Client *c = NULL, *i;
890
891 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
892 return;
893 if (arg->i > 0) {
894 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
895 if (!c)
896 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
897 } else {
898 for (i = selmon->clients; i != selmon->sel; i = i->next)
899 if (ISVISIBLE(i))
900 c = i;
901 if (!c)
902 for (; i; i = i->next)
903 if (ISVISIBLE(i))
904 c = i;
905 }
906 if (c) {
907 focus(c);
908 restack(selmon);
909 }
910}
911
912Atom
913getatomprop(Client *c, Atom prop)
914{
915 int di;
916 unsigned long dl;
917 unsigned char *p = NULL;
918 Atom da, atom = None;
919
920 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
921 &da, &di, &dl, &dl, &p) == Success && p) {
922 atom = *(Atom *)p;
923 XFree(p);
924 }
925 return atom;
926}
927
928int
929getrootptr(int *x, int *y)
930{
931 int di;
932 unsigned int dui;
933 Window dummy;
934
935 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
936}
937
938long
939getstate(Window w)
940{
941 int format;
942 long result = -1;
943 unsigned char *p = NULL;
944 unsigned long n, extra;
945 Atom real;
946
947 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
948 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
949 return -1;
950 if (n != 0)
951 result = *p;
952 XFree(p);
953 return result;
954}
955
956int
957gettextprop(Window w, Atom atom, char *text, unsigned int size)
958{
959 char **list = NULL;
960 int n;
961 XTextProperty name;
962
963 if (!text || size == 0)
964 return 0;
965 text[0] = '\0';
966 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
967 return 0;
968 if (name.encoding == XA_STRING) {
969 strncpy(text, (char *)name.value, size - 1);
970 } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
971 strncpy(text, *list, size - 1);
972 XFreeStringList(list);
973 }
974 text[size - 1] = '\0';
975 XFree(name.value);
976 return 1;
977}
978
979void
980grabbuttons(Client *c, int focused)
981{
982 updatenumlockmask();
983 {
984 unsigned int i, j;
985 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
986 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
987 if (!focused)
988 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
989 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
990 for (i = 0; i < LENGTH(buttons); i++)
991 if (buttons[i].click == ClkClientWin)
992 for (j = 0; j < LENGTH(modifiers); j++)
993 XGrabButton(dpy, buttons[i].button,
994 buttons[i].mask | modifiers[j],
995 c->win, False, BUTTONMASK,
996 GrabModeAsync, GrabModeSync, None, None);
997 }
998}
999
1000void
1001grabkeys(void)
1002{
1003 updatenumlockmask();
1004 {
1005 unsigned int i, j;
1006 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1007 KeyCode code;
1008
1009 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1010 for (i = 0; i < LENGTH(keys); i++)
1011 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1012 for (j = 0; j < LENGTH(modifiers); j++)
1013 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1014 True, GrabModeAsync, GrabModeAsync);
1015 }
1016}
1017
1018void
1019incnmaster(const Arg *arg)
1020{
1021 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1022 arrange(selmon);
1023}
1024
1025#ifdef XINERAMA
1026static int
1027isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1028{
1029 while (n--)
1030 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1031 && unique[n].width == info->width && unique[n].height == info->height)
1032 return 0;
1033 return 1;
1034}
1035#endif /* XINERAMA */
1036
1037void
1038keypress(XEvent *e)
1039{
1040 unsigned int i;
1041 KeySym keysym;
1042 XKeyEvent *ev;
1043
1044 ev = &e->xkey;
1045 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1046 for (i = 0; i < LENGTH(keys); i++)
1047 if (keysym == keys[i].keysym
1048 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1049 && keys[i].func)
1050 keys[i].func(&(keys[i].arg));
1051}
1052
1053void
1054killclient(const Arg *arg)
1055{
1056 if (!selmon->sel)
1057 return;
1058 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1059 XGrabServer(dpy);
1060 XSetErrorHandler(xerrordummy);
1061 XSetCloseDownMode(dpy, DestroyAll);
1062 XKillClient(dpy, selmon->sel->win);
1063 XSync(dpy, False);
1064 XSetErrorHandler(xerror);
1065 XUngrabServer(dpy);
1066 }
1067}
1068
1069void
1070manage(Window w, XWindowAttributes *wa)
1071{
1072 Client *c, *t = NULL;
1073 Window trans = None;
1074 XWindowChanges wc;
1075
1076 c = ecalloc(1, sizeof(Client));
1077 c->win = w;
1078 /* geometry */
1079 c->x = c->oldx = wa->x;
1080 c->y = c->oldy = wa->y;
1081 c->w = c->oldw = wa->width;
1082 c->h = c->oldh = wa->height;
1083 c->oldbw = wa->border_width;
1084
1085 updatetitle(c);
1086 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1087 c->mon = t->mon;
1088 c->tags = t->tags;
1089 } else {
1090 c->mon = selmon;
1091 applyrules(c);
1092 }
1093
1094 if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
1095 c->x = c->mon->wx + c->mon->ww - WIDTH(c);
1096 if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
1097 c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
1098 c->x = MAX(c->x, c->mon->wx);
1099 c->y = MAX(c->y, c->mon->wy);
1100 c->bw = borderpx;
1101
1102 wc.border_width = c->bw;
1103 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1104 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1105 configure(c); /* propagates border_width, if size doesn't change */
1106 updatewindowtype(c);
1107 updatesizehints(c);
1108 updatewmhints(c);
1109 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1110 grabbuttons(c, 0);
1111 if (!c->isfloating)
1112 c->isfloating = c->oldstate = trans != None || c->isfixed;
1113 if (c->isfloating)
1114 XRaiseWindow(dpy, c->win);
1115 attach(c);
1116 attachstack(c);
1117 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1118 (unsigned char *) &(c->win), 1);
1119 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1120 setclientstate(c, NormalState);
1121 if (c->mon == selmon)
1122 unfocus(selmon->sel, 0);
1123 c->mon->sel = c;
1124 arrange(c->mon);
1125 XMapWindow(dpy, c->win);
1126 focus(NULL);
1127}
1128
1129void
1130mappingnotify(XEvent *e)
1131{
1132 XMappingEvent *ev = &e->xmapping;
1133
1134 XRefreshKeyboardMapping(ev);
1135 if (ev->request == MappingKeyboard)
1136 grabkeys();
1137}
1138
1139void
1140maprequest(XEvent *e)
1141{
1142 static XWindowAttributes wa;
1143 XMapRequestEvent *ev = &e->xmaprequest;
1144
1145 if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
1146 return;
1147 if (!wintoclient(ev->window))
1148 manage(ev->window, &wa);
1149}
1150
1151void
1152monocle(Monitor *m)
1153{
1154 unsigned int n = 0;
1155 Client *c;
1156
1157 for (c = m->clients; c; c = c->next)
1158 if (ISVISIBLE(c))
1159 n++;
1160 if (n > 0) /* override layout symbol */
1161 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1162 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1163 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1164}
1165
1166void
1167motionnotify(XEvent *e)
1168{
1169 static Monitor *mon = NULL;
1170 Monitor *m;
1171 XMotionEvent *ev = &e->xmotion;
1172
1173 if (ev->window != root)
1174 return;
1175 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1176 unfocus(selmon->sel, 1);
1177 selmon = m;
1178 focus(NULL);
1179 }
1180 mon = m;
1181}
1182
1183void
1184movemouse(const Arg *arg)
1185{
1186 int x, y, ocx, ocy, nx, ny;
1187 Client *c;
1188 Monitor *m;
1189 XEvent ev;
1190 Time lasttime = 0;
1191
1192 if (!(c = selmon->sel))
1193 return;
1194 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1195 return;
1196 restack(selmon);
1197 ocx = c->x;
1198 ocy = c->y;
1199 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1200 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1201 return;
1202 if (!getrootptr(&x, &y))
1203 return;
1204 do {
1205 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1206 switch(ev.type) {
1207 case ConfigureRequest:
1208 case Expose:
1209 case MapRequest:
1210 handler[ev.type](&ev);
1211 break;
1212 case MotionNotify:
1213 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1214 continue;
1215 lasttime = ev.xmotion.time;
1216
1217 nx = ocx + (ev.xmotion.x - x);
1218 ny = ocy + (ev.xmotion.y - y);
1219 if (abs(selmon->wx - nx) < snap)
1220 nx = selmon->wx;
1221 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1222 nx = selmon->wx + selmon->ww - WIDTH(c);
1223 if (abs(selmon->wy - ny) < snap)
1224 ny = selmon->wy;
1225 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1226 ny = selmon->wy + selmon->wh - HEIGHT(c);
1227 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1228 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1229 togglefloating(NULL);
1230 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1231 resize(c, nx, ny, c->w, c->h, 1);
1232 break;
1233 }
1234 } while (ev.type != ButtonRelease);
1235 XUngrabPointer(dpy, CurrentTime);
1236 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1237 sendmon(c, m);
1238 selmon = m;
1239 focus(NULL);
1240 }
1241}
1242
1243Client *
1244nexttiled(Client *c)
1245{
1246 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1247 return c;
1248}
1249
1250void
1251pop(Client *c)
1252{
1253 detach(c);
1254 attach(c);
1255 focus(c);
1256 arrange(c->mon);
1257}
1258
1259void
1260propertynotify(XEvent *e)
1261{
1262 Client *c;
1263 Window trans;
1264 XPropertyEvent *ev = &e->xproperty;
1265
1266 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1267 updatestatus();
1268 else if (ev->state == PropertyDelete)
1269 return; /* ignore */
1270 else if ((c = wintoclient(ev->window))) {
1271 switch(ev->atom) {
1272 default: break;
1273 case XA_WM_TRANSIENT_FOR:
1274 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1275 (c->isfloating = (wintoclient(trans)) != NULL))
1276 arrange(c->mon);
1277 break;
1278 case XA_WM_NORMAL_HINTS:
1279 c->hintsvalid = 0;
1280 break;
1281 case XA_WM_HINTS:
1282 updatewmhints(c);
1283 drawbars();
1284 break;
1285 }
1286 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1287 updatetitle(c);
1288 if (c == c->mon->sel)
1289 drawbar(c->mon);
1290 }
1291 if (ev->atom == netatom[NetWMWindowType])
1292 updatewindowtype(c);
1293 }
1294}
1295
1296void
1297quit(const Arg *arg)
1298{
1299 running = 0;
1300}
1301
1302Monitor *
1303recttomon(int x, int y, int w, int h)
1304{
1305 Monitor *m, *r = selmon;
1306 int a, area = 0;
1307
1308 for (m = mons; m; m = m->next)
1309 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1310 area = a;
1311 r = m;
1312 }
1313 return r;
1314}
1315
1316void
1317resize(Client *c, int x, int y, int w, int h, int interact)
1318{
1319 if (applysizehints(c, &x, &y, &w, &h, interact))
1320 resizeclient(c, x, y, w, h);
1321}
1322
1323void
1324resizeclient(Client *c, int x, int y, int w, int h)
1325{
1326 XWindowChanges wc;
1327
1328 c->oldx = c->x; c->x = wc.x = x;
1329 c->oldy = c->y; c->y = wc.y = y;
1330 c->oldw = c->w; c->w = wc.width = w;
1331 c->oldh = c->h; c->h = wc.height = h;
1332 wc.border_width = c->bw;
1333 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1334 configure(c);
1335 XSync(dpy, False);
1336}
1337
1338void
1339resizemouse(const Arg *arg)
1340{
1341 int ocx, ocy, nw, nh;
1342 Client *c;
1343 Monitor *m;
1344 XEvent ev;
1345 Time lasttime = 0;
1346
1347 if (!(c = selmon->sel))
1348 return;
1349 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1350 return;
1351 restack(selmon);
1352 ocx = c->x;
1353 ocy = c->y;
1354 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1355 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1356 return;
1357 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1358 do {
1359 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1360 switch(ev.type) {
1361 case ConfigureRequest:
1362 case Expose:
1363 case MapRequest:
1364 handler[ev.type](&ev);
1365 break;
1366 case MotionNotify:
1367 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1368 continue;
1369 lasttime = ev.xmotion.time;
1370
1371 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1372 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1373 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1374 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1375 {
1376 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1377 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1378 togglefloating(NULL);
1379 }
1380 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1381 resize(c, c->x, c->y, nw, nh, 1);
1382 break;
1383 }
1384 } while (ev.type != ButtonRelease);
1385 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1386 XUngrabPointer(dpy, CurrentTime);
1387 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1388 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1389 sendmon(c, m);
1390 selmon = m;
1391 focus(NULL);
1392 }
1393}
1394
1395void
1396restack(Monitor *m)
1397{
1398 Client *c;
1399 XEvent ev;
1400 XWindowChanges wc;
1401
1402 drawbar(m);
1403 if (!m->sel)
1404 return;
1405 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1406 XRaiseWindow(dpy, m->sel->win);
1407 if (m->lt[m->sellt]->arrange) {
1408 wc.stack_mode = Below;
1409 wc.sibling = m->barwin;
1410 for (c = m->stack; c; c = c->snext)
1411 if (!c->isfloating && ISVISIBLE(c)) {
1412 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1413 wc.sibling = c->win;
1414 }
1415 }
1416 XSync(dpy, False);
1417 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1418}
1419
1420void
1421run(void)
1422{
1423 XEvent ev;
1424 /* main event loop */
1425 XSync(dpy, False);
1426 while (running && !XNextEvent(dpy, &ev))
1427 if (handler[ev.type])
1428 handler[ev.type](&ev); /* call handler */
1429}
1430
1431void
1432scan(void)
1433{
1434 unsigned int i, num;
1435 Window d1, d2, *wins = NULL;
1436 XWindowAttributes wa;
1437
1438 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1439 for (i = 0; i < num; i++) {
1440 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1441 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1442 continue;
1443 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1444 manage(wins[i], &wa);
1445 }
1446 for (i = 0; i < num; i++) { /* now the transients */
1447 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1448 continue;
1449 if (XGetTransientForHint(dpy, wins[i], &d1)
1450 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1451 manage(wins[i], &wa);
1452 }
1453 if (wins)
1454 XFree(wins);
1455 }
1456}
1457
1458void
1459sendmon(Client *c, Monitor *m)
1460{
1461 if (c->mon == m)
1462 return;
1463 unfocus(c, 1);
1464 detach(c);
1465 detachstack(c);
1466 c->mon = m;
1467 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1468 attach(c);
1469 attachstack(c);
1470 focus(NULL);
1471 arrange(NULL);
1472}
1473
1474void
1475setclientstate(Client *c, long state)
1476{
1477 long data[] = { state, None };
1478
1479 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1480 PropModeReplace, (unsigned char *)data, 2);
1481}
1482
1483int
1484sendevent(Client *c, Atom proto)
1485{
1486 int n;
1487 Atom *protocols;
1488 int exists = 0;
1489 XEvent ev;
1490
1491 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1492 while (!exists && n--)
1493 exists = protocols[n] == proto;
1494 XFree(protocols);
1495 }
1496 if (exists) {
1497 ev.type = ClientMessage;
1498 ev.xclient.window = c->win;
1499 ev.xclient.message_type = wmatom[WMProtocols];
1500 ev.xclient.format = 32;
1501 ev.xclient.data.l[0] = proto;
1502 ev.xclient.data.l[1] = CurrentTime;
1503 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1504 }
1505 return exists;
1506}
1507
1508void
1509setfocus(Client *c)
1510{
1511 if (!c->neverfocus) {
1512 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1513 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1514 XA_WINDOW, 32, PropModeReplace,
1515 (unsigned char *) &(c->win), 1);
1516 }
1517 sendevent(c, wmatom[WMTakeFocus]);
1518}
1519
1520void
1521setfullscreen(Client *c, int fullscreen)
1522{
1523 if (fullscreen && !c->isfullscreen) {
1524 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1525 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1526 c->isfullscreen = 1;
1527 c->oldstate = c->isfloating;
1528 c->oldbw = c->bw;
1529 c->bw = 0;
1530 c->isfloating = 1;
1531 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1532 XRaiseWindow(dpy, c->win);
1533 } else if (!fullscreen && c->isfullscreen){
1534 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1535 PropModeReplace, (unsigned char*)0, 0);
1536 c->isfullscreen = 0;
1537 c->isfloating = c->oldstate;
1538 c->bw = c->oldbw;
1539 c->x = c->oldx;
1540 c->y = c->oldy;
1541 c->w = c->oldw;
1542 c->h = c->oldh;
1543 resizeclient(c, c->x, c->y, c->w, c->h);
1544 arrange(c->mon);
1545 }
1546}
1547
1548void
1549setlayout(const Arg *arg)
1550{
1551 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1552 selmon->sellt ^= 1;
1553 if (arg && arg->v)
1554 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1555 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1556 if (selmon->sel)
1557 arrange(selmon);
1558 else
1559 drawbar(selmon);
1560}
1561
1562/* arg > 1.0 will set mfact absolutely */
1563void
1564setmfact(const Arg *arg)
1565{
1566 float f;
1567
1568 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1569 return;
1570 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1571 if (f < 0.05 || f > 0.95)
1572 return;
1573 selmon->mfact = f;
1574 arrange(selmon);
1575}
1576
1577void
1578setup(void)
1579{
1580 int i;
1581 XSetWindowAttributes wa;
1582 Atom utf8string;
1583
1584 /* clean up any zombies immediately */
1585 sigchld(0);
1586
1587 /* init screen */
1588 screen = DefaultScreen(dpy);
1589 sw = DisplayWidth(dpy, screen);
1590 sh = DisplayHeight(dpy, screen);
1591 root = RootWindow(dpy, screen);
1592 drw = drw_create(dpy, screen, root, sw, sh);
1593 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1594 die("no fonts could be loaded.");
1595 lrpad = drw->fonts->h;
1596 bh = drw->fonts->h + 2;
1597 updategeom();
1598 /* init atoms */
1599 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1600 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1601 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1602 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1603 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1604 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1605 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1606 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1607 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1608 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1609 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1610 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1611 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1612 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1613 /* init cursors */
1614 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1615 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1616 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1617 /* init appearance */
1618 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1619 for (i = 0; i < LENGTH(colors); i++)
1620 scheme[i] = drw_scm_create(drw, colors[i], 3);
1621 /* init bars */
1622 updatebars();
1623 updatestatus();
1624 /* supporting window for NetWMCheck */
1625 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1626 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1627 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1628 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1629 PropModeReplace, (unsigned char *) "dwm", 3);
1630 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1631 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1632 /* EWMH support per view */
1633 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1634 PropModeReplace, (unsigned char *) netatom, NetLast);
1635 XDeleteProperty(dpy, root, netatom[NetClientList]);
1636 /* select events */
1637 wa.cursor = cursor[CurNormal]->cursor;
1638 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1639 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1640 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1641 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1642 XSelectInput(dpy, root, wa.event_mask);
1643 grabkeys();
1644 focus(NULL);
1645}
1646
1647void
1648seturgent(Client *c, int urg)
1649{
1650 XWMHints *wmh;
1651
1652 c->isurgent = urg;
1653 if (!(wmh = XGetWMHints(dpy, c->win)))
1654 return;
1655 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1656 XSetWMHints(dpy, c->win, wmh);
1657 XFree(wmh);
1658}
1659
1660void
1661showhide(Client *c)
1662{
1663 if (!c)
1664 return;
1665 if (ISVISIBLE(c)) {
1666 /* show clients top down */
1667 XMoveWindow(dpy, c->win, c->x, c->y);
1668 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1669 resize(c, c->x, c->y, c->w, c->h, 0);
1670 showhide(c->snext);
1671 } else {
1672 /* hide clients bottom up */
1673 showhide(c->snext);
1674 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1675 }
1676}
1677
1678void
1679sigchld(int unused)
1680{
1681 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1682 die("can't install SIGCHLD handler:");
1683 while (0 < waitpid(-1, NULL, WNOHANG));
1684}
1685
1686void
1687spawn(const Arg *arg)
1688{
1689 if (fork() == 0) {
1690 if (dpy)
1691 close(ConnectionNumber(dpy));
1692 setsid();
1693 execvp(((char **)arg->v)[0], (char **)arg->v);
1694 die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
1695 }
1696}
1697
1698void
1699tag(const Arg *arg)
1700{
1701 if (selmon->sel && arg->ui & TAGMASK) {
1702 selmon->sel->tags = arg->ui & TAGMASK;
1703 focus(NULL);
1704 arrange(selmon);
1705 }
1706}
1707
1708void
1709tagmon(const Arg *arg)
1710{
1711 if (!selmon->sel || !mons->next)
1712 return;
1713 sendmon(selmon->sel, dirtomon(arg->i));
1714}
1715
1716void
1717tile(Monitor *m)
1718{
1719 unsigned int i, n, h, mw, my, ty;
1720 Client *c;
1721
1722 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1723 if (n == 0)
1724 return;
1725
1726 if (n > m->nmaster)
1727 mw = m->nmaster ? m->ww * m->mfact : 0;
1728 else
1729 mw = m->ww;
1730 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1731 if (i < m->nmaster) {
1732 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1733 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1734 if (my + HEIGHT(c) < m->wh)
1735 my += HEIGHT(c);
1736 } else {
1737 h = (m->wh - ty) / (n - i);
1738 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1739 if (ty + HEIGHT(c) < m->wh)
1740 ty += HEIGHT(c);
1741 }
1742}
1743
1744void
1745togglebar(const Arg *arg)
1746{
1747 selmon->showbar = !selmon->showbar;
1748 updatebarpos(selmon);
1749 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1750 arrange(selmon);
1751}
1752
1753void
1754togglefloating(const Arg *arg)
1755{
1756 if (!selmon->sel)
1757 return;
1758 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1759 return;
1760 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1761 if (selmon->sel->isfloating)
1762 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1763 selmon->sel->w, selmon->sel->h, 0);
1764 arrange(selmon);
1765}
1766
1767void
1768toggletag(const Arg *arg)
1769{
1770 unsigned int newtags;
1771
1772 if (!selmon->sel)
1773 return;
1774 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1775 if (newtags) {
1776 selmon->sel->tags = newtags;
1777 focus(NULL);
1778 arrange(selmon);
1779 }
1780}
1781
1782void
1783toggleview(const Arg *arg)
1784{
1785 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1786
1787 if (newtagset) {
1788 selmon->tagset[selmon->seltags] = newtagset;
1789 focus(NULL);
1790 arrange(selmon);
1791 }
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{
2085 if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2086 return;
2087 selmon->seltags ^= 1; /* toggle sel tagset */
2088 if (arg->ui & TAGMASK)
2089 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2090 focus(NULL);
2091 arrange(selmon);
2092}
2093
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}