1 /* See LICENSE file for copyright and license details. */
11 attachstack(Client *c) {
17 detachstack(Client *c) {
20 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
25 grabbuttons(Client *c, Bool focused) {
26 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
29 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
30 GrabModeAsync, GrabModeSync, None, None);
31 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
32 GrabModeAsync, GrabModeSync, None, None);
33 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
34 GrabModeAsync, GrabModeSync, None, None);
35 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
36 GrabModeAsync, GrabModeSync, None, None);
38 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
39 GrabModeAsync, GrabModeSync, None, None);
40 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
41 GrabModeAsync, GrabModeSync, None, None);
42 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
43 GrabModeAsync, GrabModeSync, None, None);
44 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
45 GrabModeAsync, GrabModeSync, None, None);
47 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
49 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
50 GrabModeAsync, GrabModeSync, None, None);
51 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
52 GrabModeAsync, GrabModeSync, None, None);
53 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
54 GrabModeAsync, GrabModeSync, None, None);
57 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
58 GrabModeAsync, GrabModeSync, None, None);
62 isprotodel(Client *c) {
67 if(XGetWMProtocols(dpy, c->win, &protocols, &n)) {
68 for(i = 0; !ret && i < n; i++)
69 if(protocols[i] == wmatom[WMDelete])
77 setclientstate(Client *c, long state) {
78 long data[] = {state, None};
80 XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32,
81 PropModeReplace, (unsigned char *)data, 2);
85 xerrordummy(Display *dsply, XErrorEvent *ee) {
103 XUnmapWindow(dpy, c->win);
104 setclientstate(c, IconicState);
110 configure(Client *c) {
113 ce.type = ConfigureNotify;
121 ce.border_width = c->border;
123 ce.override_redirect = False;
124 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce);
130 c->prev->next = c->next;
132 c->next->prev = c->prev;
135 c->next = c->prev = NULL;
140 if((!c && selscreen) || (c && !isvisible(c)))
141 for(c = stack; c && !isvisible(c); c = c->snext);
142 if(sel && sel != c) {
143 grabbuttons(sel, False);
144 XSetWindowBorder(dpy, sel->win, dc.norm[ColBorder]);
149 grabbuttons(c, True);
156 XSetWindowBorder(dpy, c->win, dc.sel[ColBorder]);
157 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
160 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
164 killclient(const char *arg) {
169 if(isprotodel(sel)) {
170 ev.type = ClientMessage;
171 ev.xclient.window = sel->win;
172 ev.xclient.message_type = wmatom[WMProtocols];
173 ev.xclient.format = 32;
174 ev.xclient.data.l[0] = wmatom[WMDelete];
175 ev.xclient.data.l[1] = CurrentTime;
176 XSendEvent(dpy, sel->win, False, NoEventMask, &ev);
179 XKillClient(dpy, sel->win);
183 manage(Window w, XWindowAttributes *wa) {
184 Client *c, *t = NULL;
189 c = emallocz(sizeof(Client));
190 c->tags = emallocz(ntags * sizeof(Bool));
196 c->oldborder = wa->border_width;
197 if(c->w == sw && c->h == sh) {
200 c->border = wa->border_width;
203 if(c->x + c->w + 2 * c->border > wax + waw)
204 c->x = wax + waw - c->w - 2 * c->border;
205 if(c->y + c->h + 2 * c->border > way + wah)
206 c->y = way + wah - c->h - 2 * c->border;
211 c->border = BORDERPX;
213 wc.border_width = c->border;
214 XConfigureWindow(dpy, w, CWBorderWidth, &wc);
215 XSetWindowBorder(dpy, w, dc.norm[ColBorder]);
216 configure(c); /* propagates border_width, if size doesn't change */
219 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
220 grabbuttons(c, False);
222 if((rettrans = XGetTransientForHint(dpy, w, &trans) == Success))
223 for(t = clients; t && t->win != trans; t = t->next);
226 c->isfloating = (rettrans == Success) || c->isfixed;
229 XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); /* some windows require this */
230 setclientstate(c, IconicState);
237 resize(Client *c, int x, int y, int w, int h, Bool sizehints) {
238 float dx, dy, max, min, ratio;
244 if(c->minay > 0 && c->maxay > 0 && (h - c->baseh) > 0) {
245 dx = (float)(w - c->basew);
246 dy = (float)(h - c->baseh);
247 min = (float)(c->minax) / (float)(c->minay);
248 max = (float)(c->maxax) / (float)(c->maxay);
250 if(max > 0 && min > 0 && ratio > 0) {
252 dy = (dx * min + dy) / (min * min + 1);
254 w = (int)dx + c->basew;
255 h = (int)dy + c->baseh;
257 else if(ratio > max) {
258 dy = (dx * min + dy) / (max * max + 1);
260 w = (int)dx + c->basew;
261 h = (int)dy + c->baseh;
265 if(c->minw && w < c->minw)
267 if(c->minh && h < c->minh)
269 if(c->maxw && w > c->maxw)
271 if(c->maxh && h > c->maxh)
274 w -= (w - c->basew) % c->incw;
276 h -= (h - c->baseh) % c->inch;
280 /* offscreen appearance fixes */
282 x = sw - w - 2 * c->border;
284 y = sh - h - 2 * c->border;
285 if(x + w + 2 * c->border < sx)
287 if(y + h + 2 * c->border < sy)
289 if(c->x != x || c->y != y || c->w != w || c->h != h) {
293 c->h = wc.height = h;
294 wc.border_width = c->border;
295 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
302 togglefloating(const char *arg) {
303 if(!sel || lt->arrange == floating)
305 sel->isfloating = !sel->isfloating;
307 resize(sel, sel->x, sel->y, sel->w, sel->h, True);
315 XMapWindow(dpy, c->win);
316 setclientstate(c, NormalState);
321 unmanage(Client *c) {
324 wc.border_width = c->oldborder;
325 /* The server grab construct avoids race conditions. */
327 XSetErrorHandler(xerrordummy);
328 XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */
333 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
334 setclientstate(c, WithdrawnState);
338 XSetErrorHandler(xerror);
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;
355 else if(c->flags & PMinSize) {
356 c->basew = size.min_width;
357 c->baseh = size.min_height;
360 c->basew = c->baseh = 0;
361 if(c->flags & PResizeInc) {
362 c->incw = size.width_inc;
363 c->inch = size.height_inc;
366 c->incw = c->inch = 0;
367 if(c->flags & PMaxSize) {
368 c->maxw = size.max_width;
369 c->maxh = size.max_height;
372 c->maxw = c->maxh = 0;
373 if(c->flags & PMinSize) {
374 c->minw = size.min_width;
375 c->minh = size.min_height;
377 else if(c->flags & PBaseSize) {
378 c->minw = size.base_width;
379 c->minh = size.base_height;
382 c->minw = c->minh = 0;
383 if(c->flags & PAspect) {
384 c->minax = size.min_aspect.x;
385 c->maxax = size.max_aspect.x;
386 c->minay = size.min_aspect.y;
387 c->maxay = size.max_aspect.y;
390 c->minax = c->maxax = c->minay = c->maxay = 0;
391 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh
392 && c->maxw == c->minw && c->maxh == c->minh);
396 updatetitle(Client *c) {
403 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
405 XGetWMName(dpy, c->win, &name);
408 if(name.encoding == XA_STRING)
409 strncpy(c->name, (char *)name.value, sizeof c->name - 1);
411 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
414 strncpy(c->name, *list, sizeof c->name - 1);
415 XFreeStringList(list);
418 c->name[sizeof c->name - 1] = '\0';