aewl

annotate client.c @ 168:9d3c91a30fe3

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