dwm-meillo

annotate view.c @ 523:c1dd19da63ef

yet another simplification of dotile()
author Anselm R. Garbe <arg@10kloc.org>
date Thu, 05 Oct 2006 12:18:04 +0200
parents fd1275f3b205
children 1a9a0877650c
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@522 7 #define MINDIM 100
arg@522 8
arg@380 9 /* static */
arg@380 10
arg@382 11 static Client *
arg@487 12 minclient(void) {
arg@382 13 Client *c, *min;
arg@382 14
arg@443 15 if((clients && clients->isfloat) || arrange == dofloat)
arg@443 16 return clients; /* don't touch floating order */
arg@382 17 for(min = c = clients; c; c = c->next)
arg@382 18 if(c->weight < min->weight)
arg@382 19 min = c;
arg@382 20 return min;
arg@382 21 }
arg@382 22
arg@480 23 static Client *
arg@480 24 nexttiled(Client *c) {
arg@480 25 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
arg@480 26 return c;
arg@480 27 }
arg@480 28
arg@442 29 static void
arg@487 30 reorder(void) {
arg@382 31 Client *c, *newclients, *tail;
arg@380 32
arg@382 33 newclients = tail = NULL;
arg@382 34 while((c = minclient())) {
arg@381 35 detach(c);
arg@382 36 if(tail) {
arg@382 37 c->prev = tail;
arg@382 38 tail->next = c;
arg@382 39 tail = c;
arg@381 40 }
arg@381 41 else
arg@382 42 tail = newclients = c;
arg@380 43 }
arg@382 44 clients = newclients;
arg@380 45 }
arg@380 46
arg@480 47 static void
arg@480 48 togglemax(Client *c)
arg@480 49 {
arg@481 50 XEvent ev;
arg@480 51 if((c->ismax = !c->ismax)) {
arg@480 52 c->rx = c->x; c->x = sx;
arg@480 53 c->ry = c->y; c->y = bh;
arg@502 54 c->rw = c->w; c->w = sw - 2 * BORDERPX;
arg@502 55 c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
arg@480 56 }
arg@480 57 else {
arg@480 58 c->x = c->rx;
arg@480 59 c->y = c->ry;
arg@481 60 c->w = c->rw;
arg@481 61 c->h = c->rh;
arg@480 62 }
arg@480 63 resize(c, True, TopLeft);
arg@480 64 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@430 65 }
arg@430 66
arg@327 67 /* extern */
arg@327 68
arg@327 69 void (*arrange)(Arg *) = DEFMODE;
arg@505 70 StackPos stackpos = STACKPOS;
arg@327 71
arg@327 72 void
arg@461 73 detach(Client *c) {
arg@378 74 if(c->prev)
arg@378 75 c->prev->next = c->next;
arg@378 76 if(c->next)
arg@378 77 c->next->prev = c->prev;
arg@378 78 if(c == clients)
arg@378 79 clients = c->next;
arg@378 80 c->next = c->prev = NULL;
arg@378 81 }
arg@378 82
arg@378 83 void
arg@461 84 dofloat(Arg *arg) {
arg@402 85 Client *c;
arg@400 86
arg@327 87 for(c = clients; c; c = c->next) {
arg@327 88 if(isvisible(c)) {
arg@327 89 resize(c, True, TopLeft);
arg@327 90 }
arg@327 91 else
arg@327 92 ban(c);
arg@327 93 }
arg@446 94 if(!sel || !isvisible(sel)) {
arg@450 95 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 96 focus(c);
arg@446 97 }
arg@327 98 restack();
arg@327 99 }
arg@327 100
arg@504 101 /* This algorithm is based on a (M)aster area and a (S)tacking area.
arg@504 102 * It supports following arrangements:
arg@507 103 * SSMMM MMMMM MMMSS
arg@507 104 * SSMMM SSSSS MMMSS
arg@504 105 */
arg@327 106 void
arg@461 107 dotile(Arg *arg) {
arg@507 108 int i, n, stackw, stackh, tw, th;
arg@402 109 Client *c;
arg@400 110
arg@488 111 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
arg@488 112 n++;
arg@327 113
arg@507 114 if(stackpos == StackBottom) {
arg@507 115 stackw = sw;
arg@507 116 stackh = sh - bh - master;
arg@507 117 }
arg@507 118 else {
arg@507 119 stackw = sw - master;
arg@507 120 stackh = sh - bh;
arg@507 121 }
arg@507 122
arg@511 123 tw = stackw;
arg@511 124 if(n > 1)
arg@511 125 th = stackh / (n - 1);
arg@511 126 else
arg@507 127 th = stackh;
arg@327 128
arg@327 129 for(i = 0, c = clients; c; c = c->next) {
arg@327 130 if(isvisible(c)) {
arg@327 131 if(c->isfloat) {
arg@327 132 resize(c, True, TopLeft);
arg@327 133 continue;
arg@327 134 }
arg@488 135 c->ismax = False;
arg@523 136 c->x = sx;
arg@523 137 c->y = sy + bh;
arg@507 138 if(n == 1) { /* only 1 window */
arg@502 139 c->w = sw - 2 * BORDERPX;
arg@502 140 c->h = sh - 2 * BORDERPX - bh;
arg@327 141 }
arg@507 142 else if(i == 0) { /* master window */
arg@522 143 if(stackpos == StackLeft)
arg@522 144 c->x += stackw;
arg@508 145 switch(stackpos) {
arg@508 146 case StackLeft:
arg@522 147 case StackRight:
arg@507 148 c->w = master - 2 * BORDERPX;
arg@508 149 c->h = sh - bh - 2 * BORDERPX;
arg@508 150 break;
arg@508 151 case StackBottom:
arg@508 152 c->w = sw - 2 * BORDERPX;
arg@507 153 c->h = master - 2 * BORDERPX;
arg@508 154 break;
arg@507 155 }
arg@507 156 }
arg@523 157 else { /* tile window */
arg@522 158 if(stackpos == StackRight)
arg@522 159 c->x += master;
arg@523 160 if(th > bh) {
arg@523 161 switch(stackpos) {
arg@523 162 case StackLeft:
arg@523 163 case StackRight:
arg@523 164 c->y = sy + (i - 1) * th + bh;
arg@523 165 if(i + 1 == n)
arg@523 166 c->h = sh - c->y - 2 * BORDERPX;
arg@523 167 break;
arg@523 168 case StackBottom:
arg@523 169 c->y = sy + master + (i - 1) * th + bh;
arg@523 170 if(i + 1 == n)
arg@523 171 c->h = sh - c->y - 2 * BORDERPX;
arg@523 172 break;
arg@523 173 }
arg@523 174 c->w = tw - 2 * BORDERPX;
arg@523 175 c->h = th - 2 * BORDERPX;
arg@507 176 }
arg@523 177 else { /* fallback if th < bh */
arg@523 178 if(stackpos == StackBottom)
arg@523 179 c->y += master;
arg@523 180 c->w = stackw - 2 * BORDERPX;
arg@523 181 c->h = stackh - 2 * BORDERPX;
arg@523 182 }
arg@327 183 }
arg@327 184 resize(c, False, TopLeft);
arg@327 185 i++;
arg@327 186 }
arg@327 187 else
arg@327 188 ban(c);
arg@327 189 }
arg@446 190 if(!sel || !isvisible(sel)) {
arg@450 191 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 192 focus(c);
arg@446 193 }
arg@327 194 restack();
arg@327 195 }
arg@327 196
arg@327 197 void
arg@461 198 focusnext(Arg *arg) {
arg@327 199 Client *c;
arg@327 200
arg@327 201 if(!sel)
arg@327 202 return;
arg@327 203
arg@327 204 if(!(c = getnext(sel->next)))
arg@327 205 c = getnext(clients);
arg@327 206 if(c) {
arg@327 207 focus(c);
arg@327 208 restack();
arg@327 209 }
arg@327 210 }
arg@327 211
arg@327 212 void
arg@461 213 focusprev(Arg *arg) {
arg@327 214 Client *c;
arg@327 215
arg@327 216 if(!sel)
arg@327 217 return;
arg@327 218
arg@327 219 if(!(c = getprev(sel->prev))) {
arg@327 220 for(c = clients; c && c->next; c = c->next);
arg@327 221 c = getprev(c);
arg@327 222 }
arg@327 223 if(c) {
arg@327 224 focus(c);
arg@327 225 restack();
arg@327 226 }
arg@327 227 }
arg@327 228
arg@420 229 Bool
arg@461 230 isvisible(Client *c) {
arg@420 231 unsigned int i;
arg@420 232
arg@420 233 for(i = 0; i < ntags; i++)
arg@420 234 if(c->tags[i] && seltag[i])
arg@420 235 return True;
arg@420 236 return False;
arg@420 237 }
arg@420 238
arg@415 239 void
arg@461 240 resizecol(Arg *arg) {
arg@521 241 int s;
arg@423 242 unsigned int n;
arg@423 243 Client *c;
arg@418 244
arg@430 245 for(n = 0, c = clients; c; c = c->next)
arg@430 246 if(isvisible(c) && !c->isfloat)
arg@423 247 n++;
arg@486 248 if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
arg@415 249 return;
arg@423 250
arg@521 251 s = stackpos == StackBottom ? sh - bh : sw;
arg@415 252 if(sel == getnext(clients)) {
arg@522 253 if(master + arg->i > s - MINDIM || master + arg->i < MINDIM)
arg@415 254 return;
arg@505 255 master += arg->i;
arg@415 256 }
arg@415 257 else {
arg@522 258 if(master - arg->i > s - MINDIM || master - arg->i < MINDIM)
arg@415 259 return;
arg@505 260 master -= arg->i;
arg@415 261 }
arg@415 262 arrange(NULL);
arg@415 263 }
arg@415 264
arg@327 265 void
arg@487 266 restack(void) {
arg@327 267 Client *c;
arg@327 268 XEvent ev;
arg@481 269
arg@437 270 if(!sel) {
arg@437 271 drawstatus();
arg@436 272 return;
arg@437 273 }
arg@436 274 if(sel->isfloat || arrange == dofloat) {
arg@436 275 XRaiseWindow(dpy, sel->win);
arg@436 276 XRaiseWindow(dpy, sel->twin);
arg@436 277 }
arg@512 278 if(arrange != dofloat) {
arg@512 279 if(!sel->isfloat) {
arg@512 280 XLowerWindow(dpy, sel->twin);
arg@512 281 XLowerWindow(dpy, sel->win);
arg@512 282 }
arg@436 283 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
arg@512 284 if(c == sel)
arg@512 285 continue;
arg@436 286 XLowerWindow(dpy, c->twin);
arg@436 287 XLowerWindow(dpy, c->win);
arg@327 288 }
arg@512 289 }
arg@327 290 drawall();
arg@327 291 XSync(dpy, False);
arg@327 292 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@327 293 }
arg@327 294
arg@327 295 void
arg@461 296 togglemode(Arg *arg) {
arg@333 297 arrange = (arrange == dofloat) ? dotile : dofloat;
arg@327 298 if(sel)
arg@327 299 arrange(NULL);
arg@327 300 else
arg@327 301 drawstatus();
arg@327 302 }
arg@327 303
arg@327 304 void
arg@461 305 toggleview(Arg *arg) {
arg@327 306 unsigned int i;
arg@327 307
arg@327 308 seltag[arg->i] = !seltag[arg->i];
arg@327 309 for(i = 0; i < ntags && !seltag[i]; i++);
arg@327 310 if(i == ntags)
arg@327 311 seltag[arg->i] = True; /* cannot toggle last view */
arg@381 312 reorder();
arg@327 313 arrange(NULL);
arg@327 314 }
arg@327 315
arg@327 316 void
arg@508 317 togglestackpos(Arg *arg) {
arg@508 318 if(arrange == dofloat)
arg@508 319 return;
arg@508 320 if(stackpos == StackBottom)
arg@508 321 stackpos = STACKPOS;
arg@508 322 else
arg@508 323 stackpos = StackBottom;
arg@515 324 master = ((stackpos == StackBottom ? sh - bh : sw) * MASTER) / 100;
arg@508 325 arrange(NULL);
arg@508 326 }
arg@508 327
arg@508 328 void
arg@461 329 view(Arg *arg) {
arg@327 330 unsigned int i;
arg@327 331
arg@327 332 for(i = 0; i < ntags; i++)
arg@327 333 seltag[i] = False;
arg@327 334 seltag[arg->i] = True;
arg@381 335 reorder();
arg@327 336 arrange(NULL);
arg@327 337 }
arg@327 338
arg@327 339 void
arg@461 340 viewall(Arg *arg) {
arg@395 341 unsigned int i;
arg@395 342
arg@395 343 for(i = 0; i < ntags; i++)
arg@395 344 seltag[i] = True;
arg@397 345 reorder();
arg@395 346 arrange(NULL);
arg@395 347 }
arg@395 348
arg@508 349
arg@508 350
arg@395 351 void
arg@461 352 zoom(Arg *arg) {
arg@423 353 unsigned int n;
arg@423 354 Client *c;
arg@473 355
arg@473 356 if(!sel)
arg@473 357 return;
arg@473 358
arg@473 359 if(sel->isfloat || (arrange == dofloat)) {
arg@480 360 togglemax(sel);
arg@473 361 return;
arg@473 362 }
arg@327 363
arg@430 364 for(n = 0, c = clients; c; c = c->next)
arg@430 365 if(isvisible(c) && !c->isfloat)
arg@423 366 n++;
arg@486 367 if(n < 2 || (arrange == dofloat))
arg@327 368 return;
arg@327 369
arg@430 370 if((c = sel) == nexttiled(clients))
arg@433 371 if(!(c = nexttiled(c->next)))
arg@429 372 return;
arg@443 373 detach(c);
arg@443 374 if(clients)
arg@443 375 clients->prev = c;
arg@443 376 c->next = clients;
arg@443 377 clients = c;
arg@378 378 focus(c);
arg@327 379 arrange(NULL);
arg@327 380 }