]> git.armaanb.net Git - dwm.git/blobdiff - dwm.c
Update
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index dbff59b10554ec6b4a91a90482d644cf35e90900..2a8696b1b6c468a6abde03bebc49d370105ae99f 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -3,7 +3,7 @@
  * dynamic window manager is designed like any other X client as well. It is
  * driven through handling X events. In contrast to other X clients, a window
  * manager selects for SubstructureRedirectMask on the root window, to receive
- * events about window (dis-)appearance.  Only one X connection at a time is
+ * events about window (dis-)appearance. Only one X connection at a time is
  * allowed to select for this event mask.
  *
  * The event handlers of dwm are organized in an array which is accessed
@@ -11,7 +11,7 @@
  * in O(1) time.
  *
  * Each child of the root window is called a client, except windows which have
- * set the override_redirect flag.  Clients are organized in a linked client
+ * set the override_redirect flag. Clients are organized in a linked client
  * list on each monitor, the focus history is remembered through a stack list
  * on each monitor. Each client contains a bit array to indicate the tags of a
  * client.
@@ -40,6 +40,7 @@
 #include <X11/extensions/Xinerama.h>
 #endif /* XINERAMA */
 #include <X11/Xft/Xft.h>
+#include <X11/XF86keysym.h>
 
 #include "drw.h"
 #include "util.h"
 #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
 #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
 #define TEXTW(X)                (drw_fontset_getwidth(drw, (X)) + lrpad)
