aewl

annotate client.c @ 733:1950833a5614

yet another fix
author Anselm R. Garbe <arg@suckless.org>
date Tue, 06 Feb 2007 15:29:19 +0100
parents a642068c4834
children 6283adb1fcf2
rev   line source
arg@644 1 /* (C)opyright MMVI-MMVII 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
arg@730 10 /* static */
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
arg@725 56 static void
arg@725 57 setclientstate(Client *c, long state) {
arg@725 58 long data[] = {state, None};
arg@725 59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
arg@725 60 PropModeReplace, (unsigned char *)data, 2);
arg@725 61 }
arg@725 62
garbeam@76 63 static int
arg@461 64 xerrordummy(Display *dsply, XErrorEvent *ee) {
garbeam@76 65 return 0;
garbeam@5 66 }
garbeam@5 67
arg@730 68 /* extern */
garbeam@20 69
garbeam@20 70 void
arg@491 71 configure(Client *c) {
arg@491 72 XEvent synev;
arg@491 73
arg@491 74 synev.type = ConfigureNotify;
arg@491 75 synev.xconfigure.display = dpy;
arg@491 76 synev.xconfigure.event = c->win;
arg@491 77 synev.xconfigure.window = c->win;
arg@491 78 synev.xconfigure.x = c->x;
arg@491 79 synev.xconfigure.y = c->y;
arg@491 80 synev.xconfigure.width = c->w;
arg@491 81 synev.xconfigure.height = c->h;
arg@491 82 synev.xconfigure.border_width = c->border;
arg@491 83 synev.xconfigure.above = None;
arg@491 84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
arg@491 85 }
arg@491 86
arg@491 87 void
arg@461 88 focus(Client *c) {
arg@709 89 if(c && !isvisible(c))
arg@239 90 return;
arg@712 91 if(sel && sel != c) {
arg@712 92 grabbuttons(sel, False);
arg@712 93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
arg@318 94 }
arg@400 95 if(c) {
arg@711 96 detachstack(c);
arg@711 97 c->snext = stack;
arg@711 98 stack = c;
arg@711 99 grabbuttons(c, True);
arg@714 100 }
arg@714 101 sel = c;
arg@714 102 drawstatus();
arg@716 103 if(!selscreen)
arg@714 104 return;
arg@715 105 if(c) {
arg@711 106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
arg@711 107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
arg@400 108 }
arg@712 109 else
arg@400 110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
garbeam@13 111 }
garbeam@13 112
garbeam@76 113 Client *
arg@461 114 getclient(Window w) {
garbeam@76 115 Client *c;
arg@123 116
garbeam@76 117 for(c = clients; c; c = c->next)
garbeam@76 118 if(c->win == w)
garbeam@76 119 return c;
garbeam@76 120 return NULL;
garbeam@76 121 }
garbeam@76 122
garbeam@76 123 void
arg@461 124 killclient(Arg *arg) {
garbeam@76 125 if(!sel)
garbeam@76 126 return;
arg@157 127 if(sel->proto & PROTODELWIN)
garbeam@77 128 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
garbeam@76 129 else
garbeam@76 130 XKillClient(dpy, sel->win);
garbeam@76 131 }
garbeam@76 132
garbeam@76 133 void
arg@461 134 manage(Window w, XWindowAttributes *wa) {
arg@431 135 Client *c;
arg@123 136 Window trans;
garbeam@5 137
garbeam@5 138 c = emallocz(sizeof(Client));
arg@178 139 c->tags = emallocz(ntags * sizeof(Bool));
garbeam@5 140 c->win = w;
arg@687 141 c->x = wa->x;
arg@687 142 c->y = wa->y;
arg@687 143 c->w = wa->width;
arg@115 144 c->h = wa->height;
arg@708 145 if(c->w == sw && c->h == sh) {
arg@708 146 c->border = 0;
arg@708 147 c->x = sx;
arg@708 148 c->y = sy;
arg@708 149 }
arg@708 150 else {
arg@708 151 c->border = BORDERPX;
arg@718 152 if(c->x + c->w + 2 * c->border > wax + waw)
arg@718 153 c->x = wax + waw - c->w - 2 * c->border;
arg@718 154 if(c->y + c->h + 2 * c->border > way + wah)
arg@718 155 c->y = way + wah - c->h - 2 * c->border;
arg@708 156 if(c->x < wax)
arg@708 157 c->x = wax;
arg@708 158 if(c->y < way)
arg@708 159 c->y = way;
arg@708 160 }
arg@639 161 updatesizehints(c);
garbeam@75 162 c->proto = getproto(c->win);
garbeam@26 163 XSelectInput(dpy, c->win,
arg@127 164 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
garbeam@53 165 XGetTransientForHint(dpy, c->win, &trans);
arg@372 166 grabbuttons(c, False);
arg@715 167 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
arg@500 168 updatetitle(c);
arg@431 169 settags(c, getclient(trans));
garbeam@80 170 if(!c->isfloat)
arg@549 171 c->isfloat = trans || c->isfixed;
arg@381 172 if(clients)
arg@381 173 clients->prev = c;
arg@381 174 c->next = clients;
arg@446 175 c->snext = stack;
arg@446 176 stack = clients = c;
arg@687 177 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
arg@270 178 XMapWindow(dpy, c->win);
arg@725 179 setclientstate(c, NormalState);
arg@261 180 if(isvisible(c))
garbeam@51 181 focus(c);
arg@533 182 arrange();
garbeam@94 183 }
garbeam@94 184
garbeam@94 185 void
arg@708 186 resize(Client *c, Bool sizehints) {
arg@733 187 float actual, dx, dy, max, min;
arg@163 188 XWindowChanges wc;
garbeam@18 189
arg@721 190 if(c->w <= 0 || c->h <= 0)
arg@721 191 return;
arg@129 192 if(sizehints) {
arg@129 193 if(c->minw && c->w < c->minw)
arg@129 194 c->w = c->minw;
arg@129 195 if(c->minh && c->h < c->minh)
arg@129 196 c->h = c->minh;
arg@129 197 if(c->maxw && c->w > c->maxw)
arg@129 198 c->w = c->maxw;
arg@129 199 if(c->maxh && c->h > c->maxh)
arg@129 200 c->h = c->maxh;
arg@731 201 /* inspired by algorithm from fluxbox */
arg@731 202 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
arg@731 203 dx = (float)(c->w - c->basew);
arg@731 204 dy = (float)(c->h - c->baseh);
arg@731 205 min = (float)(c->minax) / (float)(c->minay);
arg@731 206 max = (float)(c->maxax) / (float)(c->maxay);
arg@731 207 actual = dx / dy;
arg@731 208 if(max > 0 && min > 0 && actual > 0) {
arg@731 209 if(actual < min) {
arg@732 210 dy = (dx * min + dy) / (min * min + 1);
arg@732 211 dx = dy * min;
arg@731 212 c->w = (int)dx + c->basew;
arg@731 213 c->h = (int)dy + c->baseh;
arg@731 214 }
arg@731 215 else if(actual > max) {
arg@732 216 dy = (dx * min + dy) / (max * max + 1);
arg@732 217 dx = dy * min;
arg@731 218 c->w = (int)dx + c->basew;
arg@731 219 c->h = (int)dy + c->baseh;
arg@731 220 }
arg@731 221 }
arg@731 222 }
arg@731 223 if(c->incw)
arg@731 224 c->w -= (c->w - c->basew) % c->incw;
arg@731 225 if(c->inch)
arg@731 226 c->h -= (c->h - c->baseh) % c->inch;
garbeam@52 227 }
arg@708 228 if(c->w == sw && c->h == sh)
arg@708 229 c->border = 0;
arg@708 230 else
arg@708 231 c->border = BORDERPX;
arg@465 232 /* offscreen appearance fixes */
arg@718 233 if(c->x > sw)
arg@718 234 c->x = sw - c->w - 2 * c->border;
arg@718 235 if(c->y > sh)
arg@718 236 c->y = sh - c->h - 2 * c->border;
arg@708 237 if(c->x + c->w + 2 * c->border < sx)
arg@517 238 c->x = sx;
arg@708 239 if(c->y + c->h + 2 * c->border < sy)
arg@708 240 c->y = sy;
arg@163 241 wc.x = c->x;
arg@163 242 wc.y = c->y;
arg@163 243 wc.width = c->w;
arg@163 244 wc.height = c->h;
arg@708 245 wc.border_width = c->border;
arg@452 246 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
arg@630 247 configure(c);
garbeam@79 248 XSync(dpy, False);
garbeam@18 249 }
garbeam@18 250
garbeam@76 251 void
arg@639 252 updatesizehints(Client *c) {
arg@123 253 long msize;
garbeam@76 254 XSizeHints size;
arg@123 255
garbeam@76 256 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
garbeam@76 257 size.flags = PSize;
garbeam@76 258 c->flags = size.flags;
garbeam@76 259 if(c->flags & PBaseSize) {
garbeam@76 260 c->basew = size.base_width;
garbeam@76 261 c->baseh = size.base_height;
garbeam@76 262 }
garbeam@76 263 else
garbeam@76 264 c->basew = c->baseh = 0;
garbeam@76 265 if(c->flags & PResizeInc) {
garbeam@76 266 c->incw = size.width_inc;
garbeam@76 267 c->inch = size.height_inc;
garbeam@76 268 }
garbeam@76 269 else
garbeam@76 270 c->incw = c->inch = 0;
garbeam@76 271 if(c->flags & PMaxSize) {
garbeam@76 272 c->maxw = size.max_width;
garbeam@76 273 c->maxh = size.max_height;
garbeam@76 274 }
garbeam@76 275 else
garbeam@76 276 c->maxw = c->maxh = 0;
garbeam@76 277 if(c->flags & PMinSize) {
garbeam@76 278 c->minw = size.min_width;
garbeam@76 279 c->minh = size.min_height;
garbeam@76 280 }
garbeam@76 281 else
garbeam@76 282 c->minw = c->minh = 0;
arg@731 283 if(c->flags & PAspect) {
arg@731 284 c->minax = size.min_aspect.x;
arg@731 285 c->minay = size.min_aspect.y;
arg@731 286 c->maxax = size.max_aspect.x;
arg@731 287 c->maxay = size.max_aspect.y;
arg@731 288 }
arg@731 289 else
arg@731 290 c->minax = c->minay = c->maxax = c->maxay = 0;
arg@550 291 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
arg@550 292 c->maxw == c->minw && c->maxh == c->minh);
garbeam@76 293 }
garbeam@76 294
garbeam@76 295 void
arg@461 296 updatetitle(Client *c) {
arg@123 297 char **list = NULL;
arg@377 298 int n;
garbeam@76 299 XTextProperty name;
garbeam@76 300
garbeam@76 301 name.nitems = 0;
garbeam@76 302 c->name[0] = 0;
garbeam@77 303 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
garbeam@76 304 if(!name.nitems)
garbeam@76 305 XGetWMName(dpy, c->win, &name);
garbeam@76 306 if(!name.nitems)
garbeam@76 307 return;
garbeam@76 308 if(name.encoding == XA_STRING)
arg@581 309 strncpy(c->name, (char *)name.value, sizeof c->name);
garbeam@76 310 else {
garbeam@76 311 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
garbeam@76 312 && n > 0 && *list)
garbeam@76 313 {
arg@581 314 strncpy(c->name, *list, sizeof c->name);
garbeam@76 315 XFreeStringList(list);
garbeam@76 316 }
garbeam@76 317 }
garbeam@76 318 XFree(name.value);
garbeam@10 319 }
garbeam@10 320
garbeam@10 321 void
arg@461 322 unmanage(Client *c) {
arg@450 323 Client *nc;
arg@450 324
arg@472 325 /* The server grab construct avoids race conditions. */
garbeam@10 326 XGrabServer(dpy);
garbeam@75 327 XSetErrorHandler(xerrordummy);
arg@400 328 detach(c);
arg@448 329 detachstack(c);
arg@400 330 if(sel == c) {
arg@450 331 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
arg@450 332 focus(nc);
arg@400 333 }
garbeam@18 334 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
arg@725 335 setclientstate(c, WithdrawnState);
arg@178 336 free(c->tags);
garbeam@10 337 free(c);
garbeam@79 338 XSync(dpy, False);
garbeam@74 339 XSetErrorHandler(xerror);
garbeam@10 340 XUngrabServer(dpy);
arg@533 341 arrange();
garbeam@10 342 }