gappx from xresources
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 7b8c1cc..5114eea 100644 (file)
--- a/dwm.c
+++ b/dwm.c
 #include <X11/extensions/Xinerama.h>
 #endif /* XINERAMA */
 #include <X11/Xft/Xft.h>
+#include <X11/Xlib-xcb.h>
+#include <xcb/res.h>
+#ifdef __OpenBSD__
+#include <sys/sysctl.h>
+#include <kvm.h>
+#endif /* __OpenBSD */
 
 #include "drw.h"
 #include "util.h"
@@ -94,9 +100,11 @@ struct Client {
        int basew, baseh, incw, inch, maxw, maxh, minw, minh;
        int bw, oldbw;
        unsigned int tags;
-       int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen;
+       int isfixed, isfloating, isurgent, neverfocus, oldstate, isfullscreen, isterminal, noswallow;
+       pid_t pid;
        Client *next;
        Client *snext;
+       Client *swallowing;
        Monitor *mon;
        Window win;
 };
@@ -113,6 +121,7 @@ typedef struct {
        void (*arrange)(Monitor *);
 } Layout;
 
+typedef struct Pertag Pertag;
 struct Monitor {
        char ltsymbol[16];
        float mfact;
@@ -133,6 +142,7 @@ struct Monitor {
        Monitor *next;
        Window barwin;
        const Layout *lt[2];
+       Pertag *pertag;
 };
 
 typedef struct {
@@ -141,6 +151,8 @@ typedef struct {
        const char *title;
        unsigned int tags;
        int isfloating;
+       int isterminal;
+       int noswallow;
        int monitor;
 } Rule;
 
@@ -252,6 +264,11 @@ static int xerror(Display *dpy, XErrorEvent *ee);
 static int xerrordummy(Display *dpy, XErrorEvent *ee);
 static int xerrorstart(Display *dpy, XErrorEvent *ee);
 static void zoom(const Arg *arg);
+static pid_t getparentprocess(pid_t p);
+static int isdescprocess(pid_t p, pid_t c);
+static Client *swallowingclient(Window w);
+static Client *termforwin(const Client *c);
+static pid_t winpid(Window w);
 static void load_xresources(void);
 static void resource_load(XrmDatabase db, char *name, enum resource_type rtype, void *dst);
 
@@ -295,9 +312,20 @@ static Drw *drw;
 static Monitor *mons, *selmon;
 static Window root, wmcheckwin;
 
+static xcb_connection_t *xcon;
+
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
+struct Pertag {
+       unsigned int curtag, prevtag; /* current and previous tag */
+       int nmasters[LENGTH(tags) + 1]; /* number of windows in master area */
+       float mfacts[LENGTH(tags) + 1]; /* mfacts per tag */
+       unsigned int sellts[LENGTH(tags) + 1]; /* selected layouts */
+       const Layout *ltidxs[LENGTH(tags) + 1][2]; /* matrix of tags and layouts indexes  */
+       int showbars[LENGTH(tags) + 1]; /* display bar for the current tag */
+};
+
 /* compile-time check if all tags fit into an unsigned int bit array. */
 struct NumTags { char limitexceeded[LENGTH(tags) > 31 ? -1 : 1]; };
 
@@ -324,6 +352,8 @@ applyrules(Client *c)
                && (!r->class || strstr(class, r->class))
                && (!r->instance || strstr(instance, r->instance)))
                {
+                       c->isterminal = r->isterminal;
+                       c->noswallow  = r->noswallow;
                        c->isfloating = r->isfloating;
                        c->tags |= r->tags;
                        for (m = mons; m && m->num != r->monitor; m = m->next);
@@ -440,6 +470,53 @@ attachstack(Client *c)
        c->mon->stack = c;
 }
 
+void
+swallow(Client *p, Client *c)
+{
+
+       if (c->noswallow || c->isterminal)
+               return;
+       if (c->noswallow && !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;
+       updatetitle(p);
+       XMoveResizeWindow(dpy, p->win, p->x, p->y, p->w, p->h);
+       arrange(p->mon);
+       configure(p);
+       updateclientlist();
+}
+
+void
+unswallow(Client *c)
+{
+       c->win = c->swallowing->win;
+
+       free(c->swallowing);
+       c->swallowing = NULL;
+
+       /* 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(NULL);
+       arrange(c->mon);
+}
+
 void
 buttonpress(XEvent *e)
 {
@@ -548,7 +625,7 @@ clientmessage(XEvent *e)
                if (cme->data.l[1] == netatom[NetWMFullscreen]
                || cme->data.l[2] == netatom[NetWMFullscreen])
                        setfullscreen(c, (cme->data.l[0] == 1 /* _NET_WM_STATE_ADD    */
-                               || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
+                               || cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */));
        } else if (cme->message_type == netatom[NetActiveWindow]) {
                if (c != selmon->sel && !c->isurgent)
                        seturgent(c, 1);
@@ -578,7 +655,6 @@ void
 configurenotify(XEvent *e)
 {
        Monitor *m;
-       Client *c;
        XConfigureEvent *ev = &e->xconfigure;
        int dirty;
 
@@ -591,9 +667,6 @@ configurenotify(XEvent *e)
                        drw_resize(drw, sw, bh);
                        updatebars();
                        for (m = mons; m; m = m->next) {
-                               for (c = m->clients; c; c = c->next)
-                                       if (c->isfullscreen)
-                                               resizeclient(c, m->mx, m->my, m->mw, m->mh);
                                XMoveResizeWindow(dpy, m->barwin, m->wx + sp, m->by + vp, m->ww -  2 * sp, bh);
                        }
                        focus(NULL);
@@ -658,6 +731,7 @@ Monitor *
 createmon(void)
 {
        Monitor *m;
+       unsigned int i;
 
        m = ecalloc(1, sizeof(Monitor));
        m->tagset[0] = m->tagset[1] = 1;
@@ -669,6 +743,20 @@ createmon(void)
        m->lt[0] = &layouts[0];
        m->lt[1] = &layouts[1 % LENGTH(layouts)];
        strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
+       m->pertag = ecalloc(1, sizeof(Pertag));
+       m->pertag->curtag = m->pertag->prevtag = 1;
+
+       for (i = 0; i <= LENGTH(tags); i++) {
+               m->pertag->nmasters[i] = m->nmaster;
+               m->pertag->mfacts[i] = m->mfact;
+
+               m->pertag->ltidxs[i][0] = m->lt[0];
+               m->pertag->ltidxs[i][1] = m->lt[1];
+               m->pertag->sellts[i] = m->sellt;
+
+               m->pertag->showbars[i] = m->showbar;
+       }
+
        return m;
 }
 
@@ -680,6 +768,9 @@ destroynotify(XEvent *e)
 
        if ((c = wintoclient(ev->window)))
                unmanage(c, 1);
+
+       else if ((c = swallowingclient(ev->window)))
+               unmanage(c->swallowing, 1);
 }
 
 void
@@ -994,7 +1085,7 @@ grabkeys(void)
 void
 incnmaster(const Arg *arg)
 {
-       selmon->nmaster = MAX(selmon->nmaster + arg->i, 0);
+       selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag] = MAX(selmon->nmaster + arg->i, 0);
        arrange(selmon);
 }
 
