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