Increased font size for status bar and dmenu.
[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
a5832938
AT
1280 // TODO: Create a central location for the dmenu font specification.
1281 // It should not be hardcoded in multiple locations.
1282 if(!(f = popen("dmenu -fn monospace:size=14 < /dev/null", "r"))) {
c56e7560
AT
1283 fprintf(stderr, "dwm: popen 'dmenu < /dev/null' failed%s%s\n", errno ? ": " : "", errno ? strerror(errno) : "");
1284 return;
1285 }
1286 if (!(p = fgets(name, MAX_TAGLEN, f)) && (i = errno) && ferror(f))
1287 fprintf(stderr, "dwm: fgets failed: %s\n", strerror(i));
1288 if (pclose(f) < 0)
1289 fprintf(stderr, "dwm: pclose failed: %s\n", strerror(errno));
1290 if(!p)
1291 return;
1292 if((p = strchr(name, '\n')))
1293 *p = '\0';
1294
1295 for(i = 0; i < LENGTH(tags); i++)
1296 if(selmon->tagset[selmon->seltags] & (1 << i))
1297 strcpy(tags[i], name);
1298 drawbars();
1299}
1300
5715edf5
AT
1301Client *
1302nexttiled(Client *c)
1303{
1304 for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1305 return c;
1306}
1307
1308void
1309pop(Client *c)
1310{
1311 detach(c);
1312 attach(c);
1313 focus(c);
1314 arrange(c->mon);
1315}
1316
1317void
1318propertynotify(XEvent *e)
1319{
1320 Client *c;
1321 Window trans;
1322 XPropertyEvent *ev = &e->xproperty;
1323
1324 if ((ev->window == root) && (ev->atom == XA_WM_NAME))
1325 updatestatus();
1326 else if (ev->state == PropertyDelete)
1327 return; /* ignore */
1328 else if ((c = wintoclient(ev->window))) {
1329 switch(ev->atom) {
1330 default: break;
1331 case XA_WM_TRANSIENT_FOR:
1332 if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1333 (c->isfloating = (wintoclient(trans)) != NULL))
1334 arrange(c->mon);
1335 break;
1336 case XA_WM_NORMAL_HINTS:
1337 c->hintsvalid = 0;
1338 break;
1339 case XA_WM_HINTS:
1340 updatewmhints(c);
1341 drawbars();
1342 break;
1343 }
1344 if (ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1345 updatetitle(c);
1346 if (c == c->mon->sel)
1347 drawbar(c->mon);
1348 }
1349 if (ev->atom == netatom[NetWMWindowType])
1350 updatewindowtype(c);
1351 }
1352}
1353
1354void
1355quit(const Arg *arg)
1356{
1357 running = 0;
1358}
1359
1da78b82
AT
1360void
1361quitprompt(const Arg *arg)
1362{
a5832938
AT
1363 // TODO: Create a central location for the dmenu font specification.
1364 // It should not be hardcoded in multiple locations.
1365 FILE *pp = popen("echo -e \"no\nrestart\nyes\" | dmenu -fn monospace:size=14 -i -sb red -p \"Quit DWM?\"", "r");
1da78b82
AT
1366 if(pp != NULL) {
1367 char buf[1024];
1368 if (fgets(buf, sizeof(buf), pp) == NULL) {
1369 fprintf(stderr, "Quitprompt: Error reading pipe!\n");
1370 return;
1371 }
1372 if (strcmp(buf, "yes\n") == 0) {
1373 pclose(pp);
1374 restart = 0;
1375 quit(NULL);
1376 } else if (strcmp(buf, "restart\n") == 0) {
1377 pclose(pp);
1378 restart = 1;
1379 quit(NULL);
1380 } else if (strcmp(buf, "no\n") == 0) {
1381 pclose(pp);
1382 return;
1383 }
1384 }
1385}
1386
5715edf5
AT
1387Monitor *
1388recttomon(int x, int y, int w, int h)
1389{
1390 Monitor *m, *r = selmon;
1391 int a, area = 0;
1392
1393 for (m = mons; m; m = m->next)
1394 if ((a = INTERSECT(x, y, w, h, m)) > area) {
1395 area = a;
1396 r = m;
1397 }
1398 return r;
1399}
1400
1401void
1402resize(Client *c, int x, int y, int w, int h, int interact)
1403{
1404 if (applysizehints(c, &x, &y, &w, &h, interact))
1405 resizeclient(c, x, y, w, h);
1406}
1407
1408void
1409resizeclient(Client *c, int x, int y, int w, int h)
1410{
1411 XWindowChanges wc;
1412
1413 c->oldx = c->x; c->x = wc.x = x;
1414 c->oldy = c->y; c->y = wc.y = y;
1415 c->oldw = c->w; c->w = wc.width = w;
1416 c->oldh = c->h; c->h = wc.height = h;
1417 wc.border_width = c->bw;
1418 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
1419 configure(c);
1420 XSync(dpy, False);
1421}
1422
1423void
1424resizemouse(const Arg *arg)
1425{
1426 int ocx, ocy, nw, nh;
1427 Client *c;
1428 Monitor *m;
1429 XEvent ev;
1430 Time lasttime = 0;
1431
1432 if (!(c = selmon->sel))
1433 return;
1434 if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1435 return;
1436 restack(selmon);
1437 ocx = c->x;
1438 ocy = c->y;
1439 if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1440 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1441 return;
1442 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1443 do {
1444 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1445 switch(ev.type) {
1446 case ConfigureRequest:
1447 case Expose:
1448 case MapRequest:
1449 handler[ev.type](&ev);
1450 break;
1451 case MotionNotify:
1452 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1453 continue;
1454 lasttime = ev.xmotion.time;
1455
1456 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1457 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1458 if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1459 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1460 {
1461 if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
1462 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1463 togglefloating(NULL);
1464 }
1465 if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1466 resize(c, c->x, c->y, nw, nh, 1);
1467 break;
1468 }
1469 } while (ev.type != ButtonRelease);
1470 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1471 XUngrabPointer(dpy, CurrentTime);
1472 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1473 if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1474 sendmon(c, m);
1475 selmon = m;
1476 focus(NULL);
1477 }
1478}
1479
1480void
1481restack(Monitor *m)
1482{
1483 Client *c;
1484 XEvent ev;
1485 XWindowChanges wc;
1486
1487 drawbar(m);
1488 if (!m->sel)
1489 return;
1490 if (m->sel->isfloating || !m->lt[m->sellt]->arrange)
1491 XRaiseWindow(dpy, m->sel->win);
1492 if (m->lt[m->sellt]->arrange) {
1493 wc.stack_mode = Below;
1494 wc.sibling = m->barwin;
1495 for (c = m->stack; c; c = c->snext)
1496 if (!c->isfloating && ISVISIBLE(c)) {
1497 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1498 wc.sibling = c->win;
1499 }
1500 }
1501 XSync(dpy, False);
1502 while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1503}
1504
1505void
1506run(void)
1507{
1508 XEvent ev;
1509 /* main event loop */
1510 XSync(dpy, False);
1511 while (running && !XNextEvent(dpy, &ev))
1512 if (handler[ev.type])
1513 handler[ev.type](&ev); /* call handler */
1514}
1515
1516void
1517scan(void)
1518{
1519 unsigned int i, num;
1520 Window d1, d2, *wins = NULL;
1521 XWindowAttributes wa;
1522
1523 if (XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1524 for (i = 0; i < num; i++) {
1525 if (!XGetWindowAttributes(dpy, wins[i], &wa)
1526 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1527 continue;
1528 if (wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1529 manage(wins[i], &wa);
1530 }
1531 for (i = 0; i < num; i++) { /* now the transients */
1532 if (!XGetWindowAttributes(dpy, wins[i], &wa))
1533 continue;
1534 if (XGetTransientForHint(dpy, wins[i], &d1)
1535 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1536 manage(wins[i], &wa);
1537 }
1538 if (wins)
1539 XFree(wins);
1540 }
1541}
1542
1543void
1544sendmon(Client *c, Monitor *m)
1545{
1546 if (c->mon == m)
1547 return;
1548 unfocus(c, 1);
1549 detach(c);
1550 detachstack(c);
1551 c->mon = m;
ba835934 1552 c->tags = (m->tagset[m->seltags] ? m->tagset[m->seltags] : 1);
5715edf5
AT
1553 attach(c);
1554 attachstack(c);
1555 focus(NULL);
1556 arrange(NULL);
1557}
1558
1559void
1560setclientstate(Client *c, long state)
1561{
1562 long data[] = { state, None };
1563
1564 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1565 PropModeReplace, (unsigned char *)data, 2);
1566}
1567
1568int
1569sendevent(Client *c, Atom proto)
1570{
1571 int n;
1572 Atom *protocols;
1573 int exists = 0;
1574 XEvent ev;
1575
1576 if (XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1577 while (!exists && n--)
1578 exists = protocols[n] == proto;
1579 XFree(protocols);
1580 }
1581 if (exists) {
1582 ev.type = ClientMessage;
1583 ev.xclient.window = c->win;
1584 ev.xclient.message_type = wmatom[WMProtocols];
1585 ev.xclient.format = 32;
1586 ev.xclient.data.l[0] = proto;
1587 ev.xclient.data.l[1] = CurrentTime;
1588 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1589 }
1590 return exists;
1591}
1592
1593void
1594setfocus(Client *c)
1595{
1596 if (!c->neverfocus) {
1597 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1598 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1599 XA_WINDOW, 32, PropModeReplace,
1600 (unsigned char *) &(c->win), 1);
1601 }
1602 sendevent(c, wmatom[WMTakeFocus]);
1603}
1604
1605void
1606setfullscreen(Client *c, int fullscreen)
1607{
1608 if (fullscreen && !c->isfullscreen) {
1609 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1610 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1611 c->isfullscreen = 1;
1612 c->oldstate = c->isfloating;
1613 c->oldbw = c->bw;
1614 c->bw = 0;
1615 c->isfloating = 1;
1616 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
1617 XRaiseWindow(dpy, c->win);
1618 } else if (!fullscreen && c->isfullscreen){
1619 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1620 PropModeReplace, (unsigned char*)0, 0);
1621 c->isfullscreen = 0;
1622 c->isfloating = c->oldstate;
1623 c->bw = c->oldbw;
1624 c->x = c->oldx;
1625 c->y = c->oldy;
1626 c->w = c->oldw;
1627 c->h = c->oldh;
1628 resizeclient(c, c->x, c->y, c->w, c->h);
1629 arrange(c->mon);
1630 }
1631}
1632
1633void
1634setlayout(const Arg *arg)
1635{
1636 if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1637 selmon->sellt ^= 1;
1638 if (arg && arg->v)
1639 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1640 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1641 if (selmon->sel)
1642 arrange(selmon);
1643 else
1644 drawbar(selmon);
1645}
1646
1647/* arg > 1.0 will set mfact absolutely */
1648void
1649setmfact(const Arg *arg)
1650{
1651 float f;
1652
1653 if (!arg || !selmon->lt[selmon->sellt]->arrange)
1654 return;
1655 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1656 if (f < 0.05 || f > 0.95)
1657 return;
1658 selmon->mfact = f;
1659 arrange(selmon);
1660}
1661
1662void
1663setup(void)
1664{
1665 int i;
1666 XSetWindowAttributes wa;
1667 Atom utf8string;
1668
1669 /* clean up any zombies immediately */
1670 sigchld(0);
1671
1672 /* init screen */
1673 screen = DefaultScreen(dpy);
1674 sw = DisplayWidth(dpy, screen);
1675 sh = DisplayHeight(dpy, screen);
1676 root = RootWindow(dpy, screen);
1677 drw = drw_create(dpy, screen, root, sw, sh);
1678 if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
1679 die("no fonts could be loaded.");
1680 lrpad = drw->fonts->h;
1681 bh = drw->fonts->h + 2;
1682 updategeom();
1683 /* init atoms */
1684 utf8string = XInternAtom(dpy, "UTF8_STRING", False);
1685 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1686 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1687 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1688 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1689 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1690 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1691 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1692 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1693 netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1694 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1695 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1696 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1697 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1698 /* init cursors */
1699 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1700 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1701 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1702 /* init appearance */
1703 scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
1704 for (i = 0; i < LENGTH(colors); i++)
1705 scheme[i] = drw_scm_create(drw, colors[i], 3);
1706 /* init bars */
1707 updatebars();
1708 updatestatus();
1709 /* supporting window for NetWMCheck */
1710 wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
1711 XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
1712 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1713 XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
1714 PropModeReplace, (unsigned char *) "dwm", 3);
1715 XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
1716 PropModeReplace, (unsigned char *) &wmcheckwin, 1);
1717 /* EWMH support per view */
1718 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1719 PropModeReplace, (unsigned char *) netatom, NetLast);
1720 XDeleteProperty(dpy, root, netatom[NetClientList]);
1721 /* select events */
1722 wa.cursor = cursor[CurNormal]->cursor;
1723 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
1724 |ButtonPressMask|PointerMotionMask|EnterWindowMask
1725 |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1726 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1727 XSelectInput(dpy, root, wa.event_mask);
1728 grabkeys();
1729 focus(NULL);
1730}
1731
1732void
1733seturgent(Client *c, int urg)
1734{
1735 XWMHints *wmh;
1736
1737 c->isurgent = urg;
1738 if (!(wmh = XGetWMHints(dpy, c->win)))
1739 return;
1740 wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
1741 XSetWMHints(dpy, c->win, wmh);
1742 XFree(wmh);
1743}
1744
1745void
1746showhide(Client *c)
1747{
1748 if (!c)
1749 return;
1750 if (ISVISIBLE(c)) {
1751 /* show clients top down */
1752 XMoveWindow(dpy, c->win, c->x, c->y);
1753 if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1754 resize(c, c->x, c->y, c->w, c->h, 0);
1755 showhide(c->snext);
1756 } else {
1757 /* hide clients bottom up */
1758 showhide(c->snext);
1759 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1760 }
1761}
1762
1763void
1764sigchld(int unused)
1765{
1766 if (signal(SIGCHLD, sigchld) == SIG_ERR)
1767 die("can't install SIGCHLD handler:");
1768 while (0 < waitpid(-1, NULL, WNOHANG));
1769}
1770
1771void
1772spawn(const Arg *arg)
1773{
1774 if (fork() == 0) {
1775 if (dpy)
1776 close(ConnectionNumber(dpy));
1777 setsid();
1778 execvp(((char **)arg->v)[0], (char **)arg->v);
1779 die("dwm: execvp '%s' failed:", ((char **)arg->v)[0]);
1780 }
1781}
1782
1783void
1784tag(const Arg *arg)
1785{
1786 if (selmon->sel && arg->ui & TAGMASK) {
1787 selmon->sel->tags = arg->ui & TAGMASK;
1788 focus(NULL);
1789 arrange(selmon);
1790 }
1791}
1792
1793void
1794tagmon(const Arg *arg)
1795{
1796 if (!selmon->sel || !mons->next)
1797 return;
1798 sendmon(selmon->sel, dirtomon(arg->i));
1799}
1800
1801void
1802tile(Monitor *m)
1803{
1804 unsigned int i, n, h, mw, my, ty;
1805 Client *c;
1806
1807 for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1808 if (n == 0)
1809 return;
1810
1811 if (n > m->nmaster)
1812 mw = m->nmaster ? m->ww * m->mfact : 0;
1813 else
1814 mw = m->ww;
1815 for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1816 if (i < m->nmaster) {
1817 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1818 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
1819 if (my + HEIGHT(c) < m->wh)
1820 my += HEIGHT(c);
1821 } else {
1822 h = (m->wh - ty) / (n - i);
1823 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
1824 if (ty + HEIGHT(c) < m->wh)
1825 ty += HEIGHT(c);
1826 }
1827}
1828
1829void
1830togglebar(const Arg *arg)
1831{
1832 selmon->showbar = !selmon->showbar;
1833 updatebarpos(selmon);
1834 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1835 arrange(selmon);
1836}
1837
1838void
1839togglefloating(const Arg *arg)
1840{
1841 if (!selmon->sel)
1842 return;
1843 if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
1844 return;
1845 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1846 if (selmon->sel->isfloating)
1847 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1848 selmon->sel->w, selmon->sel->h, 0);
1849 arrange(selmon);
1850}
1851
1852void
1853toggletag(const Arg *arg)
1854{
1855 unsigned int newtags;
1856
1857 if (!selmon->sel)
1858 return;
1859 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1860 if (newtags) {
1861 selmon->sel->tags = newtags;
1862 focus(NULL);
1863 arrange(selmon);
1864 }
1865}
1866
1867void
1868toggleview(const Arg *arg)
1869{
1870 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1871
ba835934
AT
1872 selmon->tagset[selmon->seltags] = newtagset;
1873 focus(NULL);
1874 arrange(selmon);
5715edf5
AT
1875}
1876
1877void
1878unfocus(Client *c, int setfocus)
1879{
1880 if (!c)
1881 return;
1882 grabbuttons(c, 0);
1883 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel);
1884 if (setfocus) {
1885 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1886 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1887 }
1888}
1889
1890void
1891unmanage(Client *c, int destroyed)
1892{
1893 Monitor *m = c->mon;
1894 XWindowChanges wc;
1895
1896 detach(c);
1897 detachstack(c);
1898 if (!destroyed) {
1899 wc.border_width = c->oldbw;
1900 XGrabServer(dpy); /* avoid race conditions */
1901 XSetErrorHandler(xerrordummy);
1902 XSelectInput(dpy, c->win, NoEventMask);
1903 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1904 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1905 setclientstate(c, WithdrawnState);
1906 XSync(dpy, False);
1907 XSetErrorHandler(xerror);
1908 XUngrabServer(dpy);
1909 }
1910 free(c);
1911 focus(NULL);
1912 updateclientlist();
1913 arrange(m);
1914}
1915
1916void
1917unmapnotify(XEvent *e)
1918{
1919 Client *c;
1920 XUnmapEvent *ev = &e->xunmap;
1921
1922 if ((c = wintoclient(ev->window))) {
1923 if (ev->send_event)
1924 setclientstate(c, WithdrawnState);
1925 else
1926 unmanage(c, 0);
1927 }
1928}
1929
1930void
1931updatebars(void)
1932{
1933 Monitor *m;
1934 XSetWindowAttributes wa = {
1935 .override_redirect = True,
1936 .background_pixmap = ParentRelative,
1937 .event_mask = ButtonPressMask|ExposureMask
1938 };
1939 XClassHint ch = {"dwm", "dwm"};
1940 for (m = mons; m; m = m->next) {
1941 if (m->barwin)
1942 continue;
1943 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1944 CopyFromParent, DefaultVisual(dpy, screen),
1945 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1946 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1947 XMapRaised(dpy, m->barwin);
1948 XSetClassHint(dpy, m->barwin, &ch);
1949 }
1950}
1951
1952void
1953updatebarpos(Monitor *m)
1954{
1955 m->wy = m->my;
1956 m->wh = m->mh;
1957 if (m->showbar) {
1958 m->wh -= bh;
1959 m->by = m->topbar ? m->wy : m->wy + m->wh;
1960 m->wy = m->topbar ? m->wy + bh : m->wy;
1961 } else
1962 m->by = -bh;
1963}
1964
1965void
1966updateclientlist()
1967{
1968 Client *c;
1969 Monitor *m;
1970
1971 XDeleteProperty(dpy, root, netatom[NetClientList]);
1972 for (m = mons; m; m = m->next)
1973 for (c = m->clients; c; c = c->next)
1974 XChangeProperty(dpy, root, netatom[NetClientList],
1975 XA_WINDOW, 32, PropModeAppend,
1976 (unsigned char *) &(c->win), 1);
1977}
1978
1979int
1980updategeom(void)
1981{
1982 int dirty = 0;
1983
1984#ifdef XINERAMA
1985 if (XineramaIsActive(dpy)) {
1986 int i, j, n, nn;
1987 Client *c;
1988 Monitor *m;
1989 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1990 XineramaScreenInfo *unique = NULL;
1991
1992 for (n = 0, m = mons; m; m = m->next, n++);
1993 /* only consider unique geometries as separate screens */
1994 unique = ecalloc(nn, sizeof(XineramaScreenInfo));
1995 for (i = 0, j = 0; i < nn; i++)
1996 if (isuniquegeom(unique, j, &info[i]))
1997 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1998 XFree(info);
1999 nn = j;
2000
2001 /* new monitors if nn > n */
2002 for (i = n; i < nn; i++) {
2003 for (m = mons; m && m->next; m = m->next);
2004 if (m)
2005 m->next = createmon();
2006 else
2007 mons = createmon();
2008 }
2009 for (i = 0, m = mons; i < nn && m; m = m->next, i++)
2010 if (i >= n
2011 || unique[i].x_org != m->mx || unique[i].y_org != m->my
2012 || unique[i].width != m->mw || unique[i].height != m->mh)
2013 {
2014 dirty = 1;
2015 m->num = i;
2016 m->mx = m->wx = unique[i].x_org;
2017 m->my = m->wy = unique[i].y_org;
2018 m->mw = m->ww = unique[i].width;
2019 m->mh = m->wh = unique[i].height;
2020 updatebarpos(m);
2021 }
2022 /* removed monitors if n > nn */
2023 for (i = nn; i < n; i++) {
2024 for (m = mons; m && m->next; m = m->next);
2025 while ((c = m->clients)) {
2026 dirty = 1;
2027 m->clients = c->next;
2028 detachstack(c);
2029 c->mon = mons;
2030 attach(c);
2031 attachstack(c);
2032 }
2033 if (m == selmon)
2034 selmon = mons;
2035 cleanupmon(m);
2036 }
2037 free(unique);
2038 } else
2039#endif /* XINERAMA */
2040 { /* default monitor setup */
2041 if (!mons)
2042 mons = createmon();
2043 if (mons->mw != sw || mons->mh != sh) {
2044 dirty = 1;
2045 mons->mw = mons->ww = sw;
2046 mons->mh = mons->wh = sh;
2047 updatebarpos(mons);
2048 }
2049 }
2050 if (dirty) {
2051 selmon = mons;
2052 selmon = wintomon(root);
2053 }
2054 return dirty;
2055}
2056
2057void
2058updatenumlockmask(void)
2059{
2060 unsigned int i, j;
2061 XModifierKeymap *modmap;
2062
2063 numlockmask = 0;
2064 modmap = XGetModifierMapping(dpy);
2065 for (i = 0; i < 8; i++)
2066 for (j = 0; j < modmap->max_keypermod; j++)
2067 if (modmap->modifiermap[i * modmap->max_keypermod + j]
2068 == XKeysymToKeycode(dpy, XK_Num_Lock))
2069 numlockmask = (1 << i);
2070 XFreeModifiermap(modmap);
2071}
2072
2073void
2074updatesizehints(Client *c)
2075{
2076 long msize;
2077 XSizeHints size;
2078
2079 if (!XGetWMNormalHints(dpy, c->win, &size, &msize))
2080 /* size is uninitialized, ensure that size.flags aren't used */
2081 size.flags = PSize;
2082 if (size.flags & PBaseSize) {
2083 c->basew = size.base_width;
2084 c->baseh = size.base_height;
2085 } else if (size.flags & PMinSize) {
2086 c->basew = size.min_width;
2087 c->baseh = size.min_height;
2088 } else
2089 c->basew = c->baseh = 0;
2090 if (size.flags & PResizeInc) {
2091 c->incw = size.width_inc;
2092 c->inch = size.height_inc;
2093 } else
2094 c->incw = c->inch = 0;
2095 if (size.flags & PMaxSize) {
2096 c->maxw = size.max_width;
2097 c->maxh = size.max_height;
2098 } else
2099 c->maxw = c->maxh = 0;
2100 if (size.flags & PMinSize) {
2101 c->minw = size.min_width;
2102 c->minh = size.min_height;
2103 } else if (size.flags & PBaseSize) {
2104 c->minw = size.base_width;
2105 c->minh = size.base_height;
2106 } else
2107 c->minw = c->minh = 0;
2108 if (size.flags & PAspect) {
2109 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2110 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2111 } else
2112 c->maxa = c->mina = 0.0;
2113 c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
2114 c->hintsvalid = 1;
2115}
2116
2117void
2118updatestatus(void)
2119{
2120 if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2121 strcpy(stext, "dwm-"VERSION);
2122 drawbar(selmon);
2123}
2124
2125void
2126updatetitle(Client *c)
2127{
2128 if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2129 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2130 if (c->name[0] == '\0') /* hack to mark broken clients */
2131 strcpy(c->name, broken);
2132}
2133
2134void
2135updatewindowtype(Client *c)
2136{
2137 Atom state = getatomprop(c, netatom[NetWMState]);
2138 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2139
2140 if (state == netatom[NetWMFullscreen])
2141 setfullscreen(c, 1);
2142 if (wtype == netatom[NetWMWindowTypeDialog])
2143 c->isfloating = 1;
2144}
2145
2146void
2147updatewmhints(Client *c)
2148{
2149 XWMHints *wmh;
2150
2151 if ((wmh = XGetWMHints(dpy, c->win))) {
2152 if (c == selmon->sel && wmh->flags & XUrgencyHint) {
2153 wmh->flags &= ~XUrgencyHint;
2154 XSetWMHints(dpy, c->win, wmh);
2155 } else
2156 c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0;
2157 if (wmh->flags & InputHint)
2158 c->neverfocus = !wmh->input;
2159 else
2160 c->neverfocus = 0;
2161 XFree(wmh);
2162 }
2163}
2164
2165void
2166view(const Arg *arg)
2167{
ba835934 2168 if(arg->ui && (arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
5715edf5
AT
2169 return;
2170 selmon->seltags ^= 1; /* toggle sel tagset */
2171 if (arg->ui & TAGMASK)
2172 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2173 focus(NULL);
2174 arrange(selmon);
2175}
2176
2177Client *
2178wintoclient(Window w)
2179{
2180 Client *c;
2181 Monitor *m;
2182
2183 for (m = mons; m; m = m->next)
2184 for (c = m->clients; c; c = c->next)
2185 if (c->win == w)
2186 return c;
2187 return NULL;
2188}
2189
2190Monitor *
2191wintomon(Window w)
2192{
2193 int x, y;
2194 Client *c;
2195 Monitor *m;
2196
2197 if (w == root && getrootptr(&x, &y))
2198 return recttomon(x, y, 1, 1);
2199 for (m = mons; m; m = m->next)
2200 if (w == m->barwin)
2201 return m;
2202 if ((c = wintoclient(w)))
2203 return c->mon;
2204 return selmon;
2205}
2206
2207/* There's no way to check accesses to destroyed windows, thus those cases are
2208 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2209 * default error handler, which may call exit. */
2210int
2211xerror(Display *dpy, XErrorEvent *ee)
2212{
2213 if (ee->error_code == BadWindow
2214 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2215 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2216 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2217 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2218 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2219 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2220 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2221 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2222 return 0;
2223 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2224 ee->request_code, ee->error_code);
2225 return xerrorxlib(dpy, ee); /* may call exit */
2226}
2227
2228int
2229xerrordummy(Display *dpy, XErrorEvent *ee)
2230{
2231 return 0;
2232}
2233
2234/* Startup Error handler to check if another window manager
2235 * is already running. */
2236int
2237xerrorstart(Display *dpy, XErrorEvent *ee)
2238{
2239 die("dwm: another window manager is already running");
2240 return -1;
2241}
2242
2243void
2244zoom(const Arg *arg)
2245{
2246 Client *c = selmon->sel;
2247
2248 if (!selmon->lt[selmon->sellt]->arrange || !c || c->isfloating)
2249 return;
2250 if (c == nexttiled(selmon->clients) && !(c = nexttiled(c->next)))
2251 return;
2252 pop(c);
2253}
2254
2255int
2256main(int argc, char *argv[])
2257{
2258 if (argc == 2 && !strcmp("-v", argv[1]))
2259 die("dwm-"VERSION);
2260 else if (argc != 1)
2261 die("usage: dwm [-v]");
2262 if (!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2263 fputs("warning: no locale support\n", stderr);
2264 if (!(dpy = XOpenDisplay(NULL)))
2265 die("dwm: cannot open display");
2266 checkotherwm();
2267 setup();
2268#ifdef __OpenBSD__
2269 if (pledge("stdio rpath proc exec", NULL) == -1)
2270 die("pledge");
2271#endif /* __OpenBSD__ */
2272 scan();
2273 run();
2274 cleanup();
2275 XCloseDisplay(dpy);
1da78b82 2276 if (restart == 1) {
2ec77049 2277 execlp("dwm-sgk", "dwm-sgk", NULL);
1da78b82 2278 }
5715edf5
AT
2279 return EXIT_SUCCESS;
2280}