JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
applied Sanders max_and_focus.patch
[dwm.git] / view.c
diff --git a/view.c b/view.c
index ac336e3..86902d4 100644 (file)
--- a/view.c
+++ b/view.c
@@ -3,30 +3,39 @@
  * See LICENSE file for license details.
  */
 #include "dwm.h"
+#include <stdio.h>
 
 /* static */
 
 static Client *
-getslot(Client *c)
+minclient()
 {
-       unsigned int i, tic;
-       Client *p;
-
-       for(tic = 0; tic < ntags && !c->tags[tic]; tic++);
-       for(p = clients; p; p = p->next) {
-               for(i = 0; i < ntags && !p->tags[i]; i++);
-               if(tic < i)
-                       return p;
-       }
-       return p;
+       Client *c, *min;
+
+       for(min = c = clients; c; c = c->next)
+               if(c->weight < min->weight)
+                       min = c;
+       return min;
 }
 
-static Client *
-tail()
+
+static void
+reorder()
 {
-       Client *c;
-       for(c = clients; c && c->next; c = c->next);
-       return c;
+       Client *c, *newclients, *tail;
+
+       newclients = tail = NULL;
+       while((c = minclient())) {
+               detach(c);
+               if(tail) {
+                       c->prev = tail;
+                       tail->next = c;
+                       tail = c;
+               }
+               else
+                       tail = newclients = c;
+       }
+       clients = newclients;
 }
 
 /* extern */
@@ -34,35 +43,6 @@ tail()
 void (*arrange)(Arg *) = DEFMODE;
 
 void
-attach(Client *c)
-{
-       Client *p;
-
-       if(!clients) {
-               clients = c;
-               return;
-       }
-       if(!(p = getnext(clients)) && !(p = getslot(c))) {
-               p = tail();
-               c->prev = p;
-               p->next = c;
-               return;
-       }
-
-       if(p == clients) {
-               c->next = clients;
-               clients->prev = c;
-               clients = c;
-       }
-       else {
-               p->prev->next = c;
-               c->prev = p->prev;
-               p->prev = c;
-               c->next = p;
-       }
-}
-
-void
 detach(Client *c)
 {
        if(c->prev)
@@ -77,22 +57,20 @@ detach(Client *c)
 void
 dofloat(Arg *arg)
 {
-       Client *c;
+       Client *c, *fc;
+
+       maximized = False;
 
        for(c = clients; c; c = c->next) {
-               c->ismax = False;
                if(isvisible(c)) {
                        resize(c, True, TopLeft);
                }
                else
                        ban(c);
        }
-       if(!sel || !isvisible(sel))
-               sel = getnext(clients);
-       if(sel)
-               focus(sel);
-       else
-               XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+       if(!(fc = sel) || !isvisible(fc))
+               fc = getnext(clients);
+       focus(fc);
        restack();
 }
 
@@ -100,7 +78,9 @@ void
 dotile(Arg *arg)
 {
        int h, i, n, w;
-       Client *c;
+       Client *c, *fc;
+
+       maximized = False;
 
        w = sw - mw;
        for(n = 0, c = clients; c; c = c->next)
@@ -113,7 +93,6 @@ dotile(Arg *arg)
                h = sh - bh;
 
        for(i = 0, c = clients; c; c = c->next) {
-               c->ismax = False;
                if(isvisible(c)) {
                        if(c->isfloat) {
                                resize(c, True, TopLeft);
@@ -152,12 +131,9 @@ dotile(Arg *arg)
                else
                        ban(c);
        }
-       if(!sel || !isvisible(sel))
-               sel = getnext(clients);
-       if(sel)
-               focus(sel);
-       else
-               XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
+       if(!(fc = sel) || !isvisible(fc))
+               fc = getnext(clients);
+       focus(fc);
        restack();
 }
 
@@ -277,6 +253,7 @@ toggleview(Arg *arg)
        for(i = 0; i < ntags && !seltag[i]; i++);
        if(i == ntags)
                seltag[arg->i] = True; /* cannot toggle last view */
+       reorder();
        arrange(NULL);
 }
 
@@ -288,6 +265,18 @@ view(Arg *arg)
        for(i = 0; i < ntags; i++)
                seltag[i] = False;
        seltag[arg->i] = True;
+       reorder();
+       arrange(NULL);
+}
+
+void
+viewall(Arg *arg)
+{
+       unsigned int i;
+
+       for(i = 0; i < ntags; i++)
+               seltag[i] = True;
+       reorder();
        arrange(NULL);
 }
 
@@ -296,14 +285,16 @@ zoom(Arg *arg)
 {
        Client *c = sel;
 
-       if(!c || (arrange != dotile) || c->isfloat || c->ismax)
+       if(!c || (arrange != dotile) || c->isfloat || maximized)
                return;
 
        if(c == getnext(clients))
                if(!(c = getnext(c->next)))
                        return;
        detach(c);
-       attach(c);
+       c->next = clients;
+       clients->prev = c;
+       clients = c;
        focus(c);
        arrange(NULL);
 }