-#define ColBorder               2
 
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
-enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
-enum { NetSupported, NetWMName, NetWMState,
+enum { SchemeNorm, SchemeSel }; /* color schemes */
+enum { NetSupported, NetWMName, NetWMState, NetWMCheck,
        NetWMFullscreen, NetActiveWindow, NetWMWindowType,
        NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
@@ -112,6 +112,7 @@ typedef struct {
        void (*arrange)(Monitor *);
 } Layout;
 
+typedef struct Pertag Pertag;
 struct Monitor {
        char ltsymbol[16];
        float mfact;
@@ -131,6 +132,7 @@ struct Monitor {
        Monitor *next;
        Window barwin;
        const Layout *lt[2];
+       Pertag *pertag;
 };
 
 typedef struct {
@@ -148,12 +150,12 @@ static int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interac
 static void arrange(Monitor *m);
 static void arrangemon(Monitor *m);
 static void attach(Client *c);
+static void attachbottom(Client *c);
 static void attachstack(Client *c);
 static void buttonpress(XEvent *e);
 static void checkotherwm(void);
 static void cleanup(void);
 static void cleanupmon(Monitor *mon);
-static void clearurgent(Client *c);
 static void clientmessage(XEvent *e);
 static void configure(Client *c);
 static void configurenotify(XEvent *e);
@@ -167,10 +169,12 @@ static void drawbar(Monitor *m);
 static void drawbars(void);
 static void enternotify(XEvent *e);
 static void expose(XEvent *e);
+static Client *findbefore(Client *c);
 static void focus(Client *c);
 static void focusin(XEvent *e);
 static void focusmon(const Arg *arg);
 static void focusstack(const Arg *arg);
+static Atom getatomprop(Client *c, Atom prop);
 static int getrootptr(int *x, int *y);
 static long getstate(Window w);
 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
@@ -186,8 +190,10 @@ static void monocle(Monitor *m);
 static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static Client *nexttiled(Client *c);
-static void pop(Client *);
+static Client *prevtiled(Client *c);
 static void propertynotify(XEvent *e);
+static void pushdown(const Arg *arg);
+static void pushup(const Arg *arg);
 static void quit(const Arg *arg);
 static Monitor *recttomon(int x, int y, int w, int h);
 static void resize(Client *c, int x, int y, int w, int h, int interact);
@@ -204,6 +210,7 @@ static void setfullscreen(Client *c, int fullscreen);
 static void setlayout(const Arg *arg);
 static void setmfact(const Arg *arg);
 static void setup(void);
+static void seturgent(Client *c, int urg);
 static void showhide(Client *c);
 static void sigchld(int unused);
 static void spawn(const Arg *arg);
@@ -217,15 +224,15 @@ static void toggleview(const Arg *arg);
 static void unfocus(Client *c, int setfocus);
 static void unmanage(Client *c, int destroyed);
 static void unmapnotify(XEvent *e);
-static int updategeom(void);
 static void updatebarpos(Monitor *m);
 static void updatebars(void);
 static void updateclientlist(void);
+static int updategeom(void);
 static void updatenumlockmask(void);
 static void updatesizehints(Client *c);
 static void updatestatus(void);
-static void updatewindowtype(Client *c);
 static void updatetitle(Client *c);
+static void updatewindowtype(Client *c);
 static void updatewmhints(Client *c);
 static void view(const Arg *arg);
 static Client *wintoclient(Window w);
@@ -236,6 +243,7 @@ static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
 
 /* variables */
+static Client *prevzoom = NULL;
 static const char broken[] = "broken";
 static char stext[256];
 static int screen;
@@ -263,18 +271,55 @@ static void (*handler[LASTEvent]) (XEvent *) = {
 static Atom wmatom[WMLast], netatom[NetLast];
 static int running = 1;
 static Cur *cursor[CurLast];
-static Scm scheme[SchemeLast];
+static Clr **scheme;
 static Display *dpy;
 static Drw *drw;
 static Monitor *mons, *selmon;
-static Window root;
+static Window root, wmcheckwin;
 
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
+struct Pertag {
+       unsigned int curtag, prevtag; /* current and previous tag */
+       int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
+       float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
+       unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
+       const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes  */
+       int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
+};
+
 /* compile-time check if all tags fit into an unsigned int bit array. */
 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
 
+/* dwm will keep pid's of processes from autostart array and kill them at quit */
+static pid_t *autostart_pids;
+static size_t autostart_len;
+
+/* execute command from autostart array */
+static void
+autostart_exec() {
+       const char *const *p;
+       size_t i = 0;
+
+       /* count entries */
+       for (p = autostart; *p; autostart_len++, p++)
+               while (*++p);
+
+       autostart_pids = malloc(autostart_len * sizeof(pid_t));
+       for (p = autostart; *p; i++, p++) {
+               if ((autostart_pids[i] = fork()) == 0) {
+                       setsid();
+                       execvp(*p, (char *const *)p);
+                       fprintf(stderr, "dwm: execvp %s\n", *p);
+                       perror(" failed");
+                       _exit(EXIT_FAILURE);
+               }
+               /* skip arguments */
+               while (*++p);
+       }
+}
+
 /* function implementations */
 void
 applyrules(Client *c)
@@ -407,6 +452,15 @@ attach(Client *c)
        c->mon->clients = c;
 }
 
+void
+attachbottom(Client *c)
+{
+       Client **tc;
+       c->next = NULL;
+       for (tc = &c->mon->clients; *tc; tc = &(*tc)->next);
+       *tc = c;
+}
+
 void
 attachstack(Client *c)
 {
@@ -440,12 +494,14 @@ buttonpress(XEvent *e)
                        arg.ui = 1 << i;
                } else if (ev->x < x + blw)
                        click = ClkLtSymbol;
-               else if (ev->x > selmon->ww - TEXTW(stext))
+               else if (ev->x > selmon->ww - (int)TEXTW(stext))
                        click = ClkStatusText;
                else
                        click = ClkWinTitle;
        } else if ((c = wintoclient(ev->window))) {
                focus(c);
+               restack(selmon);
+               XAllowEvents(dpy, ReplayPointer, CurrentTime);
                click = ClkClientWin;
        }
        for (i = 0; i < LENGTH(buttons); i++)
@@ -483,8 +539,9 @@ cleanup(void)
                cleanupmon(mons);
        for (i = 0; i < CurLast; i++)
                drw_cur_free(drw, cursor[i]);
-       for (i = 0; i < SchemeLast; i++)
+       for (i = 0; i < LENGTH(colors); i++)
                free(scheme[i]);
+       XDestroyWindow(dpy, wmcheckwin);
        drw_free(drw);
        XSync(dpy, False);
        XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
@@ -507,19 +564,6 @@ cleanupmon(Monitor *mon)
        free(mon);
 }
 
-void
-clearurgent(Client *c)
-{
-       XWMHints *wmh;
-
-       c->isurgent = 0;
-       if (!(wmh = XGetWMHints(dpy, c->win)))
-               return;
-       wmh->flags &= ~XUrgencyHint;
-       XSetWMHints(dpy, c->win, wmh);
-       XFree(wmh);
-}
-
 void
 clientmessage(XEvent *e)
 {
@@ -529,15 +573,13 @@ clientmessage(XEvent *e)
        if (!c)
                return;
        if (cme->message_type == netatom[NetWMState]) {
-               if (cme->data.l[1] == netatom[NetWMFullscreen] || cme->data.l[2] == netatom[NetWMFullscreen])
+               if (cme->data.l[1] == netatom[NetWMFullscreen]
+               || cme->data.l[2] == netatom[NetWMFullscreen])
                        setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD    */
-                                     || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
+                               || cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */));
        } else if (cme->message_type == netatom[NetActiveWindow]) {
-               if (!ISVISIBLE(c)) {
-                       c->mon->seltags ^= 1;
-                       c->mon->tagset[c->mon->seltags] = c->tags;
-               }
-               pop(c);
+               if (c != selmon->sel && !c->isurgent)
+                       seturgent(c, 1);
        }
 }
 
