aewl

annotate client.c @ 104:3a708f113f55

implemented fallback for too many clients in stacked mode
author arg@10ksloc.org
date Wed, 19 Jul 2006 13:36:04 +0200
parents 130d49140341
children 3e74cc981e9b
rev   line source
garbeam@5 1 /*
garbeam@5 2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 3 * See LICENSE file for license details.
garbeam@5 4 */
garbeam@76 5 #include "dwm.h"
garbeam@5 6
garbeam@10 7 #include <stdlib.h>
garbeam@5 8 #include <string.h>
garbeam@5 9 #include <X11/Xatom.h>
garbeam@32 10 #include <X11/Xutil.h>
garbeam@5 11
garbeam@76 12 /* static functions */
garbeam@50 13
garbeam@26 14 static void
garbeam@75 15 resizetitle(Client *c)
garbeam@26 16 {
garbeam@31 17 int i;
garbeam@31 18
garbeam@95 19 c->bw = 0;
garbeam@31 20 for(i = 0; i < TLast; i++)
garbeam@31 21 if(c->tags[i])
garbeam@95 22 c->bw += textw(c->tags[i]);
garbeam@95 23 c->bw += textw(c->name);
garbeam@95 24 if(c->bw > *c->w)
garbeam@95 25 c->bw = *c->w + 2;
garbeam@95 26 c->bx = *c->x + *c->w - c->bw + 2;
garbeam@95 27 c->by = *c->y;
garbeam@95 28 XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
garbeam@26 29 }
garbeam@18 30
garbeam@76 31 static int
garbeam@76 32 xerrordummy(Display *dsply, XErrorEvent *ee)
garbeam@5 33 {
garbeam@76 34 return 0;
garbeam@5 35 }
garbeam@5 36
garbeam@76 37 /* extern functions */
garbeam@20 38
garbeam@20 39 void
garbeam@76 40 ban(Client *c)
garbeam@26 41 {
garbeam@95 42 XMoveWindow(dpy, c->win, *c->x + 2 * sw, *c->y);
garbeam@95 43 XMoveWindow(dpy, c->title, c->bx + 2 * sw, c->by);
garbeam@26 44 }
garbeam@26 45
garbeam@26 46 void
garbeam@13 47 focus(Client *c)
garbeam@13 48 {
garbeam@52 49 Client *old = sel;
garbeam@63 50 XEvent ev;
garbeam@52 51
garbeam@50 52 sel = c;
garbeam@52 53 if(old && old != c)
garbeam@74 54 drawtitle(old);
garbeam@74 55 drawtitle(c);
garbeam@26 56 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@79 57 XSync(dpy, False);
garbeam@63 58 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
garbeam@13 59 }
garbeam@13 60
garbeam@13 61 void
garbeam@76 62 focusnext(Arg *arg)
garbeam@76 63 {
garbeam@76 64 Client *c;
garbeam@76 65
garbeam@76 66 if(!sel)
garbeam@76 67 return;
garbeam@76 68
garbeam@93 69 if(!(c = getnext(sel->next, tsel)))
garbeam@93 70 c = getnext(clients, tsel);
garbeam@76 71 if(c) {
garbeam@76 72 higher(c);
garbeam@76 73 c->revert = sel;
garbeam@76 74 focus(c);
garbeam@76 75 }
garbeam@76 76 }
garbeam@76 77
garbeam@76 78 void
garbeam@76 79 focusprev(Arg *arg)
garbeam@76 80 {
garbeam@76 81 Client *c;
garbeam@76 82
garbeam@76 83 if(!sel)
garbeam@76 84 return;
garbeam@76 85
garbeam@76 86 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
garbeam@76 87 higher(c);
garbeam@76 88 focus(c);
garbeam@76 89 }
garbeam@76 90 }
garbeam@76 91
garbeam@76 92 Client *
garbeam@76 93 getclient(Window w)
garbeam@76 94 {
garbeam@76 95 Client *c;
garbeam@76 96 for(c = clients; c; c = c->next)
garbeam@76 97 if(c->win == w)
garbeam@76 98 return c;
garbeam@76 99 return NULL;
garbeam@76 100 }
garbeam@76 101
garbeam@76 102 Client *
garbeam@76 103 getctitle(Window w)
garbeam@76 104 {
garbeam@76 105 Client *c;
garbeam@76 106 for(c = clients; c; c = c->next)
garbeam@76 107 if(c->title == w)
garbeam@76 108 return c;
garbeam@76 109 return NULL;
garbeam@76 110 }
garbeam@76 111
garbeam@76 112 void
garbeam@76 113 gravitate(Client *c, Bool invert)
garbeam@76 114 {
garbeam@76 115 int dx = 0, dy = 0;
garbeam@76 116
garbeam@76 117 switch(c->grav) {
garbeam@76 118 case StaticGravity:
garbeam@76 119 case NorthWestGravity:
garbeam@76 120 case NorthGravity:
garbeam@76 121 case NorthEastGravity:
garbeam@76 122 dy = c->border;
garbeam@76 123 break;
garbeam@76 124 case EastGravity:
garbeam@76 125 case CenterGravity:
garbeam@76 126 case WestGravity:
garbeam@95 127 dy = -(*c->h / 2) + c->border;
garbeam@76 128 break;
garbeam@76 129 case SouthEastGravity:
garbeam@76 130 case SouthGravity:
garbeam@76 131 case SouthWestGravity:
garbeam@95 132 dy = -(*c->h);
garbeam@76 133 break;
garbeam@76 134 default:
garbeam@76 135 break;
garbeam@76 136 }
garbeam@76 137
garbeam@76 138 switch (c->grav) {
garbeam@76 139 case StaticGravity:
garbeam@76 140 case NorthWestGravity:
garbeam@76 141 case WestGravity:
garbeam@76 142 case SouthWestGravity:
garbeam@76 143 dx = c->border;
garbeam@76 144 break;
garbeam@76 145 case NorthGravity:
garbeam@76 146 case CenterGravity:
garbeam@76 147 case SouthGravity:
garbeam@95 148 dx = -(*c->w / 2) + c->border;
garbeam@76 149 break;
garbeam@76 150 case NorthEastGravity:
garbeam@76 151 case EastGravity:
garbeam@76 152 case SouthEastGravity:
garbeam@95 153 dx = -(*c->w + c->border);
garbeam@76 154 break;
garbeam@76 155 default:
garbeam@76 156 break;
garbeam@76 157 }
garbeam@76 158
garbeam@76 159 if(invert) {
garbeam@76 160 dx = -dx;
garbeam@76 161 dy = -dy;
garbeam@76 162 }
garbeam@95 163 *c->x += dx;
garbeam@95 164 *c->y += dy;
garbeam@76 165 }
garbeam@76 166
garbeam@76 167 void
garbeam@76 168 higher(Client *c)
garbeam@76 169 {
garbeam@76 170 XRaiseWindow(dpy, c->win);
garbeam@76 171 XRaiseWindow(dpy, c->title);
garbeam@76 172 }
garbeam@76 173
garbeam@76 174 void
garbeam@76 175 killclient(Arg *arg)
garbeam@76 176 {
garbeam@76 177 if(!sel)
garbeam@76 178 return;
garbeam@76 179 if(sel->proto & WM_PROTOCOL_DELWIN)
garbeam@77 180 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 181 else
garbeam@76 182 XKillClient(dpy, sel->win);
garbeam@76 183 }
garbeam@76 184
garbeam@76 185 void
garbeam@76 186 lower(Client *c)
garbeam@76 187 {
garbeam@76 188 XLowerWindow(dpy, c->title);
garbeam@76 189 XLowerWindow(dpy, c->win);
garbeam@76 190 }
garbeam@76 191
garbeam@76 192 void
garbeam@10 193 manage(Window w, XWindowAttributes *wa)
garbeam@5 194 {
arg@100 195 int diff;
garbeam@80 196 Client *c;
garbeam@5 197 XSetWindowAttributes twa;
garbeam@53 198 Window trans;
garbeam@5 199
garbeam@5 200 c = emallocz(sizeof(Client));
garbeam@5 201 c->win = w;
garbeam@95 202 c->bx = c->fx = c->tx = wa->x;
garbeam@95 203 c->by = c->fy = c->ty = wa->y;
garbeam@95 204 c->bw = c->fw = c->tw = wa->width;
garbeam@95 205 c->fh = c->th = wa->height;
garbeam@95 206 c->bh = bh;
arg@100 207
arg@100 208 diff = sw - c->fw;
arg@102 209 c->fx = random() % (diff ? diff : 1);
arg@103 210 diff = sh - c->fh - bh;
arg@102 211 c->fy = random() % (diff ? diff : 1);
arg@100 212
arg@104 213 if(c->fy < bh)
arg@104 214 c->by = c->fy = c->ty = bh;
arg@104 215
garbeam@29 216 c->border = 1;
garbeam@75 217 c->proto = getproto(c->win);
garbeam@74 218 setsize(c);
garbeam@26 219 XSelectInput(dpy, c->win,
garbeam@26 220 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 221 XGetTransientForHint(dpy, c->win, &trans);
garbeam@5 222 twa.override_redirect = 1;
garbeam@5 223 twa.background_pixmap = ParentRelative;
garbeam@23 224 twa.event_mask = ExposureMask;
garbeam@5 225
garbeam@95 226 c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
garbeam@20 227 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 228 DefaultVisual(dpy, screen),
garbeam@5 229 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@31 230
garbeam@75 231 settags(c);
garbeam@49 232
garbeam@80 233 c->next = clients;
garbeam@80 234 clients = c;
garbeam@49 235
garbeam@80 236 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
garbeam@80 237 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 238 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 239 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 240 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 241 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 242 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@18 243 GrabModeAsync, GrabModeSync, None, None);
garbeam@53 244
garbeam@80 245 if(!c->isfloat)
garbeam@80 246 c->isfloat = trans
garbeam@53 247 || ((c->maxw == c->minw) && (c->maxh == c->minh));
garbeam@53 248
garbeam@95 249 setgeom(c);
garbeam@95 250 settitle(c);
garbeam@95 251
garbeam@46 252 arrange(NULL);
garbeam@95 253
garbeam@60 254 /* mapping the window now prevents flicker */
garbeam@60 255 if(c->tags[tsel]) {
garbeam@60 256 XMapRaised(dpy, c->win);
garbeam@60 257 XMapRaised(dpy, c->title);
garbeam@51 258 focus(c);
garbeam@60 259 }
garbeam@60 260 else {
garbeam@74 261 ban(c);
garbeam@60 262 XMapRaised(dpy, c->win);
garbeam@60 263 XMapRaised(dpy, c->title);
garbeam@81 264 XSync(dpy, False);
garbeam@60 265 }
garbeam@5 266 }
garbeam@9 267
garbeam@18 268 void
garbeam@76 269 maximize(Arg *arg)
garbeam@29 270 {
garbeam@76 271 if(!sel)
garbeam@76 272 return;
garbeam@95 273 *sel->x = sx;
garbeam@95 274 *sel->y = sy + bh;
garbeam@95 275 *sel->w = sw - 2 * sel->border;
garbeam@95 276 *sel->h = sh - 2 * sel->border - bh;
garbeam@76 277 higher(sel);
arg@99 278 resize(sel, False, TopLeft);
garbeam@29 279 }
garbeam@29 280
garbeam@29 281 void
garbeam@94 282 pop(Client *c)
garbeam@94 283 {
garbeam@94 284 Client **l;
garbeam@94 285 for(l = &clients; *l && *l != c; l = &(*l)->next);
garbeam@94 286 *l = c->next;
garbeam@94 287
garbeam@94 288 c->next = clients; /* pop */
garbeam@94 289 clients = c;
garbeam@94 290 arrange(NULL);
garbeam@94 291 }
garbeam@94 292
garbeam@94 293 void
arg@99 294 resize(Client *c, Bool inc, Corner sticky)
garbeam@18 295 {
garbeam@18 296 XConfigureEvent e;
arg@99 297 int right = *c->x + *c->w;
arg@99 298 int bottom = *c->y + *c->h;
garbeam@18 299
garbeam@52 300 if(inc) {
garbeam@52 301 if(c->incw)
garbeam@95 302 *c->w -= (*c->w - c->basew) % c->incw;
garbeam@52 303 if(c->inch)
garbeam@95 304 *c->h -= (*c->h - c->baseh) % c->inch;
garbeam@52 305 }
garbeam@95 306 if(*c->x > sw) /* might happen on restart */
garbeam@95 307 *c->x = sw - *c->w;
garbeam@95 308 if(*c->y > sh)
garbeam@95 309 *c->y = sh - *c->h;
garbeam@95 310 if(c->minw && *c->w < c->minw)
garbeam@95 311 *c->w = c->minw;
garbeam@95 312 if(c->minh && *c->h < c->minh)
garbeam@95 313 *c->h = c->minh;
garbeam@95 314 if(c->maxw && *c->w > c->maxw)
garbeam@95 315 *c->w = c->maxw;
garbeam@95 316 if(c->maxh && *c->h > c->maxh)
garbeam@95 317 *c->h = c->maxh;
arg@99 318 if(sticky == TopRight || sticky == BottomRight)
arg@99 319 *c->x = right - *c->w;
arg@99 320 if(sticky == BottomLeft || sticky == BottomRight)
arg@99 321 *c->y = bottom - *c->h;
garbeam@75 322 resizetitle(c);
garbeam@55 323 XSetWindowBorderWidth(dpy, c->win, 1);
garbeam@95 324 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
garbeam@18 325 e.type = ConfigureNotify;
garbeam@18 326 e.event = c->win;
garbeam@18 327 e.window = c->win;
garbeam@95 328 e.x = *c->x;
garbeam@95 329 e.y = *c->y;
garbeam@95 330 e.width = *c->w;
garbeam@95 331 e.height = *c->h;
garbeam@29 332 e.border_width = c->border;
garbeam@18 333 e.above = None;
garbeam@18 334 e.override_redirect = False;
garbeam@18 335 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
garbeam@79 336 XSync(dpy, False);
garbeam@18 337 }
garbeam@18 338
garbeam@76 339 void
garbeam@95 340 setgeom(Client *c)
garbeam@95 341 {
garbeam@95 342 if((arrange == dotile) && !c->isfloat) {
garbeam@95 343 c->x = &c->tx;
garbeam@95 344 c->y = &c->ty;
garbeam@95 345 c->w = &c->tw;
garbeam@95 346 c->h = &c->th;
garbeam@95 347 }
garbeam@95 348 else {
garbeam@95 349 c->x = &c->fx;
garbeam@95 350 c->y = &c->fy;
garbeam@95 351 c->w = &c->fw;
garbeam@95 352 c->h = &c->fh;
garbeam@95 353 }
garbeam@95 354 }
garbeam@95 355
garbeam@95 356 void
garbeam@76 357 setsize(Client *c)
garbeam@10 358 {
garbeam@76 359 XSizeHints size;
garbeam@76 360 long msize;
garbeam@76 361 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 362 size.flags = PSize;
garbeam@76 363 c->flags = size.flags;
garbeam@76 364 if(c->flags & PBaseSize) {
garbeam@76 365 c->basew = size.base_width;
garbeam@76 366 c->baseh = size.base_height;
garbeam@76 367 }
garbeam@76 368 else
garbeam@76 369 c->basew = c->baseh = 0;
garbeam@76 370 if(c->flags & PResizeInc) {
garbeam@76 371 c->incw = size.width_inc;
garbeam@76 372 c->inch = size.height_inc;
garbeam@76 373 }
garbeam@76 374 else
garbeam@76 375 c->incw = c->inch = 0;
garbeam@76 376 if(c->flags & PMaxSize) {
garbeam@76 377 c->maxw = size.max_width;
garbeam@76 378 c->maxh = size.max_height;
garbeam@76 379 }
garbeam@76 380 else
garbeam@76 381 c->maxw = c->maxh = 0;
garbeam@76 382 if(c->flags & PMinSize) {
garbeam@76 383 c->minw = size.min_width;
garbeam@76 384 c->minh = size.min_height;
garbeam@76 385 }
garbeam@76 386 else
garbeam@76 387 c->minw = c->minh = 0;
garbeam@76 388 if(c->flags & PWinGravity)
garbeam@76 389 c->grav = size.win_gravity;
garbeam@76 390 else
garbeam@76 391 c->grav = NorthWestGravity;
garbeam@76 392 }
garbeam@76 393
garbeam@76 394 void
garbeam@76 395 settitle(Client *c)
garbeam@76 396 {
garbeam@76 397 XTextProperty name;
garbeam@76 398 int n;
garbeam@76 399 char **list = NULL;
garbeam@76 400
garbeam@76 401 name.nitems = 0;
garbeam@76 402 c->name[0] = 0;
garbeam@77 403 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 404 if(!name.nitems)
garbeam@76 405 XGetWMName(dpy, c->win, &name);
garbeam@76 406 if(!name.nitems)
garbeam@76 407 return;
garbeam@76 408 if(name.encoding == XA_STRING)
garbeam@76 409 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@76 410 else {
garbeam@76 411 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 412 && n > 0 && *list)
garbeam@76 413 {
garbeam@76 414 strncpy(c->name, *list, sizeof(c->name));
garbeam@76 415 XFreeStringList(list);
garbeam@76 416 }
garbeam@76 417 }
garbeam@76 418 XFree(name.value);
garbeam@76 419 resizetitle(c);
garbeam@10 420 }
garbeam@10 421
garbeam@10 422 void
garbeam@10 423 unmanage(Client *c)
garbeam@10 424 {
garbeam@50 425 Client **l;
garbeam@50 426
garbeam@10 427 XGrabServer(dpy);
garbeam@75 428 XSetErrorHandler(xerrordummy);
garbeam@10 429
garbeam@18 430 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 431 XDestroyWindow(dpy, c->title);
garbeam@10 432
garbeam@50 433 for(l = &clients; *l && *l != c; l = &(*l)->next);
garbeam@50 434 *l = c->next;
garbeam@50 435 for(l = &clients; *l; l = &(*l)->next)
garbeam@50 436 if((*l)->revert == c)
garbeam@50 437 (*l)->revert = NULL;
garbeam@50 438 if(sel == c)
garbeam@50 439 sel = sel->revert ? sel->revert : clients;
garbeam@49 440
garbeam@10 441 free(c);
garbeam@10 442
garbeam@79 443 XSync(dpy, False);
garbeam@74 444 XSetErrorHandler(xerror);
garbeam@10 445 XUngrabServer(dpy);
garbeam@46 446 arrange(NULL);
garbeam@50 447 if(sel)
garbeam@50 448 focus(sel);
garbeam@10 449 }
garbeam@10 450
garbeam@75 451 void
garbeam@75 452 zoom(Arg *arg)
garbeam@75 453 {
garbeam@94 454 Client *c;
garbeam@75 455
garbeam@75 456 if(!sel)
garbeam@75 457 return;
garbeam@75 458
garbeam@93 459 if(sel == getnext(clients, tsel) && sel->next) {
garbeam@93 460 if((c = getnext(sel->next, tsel)))
garbeam@75 461 sel = c;
garbeam@75 462 }
garbeam@75 463
garbeam@94 464 pop(sel);
garbeam@75 465 focus(sel);
garbeam@75 466 }