JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
fixed several issues with focus handling via mouse, also added sending clients to...
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 5625237..9779654 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -1,3 +1,4 @@
+//#define XINULATOR /* debug, simulates dual head */
 /* See LICENSE file for copyright and license details.
  *
  * dynamic window manager is designed like any other X client as well. It is
@@ -165,7 +166,7 @@ static void detach(Client *c);
 static void detachstack(Client *c);
 static void die(const char *errstr, ...);
 static void drawbar(Monitor *m);
-static void drawbars();
+static void drawbars(void);
 static void drawsquare(Bool filled, Bool empty, Bool invert, unsigned long col[ColLast]);
 static void drawtext(const char *text, unsigned long col[ColLast], Bool invert);
 static void enternotify(XEvent *e);
@@ -175,6 +176,9 @@ static void focusin(XEvent *e);
 static void focusstack(const Arg *arg);
 static Client *getclient(Window w);
 static unsigned long getcolor(const char *colstr);
+static Monitor *getmonitor(Window w);
+static Monitor *getmonitorxy(int x, int y);
+static Bool getrootpointer(int *x, int *y);
 static long getstate(Window w);
 static Bool gettextprop(Window w, Atom atom, char *text, unsigned int size);
 static void grabbuttons(Client *c, Bool focused);
@@ -196,6 +200,7 @@ static void resizemouse(const Arg *arg);
 static void restack(Monitor *m);
 static void run(void);
 static void scan(void);
+static void sendmon(Client *c, Monitor *m);
 static void setclientstate(Client *c, long state);
 static void setlayout(const Arg *arg);
 static void setmfact(const Arg *arg);
@@ -210,6 +215,7 @@ static void togglebar(const Arg *arg);
 static void togglefloating(const Arg *arg);
 static void toggletag(const Arg *arg);
 static void toggleview(const Arg *arg);
+static void unfocus(Client *c);
 static void unmanage(Client *c);
 static void unmapnotify(XEvent *e);
 static void updategeom(void);
@@ -375,14 +381,14 @@ arrange(void) {
 
 void
 attach(Client *c) {
-       c->next = selmon->clients;
-       selmon->clients = c;
+       c->next = c->mon->clients;
+       c->mon->clients = c;
 }
 
 void
 attachstack(Client *c) {
-       c->snext = selmon->stack;
-       selmon->stack = c;
+       c->snext = c->mon->stack;
+       c->mon->stack = c;
 }
 
 void
@@ -390,9 +396,16 @@ buttonpress(XEvent *e) {
        unsigned int i, x, click;
        Arg arg = {0};
        Client *c;
+       Monitor *m;
        XButtonPressedEvent *ev = &e->xbutton;
 
        click = ClkRootWin;
+       /* focus monitor if necessary */
+       if((m = getmonitor(ev->window)) && m != selmon) {
+               unfocus(selmon->sel);
+               selmon = m;
+               focus(NULL);
+       }
        if(ev->window == selmon->barwin && ev->x >= selmon->btx) {
                i = 0;
                x = selmon->btx;
@@ -643,7 +656,7 @@ drawbar(Monitor *m) {
        }
        else
                x = dc.x;
-       if(m == selmon) {
+       if(m == selmon) { /* status is only drawn on selected monitor */
                dc.w = TEXTW(stext);
                dc.x = m->ww - dc.w;
                if(dc.x < x) {
@@ -651,27 +664,26 @@ drawbar(Monitor *m) {
                        dc.w = m->ww - x;
                }
                drawtext(stext, dc.norm, False);
-               if((dc.w = dc.x - x) > bh) {
-                       dc.x = x;
-                       if(selmon->sel) {
-                               drawtext(selmon->sel->name, dc.sel, False);
-                               drawsquare(selmon->sel->isfixed, selmon->sel->isfloating, False, dc.sel);
-                       }
-                       else
-                               drawtext(NULL, dc.norm, False);
-               }
        }
        else {
+               dc.x = m->ww;
+       }
+       if((dc.w = dc.x - x) > bh) {
                dc.x = x;
-               dc.w = m->ww - x;
-               drawtext(NULL, dc.norm, False);
+               if(m->sel) {
+                       col = m == selmon ? dc.sel : dc.norm;
+                       drawtext(m->sel->name, col, False);
+                       drawsquare(m->sel->isfixed, m->sel->isfloating, False, col);
+               }
+               else
+                       drawtext(NULL, dc.norm, False);
        }
        XCopyArea(dpy, dc.drawable, m->barwin, dc.gc, 0, 0, m->ww, bh, 0, 0);
        XSync(dpy, False);
 }
 
 void
-drawbars() {
+drawbars(void) {
        Monitor *m;
 
        for(m = mons; m; m = m->next)
@@ -730,10 +742,15 @@ drawtext(const char *text, unsigned long col[ColLast], Bool invert) {
 void
 enternotify(XEvent *e) {
        Client *c;
+       Monitor *m;
        XCrossingEvent *ev = &e->xcrossing;
 
        if((ev->mode != NotifyNormal || ev->detail == NotifyInferior) && ev->window != root)
                return;
+       if((m = getmonitor(ev->window)) && m != selmon) {
+               unfocus(selmon->sel);
+               selmon = m;
+       }
        if((c = getclient(ev->window)))
                focus(c);
        else
@@ -745,23 +762,19 @@ expose(XEvent *e) {
        Monitor *m;
        XExposeEvent *ev = &e->xexpose;
 
-       if(ev->count == 0)
-               for(m = mons; m; m = m->next)
-                       if(ev->window == m->barwin) {
-                               drawbar(m);
-                               break;
-                       }
+       if(ev->count == 0 && (m = getmonitor(ev->window)))
+               drawbar(m);
 }
 
 void
 focus(Client *c) {
        if(!c || !ISVISIBLE(c))
                for(c = selmon->stack; c && !ISVISIBLE(c); c = c->snext);
-       if(selmon->sel && selmon->sel != c) {
-               grabbuttons(selmon->sel, False);
-               XSetWindowBorder(dpy, selmon->sel->win, dc.norm[ColBorder]);
-       }
+       if(selmon->sel)
+               unfocus(selmon->sel);
        if(c) {
+               if(c->mon != selmon)
+                       selmon = c->mon;
                if(c->isurgent)
                        clearurgent(c);
                detachstack(c);
@@ -792,9 +805,11 @@ focusmon(const Arg *arg) {
 
        for(i = 0, m = mons; m; m = m->next, i++)
                if(i == arg->ui) {
+                       if(m == selmon)
+                               return;
+                       unfocus(selmon->sel);
                        selmon = m;
                        focus(NULL);
-                       drawbars();
                        break;
                }
 }
@@ -828,12 +843,14 @@ focusstack(const Arg *arg) {
 
 Client *
 getclient(Window w) {
-       Client *c = NULL;
+       Client *c;
        Monitor *m;
 
        for(m = mons; m; m = m->next)
-               for(c = m->clients; c && c->win != w; c = c->next);
-       return c;
+               for(c = m->clients; c; c = c->next)
+                       if(c->win == w)
+                               return c;
+       return NULL;
 }
 
 unsigned long
@@ -846,6 +863,40 @@ getcolor(const char *colstr) {
        return color.pixel;
 }
 
+Monitor *
+getmonitor(Window w) {
+       int x, y;
+       Client *c;
+       Monitor *m;
+
+       if(w == root && getrootpointer(&x, &y))
+               return getmonitorxy(x, y);
+       for(m = mons; m; m = m->next)
+               if(w == m->barwin)
+                       return m;
+       if((c = getclient(w)))
+               return c->mon;
+       return NULL;
+}
+
+Monitor *
+getmonitorxy(int x, int y) {
+       Monitor *m;
+
+       for(m = mons; m; m = m->next)
+               if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh))
+                       return m;
+       return NULL;
+}
+
+Bool
+getrootpointer(int *x, int *y) {
+       int di;
+       unsigned int dui;
+       Window dummy;
+       return XQueryPointer(dpy, root, &dummy, &dummy, x, y, &di, &di, &dui);
+}
+
 long
 getstate(Window w) {
        int format, status;
@@ -1044,8 +1095,8 @@ manage(Window w, XWindowAttributes *wa) {
                        c->y = sy + sh - HEIGHT(c);
                c->x = MAX(c->x, sx);
                /* only fix client y-offset, if the client center might cover the bar */
-               c->y = MAX(c->y, ((selmon->by == 0) && (c->x + (c->w / 2) >= selmon->wx)
-                          && (c->x + (c->w / 2) < selmon->wx + selmon->ww)) ? bh : sy);
+               c->y = MAX(c->y, ((c->mon->by == 0) && (c->x + (c->w / 2) >= c->mon->wx)
+                          && (c->x + (c->w / 2) < c->mon->wx + c->mon->ww)) ? bh : sy);
                c->bw = borderpx;
        }
 
@@ -1107,10 +1158,9 @@ monocle(Monitor *m) {
 
 void
 movemouse(const Arg *arg) {
-       int x, y, ocx, ocy, di, nx, ny;
-       unsigned int dui;
+       int x, y, ocx, ocy, nx, ny;
        Client *c;
-       Window dummy;
+       Monitor *m;
        XEvent ev;
 
        if(!(c = selmon->sel))
@@ -1121,7 +1171,8 @@ movemouse(const Arg *arg) {
        if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
        None, cursor[CurMove], CurrentTime) != GrabSuccess)
                return;
-       XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui);
+       if(!getrootpointer(&x, &y))
+               return;
        do {
                XMaskEvent(dpy, MOUSEMASK|ExposureMask|SubstructureRedirectMask, &ev);
                switch (ev.type) {
@@ -1154,11 +1205,12 @@ movemouse(const Arg *arg) {
        }
        while(ev.type != ButtonRelease);
        XUngrabPointer(dpy, CurrentTime);
+       if((m = getmonitorxy(c->x + c->w / 2, c->y + c->h / 2)) != selmon)
+               sendmon(c, m);
 }
 
 Client *
 nexttiled(Client *c) {
-       // TODO: m handling
        for(; c && (c->isfloating || !ISVISIBLE(c)); c = c->next);
        return c;
 }
@@ -1212,8 +1264,7 @@ resize(Client *c, int x, int y, int w, int h) {
                c->w = wc.width = w;
                c->h = wc.height = h;
                wc.border_width = c->bw;
-               XConfigureWindow(dpy, c->win,
-                               CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
+               XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
                configure(c);
                XSync(dpy, False);
        }
@@ -1224,6 +1275,7 @@ resizemouse(const Arg *arg) {
        int ocx, ocy;
        int nw, nh;
        Client *c;
+       Monitor *m;
        XEvent ev;
 
        if(!(c = selmon->sel))
@@ -1262,6 +1314,8 @@ resizemouse(const Arg *arg) {
        XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->bw - 1, c->h + c->bw - 1);
        XUngrabPointer(dpy, CurrentTime);
        while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
+       if((m = getmonitorxy(c->x + c->w / 2, c->y + c->h / 2)) != selmon)
+               sendmon(c, m);
 }
 
 void
@@ -1271,10 +1325,10 @@ restack(Monitor *m) {
        XWindowChanges wc;
 
        drawbars();
-       if(!selmon->sel)
+       if(!m->sel)
                return;
-       if(m == selmon && (selmon->sel->isfloating || !lt[m->sellt]->arrange))
-               XRaiseWindow(dpy, selmon->sel->win);
+       if(m->sel->isfloating || !lt[m->sellt]->arrange)
+               XRaiseWindow(dpy, m->sel->win);
        if(lt[m->sellt]->arrange) {
                wc.stack_mode = Below;
                wc.sibling = m->barwin;
@@ -1327,6 +1381,20 @@ scan(void) {
 }
 
 void
+sendmon(Client *c, Monitor *m) {
+       if(c->mon == m)
+               return;
+       detach(c);
+       detachstack(c);
+       c->mon = m;
+       c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
+       attach(c);
+       attachstack(c);
+       focus(NULL);
+       arrange();
+}
+
+void
 setclientstate(Client *c, long state) {
        long data[] = {state, None};
 
@@ -1475,12 +1543,14 @@ tag(const Arg *arg) {
 void
 tagmon(const Arg *arg) {
        unsigned int i;
+       Client *c;
        Monitor *m;
 
+       if(!(c = selmon->sel))
+               return;
        for(i = 0, m = mons; m; m = m->next, i++)
                if(i == arg->ui) {
-                       selmon->sel->m = m;
-                       arrange();
+                       sendmon(c, m);
                        break;
                }
 }
@@ -1574,6 +1644,15 @@ toggleview(const Arg *arg) {
 }
 
 void
+unfocus(Client *c) {
+       if(!c)
+               return;
+       grabbuttons(c, False);
+       XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
+       XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+}
+
+void
 unmanage(Client *c) {
        XWindowChanges wc;
 
@@ -1584,8 +1663,13 @@ unmanage(Client *c) {
        XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
        detach(c);
        detachstack(c);
-       if(selmon->sel == c)
+       if(c->mon->sel == c) {
+               /* TODO: consider separate the next code into a function or into detachstack? */
+               Client *tc;
+               for(tc = c->mon->stack; tc && !ISVISIBLE(tc); tc = tc->snext);
+               c->mon->sel = tc;
                focus(NULL);
+       }
        XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
        setclientstate(c, WithdrawnState);
        free(c);
@@ -1642,7 +1726,9 @@ updategeom(void) {
        Client *c;
        Monitor *newmons = NULL, *m, *tm;
 
-#ifdef XINERAMA
+#ifdef XINULATOR
+       n = 2;
+#elif defined(XINERAMA)
        XineramaScreenInfo *info = NULL;
 
        if(XineramaIsActive(dpy))
@@ -1656,7 +1742,23 @@ updategeom(void) {
        }
 
        /* initialise monitor(s) */
-#ifdef XINERAMA
+#ifdef XINULATOR
+       if(1) {
+               m = newmons;
+               m->screen_number = 0;
+               m->wx = sx;
+               m->my = m->wy = sy;
+               m->ww = sw;
+               m->mh = m->wh = sh / 2;
+               m = newmons->next;
+               m->screen_number = 1;
+               m->wx = sx;
+               m->my = m->wy = sy + sh / 2;
+               m->ww = sw;
+               m->mh = m->wh = sh / 2;
+       }
+       else
+#elif defined(XINERAMA)
        if(XineramaIsActive(dpy)) {
                for(i = 0, m = newmons; m; m = m->next, i++) {
                        m->screen_number = info[i].screen_number;
@@ -1691,52 +1793,23 @@ updategeom(void) {
                m->showbar = showbar;
                m->topbar = topbar;
                updatebarpos(m);
-               /* reassign all clients with same screen number */
-               for(tm = mons; tm; tm = tm->next)
-                       if(tm->screen_number == m->screen_number) {
-                               m->clients = tm->clients;
-                               m->stack = tm->stack;
-                               tm->clients = NULL;
-                               tm->stack = NULL;
-                               for(c = m->clients; c; c = c->next)
-                                       c->mon = m;
-                       }
        }
 
        /* reassign left over clients of disappeared monitors */
-       for(tm = mons; tm; tm = tm->next) {
+       for(tm = mons; tm; tm = tm->next)
                while(tm->clients) {
-                       c = tm->clients->next;
-                       tm->clients->next = newmons->clients;
-                       tm->clients->mon = newmons;
-                       newmons->clients = tm->clients;
-                       tm->clients = c;
+                       c = tm->clients;
+                       tm->clients = c->next;
+                       detachstack(c);
+                       c->mon = newmons;
+                       attach(c);
+                       attachstack(c);
                }
-               while(tm->stack) {
-                       c = tm->stack->snext;
-                       tm->stack->snext = newmons->stack;
-                       newmons->stack = tm->stack;
-                       tm->stack = c;
-               }
-       }
 
        /* select focused monitor */
-       if(!selmon) {
-               selmon = newmons;
-               int di, x, y;
-               unsigned int dui;
-               Window dummy;
-               if(XQueryPointer(dpy, root, &dummy, &dummy, &x, &y, &di, &di, &dui)) 
-                       for(m = newmons; m; m = m->next)
-                               if(INRECT(x, y, m->wx, m->wy, m->ww, m->wh)) {
-                                       selmon = m;
-                                       break;
-                               }
-       }
-
-       /* final assignment of new monitors */
        cleanupmons();
        mons = newmons;
+       selmon = getmonitor(root);
 }
 
 void
@@ -1811,7 +1884,7 @@ updatetitle(Client *c) {
 }
 
 void
-updatestatus() {
+updatestatus(void) {
        if(!gettextprop(root, XA_WM_NAME, stext, sizeof(stext)))
                strcpy(stext, "dwm-"VERSION);
        drawbar(selmon);