JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed Gottox' mail address
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index ff9e66a..0e47626 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -60,6 +60,7 @@ enum { NetSupported, NetWMName, NetLast };            /* EWMH atoms */
 enum { WMProtocols, WMDelete, WMName, WMState, WMLast };/* default atoms */
 
 /* typedefs */
+typedef struct Monitor Monitor;
 typedef struct Client Client;
 struct Client {
        char name[256];
@@ -74,7 +75,7 @@ struct Client {
        Client *prev;
        Client *snext;
        Window win;
-       int monitor;
+       Monitor *monitor;
 };
 
 typedef struct {
@@ -99,7 +100,6 @@ typedef struct {
        const char *arg;
 } Key;
 
-typedef struct Monitor Monitor;
 typedef struct {
        const char *symbol;
        void (*arrange)(Monitor *);
@@ -118,7 +118,6 @@ typedef struct {
 } Regs;
 
 struct Monitor {
-       unsigned int id;
        int sx, sy, sw, sh, wax, way, wah, waw;
        double mwfact;
        Bool *seltags;
@@ -130,7 +129,7 @@ struct Monitor {
 
 /* function declarations */
 void applyrules(Client *c);
-void arrange(void);
+void arrange(Monitor *m);
 void attach(Client *c);
 void attachstack(Client *c);
 void ban(Client *c);
@@ -144,9 +143,9 @@ void configurerequest(XEvent *e);
 void destroynotify(XEvent *e);
 void detach(Client *c);
 void detachstack(Client *c);
-void drawbar(void);
-void drawsquare(Monitor *, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
-void drawtext(Monitor *, const char *text, unsigned long col[ColLast], Bool invert);
+void drawbar(Monitor *m);
+void drawsquare(Monitor *m, Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
+void drawtext(Monitor *m, const char *text, unsigned long col[ColLast], Bool invert);
 void *emallocz(unsigned int size);
 void enternotify(XEvent *e);
 void eprint(const char *errstr, ...);
@@ -158,29 +157,31 @@ void focusnext(const char *arg);
 void focusprev(const char *arg);
 Client *getclient(Window w);
 unsigned long getcolor(const char *colstr);
+Monitor *getmonitor(Window barwin);
 long getstate(Window w);
 Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 void grabbuttons(Client *c, Bool focused);
 void grabkeys(void);
 unsigned int idxoftag(const char *tag);
 void initfont(const char *fontstr);
-Bool isoccupied(unsigned int monitor, unsigned int t);
+Bool isoccupied(Monitor *monitor, unsigned int t);
 Bool isprotodel(Client *c);
-Bool isurgent(unsigned int monitor, unsigned int t);
-Bool isvisible(Client *c, int monitor);
+Bool isurgent(Monitor *monitor, unsigned int t);
+Bool isvisible(Client *c, Monitor *m);
 void keypress(XEvent *e);
 void killclient(const char *arg);
 void manage(Window w, XWindowAttributes *wa);
 void mappingnotify(XEvent *e);
 void maprequest(XEvent *e);
+Monitor *monitorat(void);
 void movemouse(Client *c);
-Client *nexttiled(Client *c, int monitor);
+Client *nexttiled(Client *c, Monitor *monitor);
 void propertynotify(XEvent *e);
 void quit(const char *arg);
 void reapply(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 restack(Monitor *m);
 void run(void);
 void scan(void);
 void setclientstate(Client *c, long state);
@@ -209,14 +210,13 @@ int xerror(Display *dpy, XErrorEvent *ee);
 int xerrordummy(Display *dsply, XErrorEvent *ee);
 int xerrorstart(Display *dsply, XErrorEvent *ee);
 void zoom(const char *arg);
-int monitorat(void);
 void movetomonitor(const char *arg);
 void selectmonitor(const char *arg);
 
 /* variables */
 char stext[256];
 int mcount = 1;
-int selmonitor = 0;
+Monitor *selmonitor;
 int screen;
 int (*xerrorxlib)(Display *, XErrorEvent *);
 unsigned int bh, bpos;
@@ -276,7 +276,7 @@ applyrules(Client *c) {
                if(regs[i].propregex && !regexec(regs[i].propregex, buf, 1, &tmp, 0)) {
                        if (rules[i].monitor >= 0 && rules[i].monitor < mcount) {
                                matched_monitor = True;
-                               c->monitor = rules[i].monitor;
+                               c->monitor = &monitors[rules[i].monitor];
                        }
 
                        c->isfloating = rules[i].isfloating;
@@ -292,13 +292,14 @@ applyrules(Client *c) {
        if(ch.res_name)
                XFree(ch.res_name);
        if(!matched_tag)
-               memcpy(c->tags, monitors[monitorat()].seltags, sizeof initags);
+               memcpy(c->tags, monitorat()->seltags, sizeof initags);
        if (!matched_monitor)
                c->monitor = monitorat();
 }
 
 void
-arrange(void) {
+arrange(Monitor *m) {
+       unsigned int i;
        Client *c;
 
        for(c = clients; c; c = c->next)
@@ -307,9 +308,13 @@ arrange(void) {
                else
                        ban(c);
 
-       monitors[selmonitor].layout->arrange(&monitors[selmonitor]);
+       if(m)
+               m->layout->arrange(m);
+       else
+               for(i = 0; i < mcount; i++)
+                       monitors[i].layout->arrange(&monitors[i]);
        focus(NULL);
-       restack();
+       restack(m);
 }
 
 void
@@ -330,7 +335,7 @@ void
 ban(Client *c) {
        if(c->isbanned)
                return;
-       XMoveWindow(dpy, c->win, c->x + 3 * monitors[c->monitor].sw, c->y);
+       XMoveWindow(dpy, c->win, c->x + 3 * c->monitor->sw, c->y);
        c->isbanned = True;
 }
 
@@ -340,7 +345,7 @@ buttonpress(XEvent *e) {
        Client *c;
        XButtonPressedEvent *ev = &e->xbutton;
 
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        if(ev->window == m->barwin) {
                x = 0;
@@ -370,7 +375,7 @@ buttonpress(XEvent *e) {
                if(CLEANMASK(ev->state) != MODKEY)
                        return;
                if(ev->button == Button1) {
-                       restack();
+                       restack(c->monitor);
                        movemouse(c);
                }
                else if(ev->button == Button2) {
@@ -380,7 +385,7 @@ buttonpress(XEvent *e) {
                                zoom(NULL);
                }
                else if(ev->button == Button3 && !c->isfixed) {
-                       restack();
+                       restack(c->monitor);
                        resizemouse(c);
                }
        }
@@ -474,7 +479,7 @@ configure(Client *c) {
 void
 configurenotify(XEvent *e) {
        XConfigureEvent *ev = &e->xconfigure;
-       Monitor *m = &monitors[selmonitor];
+       Monitor *m = selmonitor;
 
        if(ev->window == root && (ev->width != m->sw || ev->height != m->sh)) {
                /* TODO -- update Xinerama dimensions here */
@@ -484,7 +489,7 @@ configurenotify(XEvent *e) {
                dc.drawable = XCreatePixmap(dpy, root, DisplayWidth(root, screen), bh, DefaultDepth(dpy, screen));
                XResizeWindow(dpy, m->barwin, m->sw, bh);
                updatebarpos(m);
-               arrange();
+               arrange(m);
        }
 }
 
@@ -495,7 +500,7 @@ configurerequest(XEvent *e) {
        XWindowChanges wc;
 
        if((c = getclient(ev->window))) {
-               Monitor *m = &monitors[c->monitor];
+               Monitor *m = c->monitor;
                if(ev->value_mask & CWBorderWidth)
                        c->border = ev->border_width;
                if(c->isfixed || c->isfloating || (floating == m->layout->arrange)) {
@@ -562,55 +567,51 @@ detachstack(Client *c) {
 }
 
 void
-drawbar(void) {
-       int i, j, x;
+drawbar(Monitor *m) {
+       int j, x;
        Client *c;
 
-       for(i = 0; i < mcount; i++) {
-               Monitor *m = &monitors[i];
-               dc.x = 0;
-               for(c = stack; c && !isvisible(c, i); c = c->snext);
-               fprintf(stderr, "m%d %s\n", i, c ? c->name : "NIL");
-               for(j = 0; j < LENGTH(tags); j++) {
-                       dc.w = textw(tags[j]);
-                       if(m->seltags[j]) {
-                               drawtext(m, tags[j], dc.sel, isurgent(i, j));
-                               drawsquare(m, c && c->tags[j] && c->monitor == i,
-                                               isoccupied(i, j), isurgent(i, j), dc.sel);
-                       }
-                       else {
-                               drawtext(m, tags[j], dc.norm, isurgent(i, j));
-                               drawsquare(m, c && c->tags[j] && c->monitor == i,
-                                               isoccupied(i, j), isurgent(i, j), dc.norm);
-                       }
-                       dc.x += dc.w;
+       dc.x = 0;
+       for(c = stack; c && !isvisible(c, m); c = c->snext);
+       for(j = 0; j < LENGTH(tags); j++) {
+               dc.w = textw(tags[j]);
+               if(m->seltags[j]) {
+                       drawtext(m, tags[j], dc.sel, isurgent(m, j));
+                       drawsquare(m, c && c->tags[j] && c->monitor == m,
+                                       isoccupied(m, j), isurgent(m, j), dc.sel);
                }
-               dc.w = blw;
-               drawtext(m, m->layout->symbol, dc.norm, False);
-               x = dc.x + dc.w;
-               if(i == selmonitor) {
-                       dc.w = textw(stext);
-                       dc.x = m->sw - dc.w;
-                       if(dc.x < x) {
-                               dc.x = x;
-                               dc.w = m->sw - x;
-                       }
-                       drawtext(m, stext, dc.norm, False);
+               else {
+                       drawtext(m, tags[j], dc.norm, isurgent(m, j));
+                       drawsquare(m, c && c->tags[j] && c->monitor == m,
+                                       isoccupied(m, j), isurgent(m, j), dc.norm);
                }
-               else
-                       dc.x = m->sw;
-               if((dc.w = dc.x - x) > bh) {
+               dc.x += dc.w;
+       }
+       dc.w = blw;
+       drawtext(m, m->layout->symbol, dc.norm, False);
+       x = dc.x + dc.w;
+       if(m == selmonitor) {
+               dc.w = textw(stext);
+               dc.x = m->sw - dc.w;
+               if(dc.x < x) {
                        dc.x = x;
-                       if(c) {
-                               drawtext(m, c->name, dc.sel, False);
-                               drawsquare(m, False, c->isfloating, False, dc.sel);
-                       }
-                       else
-                               drawtext(m, NULL, dc.norm, False);
+                       dc.w = m->sw - x;
                }
-               XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->sw, bh, 0, 0);
-               XSync(dpy, False);
+               drawtext(m, stext, dc.norm, False);
        }
+       else
+               dc.x = m->sw;
+       if((dc.w = dc.x - x) > bh) {
+               dc.x = x;
+               if(c) {
+                       drawtext(m, c->name, dc.sel, False);
+                       drawsquare(m, False, c->isfloating, False, dc.sel);
+               }
+               else
+                       drawtext(m, NULL, dc.norm, False);
+       }
+       XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->sw, bh, 0, 0);
+       XSync(dpy, False);
 }
 
 void
@@ -696,7 +697,7 @@ enternotify(XEvent *e) {
                focus(c);
        else {
                selmonitor = monitorat();
-               fprintf(stderr, "updating selmonitor %d\n", selmonitor);
+               fprintf(stderr, "updating selmonitor %d\n", selmonitor - monitors);
                focus(NULL);
        }
 }
@@ -713,12 +714,11 @@ eprint(const char *errstr, ...) {
 
 void
 expose(XEvent *e) {
+       Monitor *m;
        XExposeEvent *ev = &e->xexpose;
 
-       if(ev->count == 0) {
-               if(ev->window == monitors[selmonitor].barwin)
-                       drawbar();
-       }
+       if(ev->count == 0 && (m = getmonitor(ev->window)))
+               drawbar(m);
 }
 
 void
@@ -727,17 +727,14 @@ floating(Monitor *m) { /* default floating layout */
 
        domwfact = dozoom = False;
        for(c = clients; c; c = c->next)
-               if(isvisible(c, m->id))
+               if(isvisible(c, m))
                        resize(c, c->x, c->y, c->w, c->h, True);
 }
 
 void
 focus(Client *c) {
-       Monitor *m;
-
        if(c)
                selmonitor = c->monitor;
-       m = &monitors[selmonitor];
        if(!c || (c && !isvisible(c, selmonitor)))
                for(c = stack; c && !isvisible(c, c->monitor); c = c->snext);
        if(sel && sel != c) {
@@ -750,16 +747,14 @@ focus(Client *c) {
                grabbuttons(c, True);
        }
        sel = c;
-       drawbar();
        if(c) {
                XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
                XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
                selmonitor = c->monitor;
        }
-       else {
+       else
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
-               drawbar();
-       }
+       drawbar(selmonitor);
 }
 
 void
@@ -781,7 +776,7 @@ focusnext(const char *arg) {
                for(c = clients; c && !isvisible(c, selmonitor); c = c->next);
        if(c) {
                focus(c);
-               restack();
+               restack(c->monitor);
        }
 }
 
@@ -798,7 +793,7 @@ focusprev(const char *arg) {
        }
        if(c) {
                focus(c);
-               restack();
+               restack(c->monitor);
        }
 }
 
@@ -820,6 +815,16 @@ getcolor(const char *colstr) {
        return color.pixel;
 }
 
+Monitor *
+getmonitor(Window barwin) {
+       unsigned int i;
+
+       for(i = 0; i < mcount; i++)
+               if(monitors[i].barwin == barwin)
+                       return &monitors[i];
+       return NULL;
+}
+
 long
 getstate(Window w) {
        int format, status;
@@ -981,7 +986,7 @@ initfont(const char *fontstr) {
 }
 
 Bool
-isoccupied(unsigned int monitor, unsigned int t) {
+isoccupied(Monitor *monitor, unsigned int t) {
        Client *c;
 
        for(c = clients; c; c = c->next)
@@ -1006,7 +1011,7 @@ isprotodel(Client *c) {
 }
 
 Bool
-isurgent(unsigned int monitor, unsigned int t) {
+isurgent(Monitor *monitor, unsigned int t) {
        Client *c;
 
        for(c = clients; c; c = c->next)
@@ -1016,13 +1021,13 @@ isurgent(unsigned int monitor, unsigned int t) {
 }
 
 Bool
-isvisible(Client *c, int monitor) {
+isvisible(Client *c, Monitor *m) {
        unsigned int i;
 
-       if(c->monitor != monitor)
+       if(c->monitor != m)
                return False;
        for(i = 0; i < LENGTH(tags); i++)
-               if(c->tags[i] && monitors[c->monitor].seltags[i])
+               if(c->tags[i] && c->monitor->seltags[i])
                        return True;
        return False;
 }
@@ -1077,7 +1082,7 @@ manage(Window w, XWindowAttributes *wa) {
 
        applyrules(c);
 
-       m = &monitors[c->monitor];
+       m = c->monitor;
 
        c->x = wa->x + m->sx;
        c->y = wa->y + m->sy;
@@ -1121,7 +1126,7 @@ manage(Window w, XWindowAttributes *wa) {
        ban(c);
        XMapWindow(dpy, c->win);
        setclientstate(c, NormalState);
-       arrange();
+       arrange(m);
 }
 
 void
@@ -1146,7 +1151,7 @@ maprequest(XEvent *e) {
                manage(ev->window, &wa);
 }
 
-int
+Monitor *
 monitorat() {
        int i, x, y;
        Window win;
@@ -1156,10 +1161,10 @@ monitorat() {
        for(i = 0; i < mcount; i++) {
                if((x >= monitors[i].sx && x < monitors[i].sx + monitors[i].sw)
                && (y >= monitors[i].sy && y < monitors[i].sy + monitors[i].sh)) {
-                       return i;
+                       return &monitors[i];
                }
        }
-       return 0;
+       return NULL;
 }
 
 void
@@ -1172,7 +1177,7 @@ movemouse(Client *c) {
 
        ocx = nx = c->x;
        ocy = ny = c->y;
-       m = &monitors[c->monitor];
+       m = c->monitor;
        if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
                        None, cursor[CurMove], CurrentTime) != GrabSuccess)
                return;
@@ -1200,7 +1205,7 @@ movemouse(Client *c) {
                                ny = m->way;
                        else if(abs((m->way + m->wah) - (ny + c->h + 2 * c->border)) < SNAP)
                                ny = m->way + m->wah - c->h - 2 * c->border;
-                       if((m->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
+                       if(!c->isfloating && (m->layout->arrange != floating) && (abs(nx - c->x) > SNAP || abs(ny - c->y) > SNAP))
                                togglefloating(NULL);
                        if((m->layout->arrange == floating) || c->isfloating)
                                resize(c, nx, ny, c->w, c->h, False);
@@ -1210,7 +1215,7 @@ movemouse(Client *c) {
 }
 
 Client *
-nexttiled(Client *c, int monitor) {
+nexttiled(Client *c, Monitor *monitor) {
        for(; c && (c->isfloating || !isvisible(c, monitor)); c = c->next);
        return c;
 }
@@ -1225,24 +1230,24 @@ propertynotify(XEvent *e) {
                return; /* ignore */
        if((c = getclient(ev->window))) {
                switch (ev->atom) {
-                       default: break;
-                       case XA_WM_TRANSIENT_FOR:
-                               XGetTransientForHint(dpy, c->win, &trans);
-                               if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
-                                       arrange();
-                               break;
-                       case XA_WM_NORMAL_HINTS:
-                               updatesizehints(c);
-                               break;
-                       case XA_WM_HINTS:
-                               updatewmhints(c);
-                               drawbar();
-                               break;
+               default: break;
+               case XA_WM_TRANSIENT_FOR:
+                       XGetTransientForHint(dpy, c->win, &trans);
+                       if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
+                               arrange(c->monitor);
+                       break;
+               case XA_WM_NORMAL_HINTS:
+                       updatesizehints(c);
+                       break;
+               case XA_WM_HINTS:
+                       updatewmhints(c);
+                       drawbar(c->monitor);
+                       break;
                }
                if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
                        updatetitle(c);
                        if(c == sel)
-                               drawbar();
+                               drawbar(c->monitor);
                }
        }
 }
@@ -1261,7 +1266,7 @@ reapply(const char *arg) {
                memcpy(c->tags, zerotags, sizeof zerotags);
                applyrules(c);
        }
-       arrange();
+       arrange(NULL);
 }
 
 void
@@ -1269,8 +1274,7 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
        Monitor *m;
        XWindowChanges wc;
 
-       m = &monitors[c->monitor];
-
+       m = c->monitor;
        if(sizehints) {
                /* set minimum possible */
                if (w < 1)
@@ -1319,6 +1323,7 @@ resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
                x = m->sx;
        if(y + h + 2 * c->border < m->sy)
                y = m->sy;
+       fprintf(stderr, "resize %d %d %d %d (%d %d %d %d)\n", x, y , w, h, m->sx, m->sy, m->sw, m->sh);
        if(c->x != x || c->y != y || c->w != w || c->h != h) {
                c->x = wc.x = x;
                c->y = wc.y = y;
@@ -1340,7 +1345,7 @@ resizemouse(Client *c) {
 
        ocx = c->x;
        ocy = c->y;
-       m = &monitors[c->monitor];
+       m = c->monitor;
        if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
                        None, cursor[CurResize], CurrentTime) != GrabSuccess)
                return;
@@ -1365,7 +1370,7 @@ resizemouse(Client *c) {
                                nw = 1;
                        if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
                                nh = 1;
-                       if((m->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
+                       if(!c->isfloating && (m->layout->arrange != floating) && (abs(nw - c->w) > SNAP || abs(nh - c->h) > SNAP))
                                togglefloating(NULL);
                        if((m->layout->arrange == floating) || c->isfloating)
                                resize(c, c->x, c->y, nw, nh, True);
@@ -1375,31 +1380,28 @@ resizemouse(Client *c) {
 }
 
 void
-restack(void) {
-       unsigned int i;
+restack(Monitor *m) {
        Client *c;
        XEvent ev;
        XWindowChanges wc;
 
-       drawbar();
+       drawbar(m);
        if(!sel)
                return;
-       if(sel->isfloating || (monitors[selmonitor].layout->arrange == floating))
+       if(sel->isfloating || (m->layout->arrange == floating))
                XRaiseWindow(dpy, sel->win);
-       if(monitors[selmonitor].layout->arrange != floating) {
+       if(m->layout->arrange != floating) {
                wc.stack_mode = Below;
-               wc.sibling = monitors[selmonitor].barwin;
+               wc.sibling = m->barwin;
                if(!sel->isfloating) {
                        XConfigureWindow(dpy, sel->win, CWSibling | CWStackMode, &wc);
                        wc.sibling = sel->win;
                }
-               for(i = 0; i < mcount; i++) {
-                       for(c = nexttiled(clients, i); c; c = nexttiled(c->next, i)) {
-                               if(c == sel)
-                                       continue;
-                               XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
-                               wc.sibling = c->win;
-                       }
+               for(c = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
+                       if(c == sel)
+                               continue;
+                       XConfigureWindow(dpy, c->win, CWSibling | CWStackMode, &wc);
+                       wc.sibling = c->win;
                }
        }
        XSync(dpy, False);
@@ -1456,7 +1458,7 @@ run(void) {
                                        }
                                break;
                        }
-                       drawbar();
+                       drawbar(selmonitor);
                }
                while(XPending(dpy)) {
                        XNextEvent(dpy, &ev);
@@ -1504,7 +1506,7 @@ setclientstate(Client *c, long state) {
 void
 setlayout(const char *arg) {
        unsigned int i;
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        if(!arg) {
                m->layout++;
@@ -1520,16 +1522,16 @@ setlayout(const char *arg) {
                m->layout = &layouts[i];
        }
        if(sel)
-               arrange();
+               arrange(m);
        else
-               drawbar();
+               drawbar(m);
 }
 
 void
 setmwfact(const char *arg) {
        double delta;
 
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        if(!domwfact)
                return;
@@ -1546,7 +1548,7 @@ setmwfact(const char *arg) {
                else if(m->mwfact > 0.9)
                        m->mwfact = 0.9;
        }
-       arrange();
+       arrange(m);
 }
 
 void
@@ -1570,10 +1572,9 @@ setup(void) {
        cursor[CurMove] = XCreateFontCursor(dpy, XC_fleur);
 
        // init screens/monitors first
-       mcount = 1;
        if((isxinerama = XineramaIsActive(dpy)))
                info = XineramaQueryScreens(dpy, &mcount);
-       monitors = emallocz(mcount * sizeof(Monitor));
+       selmonitor = monitors = emallocz(mcount * sizeof(Monitor));
 
        screen = DefaultScreen(dpy);
        root = RootWindow(dpy, screen);
@@ -1601,9 +1602,8 @@ setup(void) {
        for(i = 0; i < mcount; i++) {
                /* init geometry */
                m = &monitors[i];
-               m->id = i;
 
-               if (mcount != 1 && isxinerama) {
+               if(mcount != 1 && isxinerama) {
                        m->sx = info[i].x_org;
                        m->sy = info[i].y_org;
                        m->sw = info[i].width;
@@ -1651,6 +1651,8 @@ setup(void) {
                                | EnterWindowMask | LeaveWindowMask | StructureNotifyMask;
                XChangeWindowAttributes(dpy, root, CWEventMask | CWCursor, &wa);
                XSelectInput(dpy, root, wa.event_mask);
+
+               drawbar(m);
        }
        if(info)
                XFree(info);
@@ -1662,7 +1664,7 @@ setup(void) {
        compileregs();
 
        selmonitor = monitorat();
-       fprintf(stderr, "selmonitor == %d\n", selmonitor);
+       fprintf(stderr, "selmonitor == %d\n", selmonitor - monitors);
 }
 
 void
@@ -1698,7 +1700,7 @@ tag(const char *arg) {
        for(i = 0; i < LENGTH(tags); i++)
                sel->tags[i] = (NULL == arg);
        sel->tags[idxoftag(arg)] = True;
-       arrange();
+       arrange(sel->monitor);
 }
 
 unsigned int
@@ -1723,10 +1725,10 @@ tile(Monitor *m) {
        Client *c, *mc;
 
        domwfact = dozoom = True;
-
-       nx = ny = nw = 0; /* gcc stupidity requires this */
-
-       for(n = 0, c = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id))
+       nx = m->wax;
+       ny = m->way;
+       nw = 0;
+       for(n = 0, c = nexttiled(clients, m); c; c = nexttiled(c->next, m))
                n++;
 
        /* window geoms */
@@ -1735,7 +1737,7 @@ tile(Monitor *m) {
        if(n > 1 && th < bh)
                th = m->wah;
 
-       for(i = 0, c = mc = nexttiled(clients, m->id); c; c = nexttiled(c->next, m->id)) {
+       for(i = 0, c = mc = nexttiled(clients, m); c; c = nexttiled(c->next, m)) {
                if(i == 0) { /* master */
                        nx = m->wax;
                        ny = m->way;
@@ -1753,24 +1755,25 @@ tile(Monitor *m) {
                        else
                                nh = th - 2 * c->border;
                }
+               fprintf(stderr, "tile %d %d %d %d\n", nx, ny, nw, nh);
                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 != m->wah)
                        ny = c->y + c->h + 2 * c->border;
-
                i++;
        }
 }
+
 void
 togglebar(const char *arg) {
        if(bpos == BarOff)
                bpos = (BARPOS == BarOff) ? BarTop : BARPOS;
        else
                bpos = BarOff;
-       updatebarpos(&monitors[monitorat()]);
-       arrange();
+       updatebarpos(monitorat());
+       arrange(monitorat());
 }
 
 void
@@ -1780,7 +1783,7 @@ togglefloating(const char *arg) {
        sel->isfloating = !sel->isfloating;
        if(sel->isfloating)
                resize(sel, sel->x, sel->y, sel->w, sel->h, True);
-       arrange();
+       arrange(sel->monitor);
 }
 
 void
@@ -1794,21 +1797,20 @@ toggletag(const char *arg) {
        for(j = 0; j < LENGTH(tags) && !sel->tags[j]; j++);
        if(j == LENGTH(tags))
                sel->tags[i] = True; /* at least one tag must be enabled */
-       arrange();
+       arrange(sel->monitor);
 }
 
 void
 toggleview(const char *arg) {
        unsigned int i, j;
-
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        i = idxoftag(arg);
        m->seltags[i] = !m->seltags[i];
        for(j = 0; j < LENGTH(tags) && !m->seltags[j]; j++);
        if(j == LENGTH(tags))
                m->seltags[i] = True; /* at least one tag must be viewed */
-       arrange();
+       arrange(m);
 }
 
 void
@@ -1821,6 +1823,7 @@ unban(Client *c) {
 
 void
 unmanage(Client *c) {
+       Monitor *m = c->monitor;
        XWindowChanges wc;
 
        wc.border_width = c->oldborder;
@@ -1839,7 +1842,7 @@ unmanage(Client *c) {
        XSync(dpy, False);
        XSetErrorHandler(xerror);
        XUngrabServer(dpy);
-       arrange();
+       arrange(m);
 }
 
 void
@@ -1981,7 +1984,7 @@ void
 view(const char *arg) {
        unsigned int i;
        Bool tmp[LENGTH(tags)];
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        for(i = 0; i < LENGTH(tags); i++)
                tmp[i] = (NULL == arg);
@@ -1989,7 +1992,7 @@ view(const char *arg) {
        if(memcmp(m->seltags, tmp, sizeof initags) != 0) {
                memcpy(m->prevtags, m->seltags, sizeof initags);
                memcpy(m->seltags, tmp, sizeof initags);
-               arrange();
+               arrange(m);
        }
 }
 
@@ -1997,12 +2000,12 @@ void
 viewprevtag(const char *arg) {
        static Bool tmp[LENGTH(tags)];
 
-       Monitor *m = &monitors[monitorat()];
+       Monitor *m = monitorat();
 
        memcpy(tmp, m->seltags, sizeof initags);
        memcpy(m->seltags, m->prevtags, sizeof initags);
        memcpy(m->prevtags, tmp, sizeof initags);
-       arrange();
+       arrange(m);
 }
 
 void
@@ -2017,24 +2020,40 @@ zoom(const char *arg) {
        detach(c);
        attach(c);
        focus(c);
-       arrange();
+       arrange(c->monitor);
 }
 
 void
 movetomonitor(const char *arg) {
-       if (sel) {
-               sel->monitor = arg ? atoi(arg) : (sel->monitor+1) % mcount;
+       int i;
 
-               memcpy(sel->tags, monitors[sel->monitor].seltags, sizeof initags);
-               resize(sel, monitors[sel->monitor].wax, monitors[sel->monitor].way, sel->w, sel->h, True);
-               arrange();
+       if (sel)
+               return;
+       if(arg)
+               i = atoi(arg);
+       else {
+               for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
+               i++;
        }
+       sel->monitor = &monitors[i % mcount];
+
+       memcpy(sel->tags, sel->monitor->seltags, sizeof initags);
+       resize(sel, sel->monitor->wax, sel->monitor->way, sel->w, sel->h, True);
+       arrange(sel->monitor);
 }
 
 void
 selectmonitor(const char *arg) {
-       Monitor *m = &monitors[arg ? atoi(arg) : (monitorat()+1) % mcount];
+       int i;
+       Monitor *m;
 
+       if(arg)
+               i = atoi(arg);
+       else {
+               for(i = 0; &monitors[i] != sel->monitor && i < mcount; i++);
+               i++;
+       }
+       m = &monitors[i % mcount];
        XWarpPointer(dpy, None, root, 0, 0, 0, 0, m->wax+m->waw/2, m->way+m->wah/2);
        focus(NULL);
 }
@@ -2042,9 +2061,9 @@ selectmonitor(const char *arg) {
 
 int
 main(int argc, char *argv[]) {
+       fprintf(stderr, "%u\n", sizeof("jsjsjsjsjssjsj"));
        if(argc == 2 && !strcmp("-v", argv[1]))
-               eprint("dwm-"VERSION", © 2006-2008 Anselm R. Garbe, Sander van Dijk, "
-                      "Jukka Salmi, Premysl Hruby, Szabolcs Nagy, Christof Musik\n");
+               eprint("dwm-"VERSION", © 2006-2008 dwm engineers, see LICENSE for details\n");
        else if(argc != 1)
                eprint("usage: dwm [-v]\n");
 
@@ -2054,7 +2073,6 @@ main(int argc, char *argv[]) {
 
        checkotherwm();
        setup();
-       drawbar();
        scan();
        run();
        cleanup();