]> git.ekhem.eu.org Git - st.git/commitdiff
Patch alpha-focus
authorJakub Czajka <jakub@ekhem.eu.org>
Sun, 3 Oct 2021 14:55:02 +0000 (16:55 +0200)
committerJakub Czajka <jakub@ekhem.eu.org>
Sat, 14 Oct 2023 20:54:10 +0000 (22:54 +0200)
config.def.h
config.h
config.mk
st.c
st.h
x.c

index 20f1c886b3b1804f94cb528fe8b8509c703bdc63..d9323fe3dd3fe660dd18ccf565a5974ba4b87bdb 100644 (file)
@@ -93,6 +93,9 @@ char *termname = "st-256color";
  */
 unsigned int tabspaces = 8;
 
+/* bg opacity */
+float alpha = 0.8, alphaUnfocused = 0.6;
+
 /* Terminal colors (16 first used in escape sequence) */
 static const char *colorname[] = {
        /* 8 normal colors */
@@ -120,8 +123,7 @@ static const char *colorname[] = {
        /* more colors can be added after 255 to use with DefaultXX */
        "#cccccc",
        "#555555",
-       "gray90", /* default foreground colour */
-       "black", /* default background colour */
+       "black",
 };
 
 
@@ -133,6 +135,7 @@ unsigned int defaultfg = 258;
 unsigned int defaultbg = 259;
 unsigned int defaultcs = 256;
 static unsigned int defaultrcs = 257;
+unsigned int bg = 17, bgUnfocused = 16;
 
 /*
  * Default shape of cursor
index 9b99782b7ace2f73325f119147520a188027e1d2..9a866a3c2a07a6efe42f2bbbf0ed8b7158f141f2 100644 (file)
--- a/config.h
+++ b/config.h
@@ -93,6 +93,9 @@ char *termname = "st-256color";
  */
 unsigned int tabspaces = 8;
 
+/* bg opacity */
+float alpha = 0.8, alphaUnfocused = 0.6;
+
 /* Terminal colors (16 first used in escape sequence) */
 static const char *colorname[] = {
        /* 8 normal colors */
@@ -120,6 +123,7 @@ static const char *colorname[] = {
        /* more colors can be added after 255 to use with DefaultXX */
        "#cccccc",
        "#555555",
+       "black",
 };
 
 
@@ -131,6 +135,7 @@ unsigned int defaultfg = 7;
 unsigned int defaultbg = 0;
 static unsigned int defaultcs = 256;
 static unsigned int defaultrcs = 257;
+unsigned int bg = 17, bgUnfocused = 16;
 
 /*
  * Default shape of cursor
index 1e306f8c8f9e51cbca04911905f458bdc06287e1..47c615e63395830f06ff764c636d8082c9a58cbc 100644 (file)
--- a/config.mk
+++ b/config.mk
@@ -16,7 +16,7 @@ PKG_CONFIG = pkg-config
 INCS = -I$(X11INC) \
        `$(PKG_CONFIG) --cflags fontconfig` \
        `$(PKG_CONFIG) --cflags freetype2`
-LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
+LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\
        `$(PKG_CONFIG) --libs fontconfig` \
        `$(PKG_CONFIG) --libs freetype2`
 
diff --git a/st.c b/st.c
index 62def59f17cbf1bfd841210453ba29e5578d5610..8ee76a39bd217915dbcc5ff864b6da118cced356 100644 (file)
--- a/st.c
+++ b/st.c
@@ -194,7 +194,6 @@ static void tsetscroll(int, int);
 static void tswapscreen(void);
 static void tsetmode(int, int, const int *, int);
 static int twrite(const char *, int, int);
-static void tfulldirt(void);
 static void tcontrolcode(uchar );
 static void tdectest(char );
 static void tdefutf8(char);
diff --git a/st.h b/st.h
index fd3b0d8501e71a08cc7718be0a670510c3fa60e5..13be339b5ae678c6f9fc3865030c1bd5fd752ab6 100644 (file)
--- a/st.h
+++ b/st.h
@@ -79,6 +79,7 @@ typedef union {
 
 void die(const char *, ...);
 void redraw(void);
+void tfulldirt(void);
 void draw(void);
 
 void printscreen(const Arg *);
@@ -124,3 +125,4 @@ extern unsigned int tabspaces;
 extern unsigned int defaultfg;
 extern unsigned int defaultbg;
 extern unsigned int defaultcs;
+extern float alpha, alphaUnfocused;
diff --git a/x.c b/x.c
index ab10269ef49d0f9345d62488e8ca7b94c91353cb..09ed2cb30e131cdf45e3e94e4ebcfa040dce1d9f 100644 (file)
--- a/x.c
+++ b/x.c
@@ -119,6 +119,7 @@ typedef struct {
        XSetWindowAttributes attrs;
        int scr;
        int isfixed; /* is fixed geometry? */
+       int depth; /* bit depth */
        int l, t; /* left and top offset */
        int gm; /* geometry mask */
 } XWindow;
@@ -257,6 +258,7 @@ static char *usedfont = NULL;
 static double usedfontsize = 0;
 static double defaultfontsize = 0;
 
+static char *opt_alpha = NULL;
 static char *opt_class = NULL;
 static char **opt_cmd  = NULL;
 static char *opt_embed = NULL;
@@ -267,6 +269,9 @@ static char *opt_name  = NULL;
 static char *opt_title = NULL;
 
 static uint buttons; /* bit field of pressed buttons */
+static int oldbutton = 3; /* button event on startup: 3 = release */
+
+static int focused = 0;
 
 void
 clipcopy(const Arg *dummy)
@@ -766,7 +771,7 @@ xresize(int col, int row)
 
        XFreePixmap(xw.dpy, xw.buf);
        xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
-                       DefaultDepth(xw.dpy, xw.scr));
+                       xw.depth);
        XftDrawChange(xw.draw, xw.buf);
        xclear(0, 0, win.w, win.h);
 
