aewl

annotate client.c @ 284:5f5c56e104de

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