From 69925ee23b8b1590f65e1f71f2d9ea5156629868 Mon Sep 17 00:00:00 2001 From: Moritz Biering Date: Wed, 5 May 2021 21:16:25 +0200 Subject: [PATCH 1/2] Add support for additional alpha when the term-window is not focused --- Xdefaults | 1 + config.h | 3 +++ st.c | 1 - st.h | 2 ++ x.c | 42 ++++++++++++++++++++++++++++++------------ 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/Xdefaults b/Xdefaults index 704f2db..2c2d2e0 100644 --- a/Xdefaults +++ b/Xdefaults @@ -1,5 +1,6 @@ !! Transparency (0-1): st.alpha: 0.92 +st.alphaUnfocus: 0.62 !! Set a default font and font size as below: st.font: Monospace-11; diff --git a/config.h b/config.h index a0d8c1f..3adf598 100644 --- a/config.h +++ b/config.h @@ -108,6 +108,7 @@ unsigned int tabspaces = 8; /* bg opacity */ float alpha = 0.8; +float alphaUnfocus = 0.8; /* Terminal colors (16 first used in escape sequence) */ static const char *colorname[] = { @@ -144,6 +145,7 @@ unsigned int defaultfg = 259; unsigned int defaultbg = 258; unsigned int defaultcs = 256; unsigned int defaultrcs = 257; +unsigned int background = 258; /* * Default shape of cursor @@ -217,6 +219,7 @@ ResourcePref resources[] = { { "cwscale", FLOAT, &cwscale }, { "chscale", FLOAT, &chscale }, { "alpha", FLOAT, &alpha }, + { "alphaUnfocus", FLOAT, &alphaUnfocus }, }; /* diff --git a/st.c b/st.c index 1cf277c..f9f8068 100644 --- a/st.c +++ b/st.c @@ -201,7 +201,6 @@ static void tsetscroll(int, int); static void tswapscreen(void); static void tsetmode(int, int, 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 ac76b71..3ddfadd 100644 --- a/st.h +++ b/st.h @@ -82,6 +82,7 @@ typedef union { void die(const char *, ...); void redraw(void); +void tfulldirt(void); void draw(void); void externalpipe(const Arg *); @@ -138,4 +139,5 @@ extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; extern float alpha; +extern float alphaUnfocus; extern const int boxdraw, boxdraw_bold, boxdraw_braille; diff --git a/x.c b/x.c index 696ade9..d1721d7 100644 --- a/x.c +++ b/x.c @@ -272,6 +272,8 @@ static char *opt_line = NULL; static char *opt_name = NULL; static char *opt_title = NULL; +static int focused = 0; + static int oldbutton = 3; /* button event on startup: 3 = release */ void @@ -807,6 +809,16 @@ 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 : alphaUnfocus; + 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) { @@ -814,15 +826,12 @@ xloadcols(void) 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); + 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 (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]); @@ -830,12 +839,10 @@ xloadcols(void) die("could not allocate color %d\n", i); } - /* set alpha value of bg color */ - if (opt_alpha) - alpha = strtof(opt_alpha, NULL); - dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha); - dc.col[defaultbg].pixel &= 0x00FFFFFF; - dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24; + if (dc.collen) // cannot die, as the color is already loaded. + xloadcolor(background, NULL, &dc.col[defaultbg]); + + xloadalpha(); loaded = 1; } @@ -1910,12 +1917,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(); + } } } @@ -2280,6 +2297,7 @@ run: config_init(); cols = MAX(cols, 1); rows = MAX(rows, 1); + defaultbg = MAX(LEN(colorname), 256); tnew(cols, rows); xinit(cols, rows); xsetenv(); From bb56685063532f732f4fbdba1d890931dad3d891 Mon Sep 17 00:00:00 2001 From: Moritz Biering Date: Thu, 6 May 2021 14:32:57 +0200 Subject: [PATCH 2/2] Use the additional alpha-value as offset to support a changing alpha properly --- Xdefaults | 2 +- config.h | 5 +++-- x.c | 17 +++++++++++++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Xdefaults b/Xdefaults index 2c2d2e0..040c772 100644 --- a/Xdefaults +++ b/Xdefaults @@ -1,6 +1,6 @@ !! Transparency (0-1): st.alpha: 0.92 -st.alphaUnfocus: 0.62 +st.alphaOffset: 0.3 !! Set a default font and font size as below: st.font: Monospace-11; diff --git a/config.h b/config.h index 3adf598..d6eeade 100644 --- a/config.h +++ b/config.h @@ -108,7 +108,8 @@ unsigned int tabspaces = 8; /* bg opacity */ float alpha = 0.8; -float alphaUnfocus = 0.8; +float alphaOffset = 0.0; +float alphaUnfocus; /* Terminal colors (16 first used in escape sequence) */ static const char *colorname[] = { @@ -219,7 +220,7 @@ ResourcePref resources[] = { { "cwscale", FLOAT, &cwscale }, { "chscale", FLOAT, &chscale }, { "alpha", FLOAT, &alpha }, - { "alphaUnfocus", FLOAT, &alphaUnfocus }, + { "alphaOffset", FLOAT, &alphaOffset }, }; /* diff --git a/x.c b/x.c index d1721d7..2580d2a 100644 --- a/x.c +++ b/x.c @@ -182,6 +182,7 @@ static void xsetenv(void); static void xseturgency(int); static int evcol(XEvent *); static int evrow(XEvent *); +static float clamp(float, float, float); static void expose(XEvent *); static void visibility(XEvent *); @@ -319,10 +320,8 @@ changealpha(const Arg *arg) { if((alpha > 0 && arg->f < 0) || (alpha < 1 && arg->f > 0)) alpha += arg->f; - if(alpha < 0) - alpha = 0; - if(alpha > 1) - alpha = 1; + alpha = clamp(alpha, 0.0, 1.0); + alphaUnfocus = clamp(alpha-alphaOffset, 0.0, 1.0); xloadcols(); redraw(); @@ -381,6 +380,15 @@ evrow(XEvent *e) return y / win.ch; } +float +clamp(float value, float lower, float upper) { + if(value < lower) + return lower; + if(value > upper) + return upper; + return value; +} + void mousesel(XEvent *e, int done) { @@ -2298,6 +2306,7 @@ run: cols = MAX(cols, 1); rows = MAX(rows, 1); defaultbg = MAX(LEN(colorname), 256); + alphaUnfocus = alpha-alphaOffset; tnew(cols, rows); xinit(cols, rows); xsetenv();