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