Reversed direction of keys for switch between monitors to match the physical arrangem...
[dwm] / dwm.c
diff --git a/dwm.c b/dwm.c
index a507385..4aaa9ac 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -111,27 +111,6 @@ typedef struct {
        void (*arrange)(Monitor *);
 } Layout;
 
        void (*arrange)(Monitor *);
 } Layout;
 
-struct Monitor {
-       char ltsymbol[16];
-       float mfact;
-       int nmaster;
-       int num;
-       int by;               /* bar geometry */
-       int mx, my, mw, mh;   /* screen size */
-       int wx, wy, ww, wh;   /* window area  */
-       unsigned int seltags;
-       unsigned int sellt;
-       unsigned int tagset[2];
-       int showbar;
-       int topbar;
-       Client *clients;
-       Client *sel;
-       Client *stack;
-       Monitor *next;
-       Window barwin;
-       const Layout *lt[2];
-};
-
 typedef struct {
        const char *class;
        const char *instance;
 typedef struct {
        const char *class;
        const char *instance;
@@ -175,19 +154,21 @@ static long getstate(Window w);
 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, int focused);
 static void grabkeys(void);
 static int gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, int focused);
 static void grabkeys(void);
-static void incnmaster(const Arg *arg);
 static void keypress(XEvent *e);
 static void killclient(const Arg *arg);
 static void keypress(XEvent *e);
 static void killclient(const Arg *arg);
+static void layoutmenu(const Arg *arg);
 static void manage(Window w, XWindowAttributes *wa);
 static void mappingnotify(XEvent *e);
 static void maprequest(XEvent *e);
 static void monocle(Monitor *m);
 static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static void manage(Window w, XWindowAttributes *wa);
 static void mappingnotify(XEvent *e);
 static void maprequest(XEvent *e);
 static void monocle(Monitor *m);
 static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
+static void nametag(const Arg *arg);
 static Client *nexttiled(Client *c);
 static void pop(Client *c);
 static void propertynotify(XEvent *e);
 static void quit(const Arg *arg);
 static Client *nexttiled(Client *c);
 static void pop(Client *c);
 static void propertynotify(XEvent *e);
 static void quit(const Arg *arg);
+static void quitprompt(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);
 static void resizeclient(Client *c, int x, int y, int w, int h);
 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);
 static void resizeclient(Client *c, int x, int y, int w, int h);
@@ -235,6 +216,11 @@ static int xerrordummy(Display *dpy, XErrorEvent *ee);
 static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
 
 static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
 
+static void keyrelease(XEvent *e);
+static void combotag(const Arg *arg);
+static void comboview(const Arg *arg);
+
+
 /* variables */
 static const char broken[] = "broken";
 static char stext[256];
 /* variables */
 static const char broken[] = "broken";
 static char stext[256];
@@ -246,6 +232,7 @@ static int (*xerrorxlib)(Display *, XErrorEvent *);
 static unsigned int numlockmask = 0;
 static void (*handler[LASTEvent]) (XEvent *) = {
        [ButtonPress] = buttonpress,
 static unsigned int numlockmask = 0;
 static void (*handler[LASTEvent]) (XEvent *) = {
        [ButtonPress] = buttonpress,
+       [ButtonRelease] = keyrelease,
        [ClientMessage] = clientmessage,
        [ConfigureRequest] = configurerequest,
        [ConfigureNotify] = configurenotify,
        [ClientMessage] = clientmessage,
        [ConfigureRequest] = configurerequest,
        [ConfigureNotify] = configurenotify,
@@ -253,6 +240,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
        [EnterNotify] = enternotify,
        [Expose] = expose,
        [FocusIn] = focusin,
        [EnterNotify] = enternotify,
        [Expose] = expose,
        [FocusIn] = focusin,
+       [KeyRelease] = keyrelease,
        [KeyPress] = keypress,
        [MappingNotify] = mappingnotify,
        [MapRequest] = maprequest,
        [KeyPress] = keypress,
        [MappingNotify] = mappingnotify,
        [MapRequest] = maprequest,
@@ -262,6 +250,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
 };
 static Atom wmatom[WMLast], netatom[NetLast];
 static int running = 1;
 };
 static Atom wmatom[WMLast], netatom[NetLast];
 static int running = 1;
