JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
we need a tagset per monitor, removed tagset declaration from config.h
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 0e8c927..5610a37 100644 (file)
--- a/dwm.c
+++ b/dwm.c
 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask))
 #define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
-#define ISVISIBLE(x)            (x->tags & tagset[mon[x->mon].seltags])
-#define LENGTH(x)               (sizeof x / sizeof x[0])
-#define MAX(a, b)               ((a) > (b) ? (a) : (b))
-#define MIN(a, b)               ((a) < (b) ? (a) : (b))
+#define ISVISIBLE(M, C)         ((M) == (&mon[C->mon]) && (C->tags & M->tagset[M->seltags]))
+#define LENGTH(X)               (sizeof X / sizeof X[0])
+#define MAX(A, B)               ((A) > (B) ? (A) : (B))
+#define MIN(A, B)               ((A) < (B) ? (A) : (B))
 #define MOUSEMASK               (BUTTONMASK|PointerMotionMask)
-#define WIDTH(x)                ((x)->w + 2 * (x)->bw)
-#define HEIGHT(x)               ((x)->h + 2 * (x)->bw)
+#define WIDTH(X)                ((X)->w + 2 * (X)->bw)
+#define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
 #define TAGMASK                 ((int)((1LL << LENGTH(tags)) - 1))
