aewl

annotate client.c @ 428:16f8f05f960e

fix
author Anselm R. Garbe <arg@10kloc.org>
date Tue, 05 Sep 2006 18:43:15 +0200
parents dd44dc937e8b
children a31de8605f72
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@400 102 grabbuttons(c, True);
arg@400 103 drawtitle(c);
arg@400 104 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 105 }
arg@400 106 else
arg@400 107 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 108 }
garbeam@13 109
garbeam@76 110 Client *
garbeam@76 111 getclient(Window w)
garbeam@76 112 {
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 *
garbeam@76 122 getctitle(Window w)
garbeam@76 123 {
garbeam@76 124 Client *c;
arg@123 125
garbeam@76 126 for(c = clients; c; c = c->next)
arg@342 127 if(c->twin == w)
garbeam@76 128 return c;
garbeam@76 129 return NULL;
garbeam@76 130 }
garbeam@76 131
garbeam@76 132 void
garbeam@76 133 gravitate(Client *c, Bool invert)
garbeam@76 134 {
garbeam@76 135 int dx = 0, dy = 0;
garbeam@76 136
garbeam@76 137 switch(c->grav) {
arg@127 138 default:
arg@127 139 break;
garbeam@76 140 case StaticGravity:
garbeam@76 141 case NorthWestGravity:
garbeam@76 142 case NorthGravity:
garbeam@76 143 case NorthEastGravity:
garbeam@76 144 dy = c->border;
garbeam@76 145 break;
garbeam@76 146 case EastGravity:
garbeam@76 147 case CenterGravity:
garbeam@76 148 case WestGravity:
arg@115 149 dy = -(c->h / 2) + c->border;
garbeam@76 150 break;
garbeam@76 151 case SouthEastGravity:
garbeam@76 152 case SouthGravity:
garbeam@76 153 case SouthWestGravity:
arg@115 154 dy = -(c->h);
garbeam@76 155 break;
garbeam@76 156 }
garbeam@76 157
garbeam@76 158 switch (c->grav) {
arg@127 159 default:
arg@127 160 break;
garbeam@76 161 case StaticGravity:
garbeam@76 162 case NorthWestGravity:
garbeam@76 163 case WestGravity:
garbeam@76 164 case SouthWestGravity:
garbeam@76 165 dx = c->border;
garbeam@76 166 break;
garbeam@76 167 case NorthGravity:
garbeam@76 168 case CenterGravity:
garbeam@76 169 case SouthGravity:
arg@115 170 dx = -(c->w / 2) + c->border;
garbeam@76 171 break;
garbeam@76 172 case NorthEastGravity:
garbeam@76 173 case EastGravity:
garbeam@76 174 case SouthEastGravity:
arg@115 175 dx = -(c->w + c->border);
garbeam@76 176 break;
garbeam@76 177 }
garbeam@76 178
garbeam@76 179 if(invert) {
garbeam@76 180 dx = -dx;
garbeam@76 181 dy = -dy;
garbeam@76 182 }
arg@115 183 c->x += dx;
arg@115 184 c->y += dy;
garbeam@76 185 }
garbeam@76 186
garbeam@76 187 void
garbeam@76 188 killclient(Arg *arg)
garbeam@76 189 {
garbeam@76 190 if(!sel)
garbeam@76 191 return;
arg@157 192 if(sel->proto & PROTODELWIN)
garbeam@77 193 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 194 else
garbeam@76 195 XKillClient(dpy, sel->win);
garbeam@76 196 }
garbeam@76 197
garbeam@76 198 void
garbeam@10 199 manage(Window w, XWindowAttributes *wa)
garbeam@5 200 {
arg@320 201 unsigned int i;
arg@306 202 Client *c, *tc;
arg@123 203 Window trans;
garbeam@5 204 XSetWindowAttributes twa;
garbeam@5 205
garbeam@5 206 c = emallocz(sizeof(Client));
arg@178 207 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 208 c->win = w;
arg@115 209 c->x = c->tx = wa->x;
arg@115 210 c->y = c->ty = wa->y;
arg@115 211 c->w = c->tw = wa->width;
arg@115 212 c->h = wa->height;
arg@115 213 c->th = bh;
arg@100 214
arg@164 215 c->border = 0;
arg@163 216 setsize(c);
arg@163 217
arg@315 218 if(c->x + c->w + 2 > sw)
arg@314 219 c->x = sw - c->w - 2;
arg@314 220 if(c->x < 0)
arg@314 221 c->x = 0;
arg@315 222 if(c->y + c->h + 2 > sh)
arg@314 223 c->y = sh - c->h - 2;
arg@163 224 if(c->h != sh && c->y < bh)
arg@314 225 c->y = bh;
arg@104 226
garbeam@75 227 c->proto = getproto(c->win);
garbeam@26 228 XSelectInput(dpy, c->win,
arg@127 229 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 230 XGetTransientForHint(dpy, c->win, &trans);
garbeam@5 231 twa.override_redirect = 1;
garbeam@5 232 twa.background_pixmap = ParentRelative;
arg@161 233 twa.event_mask = ExposureMask | EnterWindowMask;
garbeam@5 234
arg@342 235 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 236 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 237 DefaultVisual(dpy, screen),
garbeam@5 238 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@31 239
arg@372 240 grabbuttons(c, False);
arg@320 241 if((tc = getclient(trans))) /* inherit tags */
arg@320 242 for(i = 0; i < ntags; i++)
arg@320 243 c->tags[i] = tc->tags[i];
arg@320 244 else
arg@320 245 settags(c);
garbeam@80 246 if(!c->isfloat)
arg@163 247 c->isfloat = trans
arg@163 248 || (c->maxw && c->minw &&
arg@164 249 c->maxw == c->minw && c->maxh == c->minh);
arg@378 250
arg@381 251 if(clients)
arg@381 252 clients->prev = c;
arg@381 253 c->next = clients;
arg@381 254 clients = c;
arg@378 255
garbeam@95 256 settitle(c);
arg@408 257 ban(c);
arg@270 258 XMapWindow(dpy, c->win);
arg@342 259 XMapWindow(dpy, c->twin);
arg@261 260 if(isvisible(c))
garbeam@51 261 focus(c);
arg@408 262 arrange(NULL);
garbeam@94 263 }
garbeam@94 264
garbeam@94 265 void
arg@129 266 resize(Client *c, Bool sizehints, Corner sticky)
garbeam@18 267 {
arg@123 268 int bottom = c->y + c->h;
arg@123 269 int right = c->x + c->w;
arg@163 270 XWindowChanges wc;
garbeam@18 271
arg@129 272 if(sizehints) {
garbeam@52 273 if(c->incw)
arg@115 274 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 275 if(c->inch)
arg@115 276 c->h -= (c->h - c->baseh) % c->inch;
arg@129 277 if(c->minw && c->w < c->minw)
arg@129 278 c->w = c->minw;
arg@129 279 if(c->minh && c->h < c->minh)
arg@129 280 c->h = c->minh;
arg@129 281 if(c->maxw && c->w > c->maxw)
arg@129 282 c->w = c->maxw;
arg@129 283 if(c->maxh && c->h > c->maxh)
arg@129 284 c->h = c->maxh;
garbeam@52 285 }
arg@105 286 if(sticky == TopRight || sticky == BotRight)
arg@115 287 c->x = right - c->w;
arg@105 288 if(sticky == BotLeft || sticky == BotRight)
arg@115 289 c->y = bottom - c->h;
arg@106 290
garbeam@75 291 resizetitle(c);
arg@163 292 wc.x = c->x;
arg@163 293 wc.y = c->y;
arg@163 294 wc.width = c->w;
arg@163 295 wc.height = c->h;
arg@164 296 if(c->w == sw && c->h == sh)
arg@164 297 wc.border_width = 0;
arg@164 298 else
arg@164 299 wc.border_width = 1;
arg@164 300 XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
garbeam@79 301 XSync(dpy, False);
garbeam@18 302 }
garbeam@18 303
garbeam@76 304 void
garbeam@76 305 setsize(Client *c)
garbeam@10 306 {
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
garbeam@76 344 settitle(Client *c)
garbeam@76 345 {
arg@123 346 char **list = NULL;
arg@377 347 int n;
garbeam@76 348 XTextProperty name;
garbeam@76 349
garbeam@76 350 name.nitems = 0;
garbeam@76 351 c->name[0] = 0;
garbeam@77 352 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 353 if(!name.nitems)
garbeam@76 354 XGetWMName(dpy, c->win, &name);
garbeam@76 355 if(!name.nitems)
garbeam@76 356 return;
garbeam@76 357 if(name.encoding == XA_STRING)
garbeam@76 358 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@76 359 else {
garbeam@76 360 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 361 && n > 0 && *list)
garbeam@76 362 {
garbeam@76 363 strncpy(c->name, *list, sizeof(c->name));
garbeam@76 364 XFreeStringList(list);
garbeam@76 365 }
garbeam@76 366 }
garbeam@76 367 XFree(name.value);
garbeam@76 368 resizetitle(c);
garbeam@10 369 }
garbeam@10 370
garbeam@10 371 void
arg@124 372 togglemax(Arg *arg)
arg@124 373 {
arg@124 374 int ox, oy, ow, oh;
arg@400 375 Client *c;
arg@124 376 XEvent ev;
arg@124 377
arg@124 378 if(!sel)
arg@124 379 return;
arg@124 380
arg@400 381 if((maximized = !maximized)) {
arg@124 382 ox = sel->x;
arg@124 383 oy = sel->y;
arg@124 384 ow = sel->w;
arg@124 385 oh = sel->h;
arg@124 386 sel->x = sx;
arg@124 387 sel->y = sy + bh;
arg@164 388 sel->w = sw - 2;
arg@164 389 sel->h = sh - 2 - bh;
arg@124 390
arg@270 391 restack();
arg@400 392 for(c = getnext(clients); c; c = getnext(c->next))
arg@400 393 if(c != sel)
arg@400 394 ban(c);
arg@247 395 resize(sel, arrange == dofloat, TopLeft);
arg@124 396
arg@124 397 sel->x = ox;
arg@124 398 sel->y = oy;
arg@124 399 sel->w = ow;
arg@124 400 sel->h = oh;
arg@124 401 }
arg@124 402 else
arg@400 403 arrange(NULL);
arg@124 404 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@124 405 }
arg@124 406
arg@124 407 void
garbeam@10 408 unmanage(Client *c)
garbeam@10 409 {
arg@400 410 Client *tc, *fc;
arg@372 411 Window trans;
garbeam@10 412 XGrabServer(dpy);
garbeam@75 413 XSetErrorHandler(xerrordummy);
garbeam@10 414
arg@400 415 detach(c);
arg@400 416 if(sel == c) {
arg@400 417 XGetTransientForHint(dpy, c->win, &trans);
arg@400 418 if(trans && (tc = getclient(trans)) && isvisible(tc))
arg@400 419 fc = tc;
arg@400 420 else
arg@400 421 fc = getnext(clients);
arg@400 422 focus(fc);
arg@400 423 }
arg@372 424
garbeam@18 425 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@342 426 XDestroyWindow(dpy, c->twin);
garbeam@10 427
arg@178 428 free(c->tags);
garbeam@10 429 free(c);
garbeam@10 430
garbeam@79 431 XSync(dpy, False);
garbeam@74 432 XSetErrorHandler(xerror);
garbeam@10 433 XUngrabServer(dpy);
arg@270 434 arrange(NULL);
garbeam@10 435 }