+static int restart = 1;
 static Cur *cursor[CurLast];
 static Clr **scheme;
 static Display *dpy;
 static Cur *cursor[CurLast];
 static Clr **scheme;
 static Display *dpy;
@@ -276,6 +265,42 @@ static Window root, wmcheckwin;
 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
 
 /* function implementations */
 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
 
 /* function implementations */
+static int combo = 0;
+
+void
+keyrelease(XEvent *e) {
+       combo = 0;
+}
+
+void
+combotag(const Arg *arg) {
+       if(selmon->sel && arg->ui & TAGMASK) {
+               if (combo) {
+                       selmon->sel->tags |= arg->ui & TAGMASK;
+               } else {
+                       combo = 1;
+                       selmon->sel->tags = arg->ui & TAGMASK;
+               }
+               focus(NULL);
+               arrange(selmon);
+       }
+}
+
+void
+comboview(const Arg *arg) {
+       unsigned newtags = arg->ui & TAGMASK;
+       if (combo) {
+               selmon->tagset[selmon->seltags] |= newtags;
+       } else {
+               selmon->seltags ^= 1;   /*toggle tagset*/
+               combo = 1;
+               if (newtags)
+                       selmon->tagset[selmon->seltags] = newtags;
+       }
+       focus(NULL);
+       arrange(selmon);
+}
+
 void
 applyrules(Client *c)
 {
 void
 applyrules(Client *c)
 {
@@ -309,7 +334,9 @@ applyrules(Client *c)
                XFree(ch.res_class);
        if (ch.res_name)
                XFree(ch.res_name);
                XFree(ch.res_class);
        if (ch.res_name)
                XFree(ch.res_name);
-       c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags];
+       if(c->tags & TAGMASK)                    c->tags = c->tags & TAGMASK;
+       else if(c->mon->tagset[c->mon->seltags]) c->tags = c->mon->tagset[c->mon->seltags];
+       else                                     c->tags = 1;
 }
 
 int
 }
 
 int
@@ -634,17 +661,32 @@ configurerequest(XEvent *e)
 Monitor *
 createmon(void)
 {
 Monitor *
 createmon(void)
 {
+       unsigned int i;
        Monitor *m;
 
        m = ecalloc(1, sizeof(Monitor));
        Monitor *m;
 
        m = ecalloc(1, sizeof(Monitor));
-       m->tagset[0] = m->tagset[1] = 1;
+       m->tagset[0] = m->tagset[1] = startontag ? 1 : 0;
        m->mfact = mfact;
        m->mfact = mfact;
-       m->nmaster = nmaster;
        m->showbar = showbar;
        m->topbar = topbar;
        m->lt[0] = &layouts[0];
        m->lt[1] = &layouts[1 % LENGTH(layouts)];
        strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
        m->showbar = showbar;
        m->topbar = topbar;
        m->lt[0] = &layouts[0];
        m->lt[1] = &layouts[1 % LENGTH(layouts)];
        strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
+       m->ltaxis[0] = layoutaxis[0];
+       m->ltaxis[1] = layoutaxis[1];
+       m->ltaxis[2] = layoutaxis[2];
+       m->msplit = 1;
+       /* init tags, bars, layouts, axes, msplits and mfacts */
+       m->curtag = m->prevtag = 1;
+       for(i = 0; i < LENGTH(tags) + 1; i++){
+               m->showbars[i] = m->showbar;
+               m->lts[i] = &layouts[0];
+               m->mfacts[i] = m->mfact;
+               m->ltaxes[i][0] = m->ltaxis[0];
+               m->ltaxes[i][1] = m->ltaxis[1];
+               m->ltaxes[i][2] = m->ltaxis[2];
+               m->msplits[i] = m->msplit;
+       }
        return m;
 }
 
        return m;
 }
 
