Xinqi Bao's Git

patch: fibonacci
[dwm.git] / dwm.c
diff --git a/dwm.c b/dwm.c
index f266a37..556d934 100644 (file)
--- a/dwm.c
+++ b/dwm.c
@@ -63,6 +63,8 @@
 #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
 #define TEXTW(X)                (drw_fontset_getwidth(drw, (X)) + lrpad)
 
+#define OPAQUE                  0xffU
+
 /* enums */
 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
 enum { SchemeNorm, SchemeSel }; /* color schemes */
@@ -127,6 +129,7 @@ struct Monitor {
        int by;               /* bar geometry */
        int mx, my, mw, mh;   /* screen size */
        int wx, wy, ww, wh;   /* window area  */
+       int gappx;            /* gaps between windows */
        unsigned int seltags;
        unsigned int sellt;
        unsigned int tagset[2];
@@ -210,18 +213,22 @@ static void sendmon(Client *c, Monitor *m);
 static void setclientstate(Client *c, long state);
 static void setfocus(Client *c);
 static void setfullscreen(Client *c, int fullscreen);
+static void setgaps(const Arg *arg);
 static void setlayout(const Arg *arg);
 static void setmfact(const Arg *arg);
 static void setup(void);
 static void seturgent(Client *c, int urg);
 static void showhide(Client *c);
 static void sigchld(int unused);
+static void sighup(int unused);
+static void sigterm(int unused);
 static void spawn(const Arg *arg);
 static void tag(const Arg *arg);
 static void tagmon(const Arg *arg);
 static void tile(Monitor *);
 static void togglebar(const Arg *arg);
 static void togglefloating(const Arg *arg);
+static void togglefullscr(const Arg *arg);
 static void toggletag(const Arg *arg);
 static void toggleview(const Arg *arg);
 static void unfocus(Client *c, int setfocus);
@@ -243,6 +250,7 @@ static Monitor *wintomon(Window w);
 static int xerror(Display *dpy, XErrorEvent *ee);
 static int xerrordummy(Display *dpy, XErrorEvent *ee);
 static int xerrorstart(Display *dpy, XErrorEvent *ee);
+static void xinitvisual();
 static void zoom(const Arg *arg);
 
 static pid_t getparentprocess(pid_t p);
@@ -277,6 +285,7 @@ static void (*handler[LASTEvent]) (XEvent *) = {
        [UnmapNotify] = unmapnotify
 };
 static Atom wmatom[WMLast], netatom[NetLast];
+static int restart = 0;
 static int running = 1;
 static Cur *cursor[CurLast];
 static Clr **scheme;
@@ -287,6 +296,11 @@ static Window root, wmcheckwin;
 
 static xcb_connection_t *xcon;
 
+static int useargb = 0;
+static Visual *visual;
+static int depth;
+static Colormap cmap;
+
 /* configuration, allows nested code to access above variables */
 #include "config.h"
 
@@ -707,6 +721,7 @@ createmon(void)
        m->nmaster = nmaster;
        m->showbar = showbar;
        m->topbar = topbar;
+       m->gappx = gappx;
        m->lt[0] = &layouts[0];
        m->lt[1] = &layouts[1 % LENGTH(layouts)];
        strncpy(m->ltsymbol, layouts[0].symbol, sizeof m->ltsymbol);
@@ -1327,6 +1342,7 @@ propertynotify(XEvent *e)
 void
 quit(const Arg *arg)
 {
+       if(arg->i) restart = 1;
        running = 0;
 }
 
@@ -1576,6 +1592,16 @@ setfullscreen(Client *c, int fullscreen)
        }
 }
 
+void
+setgaps(const Arg *arg)
+{
+       if ((arg->i == 0) || (selmon->gappx + arg->i < 0))
+               selmon->gappx = 0;
+       else
+               selmon->gappx += arg->i;
+       arrange(selmon);
+}
+
 void
 setlayout(const Arg *arg)
 {
@@ -1615,12 +1641,16 @@ setup(void)
        /* clean up any zombies immediately */
        sigchld(0);
 
+       signal(SIGHUP, sighup);
+       signal(SIGTERM, sigterm);
+
        /* init screen */
        screen = DefaultScreen(dpy);
        sw = DisplayWidth(dpy, screen);
        sh = DisplayHeight(dpy, screen);
        root = RootWindow(dpy, screen);
-       drw = drw_create(dpy, screen, root, sw, sh);
+       xinitvisual();
+       drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
        if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
                die("no fonts could be loaded.");
        lrpad = drw->fonts->h;
@@ -1648,7 +1678,7 @@ setup(void)
        /* init appearance */
        scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
        for (i = 0; i < LENGTH(colors); i++)
-               scheme[i] = drw_scm_create(drw, colors[i], 3);
+               scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3);
        /* init bars */
        updatebars();
        updatestatus();
