applied pertag
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 02db465..04742a3 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -29,6 +29,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <sys/wait.h>
 #include <X11/cursorfont.h>
 #include <X11/keysym.h>
@@ -112,6 +113,7 @@ typedef struct {
        void (*arrange)(Monitor *);
 } Layout;
 
+typedef struct Pertag Pertag;
 struct Monitor {
        char ltsymbol[16];
        float mfact;
@@ -132,6 +134,7 @@ struct Monitor {
        Monitor *next;
        Window barwin;
        const Layout *lt[2];
+       Pertag *pertag;
 };
 
 typedef struct {
@@ -209,6 +212,7 @@ static void resizeclient(Client *c, int x, int y, int w, int h);
 static void resizemouse(const Arg *arg);
 static void restack(Monitor *m);
 static void run(void);
+static void runautostart(void);
 static void scan(void);
 static int sendevent(Client *c, Atom proto);
 static void sendmon(Client *c, Monitor *m);
@@ -254,12 +258,18 @@ static void load_xresources(void);
 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
 
 /* variables */
+static const char autostartblocksh[] = "autostart_blocking.sh";
+static const char autostartsh[] = "autostart.sh";
 static const char broken[] = "broken";
+static const char dwmdir[] = "dwm";
+static const char localshare[] = ".local/share";
 static char stext[256];
 static int screen;
 static int sw, sh;           /* X display screen geometry width, height */
 static int bh, blw = 0;      /* bar geometry */
 static int lrpad;            /* sum of left and right padding for text */
+static int vp;               /* vertical padding for bar */
+static int sp;               /* side padding for bar */
 static int (*xerrorxlib)(Display *, XErrorEvent *);
 static unsigned int numlockmask = 0;
 static void (*handler[LASTEvent]) (XEvent *) = {
@@ -290,6 +300,15 @@ 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]; };
 
@@ -586,7 +605,7 @@ configurenotify(XEvent *e)
                                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);
+                               XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww -  2 * sp, bh);
                        }
                        focus(NULL);
                        arrange(NULL);
@@ -650,6 +669,7 @@ Monitor *
 createmon(void)
 {
        Monitor *m;
+       unsigned int i;
 
        m = ecalloc(1, sizeof(Monitor));
        m->tagset[0] = m->tagset[1] = 1;
@@ -661,6 +681,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;
 }
 
@@ -725,7 +759,7 @@ drawbar(Monitor *m)
        if (m == selmon) { /* status is only drawn on selected monitor */
                drw_setscheme(drw, scheme[SchemeNorm]);
                tw = TEXTW(stext) - lrpad + 2; /* 2px right padding */
-               drw_text(drw, m->ww - tw, 0, tw, bh, 0, stext, 0);
+               drw_text(drw, m->ww - tw - 2 * sp, 0, tw, bh, 0, stext, 0);
        }
 
        for (c = m->clients; c; c = c->next) {
@@ -751,12 +785,12 @@ drawbar(Monitor *m)
        if ((w = m->ww - tw - x) > bh) {
                if (m->sel) {
                        drw_setscheme(drw, scheme[m == selmon ? SchemeSel : SchemeNorm]);
-                       drw_text(drw, x, 0, w, bh, lrpad / 2, m->sel->name, 0);
+                       drw_text(drw, x, 0, w - 2 * sp, bh, lrpad / 2, m->sel->name, 0);
                        if (m->sel->isfloating)
                                drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
                } else {
                        drw_setscheme(drw, scheme[SchemeNorm]);
-                       drw_rect(drw, x, 0, w, bh, 1, 1);
+                       drw_rect(drw, x, 0, w - 2 * sp, bh, 1, 1);
                }
        }
        drw_map(drw, m->barwin, 0, 0, m->ww, bh);
@@ -986,7 +1020,16 @@ grabkeys(void)
 void
 incnmaster(const Arg *arg)
 {
+       unsigned int i;
        selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
+       for(i=0; i<LENGTH(tags); ++i)
+               if(selmon->tagset[selmon->seltags] & 1<<i)
+                       selmon->pertag->nmasters[i+1] = selmon->nmaster;
+
+       if(selmon->pertag->curtag == 0)
+       {
+               selmon->pertag->nmasters[0] = selmon->nmaster;
+       }
        arrange(selmon);
 }
 
@@ -1400,6 +1443,83 @@ run(void)
                        handler[ev.type](&ev); /* call handler */
 }
 
