aewl

annotate client.c @ 463:e93b0ad5aeb5

removed useless inclusion
author Anselm R. Garbe <arg@10kloc.org>
date Thu, 14 Sep 2006 11:07:44 +0200
parents ffb462fb7903
children bd32d89ed9d5
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@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@446 102 detachstack(c);
arg@446 103 c->snext = stack;
arg@446 104 stack = c;
arg@400 105 grabbuttons(c, True);
arg@400 106 drawtitle(c);
arg@400 107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 108 }
arg@400 109 else
arg@400 110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 111 }
garbeam@13 112
garbeam@76 113 Client *
arg@461 114 getclient(Window w) {
garbeam@76 115 Client *c;
arg@123 116
garbeam@76 117 for(c = clients; c; c = c->next)
garbeam@76 118 if(c->win == w)
garbeam@76 119 return c;
garbeam@76 120 return NULL;
garbeam@76 121 }
garbeam@76 122
garbeam@76 123 Client *
arg@461 124 getctitle(Window w) {
garbeam@76 125 Client *c;
arg@123 126
garbeam@76 127 for(c = clients; c; c = c->next)
arg@342 128 if(c->twin == w)
garbeam@76 129 return c;
garbeam@76 130 return NULL;
garbeam@76 131 }
garbeam@76 132
garbeam@76 133 void
arg@461 134 gravitate(Client *c, Bool invert) {
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
arg@461 188 killclient(Arg *arg) {
garbeam@76 189 if(!sel)
garbeam@76 190 return;
arg@157 191 if(sel->proto & PROTODELWIN)
garbeam@77 192 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 193 else
garbeam@76 194 XKillClient(dpy, sel->win);
garbeam@76 195 }
garbeam@76 196
garbeam@76 197 void
arg@461 198 manage(Window w, XWindowAttributes *wa) {
arg@431 199 Client *c;
arg@123 200 Window trans;
garbeam@5 201 XSetWindowAttributes twa;
garbeam@5 202
garbeam@5 203 c = emallocz(sizeof(Client));
arg@178 204 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 205 c->win = w;
arg@115 206 c->x = c->tx = wa->x;
arg@115 207 c->y = c->ty = wa->y;
arg@115 208 c->w = c->tw = wa->width;
arg@115 209 c->h = wa->height;
arg@115 210 c->th = bh;
arg@100 211
arg@164 212 c->border = 0;
arg@454 213 updatesize(c);
arg@163 214
arg@315 215 if(c->x + c->w + 2 > sw)
arg@314 216 c->x = sw - c->w - 2;
arg@314 217 if(c->x < 0)
arg@314 218 c->x = 0;
arg@315 219 if(c->y + c->h + 2 > sh)
arg@314 220 c->y = sh - c->h - 2;
arg@163 221 if(c->h != sh && c->y < bh)
arg@314 222 c->y = bh;
arg@104 223
garbeam@75 224 c->proto = getproto(c->win);
garbeam@26 225 XSelectInput(dpy, c->win,
arg@127 226 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 227 XGetTransientForHint(dpy, c->win, &trans);
garbeam@5 228 twa.override_redirect = 1;
garbeam@5 229 twa.background_pixmap = ParentRelative;
arg@161 230 twa.event_mask = ExposureMask | EnterWindowMask;
garbeam@5 231
arg@342 232 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 233 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 234 DefaultVisual(dpy, screen),
garbeam@5 235 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@31 236
arg@372 237 grabbuttons(c, False);
arg@431 238 settags(c, getclient(trans));
garbeam@80 239 if(!c->isfloat)
arg@163 240 c->isfloat = trans
arg@163 241 || (c->maxw && c->minw &&
arg@164 242 c->maxw == c->minw && c->maxh == c->minh);
arg@378 243
arg@381 244 if(clients)
arg@381 245 clients->prev = c;
arg@381 246 c->next = clients;
arg@446 247 c->snext = stack;
arg@446 248 stack = clients = c;
arg@378 249
arg@454 250 updatetitle(c);
arg@408 251 ban(c);
arg@270 252 XMapWindow(dpy, c->win);
arg@342 253 XMapWindow(dpy, c->twin);
arg@261 254 if(isvisible(c))
garbeam@51 255 focus(c);
arg@408 256 arrange(NULL);
garbeam@94 257 }
garbeam@94 258
garbeam@94 259 void
arg@461 260 resize(Client *c, Bool sizehints, Corner sticky) {
arg@123 261 int bottom = c->y + c->h;
arg@123 262 int right = c->x + c->w;
arg@163 263 XWindowChanges wc;
garbeam@18 264
arg@129 265 if(sizehints) {
garbeam@52 266 if(c->incw)
arg@115 267 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 268 if(c->inch)
arg@115 269 c->h -= (c->h - c->baseh) % c->inch;
arg@129 270 if(c->minw && c->w < c->minw)
arg@129 271 c->w = c->minw;
arg@129 272 if(c->minh && c->h < c->minh)
arg@129 273 c->h = c->minh;
arg@129 274 if(c->maxw && c->w > c->maxw)
arg@129 275 c->w = c->maxw;
arg@129 276 if(c->maxh && c->h > c->maxh)
arg@129 277 c->h = c->maxh;
garbeam@52 278 }
arg@105 279 if(sticky == TopRight || sticky == BotRight)
arg@115 280 c->x = right - c->w;
arg@105 281 if(sticky == BotLeft || sticky == BotRight)
arg@115 282 c->y = bottom - c->h;
arg@106 283
garbeam@75 284 resizetitle(c);
arg@163 285 wc.x = c->x;
arg@163 286 wc.y = c->y;
arg@163 287 wc.width = c->w;
arg@163 288 wc.height = c->h;
arg@164 289 if(c->w == sw && c->h == sh)
arg@164 290 wc.border_width = 0;
arg@164 291 else
arg@164 292 wc.border_width = 1;
arg@452 293 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
garbeam@79 294 XSync(dpy, False);
garbeam@18 295 }
garbeam@18 296
garbeam@76 297 void
arg@461 298 updatesize(Client *c) {
arg@123 299 long msize;
garbeam@76 300 XSizeHints size;
arg@123 301
garbeam@76 302 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 303 size.flags = PSize;
garbeam@76 304 c->flags = size.flags;
garbeam@76 305 if(c->flags & PBaseSize) {
garbeam@76 306 c->basew = size.base_width;
garbeam@76 307 c->baseh = size.base_height;
garbeam@76 308 }
garbeam@76 309 else
garbeam@76 310 c->basew = c->baseh = 0;
garbeam@76 311 if(c->flags & PResizeInc) {
garbeam@76 312 c->incw = size.width_inc;
garbeam@76 313 c->inch = size.height_inc;
garbeam@76 314 }
garbeam@76 315 else
garbeam@76 316 c->incw = c->inch = 0;
garbeam@76 317 if(c->flags & PMaxSize) {
garbeam@76 318 c->maxw = size.max_width;
garbeam@76 319 c->maxh = size.max_height;
garbeam@76 320 }
garbeam@76 321 else
garbeam@76 322 c->maxw = c->maxh = 0;
garbeam@76 323 if(c->flags & PMinSize) {
garbeam@76 324 c->minw = size.min_width;
garbeam@76 325 c->minh = size.min_height;
garbeam@76 326 }
garbeam@76 327 else
garbeam@76 328 c->minw = c->minh = 0;
garbeam@76 329 if(c->flags & PWinGravity)
garbeam@76 330 c->grav = size.win_gravity;
garbeam@76 331 else
garbeam@76 332 c->grav = NorthWestGravity;
garbeam@76 333 }
garbeam@76 334
garbeam@76 335 void
arg@461 336 updatetitle(Client *c) {
arg@123 337 char **list = NULL;
arg@377 338 int n;
garbeam@76 339 XTextProperty name;
garbeam@76 340
garbeam@76 341 name.nitems = 0;
garbeam@76 342 c->name[0] = 0;
garbeam@77 343 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 344 if(!name.nitems)
garbeam@76 345 XGetWMName(dpy, c->win, &name);
garbeam@76 346 if(!name.nitems)
garbeam@76 347 return;
garbeam@76 348 if(name.encoding == XA_STRING)
garbeam@76 349 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@76 350 else {
garbeam@76 351 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 352 && n > 0 && *list)
garbeam@76 353 {
garbeam@76 354 strncpy(c->name, *list, sizeof(c->name));
garbeam@76 355 XFreeStringList(list);
garbeam@76 356 }
garbeam@76 357 }
garbeam@76 358 XFree(name.value);
garbeam@76 359 resizetitle(c);
garbeam@10 360 }
garbeam@10 361
garbeam@10 362 void
arg@461 363 togglemax(Arg *arg) {
arg@124 364 int ox, oy, ow, oh;
arg@400 365 Client *c;
arg@124 366 XEvent ev;
arg@124 367
arg@124 368 if(!sel)
arg@124 369 return;
arg@124 370
arg@400 371 if((maximized = !maximized)) {
arg@124 372 ox = sel->x;
arg@124 373 oy = sel->y;
arg@124 374 ow = sel->w;
arg@124 375 oh = sel->h;
arg@124 376 sel->x = sx;
arg@124 377 sel->y = sy + bh;
arg@164 378 sel->w = sw - 2;
arg@164 379 sel->h = sh - 2 - bh;
arg@124 380
arg@270 381 restack();
arg@400 382 for(c = getnext(clients); c; c = getnext(c->next))
arg@400 383 if(c != sel)
arg@400 384 ban(c);
arg@247 385 resize(sel, arrange == dofloat, TopLeft);
arg@124 386
arg@124 387 sel->x = ox;
arg@124 388 sel->y = oy;
arg@124 389 sel->w = ow;
arg@124 390 sel->h = oh;
arg@124 391 }
arg@124 392 else
arg@400 393 arrange(NULL);
arg@124 394 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
arg@124 395 }
arg@124 396
arg@124 397 void
arg@461 398 unmanage(Client *c) {
arg@450 399 Client *nc;
arg@450 400
garbeam@10 401 XGrabServer(dpy);
garbeam@75 402 XSetErrorHandler(xerrordummy);
garbeam@10 403
arg@400 404 detach(c);
arg@448 405 detachstack(c);
arg@400 406 if(sel == c) {
arg@450 407 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 408 focus(nc);
arg@400 409 }
arg@372 410
garbeam@18 411 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@342 412 XDestroyWindow(dpy, c->twin);
garbeam@10 413
arg@178 414 free(c->tags);
garbeam@10 415 free(c);
garbeam@10 416
garbeam@79 417 XSync(dpy, False);
garbeam@74 418 XSetErrorHandler(xerror);
garbeam@10 419 XUngrabServer(dpy);
arg@270 420 arrange(NULL);
garbeam@10 421 }