@@ -1045,12 +1136,13 @@ killclient(const Arg *arg)
 void
 manage(Window w, XWindowAttributes *wa)
 {
-       Client *c, *t = NULL;
+       Client *c, *t = NULL, *term = NULL;
        Window trans = None;
        XWindowChanges wc;
 
        c = ecalloc(1, sizeof(Client));
        c->win = w;
+       c->pid = winpid(w);
        /* geometry */
        c->x = c->oldx = wa->x;
        c->y = c->oldy = wa->y;
@@ -1065,6 +1157,7 @@ manage(Window w, XWindowAttributes *wa)
        } else {
                c->mon = selmon;
                applyrules(c);
+               term = termforwin(c);
        }
 
        if (c->x + WIDTH(c) > c->mon->mx + c->mon->mw)
@@ -1101,6 +1194,8 @@ manage(Window w, XWindowAttributes *wa)
        c->mon->sel = c;
        arrange(c->mon);
        XMapWindow(dpy, c->win);
+       if (term)
+               swallow(term, c);
        focus(NULL);
 }
 
@@ -1171,8 +1266,6 @@ movemouse(const Arg *arg)
 
        if (!(c = selmon->sel))
                return;
-       if (c->isfullscreen) /* no support moving fullscreen windows by mouse */
-               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
@@ -1326,8 +1419,6 @@ resizemouse(const Arg *arg)
 
        if (!(c = selmon->sel))
                return;
