2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
11 /* static functions */
14 detachstack(Client *c) {
16 for(tc=&stack; *tc && *tc != c; tc=&(*tc)->snext);
21 grabbuttons(Client *c, Bool focused) {
22 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
25 XGrabButton(dpy, Button1, MODKEY, c->win, False, BUTTONMASK,
26 GrabModeAsync, GrabModeSync, None, None);
27 XGrabButton(dpy, Button1, MODKEY | LockMask, c->win, False, BUTTONMASK,
28 GrabModeAsync, GrabModeSync, None, None);
29 XGrabButton(dpy, Button1, MODKEY | numlockmask, c->win, False, BUTTONMASK,
30 GrabModeAsync, GrabModeSync, None, None);
31 XGrabButton(dpy, Button1, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
32 GrabModeAsync, GrabModeSync, None, None);
34 XGrabButton(dpy, Button2, MODKEY, c->win, False, BUTTONMASK,
35 GrabModeAsync, GrabModeSync, None, None);
36 XGrabButton(dpy, Button2, MODKEY | LockMask, c->win, False, BUTTONMASK,
37 GrabModeAsync, GrabModeSync, None, None);
38 XGrabButton(dpy, Button2, MODKEY | numlockmask, c->win, False, BUTTONMASK,
39 GrabModeAsync, GrabModeSync, None, None);
40 XGrabButton(dpy, Button2, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
41 GrabModeAsync, GrabModeSync, None, None);
43 XGrabButton(dpy, Button3, MODKEY, c->win, False, BUTTONMASK,
44 GrabModeAsync, GrabModeSync, None, None);
45 XGrabButton(dpy, Button3, MODKEY | LockMask, c->win, False, BUTTONMASK,
46 GrabModeAsync, GrabModeSync, None, None);
47 XGrabButton(dpy, Button3, MODKEY | numlockmask, c->win, False, BUTTONMASK,
48 GrabModeAsync, GrabModeSync, None, None);
49 XGrabButton(dpy, Button3, MODKEY | numlockmask | LockMask, c->win, False, BUTTONMASK,
50 GrabModeAsync, GrabModeSync, None, None);
53 XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK,
54 GrabModeAsync, GrabModeSync, None, None);
58 resizetitle(Client *c) {
59 c->tw = textw(c->name);
62 c->tx = c->x + c->w - c->tw + 2;
65 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
67 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
71 xerrordummy(Display *dsply, XErrorEvent *ee) {
75 /* extern functions */
79 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
80 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
87 if(!issel || (c && !isvisible(c)))
95 grabbuttons(old, False);
103 grabbuttons(c, True);
105 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
108 XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
112 getclient(Window w) {
115 for(c = clients; c; c = c->next)
122 getctitle(Window w) {
125 for(c = clients; c; c = c->next)
132 gravitate(Client *c, Bool invert) {
139 case NorthWestGravity:
141 case NorthEastGravity:
147 dy = -(c->h / 2) + c->border;
149 case SouthEastGravity:
151 case SouthWestGravity:
160 case NorthWestGravity:
162 case SouthWestGravity:
168 dx = -(c->w / 2) + c->border;
170 case NorthEastGravity:
172 case SouthEastGravity:
173 dx = -(c->w + c->border);
186 killclient(Arg *arg) {
189 if(sel->proto & PROTODELWIN)
190 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
192 XKillClient(dpy, sel->win);
196 manage(Window w, XWindowAttributes *wa) {
199 XSetWindowAttributes twa;
201 c = emallocz(sizeof(Client));
202 c->tags = emallocz(ntags * sizeof(Bool));
204 c->x = c->tx = wa->x;
205 c->y = c->ty = wa->y;
206 c->w = c->tw = wa->width;
217 if(c->x + c->w + 2 > sw)
218 c->x = sw - c->w - 2;
221 if(c->y + c->h + 2 > sh)
222 c->y = sh - c->h - 2;
223 if(c->h != sh && c->y < bh)
226 c->proto = getproto(c->win);
227 XSelectInput(dpy, c->win,
228 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
229 XGetTransientForHint(dpy, c->win, &trans);
230 twa.override_redirect = 1;
231 twa.background_pixmap = ParentRelative;
232 twa.event_mask = ExposureMask | EnterWindowMask;
234 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
235 0, DefaultDepth(dpy, screen), CopyFromParent,
236 DefaultVisual(dpy, screen),
237 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
239 grabbuttons(c, False);
240 settags(c, getclient(trans));
243 || (c->maxw && c->minw &&
244 c->maxw == c->minw && c->maxh == c->minh);
254 XMapWindow(dpy, c->win);
255 XMapWindow(dpy, c->twin);
262 resize(Client *c, Bool sizehints, Corner sticky) {
263 int bottom = c->y + c->h;
264 int right = c->x + c->w;
269 c->w -= (c->w - c->basew) % c->incw;
271 c->h -= (c->h - c->baseh) % c->inch;
272 if(c->minw && c->w < c->minw)
274 if(c->minh && c->h < c->minh)
276 if(c->maxw && c->w > c->maxw)
278 if(c->maxh && c->h > c->maxh)
281 if(sticky == TopRight || sticky == BotRight)
283 if(sticky == BotLeft || sticky == BotRight)
284 c->y = bottom - c->h;
286 /* offscreen appearance fixes */
301 if(c->w == sw && c->h == sh)
305 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
310 updatesize(Client *c) {
314 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
316 c->flags = size.flags;
317 if(c->flags & PBaseSize) {
318 c->basew = size.base_width;
319 c->baseh = size.base_height;
322 c->basew = c->baseh = 0;
323 if(c->flags & PResizeInc) {
324 c->incw = size.width_inc;
325 c->inch = size.height_inc;
328 c->incw = c->inch = 0;
329 if(c->flags & PMaxSize) {
330 c->maxw = size.max_width;
331 c->maxh = size.max_height;
334 c->maxw = c->maxh = 0;
335 if(c->flags & PMinSize) {
336 c->minw = size.min_width;
337 c->minh = size.min_height;
340 c->minw = c->minh = 0;
341 if(c->flags & PWinGravity)
342 c->grav = size.win_gravity;
344 c->grav = NorthWestGravity;
348 updatetitle(Client *c) {
355 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
357 XGetWMName(dpy, c->win, &name);
360 if(name.encoding == XA_STRING)
361 strncpy(c->name, (char *)name.value, sizeof(c->name));
363 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
366 strncpy(c->name, *list, sizeof(c->name));
367 XFreeStringList(list);
375 unmanage(Client *c) {
378 /* The server grab construct avoids race conditions. */
380 XSetErrorHandler(xerrordummy);
385 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
389 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
390 XDestroyWindow(dpy, c->twin);
396 XSetErrorHandler(xerror);