1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
13 detachstack(Client *c) {
15 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
20 grabbuttons(Client *c, Bool focused) {
21 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
24 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
25 GrabModeAsync, GrabModeSync, None, None);
26 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
27 GrabModeAsync, GrabModeSync, None, None);
28 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
29 GrabModeAsync, GrabModeSync, None, None);
30 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
31 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
39 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
40 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
48 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
49 GrabModeAsync, GrabModeSync, None, None);
52 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
53 GrabModeAsync, GrabModeSync, None, None);
57 setclientstate(Client *c, long state) {
58 long data[] = {state, None};
59 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
60 PropModeReplace, (unsigned char *)data, 2);
64 xerrordummy(Display *dsply, XErrorEvent *ee) {
71 configure(Client *c) {
74 ce.type = ConfigureNotify;
82 ce.border_width = c->border;
84 ce.override_redirect = False;
85 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
90 if(c && !isvisible(c))
93 grabbuttons(sel, False);
94 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
100 grabbuttons(c, True);
107 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
108 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
111 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
115 getclient(Window w) {
118 for(c = clients; c; c = c->next)
125 isprotodel(Client *c) {
130 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
131 for(i = 0; !ret && i < n; i++)
132 if(protocols[i] == wmatom[WMDelete])
140 killclient(Arg *arg) {
144 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
146 XKillClient(dpy, sel->win);
150 manage(Window w, XWindowAttributes *wa) {
154 c = emallocz(sizeof(Client));
155 c->tags = emallocz(ntags * sizeof(Bool));
161 if(c->w == sw && c->h == sh) {
167 c->border = BORDERPX;
168 if(c->x + c->w + 2 * c->border > wax + waw)
169 c->x = wax + waw - c->w - 2 * c->border;
170 if(c->y + c->h + 2 * c->border > way + wah)
171 c->y = way + wah - c->h - 2 * c->border;
178 XSelectInput(dpy, c->win,
179 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
180 XGetTransientForHint(dpy, c->win, &trans);
181 grabbuttons(c, False);
182 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
184 t = getclient(trans);
187 c->isfloat = (t != 0) || c->isfixed;
193 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
194 XMapWindow(dpy, c->win);
195 setclientstate(c, NormalState);
202 resize(Client *c, Bool sizehints) {
203 float actual, dx, dy, max, min;
206 if(c->w <= 0 || c->h <= 0)
209 if(c->minw && c->w < c->minw)
211 if(c->minh && c->h < c->minh)
213 if(c->maxw && c->w > c->maxw)
215 if(c->maxh && c->h > c->maxh)
217 /* inspired by algorithm from fluxbox */
218 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
219 dx = (float)(c->w - c->basew);
220 dy = (float)(c->h - c->baseh);
221 min = (float)(c->minax) / (float)(c->minay);
222 max = (float)(c->maxax) / (float)(c->maxay);
224 if(max > 0 && min > 0 && actual > 0) {
226 dy = (dx * min + dy) / (min * min + 1);
228 c->w = (int)dx + c->basew;
229 c->h = (int)dy + c->baseh;
231 else if(actual > max) {
232 dy = (dx * min + dy) / (max * max + 1);
234 c->w = (int)dx + c->basew;
235 c->h = (int)dy + c->baseh;
240 c->w -= (c->w - c->basew) % c->incw;
242 c->h -= (c->h - c->baseh) % c->inch;
244 if(c->w == sw && c->h == sh)
247 c->border = BORDERPX;
248 /* offscreen appearance fixes */
250 c->x = sw - c->w - 2 * c->border;
252 c->y = sh - c->h - 2 * c->border;
253 if(c->x + c->w + 2 * c->border < sx)
255 if(c->y + c->h + 2 * c->border < sy)
261 wc.border_width = c->border;
262 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
268 updatesizehints(Client *c) {
272 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
274 c->flags = size.flags;
275 if(c->flags & PBaseSize) {
276 c->basew = size.base_width;
277 c->baseh = size.base_height;
280 c->basew = c->baseh = 0;
281 if(c->flags & PResizeInc) {
282 c->incw = size.width_inc;
283 c->inch = size.height_inc;
286 c->incw = c->inch = 0;
287 if(c->flags & PMaxSize) {
288 c->maxw = size.max_width;
289 c->maxh = size.max_height;
292 c->maxw = c->maxh = 0;
293 if(c->flags & PMinSize) {
294 c->minw = size.min_width;
295 c->minh = size.min_height;
298 c->minw = c->minh = 0;
299 if(c->flags & PAspect) {
300 c->minax = size.min_aspect.x;
301 c->minay = size.min_aspect.y;
302 c->maxax = size.max_aspect.x;
303 c->maxay = size.max_aspect.y;
306 c->minax = c->minay = c->maxax = c->maxay = 0;
307 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
308 && c->maxw == c->minw && c->maxh == c->minh);
312 updatetitle(Client *c) {
319 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
321 XGetWMName(dpy, c->win, &name);
324 if(name.encoding == XA_STRING)
325 strncpy(c->name, (char *)name.value, sizeof c->name);
327 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
330 strncpy(c->name, *list, sizeof c->name);
331 XFreeStringList(list);
338 unmanage(Client *c) {
341 /* The server grab construct avoids race conditions. */
343 XSetErrorHandler(xerrordummy);
347 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
350 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
351 setclientstate(c, WithdrawnState);
355 XSetErrorHandler(xerror);