JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
made the string-based setgeom working
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 2efc50b..4a9ca82 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -33,7 +33,6 @@
 #include <sys/select.h>
 #include <sys/types.h>
 #include <sys/wait.h>
-#include <regex.h>
 #include <X11/cursorfont.h>
 #include <X11/keysym.h>
 #include <X11/Xatom.h>
@@ -59,7 +58,6 @@ typedef struct Client Client;
 struct Client {
        char name[256];
        int x, y, w, h;
-       int rx, ry, rw, rh;
        int basew, baseh, incw, inch, maxw, maxh, minw, minh;
        int minax, maxax, minay, maxay;
        long flags;
@@ -97,15 +95,19 @@ typedef struct {
 typedef struct {
        const char *symbol;
        void (*arrange)(void);
-} Layout;
+       Bool isfloating;
+} Layout; 
 
 typedef struct {
-       const char *prop;
+       const char *class;
+       const char *instance;
+       const char *title;
        const char *tag;
        Bool isfloating;
 } Rule;
 
 /* function declarations */
+void applygeom(const char *arg);
 void applyrules(Client *c);
 void arrange(void);
 void attach(Client *c);
@@ -117,6 +119,7 @@ void cleanup(void);
 void configure(Client *c);
 void configurenotify(XEvent *e);
 void configurerequest(XEvent *e);
+unsigned int counttiled(void);
 void destroynotify(XEvent *e);
 void detach(Client *c);
 void detachstack(Client *c);
@@ -134,6 +137,7 @@ void focusnext(const char *arg);
 void focusprev(const char *arg);
 Client *getclient(Window w);
 unsigned long getcolor(const char *colstr);
+double getdouble(const char *s);
 long getstate(Window w);
 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 void grabbuttons(Client *c, Bool focused);
@@ -161,19 +165,26 @@ void restack(void);
 void run(void);
 void scan(void);
 void setclientstate(Client *c, long state);
+void setgeom(const char *arg);
 void setlayout(const char *arg);
 void setup(void);
 void spawn(const char *arg);
 void tag(const char *arg);
 unsigned int textnw(const char *text, unsigned int len);
 unsigned int textw(const char *text);
-void tile(void);
+void tileh(void);
+void tilehstack(unsigned int n);
+Client *tilemaster(unsigned int n);
+void tileresize(Client *c, int x, int y, int w, int h);
+void tilev(void);
+void tilevstack(unsigned int n);
 void togglefloating(const char *arg);
 void toggletag(const char *arg);
 void toggleview(const char *arg);
 void unban(Client *c);
 void unmanage(Client *c);
 void unmapnotify(XEvent *e);
+void updatebarpos(void);
 void updatesizehints(Client *c);
 void updatetitle(Client *c);
 void updatewmhints(Client *c);
@@ -188,7 +199,7 @@ void zoom(const char *arg);
 char stext[256], buf[256];
 int screen, sx, sy, sw, sh;
 int (*xerrorxlib)(Display *, XErrorEvent *);
-unsigned int bh, blw = 0;
+int bx, by, bw, bh, blw, mx, my, mw, mh, mox, moy, mow, moh, tx, ty, tw, th, wx, wy, ww, wh;
 unsigned int numlockmask = 0;
 void (*handler[LASTEvent]) (XEvent *) = {
        [ButtonPress] = buttonpress,
@@ -205,7 +216,6 @@ void (*handler[LASTEvent]) (XEvent *) = {
        [UnmapNotify] = unmapnotify
 };
 Atom wmatom[WMLast], netatom[NetLast];
-Bool dozoom = True;
 Bool otherwm, readin;
 Bool running = True;
 Bool *prevtags;
@@ -227,6 +237,55 @@ static Bool tmp[LENGTH(tags)];
 /* function implementations */
 
 void
+applygeometry(const char *arg) {
+       static const char *lastArg = NULL;
+       char delim, op, *s, *e, *p;
+       double val;
+       int i, *map[] = { &bx,  &by,  &bw,  &bh,
+                         &wx,  &wy,  &ww,  &wh,
+                         &mx,  &my,  &mw,  &mh,
+                         &tx,  &ty,  &tw,  &th,
+                         &mox, &moy, &mow, &moh };
+
+       if(!arg)
+               arg = lastArg;
+       else
+               lastArg = arg;
+       if(!lastArg)
+               return;
+       strncpy(buf, arg, sizeof buf);
+       for(i = 0, e = s = buf; i < LENGTH(map) && e; e++)
+               if(*e == ' ' || *e == 0) {
+                       delim = *e;
+                       *e = 0;
+                       op = 0;
+                       /* check if there is an operator */
+                       for(p = s; p < e && *p != '-' && *p != '+' && *p != '*'; p++);
+                       if(*p) {
+                               op = *p;
+                               *p = 0;
+                       }
+                       val = getdouble(s);
+                       if(op && p > s) { /* intermediate operand, e.g. H-B */
+                               *(map[i]) = (int)val;
+                               s = ++p;
+                               val = getdouble(s);
+                       }
+                       switch(op) {
+                       default:  *(map[i])  = (int)val; break;
+                       case '-': *(map[i]) -= (int)val; break;
+                       case '+': *(map[i]) += (int)val; break;
+                       case '*': *(map[i])  = (int)(((double)*(map[i])) * val); break;
+                       }
+                       if(delim == 0)
+                               e = NULL;
+                       else
+                               s = ++e;
+                       i++;
+               }
+}
+
+void
 applyrules(Client *c) {
        unsigned int i;
        Bool matched = False;
@@ -237,9 +296,9 @@ applyrules(Client *c) {
        XGetClassHint(dpy, c->win, &ch);
        for(i = 0; i < LENGTH(rules); i++) {
                r = &rules[i];
-               if(strstr(c->name, r->prop)
-               || (ch.res_class && strstr(ch.res_class, r->prop))
-               || (ch.res_name && strstr(ch.res_name, r->prop)))
+               if(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)))
                {
                        c->isfloating = r->isfloating;
                        if(r->tag) {
@@ -289,7 +348,7 @@ void
 ban(Client *c) {
        if(c->isbanned)
                return;
-       XMoveWindow(dpy, c->win, c->x + 3 * sw, c->y);
+       XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
        c->isbanned = True;
 }
 
@@ -401,14 +460,8 @@ void
 configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
 
-       if(ev->window == root && (ev->width != sw || ev->height != sh)) {
-               sw = ev->width;
-               sh = ev->height;
-               XFreePixmap(dpy, dc.drawable);
-               dc.drawable = XCreatePixmap(dpy, root, BW, bh, DefaultDepth(dpy, screen));
-               XMoveResizeWindow(dpy, barwin, BX, BY, BW, bh);
-               arrange();
-       }
+       if(ev->window == root && (ev->width != sw || ev->height != sh))
+               setgeom(NULL);
 }
 
 void
@@ -420,7 +473,7 @@ configurerequest(XEvent *e) {
        if((c = getclient(ev->window))) {
                if(ev->value_mask & CWBorderWidth)
                        c->border = ev->border_width;
-               if(c->isfixed || c->isfloating || (floating == lt->arrange)) {
+               if(c->isfixed || c->isfloating || lt->isfloating) {
                        if(ev->value_mask & CWX)
                                c->x = sx + ev->x;
                        if(ev->value_mask & CWY)
@@ -455,6 +508,15 @@ configurerequest(XEvent *e) {
        XSync(dpy, False);
 }
 
+unsigned int
+counttiled(void) {
+       unsigned int n;
+       Client *c;
+
+       for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next), n++);
+       return n;
+}
+
 void
 destroynotify(XEvent *e) {
        Client *c;
@@ -506,10 +568,10 @@ drawbar(void) {
        drawtext(lt->symbol, dc.norm, False);
        x = dc.x + dc.w;
        dc.w = textw(stext);
-       dc.x = BW - dc.w;
+       dc.x = bw - dc.w;
        if(dc.x < x) {
                dc.x = x;
-               dc.w = BW - x;
+               dc.w = bw - x;
        }
        drawtext(stext, dc.norm, False);
        if((dc.w = dc.x - x) > bh) {
@@ -521,7 +583,7 @@ drawbar(void) {
                else
                        drawtext(NULL, dc.norm, False);
        }
-       XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, BW, bh, 0, 0);
+       XCopyArea(dpy, dc.drawable, barwin, dc.gc, 0, 0, bw, bh, 0, 0);
        XSync(dpy, False);
 }
 
@@ -629,7 +691,6 @@ void
 floating(void) { /* default floating layout */
        Client *c;
 
-       dozoom = False;
        for(c = clients; c; c = c->next)
                if(isvisible(c))
                        resize(c, c->x, c->y, c->w, c->h, True);
@@ -642,22 +703,11 @@ focus(Client *c) {
        if(sel && sel != c) {
                grabbuttons(sel, False);
                XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
-               if(lt->arrange == monocle)
-                       resize(sel, sel->rx, sel->ry, sel->rw, sel->rh, True);
        }
        if(c) {
                detachstack(c);
                attachstack(c);
                grabbuttons(c, True);
-               if(lt->arrange == monocle) {
-                       if(sel != c) {
-                               c->rx = c->x;
-                               c->ry = c->y;
-                               c->rw = c->w;
-                               c->rh = c->h;
-                       }
-                       resize(c, MOX, MOY, MOW, MOH, RESIZEHINTS);
-               }
        }
        sel = c;
        if(c) {
@@ -979,28 +1029,29 @@ manage(Window w, XWindowAttributes *wa) {
        c->tags = emallocz(TAGSZ);
        c->win = w;
 
-       c->x = c->rx = wa->x + sx;
-       c->y = c->ry = wa->y + sy;
-       c->w = c->rw = wa->width;
-       c->h = c->rh = wa->height;
+       /* geometry */
+       c->x = wa->x;
+       c->y = wa->y;
+       c->w = wa->width;
+       c->h = wa->height;
        c->oldborder = wa->border_width;
-
        if(c->w == sw && c->h == sh) {
                c->x = sx;
                c->y = sy;
                c->border = wa->border_width;
        }
        else {
-               if(c->x + c->w + 2 * c->border > sx + sw)
-                       c->x = sx + sw - c->w - 2 * c->border;
-               if(c->y + c->h + 2 * c->border > sy + sh)
-                       c->y = sy + sh - c->h - 2 * c->border;
-               if(c->x < sx)
-                       c->x = sx;
-               if(c->y < sy)
-                       c->y = sy;
+               if(c->x + c->w + 2 * c->border > wx + ww)
+                       c->x = wx + ww - c->w - 2 * c->border;
+               if(c->y + c->h + 2 * c->border > wy + wh)
+                       c->y = wy + wh - c->h - 2 * c->border;
+               if(c->x < wx)
+                       c->x = wx;
+               if(c->y < wy)
+                       c->y = wy;
                c->border = BORDERPX;
        }
+
        wc.border_width = c->border;
        XConfigureWindow(dpy, w, CWBorderWidth, &wc);
        XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
@@ -1050,7 +1101,11 @@ maprequest(XEvent *e) {
 
 void
 monocle(void) {
-       dozoom = False;
+       Client *c;
+
+       for(c = clients; c; c = c->next)
+               if(isvisible(c))
+                       resize(c, mox, moy, mow, moh, RESIZEHINTS);
 }
 
 void
@@ -1081,17 +1136,17 @@ movemouse(Client *c) {
                        XSync(dpy, False);
                        nx = ocx + (ev.xmotion.x - x1);
                        ny = ocy + (ev.xmotion.y - y1);
-                       if(abs(sx - nx) < SNAP)
-                               nx = sx;
-                       else if(abs((sx + sw) - (nx + c->w + 2 * c->border)) < SNAP)
-                               nx = sx + sw - c->w - 2 * c->border;
-                       if(abs(sy - ny) < SNAP)
-                               ny = sy;
-                       else if(abs((sy + sh) - (ny + c->h + 2 * c->border)) < SNAP)
-                               ny = sy + sh - c->h - 2 * c->border;
-                       if(!c->isfloating && (lt->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
+                       if(abs(wx - nx) < SNAP)
+                               nx = wx;
+                       else if(abs((wx + ww) - (nx + c->w + 2 * c->border)) < SNAP)
+                               nx = wx + ww - c->w - 2 * c->border;
+                       if(abs(wy - ny) < SNAP)
+                               ny = wy;
+                       else if(abs((wy + wh) - (ny + c->h + 2 * c->border)) < SNAP)
+                               ny = wy + wh - c->h - 2 * c->border;
+                       if(!c->isfloating && !lt->isfloating && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
                                togglefloating(NULL);
-                       if((lt->arrange == floating) || c->isfloating)
+                       if((lt->isfloating) || c->isfloating)
                                resize(c, nx, ny, c->w, c->h, False);
                        break;
                }
@@ -1250,9 +1305,9 @@ resizemouse(Client *c) {
                                nw = 1;
                        if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
                                nh = 1;
-                       if(!c->isfloating && (lt->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
+                       if(!c->isfloating && !lt->isfloating && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
                                togglefloating(NULL);
-                       if((lt->arrange == floating) || c->isfloating)
+                       if((lt->isfloating) || c->isfloating)
                                resize(c, c->x, c->y, nw, nh, True);
                        break;
                }
@@ -1268,9 +1323,9 @@ restack(void) {
        drawbar();
        if(!sel)
                return;
-       if(sel->isfloating || (lt->arrange == floating))
+       if(sel->isfloating || lt->isfloating)
                XRaiseWindow(dpy, sel->win);
-       if(lt->arrange != floating) {
+       if(!lt->isfloating) {
                wc.stack_mode = Below;
                wc.sibling = barwin;
                if(!sel->isfloating) {
@@ -1383,8 +1438,34 @@ setclientstate(Client *c, long state) {
                        PropModeReplace, (unsigned char *)data, 2);
 }
 
+double
+getdouble(const char *s) {
+       char *endp;
+       double result = 0;
+
+       switch(*s) {
+       default: 
+               result = strtod(s, &endp);
+               if(s == endp || *endp != 0)
+                       result = strtol(s, &endp, 0);
+               break;
+       case 'B': result = dc.font.height + 2; break;
+       case 'W': result = sw; break;
+       case 'H': result = sh; break;
+       }
+       return result;
+}
+
+void
+setgeom(const char *arg) {
+       applygeometry(arg);
+       updatebarpos();
+       arrange();
+}
+
 void
 setlayout(const char *arg) {
+       static Layout *revert = 0;
        unsigned int i;
 
        if(!arg)
@@ -1394,7 +1475,12 @@ setlayout(const char *arg) {
                        break;
        if(i == LENGTH(layouts))
                return;
-       lt = &layouts[i];
+       if(revert && &layouts[i] == lt)
+               lt = revert;
+       else {
+               revert = lt;
+               lt = &layouts[i];
+       }
        if(sel)
                arrange();
        else
@@ -1409,10 +1495,14 @@ setup(void) {
        /* init screen */
        screen = DefaultScreen(dpy);
        root = RootWindow(dpy, screen);
+       initfont(FONT);
+
+       /* apply default dimensions */
        sx = 0;
        sy = 0;
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
+       applygeometry(GEOMETRY);
 
        /* init atoms */
        wmatom[WMProtocols] = XInternAtom(dpy, "WM_PROTOCOLS", False);
@@ -1435,7 +1525,7 @@ setup(void) {
        dc.sel[ColBG] = getcolor(SELBGCOLOR);
        dc.sel[ColFG] = getcolor(SELFGCOLOR);
        initfont(FONT);
-       dc.h = bh = dc.font.height + 2;
+       dc.h = bh;
        dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
        dc.gc = XCreateGC(dpy, root, 0, 0);
        XSetLineAttributes(dpy, dc.gc, 1, LineSolid, CapButt, JoinMiter);
@@ -1461,7 +1551,7 @@ setup(void) {
        wa.background_pixmap = ParentRelative;
        wa.event_mask = ButtonPressMask|ExposureMask;
 
-       barwin = XCreateWindow(dpy, root, BX, BY, BW, bh, 0, DefaultDepth(dpy, screen),
+       barwin = XCreateWindow(dpy, root, bx, by, bw, bh, 0, DefaultDepth(dpy, screen),
                                CopyFromParent, DefaultVisual(dpy, screen),
                                CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
        XDefineCursor(dpy, barwin, cursor[CurNormal]);
@@ -1537,46 +1627,75 @@ textw(const char *text) {
 }
 
 void
-tile(void) {
-       unsigned int i, n, nx, ny, nw, nh, mw, th;
-       Client *c, *mc;
+tileh(void) {
+       int x, w;
+       unsigned int i, n = counttiled();
+       Client *c;
 
-       dozoom = True;
-       nx = MX;
-       ny = MY;
-       nw = 0;
-       for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
-               n++;
+       if(n == 0)
+               return;
+       c = tilemaster(n);
+       if(--n == 0)
+               return;
 
-       /* window geoms */
-       mw = (n == 1) ? MOW : MW;
-       th = (n > 1) ? TH / (n - 1) : 0;
-       if(n > 1 && th < bh)
-               th = TH;
+       x = tx;
+       w = tw / n;
+       if(w < bh)
+               w = tw;
 
-       for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next)) {
-               if(i == 0) { /* master */
-                       nw = mw - 2 * c->border;
-                       nh = MH - 2 * c->border;
-               }
-               else {  /* tile window */
-                       if(i == 1) {
-                               ny = TY;
-                               nx = TX;
-                               nw = TW - 2 * c->border;
-                       }
-                       if(i + 1 == n) /* remainder */
-                               nh = (TY + TH) - ny - 2 * c->border;
-                       else
-                               nh = th - 2 * c->border;
-               }
-               resize(c, nx, ny, nw, nh, RESIZEHINTS);
-               if((RESIZEHINTS) && ((c->h < bh) || (c->h > nh) || (c->w < bh) || (c->w > nw)))
-                       /* client doesn't accept size constraints */
-                       resize(c, nx, ny, nw, nh, False);
-               if(n > 1 && th != TH)
-                       ny = c->y + c->h + 2 * c->border;
-               i++;
+       for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
+               if(i + 1 == n) /* remainder */
+                       tileresize(c, x, ty, (tx + tw) - x - 2 * c->border, th - 2 * c->border);
+               else
+                       tileresize(c, x, ty, w - 2 * c->border, th - 2 * c->border);
+               if(w != tw)
+                       x = c->x + c->w + 2 * c->border;
+       }
+}
+
+Client *
+tilemaster(unsigned int n) {
+       Client *c = nexttiled(clients);
+
+       if(n == 1)
+               tileresize(c, mox, moy, mow - 2 * c->border, moh - 2 * c->border);
+       else
+               tileresize(c, mx, my, mw - 2 * c->border, mh - 2 * c->border);
+       return c;
+}
+
+void
+tileresize(Client *c, int x, int y, int w, int h) {
+       resize(c, x, y, w, h, RESIZEHINTS);
+       if((RESIZEHINTS) && ((c->h < bh) || (c->h > h) || (c->w < bh) || (c->w > w)))
+               /* client doesn't accept size constraints */
+               resize(c, x, y, w, h, False);
+}
+
+void
+tilev(void) {
+       int y, h;
+       unsigned int i, n = counttiled();
+       Client *c;
+
+       if(n == 0)
+               return;
+       c = tilemaster(n);
+       if(--n == 0)
+               return;
+
+       y = ty;
+       h = th / n;
+       if(h < bh)
+               h = th;
+
+       for(i = 0, c = nexttiled(c->next); c; c = nexttiled(c->next), i++) {
+               if(i + 1 == n) /* remainder */
+                       tileresize(c, tx, y, tw - 2 * c->border, (ty + th) - y - 2 * c->border);
+               else
+                       tileresize(c, tx, y, tw - 2 * c->border, h - 2 * c->border);
+               if(h != th)
+                       y = c->y + c->h + 2 * c->border;
        }
 }
 
@@ -1657,6 +1776,15 @@ unmapnotify(XEvent *e) {
 }
 
 void
+updatebarpos(void) {
+
+       if(dc.drawable != 0)
+               XFreePixmap(dpy, dc.drawable);
+       dc.drawable = XCreatePixmap(dpy, root, bw, bh, DefaultDepth(dpy, screen));
+       XMoveResizeWindow(dpy, barwin, bx, by, bw, bh);
+}
+
+void
 updatesizehints(Client *c) {
        long msize;
        XSizeHints size;
@@ -1788,7 +1916,7 @@ void
 zoom(const char *arg) {
        Client *c = sel;
 
-       if(!sel || !dozoom || sel->isfloating)
+       if(!sel || lt->isfloating || sel->isfloating)
                return;
        if(c == nexttiled(clients))
                if(!(c = nexttiled(c->next)))