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/XF86keysym.h>
36 #include <X11/Xatom.h>
38 #include <X11/Xproto.h>
39 #include <X11/Xutil.h>
41 #include <X11/extensions/Xinerama.h>
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_NOPAD(X) drw_font_getexts_width(drw->font, X, strlen(X))
59 #define TEXTW(X) (TEXTW_NOPAD(X) + drw->font->h)
62 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
63 enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
64 enum { NetSupported, NetWMName, NetWMState,
65 NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
66 NetWMWindowTypeDialog, NetClientList, NetSupportingWMCheck, NetLast }; /* EWMH atoms */
67 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
68 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
69 ClkClientWin, ClkRootWin, ClkAnywhere, ClkLast }; /* clicks */
82 void (*func)(const Arg *arg);
86 typedef struct Monitor Monitor;
87 typedef struct Client Client;
92 int oldx, oldy, oldw, oldh;
93 int basew, baseh, incw, inch, maxw, maxh, minw, minh;
97 Bool isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, screen_hog;
107 void (*func)(const Arg *);
113 void (*arrange)(Monitor *);
121 int by; /* bar geometry */
122 int mx, my, mw, mh; /* screen size */
123 int wx, wy, ww, wh; /* window area */
124 unsigned int seltags;
126 unsigned int tagset[2];
139 const char *instance;
147 /* function declarations */
148 static void applyrules(Client *c);
149 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
150 static void arrange(Monitor *m);
151 static void arrangemon(Monitor *m);
152 static void attach_as_master(Client *c);
153 static void attach(Client *c);
154 static void attachstack(Client *c);
155 static void buttonpress(XEvent *e);
156 static void checkotherwm(void);
157 static void cleanup(void);
158 static void cleanupmon(Monitor *mon);
159 static void clearurgent(Client *c);
160 static void clientmessage(XEvent *e);
161 static void configure(Client *c);
162 static void configurenotify(XEvent *e);
163 static void configurerequest(XEvent *e);
164 static Monitor *createmon(void);
165 static void destroynotify(XEvent *e);
166 static void detach(Client *c);
167 static void detachstack(Client *c);
168 static Monitor *dirtomon(int dir);
169 static void drawbar(Monitor *m);
170 static void drawbars(void);
171 static void enternotify(XEvent *e);
172 static void expose(XEvent *e);
173 static void focus(Client *c);
174 static void focusin(XEvent *e);
175 static void focusmon(const Arg *arg);
176 static void focusstack(const Arg *arg);
177 static Bool getrootptr(int *x, int *y);
178 static long getstate(Window w);
179 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
180 static void grabbuttons(Client *c, Bool focused);
181 static void grabkeys(void);
182 static void incnmaster(const Arg *arg);
183 static void keypress(XEvent *e);
184 static void killclient(const Arg *arg);
185 static void manage(Window w, XWindowAttributes *wa);
186 static void mappingnotify(XEvent *e);
187 static void maprequest(XEvent *e);
188 static void monocle(Monitor *m);
189 static void motionnotify(XEvent *e);
190 static void movemouse(const Arg *arg);
191 static Client *nexttiled(Client *c);
192 static Client *snexttiled(Client *c);
193 static Client *nextvisible(Client *c);
194 static void pop(Client *);
195 static void propertynotify(XEvent *e);
196 static void quit(const Arg *arg);
197 static Monitor *recttomon(int x, int y, int w, int h);
198 static void resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base);
199 static void resizeclient(Client *c, int x, int y, int w, int h, Client *base);
200 static void resizemouse(const Arg *arg);
201 static void restack(Monitor *m);
202 static void run(void);
203 static void scan(void);
204 static Bool sendevent(Client *c, Atom proto);
205 static void sendmon(Client *c, Monitor *m);
206 static void setclientstate(Client *c, long state);
207 static void setfocus(Client *c);
208 static void setfullscreen(Client *c, Bool fullscreen);
209 static void setlayout(const Arg *arg);
210 static void setmfact(const Arg *arg);
211 static void setup(void);
212 static void showhide(Client *c);
213 static void sigchld(int unused);
214 static void kbspawn(const Arg *arg);
215 static void spawn(const Arg *arg);
216 static void tag(const Arg *arg);
217 static void tagmon(const Arg *arg);
218 static void jason_layout(Monitor *);
219 static void tile(Monitor *);
220 static void togglebar(const Arg *arg);
221 static void togglefloating(const Arg *arg);
222 static void toggletag(const Arg *arg);
223 static void toggleview(const Arg *arg);
224 static void unfocus(Client *c, Bool setfocus);
225 static void unmanage(Client *c, Bool destroyed);
226 static void unmapnotify(XEvent *e);
227 static Bool updategeom(void);
228 static void updatebarpos(Monitor *m);
229 static void updatebars(void);
230 static void updateclientlist(void);
231 static void updatenumlockmask(void);
232 static void updatesizehints(Client *c);
233 static void updatestatus(void);
234 static void updatewindowtype(Client *c);
235 static void updatetitle(Client *c);
236 static void updatewmhints(Client *c);
237 static void view(const Arg *arg);
238 static Client *wintoclient(Window w);
239 static Monitor *wintomon(Window w);
240 static int xerror(Display *dpy, XErrorEvent *ee);
241 static int xerrordummy(Display *dpy, XErrorEvent *ee);
242 static int xerrorstart(Display *dpy, XErrorEvent *ee);
243 static void zoom(const Arg *arg);
246 static Bool key_buffering = False;
247 static const char broken[] = "broken";
248 static char stext[256];
250 static int sw, sh; // X display screen geometry width, height
251 static int bh; // bar height
252 static int (*xerrorxlib)(Display *, XErrorEvent *);
253 static unsigned int numlockmask = 0;
254 static void (*handler[LASTEvent]) (XEvent *) = {
255 [ButtonPress] = buttonpress,
256 [ClientMessage] = clientmessage,
257 [ConfigureRequest] = configurerequest,
258 [ConfigureNotify] = configurenotify,
259 [DestroyNotify] = destroynotify,
260 [EnterNotify] = enternotify,
263 [KeyPress] = keypress,
264 [MappingNotify] = mappingnotify,
265 [MapRequest] = maprequest,
266 [MotionNotify] = motionnotify,
267 [PropertyNotify] = propertynotify,
268 [UnmapNotify] = unmapnotify
270 static Atom wmatom[WMLast], netatom[NetLast];
271 static Bool running = True;
272 static Cur *cursor[CurLast];
273 static ClrScheme scheme[SchemeLast];
277 static Monitor *mons, *selmon;
280 // unfocused windows get transparent (feature)
281 static const unsigned long opacities[] = { 0, 0xbfffffff, 0x00000000 }; // first unused
282 static void window_set_opaque(Client *c);
283 static void window_set_translucent(Client *c);
284 static void window_set_invisible(Client *c);
285 static void window_set_opacity(Client *c, int opacity_index);
286 static Client* choose_slave (Client *master, Client *focused);
287 static void update_window_opacities(Monitor *m);
289 window_set_opacity(Client *c, int opacity_index) {
290 if (c->opacity == opacity_index) {
293 c->opacity = opacity_index;
294 if (opacity_index == 0) {
295 XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
297 XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)(&opacities[opacity_index]), 1);
301 window_set_opaque(Client *c) {
302 window_set_opacity(c, 0);
305 window_set_translucent(Client *c) {
306 window_set_opacity(c, 1);
309 window_set_invisible(Client *c) {
310 window_set_opacity(c, 2);
313 update_window_opacities(Monitor *m) {
314 Client *master, *slave, *focused, *c;
315 Bool focused_is_floating = False;
316 master = nexttiled(m->clients);
317 focused = (m->sel && ISVISIBLE(m->sel)) ? m->sel : NULL;
318 slave = choose_slave(master, focused);
319 // detect if the focused window is floating (and visible)
320 if (master && focused && focused != slave && focused != master) {
321 if (nexttiled(focused) != focused) {
322 focused_is_floating = True;
325 // set opacity of visible floaters, master and slave
326 for (c = nextvisible(m->clients); c; c = nextvisible(c->next)) {
327 if (c->isfloating || c == focused || (focused_is_floating && (c == master || c == slave))) {
328 window_set_opaque(c);
329 } else if (c == master || c == slave) {
330 window_set_translucent(c);
336 /* configuration, allows nested code to access above variables */
339 /* compile-time check if all tags fit into an unsigned int bit array. */
340 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
342 /* function implementations */
344 applyrules(Client *c) {
345 const char *class, *instance;
349 XClassHint ch = { NULL, NULL };
352 c->isfloating = c->tags = c->screen_hog = 0;
353 XGetClassHint(dpy, c->win, &ch);
354 class = ch.res_class ? ch.res_class : broken;
355 instance = ch.res_name ? ch.res_name : broken;
357 for(i = 0; i < LENGTH(rules); i++) {
359 if((!r->title || c->name == strstr(c->name, r->title))
360 && (!r->class || strstr(class, r->class))
361 && (!r->instance || strstr(instance, r->instance)))
363 c->isfloating = r->isfloating;
365 c->x = -1; c->y = -2; // secret code for centered
368 c->screen_hog = r->screen_hog;
369 for(m = mons; m && m->num != r->monitor; m = m->next);
378 c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
382 applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact) {
384 // Monitor *m = c->mon;
386 /* set minimum possible */
394 if(*x + *w + 2 * c->bw < 0)
396 if(*y + *h + 2 * c->bw < 0)
399 // jason: let windows be offscreen
401 // if(*x >= m->wx + m->ww)
402 // *x = m->wx + m->ww - WIDTH(c);
403 // if(*y >= m->wy + m->wh)
404 // *y = m->wy + m->wh - HEIGHT(c);
405 // if(*x + *w + 2 * c->bw <= m->wx)
407 // if(*y + *h + 2 * c->bw <= m->wy)
414 if(resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) {
415 /* see last two sentences in ICCCM 4.1.2.3 */
416 baseismin = c->basew == c->minw && c->baseh == c->minh;
417 if(!baseismin) { /* temporarily remove base dimensions */
421 /* adjust for aspect limits */
422 if(c->mina > 0 && c->maxa > 0) {
423 if(c->maxa < (float)*w / *h)
424 *w = *h * c->maxa + 0.5;
425 else if(c->mina < (float)*h / *w)
426 *h = *w * c->mina + 0.5;
428 if(baseismin) { /* increment calculation requires this */
432 /* adjust for increment value */
437 /* restore base dimensions */
438 *w = MAX(*w + c->basew, c->minw);
439 *h = MAX(*h + c->baseh, c->minh);
441 *w = MIN(*w, c->maxw);
443 *h = MIN(*h, c->maxh);
445 return *x != c->x || *y != c->y || *w != c->w || *h != c->h;
449 arrange(Monitor *m) {
452 else for(m = mons; m; m = m->next)
457 } else for(m = mons; m; m = m->next)
462 arrangemon(Monitor *m) {
463 strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
464 if(m->lt[m->sellt]->arrange)
465 m->lt[m->sellt]->arrange(m);
469 attach_as_master(Client *c) {
470 c->next = c->mon->clients;
476 c->next = c->mon->sel->next;
477 c->mon->sel->next = c;
484 attachstack(Client *c) {
485 c->snext = c->mon->stack;
490 buttonpress(XEvent *e) {
491 unsigned int i, click;
495 XButtonPressedEvent *ev = &e->xbutton;
498 for(i = 0; i < LENGTH(buttons); i++) {
499 if(buttons[i].click == ClkAnywhere && buttons[i].button == ev->button
500 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
501 buttons[i].func(&buttons[i].arg);
510 /* focus monitor if necessary */
511 if((m = wintomon(ev->window)) && m != selmon) {
512 unfocus(selmon->sel, True);
516 if(ev->window == selmon->barwin) {
518 } else if((c = wintoclient(ev->window))) {
520 click = ClkClientWin;
522 for(i = 0; i < LENGTH(buttons); i++) {
523 if(click == buttons[i].click && buttons[i].func && buttons[i].button == ev->button
524 && CLEANMASK(buttons[i].mask) == CLEANMASK(ev->state)) {
525 if (click == ClkTagBar && buttons[i].arg.i == 0) {
526 buttons[i].func(&arg);
528 buttons[i].func(&buttons[i].arg);
536 xerrorxlib = XSetErrorHandler(xerrorstart);
537 /* this causes an error if some other window manager is running */
538 XSelectInput(dpy, DefaultRootWindow(dpy), SubstructureRedirectMask);
540 XSetErrorHandler(xerror);
547 Layout foo = { "", NULL };
551 selmon->lt[selmon->sellt] = &foo;
552 for(m = mons; m; m = m->next)
554 unmanage(m->stack, False);
555 XUngrabKey(dpy, AnyKey, AnyModifier, root);
558 drw_cur_free(drw, cursor[CurNormal]);
559 drw_cur_free(drw, cursor[CurResize]);
560 drw_cur_free(drw, cursor[CurMove]);
561 drw_font_free(dpy, fnt);
562 drw_clr_free(scheme[SchemeNorm].border);
563 drw_clr_free(scheme[SchemeNorm].bg);
564 drw_clr_free(scheme[SchemeNorm].fg);
565 drw_clr_free(scheme[SchemeSel].border);
566 drw_clr_free(scheme[SchemeSel].bg);
567 drw_clr_free(scheme[SchemeSel].fg);
570 XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
571 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
575 cleanupmon(Monitor *mon) {
581 for(m = mons; m && m->next != mon; m = m->next);
584 XUnmapWindow(dpy, mon->barwin);
585 XDestroyWindow(dpy, mon->barwin);
590 clearurgent(Client *c) {
594 if(!(wmh = XGetWMHints(dpy, c->win)))
596 wmh->flags &= ~XUrgencyHint;
597 XSetWMHints(dpy, c->win, wmh);
602 clientmessage(XEvent *e) {
603 XClientMessageEvent *cme = &e->xclient;
604 Client *c = wintoclient(cme->window);
608 if(cme->message_type == netatom[NetWMState]) {
609 if(cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
610 setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD */
611 || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
613 else if(cme->message_type == netatom[NetActiveWindow]) {
614 // Jason added this so apps can't steal focus:
617 c->mon->seltags ^= 1;
618 c->mon->tagset[c->mon->seltags] = c->tags;
625 configure(Client *c) {
628 ce.type = ConfigureNotify;
636 ce.border_width = c->bw;
638 ce.override_redirect = False;
639 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
643 configurenotify(XEvent *e) {
645 XConfigureEvent *ev = &e->xconfigure;
648 // TODO: updategeom handling sucks, needs to be simplified
649 if(ev->window == root) {
650 dirty = (sw != ev->width || sh != ev->height);
653 if(updategeom() || dirty) {
654 drw_resize(drw, sw, bh);
656 for(m = mons; m; m = m->next)
657 XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
665 configurerequest(XEvent *e) {
668 XConfigureRequestEvent *ev = &e->xconfigurerequest;
671 if((c = wintoclient(ev->window))) {
672 if(ev->value_mask & CWBorderWidth)
673 c->bw = ev->border_width;
674 else if(c->isfloating || !selmon->lt[selmon->sellt]->arrange) {
676 if(ev->value_mask & CWX) {
678 c->x = m->mx + ev->x;
680 if(ev->value_mask & CWY) {
682 c->y = m->my + ev->y;
684 if(ev->value_mask & CWWidth) {
688 if(ev->value_mask & CWHeight) {
692 if((c->x + c->w) > m->mx + m->mw && c->isfloating)
693 c->x = m->mx + (m->mw / 2 - WIDTH(c) / 2); /* center in x direction */
694 if((c->y + c->h) > m->my + m->mh && c->isfloating)
695 c->y = m->my + (m->mh / 2 - HEIGHT(c) / 2); /* center in y direction */
696 if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
699 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
707 wc.width = ev->width;
708 wc.height = ev->height;
709 wc.border_width = ev->border_width;
710 wc.sibling = ev->above;
711 wc.stack_mode = ev->detail;
712 XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
721 if(!(m = (Monitor *)calloc(1, sizeof(Monitor))))
722 die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
723 m->tagset[0] = m->tagset[1] = 1;
725 m->nmaster = nmaster;
726 m->showbar = showbar;
728 m->lt[0] = &layouts[0];
729 m->lt[1] = &layouts[1 % LENGTH(layouts)];
730 strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
735 destroynotify(XEvent *e) {
737 XDestroyWindowEvent *ev = &e->xdestroywindow;
739 if((c = wintoclient(ev->window)))
747 for(tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next);
751 // NOTE: the stack is for z-order and most-recently-focused
752 // only mon->clients determines position in visible layout
754 detachstack(Client *c) {
755 Client *prev = NULL, *next_sel = NULL, *i;
756 for(i = c->mon->stack; i && i != c; i = i->snext) {
759 if(c == c->mon->sel) {
760 // find last visible window before c
761 // WARNING if you detach() before detachstack() this will select last visible window
762 for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
764 // failing that, find first visible window (besides c)
766 for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
770 c->mon->sel = next_sel;
773 prev->snext = c->snext;
775 c->mon->stack = c->snext;
784 if(!(m = selmon->next))
787 else if(selmon == mons)
788 for(m = mons; m->next; m = m->next);
790 for(m = mons; m->next != selmon; m = m->next);
795 drawbar(Monitor *m) {
797 unsigned int i, occ = 0, urg = 0;
800 for(c = m->clients; c; c = c->next) {
806 for(i = 0; i < LENGTH(tags); i++) {
807 w = TEXTW_NOPAD(tags[i]);
808 drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
809 drw_text_nopad(drw, x, 0, w, bh, tags[i], urg & 1 << i);
813 if(m == selmon) { /* status is only drawn on selected monitor */
820 drw_text(drw, x, 0, w, bh, stext, 0);
824 if((w = x - xx) > bh) {
827 drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
828 drw_text(drw, x, 0, w, bh, m->sel->name, 0);
829 drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
832 drw_setscheme(drw, &scheme[SchemeNorm]);
833 drw_text(drw, x, 0, w, bh, NULL, 0);
836 drw_map(drw, m->barwin, 0, 0, m->ww, bh);
843 for(m = mons; m; m = m->next)
848 enternotify(XEvent *e) {
851 XCrossingEvent *ev = &e->xcrossing;
853 return; // jason: added to stop mouse focus
855 if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
857 c = wintoclient(ev->window);
858 m = c ? c->mon : wintomon(ev->window);
860 unfocus(selmon->sel, True);
863 else if(!c || c == selmon->sel)
871 XExposeEvent *ev = &e->xexpose;
873 if(ev->count == 0 && (m = wintomon(ev->window)))
879 if(!c || !ISVISIBLE(c))
880 for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
881 /* was if(selmon->sel) */
882 if(selmon->sel && selmon->sel != c)
883 unfocus(selmon->sel, False);
891 grabbuttons(c, True);
892 XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
896 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
897 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
901 update_window_opacities(selmon);
903 if(c && (!root || (c->win!=root)) )
904 window_set_opaque(c);
908 focusin(XEvent *e) { /* there are some broken focus acquiring clients */
909 XFocusChangeEvent *ev = &e->xfocus;
911 if(selmon->sel && ev->window != selmon->sel->win)
912 setfocus(selmon->sel);
916 focusmon(const Arg *arg) {
921 if((m = dirtomon(arg->i)) == selmon)
923 unfocus(selmon->sel, False); /* s/True/False/ fixes input focus issues
924 in gedit and anjuta */
930 focusstack(const Arg *arg) {
931 Client *c = NULL, *i;
936 for(i = selmon->clients; i != selmon->sel; i = i->next) {
942 } else if(arg->i > 0) {
943 for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
945 for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
948 for(i = selmon->clients; i != selmon->sel; i = i->next)
952 for(; i; i = i->next)
963 getatomprop(Client *c, Atom prop) {
966 unsigned char *p = NULL;
967 Atom da, atom = None;
969 if(XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
970 &da, &di, &dl, &dl, &p) == Success && p) {
978 getrootptr(int *x, int *y) {
983 return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
990 unsigned char *p = NULL;
991 unsigned long n, extra;
994 if(XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
995 &real, &format, &n, &extra, (unsigned char **)&p) != Success)
1004 gettextprop(Window w, Atom atom, char *text, unsigned int size) {
1009 if(!text || size == 0)
1012 XGetTextProperty(dpy, w, &name, atom);
1015 if(name.encoding == XA_STRING)
1016 strncpy(text, (char *)name.value, size - 1);
1018 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success && n > 0 && *list) {
1019 strncpy(text, *list, size - 1);
1020 XFreeStringList(list);
1023 text[size - 1] = '\0';
1029 grabbuttons(Client *c, Bool focused) {
1030 updatenumlockmask();
1033 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1034 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1036 for(i = 0; i < LENGTH(buttons); i++)
1037 if(buttons[i].click == ClkClientWin || buttons[i].click == ClkAnywhere)
1038 for(j = 0; j < LENGTH(modifiers); j++)
1039 XGrabButton(dpy, buttons[i].button,
1040 buttons[i].mask | modifiers[j],
1041 c->win, False, BUTTONMASK,
1042 GrabModeAsync, GrabModeSync, None, None);
1044 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
1045 BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
1052 updatenumlockmask();
1053 //XUngrabKey(dpy, AnyKey, AnyModifier, root);
1056 unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
1059 XUngrabKey(dpy, AnyKey, AnyModifier, root);
1060 for(i = 0; i < LENGTH(keys); i++)
1061 if((code = XKeysymToKeycode(dpy, keys[i].keysym)))
1062 for(j = 0; j < LENGTH(modifiers); j++)
1063 XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
1064 True, GrabModeAsync,
1065 keys[i].func == kbspawn ? GrabModeSync : GrabModeAsync);
1070 incnmaster(const Arg *arg) {
1071 selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
1077 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info) {
1079 if(unique[n].x_org == info->x_org && unique[n].y_org == info->y_org
1080 && unique[n].width == info->width && unique[n].height == info->height)
1084 #endif /* XINERAMA */
1087 keypress(XEvent *e) {
1093 keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
1094 for(i = 0; i < LENGTH(keys); i++) {
1095 if(keysym == keys[i].keysym
1096 && CLEANMASK(keys[i].mod) == CLEANMASK(ev->state)
1098 keys[i].func(&(keys[i].arg));
1104 killclient(const Arg *arg) {
1107 if(!sendevent(selmon->sel, wmatom[WMDelete])) {
1109 XSetErrorHandler(xerrordummy);
1110 XSetCloseDownMode(dpy, DestroyAll);
1111 XKillClient(dpy, selmon->sel->win);
1113 XSetErrorHandler(xerror);
1119 manage(Window w, XWindowAttributes *wa) {
1120 Client *c, *t = NULL;
1121 Window trans = None;
1124 if(!(c = calloc(1, sizeof(Client))))
1125 die("fatal: could not malloc() %u bytes\n", sizeof(Client));
1126 c->opacity = -1; // who knows
1129 if(XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
1138 if(c->x == -1 && c->y == -2) { // secret code for centered
1139 c->x = c->oldx = (c->mon->ww - wa->width) / 2;
1140 c->y = c->oldy = (c->mon->wh - wa->height) / 2;
1142 c->x = c->oldx = wa->x;
1143 c->y = c->oldy = wa->y;
1145 c->w = c->oldw = wa->width;
1146 c->h = c->oldh = wa->height;
1147 c->oldbw = wa->border_width;
1149 if(c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
1150 c->x = c->mon->mx + c->mon->mw - WIDTH(c);
1151 if(c->y + HEIGHT(c) > c->mon->my + c->mon->mh)
1152 c->y = c->mon->my + c->mon->mh - HEIGHT(c);
1153 c->x = MAX(c->x, c->mon->mx);
1154 /* only fix client y-offset, if the client center might cover the bar */
1155 c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
1156 && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
1159 wc.border_width = c->bw;
1160 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
1161 XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
1162 configure(c); /* propagates border_width, if size doesn't change */
1163 updatewindowtype(c);
1166 XSelectInput(dpy, w, EnterWindowMask|FocusChangeMask|PropertyChangeMask|StructureNotifyMask);
1167 grabbuttons(c, False);
1169 c->isfloating = c->oldstate = trans != None || c->isfixed;
1171 XRaiseWindow(dpy, c->win);
1174 XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
1175 (unsigned char *) &(c->win), 1);
1176 XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
1177 setclientstate(c, NormalState);
1178 if (c->mon == selmon)
1179 unfocus(selmon->sel, False);
1182 XMapWindow(dpy, c->win);
1185 key_buffering = False;
1186 XAllowEvents(dpy, AsyncKeyboard, CurrentTime);
1191 mappingnotify(XEvent *e) {
1192 XMappingEvent *ev = &e->xmapping;
1193 // fprintf(stderr, "MapNotify\n");
1195 XRefreshKeyboardMapping(ev);
1196 if(ev->request == MappingKeyboard)
1201 maprequest(XEvent *e) {
1202 static XWindowAttributes wa;
1203 XMapRequestEvent *ev = &e->xmaprequest;
1205 // fprintf(stderr, "MapRequest\n");
1206 if(!XGetWindowAttributes(dpy, ev->window, &wa))
1208 if(wa.override_redirect) {
1211 if(!wintoclient(ev->window))
1212 manage(ev->window, &wa);
1216 monocle(Monitor *m) {
1220 for(c = m->clients; c; c = c->next)
1223 if(n > 0) /* override layout symbol */
1224 snprintf(m->ltsymbol, sizeof m->ltsymbol, "[%d]", n);
1225 for(c = snexttiled(m->stack); c; c = snexttiled(c->snext)) {
1227 resize(c, m->wx, m->wy, m->ww - 2 * c->bw, m->wh - 2 * c->bw, False, 0);
1229 // this window is should not be visible. move off top, but don't change h/w
1230 resize(c, m->wx, m->wy - 4000, c->w, c->h, False, 0);
1236 motionnotify(XEvent *e) {
1237 static Monitor *mon = NULL;
1239 XMotionEvent *ev = &e->xmotion;
1241 if(ev->window != root)
1243 if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
1244 unfocus(selmon->sel, True);
1252 movemouse(const Arg *arg) {
1253 int x, y, ocx, ocy, nx, ny;
1259 if(!(c = selmon->sel))
1261 if(c->isfullscreen) /* no support moving fullscreen windows by mouse */
1266 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1267 None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
1269 if(!getrootptr(&x, &y))
1272 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1274 case ConfigureRequest:
1277 handler[ev.type](&ev);
1280 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1282 lasttime = ev.xmotion.time;
1284 nx = ocx + (ev.xmotion.x - x);
1285 ny = ocy + (ev.xmotion.y - y);
1286 if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
1287 && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
1288 if(abs(selmon->wx - nx) < snap)
1290 else if(abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
1291 nx = selmon->wx + selmon->ww - WIDTH(c);
1292 if(abs(selmon->wy - ny) < snap)
1294 else if(abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
1295 ny = selmon->wy + selmon->wh - HEIGHT(c);
1296 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1297 && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
1298 togglefloating(NULL);
1300 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1301 resize(c, nx, ny, c->w, c->h, True, 0);
1304 } while(ev.type != ButtonRelease);
1305 XUngrabPointer(dpy, CurrentTime);
1306 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1314 nexttiled(Client *c) {
1315 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
1320 snexttiled(Client *c) {
1321 for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->snext);
1326 nextvisible(Client *c) {
1327 for(; c && !ISVISIBLE(c); c = c->next);
1334 attach_as_master(c);
1340 propertynotify(XEvent *e) {
1343 XPropertyEvent *ev = &e->xproperty;
1345 if((ev->window == root) && (ev->atom == XA_WM_NAME))
1347 else if(ev->state == PropertyDelete)
1348 return; /* ignore */
1349 else if((c = wintoclient(ev->window))) {
1352 case XA_WM_TRANSIENT_FOR:
1353 if(!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
1354 (c->isfloating = (wintoclient(trans)) != NULL))
1357 case XA_WM_NORMAL_HINTS:
1365 if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
1367 if(c == c->mon->sel)
1370 if(ev->atom == netatom[NetWMWindowType])
1371 updatewindowtype(c);
1376 quit(const Arg *arg) {
1381 recttomon(int x, int y, int w, int h) {
1382 Monitor *m, *r = selmon;
1385 for(m = mons; m; m = m->next)
1386 if((a = INTERSECT(x, y, w, h, m)) > area) {
1394 resize(Client *c, int x, int y, int w, int h, Bool interact, Client *base) {
1395 if(applysizehints(c, &x, &y, &w, &h, interact))
1396 resizeclient(c, x, y, w, h, base);
1400 resizeclient(Client *c, int x, int y, int w, int h, Client *base) {
1402 unsigned long mask = CWX|CWY|CWWidth|CWHeight|CWBorderWidth;
1404 c->oldx = c->x; c->x = wc.x = x;
1405 c->oldy = c->y; c->y = wc.y = y;
1406 c->oldw = c->w; c->w = wc.width = w;
1407 c->oldh = c->h; c->h = wc.height = h;
1410 wc.stack_mode = Above;
1411 wc.sibling = base->win;
1412 mask |= CWStackMode|CWSibling;
1414 wc.border_width = c->bw;
1415 XConfigureWindow(dpy, c->win, mask, &wc);
1421 resizemouse(const Arg *arg) {
1422 int ocx, ocy, nw, nh;
1428 if(!(c = selmon->sel))
1430 if(c->isfullscreen) /* no support resizing fullscreen windows by mouse */
1435 if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
1436 None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
1438 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1440 XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
1442 case ConfigureRequest:
1445 handler[ev.type](&ev);
1448 if ((ev.xmotion.time - lasttime) <= (1000 / 60))
1450 lasttime = ev.xmotion.time;
1452 nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
1453 nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
1454 if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
1455 && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
1457 if(!c->isfloating && selmon->lt[selmon->sellt]->arrange
1458 && (abs(nw - c->w) > snap || abs(nh - c->h) > snap))
1459 togglefloating(NULL);
1461 if(!selmon->lt[selmon->sellt]->arrange || c->isfloating)
1462 resize(c, c->x, c->y, nw, nh, True, 0);
1465 } while(ev.type != ButtonRelease);
1466 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
1467 XUngrabPointer(dpy, CurrentTime);
1468 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1469 if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
1477 restack(Monitor *m) {
1485 if(m->sel->isfloating || !m->lt[m->sellt]->arrange)
1486 XRaiseWindow(dpy, m->sel->win);
1487 if(m->lt[m->sellt]->arrange) {
1488 wc.stack_mode = Below;
1489 wc.sibling = m->barwin;
1490 for(c = m->clients; c; c = c->next)
1491 if(!c->isfloating && ISVISIBLE(c)) {
1492 XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
1493 wc.sibling = c->win;
1494 wc.stack_mode = Above;
1498 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
1504 /* main event loop */
1506 while(running && !XNextEvent(dpy, &ev)) {
1507 if(handler[ev.type]) {
1508 // fprintf(stderr, "handling event type %i\n", ev.type);
1509 handler[ev.type](&ev); /* call handler */
1511 // fprintf(stderr, "evt type %i\n", ev.type);
1518 unsigned int i, num;
1519 Window d1, d2, *wins = NULL;
1520 XWindowAttributes wa;
1522 if(XQueryTree(dpy, root, &d1, &d2, &wins, &num)) {
1523 for(i = 0; i < num; i++) {
1524 if(!XGetWindowAttributes(dpy, wins[i], &wa)
1525 || wa.override_redirect || XGetTransientForHint(dpy, wins[i], &d1))
1527 if(wa.map_state == IsViewable || getstate(wins[i]) == IconicState)
1528 manage(wins[i], &wa);
1530 for(i = 0; i < num; i++) { /* now the transients */
1531 if(!XGetWindowAttributes(dpy, wins[i], &wa))
1533 if(XGetTransientForHint(dpy, wins[i], &d1)
1534 && (wa.map_state == IsViewable || getstate(wins[i]) == IconicState))
1535 manage(wins[i], &wa);
1543 sendmon(Client *c, Monitor *m) {
1550 c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
1558 setclientstate(Client *c, long state) {
1559 long data[] = { state, None };
1561 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
1562 PropModeReplace, (unsigned char *)data, 2);
1566 sendevent(Client *c, Atom proto) {
1569 Bool exists = False;
1572 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
1573 while(!exists && n--)
1574 exists = protocols[n] == proto;
1578 ev.type = ClientMessage;
1579 ev.xclient.window = c->win;
1580 ev.xclient.message_type = wmatom[WMProtocols];
1581 ev.xclient.format = 32;
1582 ev.xclient.data.l[0] = proto;
1583 ev.xclient.data.l[1] = CurrentTime;
1584 XSendEvent(dpy, c->win, False, NoEventMask, &ev);
1590 setfocus(Client *c) {
1591 if(!c->neverfocus) {
1592 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
1593 XChangeProperty(dpy, root, netatom[NetActiveWindow],
1594 XA_WINDOW, 32, PropModeReplace,
1595 (unsigned char *) &(c->win), 1);
1597 sendevent(c, wmatom[WMTakeFocus]);
1601 setfullscreen(Client *c, Bool fullscreen) {
1603 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1604 PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
1605 c->isfullscreen = True;
1606 c->oldstate = c->isfloating;
1609 c->isfloating = True;
1610 resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh, 0);
1611 XRaiseWindow(dpy, c->win);
1614 XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
1615 PropModeReplace, (unsigned char*)0, 0);
1616 c->isfullscreen = False;
1617 c->isfloating = c->oldstate;
1623 resizeclient(c, c->x, c->y, c->w, c->h, 0);
1629 setlayout(const Arg *arg) {
1630 if(!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
1633 selmon->lt[selmon->sellt] = (Layout *)arg->v;
1634 strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
1641 /* arg > 1.0 will set mfact absolutly */
1643 setmfact(const Arg *arg) {
1646 if(!arg || !selmon->lt[selmon->sellt]->arrange)
1648 f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
1649 if(f < 0.1 || f > 0.9)
1657 XSetWindowAttributes wa;
1659 /* clean up any zombies immediately */
1663 screen = DefaultScreen(dpy);
1664 root = RootWindow(dpy, screen);
1665 fnt = drw_font_create(dpy, font);
1666 sw = DisplayWidth(dpy, screen);
1667 sh = DisplayHeight(dpy, screen);
1669 drw = drw_create(dpy, screen, root, sw, sh);
1670 drw_setfont(drw, fnt);
1673 wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
1674 wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
1675 wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
1676 wmatom[WMTakeFocus] = XInternAtom(dpy, "WM_TAKE_FOCUS", False);
1677 netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
1678 netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
1679 netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
1680 netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
1681 netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
1682 netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
1683 netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
1684 netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
1685 netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
1686 netatom[NetSupportingWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
1687 XInternAtom(dpy, "_MOTIF_WM_HINTS", False); /* clients may request borderless/fullscreen */
1689 cursor[CurNormal] = drw_cur_create(drw, XC_left_ptr);
1690 cursor[CurResize] = drw_cur_create(drw, XC_sizing);
1691 cursor[CurMove] = drw_cur_create(drw, XC_fleur);
1692 /* init appearance */
1693 scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
1694 scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
1695 scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
1696 scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
1697 scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
1698 scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
1702 /* EWMH support per view */
1703 XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
1704 PropModeReplace, (unsigned char *) netatom, NetLast);
1705 XDeleteProperty(dpy, root, netatom[NetClientList]);
1706 /* select for events */
1707 wa.cursor = cursor[CurNormal]->cursor;
1708 wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
1709 |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
1710 XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
1711 XSelectInput(dpy, root, wa.event_mask);
1717 showhide(Client *c) {
1720 if(ISVISIBLE(c)) { /* show clients top down */
1721 XMoveWindow(dpy, c->win, c->x, c->y);
1722 if((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
1723 resize(c, c->x, c->y, c->w, c->h, False, 0);
1726 else { /* hide clients bottom up */
1728 XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y);
1733 sigchld(int unused) {
1734 if(signal(SIGCHLD, sigchld) == SIG_ERR)
1735 die("Can't install SIGCHLD handler");
1736 while(0 < waitpid(-1, NULL, WNOHANG));
1740 kbspawn(const Arg *arg) {
1741 key_buffering = True;
1746 spawn(const Arg *arg) {
1748 if(arg->v == termcmd || arg->v == belltermcmd || arg->v == runcmd) {
1749 for(i = 0; i < 32; ++i) {
1750 if(selmon->tagset[selmon->seltags] & (1 << i)) {
1755 WORKSPACE_NUMBER[17] = workspace_numbers_str[tag][0];
1756 WORKSPACE_NUMBER[18] = workspace_numbers_str[tag][1]; // 2nd digit or null
1758 if(arg->v == dmenucmd)
1759 dmenumon[0] = '0' + selmon->num;
1762 close(ConnectionNumber(dpy));
1764 execvp(((char **)arg->v)[0], (char **)arg->v);
1765 fprintf(stderr, "dwm: execvp %s", ((char **)arg->v)[0]);
1772 tag(const Arg *arg) {
1773 if(selmon->sel && arg->ui & TAGMASK) {
1774 selmon->sel->tags = arg->ui & TAGMASK;
1781 tagmon(const Arg *arg) {
1782 if(!selmon->sel || !mons->next)
1784 sendmon(selmon->sel, dirtomon(arg->i));
1789 unsigned int i, n, h, mw, my, ty;
1792 for(n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
1797 mw = m->nmaster ? m->ww * m->mfact : 0;
1799 c = nexttiled(m->clients);
1800 if (c && !c->screen_hog)
1801 mw = m->ww * m->mfact;
1805 for(i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
1806 if(i < m->nmaster) {
1807 h = (m->wh - my) / (MIN(n, m->nmaster) - i);
1808 resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), False, 0);
1812 h = (m->wh - ty) / (n - i);
1813 resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), False, 0);
1818 // search forward from start, return the last client before end
1820 prev_tiled (Client *start, Client *end) {
1822 for (w = start; w && (n = nexttiled(w->next)); w = n) {
1830 // return the window to show on the right side
1832 choose_slave (Client *master, Client *focused) {
1837 // FIXME put this same algorithm in update_window_opacities
1838 if (focused->isfloating) {
1839 // show the window just "under" it
1840 Client *prev = prev_tiled(master, focused);
1841 // fall back to the first slave
1842 if (prev && prev != master) {
1845 return nexttiled(master->next);
1848 // focused window is tiled
1849 if (focused == master) {
1850 // master is focused, show first slave
1851 return nexttiled(master->next);
1853 // focused window is a slave, so show that one
1858 // maybe we get called when there's no focused?
1859 return nexttiled(master->next);
1865 jason_layout(Monitor *m) {
1866 Client *c, *master, *slave, *focused, *base = 0;
1867 unsigned int master_width, slave_width, slave_left;
1870 master = nexttiled(m->clients);
1872 // no master? nothing to do
1877 // find focused and slave
1878 focused = (m->sel && ISVISIBLE(m->sel)) ? m->sel : NULL;
1879 slave = choose_slave(master, focused);
1881 // calculate window widths
1882 if (!slave && master->screen_hog) {
1883 master_width = m->ww;
1885 master_width = m->ww * m->mfact - GUTTER_PX / 2;
1887 slave_left = m->ww * m->mfact + GUTTER_PX / 2;
1888 slave_width = m->ww * (1 - m->mfact) - GUTTER_PX / 2;
1890 // resize/reposition master
1899 // resize/reposition slaves
1900 base = master; // window to be above in the stacking order
1901 for (c = nexttiled(master->next); c; c = nexttiled(c->next)) {
1904 c == slave ? m->wy : m->wy - m->wh,
1915 togglebar(const Arg *arg) {
1916 selmon->showbar = !selmon->showbar;
1917 updatebarpos(selmon);
1918 XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
1923 togglefloating(const Arg *arg) {
1926 if(selmon->sel->isfullscreen) /* no support for fullscreen windows */
1928 selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
1929 if(selmon->sel->isfloating)
1930 resize(selmon->sel, selmon->sel->x, selmon->sel->y,
1931 selmon->sel->w, selmon->sel->h, False, 0);
1936 toggletag(const Arg *arg) {
1937 unsigned int newtags;
1941 newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
1943 selmon->sel->tags = newtags;
1950 toggleview(const Arg *arg) {
1951 unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
1954 selmon->tagset[selmon->seltags] = newtagset;
1961 unfocus(Client *c, Bool setfocus) {
1964 grabbuttons(c, False);
1965 XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
1967 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
1968 XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
1973 unmanage(Client *c, Bool destroyed) {
1974 Monitor *m = c->mon;
1977 /* The server grab construct avoids race conditions. */
1981 wc.border_width = c->oldbw;
1983 XSetErrorHandler(xerrordummy);
1984 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
1985 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
1986 setclientstate(c, WithdrawnState);
1988 XSetErrorHandler(xerror);
1992 focus(selmon ? selmon->sel : NULL);
1998 unmapnotify(XEvent *e) {
2000 XUnmapEvent *ev = &e->xunmap;
2002 if((c = wintoclient(ev->window))) {
2004 setclientstate(c, WithdrawnState);
2013 XSetWindowAttributes wa = {
2014 .override_redirect = True,
2015 .background_pixmap = ParentRelative,
2016 .event_mask = ButtonPressMask|ExposureMask
2018 for(m = mons; m; m = m->next) {
2021 m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
2022 CopyFromParent, DefaultVisual(dpy, screen),
2023 CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
2024 XChangeProperty(dpy, root, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2025 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2026 XChangeProperty(dpy, m->barwin, netatom[NetSupportingWMCheck], XA_WINDOW, 32,
2027 PropModeReplace, (unsigned char *) &(m->barwin), 1);
2028 XChangeProperty(dpy, m->barwin, netatom[NetWMName], XA_STRING, 8,
2029 PropModeReplace, (unsigned char *) "dwm", 3);
2030 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
2031 XMapRaised(dpy, m->barwin);
2036 updatebarpos(Monitor *m) {
2041 m->by = m->topbar ? m->wy : m->wy + m->wh;
2042 m->wy = m->topbar ? m->wy + bh : m->wy;
2049 updateclientlist() {
2053 XDeleteProperty(dpy, root, netatom[NetClientList]);
2054 for(m = mons; m; m = m->next)
2055 for(c = m->clients; c; c = c->next)
2056 XChangeProperty(dpy, root, netatom[NetClientList],
2057 XA_WINDOW, 32, PropModeAppend,
2058 (unsigned char *) &(c->win), 1);
2066 if(XineramaIsActive(dpy)) {
2070 XineramaScreenInfo *info = XineramaQueryScreens(dpy, &nn);
2071 XineramaScreenInfo *unique = NULL;
2073 for(n = 0, m = mons; m; m = m->next, n++);
2074 /* only consider unique geometries as separate screens */
2075 if(!(unique = (XineramaScreenInfo *)malloc(sizeof(XineramaScreenInfo) * nn)))
2076 die("fatal: could not malloc() %u bytes\n", sizeof(XineramaScreenInfo) * nn);
2077 for(i = 0, j = 0; i < nn; i++)
2078 if(isuniquegeom(unique, j, &info[i]))
2079 memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
2083 for(i = 0; i < (nn - n); i++) { /* new monitors available */
2084 for(m = mons; m && m->next; m = m->next);
2086 m->next = createmon();
2090 for(i = 0, m = mons; i < nn && m; m = m->next, i++)
2092 || (unique[i].x_org != m->mx || unique[i].y_org != m->my
2093 || unique[i].width != m->mw || unique[i].height != m->mh))
2097 m->mx = m->wx = unique[i].x_org;
2098 m->my = m->wy = unique[i].y_org;
2099 m->mw = m->ww = unique[i].width;
2100 m->mh = m->wh = unique[i].height;
2104 else { /* less monitors available nn < n */
2105 for(i = nn; i < n; i++) {
2106 for(m = mons; m && m->next; m = m->next);
2110 m->clients = c->next;
2124 #endif /* XINERAMA */
2125 /* default monitor setup */
2129 if(mons->mw != sw || mons->mh != sh) {
2131 mons->mw = mons->ww = sw;
2132 mons->mh = mons->wh = sh;
2138 selmon = wintomon(root);
2144 updatenumlockmask(void) {
2146 XModifierKeymap *modmap;
2149 modmap = XGetModifierMapping(dpy);
2150 for(i = 0; i < 8; i++)
2151 for(j = 0; j < modmap->max_keypermod; j++)
2152 if(modmap->modifiermap[i * modmap->max_keypermod + j]
2153 == XKeysymToKeycode(dpy, XK_Num_Lock))
2154 numlockmask = (1 << i);
2155 XFreeModifiermap(modmap);
2159 updatesizehints(Client *c) {
2163 if(!XGetWMNormalHints(dpy, c->win, &size, &msize))
2164 /* size is uninitialized, ensure that size.flags aren't used */
2166 if(size.flags & PBaseSize) {
2167 c->basew = size.base_width;
2168 c->baseh = size.base_height;
2170 else if(size.flags & PMinSize) {
2171 c->basew = size.min_width;
2172 c->baseh = size.min_height;
2175 c->basew = c->baseh = 0;
2176 if(size.flags & PResizeInc) {
2177 c->incw = size.width_inc;
2178 c->inch = size.height_inc;
2181 c->incw = c->inch = 0;
2182 if(size.flags & PMaxSize) {
2183 c->maxw = size.max_width;
2184 c->maxh = size.max_height;
2187 c->maxw = c->maxh = 0;
2188 if(size.flags & PMinSize) {
2189 c->minw = size.min_width;
2190 c->minh = size.min_height;
2192 else if(size.flags & PBaseSize) {
2193 c->minw = size.base_width;
2194 c->minh = size.base_height;
2197 c->minw = c->minh = 0;
2198 if(size.flags & PAspect) {
2199 c->mina = (float)size.min_aspect.y / size.min_aspect.x;
2200 c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
2203 c->maxa = c->mina = 0.0;
2204 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
2205 && c->maxw == c->minw && c->maxh == c->minh);
2209 updatetitle(Client *c) {
2210 if(!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name))
2211 gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name);
2212 if(c->name[0] == '\0') /* hack to mark broken clients */
2213 strcpy(c->name, broken);
2217 updatestatus(void) {
2218 if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
2219 strcpy(stext, "dwm-"VERSION);
2224 updatewindowtype(Client *c) {
2225 Atom state = getatomprop(c, netatom[NetWMState]);
2226 Atom wtype = getatomprop(c, netatom[NetWMWindowType]);
2228 if(state == netatom[NetWMFullscreen])
2229 setfullscreen(c, True);
2230 if(wtype == netatom[NetWMWindowTypeDialog])
2231 c->isfloating = True;
2235 updatewmhints(Client *c) {
2238 if((wmh = XGetWMHints(dpy, c->win))) {
2239 if(c == selmon->sel && wmh->flags & XUrgencyHint) {
2240 wmh->flags &= ~XUrgencyHint;
2241 XSetWMHints(dpy, c->win, wmh);
2244 c->isurgent = (wmh->flags & XUrgencyHint) ? True : False;
2245 if(wmh->flags & InputHint)
2246 c->neverfocus = !wmh->input;
2248 c->neverfocus = False;
2254 view(const Arg *arg) {
2255 if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
2257 selmon->seltags ^= 1; /* toggle sel tagset */
2258 if(arg->ui & TAGMASK)
2259 selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
2265 wintoclient(Window w) {
2269 for(m = mons; m; m = m->next)
2270 for(c = m->clients; c; c = c->next)
2277 wintomon(Window w) {
2282 if(w == root && getrootptr(&x, &y))
2283 return recttomon(x, y, 1, 1);
2284 for(m = mons; m; m = m->next)
2287 if((c = wintoclient(w)))
2292 /* There's no way to check accesses to destroyed windows, thus those cases are
2293 * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
2294 * default error handler, which may call exit. */
2296 xerror(Display *dpy, XErrorEvent *ee) {
2297 if(ee->error_code == BadWindow
2298 || (ee->request_code == X_SetInputFocus && ee->error_code == BadMatch)
2299 || (ee->request_code == X_PolyText8 && ee->error_code == BadDrawable)
2300 || (ee->request_code == X_PolyFillRectangle && ee->error_code == BadDrawable)
2301 || (ee->request_code == X_PolySegment && ee->error_code == BadDrawable)
2302 || (ee->request_code == X_ConfigureWindow && ee->error_code == BadMatch)
2303 || (ee->request_code == X_GrabButton && ee->error_code == BadAccess)
2304 || (ee->request_code == X_GrabKey && ee->error_code == BadAccess)
2305 || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
2307 fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
2308 ee->request_code, ee->error_code);
2309 return xerrorxlib(dpy, ee); /* may call exit */
2313 xerrordummy(Display *dpy, XErrorEvent *ee) {
2317 /* Startup Error handler to check if another window manager
2318 * is already running. */
2320 xerrorstart(Display *dpy, XErrorEvent *ee) {
2321 die("dwm: another window manager is already running\n");
2326 zoom(const Arg *arg) {
2327 Client *c = selmon->sel;
2329 if(!selmon->lt[selmon->sellt]->arrange
2330 || (selmon->sel && selmon->sel->isfloating))
2332 if(c == nexttiled(selmon->clients))
2333 if(!c || !(c = nexttiled(c->next)))
2339 main(int argc, char *argv[]) {
2340 if(argc == 2 && !strcmp("-v", argv[1]))
2341 die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
2343 die("usage: dwm [-v]\n");
2344 if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())
2345 fputs("warning: no locale support\n", stderr);
2346 if(!(dpy = XOpenDisplay(NULL)))
2347 die("dwm: cannot open display\n");
2354 return EXIT_SUCCESS;