[hackers] [st][PATCH] Change background transparency on the fly

From: Bakar Chargeishvili <bakar.chargeishvili_AT_gmx.de>
Date: Tue, 9 Apr 2019 16:58:52 +0400

Hi,

This is modified version of st-alpha-0.8.2 patch. New function is added
to control background transparency using key bindings. This is
particularly useful when multiple terminal windows cover each other
(e.g. in the monocle layout of dwm) and lines on the background window
disturb to focus on the main window. In this case one can easily adjust
transparency of the front window and keep working.

Cheers,
Bakar

---
 config.def.h |  8 ++++++-
 config.mk    |  2 +-
 st.c         |  2 +-
 st.h         |  1 +
 win.h        |  2 +-
 x.c          | 59 +++++++++++++++++++++++++++++++++++++++++-----------
 6 files changed, 58 insertions(+), 16 deletions(-)
diff --git a/config.def.h b/config.def.h
index 482901e..bcbbb38 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -82,6 +82,9 @@ char *termname = "st-256color";
  */
 unsigned int tabspaces = 8;
+/* bg opacity */
+float alpha = 0.8;
+
 /* Terminal colors (16 first used in escape sequence) */
 static const char *colorname[] = {
 	/* 8 normal colors */
_AT_@ -109,6 +112,7 @@ static const char *colorname[] = {
 	/* more colors can be added after 255 to use with DefaultXX */
 	"#cccccc",
 	"#555555",
+	"black",
 };
_AT_@ -117,7 +121,7 @@ static const char *colorname[] = {
  * foreground, background, cursor, reverse cursor
  */
 unsigned int defaultfg = 7;
-unsigned int defaultbg = 0;
+unsigned int defaultbg = 258;
 static unsigned int defaultcs = 256;
 static unsigned int defaultrcs = 257;
_AT_@ -178,6 +182,8 @@ static Shortcut shortcuts[] = {
 	{ TERMMOD,              XK_Y,           selpaste,       {.i =  0} },
 	{ ShiftMask,            XK_Insert,      selpaste,       {.i =  0} },
 	{ TERMMOD,              XK_Num_Lock,    numlock,        {.i =  0} },
+	{ TERMMOD,              XK_A,           chalpha,        {.f = +0.1} },
+	{ TERMMOD,              XK_D,           chalpha,        {.f = -0.1} },
 };
 /*
diff --git a/config.mk b/config.mk
index 0cbb002..1d2f0e2 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -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 8e6ccb5..c3975f7 100644
--- a/st.c
+++ b/st.c
_AT_@ -2251,7 +2251,7 @@ eschandle(uchar ascii)
 	case 'c': /* RIS -- Reset to initial state */
 		treset();
 		resettitle();
-		xloadcols();
+		xloadcols(alpha);
 		break;
 	case '=': /* DECPAM -- Application keypad */
 		xsetmode(1, MODE_APPKEYPAD);
diff --git a/st.h b/st.h
index 4da3051..6799ef6 100644
--- a/st.h
+++ b/st.h
_AT_@ -120,3 +120,4 @@ extern char *termname;
 extern unsigned int tabspaces;
 extern unsigned int defaultfg;
 extern unsigned int defaultbg;
+extern float alpha;
diff --git a/win.h b/win.h
index a6ef1b9..13fdb0b 100644
--- a/win.h
+++ b/win.h
_AT_@ -28,7 +28,7 @@ void xclipcopy(void);
 void xdrawcursor(int, int, Glyph, int, int, Glyph);
 void xdrawline(Line, int, int, int);
 void xfinishdraw(void);
-void xloadcols(void);
+void xloadcols(float);
 int xsetcolorname(int, const char *);
 void xsettitle(char *);
 int xsetcursor(int);
diff --git a/x.c b/x.c
index 5828a3b..7a06d38 100644
--- a/x.c
+++ b/x.c
_AT_@ -53,6 +53,7 @@ static void clipcopy(const Arg *);
 static void clippaste(const Arg *);
 static void numlock(const Arg *);
 static void selpaste(const Arg *);
+static void chalpha(const Arg *);
 static void zoom(const Arg *);
 static void zoomabs(const Arg *);
 static void zoomreset(const Arg *);
_AT_@ -98,6 +99,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;
_AT_@ -233,6 +235,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;
_AT_@ -282,6 +285,19 @@ numlock(const Arg *dummy)
 	win.mode ^= MODE_NUMLOCK;
 }
+void
+chalpha(const Arg *arg)
+{
+	alpha += arg->f;
+	if (alpha > 1)
+		alpha = 0;
+	if (alpha < 0)
+		alpha = 1;
+
+	xloadcols(alpha);
+	redraw();
+}
+
 void
 zoom(const Arg *arg)
 {
_AT_@ -692,7 +708,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);
_AT_@ -731,7 +747,7 @@ xloadcolor(int i, const char *name, Color *ncolor)
 }
 void
-xloadcols(void)
+xloadcols(float alpha)
 {
 	int i;
 	static int loaded;
_AT_@ -752,6 +768,11 @@ xloadcols(void)
 			else
 				die("could not allocate color %d\n", i);
 		}
+
+	/* set alpha value of bg color */
+	dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha);
+	dc.col[defaultbg].pixel &= 0x00FFFFFF;
+	dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24;
 	loaded = 1;
 }
_AT_@ -1044,11 +1065,23 @@ xinit(int cols, int rows)
 	Window parent;
 	pid_t thispid = getpid();
 	XColor xmousefg, xmousebg;
+	XWindowAttributes attr;
+	XVisualInfo vis;
 	if (!(xw.dpy = XOpenDisplay(NULL)))
 		die("can't open display\n");
 	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())
_AT_@ -1058,8 +1091,11 @@ xinit(int cols, int rows)
 	xloadfonts(usedfont, 0);
 	/* colors */
-	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
-	xloadcols();
+	xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
+	/* set alpha value of bg color */
+	if (opt_alpha)
+		alpha = strtof(opt_alpha, NULL);
+	xloadcols(alpha);
 	/* adjust fixed window geometry */
 	win.w = 2 * borderpx + cols * win.cw;
_AT_@ -1078,19 +1114,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);
_AT_@ -1925,6 +1957,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;
--
2.21.0
Received on Tue Apr 09 2019 - 14:58:52 CEST

This archive was generated by hypermail 2.3.0 : Tue Apr 09 2019 - 15:12:22 CEST