dwm-meillo

annotate client.c @ 638:276238ad9516

small simplification to dotile() (thx to Ross for this hint)
author arg@mig29
date Wed, 20 Dec 2006 12:07:05 +0100
parents 264f02dd2ad0
children 226ef912c093
rev   line source
arg@532 1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
garbeam@5 2 * See LICENSE file for license details.
garbeam@5 3 */
garbeam@76 4 #include "dwm.h"
garbeam@10 5 #include <stdlib.h>
garbeam@5 6 #include <string.h>
garbeam@5 7 #include <X11/Xatom.h>
garbeam@32 8 #include <X11/Xutil.h>
garbeam@5 9
garbeam@76 10 /* static functions */
garbeam@50 11
garbeam@26 12 static void
arg@461 13 detachstack(Client *c) {
arg@446 14 Client **tc;
arg@446 15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
arg@446 16 *tc = c->snext;
arg@446 17 }
arg@446 18
arg@446 19 static void
arg@461 20 grabbuttons(Client *c, Bool focused) {
arg@372 21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@372 22
arg@452 23 if(focused) {
arg@372 24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
arg@372 25 GrabModeAsync, GrabModeSync, None, None);
arg@372 26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 27 GrabModeAsync, GrabModeSync, None, None);
arg@372 28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 29 GrabModeAsync, GrabModeSync, None, None);
arg@372 30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 31 GrabModeAsync, GrabModeSync, None, None);
arg@372 32
arg@372 33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
arg@372 34 GrabModeAsync, GrabModeSync, None, None);
arg@372 35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 36 GrabModeAsync, GrabModeSync, None, None);
arg@372 37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 38 GrabModeAsync, GrabModeSync, None, None);
arg@372 39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 40 GrabModeAsync, GrabModeSync, None, None);
arg@372 41
arg@372 42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
arg@372 43 GrabModeAsync, GrabModeSync, None, None);
arg@372 44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
arg@372 45 GrabModeAsync, GrabModeSync, None, None);
arg@372 46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
arg@372 47 GrabModeAsync, GrabModeSync, None, None);
arg@372 48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
arg@372 49 GrabModeAsync, GrabModeSync, None, None);
arg@372 50 }
arg@372 51 else
arg@372 52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
arg@372 53 GrabModeAsync, GrabModeSync, None, None);
arg@318 54 }
arg@318 55
garbeam@76 56 static int
arg@461 57 xerrordummy(Display *dsply, XErrorEvent *ee) {
garbeam@76 58 return 0;
garbeam@5 59 }
garbeam@5 60
garbeam@76 61 /* extern functions */
garbeam@20 62
garbeam@20 63 void
arg@461 64 ban(Client *c) {
arg@115 65 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@342 66 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
garbeam@26 67 }
garbeam@26 68
garbeam@26 69 void
arg@491 70 configure(Client *c) {
arg@491 71 XEvent synev;
arg@491 72
arg@491 73 synev.type = ConfigureNotify;
arg@491 74 synev.xconfigure.display = dpy;
arg@491 75 synev.xconfigure.event = c->win;
arg@491 76 synev.xconfigure.window = c->win;
arg@491 77 synev.xconfigure.x = c->x;
arg@491 78 synev.xconfigure.y = c->y;
arg@491 79 synev.xconfigure.width = c->w;
arg@491 80 synev.xconfigure.height = c->h;
arg@491 81 synev.xconfigure.border_width = c->border;
arg@491 82 synev.xconfigure.above = None;
arg@491 83 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@491 84 }
arg@491 85
arg@491 86 void
arg@461 87 focus(Client *c) {
arg@400 88 Client *old;
arg@286 89
arg@464 90 if(!issel || (c && !isvisible(c)))
arg@239 91 return;
arg@319 92 if(!sel)
arg@319 93 sel = c;
arg@319 94 else if(sel != c) {
arg@400 95 old = sel;
arg@319 96 sel = c;
arg@400 97 if(old) {
arg@400 98 grabbuttons(old, False);
arg@400 99 drawtitle(old);
arg@400 100 }
arg@318 101 }
arg@400 102 if(c) {
arg@446 103 detachstack(c);
arg@446 104 c->snext = stack;
arg@446 105 stack = c;
arg@400 106 grabbuttons(c, True);
arg@400 107 drawtitle(c);
arg@400 108 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 109 }
arg@400 110 else
arg@400 111 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 112 }
garbeam@13 113
garbeam@76 114 Client *
arg@461 115 getclient(Window w) {
garbeam@76 116 Client *c;
arg@123 117
garbeam@76 118 for(c = clients; c; c = c->next)
garbeam@76 119 if(c->win == w)
garbeam@76 120 return c;
garbeam@76 121 return NULL;
garbeam@76 122 }
garbeam@76 123
garbeam@76 124 Client *
arg@461 125 getctitle(Window w) {
garbeam@76 126 Client *c;
arg@123 127
garbeam@76 128 for(c = clients; c; c = c->next)
arg@342 129 if(c->twin == w)
garbeam@76 130 return c;
garbeam@76 131 return NULL;
garbeam@76 132 }
garbeam@76 133
garbeam@76 134 void
arg@461 135 killclient(Arg *arg) {
garbeam@76 136 if(!sel)
garbeam@76 137 return;
arg@157 138 if(sel->proto & PROTODELWIN)
garbeam@77 139 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 140 else
garbeam@76 141 XKillClient(dpy, sel->win);
garbeam@76 142 }
garbeam@76 143
garbeam@76 144 void
arg@461 145 manage(Window w, XWindowAttributes *wa) {
arg@431 146 Client *c;
arg@123 147 Window trans;
garbeam@5 148 XSetWindowAttributes twa;
garbeam@5 149
garbeam@5 150 c = emallocz(sizeof(Client));
arg@178 151 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 152 c->win = w;
arg@546 153 c->border = 0;
arg@544 154 c->x = c->tx = wa->x;
arg@544 155 c->y = c->ty = wa->y;
arg@115 156 c->w = c->tw = wa->width;
arg@115 157 c->h = wa->height;
arg@115 158 c->th = bh;
arg@454 159 updatesize(c);
arg@502 160 if(c->x + c->w + 2 * BORDERPX > sw)
arg@502 161 c->x = sw - c->w - 2 * BORDERPX;
arg@517 162 if(c->x < sx)
arg@517 163 c->x = sx;
arg@502 164 if(c->y + c->h + 2 * BORDERPX > sh)
arg@517 165 c->y = sh - c->h - 2 * BORDERPX;
arg@163 166 if(c->h != sh && c->y < bh)
arg@314 167 c->y = bh;
garbeam@75 168 c->proto = getproto(c->win);
garbeam@26 169 XSelectInput(dpy, c->win,
arg@127 170 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 171 XGetTransientForHint(dpy, c->win, &trans);
garbeam@5 172 twa.override_redirect = 1;
garbeam@5 173 twa.background_pixmap = ParentRelative;
arg@161 174 twa.event_mask = ExposureMask | EnterWindowMask;
arg@342 175 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
garbeam@20 176 0, DefaultDepth(dpy, screen), CopyFromParent,
garbeam@5 177 DefaultVisual(dpy, screen),
garbeam@5 178 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
arg@372 179 grabbuttons(c, False);
arg@500 180 updatetitle(c);
arg@431 181 settags(c, getclient(trans));
garbeam@80 182 if(!c->isfloat)
arg@549 183 c->isfloat = trans || c->isfixed;
arg@500 184 resizetitle(c);
arg@381 185 if(clients)
arg@381 186 clients->prev = c;
arg@381 187 c->next = clients;
arg@446 188 c->snext = stack;
arg@446 189 stack = clients = c;
arg@408 190 ban(c);
arg@270 191 XMapWindow(dpy, c->win);
arg@342 192 XMapWindow(dpy, c->twin);
arg@261 193 if(isvisible(c))
garbeam@51 194 focus(c);
arg@533 195 arrange();
garbeam@94 196 }
garbeam@94 197
garbeam@94 198 void
arg@461 199 resize(Client *c, Bool sizehints, Corner sticky) {
arg@123 200 int bottom = c->y + c->h;
arg@123 201 int right = c->x + c->w;
arg@163 202 XWindowChanges wc;
garbeam@18 203
arg@129 204 if(sizehints) {
garbeam@52 205 if(c->incw)
arg@115 206 c->w -= (c->w - c->basew) % c->incw;
garbeam@52 207 if(c->inch)
arg@115 208 c->h -= (c->h - c->baseh) % c->inch;
arg@129 209 if(c->minw && c->w < c->minw)
arg@129 210 c->w = c->minw;
arg@129 211 if(c->minh && c->h < c->minh)
arg@129 212 c->h = c->minh;
arg@129 213 if(c->maxw && c->w > c->maxw)
arg@129 214 c->w = c->maxw;
arg@129 215 if(c->maxh && c->h > c->maxh)
arg@129 216 c->h = c->maxh;
garbeam@52 217 }
arg@105 218 if(sticky == TopRight || sticky == BotRight)
arg@115 219 c->x = right - c->w;
arg@105 220 if(sticky == BotLeft || sticky == BotRight)
arg@115 221 c->y = bottom - c->h;
arg@465 222 /* offscreen appearance fixes */
arg@517 223 if(c->x + c->w < sx)
arg@517 224 c->x = sx;
arg@465 225 if(c->y + c->h < bh)
arg@465 226 c->y = bh;
arg@465 227 if(c->x > sw)
arg@465 228 c->x = sw - c->w;
arg@465 229 if(c->y > sh)
arg@465 230 c->y = sh - c->h;
garbeam@75 231 resizetitle(c);
arg@163 232 wc.x = c->x;
arg@163 233 wc.y = c->y;
arg@163 234 wc.width = c->w;
arg@163 235 wc.height = c->h;
arg@164 236 if(c->w == sw && c->h == sh)
arg@164 237 wc.border_width = 0;
arg@164 238 else
arg@502 239 wc.border_width = BORDERPX;
arg@452 240 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
arg@630 241 configure(c);
garbeam@79 242 XSync(dpy, False);
garbeam@18 243 }
garbeam@18 244
garbeam@76 245 void
arg@500 246 resizetitle(Client *c) {
arg@500 247 c->tw = textw(c->name);
arg@500 248 if(c->tw > c->w)
arg@502 249 c->tw = c->w + 2 * BORDERPX;
arg@502 250 c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
arg@500 251 c->ty = c->y;
arg@500 252 if(isvisible(c))
arg@500 253 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
arg@500 254 else
arg@500 255 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
arg@500 256 }
arg@500 257
arg@500 258 void
arg@461 259 updatesize(Client *c) {
arg@123 260 long msize;
garbeam@76 261 XSizeHints size;
arg@123 262
garbeam@76 263 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 264 size.flags = PSize;
garbeam@76 265 c->flags = size.flags;
garbeam@76 266 if(c->flags & PBaseSize) {
garbeam@76 267 c->basew = size.base_width;
garbeam@76 268 c->baseh = size.base_height;
garbeam@76 269 }
garbeam@76 270 else
garbeam@76 271 c->basew = c->baseh = 0;
garbeam@76 272 if(c->flags & PResizeInc) {
garbeam@76 273 c->incw = size.width_inc;
garbeam@76 274 c->inch = size.height_inc;
garbeam@76 275 }
garbeam@76 276 else
garbeam@76 277 c->incw = c->inch = 0;
garbeam@76 278 if(c->flags & PMaxSize) {
garbeam@76 279 c->maxw = size.max_width;
garbeam@76 280 c->maxh = size.max_height;
garbeam@76 281 }
garbeam@76 282 else
garbeam@76 283 c->maxw = c->maxh = 0;
garbeam@76 284 if(c->flags & PMinSize) {
garbeam@76 285 c->minw = size.min_width;
garbeam@76 286 c->minh = size.min_height;
garbeam@76 287 }
garbeam@76 288 else
garbeam@76 289 c->minw = c->minh = 0;
arg@550 290 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
arg@550 291 c->maxw == c->minw && c->maxh == c->minh);
garbeam@76 292 }
garbeam@76 293
garbeam@76 294 void
arg@461 295 updatetitle(Client *c) {
arg@123 296 char **list = NULL;
arg@377 297 int n;
garbeam@76 298 XTextProperty name;
garbeam@76 299
garbeam@76 300 name.nitems = 0;
garbeam@76 301 c->name[0] = 0;
garbeam@77 302 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 303 if(!name.nitems)
garbeam@76 304 XGetWMName(dpy, c->win, &name);
garbeam@76 305 if(!name.nitems)
garbeam@76 306 return;
garbeam@76 307 if(name.encoding == XA_STRING)
arg@581 308 strncpy(c->name, (char *)name.value, sizeof c->name);
garbeam@76 309 else {
garbeam@76 310 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 311 && n > 0 && *list)
garbeam@76 312 {
arg@581 313 strncpy(c->name, *list, sizeof c->name);
garbeam@76 314 XFreeStringList(list);
garbeam@76 315 }
garbeam@76 316 }
garbeam@76 317 XFree(name.value);
garbeam@10 318 }
garbeam@10 319
garbeam@10 320 void
arg@461 321 unmanage(Client *c) {
arg@450 322 Client *nc;
arg@450 323
arg@472 324 /* The server grab construct avoids race conditions. */
garbeam@10 325 XGrabServer(dpy);
garbeam@75 326 XSetErrorHandler(xerrordummy);
arg@400 327 detach(c);
arg@448 328 detachstack(c);
arg@400 329 if(sel == c) {
arg@450 330 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 331 focus(nc);
arg@400 332 }
garbeam@18 333 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@342 334 XDestroyWindow(dpy, c->twin);
arg@178 335 free(c->tags);
garbeam@10 336 free(c);
garbeam@79 337 XSync(dpy, False);
garbeam@74 338 XSetErrorHandler(xerror);
garbeam@10 339 XUngrabServer(dpy);
arg@533 340 arrange();
garbeam@10 341 }