@@ -564,7 +606,6 @@ void
 configurenotify(XEvent *e)
 {
        Monitor *m;
-       Client *c;
        XConfigureEvent *ev = &e->xconfigure;
        int dirty;
 
@@ -577,9 +618,6 @@ configurenotify(XEvent *e)
                        drw_resize(drw, sw, bh);
                        updatebars();
                        for (m = mons; m; m = m->next) {
-                               for (c = m->clients; c; c = c->next)
-                                       if (c->isfullscreen)
-                                               resizeclient(c, m->mx, m->my, m->mw, m->mh);
                                XMoveResizeWindow(dpy, m->barwin, m->wx, m->by, m->ww, bh);
                        }
                        focus(NULL);
@@ -644,6 +682,7 @@ Monitor *
 createmon(void)
 {
        Monitor *m;
+       unsigned int i;
 
        m = ecalloc(1, sizeof(Monitor));
        m->tagset[0] = m->tagset[1] = 1;
@@ -654,6 +693,20 @@ createmon(void)
        m->lt[0] = &layouts[0];
        m->lt[1] = &layouts[1 % LENGTH(layouts)];
        strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
+       m->pertag = ecalloc(1, sizeof(Pertag));
+       m->pertag->curtag = m->pertag->prevtag = 1;
+
+       for (i = 0; i <= LENGTH(tags); i++) {
+               m->pertag->nmasters[i] = m->nmaster;
+               m->pertag->mfacts[i] = m->mfact;
+
+               m->pertag->ltidxs[i][0] = m->lt[0];
+               m->pertag->ltidxs[i][1] = m->lt[1];
+               m->pertag->sellts[i] = m->sellt;
+
+               m->pertag->showbars[i] = m->showbar;
+       }
+
        return m;
 }
 
@@ -708,7 +761,7 @@ dirtomon(int dir)
 void
 drawbar(Monitor *m)
 {
-       int x, w, sw = 0;
+       int x, w, tw = 0;
        int boxs = drw->fonts->h / 9;
        int boxw = drw->fonts->h / 6 + 2;
        unsigned int i, occ = 0, urg = 0;
@@ -717,8 +770,8 @@ drawbar(Monitor *m)
        /* draw status first so it can be overdrawn by tags later */
        if (m == selmon) { /* status is only drawn on selected monitor */
                drw_setscheme(drw, scheme[SchemeNorm]);
-               sw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
-               drw_text(drw, m->ww - sw, 0, sw, bh, 0, stext, 0);
+               tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
+               drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
        }
 
        for (c = m->clients; c; c = c->next) {
@@ -733,17 +786,17 @@ drawbar(Monitor *m)
                drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
                if (occ & 1 << i)
                        drw_rect(drw, x + boxs, boxs, boxw, boxw,
-                                m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
-                                urg & 1 << i);
+                               m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
+                               urg & 1 << i);
                x += w;
        }
        w = blw = TEXTW(m->ltsymbol);
        drw_setscheme(drw, scheme[SchemeNorm]);
        x = drw_text(drw, x, 0, w, bh, lrpad / 2, m->ltsymbol, 0);
 
-       if ((w = m->ww - sw - x) > bh) {
+       if ((w = m->ww - tw - x) > bh) {
                if (m->sel) {
-                       drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
+                       drw_setscheme(drw, scheme[m == selmon ? SchemeNorm : SchemeSel]);
                        drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
                        if (m->sel->isfloating)
                                drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
@@ -793,19 +846,28 @@ expose(XEvent *e)
                drawbar(m);
 }
 
+Client *
+findbefore(Client *c)
+{
+       Client *tmp;
+       if (c == selmon->clients)
+               return NULL;
+       for (tmp = selmon->clients; tmp && tmp->next != c; tmp = tmp->next);
+       return tmp;
+}
+
 void
 focus(Client *c)
 {
        if (!c || !ISVISIBLE(c))
                for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
-       /* was if (selmon->sel) */
        if (selmon->sel && selmon->sel != c)
                unfocus(selmon->sel, 0);
        if (c) {
                if (c->mon != selmon)
                        selmon = c->mon;
                if (c->isurgent)
-                       clearurgent(c);
+                       seturgent(c, 0);
                detachstack(c);
                attachstack(c);
                grabbuttons(c, 1);
@@ -819,7 +881,7 @@ focus(Client *c)
        drawbars();
 }
 
-/* there are some broken focus acquiring clients */
+/* there are some broken focus acquiring clients needing extra handling */
 void
 focusin(XEvent *e)
 {
@@ -838,10 +900,11 @@ focusmon(const Arg *arg)
                return;
        if ((m = dirtomon(arg->i)) == selmon)
                return;
-       unfocus(selmon->sel, 0); /* s/1/0/ fixes input focus issues
-                                       in gedit and anjuta */
+       unfocus(selmon->sel, 0);
        selmon = m;
        focus(NULL);
+       if (selmon->sel)
+               XWarpPointer(dpy, None, selmon->sel->win, 0, 0, 0, 0, selmon->sel->w/2, selmon->sel->h/2);
 }
 
 void
@@ -867,6 +930,7 @@ focusstack(const Arg *arg)
        if (c) {
                focus(c);
                restack(selmon);
+               XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w/2, c->h/2);
        }
 }
 
@@ -879,7 +943,7 @@ getatomprop(Client *c, Atom prop)
        Atom da, atom = None;
 
        if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM,
-                             &da, &di, &dl, &dl, &p) == Success && p) {
+               &da, &di, &dl, &dl, &p) == Success && p) {
                atom = *(Atom *)p;
                XFree(p);
        }
@@ -906,7 +970,7 @@ getstate(Window w)
        Atom real;
 
        if (XGetWindowProperty(dpy, w, wmatom[WMState], 0L, 2L, False, wmatom[WMState],
-                             &real, &format, &n, &extra, (unsigned char **)&p) != Success)
+               &real, &format, &n, &extra, (unsigned char **)&p) != Success)
                return -1;
        if (n != 0)
                result = *p;
@@ -924,8 +988,7 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size)
        if (!text || size == 0)
                return 0;
        text[0] = '\0';
-       XGetTextProperty(dpy, w, &name, atom);
-       if (!name.nitems)
+       if (!XGetTextProperty(dpy, w, &name, atom) || !name.nitems)
                return 0;
        if (name.encoding == XA_STRING)
                strncpy(text, (char *)name.value, size - 1);
@@ -948,17 +1011,16 @@ grabbuttons(Client *c, int focused)
                unsigned int i, j;
                unsigned int modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask };
                XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
-               if (focused) {
-                       for (i = 0; i < LENGTH(buttons); i++)
-                               if (buttons[i].click == ClkClientWin)
-                                       for (j = 0; j < LENGTH(modifiers); j++)
-                                               XGrabButton(dpy, buttons[i].button,
-                                                           buttons[i].mask | modifiers[j],
-                                                           c->win, False, BUTTONMASK,
-                                                           GrabModeAsync, GrabModeSync, None, None);
-               } else
+               if (!focused)
                        XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
