aewl

annotate client.c @ 77:38c8f7f7d401

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