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 synev.type = ConfigureNotify;
75 synev.xconfigure.display = dpy;
76 synev.xconfigure.event = c->win;
77 synev.xconfigure.window = c->win;
78 synev.xconfigure.x = c->x;
79 synev.xconfigure.y = c->y;
80 synev.xconfigure.width = c->w;
81 synev.xconfigure.height = c->h;
82 synev.xconfigure.border_width = c->border;
83 synev.xconfigure.above = None;
84 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
89 if(c && !isvisible(c))
92 grabbuttons(sel, False);
93 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
106 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
107 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
110 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
114 getclient(Window w) {
117 for(c = clients; c; c = c->next)
124 isprotodel(Client *c) {
129 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
130 for(i = 0; !ret && i < n; i++)
131 if(protocols[i] == wmatom[WMDelete])
139 killclient(Arg *arg) {
143 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
145 XKillClient(dpy, sel->win);
149 manage(Window w, XWindowAttributes *wa) {
153 c = emallocz(sizeof(Client));
154 c->tags = emallocz(ntags * sizeof(Bool));
160 if(c->w == sw && c->h == sh) {
166 c->border = BORDERPX;
167 if(c->x + c->w + 2 * c->border > wax + waw)
168 c->x = wax + waw - c->w - 2 * c->border;
169 if(c->y + c->h + 2 * c->border > way + wah)
170 c->y = way + wah - c->h - 2 * c->border;
177 XSelectInput(dpy, c->win,
178 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
179 XGetTransientForHint(dpy, c->win, &trans);
180 grabbuttons(c, False);
181 XSetWindowBorder(dpy, c->win, dc.norm[ColBorder]);
183 settags(c, getclient(trans));
185 c->isfloat = trans || c->isfixed;
191 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
192 XMapWindow(dpy, c->win);
193 setclientstate(c, NormalState);
200 resize(Client *c, Bool sizehints) {
201 float actual, dx, dy, max, min;
204 if(c->w <= 0 || c->h <= 0)
207 if(c->minw && c->w < c->minw)
209 if(c->minh && c->h < c->minh)
211 if(c->maxw && c->w > c->maxw)
213 if(c->maxh && c->h > c->maxh)
215 /* inspired by algorithm from fluxbox */
216 if(c->minay > 0 && c->maxay && (c->h - c->baseh) > 0) {
217 dx = (float)(c->w - c->basew);
218 dy = (float)(c->h - c->baseh);
219 min = (float)(c->minax) / (float)(c->minay);
220 max = (float)(c->maxax) / (float)(c->maxay);
222 if(max > 0 && min > 0 && actual > 0) {
224 dy = (dx * min + dy) / (min * min + 1);
226 c->w = (int)dx + c->basew;
227 c->h = (int)dy + c->baseh;
229 else if(actual > max) {
230 dy = (dx * min + dy) / (max * max + 1);
232 c->w = (int)dx + c->basew;
233 c->h = (int)dy + c->baseh;
238 c->w -= (c->w - c->basew) % c->incw;
240 c->h -= (c->h - c->baseh) % c->inch;
242 if(c->w == sw && c->h == sh)
245 c->border = BORDERPX;
246 /* offscreen appearance fixes */
248 c->x = sw - c->w - 2 * c->border;
250 c->y = sh - c->h - 2 * c->border;
251 if(c->x + c->w + 2 * c->border < sx)
253 if(c->y + c->h + 2 * c->border < sy)
259 wc.border_width = c->border;
260 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
266 updatesizehints(Client *c) {
270 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
272 c->flags = size.flags;
273 if(c->flags & PBaseSize) {
274 c->basew = size.base_width;
275 c->baseh = size.base_height;
278 c->basew = c->baseh = 0;
279 if(c->flags & PResizeInc) {
280 c->incw = size.width_inc;
281 c->inch = size.height_inc;
284 c->incw = c->inch = 0;
285 if(c->flags & PMaxSize) {
286 c->maxw = size.max_width;
287 c->maxh = size.max_height;
290 c->maxw = c->maxh = 0;
291 if(c->flags & PMinSize) {
292 c->minw = size.min_width;
293 c->minh = size.min_height;
296 c->minw = c->minh = 0;
297 if(c->flags & PAspect) {
298 c->minax = size.min_aspect.x;
299 c->minay = size.min_aspect.y;
300 c->maxax = size.max_aspect.x;
301 c->maxay = size.max_aspect.y;
304 c->minax = c->minay = c->maxax = c->maxay = 0;
305 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
306 c->maxw == c->minw && c->maxh == c->minh);
310 updatetitle(Client *c) {
317 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
319 XGetWMName(dpy, c->win, &name);
322 if(name.encoding == XA_STRING)
323 strncpy(c->name, (char *)name.value, sizeof c->name);
325 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
328 strncpy(c->name, *list, sizeof c->name);
329 XFreeStringList(list);
336 unmanage(Client *c) {
339 /* The server grab construct avoids race conditions. */
341 XSetErrorHandler(xerrordummy);
345 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
348 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
349 setclientstate(c, WithdrawnState);
353 XSetErrorHandler(xerror);