aewl

annotate client.c @ 364:d955a84b7d4b

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