JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Eric Mertens patch to mainstream dwm, however this needs testing
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 8762269..ab9c11b 100644 (file)
--- a/dwm.c
+++ b/dwm.c
  *
  * To understand everything else, start reading main().
  */
+#include "dwm.h"
+
 #include <errno.h>
 #include <locale.h>
-#include <regex.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #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>
-#include <X11/Xlib.h>
 #include <X11/Xproto.h>
 #include <X11/Xutil.h>
 
 #define CLEANMASK(mask)                (mask & ~(numlockmask | LockMask))
 #define MOUSEMASK              (BUTTONMASK | PointerMotionMask)
 
-/* enums */
-enum { BarTop, BarBot, BarOff };                       /* bar position */
-enum { CurNormal, CurResize, CurMove, CurLast };       /* cursor */
-enum { ColBorder, ColFG, ColBG, ColLast };             /* color */
-enum { NetSupported, NetWMName, NetLast };             /* EWMH atoms */
-enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
-
-/* typedefs */
-typedef struct Client Client;
-struct Client {
-       char name[256];
-       int x, y, w, h;
-       int rx, ry, rw, rh; /* revert geometry */
-       int basew, baseh, incw, inch, maxw, maxh, minw, minh;
-       int minax, maxax, minay, maxay;
-       long flags; 
-       unsigned int border, oldborder;
-       Bool isbanned, isfixed, ismax, isfloating, wasfloating;
-       Bool *tags;
-       Client *next;
-       Client *prev;
-       Client *snext;
-       Window win;
-};
-
-typedef struct {
-       int x, y, w, h;
-       unsigned long norm[ColLast];
-       unsigned long sel[ColLast];
-       Drawable drawable;
-       GC gc;
-       struct {
-               int ascent;
-               int descent;
-               int height;
-               XFontSet set;
-               XFontStruct *xfont;
-       } font;
-} DC; /* draw context */
-
-typedef struct {
-       unsigned long mod;
-       KeySym keysym;
-       void (*func)(const char *arg);
-       const char *arg;
-} Key;
-
-typedef struct {
-       const char *symbol;
-       void (*arrange)(void);
-} Layout;
-
+/* local typedefs */
 typedef struct {
        const char *prop;
        const char *tags;
@@ -110,91 +61,12 @@ typedef struct {
        regex_t *tagregex;
 } Regs;
 
-/* forward declarations */
-void applyrules(Client *c);
-void arrange(void);
-void attach(Client *c);
-void attachstack(Client *c);
-void ban(Client *c);
-void buttonpress(XEvent *e);
-void checkotherwm(void);
-void cleanup(void);
-void compileregs(void);
-void configure(Client *c);
-void configurenotify(XEvent *e);
-void configurerequest(XEvent *e);
-void destroynotify(XEvent *e);
-void detach(Client *c);
-void detachstack(Client *c);
-void drawbar(void);
-void drawsquare(Bool filled, Bool empty, unsigned long col[ColLast]);
-void drawtext(const char *text, unsigned long col[ColLast]);
-void *emallocz(unsigned int size);
-void enternotify(XEvent *e);
-void eprint(const char *errstr, ...);
-void expose(XEvent *e);
-void floating(void); /* default floating layout */
-void focus(Client *c);
-void focusnext(const char *arg);
-void focusprev(const char *arg);
-Client *getclient(Window w);
-unsigned long getcolor(const char *colstr);
-long getstate(Window w);
-Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
-void grabbuttons(Client *c, Bool focused);
-unsigned int idxoftag(const char *tag);
-void initfont(const char *fontstr);
-Bool isarrange(void (*func)());
-Bool isoccupied(unsigned int t);
-Bool isprotodel(Client *c);
-Bool isvisible(Client *c);
-void keypress(XEvent *e);
-void killclient(const char *arg);
-void leavenotify(XEvent *e);
-void manage(Window w, XWindowAttributes *wa);
-void mappingnotify(XEvent *e);
-void maprequest(XEvent *e);
-void movemouse(Client *c);
-Client *nexttiled(Client *c);
-void propertynotify(XEvent *e);
-void quit(const char *arg);
-void resize(Client *c, int x, int y, int w, int h, Bool sizehints);
-void resizemouse(Client *c);
-void restack(void);
-void run(void);
-void scan(void);
-void setclientstate(Client *c, long state);
-void setlayout(const char *arg);
-void setmwfact(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 togglebar(const char *arg);
-void togglefloating(const char *arg);
-void togglemax(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 view(const char *arg);
-int xerror(Display *dpy, XErrorEvent *ee);
-int xerrordummy(Display *dsply, XErrorEvent *ee);
-int xerrorstart(Display *dsply, XErrorEvent *ee);
-void zoom(const char *arg);
-
 /* variables */
 char stext[256];
 double mwfact;
 int screen, sx, sy, sw, sh, wax, way, waw, wah;
 int (*xerrorxlib)(Display *, XErrorEvent *);
-unsigned int bh, bpos, ntags;
+unsigned int bh, bpos;
 unsigned int blw = 0;
 unsigned int ltidx = 0; /* default */
 unsigned int nlayouts = 0;
@@ -217,7 +89,6 @@ void (*handler[LASTEvent]) (XEvent *) = {
 Atom wmatom[WMLast], netatom[NetLast];
 Bool otherwm, readin;
 Bool running = True;
-Bool *seltags;
 Bool selscreen = True;
 Client *clients = NULL;
 Client *sel = NULL;
@@ -231,6 +102,11 @@ Regs *regs = NULL;
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
+/* Statically define the number of tags. */
+unsigned int ntags = sizeof tags / sizeof tags[0];
+Bool seltags[sizeof tags / sizeof tags[0]] = {[0] = True};
+Bool prevtags[sizeof tags / sizeof tags[0]] = {[0] = True};
+
 /* functions*/
 void
 applyrules(Client *c) {
@@ -260,8 +136,7 @@ applyrules(Client *c) {
        if(ch.res_name)
                XFree(ch.res_name);
        if(!matched)
-               for(i = 0; i < ntags; i++)
-                       c->tags[i] = seltags[i];
+               memcpy(c->tags, seltags, sizeof seltags);
 }
 
 void
@@ -341,7 +216,7 @@ buttonpress(XEvent *e) {
                        movemouse(c);
                }
                else if(ev->button == Button2) {
-                       if(isarrange(tile) && !c->isfixed && c->isfloating)
+                       if(ISTILE && !c->isfixed && c->isfloating)
                                togglefloating(NULL);
                        else
                                zoom(NULL);
@@ -392,7 +267,6 @@ cleanup(void) {
        XFreeCursor(dpy, cursor[CurMove]);
        XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
        XSync(dpy, False);
-       free(seltags);
 }
 
 void
@@ -998,14 +872,13 @@ leavenotify(XEvent *e) {
 
 void
 manage(Window w, XWindowAttributes *wa) {
-       unsigned int i;
        Client *c, *t = NULL;
        Window trans;
        Status rettrans;
        XWindowChanges wc;
 
        c = emallocz(sizeof(Client));
-       c->tags = emallocz(ntags * sizeof(Bool));
+       c->tags = emallocz(sizeof seltags);
        c->win = w;
        c->x = wa->x;
        c->y = wa->y;
@@ -1040,8 +913,7 @@ manage(Window w, XWindowAttributes *wa) {
        if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
                for(t = clients; t && t->win != trans; t = t->next);
        if(t)
-               for(i = 0; i < ntags; i++)
-                       c->tags[i] = t->tags[i];
+               memcpy(c->tags, t->tags, sizeof seltags);
        applyrules(c);
        if(!c->isfloating)
                c->isfloating = (rettrans == Success) || c->isfixed;
@@ -1161,42 +1033,45 @@ quit(const char *arg) {
 void
 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
        double dx, dy, max, min, ratio;
-       XWindowChanges wc; 
+       XWindowChanges wc;
 
        if(sizehints) {
-               if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0 && (w - c->basew) > 0) {
-                       dx = (double)(w - c->basew);
-                       dy = (double)(h - c->baseh);
-                       min = (double)(c->minax) / (double)(c->minay);
-                       max = (double)(c->maxax) / (double)(c->maxay);
-                       ratio = dx / dy;
-                       if(max > 0 && min > 0 && ratio > 0) {
-                               if(ratio < min) {
-                                       dy = (dx * min + dy) / (min * min + 1);
-                                       dx = dy * min;
-                                       w = (int)dx + c->basew;
-                                       h = (int)dy + c->baseh;
-                               }
-                               else if(ratio > max) {
-                                       dy = (dx * min + dy) / (max * max + 1);
-                                       dx = dy * min;
-                                       w = (int)dx + c->basew;
-                                       h = (int)dy + c->baseh;
-                               }
-                       }
+               /* set minimum possible */
+               if (w < 1)
+                       w = 1;
+               if (h < 1)
+                       h = 1;
+
+               /* 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)
+                               w = h * c->maxax / c->maxay;
+                       else if (w * c->minay < h * c->minax)
+                               h = w * c->minay / c->minax;
                }
-               if(c->minw && w < c->minw)
+
+               /* adjust for increment value */
+               if(c->incw)
+                       w -= w % c->incw;
+               if(c->inch)
+                       h -= h % c->inch;
+
+               /* restore base dimensions */
+               w += c->basew;
+               h += c->baseh;
+
+               if(c->minw > 0 && w < c->minw)
                        w = c->minw;
-               if(c->minh && h < c->minh)
+               if(c->minh > 0 && h < c->minh)
                        h = c->minh;
-               if(c->maxw && w > c->maxw)
+               if(c->maxw > 0 && w > c->maxw)
                        w = c->maxw;
-               if(c->maxh && h > c->maxh)
+               if(c->maxh > 0 && h > c->maxh)
                        h = c->maxh;
-               if(c->incw)
-                       w -= (w - c->basew) % c->incw;
-               if(c->inch)
-                       h -= (h - c->baseh) % c->inch;
        }
        if(w <= 0 || h <= 0)
                return;
@@ -1398,7 +1273,7 @@ void
 setmwfact(const char *arg) {
        double delta;
 
-       if(!isarrange(tile))
+       if(!ISTILE)
                return;
        /* arg handling, manipulate mwfact */
        if(arg == NULL)
@@ -1418,6 +1293,7 @@ setmwfact(const char *arg) {
 
 void
 setup(void) {
+       int d;
        unsigned int i, j, mask;
        Window w;
        XModifierKeymap *modmap;
@@ -1465,9 +1341,6 @@ setup(void) {
 
        /* init tags */
        compileregs();
-       for(ntags = 0; tags[ntags]; ntags++);
-       seltags = emallocz(sizeof(Bool) * ntags);
-       seltags[0] = True;
 
        /* init appearance */
        dc.norm[ColBorder] = getcolor(NORMBORDERCOLOR);
@@ -1507,7 +1380,7 @@ setup(void) {
                XSetFont(dpy, dc.gc, dc.font.xfont->fid);
 
        /* multihead support */
-       selscreen = XQueryPointer(dpy, root, &w, &w, &i, &i, &i, &i, &mask);
+       selscreen = XQueryPointer(dpy, root, &w, &w, &d, &d, &d, &d, &mask);
 }
 
 void
@@ -1567,7 +1440,7 @@ textw(const char *text) {
 void
 tile(void) {
        unsigned int i, n, nx, ny, nw, nh, mw, th;
-       Client *c;
+       Client *c, *mc;
 
        for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
                n++;
@@ -1580,7 +1453,8 @@ tile(void) {
 
        nx = wax;
        ny = way;
-       for(i = 0, c = nexttiled(clients); c; c = nexttiled(c->next), i++) {
+       nw = 0; /* gcc stupidity requires this */
+       for(i = 0, c = mc = nexttiled(clients); c; c = nexttiled(c->next), i++) {
                c->ismax = False;
                if(i == 0) { /* master */
                        nw = mw - 2 * c->border;
@@ -1589,9 +1463,9 @@ tile(void) {
                else {  /* tile window */
                        if(i == 1) {
                                ny = way;
-                               nx += mw;
+                               nx += mc->w + 2 * mc->border;
+                               nw = waw - nx - 2 * c->border;
                        }
-                       nw = waw - mw - 2 * c->border;
                        if(i + 1 == n) /* remainder */
                                nh = (way + wah) - ny - 2 * c->border;
                        else
@@ -1599,7 +1473,7 @@ tile(void) {
                }
                resize(c, nx, ny, nw, nh, RESIZEHINTS);
                if(n > 1 && th != wah)
-                       ny += nh + 2 * c->border;
+                       ny = c->y + c->h + 2 * c->border;
        }
 }
 
@@ -1673,7 +1547,7 @@ toggleview(const char *arg) {
        seltags[i] = !seltags[i];
        for(j = 0; j < ntags && !seltags[j]; j++);
        if(j == ntags)
-               seltags[i] = True; /* cannot toggle last view */
+               seltags[i] = True; /* at least one tag must be viewed */
        arrange();
 }
 
@@ -1837,6 +1711,7 @@ void
 view(const char *arg) {
        unsigned int i;
 
+       memcpy(prevtags, seltags, sizeof seltags);
        for(i = 0; i < ntags; i++)
                seltags[i] = arg == NULL;
        i = idxoftag(arg);
@@ -1846,10 +1721,20 @@ view(const char *arg) {
 }
 
 void
+viewprevtag(const char *arg) {
+       static Bool tmptags[sizeof tags / sizeof tags[0]];
+
+       memcpy(tmptags, seltags, sizeof seltags);
+       memcpy(seltags, prevtags, sizeof seltags);
+       memcpy(prevtags, tmptags, sizeof seltags);
+       arrange();
+}
+
+void
 zoom(const char *arg) {
        Client *c;
 
-       if(!sel || !isarrange(tile) || sel->isfloating)
+       if(!sel || !ISTILE || sel->isfloating)
                return;
        if((c = sel) == nexttiled(clients))
                if(!(c = nexttiled(c->next)))