JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
show config.h changes needed for compatibility with _SWM_WS hack
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index 7ca1ace..a6e4fc0 100644 (file)
--- a/dwm.c
+++ b/dwm.c
 #define TEXTW(X)                (drw_font_getexts_width(drw->font, X, strlen(X)) + drw->font->h)
 
 /* enums */
-enum { CurNormal, CurResize, CurMove, CurLast };        /* cursor */
-enum { ColBorder, ColFG, ColBG, ColLast };              /* color */
+enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
+enum { SchemeNorm, SchemeSel, SchemeLast }; /* color schemes */
 enum { NetSupported, NetWMName, NetWMState,
-       NetWMFullscreen, NetActiveWindow, NetWMWindowType,
-       NetWMWindowTypeDialog, NetClientList, NetLast };     /* EWMH atoms */
+       NetWMFullscreen, NetWMWindowOpacity, NetActiveWindow, NetWMWindowType,
+       NetWMWindowTypeDialog, NetClientList, NetLast }; /* EWMH atoms */
 enum { WMProtocols, WMDelete, WMState, WMTakeFocus, WMLast }; /* default atoms */
 enum { ClkTagBar, ClkLtSymbol, ClkStatusText, ClkWinTitle,
-       ClkClientWin, ClkRootWin, ClkLast };             /* clicks */
+       ClkClientWin, ClkRootWin, ClkLast }; /* clicks */
 
 typedef union {
        int i;
@@ -145,6 +145,7 @@ static void applyrules(Client *c);
 static Bool applysizehints(Client *c, int *x, int *y, int *w, int *h, Bool interact);
 static void arrange(Monitor *m);
 static void arrangemon(Monitor *m);
+static void attach_as_master(Client *c);
 static void attach(Client *c);
 static void attachstack(Client *c);
 static void buttonpress(XEvent *e);
@@ -184,6 +185,7 @@ static void monocle(Monitor *m);
 static void motionnotify(XEvent *e);
 static void movemouse(const Arg *arg);
 static Client *nexttiled(Client *c);
+static Client *nextvisible(Client *c);
 static void pop(Client *);
 static void propertynotify(XEvent *e);
 static void quit(const Arg *arg);
@@ -260,14 +262,27 @@ static void (*handler[LASTEvent]) (XEvent *) = {
 static Atom wmatom[WMLast], netatom[NetLast];
 static Bool running = True;
 static Cur *cursor[CurLast];
-static Theme thmnorm[ColLast];
-static Theme thmsel[ColLast];
+static ClrScheme scheme[SchemeLast];
 static Display *dpy;
 static Drw *drw;
 static Fnt *fnt;
 static Monitor *mons, *selmon;
 static Window root;
 
+// unfocused windows get transparent (feature)
+static const unsigned long unfocused_opacity[] = { 0xbfffffff };
+static void window_set_opaque(Client *c);
+static void window_set_translucent(Client *c);
+void
+window_set_opaque(Client *c) {
+       XDeleteProperty(dpy, c->win, netatom[NetWMWindowOpacity]);
+}
+void
+window_set_translucent(Client *c) {
+       XChangeProperty(dpy, c->win, netatom[NetWMWindowOpacity], XA_CARDINAL, 32, PropModeReplace, (unsigned char *)unfocused_opacity, 1);
+}
+
+
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
@@ -396,10 +411,19 @@ arrangemon(Monitor *m) {
 }
 
 void
-attach(Client *c) {
+attach_as_master(Client *c) {
        c->next = c->mon->clients;
        c->mon->clients = c;
 }
+void
+attach(Client *c) {
+       if (c->mon->sel) {
+               c->next = c->mon->sel->next;
+               c->mon->sel->next = c;
+       } else {
+               attach_as_master(c);
+       }
+}
 
 void
 attachstack(Client *c) {
@@ -476,12 +500,12 @@ cleanup(void) {
        drw_cur_free(drw, cursor[CurResize]);
        drw_cur_free(drw, cursor[CurMove]);
        drw_font_free(dpy, fnt);
-       drw_clr_free(thmnorm->border);
-       drw_clr_free(thmnorm->bg);
-       drw_clr_free(thmnorm->fg);
-       drw_clr_free(thmsel->border);
-       drw_clr_free(thmsel->bg);
-       drw_clr_free(thmsel->fg);
+       drw_clr_free(scheme[SchemeNorm].border);
+       drw_clr_free(scheme[SchemeNorm].bg);
+       drw_clr_free(scheme[SchemeNorm].fg);
+       drw_clr_free(scheme[SchemeSel].border);
+       drw_clr_free(scheme[SchemeSel].bg);
+       drw_clr_free(scheme[SchemeSel].fg);
        drw_free(drw);
        XSync(dpy, False);
        XSetInputFocus(dpy, PointerRoot, RevertToPointerRoot, CurrentTime);
@@ -528,6 +552,7 @@ clientmessage(XEvent *e) {
                                      || (cme->data.l[0] == 2 /* _NET_WM_STATE_TOGGLE */ && !c->isfullscreen)));
        }
        else if(cme->message_type == netatom[NetActiveWindow]) {
+               return;
                if(!ISVISIBLE(c)) {
                        c->mon->seltags ^= 1;
                        c->mon->tagset[c->mon->seltags] = c->tags;
@@ -663,16 +688,30 @@ detach(Client *c) {
        *tc = c->next;
 }
 
+// NOTE: the stack is for z-order and most-recently-focused
+// only mon->clients determines position in visible layout
 void
 detachstack(Client *c) {
-       Client **tc, *t;
-
-       for(tc = &c->mon->stack; *tc && *tc != c; tc = &(*tc)->snext);
-       *tc = c->snext;
-
+       Client *prev = NULL, *next_sel = NULL, *i;
+       for(i = c->mon->stack; i && i != c; i = i->snext) {
+               prev = i;
+       }
        if(c == c->mon->sel) {
-               for(t = c->mon->stack; t && !ISVISIBLE(t); t = t->snext);
-               c->mon->sel = t;
+               // find last visible window before c
+               // WARNING if you detach() before detachstack() this will select last visible window
+               for(i = nextvisible(c->mon->clients); i && i != c; i = nextvisible(i->next))
+                       next_sel = i;
+               // failing that, find first visible window (besides c)
+               if (!next_sel)
+                       for(i = nextvisible(c->mon->clients); i && i == c; i = nextvisible(i->next));
+                       if (i != c)
+                               next_sel = i;
+               c->mon->sel = next_sel;
+       }
+       if (prev) {
+               prev->snext = c->snext;
+       } else {
+               c->mon->stack = c->snext;
        }
 }
 
@@ -705,14 +744,14 @@ drawbar(Monitor *m) {
        x = 0;
        for(i = 0; i < LENGTH(tags); i++) {
                w = TEXTW(tags[i]);
-               drw_settheme(drw, m->tagset[m->seltags] & 1 << i ? thmsel : thmnorm);
+               drw_setscheme(drw, m->tagset[m->seltags] & 1 << i ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
                drw_text(drw, x, 0, w, bh, tags[i], urg & 1 << i);
                drw_rect(drw, x, 0, w, bh, m == selmon && selmon->sel && selmon->sel->tags & 1 << i,
                           occ & 1 << i, urg & 1 << i);
                x += w;
        }
        w = blw = TEXTW(m->ltsymbol);
-       drw_settheme(drw, thmnorm);
+       drw_setscheme(drw, &scheme[SchemeNorm]);
        drw_text(drw, x, 0, w, bh, m->ltsymbol, 0);
        x += w;
        xx = x;
@@ -730,12 +769,12 @@ drawbar(Monitor *m) {
        if((w = x - xx) > bh) {
                x = xx;
                if(m->sel) {
-                       drw_settheme(drw, m == selmon ? thmsel : thmnorm);
+                       drw_setscheme(drw, m == selmon ? &scheme[SchemeSel] : &scheme[SchemeNorm]);
                        drw_text(drw, x, 0, w, bh, m->sel->name, 0);
                        drw_rect(drw, x, 0, w, bh, m->sel->isfixed, m->sel->isfloating, 0);
                }
                else {
-                       drw_settheme(drw, thmnorm);
+                       drw_setscheme(drw, &scheme[SchemeNorm]);
                        drw_text(drw, x, 0, w, bh, NULL, 0);
                }
        }
@@ -793,7 +832,7 @@ focus(Client *c) {
                detachstack(c);
                attachstack(c);
                grabbuttons(c, True);
-               XSetWindowBorder(dpy, c->win, thmsel->border->rgb);
+               XSetWindowBorder(dpy, c->win, scheme[SchemeSel].border->rgb);
                setfocus(c);
        }
        else {
@@ -802,6 +841,8 @@ focus(Client *c) {
        }
        selmon->sel = c;
        drawbars();
+       if(c && (!root || (c->win!=root)) )
+               window_set_opaque(c);
 }
 
 void
@@ -832,7 +873,14 @@ focusstack(const Arg *arg) {
 
        if(!selmon->sel)
                return;
-       if(arg->i > 0) {
+       if(arg->i == 0) {
+               for(i = selmon->clients; i != selmon->sel; i = i->next) {
+                       if(ISVISIBLE(i)) {
+                               c = i;
+                               break;
+                       }
+               }
+       } else if(arg->i > 0) {
                for(c = selmon->sel->next; c && !ISVISIBLE(c); c = c->next);
                if(!c)
                        for(c = selmon->clients; c && !ISVISIBLE(c); c = c->next);
@@ -1041,7 +1089,7 @@ manage(Window w, XWindowAttributes *wa) {
 
        wc.border_width = c->bw;
        XConfigureWindow(dpy, w, CWBorderWidth, &wc);
-       XSetWindowBorder(dpy, w, thmnorm->border->rgb);
+       XSetWindowBorder(dpy, w, scheme[SchemeNorm].border->rgb);
        configure(c); /* propagates border_width, if size doesn't change */
        updatewindowtype(c);
        updatesizehints(c);
@@ -1063,7 +1111,7 @@ manage(Window w, XWindowAttributes *wa) {
        c->mon->sel = c;
        arrange(c->mon);
        XMapWindow(dpy, c->win);
-       focus(NULL);
+       focus(c);
 }
 
 void
@@ -1124,6 +1172,7 @@ movemouse(const Arg *arg) {
        Client *c;
        Monitor *m;
        XEvent ev;
+       Time lasttime = 0;
 
        if(!(c = selmon->sel))
                return;
@@ -1146,6 +1195,10 @@ movemouse(const Arg *arg) {
                        handler[ev.type](&ev);
                        break;
                case MotionNotify:
+                       if ((ev.xmotion.time - lasttime) <= (1000 / 60))
+                               continue;
+                       lasttime = ev.xmotion.time;
+
                        nx = ocx + (ev.xmotion.x - x);
                        ny = ocy + (ev.xmotion.y - y);
                        if(nx >= selmon->wx && nx <= selmon->wx + selmon->ww
@@ -1181,10 +1234,16 @@ nexttiled(Client *c) {
        return c;
 }
 
+Client *
+nextvisible(Client *c) {
+       for(; c && !ISVISIBLE(c); c = c->next);
+       return c;
+}
+
 void
 pop(Client *c) {
        detach(c);
-       attach(c);
+       attach_as_master(c);
        focus(c);
        arrange(c->mon);
 }
@@ -1265,11 +1324,11 @@ resizeclient(Client *c, int x, int y, int w, int h) {
 
 void
 resizemouse(const Arg *arg) {
-       int ocx, ocy;
-       int nw, nh;
+       int ocx, ocy, nw, nh;
        Client *c;
        Monitor *m;
        XEvent ev;
+       Time lasttime = 0;
 
        if(!(c = selmon->sel))
                return;
@@ -1291,6 +1350,10 @@ resizemouse(const Arg *arg) {
                        handler[ev.type](&ev);
                        break;
                case MotionNotify:
+                       if ((ev.xmotion.time - lasttime) <= (1000 / 60))
+                               continue;
+                       lasttime = ev.xmotion.time;
+
                        nw = MAX(ev.xmotion.x - ocx - 2 * c->bw + 1, 1);
                        nh = MAX(ev.xmotion.y - ocy - 2 * c->bw + 1, 1);
                        if(c->mon->wx + nw >= selmon->wx && c->mon->wx + nw <= selmon->wx + selmon->ww
@@ -1380,8 +1443,8 @@ sendmon(Client *c, Monitor *m) {
        if(c->mon == m)
                return;
        unfocus(c, True);
-       detach(c);
        detachstack(c);
+       detach(c);
        c->mon = m;
        c->tags = m->tagset[m->seltags]; /* assign tags of target monitor */
        attach(c);
@@ -1515,6 +1578,7 @@ setup(void) {
        netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
        netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
        netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
+       netatom[NetWMWindowOpacity] = XInternAtom(dpy, "_NET_WM_WINDOW_OPACITY", False);
        netatom[NetWMWindowType] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False);
        netatom[NetWMWindowTypeDialog] = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DIALOG", False);
        netatom[NetClientList] = XInternAtom(dpy, "_NET_CLIENT_LIST", False);
@@ -1523,12 +1587,12 @@ setup(void) {
        cursor[CurResize] = drw_cur_create(drw, XC_sizing);
        cursor[CurMove] = drw_cur_create(drw, XC_fleur);
        /* init appearance */
-       thmnorm->border = drw_clr_create(drw, normbordercolor);
-       thmnorm->bg = drw_clr_create(drw, normbgcolor);
-       thmnorm->fg = drw_clr_create(drw, normfgcolor);
-       thmsel->border = drw_clr_create(drw, selbordercolor);
-       thmsel->bg = drw_clr_create(drw, selbgcolor);
-       thmsel->fg = drw_clr_create(drw, selfgcolor);
+       scheme[SchemeNorm].border = drw_clr_create(drw, normbordercolor);
+       scheme[SchemeNorm].bg = drw_clr_create(drw, normbgcolor);
+       scheme[SchemeNorm].fg = drw_clr_create(drw, normfgcolor);
+       scheme[SchemeSel].border = drw_clr_create(drw, selbordercolor);
+       scheme[SchemeSel].bg = drw_clr_create(drw, selbgcolor);
+       scheme[SchemeSel].fg = drw_clr_create(drw, selfgcolor);
        /* init bars */
        updatebars();
        updatestatus();
@@ -1543,6 +1607,7 @@ setup(void) {
        XChangeWindowAttributes(dpy, root, CWEventMask|CWCursor, &wa);
        XSelectInput(dpy, root, wa.event_mask);
        grabkeys();
+       focus(NULL);
 }
 
 void
@@ -1570,6 +1635,19 @@ sigchld(int unused) {
 
 void
 spawn(const Arg *arg) {
+       int tag = 0, i;
+       if(arg->v == termcmd) {
+               for(i = 0; i < 32; ++i) {
+                       if(selmon->tagset[selmon->seltags] & (1 << i)) {
+                               tag = i;
+                               break;
+                       }
+               }
+               _SWM_WS[8] = swm_tags[tag][0];
+               _SWM_WS[9] = swm_tags[tag][1];
+       }
+       if(arg->v == dmenucmd)
+               dmenumon[0] = '0' + selmon->num;
        if(fork() == 0) {
                if(dpy)
                        close(ConnectionNumber(dpy));
@@ -1673,8 +1751,10 @@ void
 unfocus(Client *c, Bool setfocus) {
        if(!c)
                return;
+       if(!root || c->win!=root)
+               window_set_translucent(c);
        grabbuttons(c, False);
-       XSetWindowBorder(dpy, c->win, thmnorm->border->rgb);
+       XSetWindowBorder(dpy, c->win, scheme[SchemeNorm].border->rgb);
        if(setfocus) {
                XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
                XDeleteProperty(dpy, root, netatom[NetActiveWindow]);
@@ -1687,8 +1767,8 @@ unmanage(Client *c, Bool destroyed) {
        XWindowChanges wc;
 
        /* The server grab construct avoids race conditions. */
-       detach(c);
        detachstack(c);
+       detach(c);
        if(!destroyed) {
                wc.border_width = c->oldbw;
                XGrabServer(dpy);
@@ -1701,7 +1781,7 @@ unmanage(Client *c, Bool destroyed) {
                XUngrabServer(dpy);
        }
        free(c);
-       focus(NULL);
+       focus(selmon ? selmon->sel : NULL);
        updateclientlist();
        arrange(m);
 }
@@ -2044,7 +2124,7 @@ zoom(const Arg *arg) {
 int
 main(int argc, char *argv[]) {
        if(argc == 2 && !strcmp("-v", argv[1]))
-               die("dwm-"VERSION", © 2006-2012 dwm engineers, see LICENSE for details\n");
+               die("dwm-"VERSION", © 2006-2014 dwm engineers, see LICENSE for details\n");
        else if(argc != 1)
                die("usage: dwm [-v]\n");
        if(!setlocale(LC_CTYPE, "") || !XSupportsLocale())