-#define TEXTW(x)                (textnw(x, strlen(x)) + dc.font.height)
+#define TEXTW(X)                (textnw(X, strlen(X)) + dc.font.height)
 
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast };        /* cursor */
@@ -116,10 +116,12 @@ typedef struct {
 
 typedef struct {
        char symbol[4];
+       float mfact;
        int by, btx;          /* bar geometry */
        int wx, wy, ww, wh;   /* window area  */
        unsigned int seltags;
        unsigned int sellt;
+       unsigned int tagset[2];
        Bool showbar;
        Bool topbar;
        Window barwin;
@@ -251,7 +253,7 @@ static Display *dpy;
 static DC dc;
 static Layout *lt[] = { NULL, NULL };
 static Monitor *mon = NULL, *selmon = NULL;
-static unsigned int nmons;
+static unsigned int nmons = 0;
 static Window root;
 /* configuration, allows nested code to access above variables */
 #include "config.h"
@@ -283,7 +285,7 @@ applyrules(Client *c) {
                if(ch.res_name)
                        XFree(ch.res_name);
        }
-       c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : tagset[mon[c->mon].seltags];
+       c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : mon[c->mon].tagset[mon[c->mon].seltags];
 }
 
 Bool
@@ -493,7 +495,7 @@ configurenotify(XEvent *e) {
                updategeom();
                if(dc.drawable != 0)
                        XFreePixmap(dpy, dc.drawable);
-               dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(dpy, screen), bh, DefaultDepth(dpy, screen));
+               dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
                for(i = 0; i < nmons; i++)
                        XMoveResizeWindow(dpy, mon[i].barwin, mon[i].wx, mon[i].by, mon[i].ww, bh);
                arrange();
@@ -524,7 +526,7 @@ configurerequest(XEvent *e) {
                                c->y = sy + (sh / 2 - c->h / 2); /* center in y direction */
                        if((ev->value_mask & (CWX|CWY)) && !(ev->value_mask & (CWWidth|CWHeight)))
                                configure(c);
-                       if(ISVISIBLE(c))
+                       if(ISVISIBLE((&mon[c->mon]), c))
                                XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
                }
                else
@@ -604,7 +606,7 @@ drawbar(Monitor *m) {
        m->btx = dc.x;
        for(i = 0; i < LENGTH(tags); i++) {
                dc.w = TEXTW(tags[i]);
-               col = tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
+               col = m->tagset[m->seltags] & 1 << i ? dc.sel : dc.norm;
                drawtext(tags[i], col, urg & 1 << i);
                drawsquare(m == selmon && sel && sel->tags & 1 << i,
                           occ & 1 << i, urg & 1 << i, col);
@@ -636,6 +638,7 @@ drawbar(Monitor *m) {
                }
        }
        else {
+               dc.x = x;
                dc.w = m->ww - x;
                drawtext(NULL, dc.norm, False);
        }
@@ -718,17 +721,18 @@ expose(XEvent *e) {
        unsigned int i;
        XExposeEvent *ev = &e->xexpose;
 
-       for(i = 0; i < nmons; i++)
-               if(ev->count == 0 && (ev->window == mon[i].barwin)) {
-                       drawbar(&mon[i]);
-                       break;
-               }
+       if(ev->count == 0)
+               for(i = 0; i < nmons; i++)
+                       if(ev->window == mon[i].barwin) {
+                               drawbar(&mon[i]);
+                               break;
+                       }
 }
 
 void
 focus(Client *c) {
-       if(!c || !ISVISIBLE(c))
-               for(c = stack; c && !ISVISIBLE(c); c = c->snext);
+       if(!c || !ISVISIBLE((&mon[c->mon]), c))
+               for(c = stack; c && !ISVISIBLE(selmon, c); c = c->snext);
        if(sel && sel != c) {
                grabbuttons(sel, False);
                XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
@@ -745,8 +749,6 @@ focus(Client *c) {
        else
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
        sel = c;
-       if(c)
-               selmon = &mon[c->mon];
        drawbars();
 }
 
@@ -776,17 +778,17 @@ focusstack(const Arg *arg) {
        if(!sel)
                return;
        if(arg->i > 0) {
-               for(c = sel->next; c && !ISVISIBLE(c); c = c->next);
+               for(c = sel->next; c && !ISVISIBLE(selmon, c); c = c->next);
                if(!c)
-                       for(c = clients; c && !ISVISIBLE(c); c = c->next);
+                       for(c = clients; c && !ISVISIBLE(selmon, c); c = c->next);
        }
        else {
                for(i = clients; i != sel; i = i->next)
-                       if(ISVISIBLE(i))
+                       if(ISVISIBLE(selmon, i))
                                c = i;
                if(!c)
                        for(; i; i = i->next)
-                               if(ISVISIBLE(i))
+                               if(ISVISIBLE(selmon, i))
                                        c = i;
        }
        if(c) {
@@ -1126,7 +1128,7 @@ movemouse(const Arg *arg) {
 Client *
 nexttiled(Monitor *m, Client *c) {
        // TODO: m handling
-       for(; c && (c->isfloating || m != &mon[c->mon] || !ISVISIBLE(c)); c = c->next);
+       for(; c && (c->isfloating || !ISVISIBLE(m, c)); c = c->next);
        return c;
 }
 
@@ -1246,7 +1248,7 @@ restack(Monitor *m) {
                wc.stack_mode = Below;
                wc.sibling = m->barwin;
                for(c = stack; c; c = c->snext)
-                       if(!c->isfloating && m == &mon[c->mon] && ISVISIBLE(c)) {
+                       if(!c->isfloating && ISVISIBLE(m, c)) {
                                XConfigureWindow(dpy, c->win, CWSibling|CWStackMode, &wc);
                                wc.sibling = c->win;
                        }
@@ -1320,10 +1322,10 @@ setmfact(const Arg *arg) {
 
        if(!arg || !lt[selmon->sellt]->arrange)
                return;
-       f = arg->f < 1.0 ? arg->f + mfact : arg->f - 1.0;
+       f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
        if(f < 0.1 || f > 0.9)
                return;
-       mfact = f;
+       selmon->mfact = f;
        arrange();
 }
 
@@ -1408,9 +1410,9 @@ void
 showhide(Client *c) {
        if(!c)
                return;
-       if(ISVISIBLE(c)) { /* show clients top down */
+       if(ISVISIBLE((&mon[c->mon]), c)) { /* show clients top down */
                XMoveWindow(dpy, c->win, c->x, c->y);
-               if(!lt[selmon->sellt]->arrange || c->isfloating)
+               if(!lt[mon[c->mon].sellt]->arrange || c->isfloating)
                        resize(c, c->x, c->y, c->w, c->h);
                showhide(c->snext);
        }
@@ -1481,7 +1483,7 @@ tile(Monitor *m) {
 
        /* master */
        c = nexttiled(m, clients);
-       mw = mfact * m->ww;
+       mw = m->mfact * m->ww;
        resize(c, m->wx, m->wy, (n == 1 ? m->ww : mw) - 2 * c->bw, m->wh - 2 * c->bw);
 
        if(--n == 0)
@@ -1537,10 +1539,10 @@ toggletag(const Arg *arg) {
 
 void
 toggleview(const Arg *arg) {
-       unsigned int mask = tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
+       unsigned int mask = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
 
        if(mask) {
-               tagset[selmon->seltags] = mask;
+               selmon->tagset[selmon->seltags] = mask;
                arrange();
        }
 }
@@ -1579,30 +1581,33 @@ unmapnotify(XEvent *e) {
 void
 updategeom(void) {
 #ifdef XINERAMA
-       int di, x, y, n;
-       unsigned int dui, i = 0;
-       Bool pquery;
+       int n;
+       unsigned int i = 0;
        Client *c;
-       Window dummy;
        XineramaScreenInfo *info = NULL;
 
        /* window area geometry */
        if(XineramaIsActive(dpy) && (info = XineramaQueryScreens(dpy, &n))) {
-               nmons = (unsigned int)n;
-               for(c = clients; c; c = c->next)
-                       if(c->mon >= nmons)
-                               c->mon = nmons - 1;
-               if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor) * nmons)))
-                       die("fatal: could not realloc() %u bytes\n", sizeof(Monitor) * nmons);
-               pquery = XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
-               for(i = 0; i < nmons; i++) {
+               if(n != nmons) {
+                       for(c = clients; c; c = c->next)
+                               if(c->mon >= n)
+                                       c->mon = n - 1;
+                       if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor) * n)))
+                               die("fatal: could not realloc() %u bytes\n", sizeof(Monitor) * nmons);
+                       selmon = NULL;
+               }
+               for(i = 0; i < n ; i++) {
                        /* TODO: consider re-using XineramaScreenInfo */
                        mon[i].symbol[0] = '[';
                        mon[i].symbol[1] = '0' + info[i].screen_number;
                        mon[i].symbol[2] = ']';
                        mon[i].symbol[3] = 0;
-                       mon[i].showbar = showbar;
-                       mon[i].topbar = topbar;
+                       if(!selmon) { /* not initialised yet */
+                               mon[i].mfact = mfact;
+                               mon[i].showbar = showbar;
+                               mon[i].topbar = topbar;
+                               mon[i].tagset[0] = mon[i].tagset[1] = 1;
+                       }
                        mon[i].wx = info[i].x_org;
                        mon[i].wy = mon[i].showbar && mon[i].topbar ? info[i].y_org + bh : info[i].y_org;
                        mon[i].ww = info[i].width;
@@ -1613,24 +1618,40 @@ updategeom(void) {
                                mon[i].by = mon[i].topbar ? info[i].y_org : mon[i].wy + mon[i].wh;
                        else
                                mon[i].by = -bh;
-                       if(pquery && INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height))
-                               selmon = &mon[i];
+               }
+               nmons = (unsigned int)n;
+               if(!selmon) {
+                       selmon = &mon[0];
+                       int di, x, y;
+                       unsigned int dui;
+                       Window dummy;
+                       if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui)) 
+                               for(i = 0; i < nmons; i++)
+                                       if(INRECT(x, y, info[i].x_org, info[i].y_org, info[i].width, info[i].height)) {
+                                               selmon = &mon[i];
+                                               break;
+                                       }
                }
                XFree(info);
        }
        else
 #endif /* XINERAMA */
        {
-               nmons = 1;
-               if(!(mon = (Monitor *)realloc(mon, sizeof(Monitor))))
-                       die("fatal: could not realloc() %u bytes\n", sizeof(Monitor));
-               selmon = &mon[0];
-               mon[0].symbol[0] = '[';
-               mon[0].symbol[1] = '0';
-               mon[0].symbol[2] = ']';
-               mon[0].symbol[3] = 0;
-               mon[0].showbar = showbar;
-               mon[0].topbar = topbar;
+               if(!mon) {
+                       nmons = 1;
+                       if(!(mon = (Monitor *)malloc(sizeof(Monitor))))
+                               die("fatal: could not malloc() %u bytes\n", sizeof(Monitor));
+               }
+               if(!selmon) {
+                       mon[0].symbol[0] = '[';
+                       mon[0].symbol[1] = '0';
+                       mon[0].symbol[2] = ']';
+                       mon[0].symbol[3] = 0;
+                       mon[0].mfact = mfact;
+                       mon[0].showbar = showbar;
+                       mon[0].topbar = topbar;
+                       mon[0].tagset[0] = mon[0].tagset[1] = 1;
+               }
                mon[0].wx = sx;
                mon[0].wy = mon[0].showbar && mon[0].topbar ? sy + bh : sy;
                mon[0].ww = sw;
@@ -1641,6 +1662,7 @@ updategeom(void) {
                        mon[0].by = mon[0].topbar ? sy : mon[0].wy + mon[0].wh;
                else
                        mon[0].by = -bh;
+               selmon = &mon[0];
        }
 }
 
@@ -1740,11 +1762,11 @@ updatewmhints(Client *c) {
 
 void
 view(const Arg *arg) {
-       if((arg->ui & TAGMASK) == tagset[selmon->seltags])
+       if((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
                return;
        selmon->seltags ^= 1; /* toggle sel tagset */
        if(arg->ui & TAGMASK)
-               tagset[selmon->seltags] = arg->ui & TAGMASK;
+               selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
        arrange();
 }