@@ -1715,6 +1745,20 @@ sigchld(int unused)
        while (0 < waitpid(-1, NULL, WNOHANG));
 }
 
+void
+sighup(int unused)
+{
+       Arg a = {.i = 1};
+       quit(&a);
+}
+
+void
+sigterm(int unused)
+{
+       Arg a = {.i = 0};
+       quit(&a);
+}
+
 void
 spawn(const Arg *arg)
 {
@@ -1762,18 +1806,18 @@ tile(Monitor *m)
        if (n > m->nmaster)
                mw = m->nmaster ? m->ww * m->mfact : 0;
        else
-               mw = m->ww;
-       for (i = my = ty = 0, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
+               mw = m->ww - m->gappx;
+       for (i = 0, my = ty = m->gappx, c = nexttiled(m->clients); c; c = nexttiled(c->next), i++)
                if (i < m->nmaster) {
-                       h = (m->wh - my) / (MIN(n, m->nmaster) - i);
-                       resize(c, m->wx, m->wy + my, mw - (2*c->bw), h - (2*c->bw), 0);
-                       if (my + HEIGHT(c) < m->wh)
-                               my += HEIGHT(c);
+                       h = (m->wh - my) / (MIN(n, m->nmaster) - i) - m->gappx;
+                       resize(c, m->wx + m->gappx, m->wy + my, mw - (2*c->bw) - m->gappx, h - (2*c->bw), 0);
+                       if (my + HEIGHT(c) + m->gappx < m->wh)
+                               my += HEIGHT(c) + m->gappx;
                } else {
-                       h = (m->wh - ty) / (n - i);
-                       resize(c, m->wx + mw, m->wy + ty, m->ww - mw - (2*c->bw), h - (2*c->bw), 0);
-                       if (ty + HEIGHT(c) < m->wh)
-                               ty += HEIGHT(c);
+                       h = (m->wh - ty) / (n - i) - m->gappx;
+                       resize(c, m->wx + mw + m->gappx, m->wy + ty, m->ww - mw - (2*c->bw) - 2*m->gappx, h - (2*c->bw), 0);
+                       if (ty + HEIGHT(c) + m->gappx < m->wh)
+                               ty += HEIGHT(c) + m->gappx;
                }
 }
 
@@ -1800,6 +1844,13 @@ togglefloating(const Arg *arg)
        arrange(selmon);
 }
 
+void
+togglefullscr(const Arg *arg)
+{
+  if(selmon->sel)
+    setfullscreen(selmon->sel, !selmon->sel->isfullscreen);
+}
+
 void
 toggletag(const Arg *arg)
 {
@@ -1902,16 +1953,18 @@ updatebars(void)
        Monitor *m;
        XSetWindowAttributes wa = {
                .override_redirect = True,
-               .background_pixmap = ParentRelative,
+               .background_pixel = 0,
+               .border_pixel = 0,
+               .colormap = cmap,
                .event_mask = ButtonPressMask|ExposureMask
        };
        XClassHint ch = {"dwm", "dwm"};
        for (m = mons; m; m = m->next) {
                if (m->barwin)
                        continue;
-               m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
-                               CopyFromParent, DefaultVisual(dpy, screen),
-                               CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
+               m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
+                                         InputOutput, visual,
+                                         CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
                XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
                XMapRaised(dpy, m->barwin);
                XSetClassHint(dpy, m->barwin, &ch);
@@ -2338,6 +2391,43 @@ xerrorstart(Display *dpy, XErrorEvent *ee)
        return -1;
 }
 
+void
+xinitvisual()
+{
+       XVisualInfo *infos;
+       XRenderPictFormat *fmt;
+       int nitems;
+       int i;
+
+       XVisualInfo tpl = {
+               .screen = screen,
+               .depth = 32,
+               .class = TrueColor
+       };
+       long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
+
+       infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
+       visual = NULL;
+       for(i = 0; i < nitems; i ++) {
+               fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
+               if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
+                       visual = infos[i].visual;
+                       depth = infos[i].depth;
+                       cmap = XCreateColormap(dpy, root, visual, AllocNone);
+                       useargb = 1;
+                       break;
+               }
+       }
+
+       XFree(infos);
+
+       if (! visual) {
+               visual = DefaultVisual(dpy, screen);
+               depth = DefaultDepth(dpy, screen);
+               cmap = DefaultColormap(dpy, screen);
+       }
+}
+
 void
 zoom(const Arg *arg)
 {
@@ -2373,6 +2463,7 @@ main(int argc, char *argv[])
 #endif /* __OpenBSD__ */
        scan();
        run();
+       if(restart) execvp(argv[0], argv);
        cleanup();
        XCloseDisplay(dpy);
        return EXIT_SUCCESS;