JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
unfocus on slow sloppy monitor focus
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 4c6af64..2d44442 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -43,7 +43,8 @@
 /* macros */
 #define BUTTONMASK              (ButtonPressMask|ButtonReleaseMask)
 #define CLEANMASK(mask)         (mask & ~(numlockmask|LockMask) & (ShiftMask|ControlMask|Mod1Mask|Mod2Mask|Mod3Mask|Mod4Mask|Mod5Mask))
-#define INRECT(X,Y,RX,RY,RW,RH) ((X) >= (RX) && (X) < (RX) + (RW) && (Y) >= (RY) && (Y) < (RY) + (RH))
+#define INTERSECT(x,y,w,h,m)    (MAX(0, MIN((x)+(w),(m)->wx+(m)->ww) - MAX((x),(m)->wx)) \
+                               * MAX(0, MIN((y)+(h),(m)->wy+(m)->wh) - MAX((y),(m)->wy)))
 #define ISVISIBLE(C)            ((C->tags & C->mon->tagset[C->mon->seltags]))
 #define LENGTH(X)               (sizeof X / sizeof X[0])
 #define MAX(A, B)               ((A) > (B) ? (A) : (B))
@@ -199,12 +200,13 @@ static void manage(Window w, XWindowAttributes *wa);
 static void mappingnotify(XEvent *e);
 static void maprequest(XEvent *e);
 static void monocle(Monitor *m);
+static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static Client *nexttiled(Client *c);
 static void pop(Client *);
 static void propertynotify(XEvent *e);
-static Monitor *ptrtomon(int x, int y);
 static void quit(const Arg *arg);
+static Monitor *recttomon(int x, int y, int w, int h);
 static void resize(Client *c, int x, int y, int w, int h, Bool interact);
 static void resizeclient(Client *c, int x, int y, int w, int h);
 static void resizemouse(const Arg *arg);
@@ -270,6 +272,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
        [KeyPress] = keypress,
        [MappingNotify] = mappingnotify,
        [MapRequest] = maprequest,
+       [MotionNotify] = motionnotify,
        [PropertyNotify] = propertynotify,
        [UnmapNotify] = unmapnotify
 };
@@ -394,9 +397,10 @@ arrange(Monitor *m) {
                showhide(m->stack);
        else for(m = mons; m; m = m->next)
                showhide(m->stack);
-       if(m)
+       if(m) {
                arrangemon(m);
-       else for(m = mons; m; m = m->next)
+               restack(m);
+       } else for(m = mons; m; m = m->next)
                arrangemon(m);
 }
 
@@ -405,7 +409,6 @@ arrangemon(Monitor *m) {
        strncpy(m->ltsymbol, m->lt[m->sellt]->symbol, sizeof m->ltsymbol);
        if(m->lt[m->sellt]->arrange)
                m->lt[m->sellt]->arrange(m);
-       restack(m);
 }
 
 void
@@ -1201,6 +1204,22 @@ monocle(Monitor *m) {
 }
 
 void
+motionnotify(XEvent *e) {
+       static Monitor *mon = NULL;
+       Monitor *m;
+       XMotionEvent *ev = &e->xmotion;
+
+       if(ev->window != root)
+               return;
+       if((m = recttomon(ev->x_root, ev->y_root, 1, 1)) != mon && mon) {
+               unfocus(selmon->sel, True);
+               selmon = m;
+               focus(NULL);
+       }
+       mon = m;
+}
+
+void
 movemouse(const Arg *arg) {
        int x, y, ocx, ocy, nx, ny;
        Client *c;
@@ -1248,7 +1267,7 @@ movemouse(const Arg *arg) {
                }
        } while(ev.type != ButtonRelease);
        XUngrabPointer(dpy, CurrentTime);
-       if((m = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
+       if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
                sendmon(c, m);
                selmon = m;
                focus(NULL);
@@ -1305,21 +1324,24 @@ propertynotify(XEvent *e) {
        }
 }
 
-Monitor *
-ptrtomon(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 selmon;
-}
-
 void
 quit(const Arg *arg) {
        running = False;
 }
 
+Monitor *
+recttomon(int x, int y, int w, int h) {
+       Monitor *m, *r = selmon;
+       int a, area = 0;
+
+       for(m = mons; m; m = m->next)
+               if((a = INTERSECT(x, y, w, h, m)) > area) {
+                       area = a;
+                       r = m;
+               }
+       return r;
+}
+
 void
 resize(Client *c, int x, int y, int w, int h, Bool interact) {
        if(applysizehints(c, &x, &y, &w, &h, interact))
@@ -1383,7 +1405,7 @@ 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 = ptrtomon(c->x + c->w / 2, c->y + c->h / 2)) != selmon) {
+       if((m = recttomon(c->x, c->y, c->w, c->h)) != selmon) {
                sendmon(c, m);
                selmon = m;
                focus(NULL);
@@ -1610,9 +1632,8 @@ setup(void) {
                        PropModeReplace, (unsigned char *) netatom, NetLast);
        /* select for events */
        wa.cursor = cursor[CurNormal];
-       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask
-                       |EnterWindowMask|LeaveWindowMask|StructureNotifyMask
-                       |PropertyChangeMask;
+       wa.event_mask = SubstructureRedirectMask|SubstructureNotifyMask|ButtonPressMask|PointerMotionMask
+                       |EnterWindowMask|LeaveWindowMask|StructureNotifyMask|PropertyChangeMask;
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
        grabkeys();
@@ -1807,6 +1828,8 @@ updatebars(void) {
                .event_mask = ButtonPressMask|ExposureMask
        };
        for(m = mons; m; m = m->next) {
+               if (m->barwin)
+                       continue;
                m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
                                          CopyFromParent, DefaultVisual(dpy, screen),
                                          CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
@@ -2051,7 +2074,7 @@ wintomon(Window w) {
        Monitor *m;
 
        if(w == root && getrootptr(&x, &y))
-               return ptrtomon(x, y);
+               return recttomon(x, y, 1, 1);
        for(m = mons; m; m = m->next)
                if(w == m->barwin)
                        return m;
@@ -2109,7 +2132,7 @@ zoom(const Arg *arg) {
 int
 main(int argc, char *argv[]) {
        if(argc == 2 && !strcmp("-v", argv[1]))
-               die("dwm-"VERSION", © 2006-2011 dwm engineers, see LICENSE for details\n");
+               die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
        else if(argc != 1)
                die("usage: dwm [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())