-                                   BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
+                               BUTTONMASK, GrabModeSync, GrabModeSync, None, None);
+               for (i = 0; i < LENGTH(buttons); i++)
+                       if (buttons[i].click == ClkClientWin)
+                               for (j = 0; j < LENGTH(modifiers); j++)
+                                       XGrabButton(dpy, buttons[i].button,
+                                               buttons[i].mask | modifiers[j],
+                                               c->win, False, BUTTONMASK,
+                                               GrabModeAsync, GrabModeSync, None, None);
        }
 }
 
@@ -976,14 +1038,14 @@ grabkeys(void)
                        if ((code = XKeysymToKeycode(dpy, keys[i].keysym)))
                                for (j = 0; j < LENGTH(modifiers); j++)
                                        XGrabKey(dpy, code, keys[i].mod | modifiers[j], root,
-                                                True, GrabModeAsync, GrabModeAsync);
+                                               True, GrabModeAsync, GrabModeAsync);
        }
 }
 
 void
 incnmaster(const Arg *arg)
 {
-       selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
+       selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
        arrange(selmon);
 }
 
@@ -1063,7 +1125,7 @@ manage(Window w, XWindowAttributes *wa)
        c->x = MAX(c->x, c->mon->mx);
        /* only fix client y-offset, if the client center might cover the bar */
        c->y = MAX(c->y, ((c->mon->by == c->mon->my) && (c->x + (c->w / 2) >= c->mon->wx)
-                  && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
+               && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : c->mon->my);
        c->bw = borderpx;
 
        wc.border_width = c->bw;
@@ -1079,10 +1141,10 @@ manage(Window w, XWindowAttributes *wa)
                c->isfloating = c->oldstate = trans != None || c->isfixed;
        if (c->isfloating)
                XRaiseWindow(dpy, c->win);
-       attach(c);
+       attachbottom(c);
        attachstack(c);
        XChangeProperty(dpy, root, netatom[NetClientList], XA_WINDOW, 32, PropModeAppend,
-                       (unsigned char *) &(c->win), 1);
+               (unsigned char *) &(c->win), 1);
        XMoveResizeWindow(dpy, c->win, c->x + 2 * sw, c->y, c->w, c->h); /* some windows require this */
        setclientstate(c, NormalState);
        if (c->mon == selmon)
@@ -1160,13 +1222,11 @@ movemouse(const Arg *arg)
 
        if (!(c = selmon->sel))
                return;
-       if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
-               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
        if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
-       None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
+               None, cursor[CurMove]->cursor, CurrentTime) != GrabSuccess)
                return;
        if (!getrootptr(&x, &y))
                return;
@@ -1185,20 +1245,17 @@ movemouse(const Arg *arg)
 
                        nx = ocx + (ev.xmotion.x - x);
                        ny = ocy + (ev.xmotion.y - y);
-                       if (nx >= selmon->wx && nx <= selmon->wx + selmon->ww
-                       && ny >= selmon->wy && ny <= selmon->wy + selmon->wh) {
-                               if (abs(selmon->wx - nx) < snap)
-                                       nx = selmon->wx;
-                               else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
-                                       nx = selmon->wx + selmon->ww - WIDTH(c);
-                               if (abs(selmon->wy - ny) < snap)
-                                       ny = selmon->wy;
-                               else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
-                                       ny = selmon->wy + selmon->wh - HEIGHT(c);
-                               if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
-                               && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
-                                       togglefloating(NULL);
-                       }
+                       if (abs(selmon->wx - nx) < snap)
+                               nx = selmon->wx;
+                       else if (abs((selmon->wx + selmon->ww) - (nx + WIDTH(c))) < snap)
+                               nx = selmon->wx + selmon->ww - WIDTH(c);
+                       if (abs(selmon->wy - ny) < snap)
+                               ny = selmon->wy;
+                       else if (abs((selmon->wy + selmon->wh) - (ny + HEIGHT(c))) < snap)
+                               ny = selmon->wy + selmon->wh - HEIGHT(c);
+                       if (!c->isfloating && selmon->lt[selmon->sellt]->arrange
+                       && (abs(nx - c->x) > snap || abs(ny - c->y) > snap))
+                               togglefloating(NULL);
                        if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
                                resize(c, nx, ny, c->w, c->h, 1);
                        break;
@@ -1219,13 +1276,14 @@ nexttiled(Client *c)
        return c;
 }
 
-void
-pop(Client *c)
-{
-       detach(c);
-       attach(c);
-       focus(c);
-       arrange(c->mon);
+Client *
+prevtiled(Client *c) {
+       Client *p, *r;
+
+       for(p = selmon->clients, r = NULL; p && p != c; p = p->next)
+               if(!p->isfloating && ISVISIBLE(p))
+                       r = p;
+       return r;
 }
 
 void
@@ -1244,7 +1302,7 @@ propertynotify(XEvent *e)
                default: break;
                case XA_WM_TRANSIENT_FOR:
                        if (!c->isfloating && (XGetTransientForHint(dpy, c->win, &trans)) &&
-                          (c->isfloating = (wintoclient(trans)) != NULL))
+                               (c->isfloating = (wintoclient(trans)) != NULL))
                                arrange(c->mon);
                        break;
                case XA_WM_NORMAL_HINTS:
