aewl

annotate client.c @ 503:491eebbe7a64

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