commit 6a24cac844c1ec22992eb5703c0850684f768ca4
parent dd8bbb881b0fb221f66c2facc9e005ad49329175
Author: Matthew Carlson <matt@mcarlson.xyz>
Date: Fri, 2 Jul 2021 16:54:48 +0000
Merge branch 'alphafocus' into st
Diffstat:
M | config.h | | | 5 | +++-- |
M | st.c | | | 1 | - |
M | st.h | | | 3 | ++- |
M | x.c | | | 46 | +++++++++++++++++++++++++++++++--------------- |
4 files changed, 36 insertions(+), 19 deletions(-)
diff --git a/config.h b/config.h
@@ -111,7 +111,7 @@ char *termname = "st-256color";
unsigned int tabspaces = 8;
/* bg opacity */
-float alpha = 0.8;
+float alpha = 0.8, alphaUnfocused = 0.6;
/* Terminal colors (16 first used in escape sequence) */
static const char *colorname[] = {
@@ -149,9 +149,10 @@ static const char *colorname[] = {
* foreground, background, cursor, reverse cursor
*/
unsigned int defaultfg = 7;
-unsigned int defaultbg = 258;
+unsigned int defaultbg = 0;
static unsigned int defaultcs = 256;
static unsigned int defaultrcs = 257;
+unsigned int bg = 17, bgUnfocused = 16;
/*
* https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h4-Functions-using-CSI-_-ordered-by-the-final-character-lparen-s-rparen:CSI-Ps-SP-q.1D81
diff --git a/st.c b/st.c
@@ -196,7 +196,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
@@ -83,6 +83,7 @@ typedef union {
void die(const char *, ...);
void redraw(void);
+void tfulldirt(void);
void draw(void);
void printscreen(const Arg *);
@@ -135,5 +136,5 @@ extern char *termname;
extern unsigned int tabspaces;
extern unsigned int defaultfg;
extern unsigned int defaultbg;
-extern float alpha;
+extern float alpha, alphaUnfocused;
extern const int boxdraw, boxdraw_bold, boxdraw_braille;
diff --git 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 */
static int cursorblinks = 0;
@@ -801,34 +803,37 @@ xloadcolor(int i, const char *name, Color *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]);
- /* 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;
+ xloadalpha();
loaded = 1;
}
@@ -1995,12 +2000,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();
+ }
}
}
@@ -2374,6 +2389,7 @@ run:
config_init();
cols = MAX(cols, 1);
rows = MAX(rows, 1);
+ defaultbg = MAX(LEN(colorname), 256);
tnew(cols, rows);
xinit(cols, rows);
xsetenv();