aewl

annotate client.c @ 318:1b45d6f14fca

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