]> git.armaanb.net Git - dwm.git/blobdiff - dwm.c
Update
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index f2ab57c130000c0bb1f59135b879678fa840dd55..2a8696b1b6c468a6abde03bebc49d370105ae99f 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -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 {
@@ -278,9 +280,46 @@ 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)
@@ -643,6 +682,7 @@ Monitor *
 createmon(void)
 {
        Monitor *m;
+       unsigned int i;
 
        m = ecalloc(1, sizeof(Monitor));
        m->tagset[0] = m->tagset[1] = 1;
@@ -653,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;
 }
 
@@ -742,7 +796,7 @@ drawbar(Monitor *m)
 
        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);
@@ -849,6 +903,8 @@ focusmon(const Arg *arg)
        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
@@ -874,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);
        }
 }
 
@@ -988,7 +1045,7 @@ grabkeys(void)
 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);
 }
 
@@ -1312,6 +1369,16 @@ pushup(const Arg *arg) {
 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;
 }
 
@@ -1355,9 +1422,14 @@ 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))
@@ -1365,10 +1437,18 @@ resizemouse(const Arg *arg)
        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)
                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) {
@@ -1384,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)
                        {
@@ -1392,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) {
@@ -1549,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);
@@ -1570,7 +1657,7 @@ setmfact(const Arg *arg)
        f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
        if (f < 0.05 || f > 0.95)
                return;
-       selmon->mfact = f;
+       selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
        arrange(selmon);
 }
 
@@ -1679,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
@@ -1749,7 +1852,7 @@ tile(Monitor *m)
 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);
@@ -1786,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);
        }
@@ -2084,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);
 }
@@ -2209,6 +2362,7 @@ 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)