1 /* See LICENSE file for copyright and license details.
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.
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
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
19 * Keys and tagging rules are organized as arrays and defined in config.h.
21 * To understand everything else, start reading main().
31 #include <sys/types.h>
33 #include <X11/cursorfont.h>
34 #include <X11/keysym.h>
35 #include <X11/Xatom.h>
37 #include <X11/Xproto.h>
38 #include <X11/Xutil.h>
40 #include <X11/extensions/Xinerama.h>
47 #define BUTTONMASK (ButtonPressMask|ButtonReleaseMask)
48 #define CLEANMASK(mask) (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
49 #define INTERSECT(x,y,w,h,m) (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
50 * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
51 #define ISVISIBLE(C) ((C->tags & C->mon->tagset[C->mon->seltags]))
52 #define LENGTH(X) (sizeof X / sizeof X[0])
53 #define MOUSEMASK (BUTTONMASK|PointerMotionMask)
54 #define WIDTH(X) ((X)->w + 2 * (X)->bw)
55 #define HEIGHT(X) ((X)->h + 2 * (X)->bw)
56 #define TAGMASK ((1 << LENGTH(tags)) - 1)
57 #define TEXTW(X) (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
60 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
61 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
62 enum { NetSupported, NetWMName, NetWMState,
63 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
64 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
65 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
66 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
67 ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
80 void (*func)(const Arg *arg);
84 typedef struct Monitor Monitor;
85 typedef struct Client Client;
90 int oldx, oldy, oldw, oldh;
91 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
95 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
105 void (*func)(const Arg *);
111 void (*arrange)(Monitor *);
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;
124 unsigned int tagset[2];
137 const char *instance;
145 /* function declarations */
146 static void applyrules(Client *c);
147 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
148 static void arrange(Monitor *m);
149 static void arrangemon(Monitor *m);
150 static void attach_as_master(Client *c);
151 static void attach(Client *c);
152 static void attachstack(Client *c);
153 static void buttonpress(XEvent *e);
154 static void checkotherwm(void);
155 static void cleanup(void);
156 static void cleanupmon(Monitor *mon);
157 static void clearurgent(Client *c);
158 static void clientmessage(XEvent *e);
159 static void configure(Client *c);
160 static void configurenotify(XEvent *e);
161 static void configurerequest(XEvent *e);
162 static Monitor *createmon(void);
163 static void destroynotify(XEvent *e);
164 static void detach(Client *c);
165 static void detachstack(Client *c);
166 static Monitor *dirtomon(int dir);
167 static void drawbar(Monitor *m);
168 static void drawbars(void);
169 static void enternotify(XEvent *e);
170 static void expose(XEvent *e);
171 static void focus(Client *c);
172 static void focusin(XEvent *e);
173 static void focusmon(const Arg *arg);
174 static void focusstack(const Arg *arg);
175 static Bool getrootptr(int *x, int *y);
176 static long getstate(Window w);
177 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
178 static void grabbuttons(Client *c, Bool focused);
179 static void grabkeys(void);
180 static void incnmaster(const Arg *arg);
181 static void keypress(XEvent *e);
182 static void killclient(const Arg *arg);
183 static void manage(Window w, XWindowAttributes *wa);
184 static void mappingnotify(XEvent *e);
185 static void maprequest(XEvent *e);
186 static void monocle(Monitor *m);
187 static void motionnotify(XEvent *e);
188 static void movemouse(const Arg *arg);
189 static Client *nexttiled(Client *c);
190 static Client *nextvisible(Client *c);
191 static void pop(Client *);
192 static void propertynotify(XEvent *e);
193 static void quit(const Arg *arg);
194 static Monitor *recttomon(int x, int y, int w, int h);
195 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
196 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
197 static void resizemouse(const Arg *arg);
198 static void restack(Monitor *m);
199 static void run(void);
200 static void scan(void);
201 static Bool sendevent(Client *c, Atom proto);
202 static void sendmon(Client *c, Monitor *m);
203 static void setclientstate(Client *c, long state);
204 static void setfocus(Client *c);
205 static void setfullscreen(Client *c, Bool fullscreen);
206 static void setlayout(const Arg *arg);
207 static void setmfact(const Arg *arg);
208 static void setup(void);
209 static void showhide(Client *c);
210 static void sigchld(int unused);
211 static void spawn(const Arg *arg);
212 static void tag(const Arg *arg);
213 static void tagmon(const Arg *arg);
214 static void jason_layout(Monitor *);
215 static void tile(Monitor *);
216 static void togglebar(const Arg *arg);
217 static void togglefloating(const Arg *arg);
218 static void toggletag(const Arg *arg);
219 static void toggleview(const Arg *arg);
220 static void unfocus(Client *c, Bool setfocus);
221 static void unmanage(Client *c, Bool destroyed);
222 static void unmapnotify(XEvent *e);
223 static Bool updategeom(void);
224 static void updatebarpos(Monitor *m);
225 static void updatebars(void);
226 static void updateclientlist(void);
227 static void updatenumlockmask(void);
228 static void updatesizehints(Client *c);
229 static void updatestatus(void);
230 static void updatewindowtype(Client *c);
231 static void updatetitle(Client *c);
232 static void updatewmhints(Client *c);
233 static void view(const Arg *arg);
234 static Client *wintoclient(Window w);
235 static Monitor *wintomon(Window w);
236 static int xerror(Display *dpy, XErrorEvent *ee);
237 static int xerrordummy(Display *dpy, XErrorEvent *ee);
238 static int xerrorstart(Display *dpy, XErrorEvent *ee);
239 static void zoom(const Arg *arg);
242 static const char broken[] = "broken";
243 static char stext[256];
245 static int sw, sh; /* X display screen geometry width, height */
246 static int bh, blw = 0; /* bar geometry */
247 static int (*xerrorxlib)(Display *, XErrorEvent *);
248 static unsigned int numlockmask = 0;
249 static void (*handler[LASTEvent]) (XEvent *) = {
250 [ButtonPress] = buttonpress,
251 [ClientMessage] = clientmessage,
252 [ConfigureRequest] = configurerequest,
253 [ConfigureNotify] = configurenotify,
254 [DestroyNotify] = destroynotify,
255 [EnterNotify] = enternotify,
258 [KeyPress] = keypress,
259 [MappingNotify] = mappingnotify,
260 [MapRequest] = maprequest,
261 [MotionNotify] = motionnotify,
262 [PropertyNotify] = propertynotify,
263 [UnmapNotify] = unmapnotify
265 static Atom wmatom[WMLast], netatom[NetLast];
266 static Bool running = True;
267 static Cur *cursor[CurLast];
268 static ClrScheme scheme[SchemeLast];
272 static Monitor *mons, *selmon;
275 // unfocused windows get transparent (feature)
276 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
277 static void window_set_opaque(Client *c);
278 static void window_set_translucent(Client *c);
279 static void window_set_invisible(Client *c);
280 static void window_set_opacity(Client *c, int opacity_index);
281 static void update_window_opacities(Monitor *m);
283 window_set_opacity(Client *c, int opacity_index) {
284 if (c->opacity == opacity_index) {
287 c->opacity = opacity_index;
288 if (opacity_index == 0) {
289 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
291 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
295 window_set_opaque(Client *c) {
296 window_set_opacity(c, 0);
299 window_set_translucent(Client *c) {
300 window_set_opacity(c, 1);
303 window_set_invisible(Client *c) {
304 window_set_opacity(c, 2);
307 update_window_opacities(Monitor *m) {
308 Client *master, *slave, *c;
309 Bool selection_floating = False;
310 slave = master = nexttiled(m->clients);
311 if (master) slave = nexttiled(master->next);
312 if (m->sel && m->sel != master) {
313 if (nexttiled(m->sel) == m->sel) // if selection is tiled
316 selection_floating = True;
318 for (c = m->clients; c; c = c->next) {
320 if (c->isfloating || c == m->sel || (c == master && selection_floating)) {
321 window_set_opaque(c);
322 } else if (c == master || c == slave) {
323 window_set_translucent(c);
325 window_set_opaque(c);
332 /* configuration, allows nested code to access above variables */
335 /* compile-time check if all tags fit into an unsigned int bit array. */
336 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
338 /* function implementations */
340 applyrules(Client *c) {
341 const char *class, *instance;
345 XClassHint ch = { NULL, NULL };
348 c->isfloating = c->tags = c->screen_hog = 0;
349 XGetClassHint(dpy, c->win, &ch);
350 class = ch.res_class ? ch.res_class : broken;
351 instance = ch.res_name ? ch.res_name : broken;
353 for(i = 0; i < LENGTH(rules); i++) {
355 if((!r->title || c->name == strstr(c->name, r->title))
356 && (!r->class || strstr(class, r->class))
357 && (!r->instance || strstr(instance, r->instance)))
359 c->isfloating = r->isfloating;
361 c->screen_hog = r->screen_hog;
362 for(m = mons; m && m->num != r->monitor; m = m->next);
371 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
375 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
379 /* set minimum possible */
387 if(*x + *w + 2 * c->bw < 0)
389 if(*y + *h + 2 * c->bw < 0)
393 if(*x >= m->wx + m->ww)
394 *x = m->wx + m->ww - WIDTH(c);
395 if(*y >= m->wy + m->wh)
396 *y = m->wy + m->wh - HEIGHT(c);
397 if(*x + *w + 2 * c->bw <= m->wx)
399 if(*y + *h + 2 * c->bw <= m->wy)
406 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
407 /* see last two sentences in ICCCM 4.1.2.3 */
408 baseismin = c->basew == c->minw && c->baseh == c->minh;
409 if(!baseismin) { /* temporarily remove base dimensions */
413 /* adjust for aspect limits */
414 if(c->mina > 0 && c->maxa > 0) {
415 if(c->maxa < (float)*w / *h)
416 *w = *h * c->maxa + 0.5;
417 else if(c->mina < (float)*h / *w)
418 *h = *w * c->mina + 0.5;
420 if(baseismin) { /* increment calculation requires this */
424 /* adjust for increment value */
429 /* restore base dimensions */
430 *w = MAX(*w + c->basew, c->minw);
431 *h = MAX(*h + c->baseh, c->minh);
433 *w = MIN(*w, c->maxw);
435 *h = MIN(*h, c->maxh);
437 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
441 arrange(Monitor *m) {
444 else for(m = mons; m; m = m->next)
449 } else for(m = mons; m; m = m->next)
454 arrangemon(Monitor *m) {
455 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
456 if(m->lt[m->sellt]->arrange)
457 m->lt[m->sellt]->arrange(m);
461 attach_as_master(Client *c) {
462 c->next = c->mon->clients;
468 c->next = c->mon->sel->next;
469 c->mon->sel->next = c;
476 attachstack(Client *c) {
477 c->snext = c->mon->stack;
482 buttonpress(XEvent *e) {
483 unsigned int i, x, click;
487 XButtonPressedEvent *ev = &e->xbutton;
490 /* focus monitor if necessary */
491 if((m = wintomon(ev->window)) && m != selmon) {
492 unfocus(selmon->sel, True);
496 if(ev->window == selmon->barwin) {
500 while(ev->x >= x && ++i < LENGTH(tags));
501 if(i < LENGTH(tags)) {
505 else if(ev->x < x + blw)
507 else if(ev->x > selmon->ww - TEXTW(stext))
508 click = ClkStatusText;
512 else if((c = wintoclient(ev->window))) {
514 click = ClkClientWin;
516 for(i = 0; i < LENGTH(buttons); i++)
517 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
518 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state))
519 buttons[i].func(click == ClkTagBar && buttons[i].arg.i == 0 ? &arg : &buttons[i].arg);
524 xerrorxlib = XSetErrorHandler(xerrorstart);
525 /* this causes an error if some other window manager is running */
526 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
528 XSetErrorHandler(xerror);
535 Layout foo = { "", NULL };
539 selmon->lt[selmon->sellt] = &foo;
540 for(m = mons; m; m = m->next)
542 unmanage(m->stack, False);
543 XUngrabKey(dpy, AnyKey, AnyModifier, root);
546 drw_cur_free(drw, cursor[CurNormal]);
547 drw_cur_free(drw, cursor[CurResize]);
548 drw_cur_free(drw, cursor[CurMove]);
549 drw_font_free(dpy, fnt);
550 drw_clr_free(scheme[SchemeNorm].border);
551 drw_clr_free(scheme[SchemeNorm].bg);
552 drw_clr_free(scheme[SchemeNorm].fg);
553 drw_clr_free(scheme[SchemeSel].border);
554 drw_clr_free(scheme[SchemeSel].bg);
555 drw_clr_free(scheme[SchemeSel].fg);
558 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
559 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
563 cleanupmon(Monitor *mon) {
569 for(m = mons; m && m->next != mon; m = m->next);
572 XUnmapWindow(dpy, mon->barwin);
573 XDestroyWindow(dpy, mon->barwin);
578 clearurgent(Client *c) {
582 if(!(wmh = XGetWMHints(dpy, c->win)))
584 wmh->flags &= ~XUrgencyHint;
585 XSetWMHints(dpy, c->win, wmh);
590 clientmessage(XEvent *e) {
591 XClientMessageEvent *cme = &e->xclient;
592 Client *c = wintoclient(cme->window);
596 if(cme->message_type == netatom[NetWMState]) {
597 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
598 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
599 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
601 else if(cme->message_type == netatom[NetActiveWindow]) {
604 c->mon->seltags ^= 1;
605 c->mon->tagset[c->mon->seltags] = c->tags;
612 configure(Client *c) {
615 ce.type = ConfigureNotify;
623 ce.border_width = c->bw;
625 ce.override_redirect = False;
626 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
630 configurenotify(XEvent *e) {
632 XConfigureEvent *ev = &e->xconfigure;
635 // TODO: updategeom handling sucks, needs to be simplified
636 if(ev->window == root) {
637 dirty = (sw != ev->width || sh != ev->height);
640 if(updategeom() || dirty) {
641 drw_resize(drw, sw, bh);
643 for(m = mons; m; m = m->next)
644 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
652 configurerequest(XEvent *e) {
655 XConfigureRequestEvent *ev = &e->xconfigurerequest;
658 if((c = wintoclient(ev->window))) {
659 if(ev->value_mask & CWBorderWidth)
660 c->bw = ev->border_width;
661 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
663 if(ev->value_mask & CWX) {
665 c->x = m->mx + ev->x;
667 if(ev->value_mask & CWY) {
669 c->y = m->my + ev->y;
671 if(ev->value_mask & CWWidth) {
675 if(ev->value_mask & CWHeight) {
679 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
680 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
681 if((c->y + c->h) > m->my + m->mh && c->isfloating)
682 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
683 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
686 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
694 wc.width = ev->width;
695 wc.height = ev->height;
696 wc.border_width = ev->border_width;
697 wc.sibling = ev->above;
698 wc.stack_mode = ev->detail;
699 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
708 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
709 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
710 m->tagset[0] = m->tagset[1] = 1;
712 m->nmaster = nmaster;
713 m->showbar = showbar;
715 m->lt[0] = &layouts[0];
716 m->lt[1] = &layouts[1 % LENGTH(layouts)];
717 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
722 destroynotify(XEvent *e) {
724 XDestroyWindowEvent *ev = &e->xdestroywindow;
726 if((c = wintoclient(ev->window)))
734 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
738 // NOTE: the stack is for z-order and most-recently-focused
739 // only mon->clients determines position in visible layout
741 detachstack(Client *c) {
742 Client *prev = NULL, *next_sel = NULL, *i;
743 for(i = c->mon->stack; i && i != c; i = i->snext) {
746 if(c == c->mon->sel) {
747 // find last visible window before c
748 // WARNING if you detach() before detachstack() this will select last visible window
749 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
751 // failing that, find first visible window (besides c)
753 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
756 c->mon->sel = next_sel;
759 prev->snext = c->snext;
761 c->mon->stack = c->snext;
770 if(!(m = selmon->next))
773 else if(selmon == mons)
774 for(m = mons; m->next; m = m->next);
776 for(m = mons; m->next != selmon; m = m->next);
781 drawbar(Monitor *m) {
783 unsigned int i, occ = 0, urg = 0;
786 for(c = m->clients; c; c = c->next) {
792 for(i = 0; i < LENGTH(tags); i++) {
794 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
795 drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
796 drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
797 occ & 1 << i, urg & 1 << i);
800 w = blw = TEXTW(m->ltsymbol);
801 drw_setscheme(drw, &scheme[SchemeNorm]);
802 drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
805 if(m == selmon) { /* status is only drawn on selected monitor */
812 drw_text(drw, x, 0, w, bh, stext, 0);
816 if((w = x - xx) > bh) {
819 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
820 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
821 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
824 drw_setscheme(drw, &scheme[SchemeNorm]);
825 drw_text(drw, x, 0, w, bh, NULL, 0);
828 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
835 for(m = mons; m; m = m->next)
840 enternotify(XEvent *e) {
843 XCrossingEvent *ev = &e->xcrossing;
845 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
847 c = wintoclient(ev->window);
848 m = c ? c->mon : wintomon(ev->window);
850 unfocus(selmon->sel, True);
853 else if(!c || c == selmon->sel)
861 XExposeEvent *ev = &e->xexpose;
863 if(ev->count == 0 && (m = wintomon(ev->window)))
869 if(!c || !ISVISIBLE(c))
870 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
871 /* was if(selmon->sel) */
872 if(selmon->sel && selmon->sel != c)
873 unfocus(selmon->sel, False);
881 grabbuttons(c, True);
882 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
886 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
887 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
890 jason_layout(selmon);
891 update_window_opacities(selmon);
893 if(c && (!root || (c->win!=root)) )
894 window_set_opaque(c);
898 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
899 XFocusChangeEvent *ev = &e->xfocus;
901 if(selmon->sel && ev->window != selmon->sel->win)
902 setfocus(selmon->sel);
906 focusmon(const Arg *arg) {
911 if((m = dirtomon(arg->i)) == selmon)
913 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
914 in gedit and anjuta */
920 focusstack(const Arg *arg) {
921 Client *c = NULL, *i;
926 for(i = selmon->clients; i != selmon->sel; i = i->next) {
932 } else if(arg->i > 0) {
933 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
935 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
938 for(i = selmon->clients; i != selmon->sel; i = i->next)
942 for(; i; i = i->next)
953 getatomprop(Client *c, Atom prop) {
956 unsigned char *p = NULL;
957 Atom da, atom = None;
959 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
960 &da, &di, &dl, &dl, &p) == Success && p) {
968 getrootptr(int *x, int *y) {
973 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
980 unsigned char *p = NULL;
981 unsigned long n, extra;
984 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
985 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
994 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
999 if(!text || size == 0)
1002 XGetTextProperty(dpy, w, &name, atom);
1005 if(name.encoding == XA_STRING)
1006 strncpy(text, (char *)name.value, size - 1);
1008 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1009 strncpy(text, *list, size - 1);
1010 XFreeStringList(list);
1013 text[size - 1] = '\0';
1019 grabbuttons(Client *c, Bool focused) {
1020 updatenumlockmask();
1023 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1024 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1026 for(i = 0; i < LENGTH(buttons); i++)
1027 if(buttons[i].click == ClkClientWin)
1028 for(j = 0; j < LENGTH(modifiers); j++)
1029 XGrabButton(dpy, buttons[i].button,
1030 buttons[i].mask | modifiers[j],
1031 c->win, False, BUTTONMASK,
1032 GrabModeAsync, GrabModeSync, None, None);
1035 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1036 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1042 updatenumlockmask();
1045 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1048 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1049 for(i = 0; i < LENGTH(keys); i++)
1050 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1051 for(j = 0; j < LENGTH(modifiers); j++)
1052 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1053 True, GrabModeAsync, GrabModeAsync);
1058 incnmaster(const Arg *arg) {
1059 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1065 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1067 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1068 && unique[n].width == info->width && unique[n].height == info->height)
1072 #endif /* XINERAMA */
1075 keypress(XEvent *e) {
1081 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1082 for(i = 0; i < LENGTH(keys); i++)
1083 if(keysym == keys[i].keysym
1084 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1086 keys[i].func(&(keys[i].arg));
1090 killclient(const Arg *arg) {
1093 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1095 XSetErrorHandler(xerrordummy);
1096 XSetCloseDownMode(dpy, DestroyAll);
1097 XKillClient(dpy, selmon->sel->win);
1099 XSetErrorHandler(xerror);
1105 manage(Window w, XWindowAttributes *wa) {
1106 Client *c, *t = NULL;
1107 Window trans = None;
1110 if(!(c = calloc(1, sizeof(Client))))
1111 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1112 c->opacity = -1; // who knows
1115 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1124 c->x = c->oldx = wa->x;
1125 c->y = c->oldy = wa->y;
1126 c->w = c->oldw = wa->width;
1127 c->h = c->oldh = wa->height;
1128 c->oldbw = wa->border_width;
1130 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1131 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1132 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1133 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1134 c->x = MAX(c->x, c->mon->mx);
1135 /* only fix client y-offset, if the client center might cover the bar */
1136 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1137 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1140 wc.border_width = c->bw;
1141 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1142 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1143 configure(c); /* propagates border_width, if size doesn't change */
1144 updatewindowtype(c);
1147 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1148 grabbuttons(c, False);
1150 c->isfloating = c->oldstate = trans != None || c->isfixed;
1152 XRaiseWindow(dpy, c->win);
1155 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1156 (unsigned char *) &(c->win), 1);
1157 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1158 setclientstate(c, NormalState);
1159 if (c->mon == selmon)
1160 unfocus(selmon->sel, False);
1163 XMapWindow(dpy, c->win);
1168 mappingnotify(XEvent *e) {
1169 XMappingEvent *ev = &e->xmapping;
1171 XRefreshKeyboardMapping(ev);
1172 if(ev->request == MappingKeyboard)
1177 maprequest(XEvent *e) {
1178 static XWindowAttributes wa;
1179 XMapRequestEvent *ev = &e->xmaprequest;
1181 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1183 if(wa.override_redirect)
1185 if(!wintoclient(ev->window))
1186 manage(ev->window, &wa);
1190 monocle(Monitor *m) {
1194 for(c = m->clients; c; c = c->next)
1197 if(n > 0) /* override layout symbol */
1198 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1199 for(c = nexttiled(m->clients); c; c = nexttiled(c->next))
1200 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1204 motionnotify(XEvent *e) {
1205 static Monitor *mon = NULL;
1207 XMotionEvent *ev = &e->xmotion;
1209 if(ev->window != root)
1211 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1212 unfocus(selmon->sel, True);
1220 movemouse(const Arg *arg) {
1221 int x, y, ocx, ocy, nx, ny;
1227 if(!(c = selmon->sel))
1229 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1234 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1235 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1237 if(!getrootptr(&x, &y))
1240 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1242 case ConfigureRequest:
1245 handler[ev.type](&ev);
1248 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1250 lasttime = ev.xmotion.time;
1252 nx = ocx + (ev.xmotion.x - x);
1253 ny = ocy + (ev.xmotion.y - y);
1254 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1255 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1256 if(abs(selmon->wx - nx) < snap)
1258 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1259 nx = selmon->wx + selmon->ww - WIDTH(c);
1260 if(abs(selmon->wy - ny) < snap)
1262 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1263 ny = selmon->wy + selmon->wh - HEIGHT(c);
1264 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1265 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1266 togglefloating(NULL);
1268 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1269 resize(c, nx, ny, c->w, c->h, True, 0);
1272 } while(ev.type != ButtonRelease);
1273 XUngrabPointer(dpy, CurrentTime);
1274 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1282 nexttiled(Client *c) {
1283 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1288 nextvisible(Client *c) {
1289 for(; c && !ISVISIBLE(c); c = c->next);
1296 attach_as_master(c);
1302 propertynotify(XEvent *e) {
1305 XPropertyEvent *ev = &e->xproperty;
1307 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1309 else if(ev->state == PropertyDelete)
1310 return; /* ignore */
1311 else if((c = wintoclient(ev->window))) {
1314 case XA_WM_TRANSIENT_FOR:
1315 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1316 (c->isfloating = (wintoclient(trans)) != NULL))
1319 case XA_WM_NORMAL_HINTS:
1327 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1329 if(c == c->mon->sel)
1332 if(ev->atom == netatom[NetWMWindowType])
1333 updatewindowtype(c);
1338 quit(const Arg *arg) {
1343 recttomon(int x, int y, int w, int h) {
1344 Monitor *m, *r = selmon;
1347 for(m = mons; m; m = m->next)
1348 if((a = INTERSECT(x, y, w, h, m)) > area) {
1356 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1357 if(applysizehints(c, &x, &y, &w, &h, interact))
1358 resizeclient(c, x, y, w, h, base);
1362 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1364 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1366 c->oldx = c->x; c->x = wc.x = x;
1367 c->oldy = c->y; c->y = wc.y = y;
1368 c->oldw = c->w; c->w = wc.width = w;
1369 c->oldh = c->h; c->h = wc.height = h;
1371 wc.stack_mode = Above;
1372 wc.sibling = base->win;
1373 mask |= CWStackMode|CWSibling;
1375 wc.border_width = c->bw;
1376 XConfigureWindow(dpy, c->win, mask, &wc);
1382 resizemouse(const Arg *arg) {
1383 int ocx, ocy, nw, nh;
1389 if(!(c = selmon->sel))
1391 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1396 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1397 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1399 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1401 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1403 case ConfigureRequest:
1406 handler[ev.type](&ev);
1409 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1411 lasttime = ev.xmotion.time;
1413 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1414 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1415 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1416 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1418 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1419 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1420 togglefloating(NULL);
1422 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1423 resize(c, c->x, c->y, nw, nh, True, 0);
1426 } while(ev.type != ButtonRelease);
1427 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1428 XUngrabPointer(dpy, CurrentTime);
1429 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1430 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1438 restack(Monitor *m) {
1446 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1447 XRaiseWindow(dpy, m->sel->win);
1448 if(m->lt[m->sellt]->arrange) {
1449 wc.stack_mode = Below;
1450 wc.sibling = m->barwin;
1451 for(c = m->clients; c; c = c->next)
1452 if(!c->isfloating && ISVISIBLE(c)) {
1453 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1454 wc.sibling = c->win;
1455 wc.stack_mode = Above;
1459 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1465 /* main event loop */
1467 while(running && !XNextEvent(dpy, &ev))
1468 if(handler[ev.type])
1469 handler[ev.type](&ev); /* call handler */
1474 unsigned int i, num;
1475 Window d1, d2, *wins = NULL;
1476 XWindowAttributes wa;
1478 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1479 for(i = 0; i < num; i++) {
1480 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1481 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1483 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1484 manage(wins[i], &wa);
1486 for(i = 0; i < num; i++) { /* now the transients */
1487 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1489 if(XGetTransientForHint(dpy, wins[i], &d1)
1490 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1491 manage(wins[i], &wa);
1499 sendmon(Client *c, Monitor *m) {
1506 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1514 setclientstate(Client *c, long state) {
1515 long data[] = { state, None };
1517 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1518 PropModeReplace, (unsigned char *)data, 2);
1522 sendevent(Client *c, Atom proto) {
1525 Bool exists = False;
1528 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1529 while(!exists && n--)
1530 exists = protocols[n] == proto;
1534 ev.type = ClientMessage;
1535 ev.xclient.window = c->win;
1536 ev.xclient.message_type = wmatom[WMProtocols];
1537 ev.xclient.format = 32;
1538 ev.xclient.data.l[0] = proto;
1539 ev.xclient.data.l[1] = CurrentTime;
1540 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1546 setfocus(Client *c) {
1547 if(!c->neverfocus) {
1548 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1549 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1550 XA_WINDOW, 32, PropModeReplace,
1551 (unsigned char *) &(c->win), 1);
1553 sendevent(c, wmatom[WMTakeFocus]);
1557 setfullscreen(Client *c, Bool fullscreen) {
1559 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1560 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1561 c->isfullscreen = True;
1562 c->oldstate = c->isfloating;
1565 c->isfloating = True;
1566 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1567 XRaiseWindow(dpy, c->win);
1570 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1571 PropModeReplace, (unsigned char*)0, 0);
1572 c->isfullscreen = False;
1573 c->isfloating = c->oldstate;
1579 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1585 setlayout(const Arg *arg) {
1586 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1589 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1590 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1597 /* arg > 1.0 will set mfact absolutly */
1599 setmfact(const Arg *arg) {
1602 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1604 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1605 if(f < 0.1 || f > 0.9)
1613 XSetWindowAttributes wa;
1615 /* clean up any zombies immediately */
1619 screen = DefaultScreen(dpy);
1620 root = RootWindow(dpy, screen);
1621 fnt = drw_font_create(dpy, font);
1622 sw = DisplayWidth(dpy, screen);
1623 sh = DisplayHeight(dpy, screen);
1625 drw = drw_create(dpy, screen, root, sw, sh);
1626 drw_setfont(drw, fnt);
1629 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1630 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1631 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1632 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1633 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1634 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1635 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1636 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1637 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1638 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1639 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1640 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1641 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1642 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1644 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1645 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1646 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1647 /* init appearance */
1648 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1649 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1650 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1651 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1652 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1653 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1657 /* EWMH support per view */
1658 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1659 PropModeReplace, (unsigned char *) netatom, NetLast);
1660 XDeleteProperty(dpy, root, netatom[NetClientList]);
1661 /* select for events */
1662 wa.cursor = cursor[CurNormal]->cursor;
1663 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1664 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1665 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1666 XSelectInput(dpy, root, wa.event_mask);
1672 showhide(Client *c) {
1675 if(ISVISIBLE(c)) { /* show clients top down */
1676 XMoveWindow(dpy, c->win, c->x, c->y);
1677 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1678 resize(c, c->x, c->y, c->w, c->h, False, 0);
1681 else { /* hide clients bottom up */
1683 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1688 sigchld(int unused) {
1689 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1690 die("Can't install SIGCHLD handler");
1691 while(0 < waitpid(-1, NULL, WNOHANG));
1695 spawn(const Arg *arg) {
1697 if(arg->v == termcmd) {
1698 for(i = 0; i < 32; ++i) {
1699 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1704 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1705 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1];
1707 if(arg->v == dmenucmd)
1708 dmenumon[0] = '0' + selmon->num;
1711 close(ConnectionNumber(dpy));
1713 execvp(((char **)arg->v)[0], (char **)arg->v);
1714 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1721 tag(const Arg *arg) {
1722 if(selmon->sel && arg->ui & TAGMASK) {
1723 selmon->sel->tags = arg->ui & TAGMASK;
1730 tagmon(const Arg *arg) {
1731 if(!selmon->sel || !mons->next)
1733 sendmon(selmon->sel, dirtomon(arg->i));
1738 unsigned int i, n, h, mw, my, ty;
1741 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1746 mw = m->nmaster ? m->ww * m->mfact : 0;
1748 c = nexttiled(m->clients);
1749 if (c && !c->screen_hog)
1750 mw = m->ww * m->mfact;
1754 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1755 if(i < m->nmaster) {
1756 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1757 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1761 h = (m->wh - ty) / (n - i);
1762 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1768 jason_layout(Monitor *m) {
1769 unsigned int i, tiled_count, mw, right_width, tabs_count, cur_tab;
1770 Client *c, *vis_slave, *base = 0;
1772 for(tiled_count = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), tiled_count++);
1773 if(tiled_count == 0) {
1777 c = nexttiled(m->clients);
1779 // if master is selected, show first slave
1780 vis_slave = nexttiled(c->next);
1784 if(tiled_count > 1 || (tiled_count == 1 && !c->screen_hog)) {
1785 mw = m->ww * m->mfact;
1788 // * zero tiled windows
1789 // * one tiled window that's not a screen hog
1790 // * miltiple tiled windows
1793 right_width = m->ww - mw;
1794 tabs_count = tiled_count - 2;
1796 for (i = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++) {
1798 resize(c, m->wx, m->wy, mw, m->wh, False, 0);
1800 if (c == vis_slave) {
1801 resize(c, m->wx + mw, m->wy, right_width, m->wh - 30, False, base);
1803 // this function does not get called when focus changes
1804 // resize(c, m->wx + m->ww, m->wy, m->ww - mw, m->wh, False);
1805 resize(c, m->wx + mw + right_width * cur_tab / tabs_count, m->wy + m->wh - 20, right_width, m->wh - 30, False, base);
1814 togglebar(const Arg *arg) {
1815 selmon->showbar = !selmon->showbar;
1816 updatebarpos(selmon);
1817 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1822 togglefloating(const Arg *arg) {
1825 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1827 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1828 if(selmon->sel->isfloating)
1829 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1830 selmon->sel->w, selmon->sel->h, False, 0);
1835 toggletag(const Arg *arg) {
1836 unsigned int newtags;
1840 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1842 selmon->sel->tags = newtags;
1849 toggleview(const Arg *arg) {
1850 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1853 selmon->tagset[selmon->seltags] = newtagset;
1860 unfocus(Client *c, Bool setfocus) {
1863 grabbuttons(c, False);
1864 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1866 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1867 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1872 unmanage(Client *c, Bool destroyed) {
1873 Monitor *m = c->mon;
1876 /* The server grab construct avoids race conditions. */
1880 wc.border_width = c->oldbw;
1882 XSetErrorHandler(xerrordummy);
1883 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1884 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1885 setclientstate(c, WithdrawnState);
1887 XSetErrorHandler(xerror);
1891 focus(selmon ? selmon->sel : NULL);
1897 unmapnotify(XEvent *e) {
1899 XUnmapEvent *ev = &e->xunmap;
1901 if((c = wintoclient(ev->window))) {
1903 setclientstate(c, WithdrawnState);
1912 XSetWindowAttributes wa = {
1913 .override_redirect = True,
1914 .background_pixmap = ParentRelative,
1915 .event_mask = ButtonPressMask|ExposureMask
1917 for(m = mons; m; m = m->next) {
1920 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
1921 CopyFromParent, DefaultVisual(dpy, screen),
1922 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
1923 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1924 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1925 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
1926 PropModeReplace, (unsigned char *) &(m->barwin), 1);
1927 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
1928 PropModeReplace, (unsigned char *) "dwm", 3);
1929 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
1930 XMapRaised(dpy, m->barwin);
1935 updatebarpos(Monitor *m) {
1940 m->by = m->topbar ? m->wy : m->wy + m->wh;
1941 m->wy = m->topbar ? m->wy + bh : m->wy;
1948 updateclientlist() {
1952 XDeleteProperty(dpy, root, netatom[NetClientList]);
1953 for(m = mons; m; m = m->next)
1954 for(c = m->clients; c; c = c->next)
1955 XChangeProperty(dpy, root, netatom[NetClientList],
1956 XA_WINDOW, 32, PropModeAppend,
1957 (unsigned char *) &(c->win), 1);
1965 if(XineramaIsActive(dpy)) {
1969 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
1970 XineramaScreenInfo *unique = NULL;
1972 for(n = 0, m = mons; m; m = m->next, n++);
1973 /* only consider unique geometries as separate screens */
1974 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
1975 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
1976 for(i = 0, j = 0; i < nn; i++)
1977 if(isuniquegeom(unique, j, &info[i]))
1978 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
1982 for(i = 0; i < (nn - n); i++) { /* new monitors available */
1983 for(m = mons; m && m->next; m = m->next);
1985 m->next = createmon();
1989 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
1991 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
1992 || unique[i].width != m->mw || unique[i].height != m->mh))
1996 m->mx = m->wx = unique[i].x_org;
1997 m->my = m->wy = unique[i].y_org;
1998 m->mw = m->ww = unique[i].width;
1999 m->mh = m->wh = unique[i].height;
2003 else { /* less monitors available nn < n */
2004 for(i = nn; i < n; i++) {
2005 for(m = mons; m && m->next; m = m->next);
2009 m->clients = c->next;
2023 #endif /* XINERAMA */
2024 /* default monitor setup */
2028 if(mons->mw != sw || mons->mh != sh) {
2030 mons->mw = mons->ww = sw;
2031 mons->mh = mons->wh = sh;
2037 selmon = wintomon(root);
2043 updatenumlockmask(void) {
2045 XModifierKeymap *modmap;
2048 modmap = XGetModifierMapping(dpy);
2049 for(i = 0; i < 8; i++)
2050 for(j = 0; j < modmap->max_keypermod; j++)
2051 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2052 == XKeysymToKeycode(dpy, XK_Num_Lock))
2053 numlockmask = (1 << i);
2054 XFreeModifiermap(modmap);
2058 updatesizehints(Client *c) {
2062 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2063 /* size is uninitialized, ensure that size.flags aren't used */
2065 if(size.flags & PBaseSize) {
2066 c->basew = size.base_width;
2067 c->baseh = size.base_height;
2069 else if(size.flags & PMinSize) {
2070 c->basew = size.min_width;
2071 c->baseh = size.min_height;
2074 c->basew = c->baseh = 0;
2075 if(size.flags & PResizeInc) {
2076 c->incw = size.width_inc;
2077 c->inch = size.height_inc;
2080 c->incw = c->inch = 0;
2081 if(size.flags & PMaxSize) {
2082 c->maxw = size.max_width;
2083 c->maxh = size.max_height;
2086 c->maxw = c->maxh = 0;
2087 if(size.flags & PMinSize) {
2088 c->minw = size.min_width;
2089 c->minh = size.min_height;
2091 else if(size.flags & PBaseSize) {
2092 c->minw = size.base_width;
2093 c->minh = size.base_height;
2096 c->minw = c->minh = 0;
2097 if(size.flags & PAspect) {
2098 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2099 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2102 c->maxa = c->mina = 0.0;
2103 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2104 && c->maxw == c->minw && c->maxh == c->minh);
2108 updatetitle(Client *c) {
2109 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2110 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2111 if(c->name[0] == '\0') /* hack to mark broken clients */
2112 strcpy(c->name, broken);
2116 updatestatus(void) {
2117 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2118 strcpy(stext, "dwm-"VERSION);
2123 updatewindowtype(Client *c) {
2124 Atom state = getatomprop(c, netatom[NetWMState]);
2125 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2127 if(state == netatom[NetWMFullscreen])
2128 setfullscreen(c, True);
2129 if(wtype == netatom[NetWMWindowTypeDialog])
2130 c->isfloating = True;
2134 updatewmhints(Client *c) {
2137 if((wmh = XGetWMHints(dpy, c->win))) {
2138 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2139 wmh->flags &= ~XUrgencyHint;
2140 XSetWMHints(dpy, c->win, wmh);
2143 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2144 if(wmh->flags & InputHint)
2145 c->neverfocus = !wmh->input;
2147 c->neverfocus = False;
2153 view(const Arg *arg) {
2154 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2156 selmon->seltags ^= 1; /* toggle sel tagset */
2157 if(arg->ui & TAGMASK)
2158 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2164 wintoclient(Window w) {
2168 for(m = mons; m; m = m->next)
2169 for(c = m->clients; c; c = c->next)
2176 wintomon(Window w) {
2181 if(w == root && getrootptr(&x, &y))
2182 return recttomon(x, y, 1, 1);
2183 for(m = mons; m; m = m->next)
2186 if((c = wintoclient(w)))
2191 /* There's no way to check accesses to destroyed windows, thus those cases are
2192 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2193 * default error handler, which may call exit. */
2195 xerror(Display *dpy, XErrorEvent *ee) {
2196 if(ee->error_code == BadWindow
2197 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2198 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2199 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2200 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2201 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2202 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2203 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2204 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2206 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2207 ee->request_code, ee->error_code);
2208 return xerrorxlib(dpy, ee); /* may call exit */
2212 xerrordummy(Display *dpy, XErrorEvent *ee) {
2216 /* Startup Error handler to check if another window manager
2217 * is already running. */
2219 xerrorstart(Display *dpy, XErrorEvent *ee) {
2220 die("dwm: another window manager is already running\n");
2225 zoom(const Arg *arg) {
2226 Client *c = selmon->sel;
2228 if(!selmon->lt[selmon->sellt]->arrange
2229 || (selmon->sel && selmon->sel->isfloating))
2231 if(c == nexttiled(selmon->clients))
2232 if(!c || !(c = nexttiled(c->next)))
2238 main(int argc, char *argv[]) {
2239 if(argc == 2 && !strcmp("-v", argv[1]))
2240 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2242 die("usage: dwm [-v]\n");
2243 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2244 fputs("warning: no locale support\n", stderr);
2245 if(!(dpy = XOpenDisplay(NULL)))
2246 die("dwm: cannot open display\n");
2253 return EXIT_SUCCESS;