aewl

annotate view.c @ 378:83576f5f0a90

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