aewl

annotate client.c @ 310:f0b609fca3de

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