dwm-meillo

annotate 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
rev   line source
arg@327 1 /*
arg@327 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
arg@327 3 * See LICENSE file for license details.
arg@327 4 */
arg@327 5 #include "dwm.h"
arg@327 6
arg@380 7 /* static */
arg@380 8
arg@380 9 static Client *
arg@380 10 getslot(Client *c)
arg@380 11 {
arg@380 12 unsigned int i, tic;
arg@380 13 Client *p;
arg@380 14
arg@380 15 for(tic = 0; tic < ntags && !c->tags[tic]; tic++);
arg@380 16 for(p = clients; p; p = p->next) {
arg@380 17 for(i = 0; i < ntags && !p->tags[i]; i++);
arg@380 18 if(tic < i)
arg@380 19 return p;
arg@380 20 }
arg@380 21 return p;
arg@380 22 }
arg@380 23
arg@380 24 static Client *
arg@380 25 tail()
arg@380 26 {
arg@380 27 Client *c;
arg@380 28 for(c = clients; c && c->next; c = c->next);
arg@380 29 return c;
arg@380 30 }
arg@380 31
arg@327 32 /* extern */
arg@327 33
arg@327 34 void (*arrange)(Arg *) = DEFMODE;
arg@327 35
arg@327 36 void
arg@378 37 attach(Client *c)
arg@378 38 {
arg@380 39 Client *p;
arg@378 40
arg@380 41 if(!clients) {
arg@380 42 clients = c;
arg@380 43 return;
arg@378 44 }
arg@380 45 if(!(p = getnext(clients)) && !(p = getslot(c))) {
arg@380 46 p = tail();
arg@380 47 c->prev = p;
arg@380 48 p->next = c;
arg@380 49 return;
arg@380 50 }
arg@380 51
arg@380 52 if(p == clients) {
arg@378 53 c->next = clients;
arg@378 54 clients->prev = c;
arg@378 55 clients = c;
arg@378 56 }
arg@378 57 else {
arg@380 58 p->prev->next = c;
arg@380 59 c->prev = p->prev;
arg@380 60 p->prev = c;
arg@380 61 c->next = p;
arg@378 62 }
arg@378 63 }
arg@378 64
arg@378 65 void
arg@378 66 detach(Client *c)
arg@378 67 {
arg@378 68 if(c->prev)
arg@378 69 c->prev->next = c->next;
arg@378 70 if(c->next)
arg@378 71 c->next->prev = c->prev;
arg@378 72 if(c == clients)
arg@378 73 clients = c->next;
arg@378 74 c->next = c->prev = NULL;
arg@378 75 }
arg@378 76
arg@378 77 void
arg@327 78 dofloat(Arg *arg)
arg@327 79 {
arg@327 80 Client *c;
arg@327 81
arg@327 82 for(c = clients; c; c = c->next) {
arg@327 83 c->ismax = False;
arg@327 84 if(isvisible(c)) {
arg@327 85 resize(c, True, TopLeft);
arg@327 86 }
arg@327 87 else
arg@327 88 ban(c);
arg@327 89 }
arg@327 90 if(!sel || !isvisible(sel))
arg@327 91 sel = getnext(clients);
arg@327 92 if(sel)
arg@327 93 focus(sel);
arg@327 94 else
arg@327 95 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@327 96 restack();
arg@327 97 }
arg@327 98
arg@327 99 void
arg@327 100 dotile(Arg *arg)
arg@327 101 {
arg@327 102 int h, i, n, w;
arg@327 103 Client *c;
arg@327 104
arg@327 105 w = sw - mw;
arg@327 106 for(n = 0, c = clients; c; c = c->next)
arg@327 107 if(isvisible(c) && !c->isfloat)
arg@327 108 n++;
arg@327 109
arg@327 110 if(n > 1)
arg@327 111 h = (sh - bh) / (n - 1);
arg@327 112 else
arg@327 113 h = sh - bh;
arg@327 114
arg@327 115 for(i = 0, c = clients; c; c = c->next) {
arg@327 116 c->ismax = False;
arg@327 117 if(isvisible(c)) {
arg@327 118 if(c->isfloat) {
arg@327 119 resize(c, True, TopLeft);
arg@327 120 continue;
arg@327 121 }
arg@327 122 if(n == 1) {
arg@327 123 c->x = sx;
arg@327 124 c->y = sy + bh;
arg@327 125 c->w = sw - 2;
arg@327 126 c->h = sh - 2 - bh;
arg@327 127 }
arg@327 128 else if(i == 0) {
arg@327 129 c->x = sx;
arg@327 130 c->y = sy + bh;
arg@327 131 c->w = mw - 2;
arg@327 132 c->h = sh - 2 - bh;
arg@327 133 }
arg@327 134 else if(h > bh) {
arg@327 135 c->x = sx + mw;
arg@327 136 c->y = sy + (i - 1) * h + bh;
arg@327 137 c->w = w - 2;
arg@327 138 if(i + 1 == n)
arg@327 139 c->h = sh - c->y - 2;
arg@327 140 else
arg@327 141 c->h = h - 2;
arg@327 142 }
arg@327 143 else { /* fallback if h < bh */
arg@327 144 c->x = sx + mw;
arg@327 145 c->y = sy + bh;
arg@327 146 c->w = w - 2;
arg@327 147 c->h = sh - 2 - bh;
arg@327 148 }
arg@327 149 resize(c, False, TopLeft);
arg@327 150 i++;
arg@327 151 }
arg@327 152 else
arg@327 153 ban(c);
arg@327 154 }
arg@327 155 if(!sel || !isvisible(sel))
arg@327 156 sel = getnext(clients);
arg@327 157 if(sel)
arg@327 158 focus(sel);
arg@327 159 else
arg@327 160 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
arg@327 161 restack();
arg@327 162 }
arg@327 163
arg@327 164 void
arg@327 165 focusnext(Arg *arg)
arg@327 166 {
arg@327 167 Client *c;
arg@327 168
arg@327 169 if(!sel)
arg@327 170 return;
arg@327 171
arg@327 172 if(!(c = getnext(sel->next)))
arg@327 173 c = getnext(clients);
arg@327 174 if(c) {
arg@327 175 focus(c);
arg@327 176 restack();
arg@327 177 }
arg@327 178 }
arg@327 179
arg@327 180 void
arg@327 181 focusprev(Arg *arg)
arg@327 182 {
arg@327 183 Client *c;
arg@327 184
arg@327 185 if(!sel)
arg@327 186 return;
arg@327 187
arg@327 188 if(!(c = getprev(sel->prev))) {
arg@327 189 for(c = clients; c && c->next; c = c->next);
arg@327 190 c = getprev(c);
arg@327 191 }
arg@327 192 if(c) {
arg@327 193 focus(c);
arg@327 194 restack();
arg@327 195 }
arg@327 196 }
arg@327 197
arg@327 198 Bool
arg@327 199 isvisible(Client *c)
arg@327 200 {
arg@327 201 unsigned int i;
arg@327 202
arg@327 203 for(i = 0; i < ntags; i++)
arg@327 204 if(c->tags[i] && seltag[i])
arg@327 205 return True;
arg@327 206 return False;
arg@327 207 }
arg@327 208
arg@327 209 void
arg@327 210 restack()
arg@327 211 {
arg@327 212 static unsigned int nwins = 0;
arg@327 213 static Window *wins = NULL;
arg@327 214 unsigned int f, fi, m, mi, n;
arg@327 215 Client *c;
arg@327 216 XEvent ev;
arg@327 217
arg@327 218 for(f = 0, m = 0, c = clients; c; c = c->next)
arg@327 219 if(isvisible(c)) {
arg@327 220 if(c->isfloat || arrange == dofloat)
arg@327 221 f++;
arg@327 222 else
arg@327 223 m++;
arg@327 224 }
arg@327 225 if(!(n = 2 * (f + m))) {
arg@327 226 drawstatus();
arg@327 227 return;
arg@327 228 }
arg@327 229 if(nwins < n) {
arg@327 230 nwins = n;
arg@327 231 wins = erealloc(wins, nwins * sizeof(Window));
arg@327 232 }
arg@327 233
arg@327 234 fi = 0;
arg@327 235 mi = 2 * f;
arg@327 236 if(sel->isfloat || arrange == dofloat) {
arg@342 237 wins[fi++] = sel->twin;
arg@327 238 wins[fi++] = sel->win;
arg@327 239 }
arg@327 240 else {
arg@342 241 wins[mi++] = sel->twin;
arg@327 242 wins[mi++] = sel->win;
arg@327 243 }
arg@327 244 for(c = clients; c; c = c->next)
arg@327 245 if(isvisible(c) && c != sel) {
arg@327 246 if(c->isfloat || arrange == dofloat) {
arg@342 247 wins[fi++] = c->twin;
arg@327 248 wins[fi++] = c->win;
arg@327 249 }
arg@327 250 else {
arg@342 251 wins[mi++] = c->twin;
arg@327 252 wins[mi++] = c->win;
arg@327 253 }
arg@327 254 }
arg@327 255 XRestackWindows(dpy, wins, n);
arg@327 256 drawall();
arg@327 257 XSync(dpy, False);
arg@327 258 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@327 259 }
arg@327 260
arg@327 261 void
arg@327 262 togglemode(Arg *arg)
arg@327 263 {
arg@333 264 arrange = (arrange == dofloat) ? dotile : dofloat;
arg@327 265 if(sel)
arg@327 266 arrange(NULL);
arg@327 267 else
arg@327 268 drawstatus();
arg@327 269 }
arg@327 270
arg@327 271 void
arg@327 272 toggleview(Arg *arg)
arg@327 273 {
arg@327 274 unsigned int i;
arg@327 275
arg@327 276 seltag[arg->i] = !seltag[arg->i];
arg@327 277 for(i = 0; i < ntags && !seltag[i]; i++);
arg@327 278 if(i == ntags)
arg@327 279 seltag[arg->i] = True; /* cannot toggle last view */
arg@327 280 arrange(NULL);
arg@327 281 }
arg@327 282
arg@327 283 void
arg@327 284 view(Arg *arg)
arg@327 285 {
arg@327 286 unsigned int i;
arg@327 287
arg@327 288 for(i = 0; i < ntags; i++)
arg@327 289 seltag[i] = False;
arg@327 290 seltag[arg->i] = True;
arg@327 291 arrange(NULL);
arg@327 292 }
arg@327 293
arg@327 294 void
arg@327 295 zoom(Arg *arg)
arg@327 296 {
arg@378 297 Client *c = sel;
arg@327 298
arg@378 299 if(!c || (arrange != dotile) || c->isfloat || c->ismax)
arg@327 300 return;
arg@327 301
arg@378 302 if(c == getnext(clients))
arg@378 303 if(!(c = getnext(c->next)))
arg@327 304 return;
arg@378 305 detach(c);
arg@378 306 attach(c);
arg@378 307 focus(c);
arg@327 308 arrange(NULL);
arg@327 309 }