dwm-meillo

annotate client.c @ 131:65efe27cbf3d

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