1 /* (C)opyright MMVI-MMVII Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
13 attachstack(Client *c) {
19 detachstack(Client *c) {
22 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
27 grabbuttons(Client *c, Bool focused) {
28 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
31 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
32 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
37 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
38 GrabModeAsync, GrabModeSync, None, None);
40 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
41 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
46 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
47 GrabModeAsync, GrabModeSync, None, None);
49 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
50 GrabModeAsync, GrabModeSync, None, None);
51 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
52 GrabModeAsync, GrabModeSync, None, None);
53 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
54 GrabModeAsync, GrabModeSync, None, None);
55 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
56 GrabModeAsync, GrabModeSync, None, None);
59 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
60 GrabModeAsync, GrabModeSync, None, None);
64 isprotodel(Client *c) {
69 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
70 for(i = 0; !ret && i < n; i++)
71 if(protocols[i] == wmatom[WMDelete])
79 setclientstate(Client *c, long state) {
80 long data[] = {state, None};
82 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
83 PropModeReplace, (unsigned char *)data, 2);
87 togglemax(Client *c) {
92 if((c->ismax = !c->ismax)) {
97 resize(c, wax, way, waw - 2 * BORDERPX, wah - 2 * BORDERPX, True);
100 resize(c, c->rx, c->ry, c->rw, c->rh, True);
101 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
105 xerrordummy(Display *dsply, XErrorEvent *ee) {
120 configure(Client *c) {
123 ce.type = ConfigureNotify;
131 ce.border_width = c->border;
133 ce.override_redirect = False;
134 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
140 c->prev->next = c->next;
142 c->next->prev = c->prev;
145 c->next = c->prev = NULL;
150 if(c && !isvisible(c))
152 if(sel && sel != c) {
153 grabbuttons(sel, False);
154 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
159 grabbuttons(c, True);
166 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
167 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
170 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
174 focusnext(Arg *arg) {
179 for(c = sel->next; c && !isvisible(c); c = c->next);
181 for(c = clients; c && !isvisible(c); c = c->next);
189 focusprev(Arg *arg) {
194 for(c = sel->prev; c && !isvisible(c); c = c->prev);
196 for(c = clients; c && c->next; c = c->next);
197 for(; c && !isvisible(c); c = c->prev);
206 killclient(Arg *arg) {
210 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
212 XKillClient(dpy, sel->win);
216 manage(Window w, XWindowAttributes *wa) {
221 c = emallocz(sizeof(Client));
222 c->tags = emallocz(ntags * sizeof(Bool));
228 if(c->w == sw && c->h == sh) {
234 c->border = BORDERPX;
235 if(c->x + c->w + 2 * c->border > wax + waw)
236 c->x = wax + waw - c->w - 2 * c->border;
237 if(c->y + c->h + 2 * c->border > way + wah)
238 c->y = way + wah - c->h - 2 * c->border;
246 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
247 XGetTransientForHint(dpy, w, &trans);
248 grabbuttons(c, False);
249 wc.border_width = c->border;
250 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
251 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
252 configure(c); /* propagates border_width, if size doesn't change */
254 for(t = clients; t && t->win != trans; t = t->next);
257 c->swimming = (t != NULL) || c->isfixed;
261 XMoveWindow(dpy, w, c->x + 2 * sw, c->y);
263 setclientstate(c, NormalState);
270 nexttiled(Client *c) {
271 for(; c && (c->swimming || !isvisible(c)); c = c->next);
276 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
277 float actual, dx, dy, max, min;
283 if(c->minw && w < c->minw)
285 if(c->minh && h < c->minh)
287 if(c->maxw && w > c->maxw)
289 if(c->maxh && h > c->maxh)
291 /* inspired by algorithm from fluxbox */
292 if(c->minay > 0 && c->maxay && (h - c->baseh) > 0) {
293 dx = (float)(w - c->basew);
294 dy = (float)(h - c->baseh);
295 min = (float)(c->minax) / (float)(c->minay);
296 max = (float)(c->maxax) / (float)(c->maxay);
298 if(max > 0 && min > 0 && actual > 0) {
300 dy = (dx * min + dy) / (min * min + 1);
302 w = (int)dx + c->basew;
303 h = (int)dy + c->baseh;
305 else if(actual > max) {
306 dy = (dx * min + dy) / (max * max + 1);
308 w = (int)dx + c->basew;
309 h = (int)dy + c->baseh;
314 w -= (w - c->basew) % c->incw;
316 h -= (h - c->baseh) % c->inch;
318 if(w == sw && h == sh)
321 c->border = BORDERPX;
322 /* offscreen appearance fixes */
324 x = sw - w - 2 * c->border;
326 y = sh - h - 2 * c->border;
327 if(x + w + 2 * c->border < sx)
329 if(y + h + 2 * c->border < sy)
331 if(c->x != x || c->y != y || c->w != w || c->h != h) {
335 c->h = wc.height = h;
336 wc.border_width = c->border;
337 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
344 updatesizehints(Client *c) {
348 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
350 c->flags = size.flags;
351 if(c->flags & PBaseSize) {
352 c->basew = size.base_width;
353 c->baseh = size.base_height;
356 c->basew = c->baseh = 0;
357 if(c->flags & PResizeInc) {
358 c->incw = size.width_inc;
359 c->inch = size.height_inc;
362 c->incw = c->inch = 0;
363 if(c->flags & PMaxSize) {
364 c->maxw = size.max_width;
365 c->maxh = size.max_height;
368 c->maxw = c->maxh = 0;
369 if(c->flags & PMinSize) {
370 c->minw = size.min_width;
371 c->minh = size.min_height;
374 c->minw = c->minh = 0;
375 if(c->flags & PAspect) {
376 c->minax = size.min_aspect.x;
377 c->minay = size.min_aspect.y;
378 c->maxax = size.max_aspect.x;
379 c->maxay = size.max_aspect.y;
382 c->minax = c->minay = c->maxax = c->maxay = 0;
383 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
384 && c->maxw == c->minw && c->maxh == c->minh);
388 updatetitle(Client *c) {
395 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
397 XGetWMName(dpy, c->win, &name);
400 if(name.encoding == XA_STRING)
401 strncpy(c->name, (char *)name.value, sizeof c->name);
403 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
406 strncpy(c->name, *list, sizeof c->name);
407 XFreeStringList(list);
414 unmanage(Client *c) {
417 /* The server grab construct avoids race conditions. */
419 XSetErrorHandler(xerrordummy);
423 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
426 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
427 setclientstate(c, WithdrawnState);
431 XSetErrorHandler(xerror);
443 if(sel->swimming || (lt->arrange == swim)) {
447 for(n = 0, c = nexttiled(clients); c; c = nexttiled(c->next))
449 if((c = sel) == nexttiled(clients))
450 if(!(c = nexttiled(c->next)))