aewl

diff view.c @ 380:4bf79305d675

this algorithm seems to keep order for any scenario
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 29 Aug 2006 09:57:57 +0200
parents 83576f5f0a90
children b00cc483d13b
line diff
     1.1 --- a/view.c	Tue Aug 29 09:25:14 2006 +0200
     1.2 +++ b/view.c	Tue Aug 29 09:57:57 2006 +0200
     1.3 @@ -4,6 +4,31 @@
     1.4   */
     1.5  #include "dwm.h"
     1.6  
     1.7 +/* static */
     1.8 +
     1.9 +static Client *
    1.10 +getslot(Client *c)
    1.11 +{
    1.12 +	unsigned int i, tic;
    1.13 +	Client *p;
    1.14 +
    1.15 +	for(tic = 0; tic < ntags && !c->tags[tic]; tic++);
    1.16 +	for(p = clients; p; p = p->next) {
    1.17 +		for(i = 0; i < ntags && !p->tags[i]; i++);
    1.18 +		if(tic < i)
    1.19 +			return p;
    1.20 +	}
    1.21 +	return p;
    1.22 +}
    1.23 +
    1.24 +static Client *
    1.25 +tail()
    1.26 +{
    1.27 +	Client *c;
    1.28 +	for(c = clients; c && c->next; c = c->next);
    1.29 +	return c;
    1.30 +}
    1.31 +
    1.32  /* extern */
    1.33  
    1.34  void (*arrange)(Arg *) = DEFMODE;
    1.35 @@ -11,27 +36,29 @@
    1.36  void
    1.37  attach(Client *c)
    1.38  {
    1.39 -	Client *first = getnext(clients);
    1.40 +	Client *p;
    1.41  
    1.42 -	if(!first) {
    1.43 -		if(clients) {
    1.44 -			for(first = clients; first->next; first = first->next);
    1.45 -			first->next = c;
    1.46 -			c->prev = first;
    1.47 -		}
    1.48 -		else
    1.49 -			clients = c;
    1.50 +	if(!clients) {
    1.51 +		clients = c;
    1.52 +		return;
    1.53  	}
    1.54 -	else if(first == clients) {
    1.55 +	if(!(p = getnext(clients)) && !(p = getslot(c))) {
    1.56 +		p = tail();
    1.57 +		c->prev = p;
    1.58 +		p->next = c;
    1.59 +		return;
    1.60 +	}
    1.61 +
    1.62 +	if(p == clients) {
    1.63  		c->next = clients;
    1.64  		clients->prev = c;
    1.65  		clients = c;
    1.66  	}
    1.67  	else {
    1.68 -		first->prev->next = c;
    1.69 -		c->prev = first->prev;
    1.70 -		first->prev = c;
    1.71 -		c->next = first;
    1.72 +		p->prev->next = c;
    1.73 +		c->prev = p->prev;
    1.74 +		p->prev = c;
    1.75 +		c->next = p;
    1.76  	}
    1.77  }
    1.78