From 69b6d27087895ddab63fd4d623bc6636adb79722 Mon Sep 17 00:00:00 2001 From: LordRusk Date: Thu, 26 Mar 2020 00:43:14 +0000 Subject: [PATCH] AlphaFocusHighlight patch instead of Alpha patch --- config.h | 3 ++- st.h | 1 + x.c | 44 ++++++++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 15 deletions(-) diff --git a/config.h b/config.h index 81804d5..3c87f46 100644 --- a/config.h +++ b/config.h @@ -91,7 +91,8 @@ char *termname = "st-256color"; unsigned int tabspaces = 8; /* bg opacity */ -float alpha = 0.92; +float alpha = 0.92; //< alpha value used when the window is focused. +float alphaUnfocussed = 0.8; //< alpha value used when the focus is lost /* Terminal colors (16 first used in escape sequence) */ static const char *colorname[] = { diff --git a/st.h b/st.h index 4504a30..3a06279 100644 --- a/st.h +++ b/st.h @@ -132,5 +132,6 @@ extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; extern float alpha; +extern float alphaUnfocussed; extern MouseKey mkeys[]; extern int ximspot_update_interval; diff --git a/x.c b/x.c index c09392b..de2d68f 100644 --- a/x.c +++ b/x.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -261,6 +262,7 @@ static char *opt_io = NULL; static char *opt_line = NULL; static char *opt_name = NULL; static char *opt_title = NULL; +static bool focused = true; static int oldbutton = 3; /* button event on startup: 3 = release */ @@ -765,6 +767,20 @@ xloadcolor(int i, const char *name, Color *ncolor) return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor); } +void +xloadalpha(void) +{ + /* set alpha value of bg color */ + if (opt_alpha) + alpha = strtof(opt_alpha, NULL); + + float const usedAlpha = focused ? alpha : alphaUnfocussed; + + 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) { @@ -788,19 +804,9 @@ 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].color.red = - ((unsigned short)(dc.col[defaultbg].color.red * alpha)) & 0xff00; - dc.col[defaultbg].color.green = - ((unsigned short)(dc.col[defaultbg].color.green * alpha)) & 0xff00; - dc.col[defaultbg].color.blue = - ((unsigned short)(dc.col[defaultbg].color.blue * alpha)) & 0xff00; - dc.col[defaultbg].pixel &= 0x00FFFFFF; - dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24; - loaded = 1; + xloadalpha(); + loaded = 1; + } int @@ -1812,13 +1818,23 @@ focus(XEvent *ev) XSetICFocus(xw.xic); win.mode |= MODE_FOCUSED; xseturgency(0); - if (IS_SET(MODE_FOCUS)) ttywrite("\033[I", 3, 0); + if (!focused) { + focused = true; + xloadalpha(); + redraw(); + } } else { XUnsetICFocus(xw.xic); win.mode &= ~MODE_FOCUSED; if (IS_SET(MODE_FOCUS)) ttywrite("\033[O", 3, 0); + if (focused) { + focused = false; + xloadalpha(); + redraw(); + } + } }