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