aewl

annotate client.c @ 109:00309fb2b4d8

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