dwm-meillo

annotate client.c @ 124:75576e44c1d8

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