-       if (c->isfullscreen) /* no support resizing fullscreen windows by mouse */
-               return;
        restack(selmon);
        ocx = c->x;
        ocy = c->y;
@@ -1581,24 +1672,10 @@ setfullscreen(Client *c, int fullscreen)
                XChangeProperty(dpy, c->win, netatom[NetWMState], XA_ATOM, 32,
                        PropModeReplace, (unsigned char*)&netatom[NetWMFullscreen], 1);
                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, (unsigned char*)0, 0);
                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);
        }
 }
 
@@ -1616,9 +1693,9 @@ void
 setlayout(const Arg *arg)
 {
        if (!arg || !arg->v || arg->v != selmon->lt[selmon->sellt])
-               selmon->sellt ^= 1;
+               selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag] ^= 1;
        if (arg && arg->v)
-               selmon->lt[selmon->sellt] = (Layout *)arg->v;
+               selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt] = (Layout *)arg->v;
        strncpy(selmon->ltsymbol, selmon->lt[selmon->sellt]->symbol, sizeof selmon->ltsymbol);
        if (selmon->sel)
                arrange(selmon);
@@ -1637,7 +1714,7 @@ setmfact(const Arg *arg)
        f = arg->f < 1.0 ? arg->f + selmon->mfact : arg->f - 1.0;
        if (f < 0.05 || f > 0.95)
                return;
-       selmon->mfact = f;
+       selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag] = f;
        arrange(selmon);
 }
 
@@ -1737,7 +1814,7 @@ showhide(Client *c)
        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)
