dwm-meillo

annotate view.c @ 442:056a5072c70a

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