aewl

annotate client.c @ 454:ffb462fb7903

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