@@ -1265,9 +1323,62 @@ propertynotify(XEvent *e)
        }
 }
 
+void
+pushdown(const Arg *arg) {
+       Client *sel = selmon->sel, *c;
+
+       if(!sel || sel->isfloating)
+               return;
+       if((c = nexttiled(sel->next))) {
+               detach(sel);
+               sel->next = c->next;
+               c->next = sel;
+       } else {
+               detach(sel);
+               attach(sel);
+       }
+       focus(sel);
+       arrange(selmon);
+}
+
+void
+pushup(const Arg *arg) {
+       Client *sel = selmon->sel, *c;
+
+       if(!sel || sel->isfloating)
+               return;
+       if((c = prevtiled(sel))) {
+               detach(sel);
+               sel->next = c;
+               if(selmon->clients == c)
+                       selmon->clients = sel;
+               else {
+                       for(c = selmon->clients; c->next != sel->next; c = c->next);
+                       c->next = sel;
+               }
+       } else {
+               for(c = sel; c->next; c = c->next);
+               detach(sel);
+               sel->next = NULL;
+               c->next = sel;
+       }
+       focus(sel);
+       arrange(selmon);
+}
+
 void
 quit(const Arg *arg)
 {
+       size_t i;
+
+       /* kill child processes */
+       for (i = 0; i < autostart_len; i++) {
+               if (0 < autostart_pids[i]) {
+                       kill(autostart_pids[i], SIGTERM);
+                       waitpid(autostart_pids[i], NULL, 0);
+               }
+       }
+
        running = 0;
 }
 
@@ -1311,22 +1422,33 @@ void
 resizemouse(const Arg *arg)
 {
        int ocx, ocy, nw, nh;
+       int ocx2, ocy2, nx, ny;
        Client *c;
        Monitor *m;
        XEvent ev;
+       int horizcorner, vertcorner;
+       int di;
+       unsigned int dui;
+       Window dummy;
        Time lasttime = 0;
 
        if (!(c = selmon->sel))
                return;
-       if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
-               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
+       ocx2 = c->x + c->w;
+       ocy2 = c->y + c->h;
        if (XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
-                       None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
+               None, cursor[CurResize]->cursor, CurrentTime) != GrabSuccess)
                return;
-       XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
+       if (!XQueryPointer (dpy, c->win, &dummy, &dummy, &di, &di, &nx, &ny, &dui))
+               return;
+       horizcorner = nx < c->w / 2;
+       vertcorner  = ny < c->h / 2;
+       XWarpPointer (dpy, None, c->win, 0, 0, 0, 0,
+                       horizcorner ? (-c->bw) : (c->w + c->bw -1),
+                       vertcorner  ? (-c->bw) : (c->h + c->bw -1));
        do {
                XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
                switch(ev.type) {
@@ -1342,6 +1464,11 @@ resizemouse(const Arg *arg)
 
                        nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
                        nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
+                       nx = horizcorner ? ev.xmotion.x : c->x;
+                       ny = vertcorner ? ev.xmotion.y : c->y;
+                       nw = MAX(horizcorner ? (ocx2 - nx) : (ev.xmotion.x - ocx - 2 * c->bw + 1), 1);
+                       nh = MAX(vertcorner ? (ocy2 - ny) : (ev.xmotion.y - ocy - 2 * c->bw + 1), 1);
+
                        if (c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
                        && c->mon->wy + nh >= selmon->wy && c->mon->wy + nh <= selmon->wy + selmon->wh)
                        {
@@ -1350,11 +1477,13 @@ resizemouse(const Arg *arg)
                                        togglefloating(NULL);
                        }
                        if (!selmon->lt[selmon->sellt]->arrange || c->isfloating)
-                               resize(c, c->x, c->y, nw, nh, 1);
+                               resize(c, nx, ny, nw, nh, 1);
                        break;
                }
        } while (ev.type != ButtonRelease);
-       XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
+       XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
+                     horizcorner ? (-c->bw) : (c->w + c->bw - 1),
+                     vertcorner ? (-c->bw) : (c->h + c->bw - 1));
        XUngrabPointer(dpy, CurrentTime);
        while (XCheckMaskEvent(dpy, EnterWindowMask, &ev));
        if ((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
@@ -1437,7 +1566,7 @@ sendmon(Client *c, Monitor *m)
        detachstack(c);
        c->mon = m;
        c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
-       attach(c);
+       attachbottom(c);
        attachstack(c);
        focus(NULL);
        arrange(NULL);
@@ -1449,7 +1578,7 @@ setclientstate(Client *c, long state)
        long data[] = { state, None };
 
        XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
-                       PropModeReplace, (unsigned char *)data, 2);
+               PropModeReplace, (unsigned char *)data, 2);
 }
 
 int
@@ -1483,8 +1612,8 @@ setfocus(Client *c)
        if (!c->neverfocus) {
                XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
                XChangeProperty(dpy, root, netatom[NetActiveWindow],
-                               XA_WINDOW, 32, PropModeReplace,
-                               (unsigned char *) &(c->win), 1);
+                       XA_WINDOW, 32, PropModeReplace,
+                       (unsigned char *) &(c->win), 1);
        }
        sendevent(c, wmatom[WMTakeFocus]);
 }
