2 * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
3 * See LICENSE file for license details.
10 #include <X11/Xutil.h>
12 /* static functions */
15 resizetitle(Client *c)
20 for(i = 0; i < TLast; i++)
22 c->bw += textw(c->tags[i]);
23 c->bw += textw(c->name);
26 c->bx = *c->x + *c->w - c->bw + 2;
29 XMoveResizeWindow(dpy, c->title, c->bx, c->by, c->bw, c->bh);
31 XMoveResizeWindow(dpy, c->title, c->bx + 2 * sw, c->by, c->bw, c->bh);
36 xerrordummy(Display *dsply, XErrorEvent *ee)
41 /* extern functions */
46 XMoveWindow(dpy, c->win, *c->x + 2 * sw, *c->y);
47 XMoveWindow(dpy, c->title, c->bx + 2 * sw, c->by);
60 XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
62 while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
73 if(!(c = getnext(sel->next, tsel)))
74 c = getnext(clients, tsel);
90 if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
100 for(c = clients; c; c = c->next)
110 for(c = clients; c; c = c->next)
117 gravitate(Client *c, Bool invert)
123 case NorthWestGravity:
125 case NorthEastGravity:
131 dy = -(*c->h / 2) + c->border;
133 case SouthEastGravity:
135 case SouthWestGravity:
144 case NorthWestGravity:
146 case SouthWestGravity:
152 dx = -(*c->w / 2) + c->border;
154 case NorthEastGravity:
156 case SouthEastGravity:
157 dx = -(*c->w + c->border);
174 XRaiseWindow(dpy, c->win);
175 XRaiseWindow(dpy, c->title);
183 if(sel->proto & WM_PROTOCOL_DELWIN)
184 sendevent(sel->win, wmatom[WMProtocols], wmatom[WMDelete]);
186 XKillClient(dpy, sel->win);
192 XLowerWindow(dpy, c->title);
193 XLowerWindow(dpy, c->win);
197 manage(Window w, XWindowAttributes *wa)
201 XSetWindowAttributes twa;
204 c = emallocz(sizeof(Client));
206 c->bx = c->fx = c->tx = wa->x;
207 c->by = c->fy = c->ty = wa->y;
208 c->bw = c->fw = c->tw = wa->width;
209 c->fh = c->th = wa->height;
213 c->fx = random() % (diff ? diff : 1);
214 diff = sh - c->fh - bh;
215 c->fy = random() % (diff ? diff : 1);
218 c->by = c->fy = c->ty = bh;
221 c->proto = getproto(c->win);
223 XSelectInput(dpy, c->win,
224 StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
225 XGetTransientForHint(dpy, c->win, &trans);
226 twa.override_redirect = 1;
227 twa.background_pixmap = ParentRelative;
228 twa.event_mask = ExposureMask;
230 c->title = XCreateWindow(dpy, root, c->bx, c->by, c->bw, c->bh,
231 0, DefaultDepth(dpy, screen), CopyFromParent,
232 DefaultVisual(dpy, screen),
233 CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
240 XGrabButton(dpy, Button1, ControlMask, c->win, False, ButtonPressMask,
241 GrabModeAsync, GrabModeSync, None, None);
242 XGrabButton(dpy, Button1, MODKEY, c->win, False, ButtonPressMask,
243 GrabModeAsync, GrabModeSync, None, None);
244 XGrabButton(dpy, Button2, MODKEY, c->win, False, ButtonPressMask,
245 GrabModeAsync, GrabModeSync, None, None);
246 XGrabButton(dpy, Button3, MODKEY, c->win, False, ButtonPressMask,
247 GrabModeAsync, GrabModeSync, None, None);
250 c->isfloat = trans || (c->maxw && c->minw &&
251 (c->maxw == c->minw) && (c->maxh == c->minh));
259 /* mapping the window now prevents flicker */
261 XMapRaised(dpy, c->win);
262 XMapRaised(dpy, c->title);
266 XMapRaised(dpy, c->win);
267 XMapRaised(dpy, c->title);
278 *sel->w = sw - 2 * sel->border;
279 *sel->h = sh - 2 * sel->border - bh;
281 resize(sel, False, TopLeft);
288 for(l = &clients; *l && *l != c; l = &(*l)->next);
291 c->next = clients; /* pop */
297 resize(Client *c, Bool inc, Corner sticky)
300 int right = *c->x + *c->w;
301 int bottom = *c->y + *c->h;
305 *c->w -= (*c->w - c->basew) % c->incw;
307 *c->h -= (*c->h - c->baseh) % c->inch;
309 if(*c->x > sw) /* might happen on restart */
313 if(c->minw && *c->w < c->minw)
315 if(c->minh && *c->h < c->minh)
317 if(c->maxw && *c->w > c->maxw)
319 if(c->maxh && *c->h > c->maxh)
321 if(sticky == TopRight || sticky == BotRight)
322 *c->x = right - *c->w;
323 if(sticky == BotLeft || sticky == BotRight)
324 *c->y = bottom - *c->h;
327 XSetWindowBorderWidth(dpy, c->win, 1);
328 XMoveResizeWindow(dpy, c->win, *c->x, *c->y, *c->w, *c->h);
330 e.type = ConfigureNotify;
337 e.border_width = c->border;
339 e.override_redirect = False;
340 XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
347 if((arrange == dotile) && !c->isfloat) {
366 if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
368 c->flags = size.flags;
369 if(c->flags & PBaseSize) {
370 c->basew = size.base_width;
371 c->baseh = size.base_height;
374 c->basew = c->baseh = 0;
375 if(c->flags & PResizeInc) {
376 c->incw = size.width_inc;
377 c->inch = size.height_inc;
380 c->incw = c->inch = 0;
381 if(c->flags & PMaxSize) {
382 c->maxw = size.max_width;
383 c->maxh = size.max_height;
386 c->maxw = c->maxh = 0;
387 if(c->flags & PMinSize) {
388 c->minw = size.min_width;
389 c->minh = size.min_height;
392 c->minw = c->minh = 0;
393 if(c->flags & PWinGravity)
394 c->grav = size.win_gravity;
396 c->grav = NorthWestGravity;
408 XGetTextProperty(dpy, c->win, &name, netatom[NetWMName]);
410 XGetWMName(dpy, c->win, &name);
413 if(name.encoding == XA_STRING)
414 strncpy(c->name, (char *)name.value, sizeof(c->name));
416 if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
419 strncpy(c->name, *list, sizeof(c->name));
420 XFreeStringList(list);
433 XSetErrorHandler(xerrordummy);
435 XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
436 XDestroyWindow(dpy, c->title);
438 for(l = &clients; *l && *l != c; l = &(*l)->next);
440 for(l = &clients; *l; l = &(*l)->next)
441 if((*l)->revert == c)
444 sel = sel->revert ? sel->revert : clients;
449 XSetErrorHandler(xerror);
464 if(sel == getnext(clients, tsel) && sel->next) {
465 if((c = getnext(sel->next, tsel)))