+               if (!c->mon->lt[c->mon->sellt]->arrange || c->isfloating)
                        resize(c, c->x, c->y, c->w, c->h, 0);
                showhide(c->snext);
        } else {
@@ -1820,7 +1897,7 @@ tile(Monitor *m)
 void
 togglebar(const Arg *arg)
 {
-       selmon->showbar = !selmon->showbar;
+       selmon->showbar = selmon->pertag->showbars[selmon->pertag->curtag] = !selmon->showbar;
        updatebarpos(selmon);
        XMoveResizeWindow(dpy, selmon->barwin, selmon->wx + sp, selmon->by + vp, selmon->ww - 2 * sp, bh);
        arrange(selmon);
@@ -1831,8 +1908,6 @@ togglefloating(const Arg *arg)
 {
        if (!selmon->sel)
                return;
-       if (selmon->sel->isfullscreen) /* no support for fullscreen windows */
-               return;
        selmon->sel->isfloating = !selmon->sel->isfloating || selmon->sel->isfixed;
        if (selmon->sel->isfloating)
                resize(selmon->sel, selmon->sel->x, selmon->sel->y,
@@ -1859,9 +1934,33 @@ void
 toggleview(const Arg *arg)
 {
        unsigned int newtagset = selmon->tagset[selmon->seltags] ^ (arg->ui & TAGMASK);
+       int i;
 
        if (newtagset) {
                selmon->tagset[selmon->seltags] = newtagset;
+
+               if (newtagset == ~0) {
+                       selmon->pertag->prevtag = selmon->pertag->curtag;
+                       selmon->pertag->curtag = 0;
+               }
+
+               /* test if the user did not select the same tag */
+               if (!(newtagset & 1 << (selmon->pertag->curtag - 1))) {
+                       selmon->pertag->prevtag = selmon->pertag->curtag;
+                       for (i = 0; !(newtagset & 1 << i); i++) ;
+                       selmon->pertag->curtag = i + 1;
+               }
+
+               /* apply settings for this view */
+               selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
+               selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
+               selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
+               selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
+               selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
+
+               if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
+                       togglebar(NULL);
+
                focus(NULL);
                arrange(selmon);
        }
@@ -1886,6 +1985,20 @@ unmanage(Client *c, int destroyed)
        Monitor *m = c->mon;
        XWindowChanges wc;
 
+       if (c->swallowing) {
+               unswallow(c);
+               return;
+       }
+
+       Client *s = swallowingclient(c->win);
+       if (s) {
+               free(s->swallowing);
+               s->swallowing = NULL;
+               arrange(m);
+               focus(NULL);
+               return;
+       }
+
        detach(c);
        detachstack(c);
        if (!destroyed) {
@@ -1900,9 +2013,12 @@ unmanage(Client *c, int destroyed)
                XUngrabServer(dpy);
        }
        free(c);
-       focus(NULL);
-       updateclientlist();
-       arrange(m);
+
+       if (!s) {
+               arrange(m);
+               focus(NULL);
+               updateclientlist();
+       }
 }
 
 void
@@ -2156,15 +2272,171 @@ updatewmhints(Client *c)
 void
 view(const Arg *arg)
 {
+       int i;
+       unsigned int tmptag;
+
        if ((arg->ui & TAGMASK) == selmon->tagset[selmon->seltags])
                return;
        selmon->seltags ^= 1; /* toggle sel tagset */
-       if (arg->ui & TAGMASK)
+       if (arg->ui & TAGMASK) {
                selmon->tagset[selmon->seltags] = arg->ui & TAGMASK;
+               selmon->pertag->prevtag = selmon->pertag->curtag;
+
+               if (arg->ui == ~0)
+                       selmon->pertag->curtag = 0;
+               else {
+                       for (i = 0; !(arg->ui & 1 << i); i++) ;
+                       selmon->pertag->curtag = i + 1;
+               }
+       } else {
+               tmptag = selmon->pertag->prevtag;
+               selmon->pertag->prevtag = selmon->pertag->curtag;
+               selmon->pertag->curtag = tmptag;
+       }
+
+       selmon->nmaster = selmon->pertag->nmasters[selmon->pertag->curtag];
+       selmon->mfact = selmon->pertag->mfacts[selmon->pertag->curtag];
+       selmon->sellt = selmon->pertag->sellts[selmon->pertag->curtag];
+       selmon->lt[selmon->sellt] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt];
+       selmon->lt[selmon->sellt^1] = selmon->pertag->ltidxs[selmon->pertag->curtag][selmon->sellt^1];
+
+       if (selmon->showbar != selmon->pertag->showbars[selmon->pertag->curtag])
+               togglebar(NULL);
+
        focus(NULL);
        arrange(selmon);
 }
 
+pid_t
+winpid(Window w)
+{
+
+       pid_t result = 0;
+
+       #ifdef __linux__
+       xcb_res_client_id_spec_t spec = {0};
+       spec.client = w;
+       spec.mask = XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID;
+
+       xcb_generic_error_t *e = NULL;
+       xcb_res_query_client_ids_cookie_t c = xcb_res_query_client_ids(xcon, 1, &spec);
+       xcb_res_query_client_ids_reply_t *r = xcb_res_query_client_ids_reply(xcon, c, &e);
+
+       if (!r)
+               return (pid_t)0;
+
+       xcb_res_client_id_value_iterator_t i = xcb_res_query_client_ids_ids_iterator(r);
+       for (; i.rem; xcb_res_client_id_value_next(&i)) {
+               spec = i.data->spec;
+               if (spec.mask & XCB_RES_CLIENT_ID_MASK_LOCAL_CLIENT_PID) {
+                       uint32_t *t = xcb_res_client_id_value_value(i.data);
+                       result = *t;
+                       break;
+               }
+       }
+
+       free(r);
+
+       if (result == (pid_t)-1)
+               result = 0;
+
+       #endif /* __linux__ */
+
+       #ifdef __OpenBSD__
+        Atom type;
+        int format;
+        unsigned long len, bytes;
+        unsigned char *prop;
+        pid_t ret;
+
+        if (XGetWindowProperty(dpy, w, XInternAtom(dpy, "_NET_WM_PID", 1), 0, 1, False, AnyPropertyType, &type, &format, &len, &bytes, &prop) != Success || !prop)
+               return 0;
+
+        ret = *(pid_t*)prop;
+        XFree(prop);
+        result = ret;
+
+       #endif /* __OpenBSD__ */
+       return result;
+}
+
+pid_t
+getparentprocess(pid_t p)
+{
+       unsigned int v = 0;
+
+#ifdef __linux__
+       FILE *f;
+       char buf[256];
+       snprintf(buf, sizeof(buf) - 1, "/proc/%u/stat", (unsigned)p);
+
+       if (!(f = fopen(buf, "r")))
+               return 0;
+
+       fscanf(f, "%*u %*s %*c %u", &v);
+       fclose(f);
+#endif /* __linux__*/
+
+#ifdef __OpenBSD__
+       int n;
+       kvm_t *kd;
+       struct kinfo_proc *kp;
+
+       kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
+       if (!kd)
+               return 0;
+
+       kp = kvm_getprocs(kd, KERN_PROC_PID, p, sizeof(*kp), &n);
+       v = kp->p_ppid;
+#endif /* __OpenBSD__ */
+
+       return (pid_t)v;
+}
+
+int
+isdescprocess(pid_t p, pid_t c)
+{
+       while (p != c && c != 0)
+               c = getparentprocess(c);
+
+       return (int)c;
+}
+
+Client *
+termforwin(const Client *w)
+{
+       Client *c;
+       Monitor *m;
+
+       if (!w->pid || w->isterminal)
+               return NULL;
+
+       for (m = mons; m; m = m->next) {
+               for (c = m->clients; c; c = c->next) {
+                       if (c->isterminal && !c->swallowing && c->pid && isdescprocess(c->pid, w->pid))
+                               return c;
+               }
+       }
+
+       return NULL;
+}
+
+Client *
+swallowingclient(Window w)
+{
+       Client *c;
+       Monitor *m;
+
+       for (m = mons; m; m = m->next) {
+               for (c = m->clients; c; c = c->next) {
+                       if (c->swallowing && c->swallowing->win == w)
+                               return c;
+               }
+       }
+
+       return NULL;
+}
+
 Client *
 wintoclient(Window w)
 {
@@ -2310,12 +2582,14 @@ main(int argc, char *argv[])
                fputs("warning: no locale support\n", stderr);
        if (!(dpy = XOpenDisplay(NULL)))
                die("dwm: cannot open display");
+       if (!(xcon = XGetXCBConnection(dpy)))
+               die("dwm: cannot get xcb connection\n");
        checkotherwm();
        XrmInitialize();
        load_xresources();
        setup();
 #ifdef __OpenBSD__
-       if (pledge("stdio rpath proc exec", NULL) == -1)
+       if (pledge("stdio rpath proc exec ps", NULL) == -1)
                die("pledge");
 #endif /* __OpenBSD__ */
        scan();