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