aewl

annotate client.c @ 435:42388f634de0

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