1 /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
2 * See LICENSE file for license details.
10 /* static functions */
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 xerrordummy(Display *dsply, XErrorEvent *ee) {
61 /* extern functions */
65 XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
66 XMoveWindow(dpy, c->twin, c->tx + 2 * sw, c->ty);
70 configure(Client *c) {
73 synev.type = ConfigureNotify;
74 synev.xconfigure.display = dpy;
75 synev.xconfigure.event = c->win;
76 synev.xconfigure.window = c->win;
77 synev.xconfigure.x = c->x;
78 synev.xconfigure.y = c->y;
79 synev.xconfigure.width = c->w;
80 synev.xconfigure.height = c->h;
81 synev.xconfigure.border_width = c->border;
82 synev.xconfigure.above = None;
83 XSendEvent(dpy, c->win, True, NoEventMask, &synev);
90 if(!issel || (c && !isvisible(c)))
98 grabbuttons(old, False);
106 grabbuttons(c, True);
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 getctitle(Window w) {
128 for(c = clients; c; c = c->next)
135 gravitate(Client *c, Bool invert) {
142 case NorthWestGravity:
144 case NorthEastGravity:
150 dy = -(c->h / 2) + c->border;
152 case SouthEastGravity:
154 case SouthWestGravity:
162 case NorthWestGravity:
164 case SouthWestGravity:
170 dx = -(c->w / 2) + c->border;
172 case NorthEastGravity:
174 case SouthEastGravity:
175 dx = -(c->w + c->border);
187 killclient(Arg *arg) {
190 if(sel->proto & PROTODELWIN)
191 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
193 XKillClient(dpy, sel->win);
197 manage(Window w, XWindowAttributes *wa) {
200 XSetWindowAttributes twa;
202 c = emallocz(sizeof(Client));
203 c->tags = emallocz(ntags * sizeof(Bool));
206 c->x = c->tx = wa->x;
207 c->y = c->ty = wa->y;
208 c->w = c->tw = wa->width;
212 if(c->x + c->w + 2 * BORDERPX > sw)
213 c->x = sw - c->w - 2 * BORDERPX;
216 if(c->y + c->h + 2 * BORDERPX > sh)
217 c->y = sh - c->h - 2 * BORDERPX;
218 if(c->h != sh && c->y < bh)
220 c->proto = getproto(c->win);
221 XSelectInput(dpy, c->win,
222 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
223 XGetTransientForHint(dpy, c->win, &trans);
224 twa.override_redirect = 1;
225 twa.background_pixmap = ParentRelative;
226 twa.event_mask = ExposureMask | EnterWindowMask;
227 c->twin = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
228 0, DefaultDepth(dpy, screen), CopyFromParent,
229 DefaultVisual(dpy, screen),
230 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
231 grabbuttons(c, False);
233 settags(c, getclient(trans));
235 c->isfloat = trans || c->isfixed;
243 XMapWindow(dpy, c->win);
244 XMapWindow(dpy, c->twin);
251 resize(Client *c, Bool sizehints, Corner sticky) {
252 int bottom = c->y + c->h;
253 int right = c->x + c->w;
258 c->w -= (c->w - c->basew) % c->incw;
260 c->h -= (c->h - c->baseh) % c->inch;
261 if(c->minw && c->w < c->minw)
263 if(c->minh && c->h < c->minh)
265 if(c->maxw && c->w > c->maxw)
267 if(c->maxh && c->h > c->maxh)
270 if(sticky == TopRight || sticky == BotRight)
272 if(sticky == BotLeft || sticky == BotRight)
273 c->y = bottom - c->h;
274 /* offscreen appearance fixes */
288 if(c->w == sw && c->h == sh)
291 wc.border_width = BORDERPX;
292 XConfigureWindow(dpy, c->win, CWX | CWY | CWWidth | CWHeight | CWBorderWidth, &wc);
298 resizetitle(Client *c) {
299 c->tw = textw(c->name);
301 c->tw = c->w + 2 * BORDERPX;
302 c->tx = c->x + c->w - c->tw + 2 * BORDERPX;
305 XMoveResizeWindow(dpy, c->twin, c->tx, c->ty, c->tw, c->th);
307 XMoveResizeWindow(dpy, c->twin, c->tx + 2 * sw, c->ty, c->tw, c->th);
311 updatesize(Client *c) {
315 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
317 c->flags = size.flags;
318 if(c->flags & PBaseSize) {
319 c->basew = size.base_width;
320 c->baseh = size.base_height;
323 c->basew = c->baseh = 0;
324 if(c->flags & PResizeInc) {
325 c->incw = size.width_inc;
326 c->inch = size.height_inc;
329 c->incw = c->inch = 0;
330 if(c->flags & PMaxSize) {
331 c->maxw = size.max_width;
332 c->maxh = size.max_height;
335 c->maxw = c->maxh = 0;
336 if(c->flags & PMinSize) {
337 c->minw = size.min_width;
338 c->minh = size.min_height;
341 c->minw = c->minh = 0;
342 c->isfixed = (c->maxw && c->minw && c->maxh && c->minh &&
343 c->maxw == c->minw && c->maxh == c->minh);
344 if(c->flags & PWinGravity)
345 c->grav = size.win_gravity;
347 c->grav = NorthWestGravity;
351 updatetitle(Client *c) {
358 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
360 XGetWMName(dpy, c->win, &name);
363 if(name.encoding == XA_STRING)
364 strncpy(c->name, (char *)name.value, sizeof(c->name));
366 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
369 strncpy(c->name, *list, sizeof(c->name));
370 XFreeStringList(list);
377 unmanage(Client *c) {
380 /* The server grab construct avoids race conditions. */
382 XSetErrorHandler(xerrordummy);
386 for(nc = stack; nc && !isvisible(nc); nc = nc->snext);
389 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
390 XDestroyWindow(dpy, c->twin);
394 XSetErrorHandler(xerror);