@@ -1494,26 +1623,12 @@ setfullscreen(Client *c, int fullscreen)
 {
        if (fullscreen && !c->isfullscreen) {
                XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
-                               PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
+                       PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
                c->isfullscreen = 1;
-               c->oldstate = c->isfloating;
-               c->oldbw = c->bw;
-               c->bw = 0;
-               c->isfloating = 1;
-               resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh);
-               XRaiseWindow(dpy, c->win);
        } else if (!fullscreen && c->isfullscreen){
                XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
-                               PropModeReplace, (unsigned char*)0, 0);
+                       PropModeReplace, (unsigned char*)0, 0);
                c->isfullscreen = 0;
-               c->isfloating = c->oldstate;
-               c->bw = c->oldbw;
-               c->x = c->oldx;
-               c->y = c->oldy;
-               c->w = c->oldw;
-               c->h = c->oldh;
-               resizeclient(c, c->x, c->y, c->w, c->h);
-               arrange(c->mon);
        }
 }
 
@@ -1521,9 +1636,9 @@ void
 setlayout(const Arg *arg)
 {
        if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
-               selmon->sellt ^= 1;
+               selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
        if (arg && arg->v)
-               selmon->lt[selmon->sellt] = (Layout *)arg->v;
+               selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
        strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
        if (selmon->sel)
                arrange(selmon);
@@ -1531,7 +1646,7 @@ setlayout(const Arg *arg)
                drawbar(selmon);
 }
 
-/* arg > 1.0 will set mfact absolutly */
+/* arg > 1.0 will set mfact absolutely */
 void
 setmfact(const Arg *arg)
 {
@@ -1540,16 +1655,18 @@ setmfact(const Arg *arg)
        if (!arg || !selmon->lt[selmon->sellt]->arrange)
                return;
        f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
-       if (f < 0.1 || f > 0.9)
+       if (f < 0.05 || f > 0.95)
                return;
-       selmon->mfact = f;
+       selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
        arrange(selmon);
 }
 
 void
 setup(void)
 {
+       int i;
        XSetWindowAttributes wa;
+       Atom utf8string;
 
        /* clean up any zombies immediately */
        sigchld(0);
@@ -1566,6 +1683,7 @@ setup(void)
        bh = drw->fonts->h + 2;
        updategeom();
        /* init atoms */
+       utf8string = XInternAtom(dpy, "UTF8_STRING", False);
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
        wmatom[WMDelete] = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
        wmatom[WMState] = XInternAtom(dpy, "WM_STATE", False);
@@ -1574,6 +1692,7 @@ setup(void)
        netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
        netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
        netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
+       netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
        netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
        netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
        netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
@@ -1583,25 +1702,49 @@ setup(void)
        cursor[CurResize] = drw_cur_create(drw, XC_sizing);
        cursor[CurMove] = drw_cur_create(drw, XC_fleur);
        /* init appearance */
-       scheme[SchemeNorm] = drw_scm_create(drw, colors[SchemeNorm], 3);
-       scheme[SchemeSel] = drw_scm_create(drw, colors[SchemeSel], 3);
+       scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
+       for (i = 0; i < LENGTH(colors); i++)
+               scheme[i] = drw_scm_create(drw, colors[i], 3);
        /* init bars */
        updatebars();
        updatestatus();
+       /* supporting window for NetWMCheck */
+       wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
+       XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
+               PropModeReplace, (unsigned char *) &wmcheckwin, 1);
+       XChangeProperty(dpy, wmcheckwin, netatom[NetWMName], utf8string, 8,
+               PropModeReplace, (unsigned char *) "dwm", 3);
+       XChangeProperty(dpy, root, netatom[NetWMCheck], XA_WINDOW, 32,
+               PropModeReplace, (unsigned char *) &wmcheckwin, 1);
        /* EWMH support per view */
        XChangeProperty(dpy, root, netatom[NetSupported], XA_ATOM, 32,
-                       PropModeReplace, (unsigned char *) netatom, NetLast);
+               PropModeReplace, (unsigned char *) netatom, NetLast);
        XDeleteProperty(dpy, root, netatom[NetClientList]);
-       /* select for events */
+       /* select events */
        wa.cursor = cursor[CurNormal]->cursor;
-       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
-                       |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
+       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask
+               |ButtonPressMask|PointerMotionMask|EnterWindowMask
+               |LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
        grabkeys();
        focus(NULL);
 }
 
