aewl

annotate client.c @ 49:466591c2f967

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