Added a prompt when exiting dwm, allowing the user to exit/reload/cancel.
[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{
751 int x, w, tw = 0;
752 int boxs = drw->fonts->h / 9;
753 int boxw = drw->fonts->h / 6 + 2;
754 unsigned int i, occ = 0, urg = 0;
755 Client *c;
756
757 if (!m->showbar)
758 return;
759
760 /* draw status first so it can be overdrawn by tags later */
761 if (m == selmon) { /* status is only drawn on selected monitor */
762 drw_setscheme(drw, scheme[SchemeNorm]);
763 tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
764 drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
765 }
766
767 for (c = m->clients; c; c = c->next) {
768 occ |= c->tags;
769 if (c->isurgent)
770 urg |= c->tags;
771 }
772 x = 0;
773 for (i = 0; i < LENGTH(tags); i++) {
774 w = TEXTW(tags[i]);
775 drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
776 drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
777 if (occ & 1 << i)
778 drw_rect(drw, x + boxs, boxs, boxw, boxw,
779 m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
780 urg & 1 << i);
781 x += w;
782 }
783 w = TEXTW(m->ltsymbol);
784 drw_setscheme(drw, scheme[SchemeNorm]);
785 x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
786
787 if ((w = m->ww - tw - x) > bh) {
788 if (m->sel) {
516dd383
AT
789 /* fix overflow when window name is bigger than window width */
790 int mid = (m->ww - (int)TEXTW(m->sel->name)) / 2 - x;
791 /* make sure name will not overlap on tags even when it is very long */
792 mid = mid >= lrpad / 2 ? mid : lrpad / 2;
5715edf5 793 drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
516dd383 794 drw_text(drw, x, 0, w, bh, mid, m->sel->name, 0);
5715edf5
AT
795 if (m->sel->isfloating)
796 drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
797 } else {
798 drw_setscheme(drw, scheme[SchemeNorm]);
799 drw_rect(drw, x, 0, w, bh, 1, 1);
800 }
801 }
802 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
803}
804
805void
806drawbars(void)
807{
808 Monitor *m;
809
810 for (m = mons; m; m = m->next)
811 drawbar(m);
812}
813
814void
815enternotify(XEvent *e)
816{
817 Client *c;
818 Monitor *m;
819 XCrossingEvent *ev = &e->xcrossing;
820
821 if ((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
822 return;
823 c = wintoclient(ev->window);
824 m = c ? c->mon : wintomon(ev->window);
825 if (m != selmon) {
826 unfocus(selmon->sel, 1);
827 selmon = m;
828 } else if (!c || c == selmon->sel)
829 return;
830 focus(c);
831}
832
833void
834expose(XEvent *e)
835{
836 Monitor *m;
837 XExposeEvent *ev = &e->xexpose;
838
839 if (ev->count == 0 && (m = wintomon(ev->window)))
840 drawbar(m);
841}
842
843void
844focus(Client *c)
845{
846 if (!c || !ISVISIBLE(c))
847 for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
848 if (selmon->sel && selmon->sel != c)
849 unfocus(selmon->sel, 0);
850 if (c) {
851 if (c->mon != selmon)
852 selmon = c->mon;
853 if (c->isurgent)
854 seturgent(c, 0);
855 detachstack(c);
856 attachstack(c);
857 grabbuttons(c, 1);
858 XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel);
859 setfocus(c);
860 } else {
861 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
862 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
863 }
864 selmon->sel = c;
865 drawbars();
866}
867
868/* there are some broken focus acquiring clients needing extra handling */
869void
870focusin(XEvent *e)
871{
872 XFocusChangeEvent *ev = &e->xfocus;
873
874 if (selmon->sel && ev->window != selmon->sel->win)
875 setfocus(selmon->sel);
876}
877
878void
879focusmon(const Arg *arg)
880{
881 Monitor *m;
882
883 if (!mons->next)
884 return;
885 if ((m = dirtomon(arg->i)) == selmon)
886 return;
887 unfocus(selmon->sel, 0);
888 selmon = m;
889 focus(NULL);
890}
891
892void
893focusstack(const Arg *arg)
894{
895 Client *c = NULL, *i;
896
897 if (!selmon->sel || (selmon->sel->isfullscreen && lockfullscreen))
898 return;
899 if (arg->i > 0) {
900 for (c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
901 if (!c)
902 for (c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
903 } else {
904 for (i = selmon->clients; i != selmon->sel; i = i->next)
905 if (ISVISIBLE(i))
906 c = i;
907 if (!c)
908 for (; i; i = i->next)
909 if (ISVISIBLE(i))
910 c = i;
911 }
912 if (c) {
913 focus(c);
914 restack(selmon);
915 }
916}
917
918Atom
919getatomprop(Client *c, Atom prop)
920{
921 int di;
922 unsigned long dl;
923 unsigned char *p = NULL;
924 Atom da, atom = None;
925
926 if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
927 &da, &di, &dl, &dl, &p) == Success && p) {
928 atom = *(Atom *)p;
929 XFree(p);
930 }
931 return atom;
932}
933
934int
935getrootptr(int *x, int *y)
936{
937 int di;
938 unsigned int dui;
939 Window dummy;
940
941 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
942}
943
944long
945getstate(Window w)
946{
947 int format;
948 long result = -1;
949 unsigned char *p = NULL;
950 unsigned long n, extra;
951 Atom real;
952
953 if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
954 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
955 return -1;
956 if (n != 0)
957 result = *p;
958 XFree(p);
959 return result;
960}
961
962int
963gettextprop(Window w, Atom atom, char *text, unsigned int size)
964{
965 char **list = NULL;
966 int n;
967 XTextProperty name;
968
969 if (!text || size == 0)
970 return 0;
971 text[0] = '\0';
972 if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
973 return 0;
974 if (name.encoding == XA_STRING) {
975 strncpy(text, (char *)name.value, size - 1);
976 } else if (XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
977 strncpy(text, *list, size - 1);
978 XFreeStringList(list);
979 }
980 text[size - 1] = '\0';
981 XFree(name.value);
982 return 1;
983}
984
985void
986grabbuttons(Client *c, int focused)
987{
988 updatenumlockmask();
989 {
990 unsigned int i, j;
991 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
992 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
993 if (!focused)
994 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
995 BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
996 for (i = 0; i < LENGTH(buttons); i++)
997 if (buttons[i].click == ClkClientWin)
998 for (j = 0; j < LENGTH(modifiers); j++)
999 XGrabButton(dpy, buttons[i].button,
1000 buttons[i].mask | modifiers[j],
1001 c->win, False, BUTTONMASK,
1002 GrabModeAsync, GrabModeSync, None, None);
1003 }
1004}
1005
1006void
1007grabkeys(void)
1008{
1009 updatenumlockmask();
1010 {
1011 unsigned int i, j;
1012 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1013 KeyCode code;
1014
1015 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1016 for (i = 0; i < LENGTH(keys); i++)
1017 if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1018 for (j = 0; j < LENGTH(modifiers); j++)
1019 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1020 True, GrabModeAsync, GrabModeAsync);
1021 }
1022}
1023
1024void
1025incnmaster(const Arg *arg)
1026{
1027 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1028 arrange(selmon);
1029}
1030
1031#ifdef XINERAMA
1032static int
1033isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
1034{
1035 while (n--)
1036 if (unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1037 && unique[n].width == info->width && unique[n].height == info->height)
1038 return 0;
1039 return 1;
1040}
1041#endif /* XINERAMA */
1042
1043void
1044keypress(XEvent *e)
1045{
1046 unsigned int i;
1047 KeySym keysym;
1048 XKeyEvent *ev;
1049
1050 ev = &e->xkey;
1051 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1052 for (i = 0; i < LENGTH(keys); i++)
1053 if (keysym == keys[i].keysym
1054 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1055 && keys[i].func)
1056 keys[i].func(&(keys[i].arg));
1057}
1058
1059void
1060killclient(const Arg *arg)
1061{
1062 if (!selmon->sel)
1063 return;
1064 if (!sendevent(selmon->sel, wmatom[WMDelete])) {
1065 XGrabServer(dpy);
1066 XSetErrorHandler(xerrordummy);
1067 XSetCloseDownMode(dpy, DestroyAll);
1068 XKillClient(dpy, selmon->sel->win);
1069 XSync(dpy, False);
1070 XSetErrorHandler(xerror);
1071 XUngrabServer(dpy);
1072 }
1073}
1074
9813966c
AT
1075void
1076layoutmenu(const Arg *arg) {
1077 FILE *p;
1078 char c[3], *s;
1079 int i;
1080
1081 if (!(p = popen(layoutmenu_cmd, "r")))
1082 return;
1083 s = fgets(c, sizeof(c), p);
1084 pclose(p);
1085
1086 if (!s || *s == '\0' || c[0] == '\0')
1087 return;
1088
1089 i = atoi(c);
1090 setlayout(&((Arg) { .v = &layouts[i] }));
1091}
1092
5715edf5
AT
1093void
1094manage(Window w, XWindowAttributes *wa)
1095{
1096 Client *c, *t = NULL;
1097 Window trans = None;
1098 XWindowChanges wc;
1099
1100 c = ecalloc(1, sizeof(Client));
1101 c->win = w;
1102 /* geometry */
1103 c->x = c->oldx = wa->x;
1104 c->y = c->oldy = wa->y;
1105 c->w = c->oldw = wa->width;
1106 c->h = c->oldh = wa->height;
1107 c->oldbw = wa->border_width;
1108
1109 updatetitle(c);
1110 if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1111 c->mon = t->mon;
1112 c->tags = t->tags;
1113 } else {
1114 c->mon = selmon;
1115 applyrules(c);
1116 }
1117
1118 if (c->x + WIDTH(c) > c->mon->wx + c->mon->ww)
1119 c->x = c->mon->wx + c->mon->ww - WIDTH(c);
1120 if (c->y + HEIGHT(c) > c->mon->wy + c->mon->wh)
1121 c->y = c->mon->wy + c->mon->wh - HEIGHT(c);
1122 c->x = MAX(c->x, c->mon->wx);
1123 c->y = MAX(c->y, c->mon->wy);
1124 c->bw = borderpx;
1125
1126 wc.border_width = c->bw;
1127 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1128 XSetWindowBorder(dpy, w, scheme[SchemeNorm][ColBorder].pixel);
1129 configure(c); /* propagates border_width, if size doesn't change */
1130 updatewindowtype(c);
1131 updatesizehints(c);
1132 updatewmhints(c);
1133 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1134 grabbuttons(c, 0);
1135 if (!c->isfloating)
1136 c->isfloating = c->oldstate = trans != None || c->isfixed;
1137 if (c->isfloating)
1138 XRaiseWindow(dpy, c->win);
1139 attach(c);
1140 attachstack(c);
1141 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1142 (unsigned char *) &(c->win), 1);
1143 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1144 setclientstate(c, NormalState);
1145 if (c->mon == selmon)
1146 unfocus(selmon->sel, 0);
1147 c->mon->sel = c;
1148 arrange(c->mon);
1149 XMapWindow(dpy, c->win);
1150 focus(NULL);
1151}
1152
1153void
1154mappingnotify(XEvent *e)
1155{
1156 XMappingEvent *ev = &e->xmapping;
1157
1158 XRefreshKeyboardMapping(ev);
1159 if (ev->request == MappingKeyboard)
1160 grabkeys();
1161}
1162
1163void
1164maprequest(XEvent *e)
1165{
1166 static XWindowAttributes wa;
1167 XMapRequestEvent *ev = &e->xmaprequest;
1168
1169 if (!XGetWindowAttributes(dpy, ev->window, &wa) || wa.override_redirect)
1170 return;
1171 if (!wintoclient(ev->window))
1172 manage(ev->window, &wa);
1173}
1174
1175void
1176monocle(Monitor *m)
1177{
1178 unsigned int n = 0;
1179 Client *c;
1180
1181 for (c = m->clients; c; c = c->next)
1182 if (ISVISIBLE(c))
1183 n++;
1184 if (n > 0) /* override layout symbol */
1185 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1186 for (c = nexttiled(m->clients); c; c = nexttiled(c->next))
1187 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, 0);
1188}
1189
1190void
1191motionnotify(XEvent *e)
1192{
1193 static Monitor *mon = NULL;
1194 Monitor *m;
1195 XMotionEvent *ev = &e->xmotion;
1196
1197 if (ev->window != root)
1198 return;
1199 if ((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1200 unfocus(selmon->sel, 1);
1201 selmon = m;
1202 focus(NULL);
1203 }
1204 mon = m;
1205}
1206
1207void
1208movemouse(const Arg *arg)
1209{
1210 int x, y, ocx, ocy, nx, ny;
1211 Client *c;
1212 Monitor *m;
1213 XEvent ev;
1214 Time lasttime = 0;
1215
1216 if (!(c = selmon->sel))
1217 return;
1218 if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
1219 return;
1220 restack(selmon);
1221 ocx = c->x;
1222 ocy = c->y;
1223 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1224 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1225 return;
1226 if (!getrootptr(&x, &y))
1227 return;
1228 do {
1229 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1230 switch(ev.type) {
1231 case ConfigureRequest:
1232 case Expose:
1233 case MapRequest:
1234 handler[ev.type](&ev);
1235 break;
1236 case MotionNotify:
1237 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1238 continue;
1239 lasttime = ev.xmotion.time;
1240
1241 nx = ocx + (ev.xmotion.x - x);
1242 ny = ocy + (ev.xmotion.y - y);
1243 if (abs(selmon->wx - nx) < snap)
1244 nx = selmon->wx;
1245 else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1246 nx = selmon->wx + selmon->ww - WIDTH(c);
1247 if (abs(selmon->wy - ny) < snap)
1248 ny = selmon->wy;
1249 else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1250 ny = selmon->wy + selmon->wh - HEIGHT(c);
1251 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1252 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1253 togglefloating(NULL);
1254 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1255 resize(c, nx, ny, c->w, c->h, 1);
1256 break;
1257 }
1258 } while (ev.type != ButtonRelease);
1259 XUngrabPointer(dpy, CurrentTime);
1260 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1261 sendmon(c, m);
1262 selmon = m;
1263 focus(NULL);
1264 }
1265}
1266
c56e7560
AT
1267void
1268nametag(const Arg *arg) {
1269 char *p, name[MAX_TAGLEN];
1270 FILE *f;
1271 int i;
1272
1273 errno = 0; // popen(3p) says on failure it "may" set errno
1274 if(!(f = popen("dmenu < /dev/null", "r"))) {
1275 fprintf(stderr, "dwm: popen 'dmenu < /dev/null' failed%s%s\n", errno ? ": " : "", errno ? strerror(errno) : "");
1276 return;
1277 }
1278 if (!(p = fgets(name, MAX_TAGLEN, f)) && (i = errno) && ferror(f))
1279 fprintf(stderr, "dwm: fgets failed: %s\n", strerror(i));
1280 if (pclose(f) < 0)
1281 fprintf(stderr, "dwm: pclose failed: %s\n", strerror(errno));
1282 if(!p)
1283 return;
1284 if((p = strchr(name, '\n')))
1285 *p = '\0';
1286
1287 for(i = 0; i < LENGTH(tags); i++)
1288 if(selmon->tagset[selmon->seltags] & (1 << i))
1289 strcpy(tags[i], name);
1290 drawbars();
1291}
1292
5715edf5
AT
1293Client *
1294nexttiled(Client *c)
1295{
1296 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1297 return c;
1298}
1299
1300void
1301pop(Client *c)
1302{
1303 detach(c);
1304 attach(c);
1305 focus(c);
1306 arrange(c->mon);
1307}
1308
1309void
1310propertynotify(XEvent *e)
1311{
1312 Client *c;
1313 Window trans;
1314 XPropertyEvent *ev = &e->xproperty;
1315
1316 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1317 updatestatus();
1318 else if (ev->state == PropertyDelete)
1319 return; /* ignore */
1320 else if ((c = wintoclient(ev->window))) {
1321 switch(ev->atom) {
1322 default: break;
1323 case XA_WM_TRANSIENT_FOR:
1324 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1325 (c->isfloating = (wintoclient(trans)) != NULL))
1326 arrange(c->mon);
1327 break;
1328 case XA_WM_NORMAL_HINTS:
1329 c->hintsvalid = 0;
1330 break;
1331 case XA_WM_HINTS:
1332 updatewmhints(c);
1333 drawbars();
1334 break;
1335 }
1336 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1337 updatetitle(c);
1338 if (c == c->mon->sel)
1339 drawbar(c->mon);
1340 }
1341 if (ev->atom == netatom[NetWMWindowType])
1342 updatewindowtype(c);
1343 }
1344}
1345
1346void
1347quit(const Arg *arg)
1348{
1349 running = 0;
1350}
1351
1da78b82
AT
1352void
1353quitprompt(const Arg *arg)
1354{
1355 FILE *pp = popen("echo -e \"no\nrestart\nyes\" | dmenu -i -sb red -p \"Quit DWM?\"", "r");
1356 if(pp != NULL) {
1357 char buf[1024];
1358 if (fgets(buf, sizeof(buf), pp) == NULL) {
1359 fprintf(stderr, "Quitprompt: Error reading pipe!\n");
1360 return;
1361 }
1362 if (strcmp(buf, "yes\n") == 0) {
1363 pclose(pp);
1364 restart = 0;
1365 quit(NULL);
1366 } else if (strcmp(buf, "restart\n") == 0) {
1367 pclose(pp);
1368 restart = 1;
1369 quit(NULL);
1370 } else if (strcmp(buf, "no\n") == 0) {
1371 pclose(pp);
1372 return;
1373 }
1374 }
1375}
1376
5715edf5
AT
1377Monitor *
1378recttomon(int x, int y, int w, int h)
1379{
1380 Monitor *m, *r = selmon;
1381 int a, area = 0;
1382
1383 for (m = mons; m; m = m->next)
1384 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1385 area = a;
1386 r = m;
1387 }
1388 return r;
1389}
1390
1391void
1392resize(Client *c, int x, int y, int w, int h, int interact)
1393{
1394 if (applysizehints(c, &x, &y, &w, &h, interact))
1395 resizeclient(c, x, y, w, h);
1396}
1397
1398void
1399resizeclient(Client *c, int x, int y, int w, int h)
1400{
1401 XWindowChanges wc;
1402
1403 c->oldx = c->x; c->x = wc.x = x;
1404 c->oldy = c->y; c->y = wc.y = y;
1405 c->oldw = c->w; c->w = wc.width = w;
1406 c->oldh = c->h; c->h = wc.height = h;
1407 wc.border_width = c->bw;
1408 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1409 configure(c);
1410 XSync(dpy, False);
1411}
1412
1413void
1414resizemouse(const Arg *arg)
1415{
1416 int ocx, ocy, nw, nh;
1417 Client *c;
1418 Monitor *m;
1419 XEvent ev;
1420 Time lasttime = 0;
1421
1422 if (!(c = selmon->sel))
1423 return;
1424 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1425 return;
1426 restack(selmon);
1427 ocx = c->x;
1428 ocy = c->y;
1429 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1430 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1431 return;
1432 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1433 do {
1434 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1435 switch(ev.type) {
1436 case ConfigureRequest:
1437 case Expose:
1438 case MapRequest:
1439 handler[ev.type](&ev);
1440 break;
1441 case MotionNotify:
1442 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1443 continue;
1444 lasttime = ev.xmotion.time;
1445
1446 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1447 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1448 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1449 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1450 {
1451 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1452 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1453 togglefloating(NULL);
1454 }
1455 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1456 resize(c, c->x, c->y, nw, nh, 1);
1457 break;
1458 }
1459 } while (ev.type != ButtonRelease);
1460 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1461 XUngrabPointer(dpy, CurrentTime);
1462 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1463 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1464 sendmon(c, m);
1465 selmon = m;
1466 focus(NULL);
1467 }
1468}
1469
1470void
1471restack(Monitor *m)
1472{
1473 Client *c;
1474 XEvent ev;
1475 XWindowChanges wc;
1476
1477 drawbar(m);
1478 if (!m->sel)
1479 return;
1480 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1481 XRaiseWindow(dpy, m->sel->win);
1482 if (m->lt[m->sellt]->arrange) {
1483 wc.stack_mode = Below;
1484 wc.sibling = m->barwin;
1485 for (c = m->stack; c; c = c->snext)
1486 if (!c->isfloating && ISVISIBLE(c)) {
1487 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1488 wc.sibling = c->win;
1489 }
1490 }
1491 XSync(dpy, False);
1492 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1493}
1494
1495void
1496run(void)
1497{
1498 XEvent ev;
1499 /* main event loop */
1500 XSync(dpy, False);
1501 while (running && !XNextEvent(dpy, &ev))
1502 if (handler[ev.type])
1503 handler[ev.type](&ev); /* call handler */
1504}
1505
1506void
1507scan(void)
1508{
1509 unsigned int i, num;
1510 Window d1, d2, *wins = NULL;
1511 XWindowAttributes wa;
1512
1513 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1514 for (i = 0; i < num; i++) {
1515 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1516 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1517 continue;
1518 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1519 manage(wins[i], &wa);
1520 }
1521 for (i = 0; i < num; i++) { /* now the transients */
1522 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1523 continue;
1524 if (XGetTransientForHint(dpy, wins[i], &d1)
1525 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1526 manage(wins[i], &wa);
1527 }
1528 if (wins)
1529 XFree(wins);
1530 }
1531}
1532
1533void
1534sendmon(Client *c, Monitor *m)
1535{
1536 if (c->mon == m)
1537 return;
1538 unfocus(c, 1);
1539 detach(c);
1540 detachstack(c);
1541 c->mon = m;
ba835934 1542 c->tags = (m->tagset[m->seltags] ? m->tagset[m->seltags] : 1);
5715edf5
AT
1543 attach(c);
1544 attachstack(c);
1545 focus(NULL);
1546 arrange(NULL);
1547}
1548
1549void
1550setclientstate(Client *c, long state)
1551{
1552 long data[] = { state, None };
1553
1554 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1555 PropModeReplace, (unsigned char *)data, 2);
1556}
1557
1558int
1559sendevent(Client *c, Atom proto)
1560{
1561 int n;
1562 Atom *protocols;
1563 int exists = 0;
1564 XEvent ev;
1565
1566 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1567 while (!exists && n--)
1568 exists = protocols[n] == proto;
1569 XFree(protocols);
1570 }
1571 if (exists) {
1572 ev.type = ClientMessage;
1573 ev.xclient.window = c->win;
1574 ev.xclient.message_type = wmatom[WMProtocols];
1575 ev.xclient.format = 32;
1576 ev.xclient.data.l[0] = proto;
1577 ev.xclient.data.l[1] = CurrentTime;
1578 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1579 }
1580 return exists;
1581}
1582
1583void
1584setfocus(Client *c)
1585{
1586 if (!c->neverfocus) {
1587 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1588 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1589 XA_WINDOW, 32, PropModeReplace,
1590 (unsigned char *) &(c->win), 1);
1591 }
1592 sendevent(c, wmatom[WMTakeFocus]);
1593}
1594
1595void
1596setfullscreen(Client *c, int fullscreen)
1597{
1598 if (fullscreen && !c->isfullscreen) {
1599 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1600 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1601 c->isfullscreen = 1;
1602 c->oldstate = c->isfloating;
1603 c->oldbw = c->bw;
1604 c->bw = 0;
1605 c->isfloating = 1;
1606 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1607 XRaiseWindow(dpy, c->win);
1608 } else if (!fullscreen && c->isfullscreen){
1609 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1610 PropModeReplace, (unsigned char*)0, 0);
1611 c->isfullscreen = 0;
1612 c->isfloating = c->oldstate;
1613 c->bw = c->oldbw;
1614 c->x = c->oldx;
1615 c->y = c->oldy;
1616 c->w = c->oldw;
1617 c->h = c->oldh;
1618 resizeclient(c, c->x, c->y, c->w, c->h);
1619 arrange(c->mon);
1620 }
1621}
1622
1623void
1624setlayout(const Arg *arg)
1625{
1626 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1627 selmon->sellt ^= 1;
1628 if (arg && arg->v)
1629 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1630 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1631 if (selmon->sel)
1632 arrange(selmon);
1633 else
1634 drawbar(selmon);
1635}
1636
1637/* arg > 1.0 will set mfact absolutely */
1638void
1639setmfact(const Arg *arg)
1640{
1641 float f;
1642
1643 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1644 return;
1645 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1646 if (f < 0.05 || f > 0.95)
1647 return;
1648 selmon->mfact = f;
1649 arrange(selmon);
1650}
1651
1652void
1653setup(void)
1654{
1655 int i;
1656 XSetWindowAttributes wa;
1657 Atom utf8string;
1658
1659 /* clean up any zombies immediately */
1660 sigchld(0);
1661
1662 /* init screen */
1663 screen = DefaultScreen(dpy);
1664 sw = DisplayWidth(dpy, screen);
1665 sh = DisplayHeight(dpy, screen);
1666 root = RootWindow(dpy, screen);
1667 drw = drw_create(dpy, screen, root, sw, sh);
1668 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1669 die("no fonts could be loaded.");
1670 lrpad = drw->fonts->h;
1671 bh = drw->fonts->h + 2;
1672 updategeom();
1673 /* init atoms */
1674 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1675 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1676 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1677 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1678 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1679 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1680 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1681 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1682 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1683 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1684 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1685 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1686 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1687 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1688 /* init cursors */
1689 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1690 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1691 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1692 /* init appearance */
1693 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1694 for (i = 0; i < LENGTH(colors); i++)
1695 scheme[i] = drw_scm_create(drw, colors[i], 3);
1696 /* init bars */
1697 updatebars();
1698 updatestatus();
1699 /* supporting window for NetWMCheck */
1700 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1701 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1702 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1703 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1704 PropModeReplace, (unsigned char *) "dwm", 3);
1705 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1706 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1707 /* EWMH support per view */
1708 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1709 PropModeReplace, (unsigned char *) netatom, NetLast);
1710 XDeleteProperty(dpy, root, netatom[NetClientList]);
1711 /* select events */
1712 wa.cursor = cursor[CurNormal]->cursor;
1713 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1714 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1715 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1716 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1717 XSelectInput(dpy, root, wa.event_mask);
1718 grabkeys();
1719 focus(NULL);
1720}
1721
1722void
1723seturgent(Client *c, int urg)
1724{
1725 XWMHints *wmh;
1726
1727 c->isurgent = urg;
1728 if (!(wmh = XGetWMHints(dpy, c->win)))
1729 return;
1730 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1731 XSetWMHints(dpy, c->win, wmh);
1732 XFree(wmh);
1733}
1734
1735void
1736showhide(Client *c)
1737{
1738 if (!c)
1739 return;
1740 if (ISVISIBLE(c)) {
1741 /* show clients top down */
1742 XMoveWindow(dpy, c->win, c->x, c->y);
1743 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1744 resize(c, c->x, c->y, c->w, c->h, 0);
1745 showhide(c->snext);
1746 } else {
1747 /* hide clients bottom up */
1748 showhide(c->snext);
1749 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1750 }
1751}
1752
1753void
1754sigchld(int unused)
1755{
1756 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1757 die("can't install SIGCHLD handler:");
1758 while (0 < waitpid(-1, NULL, WNOHANG));
1759}
1760
1761void
1762spawn(const Arg *arg)
1763{
1764 if (fork() == 0) {
1765 if (dpy)
1766 close(ConnectionNumber(dpy));
1767 setsid();
1768 execvp(((char **)arg->v)[0], (char **)arg->v);
1769 die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
1770 }
1771}
1772
1773void
1774tag(const Arg *arg)
1775{
1776 if (selmon->sel && arg->ui & TAGMASK) {
1777 selmon->sel->tags = arg->ui & TAGMASK;
1778 focus(NULL);
1779 arrange(selmon);
1780 }
1781}
1782
1783void
1784tagmon(const Arg *arg)
1785{
1786 if (!selmon->sel || !mons->next)
1787 return;
1788 sendmon(selmon->sel, dirtomon(arg->i));
1789}
1790
1791void
1792tile(Monitor *m)
1793{
1794 unsigned int i, n, h, mw, my, ty;
1795 Client *c;
1796
1797 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1798 if (n == 0)
1799 return;
1800
1801 if (n > m->nmaster)
1802 mw = m->nmaster ? m->ww * m->mfact : 0;
1803 else
1804 mw = m->ww;
1805 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1806 if (i < m->nmaster) {
1807 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1808 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1809 if (my + HEIGHT(c) < m->wh)
1810 my += HEIGHT(c);
1811 } else {
1812 h = (m->wh - ty) / (n - i);
1813 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1814 if (ty + HEIGHT(c) < m->wh)
1815 ty += HEIGHT(c);
1816 }
1817}
1818
1819void
1820togglebar(const Arg *arg)
1821{
1822 selmon->showbar = !selmon->showbar;
1823 updatebarpos(selmon);
1824 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1825 arrange(selmon);
1826}
1827
1828void
1829togglefloating(const Arg *arg)
1830{
1831 if (!selmon->sel)
1832 return;
1833 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1834 return;
1835 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1836 if (selmon->sel->isfloating)
1837 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1838 selmon->sel->w, selmon->sel->h, 0);
1839 arrange(selmon);
1840}
1841
1842void
1843toggletag(const Arg *arg)
1844{
1845 unsigned int newtags;
1846
1847 if (!selmon->sel)
1848 return;
1849 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1850 if (newtags) {
1851 selmon->sel->tags = newtags;
1852 focus(NULL);
1853 arrange(selmon);
1854 }
1855}
1856
1857void
1858toggleview(const Arg *arg)
1859{
1860 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1861
ba835934
AT
1862 selmon->tagset[selmon->seltags] = newtagset;
1863 focus(NULL);
1864 arrange(selmon);
5715edf5
AT
1865}
1866
1867void
1868unfocus(Client *c, int setfocus)
1869{
1870 if (!c)
1871 return;
1872 grabbuttons(c, 0);
1873 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1874 if (setfocus) {
1875 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1876 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1877 }
1878}
1879
1880void
1881unmanage(Client *c, int destroyed)
1882{
1883 Monitor *m = c->mon;
1884 XWindowChanges wc;
1885
1886 detach(c);
1887 detachstack(c);
1888 if (!destroyed) {
1889 wc.border_width = c->oldbw;
1890 XGrabServer(dpy); /* avoid race conditions */
1891 XSetErrorHandler(xerrordummy);
1892 XSelectInput(dpy, c->win, NoEventMask);
1893 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1894 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1895 setclientstate(c, WithdrawnState);
1896 XSync(dpy, False);
1897 XSetErrorHandler(xerror);
1898 XUngrabServer(dpy);
1899 }
1900 free(c);
1901 focus(NULL);
1902 updateclientlist();
1903 arrange(m);
1904}
1905
1906void
1907unmapnotify(XEvent *e)
1908{
1909 Client *c;
1910 XUnmapEvent *ev = &e->xunmap;
1911
1912 if ((c = wintoclient(ev->window))) {
1913 if (ev->send_event)
1914 setclientstate(c, WithdrawnState);
1915 else
1916 unmanage(c, 0);
1917 }
1918}
1919
1920void
1921updatebars(void)
1922{
1923 Monitor *m;
1924 XSetWindowAttributes wa = {
1925 .override_redirect = True,
1926 .background_pixmap = ParentRelative,
1927 .event_mask = ButtonPressMask|ExposureMask
1928 };
1929 XClassHint ch = {"dwm", "dwm"};
1930 for (m = mons; m; m = m->next) {
1931 if (m->barwin)
1932 continue;
1933 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1934 CopyFromParent, DefaultVisual(dpy, screen),
1935 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1936 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1937 XMapRaised(dpy, m->barwin);
1938 XSetClassHint(dpy, m->barwin, &ch);
1939 }
1940}
1941
1942void
1943updatebarpos(Monitor *m)
1944{
1945 m->wy = m->my;
1946 m->wh = m->mh;
1947 if (m->showbar) {
1948 m->wh -= bh;
1949 m->by = m->topbar ? m->wy : m->wy + m->wh;
1950 m->wy = m->topbar ? m->wy + bh : m->wy;
1951 } else
1952 m->by = -bh;
1953}
1954
1955void
1956updateclientlist()
1957{
1958 Client *c;
1959 Monitor *m;
1960
1961 XDeleteProperty(dpy, root, netatom[NetClientList]);
1962 for (m = mons; m; m = m->next)
1963 for (c = m->clients; c; c = c->next)
1964 XChangeProperty(dpy, root, netatom[NetClientList],
1965 XA_WINDOW, 32, PropModeAppend,
1966 (unsigned char *) &(c->win), 1);
1967}
1968
1969int
1970updategeom(void)
1971{
1972 int dirty = 0;
1973
1974#ifdef XINERAMA
1975 if (XineramaIsActive(dpy)) {
1976 int i, j, n, nn;
1977 Client *c;
1978 Monitor *m;
1979 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1980 XineramaScreenInfo *unique = NULL;
1981
1982 for (n = 0, m = mons; m; m = m->next, n++);
1983 /* only consider unique geometries as separate screens */
1984 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1985 for (i = 0, j = 0; i < nn; i++)
1986 if (isuniquegeom(unique, j, &info[i]))
1987 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1988 XFree(info);
1989 nn = j;
1990
1991 /* new monitors if nn > n */
1992 for (i = n; i < nn; i++) {
1993 for (m = mons; m && m->next; m = m->next);
1994 if (m)
1995 m->next = createmon();
1996 else
1997 mons = createmon();
1998 }
1999 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2000 if (i >= n
2001 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2002 || unique[i].width != m->mw || unique[i].height != m->mh)
2003 {
2004 dirty = 1;
2005 m->num = i;
2006 m->mx = m->wx = unique[i].x_org;
2007 m->my = m->wy = unique[i].y_org;
2008 m->mw = m->ww = unique[i].width;
2009 m->mh = m->wh = unique[i].height;
2010 updatebarpos(m);
2011 }
2012 /* removed monitors if n > nn */
2013 for (i = nn; i < n; i++) {
2014 for (m = mons; m && m->next; m = m->next);
2015 while ((c = m->clients)) {
2016 dirty = 1;
2017 m->clients = c->next;
2018 detachstack(c);
2019 c->mon = mons;
2020 attach(c);
2021 attachstack(c);
2022 }
2023 if (m == selmon)
2024 selmon = mons;
2025 cleanupmon(m);
2026 }
2027 free(unique);
2028 } else
2029#endif /* XINERAMA */
2030 { /* default monitor setup */
2031 if (!mons)
2032 mons = createmon();
2033 if (mons->mw != sw || mons->mh != sh) {
2034 dirty = 1;
2035 mons->mw = mons->ww = sw;
2036 mons->mh = mons->wh = sh;
2037 updatebarpos(mons);
2038 }
2039 }
2040 if (dirty) {
2041 selmon = mons;
2042 selmon = wintomon(root);
2043 }
2044 return dirty;
2045}
2046
2047void
2048updatenumlockmask(void)
2049{
2050 unsigned int i, j;
2051 XModifierKeymap *modmap;
2052
2053 numlockmask = 0;
2054 modmap = XGetModifierMapping(dpy);
2055 for (i = 0; i < 8; i++)
2056 for (j = 0; j < modmap->max_keypermod; j++)
2057 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2058 == XKeysymToKeycode(dpy, XK_Num_Lock))
2059 numlockmask = (1 << i);
2060 XFreeModifiermap(modmap);
2061}
2062
2063void
2064updatesizehints(Client *c)
2065{
2066 long msize;
2067 XSizeHints size;
2068
2069 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2070 /* size is uninitialized, ensure that size.flags aren't used */
2071 size.flags = PSize;
2072 if (size.flags & PBaseSize) {
2073 c->basew = size.base_width;
2074 c->baseh = size.base_height;
2075 } else if (size.flags & PMinSize) {
2076 c->basew = size.min_width;
2077 c->baseh = size.min_height;
2078 } else
2079 c->basew = c->baseh = 0;
2080 if (size.flags & PResizeInc) {
2081 c->incw = size.width_inc;
2082 c->inch = size.height_inc;
2083 } else
2084 c->incw = c->inch = 0;
2085 if (size.flags & PMaxSize) {
2086 c->maxw = size.max_width;
2087 c->maxh = size.max_height;
2088 } else
2089 c->maxw = c->maxh = 0;
2090 if (size.flags & PMinSize) {
2091 c->minw = size.min_width;
2092 c->minh = size.min_height;
2093 } else if (size.flags & PBaseSize) {
2094 c->minw = size.base_width;
2095 c->minh = size.base_height;
2096 } else
2097 c->minw = c->minh = 0;
2098 if (size.flags & PAspect) {
2099 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2100 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2101 } else
2102 c->maxa = c->mina = 0.0;
2103 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2104 c->hintsvalid = 1;
2105}
2106
2107void
2108updatestatus(void)
2109{
2110 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2111 strcpy(stext, "dwm-"VERSION);
2112 drawbar(selmon);
2113}
2114
2115void
2116updatetitle(Client *c)
2117{
2118 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2119 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2120 if (c->name[0] == '\0') /* hack to mark broken clients */
2121 strcpy(c->name, broken);
2122}
2123
2124void
2125updatewindowtype(Client *c)
2126{
2127 Atom state = getatomprop(c, netatom[NetWMState]);
2128 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2129
2130 if (state == netatom[NetWMFullscreen])
2131 setfullscreen(c, 1);
2132 if (wtype == netatom[NetWMWindowTypeDialog])
2133 c->isfloating = 1;
2134}
2135
2136void
2137updatewmhints(Client *c)
2138{
2139 XWMHints *wmh;
2140
2141 if ((wmh = XGetWMHints(dpy, c->win))) {
2142 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2143 wmh->flags &= ~XUrgencyHint;
2144 XSetWMHints(dpy, c->win, wmh);
2145 } else
2146 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2147 if (wmh->flags & InputHint)
2148 c->neverfocus = !wmh->input;
2149 else
2150 c->neverfocus = 0;
2151 XFree(wmh);
2152 }
2153}
2154
2155void
2156view(const Arg *arg)
2157{
ba835934 2158 if(arg->ui && (arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
5715edf5
AT
2159 return;
2160 selmon->seltags ^= 1; /* toggle sel tagset */
2161 if (arg->ui & TAGMASK)
2162 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2163 focus(NULL);
2164 arrange(selmon);
2165}
2166
2167Client *
2168wintoclient(Window w)
2169{
2170 Client *c;
2171 Monitor *m;
2172
2173 for (m = mons; m; m = m->next)
2174 for (c = m->clients; c; c = c->next)
2175 if (c->win == w)
2176 return c;
2177 return NULL;
2178}
2179
2180Monitor *
2181wintomon(Window w)
2182{
2183 int x, y;
2184 Client *c;
2185 Monitor *m;
2186
2187 if (w == root && getrootptr(&x, &y))
2188 return recttomon(x, y, 1, 1);
2189 for (m = mons; m; m = m->next)
2190 if (w == m->barwin)
2191 return m;
2192 if ((c = wintoclient(w)))
2193 return c->mon;
2194 return selmon;
2195}
2196
2197/* There's no way to check accesses to destroyed windows, thus those cases are
2198 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2199 * default error handler, which may call exit. */
2200int
2201xerror(Display *dpy, XErrorEvent *ee)
2202{
2203 if (ee->error_code == BadWindow
2204 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2205 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2206 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2207 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2208 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2209 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2210 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2211 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2212 return 0;
2213 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2214 ee->request_code, ee->error_code);
2215 return xerrorxlib(dpy, ee); /* may call exit */
2216}
2217
2218int
2219xerrordummy(Display *dpy, XErrorEvent *ee)
2220{
2221 return 0;
2222}
2223
2224/* Startup Error handler to check if another window manager
2225 * is already running. */
2226int
2227xerrorstart(Display *dpy, XErrorEvent *ee)
2228{
2229 die("dwm: another window manager is already running");
2230 return -1;
2231}
2232
2233void
2234zoom(const Arg *arg)
2235{
2236 Client *c = selmon->sel;
2237
2238 if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
2239 return;
2240 if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
2241 return;
2242 pop(c);
2243}
2244
2245int
2246main(int argc, char *argv[])
2247{
2248 if (argc == 2 && !strcmp("-v", argv[1]))
2249 die("dwm-"VERSION);
2250 else if (argc != 1)
2251 die("usage: dwm [-v]");
2252 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2253 fputs("warning: no locale support\n", stderr);
2254 if (!(dpy = XOpenDisplay(NULL)))
2255 die("dwm: cannot open display");
2256 checkotherwm();
2257 setup();
2258#ifdef __OpenBSD__
2259 if (pledge("stdio rpath proc exec", NULL) == -1)
2260 die("pledge");
2261#endif /* __OpenBSD__ */
2262 scan();
2263 run();
2264 cleanup();
2265 XCloseDisplay(dpy);
1da78b82
AT
2266 if (restart == 1) {
2267 execlp("dwm", "dwm", NULL);
2268 }
5715edf5
AT
2269 return EXIT_SUCCESS;
2270}