dwm-meillo

annotate client.c @ 46:58307ad56ec1

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