aewl

annotate client.c @ 50:148f25ed0ad7

several other additions/fixes, dwm is quite usable already
author Anselm R. Garbe <garbeam@wmii.de>
date Thu, 13 Jul 2006 18:21:38 +0200
parents 466591c2f967
children 035617ee18d1
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@5 5
garbeam@27 6 #include <math.h>
garbeam@10 7 #include <stdlib.h>
garbeam@5 8 #include <string.h>
garbeam@5 9 #include <X11/Xatom.h>
garbeam@32 10 #include <X11/Xutil.h>
garbeam@5 11
garbeam@43 12 #include "dwm.h"
garbeam@5 13
garbeam@49 14 static void (*arrange)(Arg *) = floating;
garbeam@49 15
garbeam@50 16 static void
garbeam@50 17 center(Client *c)
garbeam@50 18 {
garbeam@50 19 XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w / 2, c->h / 2);
garbeam@50 20 }
garbeam@50 21
garbeam@49 22 static Client *
garbeam@49 23 next(Client *c)
garbeam@49 24 {
garbeam@50 25 for(; c && !c->tags[tsel]; c = c->next);
garbeam@49 26 return c;
garbeam@49 27 }
garbeam@49 28
garbeam@50 29 void
garbeam@50 30 zoom(Arg *arg)
garbeam@49 31 {
garbeam@50 32 Client **l;
garbeam@50 33
garbeam@50 34 if(!sel)
garbeam@50 35 return;
garbeam@50 36
garbeam@50 37 for(l = &clients; *l && *l != sel; l = &(*l)->next);
garbeam@50 38 *l = sel->next;
garbeam@50 39
garbeam@50 40 sel->next = clients; /* pop */
garbeam@50 41 clients = sel;
garbeam@50 42 arrange(NULL);
garbeam@50 43 center(sel);
garbeam@50 44 focus(sel);
garbeam@49 45 }
garbeam@31 46
garbeam@27 47 void
garbeam@49 48 max(Arg *arg)
garbeam@28 49 {
garbeam@50 50 if(!sel)
garbeam@28 51 return;
garbeam@50 52 sel->x = sx;
garbeam@50 53 sel->y = sy;
garbeam@50 54 sel->w = sw - 2 * sel->border;
garbeam@50 55 sel->h = sh - 2 * sel->border;
garbeam@50 56 craise(sel);
garbeam@50 57 resize(sel);
garbeam@29 58 discard_events(EnterWindowMask);
garbeam@28 59 }
garbeam@28 60
garbeam@46 61 void
garbeam@50 62 view(Arg *arg)
garbeam@50 63 {
garbeam@50 64 tsel = arg->i;
garbeam@50 65 arrange(NULL);
garbeam@50 66 }
garbeam@50 67
garbeam@50 68 void
garbeam@49 69 tag(Arg *arg)
garbeam@49 70 {
garbeam@50 71 int i, n;
garbeam@50 72 if(!sel)
garbeam@49 73 return;
garbeam@49 74
garbeam@50 75 if(arg->i == tsel) {
garbeam@50 76 for(n = i = 0; i < TLast; i++)
garbeam@50 77 if(sel->tags[i])
garbeam@50 78 n++;
garbeam@50 79 if(n < 2)
garbeam@50 80 return;
garbeam@50 81 }
garbeam@49 82
garbeam@50 83 if(sel->tags[arg->i])
garbeam@50 84 sel->tags[arg->i] = NULL; /* toggle tag */
garbeam@49 85 else
garbeam@50 86 sel->tags[arg->i] = tags[arg->i];
garbeam@49 87 arrange(NULL);
garbeam@49 88 }
garbeam@49 89
garbeam@50 90 static void
garbeam@50 91 ban_client(Client *c)
garbeam@50 92 {
garbeam@50 93 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
garbeam@50 94 XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
garbeam@50 95 }
garbeam@50 96
garbeam@49 97 void
garbeam@49 98 floating(Arg *arg)
garbeam@31 99 {
garbeam@31 100 Client *c;
garbeam@31 101
garbeam@46 102 arrange = floating;
garbeam@50 103 for(c = clients; c; c = c->next) {
garbeam@50 104 if(c->tags[tsel])
garbeam@50 105 resize(c);
garbeam@50 106 else
garbeam@50 107 ban_client(c);
garbeam@50 108 }
garbeam@50 109 if(sel && !sel->tags[tsel]) {
garbeam@50 110 if((sel = next(clients))) {
garbeam@50 111 craise(sel);
garbeam@50 112 focus(sel);
garbeam@50 113 }
garbeam@50 114 }
garbeam@31 115 discard_events(EnterWindowMask);
garbeam@31 116 }
garbeam@31 117
garbeam@46 118 void
garbeam@49 119 tiling(Arg *arg)
garbeam@27 120 {
garbeam@27 121 Client *c;
garbeam@27 122 int n, cols, rows, gw, gh, i, j;
garbeam@27 123 float rt, fd;
garbeam@27 124
garbeam@46 125 arrange = tiling;
garbeam@50 126 for(n = 0, c = clients; c; c = next(c->next), n++);
garbeam@50 127 if(n) {
garbeam@50 128 rt = sqrt(n);
garbeam@50 129 if(modff(rt, &fd) < 0.5)
garbeam@50 130 rows = floor(rt);
garbeam@50 131 else
garbeam@50 132 rows = ceil(rt);
garbeam@50 133 if(rows * rows < n)
garbeam@50 134 cols = rows + 1;
garbeam@50 135 else
garbeam@50 136 cols = rows;
garbeam@50 137
garbeam@50 138 gw = (sw - 2) / cols;
garbeam@50 139 gh = (sh - 2) / rows;
garbeam@50 140 }
garbeam@27 141 else
garbeam@50 142 cols = rows = gw = gh = 0;
garbeam@27 143
garbeam@50 144 for(i = j = 0, c = clients; c; c = c->next) {
garbeam@50 145 if(c->tags[tsel]) {
garbeam@50 146 c->x = i * gw;
garbeam@50 147 c->y = j * gh;
garbeam@50 148 c->w = gw;
garbeam@50 149 c->h = gh;
garbeam@50 150 resize(c);
garbeam@50 151 if(++i == cols) {
garbeam@50 152 j++;
garbeam@50 153 i = 0;
garbeam@50 154 }
garbeam@50 155 }
garbeam@50 156 else
garbeam@50 157 ban_client(c);
garbeam@50 158 }
garbeam@50 159 if(sel && !sel->tags[tsel]) {
garbeam@50 160 if((sel = next(clients))) {
garbeam@50 161 craise(sel);
garbeam@50 162 focus(sel);
garbeam@27 163 }
garbeam@27 164 }
garbeam@29 165 discard_events(EnterWindowMask);
garbeam@27 166 }
garbeam@27 167
garbeam@27 168 void
garbeam@49 169 prevc(Arg *arg)
garbeam@27 170 {
garbeam@49 171 Client *c;
garbeam@27 172
garbeam@50 173 if(!sel)
garbeam@27 174 return;
garbeam@49 175
garbeam@50 176 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
garbeam@49 177 craise(c);
garbeam@50 178 center(c);
garbeam@49 179 focus(c);
garbeam@49 180 }
garbeam@27 181 }
garbeam@27 182
garbeam@27 183 void
garbeam@49 184 nextc(Arg *arg)
garbeam@27 185 {
garbeam@49 186 Client *c;
garbeam@49 187
garbeam@50 188 if(!sel)
garbeam@49 189 return;
garbeam@49 190
garbeam@50 191 if(!(c = next(sel->next)))
garbeam@50 192 c = next(clients);
garbeam@49 193 if(c) {
garbeam@49 194 craise(c);
garbeam@50 195 center(c);
garbeam@50 196 c->revert = sel;
garbeam@49 197 focus(c);
garbeam@49 198 }
garbeam@49 199 }
garbeam@49 200
garbeam@49 201 void
garbeam@49 202 ckill(Arg *arg)
garbeam@49 203 {
garbeam@50 204 if(!sel)
garbeam@27 205 return;
garbeam@50 206 if(sel->proto & WM_PROTOCOL_DELWIN)
garbeam@50 207 send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
garbeam@27 208 else
garbeam@50 209 XKillClient(dpy, sel->win);
garbeam@27 210 }
garbeam@27 211
garbeam@26 212 static void
garbeam@26 213 resize_title(Client *c)
garbeam@26 214 {
garbeam@31 215 int i;
garbeam@31 216
garbeam@31 217 c->tw = 0;
garbeam@31 218 for(i = 0; i < TLast; i++)
garbeam@31 219 if(c->tags[i])
garbeam@43 220 c->tw += textw(c->tags[i]) + dc.font.height;
garbeam@43 221 c->tw += textw(c->name) + dc.font.height;
garbeam@26 222 if(c->tw > c->w)
garbeam@26 223 c->tw = c->w + 2;
garbeam@26 224 c->tx = c->x + c->w - c->tw + 2;
garbeam@26 225 c->ty = c->y;
garbeam@26 226 XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
garbeam@26 227 }
garbeam@18 228
garbeam@13 229 void
garbeam@13 230 update_name(Client *c)
garbeam@5 231 {
garbeam@5 232 XTextProperty name;
garbeam@5 233 int n;
garbeam@7 234 char **list = NULL;
garbeam@5 235
garbeam@5 236 name.nitems = 0;
garbeam@5 237 c->name[0] = 0;
garbeam@5 238 XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
garbeam@5 239 if(!name.nitems)
garbeam@5 240 XGetWMName(dpy, c->win, &name);
garbeam@5 241 if(!name.nitems)
garbeam@5 242 return;
garbeam@5 243 if(name.encoding == XA_STRING)
garbeam@5 244 strncpy(c->name, (char *)name.value, sizeof(c->name));
garbeam@5 245 else {
garbeam@5 246 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@5 247 && n > 0 && *list)
garbeam@5 248 {
garbeam@5 249 strncpy(c->name, *list, sizeof(c->name));
garbeam@5 250 XFreeStringList(list);
garbeam@5 251 }
garbeam@5 252 }
garbeam@5 253 XFree(name.value);
garbeam@26 254 resize_title(c);
garbeam@5 255 }
garbeam@5 256
garbeam@10 257 void
garbeam@20 258 update_size(Client *c)
garbeam@20 259 {
garbeam@20 260 XSizeHints size;
garbeam@20 261 long msize;
garbeam@20 262 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@20 263 size.flags = PSize;
garbeam@20 264 c->flags = size.flags;
garbeam@21 265 if(c->flags & PBaseSize) {
garbeam@21 266 c->basew = size.base_width;
garbeam@21 267 c->baseh = size.base_height;
garbeam@21 268 }
garbeam@21 269 else
garbeam@21 270 c->basew = c->baseh = 0;
garbeam@21 271 if(c->flags & PResizeInc) {
garbeam@21 272 c->incw = size.width_inc;
garbeam@21 273 c->inch = size.height_inc;
garbeam@21 274 }
garbeam@21 275 else
garbeam@21 276 c->incw = c->inch = 0;
garbeam@21 277 if(c->flags & PMaxSize) {
garbeam@21 278 c->maxw = size.max_width;
garbeam@21 279 c->maxh = size.max_height;
garbeam@21 280 }
garbeam@21 281 else
garbeam@21 282 c->maxw = c->maxh = 0;
garbeam@21 283 if(c->flags & PMinSize) {
garbeam@21 284 c->minw = size.min_width;
garbeam@21 285 c->minh = size.min_height;
garbeam@21 286 }
garbeam@21 287 else
garbeam@21 288 c->minw = c->minh = 0;
garbeam@29 289 if(c->flags & PWinGravity)
garbeam@29 290 c->grav = size.win_gravity;
garbeam@29 291 else
garbeam@29 292 c->grav = NorthWestGravity;
garbeam@20 293 }
garbeam@20 294
garbeam@20 295 void
garbeam@32 296 craise(Client *c)
garbeam@26 297 {
garbeam@26 298 XRaiseWindow(dpy, c->win);
garbeam@26 299 XRaiseWindow(dpy, c->title);
garbeam@26 300 }
garbeam@26 301
garbeam@26 302 void
garbeam@26 303 lower(Client *c)
garbeam@26 304 {
garbeam@26 305 XLowerWindow(dpy, c->title);
garbeam@26 306 XLowerWindow(dpy, c->win);
garbeam@26 307 }
garbeam@26 308
garbeam@26 309 void
garbeam@13 310 focus(Client *c)
garbeam@13 311 {
garbeam@50 312 if(sel && sel != c) {
garbeam@50 313 XSetWindowBorder(dpy, sel->win, dc.bg);
garbeam@50 314 XMapWindow(dpy, sel->title);
garbeam@50 315 draw_client(sel);
garbeam@21 316 }
garbeam@50 317 sel = c;
garbeam@21 318 XUnmapWindow(dpy, c->title);
garbeam@43 319 XSetWindowBorder(dpy, c->win, dc.fg);
garbeam@27 320 draw_client(c);
garbeam@26 321 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
garbeam@13 322 XFlush(dpy);
garbeam@45 323 discard_events(EnterWindowMask);
garbeam@13 324 }
garbeam@13 325
garbeam@13 326 void
garbeam@10 327 manage(Window w, XWindowAttributes *wa)
garbeam@5 328 {
garbeam@50 329 Client *c, **l;
garbeam@5 330 XSetWindowAttributes twa;
garbeam@5 331
garbeam@5 332 c = emallocz(sizeof(Client));
garbeam@5 333 c->win = w;
garbeam@22 334 c->tx = c->x = wa->x;
garbeam@22 335 c->ty = c->y = wa->y;
garbeam@22 336 c->tw = c->w = wa->width;
garbeam@20 337 c->h = wa->height;
garbeam@32 338 c->th = th;
garbeam@29 339 c->border = 1;
garbeam@50 340 c->proto = win_proto(c->win);
garbeam@20 341 update_size(c);
garbeam@26 342 XSelectInput(dpy, c->win,
garbeam@26 343 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@5 344 XGetTransientForHint(dpy, c->win, &c->trans);
garbeam@5 345 twa.override_redirect = 1;
garbeam@5 346 twa.background_pixmap = ParentRelative;
garbeam@23 347 twa.event_mask = ExposureMask;
garbeam@5 348
garbeam@31 349 c->tags[tsel] = tags[tsel];
garbeam@22 350 c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 351 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 352 DefaultVisual(dpy, screen),
garbeam@5 353 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
garbeam@31 354
garbeam@21 355 update_name(c);
garbeam@49 356
garbeam@50 357 for(l = &clients; *l; l = &(*l)->next);
garbeam@50 358 c->next = *l; /* *l == nil */
garbeam@50 359 *l = c;
garbeam@49 360
garbeam@43 361 XSetWindowBorderWidth(dpy, c->win, 1);
garbeam@26 362 XMapRaised(dpy, c->win);
garbeam@26 363 XMapRaised(dpy, c->title);
garbeam@19 364 XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 365 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 366 XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@19 367 GrabModeAsync, GrabModeSync, None, None);
garbeam@19 368 XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
garbeam@18 369 GrabModeAsync, GrabModeSync, None, None);
garbeam@46 370 arrange(NULL);
garbeam@50 371 center(c);
garbeam@13 372 focus(c);
garbeam@5 373 }
garbeam@9 374
garbeam@18 375 void
garbeam@29 376 gravitate(Client *c, Bool invert)
garbeam@29 377 {
garbeam@29 378 int dx = 0, dy = 0;
garbeam@29 379
garbeam@29 380 switch(c->grav) {
garbeam@29 381 case StaticGravity:
garbeam@29 382 case NorthWestGravity:
garbeam@29 383 case NorthGravity:
garbeam@29 384 case NorthEastGravity:
garbeam@29 385 dy = c->border;
garbeam@29 386 break;
garbeam@29 387 case EastGravity:
garbeam@29 388 case CenterGravity:
garbeam@29 389 case WestGravity:
garbeam@29 390 dy = -(c->h / 2) + c->border;
garbeam@29 391 break;
garbeam@29 392 case SouthEastGravity:
garbeam@29 393 case SouthGravity:
garbeam@29 394 case SouthWestGravity:
garbeam@29 395 dy = -c->h;
garbeam@29 396 break;
garbeam@29 397 default:
garbeam@29 398 break;
garbeam@29 399 }
garbeam@29 400
garbeam@29 401 switch (c->grav) {
garbeam@29 402 case StaticGravity:
garbeam@29 403 case NorthWestGravity:
garbeam@29 404 case WestGravity:
garbeam@29 405 case SouthWestGravity:
garbeam@29 406 dx = c->border;
garbeam@29 407 break;
garbeam@29 408 case NorthGravity:
garbeam@29 409 case CenterGravity:
garbeam@29 410 case SouthGravity:
garbeam@29 411 dx = -(c->w / 2) + c->border;
garbeam@29 412 break;
garbeam@29 413 case NorthEastGravity:
garbeam@29 414 case EastGravity:
garbeam@29 415 case SouthEastGravity:
garbeam@29 416 dx = -(c->w + c->border);
garbeam@29 417 break;
garbeam@29 418 default:
garbeam@29 419 break;
garbeam@29 420 }
garbeam@29 421
garbeam@29 422 if(invert) {
garbeam@29 423 dx = -dx;
garbeam@29 424 dy = -dy;
garbeam@29 425 }
garbeam@29 426 c->x += dx;
garbeam@29 427 c->y += dy;
garbeam@29 428 }
garbeam@29 429
garbeam@31 430
garbeam@29 431 void
garbeam@18 432 resize(Client *c)
garbeam@18 433 {
garbeam@18 434 XConfigureEvent e;
garbeam@18 435
garbeam@31 436 if(c->incw)
garbeam@31 437 c->w -= (c->w - c->basew) % c->incw;
garbeam@31 438 if(c->inch)
garbeam@31 439 c->h -= (c->h - c->baseh) % c->inch;
garbeam@31 440 if(c->minw && c->w < c->minw)
garbeam@31 441 c->w = c->minw;
garbeam@31 442 if(c->minh && c->h < c->minh)
garbeam@31 443 c->h = c->minh;
garbeam@31 444 if(c->maxw && c->w > c->maxw)
garbeam@31 445 c->w = c->maxw;
garbeam@31 446 if(c->maxh && c->h > c->maxh)
garbeam@31 447 c->h = c->maxh;
garbeam@26 448 resize_title(c);
garbeam@20 449 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
garbeam@18 450 e.type = ConfigureNotify;
garbeam@18 451 e.event = c->win;
garbeam@18 452 e.window = c->win;
garbeam@20 453 e.x = c->x;
garbeam@20 454 e.y = c->y;
garbeam@20 455 e.width = c->w;
garbeam@20 456 e.height = c->h;
garbeam@29 457 e.border_width = c->border;
garbeam@18 458 e.above = None;
garbeam@18 459 e.override_redirect = False;
garbeam@18 460 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
garbeam@18 461 XFlush(dpy);
garbeam@18 462 }
garbeam@18 463
garbeam@10 464 static int
garbeam@33 465 dummy_error_handler(Display *dsply, XErrorEvent *err)
garbeam@10 466 {
garbeam@10 467 return 0;
garbeam@10 468 }
garbeam@10 469
garbeam@10 470 void
garbeam@10 471 unmanage(Client *c)
garbeam@10 472 {
garbeam@50 473 Client **l;
garbeam@50 474
garbeam@10 475 XGrabServer(dpy);
garbeam@10 476 XSetErrorHandler(dummy_error_handler);
garbeam@10 477
garbeam@18 478 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
garbeam@10 479 XDestroyWindow(dpy, c->title);
garbeam@10 480
garbeam@50 481 for(l = &clients; *l && *l != c; l = &(*l)->next);
garbeam@50 482 *l = c->next;
garbeam@50 483 for(l = &clients; *l; l = &(*l)->next)
garbeam@50 484 if((*l)->revert == c)
garbeam@50 485 (*l)->revert = NULL;
garbeam@50 486 if(sel == c)
garbeam@50 487 sel = sel->revert ? sel->revert : clients;
garbeam@49 488
garbeam@10 489 free(c);
garbeam@10 490
garbeam@10 491 XFlush(dpy);
garbeam@10 492 XSetErrorHandler(error_handler);
garbeam@10 493 XUngrabServer(dpy);
garbeam@46 494 arrange(NULL);
garbeam@50 495 if(sel)
garbeam@50 496 focus(sel);
garbeam@10 497 }
garbeam@10 498
garbeam@23 499 Client *
garbeam@23 500 gettitle(Window w)
garbeam@23 501 {
garbeam@23 502 Client *c;
garbeam@50 503 for(c = clients; c; c = c->next)
garbeam@23 504 if(c->title == w)
garbeam@23 505 return c;
garbeam@23 506 return NULL;
garbeam@23 507 }
garbeam@10 508
garbeam@9 509 Client *
garbeam@9 510 getclient(Window w)
garbeam@9 511 {
garbeam@9 512 Client *c;
garbeam@50 513 for(c = clients; c; c = c->next)
garbeam@9 514 if(c->win == w)
garbeam@9 515 return c;
garbeam@9 516 return NULL;
garbeam@9 517 }
garbeam@13 518
garbeam@14 519 void
garbeam@14 520 draw_client(Client *c)
garbeam@14 521 {
garbeam@31 522 int i;
garbeam@50 523 if(c == sel)
garbeam@26 524 return;
garbeam@14 525
garbeam@34 526 dc.x = dc.y = 0;
garbeam@34 527 dc.h = c->th;
garbeam@14 528
garbeam@34 529 dc.w = 0;
garbeam@31 530 for(i = 0; i < TLast; i++) {
garbeam@31 531 if(c->tags[i]) {
garbeam@34 532 dc.x += dc.w;
garbeam@43 533 dc.w = textw(c->tags[i]) + dc.font.height;
garbeam@34 534 draw(True, c->tags[i]);
garbeam@31 535 }
garbeam@31 536 }
garbeam@34 537 dc.x += dc.w;
garbeam@43 538 dc.w = textw(c->name) + dc.font.height;
garbeam@34 539 draw(True, c->name);
garbeam@34 540 XCopyArea(dpy, dc.drawable, c->title, dc.gc,
garbeam@22 541 0, 0, c->tw, c->th, 0, 0);
garbeam@21 542 XFlush(dpy);
garbeam@14 543 }