JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
focus new master when closing master
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 74a28a3..f123400 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -184,6 +184,7 @@ static void monocle(Monitor *m);
 static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static Client *nexttiled(Client *c);
+static Client *nextvisible(Client *c);
 static void pop(Client *);
 static void propertynotify(XEvent *e);
 static void quit(const Arg *arg);
@@ -273,11 +274,11 @@ static void window_set_opaque(Client *c);
 static void window_set_translucent(Client *c);
 void
 window_set_opaque(Client *c) {
-       XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
+       XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
 }
 void
 window_set_translucent(Client *c) {
-       XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
+       XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
 }
 
 
@@ -410,8 +411,13 @@ arrangemon(Monitor *m) {
 
 void
 attach(Client *c) {
-       c->next = c->mon->clients;
-       c->mon->clients = c;
+       if (c->mon->sel) {
+               c->next = c->mon->sel->next;
+               c->mon->sel->next = c;
+       } else {
+               c->next = c->mon->clients;
+               c->mon->clients = c;
+       }
 }
 
 void
@@ -676,16 +682,30 @@ detach(Client *c) {
        *tc = c->next;
 }
 
+// NOTE: the stack is for z-order and most-recently-focused
+// only mon->clients determines position in visible layout
 void
 detachstack(Client *c) {
-       Client **tc, *t;
-
-       for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
-       *tc = c->snext;
-
+       Client *prev = NULL, *next_sel = NULL, *i;
+       for(i = c->mon->stack; i && i != c; i = i->snext) {
+               prev = i;
+       }
        if(c == c->mon->sel) {
-               for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
-               c->mon->sel = t;
+               // find last visible window before c
+               // WARNING if you detach() before detachstack() this will select last visible window
+               for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
+                       next_sel = i;
+               // failing that, find first visible window (besides c)
+               if (!next_sel)
+                       for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
+                       if (i != c)
+                               next_sel = i;
+               c->mon->sel = next_sel;
+       }
+       if (prev) {
+               prev->snext = c->snext;
+       } else {
+               c->mon->stack = c->snext;
        }
 }
 
@@ -798,10 +818,6 @@ focus(Client *c) {
        /* was if(selmon->sel) */
        if(selmon->sel && selmon->sel != c)
                unfocus(selmon->sel, False);
-       if(selmon->sel && c!=selmon->sel && c && (!root || (selmon->sel->win!=root && c->win!=root)) )
-               window_set_opaque(selmon->sel);
-       if(c && c!=selmon->sel && (!root || (c->win!=root)) )
-               window_set_translucent(c);
        if(c) {
                if(c->mon != selmon)
                        selmon = c->mon;
@@ -819,8 +835,8 @@ focus(Client *c) {
        }
        selmon->sel = c;
        drawbars();
-       if(c)
-               window_set_translucent(c);
+       if(c && (!root || (c->win!=root)) )
+               window_set_opaque(c);
 }
 
 void
@@ -1089,7 +1105,7 @@ manage(Window w, XWindowAttributes *wa) {
        c->mon->sel = c;
        arrange(c->mon);
        XMapWindow(dpy, c->win);
-       focus(NULL);
+       focus(c);
 }
 
 void
@@ -1212,6 +1228,12 @@ nexttiled(Client *c) {
        return c;
 }
 
+Client *
+nextvisible(Client *c) {
+       for(; c && !ISVISIBLE(c); c = c->next);
+       return c;
+}
+
 void
 pop(Client *c) {
        detach(c);
@@ -1415,8 +1437,8 @@ sendmon(Client *c, Monitor *m) {
        if(c->mon == m)
                return;
        unfocus(c, True);
-       detach(c);
        detachstack(c);
+       detach(c);
        c->mon = m;
        c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
        attach(c);
@@ -1712,6 +1734,8 @@ void
 unfocus(Client *c, Bool setfocus) {
        if(!c)
                return;
+       if(!root || c->win!=root)
+               window_set_translucent(c);
        grabbuttons(c, False);
        XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
        if(setfocus) {
@@ -1726,8 +1750,8 @@ unmanage(Client *c, Bool destroyed) {
        XWindowChanges wc;
 
        /* The server grab construct avoids race conditions. */
-       detach(c);
        detachstack(c);
+       detach(c);
        if(!destroyed) {
                wc.border_width = c->oldbw;
                XGrabServer(dpy);
@@ -1740,7 +1764,7 @@ unmanage(Client *c, Bool destroyed) {
                XUngrabServer(dpy);
        }
        free(c);
-       focus(NULL);
+       focus(selmon ? selmon->sel : NULL);
        updateclientlist();
        arrange(m);
 }