JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Ph's seltags-simplification with some modifications
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 979d4c6..8c51275 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -41,6 +41,8 @@
 #include <X11/Xutil.h>
 
 /* macros */
+#define MAX(a, b) ((a)>(b)?(a):(b))
+#define MIN(a, b) ((a)<(b)?(a):(b))
 #define BUTTONMASK             (ButtonPressMask|ButtonReleaseMask)
 #define CLEANMASK(mask)                (mask & ~(numlockmask|LockMask))
 #define LENGTH(x)              (sizeof x / sizeof x[0])
@@ -178,6 +180,7 @@ void scan(void);
 void setclientstate(Client *c, long state);
 void setgeom(const char *arg);
 void setlayout(const char *arg);
+void setmfact(const char *arg);
 void setup(void);
 void spawn(const char *arg);
 void tag(const char *arg);
@@ -211,6 +214,8 @@ char stext[256], buf[256];
 int screen, sx, sy, sw, sh;
 int (*xerrorxlib)(Display *, XErrorEvent *);
 int bx, by, bw, bh, blw, bgw, mx, my, mw, mh, mox, moy, mow, moh, tx, ty, tw, th, wx, wy, ww, wh;
+int viewtags_set = 0;
+double mfact;
 unsigned int numlockmask = 0;
 void (*handler[LASTEvent]) (XEvent *) = {
        [ButtonPress] = buttonpress,
@@ -229,22 +234,21 @@ void (*handler[LASTEvent]) (XEvent *) = {
 Atom wmatom[WMLast], netatom[NetLast];
 Bool otherwm, readin;
 Bool running = True;
-Bool *prevtags;
 Bool *seltags;
+Bool *viewtags[2];
 Client *clients = NULL;
 Client *sel = NULL;
 Client *stack = NULL;
 Cursor cursor[CurLast];
 Display *dpy;
 DC dc = {0};
-Geom *geom = NULL;
-Layout *lt = NULL;
 Window root, barwin;
 
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 #define TAGSZ (LENGTH(tags) * sizeof(Bool))
-static Bool tmp[LENGTH(tags)];
+Layout *lt = layouts;
+Geom *geom = geoms;
 
 /* function implementations */
 
@@ -259,10 +263,9 @@ applyrules(Client *c) {
        XGetClassHint(dpy, c->win, &ch);
        for(i = 0; i < LENGTH(rules); i++) {
                r = &rules[i];
-               if(r->title && strstr(c->name, r->title)
-               || (ch.res_class && r->class && strstr(ch.res_class, r->class))
-               || (ch.res_name && r->instance && strstr(ch.res_name, r->instance)))
-               {
+               if((!r->title || strstr(c->name, r->title))
+               && (!r->class || (ch.res_class && strstr(ch.res_class, r->class)))
+               && (!r->instance || (ch.res_name && strstr(ch.res_name, r->instance)))) {
                        c->isfloating = r->isfloating;
                        if(r->tag) {
                                c->tags[idxoftag(r->tag)] = True;
@@ -322,10 +325,14 @@ buttonpress(XEvent *e) {
        XButtonPressedEvent *ev = &e->xbutton;
 
        if(ev->window == barwin) {
+               if((ev->x < bgw) && ev->button == Button1) {
+                       setgeom(NULL);
+                       return;
+               }
                x = bgw;
                for(i = 0; i < LENGTH(tags); i++) {
                        x += textw(tags[i]);
-                       if(ev->x > bgw && ev->x < x) {
+                       if(ev->x >= bgw && ev->x < x) {
                                if(ev->button == Button1) {
                                        if(ev->state & MODKEY)
                                                tag(tags[i]);
@@ -341,6 +348,8 @@ buttonpress(XEvent *e) {
                                return;
                        }
                }
+               if((ev->x < x + blw) && ev->button == Button1) 
+                       setlayout(NULL);
        }
        else if((c = getclient(ev->window))) {
                focus(c);
@@ -423,8 +432,11 @@ void
 configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
 
-       if(ev->window == root && (ev->width != sw || ev->height != sh))
-               setgeom(NULL);
+       if(ev->window == root && (ev->width != sw || ev->height != sh)) {
+               sw = ev->width;
+               sh = ev->height;
+               setgeom(geom->symbol);
+       }
 }
 
 void
@@ -514,9 +526,11 @@ drawbar(void) {
        Client *c;
 
        dc.x = 0;
-       dc.w = bgw;
-       drawtext(geom->symbol, dc.norm, False);
-       dc.x += bgw;
+       if(bgw > 0) {
+               dc.w = bgw;
+               drawtext(geom->symbol, dc.norm, False);
+               dc.x += bgw;
+       }
        for(c = stack; c && !isvisible(c); c = c->snext);
        for(i = 0; i < LENGTH(tags); i++) {
                dc.w = textw(tags[i]);
@@ -530,9 +544,13 @@ drawbar(void) {
                }
                dc.x += dc.w;
        }
-       dc.w = blw;
-       drawtext(lt->symbol, dc.norm, False);
-       x = dc.x + dc.w;
+       if(blw > 0) {
+               dc.w = blw;
+               drawtext(lt->symbol, dc.norm, False);
+               x = dc.x + dc.w;
+       }
+       else
+               x = dc.x;
        dc.w = textw(stext);
        dc.x = bw - dc.w;
        if(dc.x < x) {
@@ -585,9 +603,8 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
        if(!text)
                return;
        w = 0;
-       olen = len = strlen(text);
-       if(len >= sizeof buf)
-               len = sizeof buf - 1;
+       olen = strlen(text);
+       len = MIN(olen, sizeof buf - 1);
        memcpy(buf, text, len);
        buf[len] = 0;
        h = dc.font.ascent + dc.font.descent;
@@ -789,39 +806,20 @@ gettextprop(Window w, Atom atom, char *text, unsigned int size) {
 
 void
 grabbuttons(Client *c, Bool focused) {
-       XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
+       int i, j;
+       unsigned int buttons[]   = { Button1, Button2, Button3 };
+       unsigned int modifiers[] = { MODKEY, MODKEY|LockMask, MODKEY|numlockmask,
+                                    MODKEY|numlockmask|LockMask} ;
 
-       if(focused) {
-               XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button1, MODKEY|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button1, MODKEY|numlockmask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button1, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-
-               XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button2, MODKEY|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button2, MODKEY|numlockmask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button2, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-
-               XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button3, MODKEY|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button3, MODKEY|numlockmask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-               XGrabButton(dpy, Button3, MODKEY|numlockmask|LockMask, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
-       }
+       XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
+       if(focused)
+               for(i = 0; i < LENGTH(buttons); i++)
+                       for(j = 0; j < LENGTH(modifiers); j++)
+                               XGrabButton(dpy, buttons[i], modifiers[j], c->win, False,
+                                           BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
        else
-               XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
-                               GrabModeAsync, GrabModeSync, None, None);
+               XGrabButton(dpy, AnyButton, AnyModifier, c->win, False,
+                           BUTTONMASK, GrabModeAsync, GrabModeSync, None, None);
 }
 
 void
@@ -857,7 +855,7 @@ unsigned int
 idxoftag(const char *t) {
        unsigned int i;
 
-       for(i = 0; (i < LENGTH(tags)) && strcmp(tags[i], t); i++);
+       for(i = 0; (i < LENGTH(tags)) && t && strcmp(tags[i], t); i++);
        return (i < LENGTH(tags)) ? i : 0;
 }
 
@@ -883,10 +881,8 @@ initfont(const char *fontstr) {
                font_extents = XExtentsOfFontSet(dc.font.set);
                n = XFontsOfFontSet(dc.font.set, &xfonts, &font_names);
                for(i = 0, dc.font.ascent = 0, dc.font.descent = 0; i < n; i++) {
-                       if(dc.font.ascent < (*xfonts)->ascent)
-                               dc.font.ascent = (*xfonts)->ascent;
-                       if(dc.font.descent < (*xfonts)->descent)
-                               dc.font.descent = (*xfonts)->descent;
+                       dc.font.ascent = MAX(dc.font.ascent, (*xfonts)->ascent);
+                       dc.font.descent = MAX(dc.font.descent,(*xfonts)->descent);
                        xfonts++;
                }
        }
@@ -1011,10 +1007,8 @@ manage(Window w, XWindowAttributes *wa) {
                        c->x = wx + ww - c->w - 2 * c->bw;
                if(c->y + c->h + 2 * c->bw > wy + wh)
                        c->y = wy + wh - c->h - 2 * c->bw;
-               if(c->x < wx)
-                       c->x = wx;
-               if(c->y < wy)
-                       c->y = wy;
+               c->x = MAX(c->x, wx);
+               c->y = MAX(c->y, wy);
                c->bw = BORDERPX;
        }
 
@@ -1070,7 +1064,7 @@ monocle(void) {
        Client *c;
 
        for(c = clients; c; c = c->next)
-               if(isvisible(c))
+               if((lt->isfloating || !c->isfloating) &&  isvisible(c))
                        resize(c, mox, moy, mow - 2 * c->bw, moh - 2 * c->bw, RESIZEHINTS);
 }
 
@@ -1164,11 +1158,10 @@ quit(const char *arg) {
 
 void
 reapply(const char *arg) {
-       static Bool zerotags[LENGTH(tags)] = { 0 };
        Client *c;
 
        for(c = clients; c; c = c->next) {
-               memcpy(c->tags, zerotags, sizeof zerotags);
+               memset(c->tags, 0, TAGSZ);
                applyrules(c);
        }
        arrange();
@@ -1180,20 +1173,20 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
 
        if(sizehints) {
                /* set minimum possible */
-               if (w < 1)
-                       w = 1;
-               if (h < 1)
-                       h = 1;
+               w = MAX(1, w);
+               h = MAX(1, h);
 
                /* temporarily remove base dimensions */
                w -= c->basew;
                h -= c->baseh;
 
                /* adjust for aspect limits */
-               if (c->minay > 0 && c->maxay > 0 && c->minax > 0 && c->maxax > 0) {
-                       if (w * c->maxay > h * c->maxax)
+               if(c->minax != c->maxax && c->minay != c->maxay 
+               && c->minax > 0 && c->maxax > 0 && c->minay > 0 && c->maxay > 0)
+               {
+                       if(w * c->maxay > h * c->maxax)
                                w = h * c->maxax / c->maxay;
-                       else if (w * c->minay < h * c->minax)
+                       else if(w * c->minay < h * c->minax)
                                h = w * c->minay / c->minax;
                }
 
@@ -1207,14 +1200,14 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
                w += c->basew;
                h += c->baseh;
 
-               if(c->minw > 0 && w < c->minw)
-                       w = c->minw;
-               if(c->minh > 0 && h < c->minh)
-                       h = c->minh;
-               if(c->maxw > 0 && w > c->maxw)
-                       w = c->maxw;
-               if(c->maxh > 0 && h > c->maxh)
-                       h = c->maxh;
+               w = MAX(w, c->minw);
+               h = MAX(h, c->minh);
+               
+               if (c->maxw)
+                       w = MIN(w, c->maxw);
+
+               if (c->maxh)
+                       h = MIN(h, c->maxh);
        }
        if(w <= 0 || h <= 0)
                return;
@@ -1267,10 +1260,8 @@ resizemouse(Client *c) {
                        break;
                case MotionNotify:
                        XSync(dpy, False);
-                       if((nw = ev.xmotion.x - ocx - 2 * c->bw + 1) <= 0)
-                               nw = 1;
-                       if((nh = ev.xmotion.y - ocy - 2 * c->bw + 1) <= 0)
-                               nh = 1;
+                       nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
+                       nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
                        if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
                                togglefloating(NULL);
                        if((lt->isfloating) || c->isfloating)
@@ -1408,12 +1399,18 @@ void
 setgeom(const char *arg) {
        unsigned int i;
 
-       for(i = 0; arg && i < LENGTH(geoms); i++)
-               if(!strcmp(geoms[i].symbol, arg))
-                       break;
-       if(i == LENGTH(geoms))
-               return;
-       geom = &geoms[i];
+       if(!arg) {
+               if(++geom == &geoms[LENGTH(geoms)])
+                       geom = &geoms[0];
+       }
+       else {
+               for(i = 0; i < LENGTH(geoms); i++)
+                       if(!strcmp(geoms[i].symbol, arg))
+                               break;
+               if(i == LENGTH(geoms))
+                       return;
+               geom = &geoms[i];
+       }
        geom->apply();
        updatebarpos();
        arrange();
@@ -1421,20 +1418,18 @@ setgeom(const char *arg) {
 
 void
 setlayout(const char *arg) {
-       static Layout *revert = 0;
        unsigned int i;
 
-       if(!arg)
-               return;
-       for(i = 0; i < LENGTH(layouts); i++)
-               if(!strcmp(arg, layouts[i].symbol))
-                       break;
-       if(i == LENGTH(layouts))
-               return;
-       if(revert && &layouts[i] == lt)
-               lt = revert;
+       if(!arg) {
+               if(++lt == &layouts[LENGTH(layouts)])
+                       lt = &layouts[0];
+       }
        else {
-               revert = lt;
+               for(i = 0; i < LENGTH(layouts); i++)
+                       if(!strcmp(arg, layouts[i].symbol))
+                               break;
+               if(i == LENGTH(layouts))
+                       return;
                lt = &layouts[i];
        }
        if(sel)
@@ -1444,8 +1439,27 @@ setlayout(const char *arg) {
 }
 
 void
+setmfact(const char *arg) {
+       double d;
+
+       if(lt->isfloating)
+               return;
+       if(!arg)
+               mfact = MFACT;
+       else {
+               d = strtod(arg, NULL);
+               if(arg[0] == '-' || arg[0] == '+')
+                       d += mfact;
+               if(d < 0.1 || d > 0.9)
+                       return;
+               mfact = d;
+       }
+       setgeom(geom->symbol);
+}
+
+void
 setup(void) {
-       unsigned int i;
+       unsigned int i, w;
        XSetWindowAttributes wa;
 
        /* init screen */
@@ -1459,7 +1473,7 @@ setup(void) {
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
        bh = dc.font.height + 2;
-       geom = &geoms[0];
+       mfact = MFACT;
        geom->apply();
 
        /* init atoms */
@@ -1491,23 +1505,19 @@ setup(void) {
                XSetFont(dpy, dc.gc, dc.font.xfont->fid);
 
        /* init tags */
-       seltags = emallocz(TAGSZ);
-       prevtags = emallocz(TAGSZ);
-       seltags[0] = prevtags[0] = True;
-
-       /* init layouts */
-       lt = &layouts[0];
+       viewtags[0] = emallocz(TAGSZ);
+       viewtags[1] = emallocz(TAGSZ);
+       viewtags[0][0] = viewtags[1][0] = True;
+       seltags = viewtags[0];
 
        /* init bar */
-       for(blw = i = 0; i < LENGTH(layouts); i++) {
-               i = textw(layouts[i].symbol);
-               if(i > blw)
-                       blw = i;
+       for(blw = i = 0; LENGTH(layouts) > 1 && i < LENGTH(layouts); i++) {
+               w = textw(layouts[i].symbol);
+               blw = MAX(blw, w);
        }
-       for(bgw = i = 0; i < LENGTH(geoms); i++) {
-               i = textw(geoms[i].symbol);
-               if(i > bgw)
-                       bgw = i;
+       for(bgw = i = 0; LENGTH(geoms) > 1 && i < LENGTH(geoms); i++) {
+               w = textw(geoms[i].symbol);
+               bgw = MAX(bgw, w);
        }
 
        wa.override_redirect = 1;
@@ -1818,9 +1828,9 @@ updatewmhints(Client *c) {
        }
 }
 
-
 void
 view(const char *arg) {
+       Bool tmp[LENGTH(tags)];
        unsigned int i;
 
        for(i = 0; i < LENGTH(tags); i++)
@@ -1828,7 +1838,7 @@ view(const char *arg) {
        tmp[idxoftag(arg)] = True;
 
        if(memcmp(seltags, tmp, TAGSZ) != 0) {
-               memcpy(prevtags, seltags, TAGSZ);
+               seltags = viewtags[viewtags_set ^= 1];  /* toggle tagset */
                memcpy(seltags, tmp, TAGSZ);
                arrange();
        }
@@ -1836,10 +1846,7 @@ view(const char *arg) {
 
 void
 viewprevtag(const char *arg) {
-
-       memcpy(tmp, seltags, TAGSZ);
-       memcpy(seltags, prevtags, TAGSZ);
-       memcpy(prevtags, tmp, TAGSZ);
+       seltags = viewtags[viewtags_set ^= 1];  /* toggle tagset */
        arrange();
 }
 
@@ -1910,3 +1917,4 @@ main(int argc, char *argv[]) {
        XCloseDisplay(dpy);
        return 0;
 }
+