+
+void
+seturgent(Client *c, int urg)
+{
+       XWMHints *wmh;
+
+       c->isurgent = urg;
+       if (!(wmh = XGetWMHints(dpy, c->win)))
+               return;
+       wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint);
+       XSetWMHints(dpy, c->win, wmh);
+       XFree(wmh);
+}
+
 void
 showhide(Client *c)
 {
@@ -1610,7 +1753,7 @@ showhide(Client *c)
        if (ISVISIBLE(c)) {
                /* show clients top down */
                XMoveWindow(dpy, c->win, c->x, c->y);
-               if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen)
+               if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
                        resize(c, c->x, c->y, c->w, c->h, 0);
                showhide(c->snext);
        } else {
@@ -1623,9 +1766,25 @@ showhide(Client *c)
 void
 sigchld(int unused)
 {
+       pid_t pid;
+
        if (signal(SIGCHLD, sigchld) == SIG_ERR)
                die("can't install SIGCHLD handler:");
-       while (0 < waitpid(-1, NULL, WNOHANG));
+       while (0 < (pid = waitpid(-1, NULL, WNOHANG))) {
+               pid_t *p, *lim;
+
+               if (!(p = autostart_pids))
+                       continue;
+               lim = &p[autostart_len];
+
+               for (; p < lim; p++) {
+                       if (*p == pid) {
+                               *p = -1;
+                               break;
+                       }
+               }
+
+       }
 }
 
 void
@@ -1680,18 +1839,20 @@ tile(Monitor *m)
                if (i < m->nmaster) {
                        h = (m->wh - my) / (MIN(n, m->nmaster) - i);
                        resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
-                       my += HEIGHT(c);
+                       if (my + HEIGHT(c) < m->wh)
+                               my += HEIGHT(c);
                } else {
                        h = (m->wh - ty) / (n - i);
                        resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
-                       ty += HEIGHT(c);
+                       if (ty + HEIGHT(c) < m->wh)
+                               ty += HEIGHT(c);
                }
 }
 
 void
 togglebar(const Arg *arg)
 {
-       selmon->showbar = !selmon->showbar;
+       selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
        updatebarpos(selmon);
        XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
        arrange(selmon);
@@ -1702,12 +1863,10 @@ togglefloating(const Arg *arg)
 {
        if (!selmon->sel)
                return;
-       if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
-               return;
        selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
        if (selmon->sel->isfloating)
                resize(selmon->sel, selmon->sel->x, selmon->sel->y,
-                      selmon->sel->w, selmon->sel->h, 0);
+                       selmon->sel->w, selmon->sel->h, 0);
        arrange(selmon);
 }
 
@@ -1730,9 +1889,33 @@ void
 toggleview(const Arg *arg)
 {
        unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
+       int i;
 
        if (newtagset) {
                selmon->tagset[selmon->seltags] = newtagset;
+
+               if (newtagset == ~0) {
+                       selmon->pertag->prevtag = selmon->pertag->curtag;
+                       selmon->pertag->curtag = 0;
+               }
+
+               /* test if the user did not select the same tag */
+               if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
+                       selmon->pertag->prevtag = selmon->pertag->curtag;
+                       for (i = 0; !(newtagset & 1 << i); i++) ;
+                       selmon->pertag->curtag = i + 1;
+               }
+
+               /* apply settings for this view */
+               selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
+               selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
+               selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
+               selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
+               selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
+
+               if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
+                       togglebar(NULL);
+
                focus(NULL);
                arrange(selmon);
        }
@@ -1757,12 +1940,11 @@ unmanage(Client *c, int destroyed)
        Monitor *m = c->mon;
        XWindowChanges wc;
 
-       /* The server grab construct avoids race conditions. */
        detach(c);
        detachstack(c);
        if (!destroyed) {
                wc.border_width = c->oldbw;
-               XGrabServer(dpy);
+               XGrabServer(dpy); /* avoid race conditions */
                XSetErrorHandler(xerrordummy);
                XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
                XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
@@ -1772,6 +1954,7 @@ unmanage(Client *c, int destroyed)
                XUngrabServer(dpy);
        }
        free(c);
+
        focus(NULL);
        updateclientlist();
        arrange(m);
@@ -1800,14 +1983,16 @@ updatebars(void)
                .background_pixmap = ParentRelative,
                .event_mask = ButtonPressMask|ExposureMask
        };
+       XClassHint ch = {"dwm", "dwm"};
        for (m = mons; m; m = m->next) {
                if (m->barwin)
                        continue;
                m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
-                                         CopyFromParent, DefaultVisual(dpy, screen),
-                                         CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
+                               CopyFromParent, DefaultVisual(dpy, screen),
+                               CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
                XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
                XMapRaised(dpy, m->barwin);
+               XSetClassHint(dpy, m->barwin, &ch);
        }
 }
 
@@ -1834,8 +2019,8 @@ updateclientlist()
        for (m = mons; m; m = m->next)
                for (c = m->clients; c; c = c->next)
                        XChangeProperty(dpy, root, netatom[NetClientList],
-                                       XA_WINDOW, 32, PropModeAppend,
-                                       (unsigned char *) &(c->win), 1);
+                               XA_WINDOW, 32, PropModeAppend,
+                               (unsigned char *) &(c->win), 1);
 }
 
 int
@@ -1859,8 +2044,8 @@ updategeom(void)
                                memcpy(&unique[j++], &info[i], sizeof(XineramaScreenInfo));
                XFree(info);
                nn = j;
