aewl

annotate view.c @ 529:e2bd83191fc6

small boundary check fix
author Anselm R. Garbe <arg@10kloc.org>
date Thu, 05 Oct 2006 18:23:28 +0200
parents c0fffe6c207e
children 451f19d48845
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@382 9 static Client *
arg@487 10 minclient(void) {
arg@382 11 Client *c, *min;
arg@382 12
arg@443 13 if((clients && clients->isfloat) || arrange == dofloat)
arg@443 14 return clients; /* don't touch floating order */
arg@382 15 for(min = c = clients; c; c = c->next)
arg@382 16 if(c->weight < min->weight)
arg@382 17 min = c;
arg@382 18 return min;
arg@382 19 }
arg@382 20
arg@480 21 static Client *
arg@480 22 nexttiled(Client *c) {
arg@480 23 for(c = getnext(c); c && c->isfloat; c = getnext(c->next));
arg@480 24 return c;
arg@480 25 }
arg@480 26
arg@442 27 static void
arg@487 28 reorder(void) {
arg@382 29 Client *c, *newclients, *tail;
arg@380 30
arg@382 31 newclients = tail = NULL;
arg@382 32 while((c = minclient())) {
arg@381 33 detach(c);
arg@382 34 if(tail) {
arg@382 35 c->prev = tail;
arg@382 36 tail->next = c;
arg@382 37 tail = c;
arg@381 38 }
arg@381 39 else
arg@382 40 tail = newclients = c;
arg@380 41 }
arg@382 42 clients = newclients;
arg@380 43 }
arg@380 44
arg@480 45 static void
arg@480 46 togglemax(Client *c)
arg@480 47 {
arg@481 48 XEvent ev;
arg@480 49 if((c->ismax = !c->ismax)) {
arg@480 50 c->rx = c->x; c->x = sx;
arg@480 51 c->ry = c->y; c->y = bh;
arg@502 52 c->rw = c->w; c->w = sw - 2 * BORDERPX;
arg@502 53 c->rh = c->h; c->h = sh - bh - 2 * BORDERPX;
arg@480 54 }
arg@480 55 else {
arg@480 56 c->x = c->rx;
arg@480 57 c->y = c->ry;
arg@481 58 c->w = c->rw;
arg@481 59 c->h = c->rh;
arg@480 60 }
arg@480 61 resize(c, True, TopLeft);
arg@480 62 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@430 63 }
arg@430 64
arg@327 65 /* extern */
arg@327 66
arg@327 67 void (*arrange)(Arg *) = DEFMODE;
arg@505 68 StackPos stackpos = STACKPOS;
arg@327 69
arg@327 70 void
arg@461 71 detach(Client *c) {
arg@378 72 if(c->prev)
arg@378 73 c->prev->next = c->next;
arg@378 74 if(c->next)
arg@378 75 c->next->prev = c->prev;
arg@378 76 if(c == clients)
arg@378 77 clients = c->next;
arg@378 78 c->next = c->prev = NULL;
arg@378 79 }
arg@378 80
arg@378 81 void
arg@461 82 dofloat(Arg *arg) {
arg@402 83 Client *c;
arg@400 84
arg@327 85 for(c = clients; c; c = c->next) {
arg@327 86 if(isvisible(c)) {
arg@327 87 resize(c, True, TopLeft);
arg@327 88 }
arg@327 89 else
arg@327 90 ban(c);
arg@327 91 }
arg@446 92 if(!sel || !isvisible(sel)) {
arg@450 93 for(c = stack; c && !isvisible(c); c = c->snext);
arg@450 94 focus(c);
arg@446 95 }
arg@327 96 restack();
arg@327 97 }
arg@327 98
arg@504 99 /* This algorithm is based on a (M)aster area and a (S)tacking area.
arg@504 100 * It supports following arrangements:
arg@507 101 * SSMMM MMMMM MMMSS
arg@507 102 * SSMMM SSSSS MMMSS
arg@504 103 */
arg@327 104 void
arg@461 105 dotile(Arg *arg) {
arg@526 106 unsigned int i, n, md, stackw, stackh, tw, th;
arg@402 107 Client *c;
arg@400 108
arg@488 109 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
arg@488 110 n++;
arg@327 111
arg@507 112 if(stackpos == StackBottom) {
arg@528 113 md = ((sh - bh) * master) / 1000;
arg@507 114 stackw = sw;
arg@524 115 stackh = sh - bh - md;
arg@507 116 }
arg@507 117 else {
arg@528 118 md = (sw * master) / 1000;
arg@524 119 stackw = sw - md;
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@524 148 c->w = md - 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@524 153 c->h = md - 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@524 159 c->x += md;
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@524 169 c->y = sy + md + (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@524 179 c->y += md;
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@423 241 unsigned int n;
arg@423 242 Client *c;
arg@418 243
arg@430 244 for(n = 0, c = clients; c; c = c->next)
arg@430 245 if(isvisible(c) && !c->isfloat)
arg@423 246 n++;
arg@486 247 if(!sel || sel->isfloat || n < 2 || (arrange == dofloat))
arg@415 248 return;
arg@423 249
arg@415 250 if(sel == getnext(clients)) {
arg@529 251 if(master + arg->i > 950 || master + arg->i < 50)
arg@415 252 return;
arg@505 253 master += arg->i;
arg@415 254 }
arg@415 255 else {
arg@529 256 if(master - arg->i > 950 || master - arg->i < 50)
arg@415 257 return;
arg@505 258 master -= arg->i;
arg@415 259 }
arg@415 260 arrange(NULL);
arg@415 261 }
arg@415 262
arg@327 263 void
arg@487 264 restack(void) {
arg@327 265 Client *c;
arg@327 266 XEvent ev;
arg@481 267
arg@437 268 if(!sel) {
arg@437 269 drawstatus();
arg@436 270 return;
arg@437 271 }
arg@436 272 if(sel->isfloat || arrange == dofloat) {
arg@436 273 XRaiseWindow(dpy, sel->win);
arg@436 274 XRaiseWindow(dpy, sel->twin);
arg@436 275 }
arg@512 276 if(arrange != dofloat) {
arg@512 277 if(!sel->isfloat) {
arg@512 278 XLowerWindow(dpy, sel->twin);
arg@512 279 XLowerWindow(dpy, sel->win);
arg@512 280 }
arg@436 281 for(c = nexttiled(clients); c; c = nexttiled(c->next)) {
arg@512 282 if(c == sel)
arg@512 283 continue;
arg@436 284 XLowerWindow(dpy, c->twin);
arg@436 285 XLowerWindow(dpy, c->win);
arg@327 286 }
arg@512 287 }
arg@327 288 drawall();
arg@327 289 XSync(dpy, False);
arg@327 290 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@327 291 }
arg@327 292
arg@327 293 void
arg@461 294 togglemode(Arg *arg) {
arg@333 295 arrange = (arrange == dofloat) ? dotile : dofloat;
arg@327 296 if(sel)
arg@327 297 arrange(NULL);
arg@327 298 else
arg@327 299 drawstatus();
arg@327 300 }
arg@327 301
arg@327 302 void
arg@461 303 toggleview(Arg *arg) {
arg@327 304 unsigned int i;
arg@327 305
arg@327 306 seltag[arg->i] = !seltag[arg->i];
arg@327 307 for(i = 0; i < ntags && !seltag[i]; i++);
arg@327 308 if(i == ntags)
arg@327 309 seltag[arg->i] = True; /* cannot toggle last view */
arg@381 310 reorder();
arg@327 311 arrange(NULL);
arg@327 312 }
arg@327 313
arg@327 314 void
arg@508 315 togglestackpos(Arg *arg) {
arg@508 316 if(arrange == dofloat)
arg@508 317 return;
arg@508 318 if(stackpos == StackBottom)
arg@508 319 stackpos = STACKPOS;
arg@508 320 else
arg@508 321 stackpos = StackBottom;
arg@508 322 arrange(NULL);
arg@508 323 }
arg@508 324
arg@508 325 void
arg@461 326 view(Arg *arg) {
arg@327 327 unsigned int i;
arg@327 328
arg@327 329 for(i = 0; i < ntags; i++)
arg@327 330 seltag[i] = False;
arg@327 331 seltag[arg->i] = True;
arg@381 332 reorder();
arg@327 333 arrange(NULL);
arg@327 334 }
arg@327 335
arg@327 336 void
arg@461 337 viewall(Arg *arg) {
arg@395 338 unsigned int i;
arg@395 339
arg@395 340 for(i = 0; i < ntags; i++)
arg@395 341 seltag[i] = True;
arg@397 342 reorder();
arg@395 343 arrange(NULL);
arg@395 344 }
arg@395 345
arg@508 346
arg@508 347
arg@395 348 void
arg@461 349 zoom(Arg *arg) {
arg@423 350 unsigned int n;
arg@423 351 Client *c;
arg@473 352
arg@473 353 if(!sel)
arg@473 354 return;
arg@473 355
arg@473 356 if(sel->isfloat || (arrange == dofloat)) {
arg@480 357 togglemax(sel);
arg@473 358 return;
arg@473 359 }
arg@327 360
arg@430 361 for(n = 0, c = clients; c; c = c->next)
arg@430 362 if(isvisible(c) && !c->isfloat)
arg@423 363 n++;
arg@486 364 if(n < 2 || (arrange == dofloat))
arg@327 365 return;
arg@327 366
arg@430 367 if((c = sel) == nexttiled(clients))
arg@433 368 if(!(c = nexttiled(c->next)))
arg@429 369 return;
arg@443 370 detach(c);
arg@443 371 if(clients)
arg@443 372 clients->prev = c;
arg@443 373 c->next = clients;
arg@443 374 clients = c;
arg@378 375 focus(c);
arg@327 376 arrange(NULL);
arg@327 377 }