#include "dwm.h" void applyrules(Client *c) { char *class, *instance; uint i; Rule *r; Monitor *m; XClassHint ch = { nil, nil }; c->isfloating = 0; c->tags = 0; c->noswallow = -1; /* rule matching */ XGetClassHint(dpy, c->win, &ch); class = ch.res_class ? ch.res_class : broken; instance = ch.res_name ? ch.res_name : broken; for (i = 0; i < arrlen(rules); i++) { r = &rules[i]; if ((!r->title || strstr(c->name, r->title)) && (!r->class || strstr(class, r->class)) && (!r->instance || strstr(instance, r->instance))) { c->isterm = r->isterm; c->noswallow = r->noswallow; c->isfloating = r->isfloating; c->tags |= r->tags; for (m = mons; m && m->num != r->monitor; m = m->next) ; if (m) c->mon = m; } } if (ch.res_class) XFree(ch.res_class); if (ch.res_name) XFree(ch.res_name); c->tags = c->tags & TAGMASK ? c->tags & TAGMASK : c->mon->tagset[c->mon->seltags]; } int applysizehints(Client *c, int *x, int *y, int *w, int *h, int interact) { int baseismin; Monitor *m = c->mon; /* set minimum possible */ *w = MAX(1, *w); *h = MAX(1, *h); if (interact) { if (*x > sw) *x = sw - WIDTH(c); if (*y > sh) *y = sh - HEIGHT(c); if (*x + *w + 2 * c->bw < 0) *x = 0; if (*y + *h + 2 * c->bw < 0) *y = 0; } else { if (*x >= m->wx + m->ww) *x = m->wx + m->ww - WIDTH(c); if (*y >= m->wy + m->wh) *y = m->wy + m->wh - HEIGHT(c); if (*x + *w + 2 * c->bw <= m->wx) *x = m->wx; if (*y + *h + 2 * c->bw <= m->wy) *y = m->wy; } if (*h < bh) *h = bh; if (*w < bh) *w = bh; if (resizehints || c->isfloating || !c->mon->lt[c->mon->sellt]->arrange) { /* see last two sentences in ICCCM 4.1.2.3 */ baseismin = c->basew == c->minw && c->baseh == c->minh; if (!baseismin) { /* temporarily remove base dimensions */ *w -= c->basew; *h -= c->baseh; } /* adjust for aspect limits */ if (c->mina > 0 && c->maxa > 0) { if (c->maxa < (float)*w / *h) *w = *h * c->maxa + 0.5; else if (c->mina < (float)*h / *w) *h = *w * c->mina + 0.5; } if (baseismin) { /* increment calculation requires this */ *w -= c->basew; *h -= c->baseh; } /* adjust for increment value */ if (c->incw) *w -= *w % c->incw; if (c->inch) *h -= *h % c->inch; /* restore base dimensions */ *w = MAX(*w + c->basew, c->minw); *h = MAX(*h + c->baseh, c->minh); if (c->maxw) *w = MIN(*w, c->maxw); if (c->maxh) *h = MIN(*h, c->maxh); } return *x != c->x || *y != c->y || *w != c->w || *h != c->h; } void attach(Client *c) { c->next = c->mon->clients; c->mon->clients = c; } void enqueue(Client *c) { Client *l; for (l = c->mon->clients; l && l->next; l = l->next) ; if (l) { l->next = c; c->next = nil; } } void attachbottom(Client *c) { Client **tc; c->next = nil; for (tc = &c->mon->clients; *tc; tc = &(*tc)->next) ; *tc = c; } void attachstack(Client *c) { c->snext = c->mon->stack; c->mon->stack = c; } void enqueuestack(Client *c) { Client *l; for (l = c->mon->clients; l && l->next; l = l->next) ; if (l) { l->snext = c; c->snext = nil; } } void configure(Client *c) { XConfigureEvent ce; ce.type = ConfigureNotify; ce.display = dpy; ce.event = c->win; ce.window = c->win; ce.x = c->x; ce.y = c->y; ce.width = c->w; ce.height = c->h; ce.border_width = c->bw; ce.above = None; ce.override_redirect = False; XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&ce); } void detach(Client *c) { Client **tc; for (tc = &c->mon->clients; *tc && *tc != c; tc = &(*tc)->next) ; *tc = c->next; } void detachstack(Client *c) { Client **tc, *t; for (tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext) ; *tc = c->snext; if (c == c->mon->sel) { for (t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext) ; c->mon->sel = t; } } void focus(Client *c) { if (!c || !ISVISIBLE(c)) for (c = selmon->stack; c && !ISVISIBLE(c); c = c->snext) ; if (selmon->sel && selmon->sel != c) unfocus(selmon->sel, 0); if (c) { if (c->mon != selmon) selmon = c->mon; if (c->isurgent) seturgent(c, 0); detachstack(c); attachstack(c); grabbuttons(c, 1); XSetWindowBorder(dpy, c->win, scheme[SchemeSel][ColBorder].pixel); setfocus(c); } else { XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); XDeleteProperty(dpy, root, netatom[NetActiveWindow]); } selmon->sel = c; drawbars(); } Atom getatomprop(Client *c, Atom prop) { int di; unsigned long dl; uchar *p = nil; Atom da, atom = None; if (XGetWindowProperty(dpy, c->win, prop, 0L, sizeof atom, False, XA_ATOM, &da, &di, &dl, &dl, &p) == Success && p) { atom = *(Atom *)p; XFree(p); } return atom; } void grabbuttons(Client *c, int focused) { updatenumlockmask(); { uint i, j; uint modifiers[] = { 0, LockMask, numlockmask, numlockmask|LockMask }; XUngrabButton(dpy, AnyButton, AnyModifier, c->win); if (!focused) XGrabButton(dpy, AnyButton, AnyModifier, c->win, False, BUTTONMASK, GrabModeSync, GrabModeSync, None, None); for (i = 0; i < arrlen(buttons); i++) if (buttons[i].click == ClkClientWin) for (j = 0; j < arrlen(modifiers); j++) XGrabButton(dpy, buttons[i].button, buttons[i].mask | modifiers[j], c->win, False, BUTTONMASK, GrabModeAsync, GrabModeSync, None, None); } } Client * nexttiled(Client *c) { for (; c && (c->isfloating || !ISVISIBLE(c)); c = c->next) ; return c; } void pop(Client *c) { detach(c); attach(c); focus(c); arrange(c->mon); } void resize(Client *c, int x, int y, int w, int h, int interact) { if (applysizehints(c, &x, &y, &w, &h, interact)) resizeclient(c, x, y, w, h); } void resizeclient(Client *c, int x, int y, int w, int h) { XWindowChanges wc; c->oldx = c->x; c->x = wc.x = x; c->oldy = c->y; c->y = wc.y = y; c->oldw = c->w; c->w = wc.width = w; c->oldh = c->h; c->h = wc.height = h; wc.border_width = c->bw; XConfigureWindow(dpy, c->win, CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc); configure(c); XSync(dpy, False); } void sendtomon(Client *c, Monitor *m) { if (c->mon == m) return; unfocus(c, 1); detach(c); detachstack(c); c->mon = m; c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */ /* attach(c); */ attachbottom(c); attachstack(c); focus(nil); arrange(nil); } void setclientstate(Client *c, long state) { long data[] = { state, None }; XChangeProperty(dpy, c->win, wmatom[WMState], wmatom[WMState], 32, PropModeReplace, (uchar *)data, 2); } int sendevent(Client *c, Atom proto) { int n; Atom *protocols; int exists = 0; XEvent ev; if (XGetWMProtocols(dpy, c->win, &protocols, &n)) { while (!exists && n--) exists = protocols[n] == proto; XFree(protocols); } if (exists) { ev.type = ClientMessage; ev.xclient.window = c->win; ev.xclient.message_type = wmatom[WMProtocols]; ev.xclient.format = 32; ev.xclient.data.l[0] = proto; ev.xclient.data.l[1] = CurrentTime; XSendEvent(dpy, c->win, False, NoEventMask, &ev); } return exists; } void setfocus(Client *c) { if (!c->neverfocus) { XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); XChangeProperty(dpy, root, netatom[NetActiveWindow], XA_WINDOW, 32, PropModeReplace, (uchar *) &(c->win), 1); } sendevent(c, wmatom[WMTakeFocus]); } void setfullscreen(Client *c, int fullscreen) { static uint32 opacity = 0xFFFFFFFFul; if (fullscreen && !c->isfullscreen) { XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, PropModeReplace, (uchar*)&netatom[NetWMFullscreen], 1); XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (uchar *)&opacity, 1L); c->isfullscreen = 1; c->oldstate = c->isfloating; c->oldbw = c->bw; c->bw = 0; c->isfloating = 1; resizeclient(c, c->mon->mx, c->mon->my, c->mon->mw, c->mon->mh); XRaiseWindow(dpy, c->win); } else if (!fullscreen && c->isfullscreen){ XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32, PropModeReplace, (uchar*)nil, 0); XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]); c->isfullscreen = 0; c->isfloating = c->oldstate; c->bw = c->oldbw; c->x = c->oldx; c->y = c->oldy; c->w = c->oldw; c->h = c->oldh; resizeclient(c, c->x, c->y, c->w, c->h); arrange(c->mon); } } void seturgent(Client *c, int urg) { XWMHints *wmh; c->isurgent = urg; if (!(wmh = XGetWMHints(dpy, c->win))) return; wmh->flags = urg ? (wmh->flags | XUrgencyHint) : (wmh->flags & ~XUrgencyHint); XSetWMHints(dpy, c->win, wmh); XFree(wmh); } void showhide(Client *c) { if (!c) return; if (ISVISIBLE(c)) { /* show clients top down */ XMoveWindow(dpy, c->win, c->x, c->y); if ((!c->mon->lt[c->mon->sellt]->arrange || c->isfloating) && !c->isfullscreen) resize(c, c->x, c->y, c->w, c->h, 0); showhide(c->snext); } else { /* hide clients bottom up */ showhide(c->snext); XMoveWindow(dpy, c->win, WIDTH(c) * -2, c->y); } } void swallow(Client *p, Client *c) { Client *s; if (c->noswallow > 0 || c->isterm) return; if (c->noswallow < 0 && !swallowfloating && c->isfloating) return; detach(c); detachstack(c); setclientstate(c, WithdrawnState); XUnmapWindow(dpy, p->win); p->swallowing = c; c->mon = p->mon; Window w = p->win; p->win = c->win; c->win = w; XChangeProperty(dpy, c->win, netatom[NetClientList], XA_WINDOW, 32, PropModeReplace, (unsigned char *) &(p->win), 1); updatetitle(p); s = scanner ? c : p; XMoveResizeWindow(dpy, p->win, s->x, s->y, s->w, s->h); arrange(p->mon); configure(p); updateclientlist(); } Client * termof(Client *w) { Client *c; Monitor *m; if (!w->pid || w->isterm) return NULL; for (m = mons; m; m = m->next) { for (c = m->clients; c; c = c->next) { if (c->isterm && !c->swallowing && c->pid && isdescendent(c->pid, w->pid)) return c; } } return NULL; } void unfocus(Client *c, int setfocus) { if (!c) return; grabbuttons(c, 0); XSetWindowBorder(dpy, c->win, scheme[SchemeNorm][ColBorder].pixel); if (setfocus) { XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime); XDeleteProperty(dpy, root, netatom[NetActiveWindow]); } } void unmanage(Client *c, int destroyed) { Client *s; Monitor *m = c->mon; XWindowChanges wc; if (c->swallowing) { unswallow(c); return; } s = swallowing(c->win); if (s) { free(s->swallowing); s->swallowing = nil; arrange(m); focus(nil); return; } detach(c); detachstack(c); if (!destroyed) { wc.border_width = c->oldbw; XGrabServer(dpy); /* avoid race conditions */ XSetErrorHandler(xerrordummy); XConfigureWindow(dpy, c->win, CWBorderWidth, &wc); /* restore border */ XUngrabButton(dpy, AnyButton, AnyModifier, c->win); setclientstate(c, WithdrawnState); XSync(dpy, False); XSetErrorHandler(xerror); XUngrabServer(dpy); } free(c); focus(nil); updateclientlist(); arrange(m); if (!s) { // arrange(m); focus(nil); updateclientlist(); } } void unswallow(Client *c) { c->win = c->swallowing->win; free(c->swallowing); c->swallowing = nil; XDeleteProperty(dpy, c->win, netatom[NetClientList]); /* unfullscreen the client */ setfullscreen(c, 0); updatetitle(c); arrange(c->mon); XMapWindow(dpy, c->win); XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h); setclientstate(c, NormalState); focus(nil); arrange(c->mon); } void updatesizehints(Client *c) { long msize; XSizeHints size; if (!XGetWMNormalHints(dpy, c->win, &size, &msize)) /* size is uninitialized, ensure that size.flags aren't used */ size.flags = PSize; if (size.flags & PBaseSize) { c->basew = size.base_width; c->baseh = size.base_height; } else if (size.flags & PMinSize) { c->basew = size.min_width; c->baseh = size.min_height; } else c->basew = c->baseh = 0; if (size.flags & PResizeInc) { c->incw = size.width_inc; c->inch = size.height_inc; } else c->incw = c->inch = 0; if (size.flags & PMaxSize) { c->maxw = size.max_width; c->maxh = size.max_height; } else c->maxw = c->maxh = 0; if (size.flags & PMinSize) { c->minw = size.min_width; c->minh = size.min_height; } else if (size.flags & PBaseSize) { c->minw = size.base_width; c->minh = size.base_height; } else c->minw = c->minh = 0; if (size.flags & PAspect) { c->mina = (float)size.min_aspect.y / size.min_aspect.x; c->maxa = (float)size.max_aspect.x / size.max_aspect.y; } else c->maxa = c->mina = 0.0; c->isfixed = (c->maxw && c->maxh && c->maxw == c->minw && c->maxh == c->minh); } void updatetitle(Client *c) { if (!gettextprop(c->win, netatom[NetWMName], c->name, sizeof c->name)) gettextprop(c->win, XA_WM_NAME, c->name, sizeof c->name); if (c->name[0] == '\0') /* hack to mark broken clients */ strcpy(c->name, broken); } void updatewindowtype(Client *c) { Atom state = getatomprop(c, netatom[NetWMState]); Atom wtype = getatomprop(c, netatom[NetWMWindowType]); if (state == netatom[NetWMFullscreen]) setfullscreen(c, 1); if (wtype == netatom[NetWMWindowTypeDialog]) c->isfloating = 1; } void updatewmhints(Client *c) { XWMHints *wmh; if ((wmh = XGetWMHints(dpy, c->win))) { if (c == selmon->sel && wmh->flags & XUrgencyHint) { wmh->flags &= ~XUrgencyHint; XSetWMHints(dpy, c->win, wmh); } else c->isurgent = (wmh->flags & XUrgencyHint) ? 1 : 0; if (wmh->flags & InputHint) c->neverfocus = !wmh->input; else c->neverfocus = 0; XFree(wmh); } }