dwm-meillo

annotate client.c @ 31:386649deb651

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