aewl

annotate client.c @ 479:de69a7b0c8fa

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