@@ -804,28 +809,38 @@ xloadcolor(int i, const char *name, Color *ncolor)
        return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
 }
 
+void
+xloadalpha(void)
+{
+       float const usedAlpha = focused ? alpha : alphaUnfocused;
+       if (opt_alpha) alpha = strtof(opt_alpha, NULL);
+       dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha);
+       dc.col[defaultbg].pixel &= 0x00FFFFFF;
+       dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24;
+}
+
 void
 xloadcols(void)
 {
-       int i;
        static int loaded;
        Color *cp;
 
-       if (loaded) {
-               for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
-                       XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
-       } else {
-               dc.collen = MAX(LEN(colorname), 256);
-               dc.col = xmalloc(dc.collen * sizeof(Color));
+       if (!loaded) {
+               dc.collen = 1 + (defaultbg = MAX(LEN(colorname), 256));
+               dc.col = xmalloc((dc.collen) * sizeof(Color));
        }
 
-       for (i = 0; i < dc.collen; i++)
+       for (int i = 0; i+1 < dc.collen; ++i)
                if (!xloadcolor(i, NULL, &dc.col[i])) {
                        if (colorname[i])
                                die("could not allocate color '%s'\n", colorname[i]);
                        else
                                die("could not allocate color %d\n", i);
                }
+       if (dc.collen) // cannot die, as the color is already loaded.
+               xloadcolor(focused ?bg :bgUnfocused, NULL, &dc.col[defaultbg]);
+
+       xloadalpha();
        loaded = 1;
 }
 
@@ -1148,9 +1163,22 @@ xinit(int cols, int rows)
        Window parent;
        pid_t thispid = getpid();
        XColor xmousefg, xmousebg;
+       XWindowAttributes attr;
+       XVisualInfo vis;
 
        xw.scr = XDefaultScreen(xw.dpy);
-       xw.vis = XDefaultVisual(xw.dpy, xw.scr);
+
+       if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
+               parent = XRootWindow(xw.dpy, xw.scr);
+               xw.depth = 32;
+       } else {
+               XGetWindowAttributes(xw.dpy, parent, &attr);
+               xw.depth = attr.depth;
+       }
+
+       XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
+       xw.vis = vis.visual;
+
 
        /* font */
        if (!FcInit())
@@ -1160,7 +1188,7 @@ xinit(int cols, int rows)
        xloadfonts(usedfont, 0);
 
        /* colors */
-       xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
+       xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
        xloadcols();
 
        /* adjust fixed window geometry */
@@ -1180,19 +1208,15 @@ xinit(int cols, int rows)
                | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
        xw.attrs.colormap = xw.cmap;
 
-       if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
-               parent = XRootWindow(xw.dpy, xw.scr);
        xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
-                       win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
+                       win.w, win.h, 0, xw.depth, InputOutput,
                        xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
                        | CWEventMask | CWColormap, &xw.attrs);
 
        memset(&gcvalues, 0, sizeof(gcvalues));
        gcvalues.graphics_exposures = False;
-       dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
-                       &gcvalues);
-       xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
-                       DefaultDepth(xw.dpy, xw.scr));
+       xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
+       dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
        XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
        XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
 
@@ -1789,12 +1813,22 @@ focus(XEvent *ev)
                xseturgency(0);
                if (IS_SET(MODE_FOCUS))
                        ttywrite("\033[I", 3, 0);
+               if (!focused) {
+                       focused = 1;
+                       xloadcols();
+                       tfulldirt();
+               }
        } else {
                if (xw.ime.xic)
                        XUnsetICFocus(xw.ime.xic);
                win.mode &= ~MODE_FOCUSED;
                if (IS_SET(MODE_FOCUS))
                        ttywrite("\033[O", 3, 0);
+               if (focused) {
+                       focused = 0;
+                       xloadcols();
+                       tfulldirt();
+               }
        }
 }
 
@@ -2103,6 +2137,9 @@ main(int argc, char *argv[])
        case 'a':
                allowaltscreen = 0;
                break;
+       case 'A':
+               opt_alpha = EARGF(usage());
+               break;
        case 'c':
                opt_class = EARGF(usage());
                break;
@@ -2159,6 +2196,7 @@ run:
        config_init();
        cols = MAX(cols, 1);
        rows = MAX(rows, 1);
+       defaultbg = MAX(LEN(colorname), 256);
        tnew(cols, rows);
        xinit(cols, rows);
        xsetenv();