+void
+runautostart(void)
+{
+       char *pathpfx;
+       char *path;
+       char *xdgdatahome;
+       char *home;
+       struct stat sb;
+
+       if ((home = getenv("HOME")) == NULL)
+               /* this is almost impossible */
+               return;
+
+       /* if $XDG_DATA_HOME is set and not empty, use $XDG_DATA_HOME/dwm,
+        * otherwise use ~/.local/share/dwm as autostart script directory
+        */
+       xdgdatahome = getenv("XDG_DATA_HOME");
+       if (xdgdatahome != NULL && *xdgdatahome != '\0') {
+               /* space for path segments, separators and nul */
+               pathpfx = ecalloc(1, strlen(xdgdatahome) + strlen(dwmdir) + 2);
+
+               if (sprintf(pathpfx, "%s/%s", xdgdatahome, dwmdir) <= 0) {
+                       free(pathpfx);
+                       return;
+               }
+       } else {
+               /* space for path segments, separators and nul */
+               pathpfx = ecalloc(1, strlen(home) + strlen(localshare)
+                                    + strlen(dwmdir) + 3);
+
+               if (sprintf(pathpfx, "%s/%s/%s", home, localshare, dwmdir) < 0) {
+                       free(pathpfx);
+                       return;
+               }
+       }
+
+       /* check if the autostart script directory exists */
+       if (! (stat(pathpfx, &sb) == 0 && S_ISDIR(sb.st_mode))) {
+               /* the XDG conformant path does not exist or is no directory
+                * so we try ~/.dwm instead
+                */
+               char *pathpfx_new = realloc(pathpfx, strlen(home) + strlen(dwmdir) + 3);
+               if(pathpfx_new == NULL) {
+                       free(pathpfx);
+                       return;
+               }
+   pathpfx = pathpfx_new;
+
+               if (sprintf(pathpfx, "%s/.%s", home, dwmdir) <= 0) {
+                       free(pathpfx);
+                       return;
+               }
+       }
+
+       /* try the blocking script first */
+       path = ecalloc(1, strlen(pathpfx) + strlen(autostartblocksh) + 2);
+       if (sprintf(path, "%s/%s", pathpfx, autostartblocksh) <= 0) {
+               free(path);
+               free(pathpfx);
+       }
+
+       if (access(path, X_OK) == 0)
+               system(path);
+
+       /* now the non-blocking script */
+       if (sprintf(path, "%s/%s", pathpfx, autostartsh) <= 0) {
+               free(path);
+               free(pathpfx);
+       }
+
+       if (access(path, X_OK) == 0)
+               system(strcat(path, " &"));
+
+       free(pathpfx);
+       free(path);
+}
+
 void
 scan(void)
 {
@@ -1530,11 +1650,26 @@ setgaps(const Arg *arg)
 void
 setlayout(const Arg *arg)
 {
+       unsigned int i;
        if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
                selmon->sellt ^= 1;
        if (arg && arg->v)
                selmon->lt[selmon->sellt] = (Layout *)arg->v;
        strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
+
+       for(i=0; i<LENGTH(tags); ++i)
+               if(selmon->tagset[selmon->seltags] & 1<<i)
+               {
+                       selmon->pertag->ltidxs[i+1][selmon->sellt] = selmon->lt[selmon->sellt];
+                       selmon->pertag->sellts[i+1] = selmon->sellt;
+               }
+
+       if(selmon->pertag->curtag == 0)
+       {
+               selmon->pertag->ltidxs[0][selmon->sellt] = selmon->lt[selmon->sellt];
+               selmon->pertag->sellts[0] = selmon->sellt;
+       }
+
        if (selmon->sel)
                arrange(selmon);
        else
@@ -1546,13 +1681,24 @@ void
 setmfact(const Arg *arg)
 {
        float f;
+       unsigned int i;
 
        if (!arg || !selmon->lt[selmon->sellt]->arrange)
                return;
        f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
+       if (arg->f == 0.0)
+               f = mfact;
        if (f < 0.05 || f > 0.95)
                return;
        selmon->mfact = f;
+       for(i=0; i<LENGTH(tags); ++i)
+               if(selmon->tagset[selmon->seltags] & 1<<i)
+                       selmon->pertag->mfacts[i+1] = f;
+
+       if(selmon->pertag->curtag == 0)
+       {
+               selmon->pertag->mfacts[0] = f;
+       }
        arrange(selmon);
 }
 
@@ -1577,6 +1723,9 @@ setup(void)
        lrpad = drw->fonts->h;
        bh = drw->fonts->h + 2;
        updategeom();
+       sp = sidepad;
+       vp = (topbar == 1) ? vertpad : - vertpad;
+
        /* init atoms */
        utf8string = XInternAtom(dpy, "UTF8_STRING", False);
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
@@ -1603,6 +1752,7 @@ setup(void)
        /* init bars */
        updatebars();
        updatestatus();
+       updatebarpos(selmon);
        /* supporting window for NetWMCheck */
        wmcheckwin = XCreateSimpleWindow(dpy, root, 0, 0, 1, 1, 0, 0, 0);
        XChangeProperty(dpy, wmcheckwin, netatom[NetWMCheck], XA_WINDOW, 32,
@@ -1731,9 +1881,18 @@ tile(Monitor *m)
 void
 togglebar(const Arg *arg)
 {
+       unsigned int i;
        selmon->showbar = !selmon->showbar;
+       for(i=0; i<LENGTH(tags); ++i)
+               if(selmon->tagset[selmon->seltags] & 1<<i)
+                       selmon->pertag->showbars[i+1] = selmon->showbar;
+
+       if(selmon->pertag->curtag == 0)
+       {
+               selmon->pertag->showbars[0] = selmon->showbar;
+       }
        updatebarpos(selmon);
-       XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
+       XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
        arrange(selmon);
 }
 
@@ -1770,9 +1929,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);
        }
@@ -1843,7 +2026,7 @@ updatebars(void)
        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),
+               m->barwin = XCreateWindow(dpy, root, m->wx + sp, m->by + vp, m->ww - 2 * sp, bh, 0, DefaultDepth(dpy, screen),
                                CopyFromParent, DefaultVisual(dpy, screen),
                                CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
                XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
@@ -1858,11 +2041,11 @@ updatebarpos(Monitor *m)
        m->wy = m->my;
        m->wh = m->mh;
        if (m->showbar) {
-               m->wh -= bh;
-               m->by = m->topbar ? m->wy : m->wy + m->wh;
-               m->wy = m->topbar ? m->wy + bh : m->wy;
+               m->wh = m->wh - vertpad - bh;
+               m->by = m->topbar ? m->wy : m->wy + m->wh + vertpad;
+               m->wy = m->topbar ? m->wy + bh + vp : m->wy;
        } else
-               m->by = -bh;
+               m->by = -bh - vp;
 }
 
 void
@@ -2067,11 +2250,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);
 }
@@ -2230,6 +2439,7 @@ main(int argc, char *argv[])
                die("pledge");
 #endif /* __OpenBSD__ */
        scan();
+       runautostart();
        run();
        cleanup();
        XCloseDisplay(dpy);