aewl

annotate client.c @ 731:29c9b557ed95

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