-               if (n <= nn) {
-                       for (i = 0; i < (nn - n); i++) { /* new monitors available */
+               if (n <= nn) { /* new monitors available */
+                       for (i = 0; i < (nn - n); i++) {
                                for (m = mons; m && m->next; m = m->next);
                                if (m)
                                        m->next = createmon();
@@ -1869,8 +2054,8 @@ updategeom(void)
                        }
                        for (i = 0, m = mons; i < nn && m; m = m->next, i++)
                                if (i >= n
-                               || (unique[i].x_org != m->mx || unique[i].y_org != m->my
-                                   || unique[i].width != m->mw || unique[i].height != m->mh))
+                               || unique[i].x_org != m->mx || unique[i].y_org != m->my
+                               || unique[i].width != m->mw || unique[i].height != m->mh)
                                {
                                        dirty = 1;
                                        m->num = i;
@@ -1880,17 +2065,15 @@ updategeom(void)
                                        m->mh = m->wh = unique[i].height;
                                        updatebarpos(m);
                                }
-               } else {
-                       /* less monitors available nn < n */
+               } else { /* less monitors available nn < n */
                        for (i = nn; i < n; i++) {
                                for (m = mons; m && m->next; m = m->next);
-                               while (m->clients) {
+                               while ((c = m->clients)) {
                                        dirty = 1;
-                                       c = m->clients;
                                        m->clients = c->next;
                                        detachstack(c);
                                        c->mon = mons;
-                                       attach(c);
+                                       attachbottom(c);
                                        attachstack(c);
                                }
                                if (m == selmon)
@@ -1901,8 +2084,7 @@ updategeom(void)
                free(unique);
        } else
 #endif /* XINERAMA */
-       /* default monitor setup */
-       {
+       { /* default monitor setup */
                if (!mons)
                        mons = createmon();
                if (mons->mw != sw || mons->mh != sh) {
@@ -1930,7 +2112,7 @@ updatenumlockmask(void)
        for (i = 0; i < 8; i++)
                for (j = 0; j < modmap->max_keypermod; j++)
                        if (modmap->modifiermap[i * modmap->max_keypermod + j]
-                          == XKeysymToKeycode(dpy, XK_Num_Lock))
+                               == XKeysymToKeycode(dpy, XK_Num_Lock))
                                numlockmask = (1 << i);
        XFreeModifiermap(modmap);
 }
@@ -1975,8 +2157,15 @@ updatesizehints(Client *c)
                c->maxa = (float)size.max_aspect.x / size.max_aspect.y;
        } else
                c->maxa = c->mina = 0.0;
-       c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
-                    && c->maxw == c->minw && c->maxh == c->minh);
+       c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh);
+}
+
+void
+updatestatus(void)
+{
+       if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
+               strcpy(stext, "dwm-"VERSION);
+       drawbar(selmon);
 }
 
 void
@@ -1988,14 +2177,6 @@ updatetitle(Client *c)
                strcpy(c->name, broken);
 }
 
-void
-updatestatus(void)
-{
-       if (!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
-               strcpy(stext, "dwm-"VERSION);
-       drawbar(selmon);
-}
-
 void
 updatewindowtype(Client *c)
 {
@@ -2030,11 +2211,37 @@ updatewmhints(Client *c)
 void
 view(const Arg *arg)
 {
+       int i;
+       unsigned int tmptag;
+
        if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
                return;
        selmon->seltags ^= 1; /* toggle sel tagset */
-       if (arg->ui & TAGMASK)
+       if (arg->ui & TAGMASK) {
                selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
+               selmon->pertag->prevtag = selmon->pertag->curtag;
+
+               if (arg->ui == ~0)
+                       selmon->pertag->curtag = 0;
+               else {
+                       for (i = 0; !(arg->ui & 1 << i); i++) ;
+                       selmon->pertag->curtag = i + 1;
+               }
+       } else {
+               tmptag = selmon->pertag->prevtag;
+               selmon->pertag->prevtag = selmon->pertag->curtag;
+               selmon->pertag->curtag = tmptag;
+       }
+
+       selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
+       selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
+       selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
+       selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
+       selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
+
+       if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
+               togglebar(NULL);
+
        focus(NULL);
        arrange(selmon);
 }
@@ -2070,8 +2277,8 @@ wintomon(Window w)
 }
 
 /* There's no way to check accesses to destroyed windows, thus those cases are
- * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
- * default error handler, which may call exit.  */
+ * ignored (especially on UnmapNotify's). Other types of errors call Xlibs
+ * default error handler, which may call exit. */
 int
 xerror(Display *dpy, XErrorEvent *ee)
 {
@@ -2086,7 +2293,7 @@ xerror(Display *dpy, XErrorEvent *ee)
        || (ee->request_code == X_CopyArea && ee->error_code == BadDrawable))
                return 0;
        fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
-                       ee->request_code, ee->error_code);
+               ee->request_code, ee->error_code);
        return xerrorxlib(dpy, ee); /* may call exit */
 }
 
@@ -2109,14 +2316,38 @@ void
 zoom(const Arg *arg)
 {
        Client *c = selmon->sel;
+       Client *at = NULL, *cold, *cprevious = NULL;
 
        if (!selmon->lt[selmon->sellt]->arrange
        || (selmon->sel && selmon->sel->isfloating))
                return;
-       if (c == nexttiled(selmon->clients))
-               if (!c || !(c = nexttiled(c->next)))
-                       return;
-       pop(c);
+       if (c == nexttiled(selmon->clients)) {
+               at = findbefore(prevzoom);
+               if (at)
+                       cprevious = nexttiled(at->next);
+               if (!cprevious || cprevious != prevzoom) {
+                       prevzoom = NULL;
+                       if (!c || !(c = nexttiled(c->next)))
+                               return;
+               } else
+                       c = cprevious;
+       }
+       cold = nexttiled(selmon->clients);
+       if (c != cold && !at)
+               at = findbefore(c);
+       detach(c);
+       attach(c);
+       /* swap windows instead of pushing the previous one down */
+       if (c != cold && at) {
+               prevzoom = cold;
+               if (cold && at != cold) {
+                       detach(cold);
+                       cold->next = at->next;
+                       at->next = cold;
+               }
+       }
+       focus(c);
+       arrange(c->mon);
 }
 
 int
@@ -2131,7 +2362,12 @@ main(int argc, char *argv[])
        if (!(dpy = XOpenDisplay(NULL)))
                die("dwm: cannot open display");
        checkotherwm();
+       autostart_exec();
        setup();
+#ifdef __OpenBSD__
+       if (pledge("stdio rpath proc exec", NULL) == -1)
+               die("pledge");
+#endif /* __OpenBSD__ */
        scan();
        run();
        cleanup();