@@ -699,6 +741,7 @@ dirtomon(int dir)
 void
 drawbar(Monitor *m)
 {
 void
 drawbar(Monitor *m)
 {
+       int indn;
        int x, w, tw = 0;
        int boxs = drw->fonts->h / 9;
        int boxw = drw->fonts->h / 6 + 2;
        int x, w, tw = 0;
        int boxs = drw->fonts->h / 9;
        int boxw = drw->fonts->h / 6 + 2;
@@ -722,13 +765,18 @@ drawbar(Monitor *m)
        }
        x = 0;
        for (i = 0; i < LENGTH(tags); i++) {
        }
        x = 0;
        for (i = 0; i < LENGTH(tags); i++) {
+               indn = 0;
                w = TEXTW(tags[i]);
                drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
                drw_text(drw, x, 0, w, bh, lrpad / 2, tags[i], urg & 1 << i);
                w = TEXTW(tags[i]);
                drw_setscheme(drw, scheme[m->tagset[m->seltags] & 1 << i ? SchemeSel : SchemeNorm]);
                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);
+
+               for (c = m->clients; c; c = c->next) {
+                       if (c->tags & (1 << i)) {
+                               drw_rect(drw, x, 1 + (indn * 2), selmon->sel == c ? 6 : 1, 1, 1, urg & 1 << i);
+                               indn++;
+                       }
+               }
+
                x += w;
        }
        w = TEXTW(m->ltsymbol);
                x += w;
        }
        w = TEXTW(m->ltsymbol);
@@ -972,13 +1020,6 @@ grabkeys(void)
        }
 }
 
        }
 }
 
-void
-incnmaster(const Arg *arg)
-{
-       selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
-       arrange(selmon);
-}
-
 #ifdef XINERAMA
 static int
 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
 #ifdef XINERAMA
 static int
 isuniquegeom(XineramaScreenInfo *unique, size_t n, XineramaScreenInfo *info)
@@ -1023,6 +1064,24 @@ killclient(const Arg *arg)
        }
 }
 
        }
 }
 
+void
+layoutmenu(const Arg *arg) {
+       FILE *p;
+       char c[3], *s;
+       int i;
+
+       if (!(p = popen(layoutmenu_cmd, "r")))
+                return;
+       s = fgets(c, sizeof(c), p);
+       pclose(p);
+
+       if (!s || *s == '\0' || c[0] == '\0')
+                return;
+
+       i = atoi(c);
+       setlayout(&((Arg) { .v = &layouts[i] }));
+}
+
 void
 manage(Window w, XWindowAttributes *wa)
 {
 void
 manage(Window w, XWindowAttributes *wa)
 {
@@ -1197,6 +1256,34 @@ movemouse(const Arg *arg)
        }
 }
 
        }
 }
 
+void
+nametag(const Arg *arg) {
+       char *p, name[MAX_TAGLEN];
+       FILE *f;
+       int i;
+
+       errno = 0; // popen(3p) says on failure it "may" set errno
+    // TODO: Create a central location for the dmenu font specification.
+    // It should not be hardcoded in multiple locations.
+       if(!(f = popen("dmenu -fn monospace:size=12 < /dev/null", "r"))) {
+               fprintf(stderr, "dwm: popen 'dmenu < /dev/null' failed%s%s\n", errno ? ": " : "", errno ? strerror(errno) : "");
+               return;
+       }
+       if (!(p = fgets(name, MAX_TAGLEN, f)) && (i = errno) && ferror(f))
+               fprintf(stderr, "dwm: fgets failed: %s\n", strerror(i));
+       if (pclose(f) < 0)
+               fprintf(stderr, "dwm: pclose failed: %s\n", strerror(errno));
+       if(!p)
+               return;
+       if((p = strchr(name, '\n')))
+               *p = '\0';
+
+       for(i = 0; i < LENGTH(tags); i++)
+               if(selmon->tagset[selmon->seltags] & (1 << i))
+                       strcpy(tags[i], name);
+       drawbars();
+}
+
 Client *
 nexttiled(Client *c)
 {
 Client *
 nexttiled(Client *c)
 {
@@ -1256,6 +1343,33 @@ quit(const Arg *arg)
        running = 0;
 }
 
        running = 0;
 }
 
+void
+quitprompt(const Arg *arg)
+{
+    // TODO: Create a central location for the dmenu font specification.
+    // It should not be hardcoded in multiple locations.
+       FILE *pp = popen("echo -e \"no\nrestart\nyes\" | dmenu -fn monospace:size=12 -i -sb red -p \"Quit DWM?\"", "r");
+       if(pp != NULL) {
+               char buf[1024];
+               if (fgets(buf, sizeof(buf), pp) == NULL) {
+                       fprintf(stderr, "Quitprompt: Error reading pipe!\n");
+                       return;
+               }
+               if (strcmp(buf, "yes\n") == 0) {
+                       pclose(pp);
+                       restart = 0;
+                       quit(NULL);
+               } else if (strcmp(buf, "restart\n") == 0) {
+                       pclose(pp);
+                       restart = 1;
+                       quit(NULL);
+               } else if (strcmp(buf, "no\n") == 0) {
+                       pclose(pp);
+                       return;
+               }
+       }
+}
+
 Monitor *
 recttomon(int x, int y, int w, int h)
 {
 Monitor *
 recttomon(int x, int y, int w, int h)
 {
@@ -1421,7 +1535,7 @@ sendmon(Client *c, Monitor *m)
        detach(c);
        detachstack(c);
        c->mon = m;
        detach(c);
        detachstack(c);
        c->mon = m;
-       c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
+       c->tags = (m->tagset[m->seltags] ? m->tagset[m->seltags] : 1);
        attach(c);
        attachstack(c);
        focus(NULL);
        attach(c);
        attachstack(c);
        focus(NULL);
@@ -1508,7 +1622,7 @@ setlayout(const Arg *arg)
        if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
                selmon->sellt ^= 1;
        if (arg && arg->v)
        if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
                selmon->sellt ^= 1;
        if (arg && arg->v)
-               selmon->lt[selmon->sellt] = (Layout *)arg->v;
+               selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag] = (Layout *)arg->v;
        strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
        if (selmon->sel)
                arrange(selmon);
        strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
        if (selmon->sel)
                arrange(selmon);
@@ -1527,7 +1641,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;
        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->mfacts[selmon->curtag] = f;
        arrange(selmon);
 }
 
        arrange(selmon);
 }
 
@@ -1670,38 +1784,10 @@ tagmon(const Arg *arg)
        sendmon(selmon->sel, dirtomon(arg->i));
 }
 
        sendmon(selmon->sel, dirtomon(arg->i));
 }
 
-void
-tile(Monitor *m)
-{
-       unsigned int i, n, h, mw, my, ty;
-       Client *c;
-
-       for (n = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), n++);
-       if (n == 0)
-               return;
-
-       if (n > m->nmaster)
-               mw = m->nmaster ? m->ww * m->mfact : 0;
-       else
-               mw = m->ww;
-       for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
-               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);
-                       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);
-                       if (ty + HEIGHT(c) < m->wh)
-                               ty += HEIGHT(c);
-               }
-}
-
 void
 togglebar(const Arg *arg)
 {
 void
 togglebar(const Arg *arg)
 {
-       selmon->showbar = !selmon->showbar;
+       selmon->showbar = selmon->showbars[selmon->curtag] = !selmon->showbar;
        updatebarpos(selmon);
        XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
        arrange(selmon);
        updatebarpos(selmon);
        XMoveResizeWindow(dpy, selmon->barwin, selmon->wx, selmon->by, selmon->ww, bh);
        arrange(selmon);
@@ -1725,12 +1811,31 @@ void
 toggletag(const Arg *arg)
 {
        unsigned int newtags;
 toggletag(const Arg *arg)
 {
        unsigned int newtags;
+       unsigned int i;
 
        if (!selmon->sel)
                return;
        newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
        if (newtags) {
                selmon->sel->tags = newtags;
 
        if (!selmon->sel)
                return;
        newtags = selmon->sel->tags ^ (arg->ui & TAGMASK);
        if (newtags) {
                selmon->sel->tags = newtags;
+               if(newtags == ~0) {
+                       selmon->prevtag = selmon->curtag;
+                       selmon->curtag = 0;
+               }
+               if(!(newtags & 1 << (selmon->curtag - 1))) {
+                       selmon->prevtag = selmon->curtag;
+                       for (i=0; !(newtags & 1 << i); i++);
+                       selmon->curtag = i + 1;
+               }
+               selmon->sel->tags = newtags;
+               selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag];
+               selmon->mfact = selmon->mfacts[selmon->curtag];
+               if (selmon->showbar != selmon->showbars[selmon->curtag])
+                       togglebar(NULL);
+               selmon->ltaxis[0] = selmon->ltaxes[selmon->curtag][0];
+               selmon->ltaxis[1] = selmon->ltaxes[selmon->curtag][1];
+               selmon->ltaxis[2] = selmon->ltaxes[selmon->curtag][2];
+               selmon->msplit = selmon->msplits[selmon->curtag];
                focus(NULL);
                arrange(selmon);
        }
                focus(NULL);
                arrange(selmon);
        }
@@ -1741,11 +1846,9 @@ toggleview(const Arg *arg)
 {
        unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
 
 {
        unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
 
-       if (newtagset) {
-               selmon->tagset[selmon->seltags] = newtagset;
-               focus(NULL);
-               arrange(selmon);
-       }
+       selmon->tagset[selmon->seltags] = newtagset;
+       focus(NULL);
+       arrange(selmon);
 }
 
 void
 }
 
 void
@@ -2039,11 +2142,33 @@ updatewmhints(Client *c)
 void
 view(const Arg *arg)
 {
 void
 view(const Arg *arg)
 {
-       if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
+       unsigned int i;
+
+       if(arg->ui && (arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
                return;
        selmon->seltags ^= 1; /* toggle sel tagset */
                return;
        selmon->seltags ^= 1; /* toggle sel tagset */
-       if (arg->ui & TAGMASK)
+       if (arg->ui & TAGMASK) {
                selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
                selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
+               selmon->prevtag = selmon->curtag;
+               if(arg->ui == ~0)
+                       selmon->curtag = 0;
+               else {
+                       for (i=0; !(arg->ui & 1 << i); i++);
+                       selmon->curtag = i + 1;
+               }
+       } else {
+               selmon->prevtag = selmon->curtag ^ selmon->prevtag;
+               selmon->curtag ^= selmon->prevtag;
+               selmon->prevtag = selmon->curtag ^ selmon->prevtag;
+       }
+       selmon->lt[selmon->sellt] = selmon->lts[selmon->curtag];
+       selmon->mfact = selmon->mfacts[selmon->curtag];
+       if(selmon->showbar != selmon->showbars[selmon->curtag])
+               togglebar(NULL);
+       selmon->ltaxis[0] = selmon->ltaxes[selmon->curtag][0];
+       selmon->ltaxis[1] = selmon->ltaxes[selmon->curtag][1];
+       selmon->ltaxis[2] = selmon->ltaxes[selmon->curtag][2];
+       selmon->msplit = selmon->msplits[selmon->curtag];
        focus(NULL);
        arrange(selmon);
 }
        focus(NULL);
        arrange(selmon);
 }
@@ -2147,5 +2272,8 @@ main(int argc, char *argv[])
        run();
        cleanup();
        XCloseDisplay(dpy);
        run();
        cleanup();
        XCloseDisplay(dpy);
+       if (restart == 1) {
+               execlp("dwm-sgk", "dwm-sgk", NULL);
+       }
        return EXIT_SUCCESS;
 }
        return EXIT_SUCCESS;
 }