st-focus-20200530-patch_alpha.diff (3848B)
1 From c2c9e874fa069bc24df0982505788ae14c3024f3 Mon Sep 17 00:00:00 2001 2 From: Julius Huelsmann <juliusHuelsmann@gmail.com> 3 Date: Sat, 6 Jun 2020 13:12:28 +0200 4 Subject: [PATCH] patch: focus 5 6 --- 7 config.def.h | 5 +++-- 8 st.h | 2 +- 9 x.c | 44 ++++++++++++++++++++++++++++++-------------- 10 3 files changed, 34 insertions(+), 17 deletions(-) 11 12 diff --git a/config.def.h b/config.def.h 13 index b94b23c..577d1f1 100644 14 --- a/config.def.h 15 +++ b/config.def.h 16 @@ -85,7 +85,7 @@ char *termname = "st-256color"; 17 unsigned int tabspaces = 8; 18 19 /* bg opacity */ 20 -float alpha = 0.8; 21 +float alpha = 0.8, alphaUnfocused = 0.6; 22 23 /* Terminal colors (16 first used in escape sequence) */ 24 static const char *colorname[] = { 25 @@ -123,9 +123,10 @@ static const char *colorname[] = { 26 * foreground, background, cursor, reverse cursor 27 */ 28 unsigned int defaultfg = 7; 29 -unsigned int defaultbg = 258; 30 +unsigned int defaultbg = 0; 31 static unsigned int defaultcs = 256; 32 static unsigned int defaultrcs = 257; 33 +unsigned int bg = 17, bgUnfocused = 16; 34 35 /* 36 * Default shape of cursor 37 diff --git a/st.h b/st.h 38 index 2c656af..b5f1cf6 100644 39 --- a/st.h 40 +++ b/st.h 41 @@ -122,4 +122,4 @@ extern char *termname; 42 extern unsigned int tabspaces; 43 extern unsigned int defaultfg; 44 extern unsigned int defaultbg; 45 -extern float alpha; 46 +extern float alpha, alphaUnfocused; 47 diff --git a/x.c b/x.c 48 index 50da23c..a0c3223 100644 49 --- a/x.c 50 +++ b/x.c 51 @@ -254,6 +254,8 @@ static char *opt_line = NULL; 52 static char *opt_name = NULL; 53 static char *opt_title = NULL; 54 55 +static int focused = 0; 56 + 57 static int oldbutton = 3; /* button event on startup: 3 = release */ 58 59 void 60 @@ -774,35 +776,38 @@ xloadcolor(int i, const char *name, Color *ncolor) 61 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor); 62 } 63 64 +void 65 +xloadalpha(void) 66 +{ 67 + float const usedAlpha = focused ? alpha : alphaUnfocused; 68 + if (opt_alpha) alpha = strtof(opt_alpha, NULL); 69 + dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha); 70 + dc.col[defaultbg].pixel &= 0x00FFFFFF; 71 + dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24; 72 +} 73 + 74 void 75 xloadcols(void) 76 { 77 - int i; 78 static int loaded; 79 Color *cp; 80 81 - if (loaded) { 82 - for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp) 83 - XftColorFree(xw.dpy, xw.vis, xw.cmap, cp); 84 - } else { 85 - dc.collen = MAX(LEN(colorname), 256); 86 - dc.col = xmalloc(dc.collen * sizeof(Color)); 87 + if (!loaded) { 88 + dc.collen = 1 + (defaultbg = MAX(LEN(colorname), 256)); 89 + dc.col = xmalloc((dc.collen) * sizeof(Color)); 90 } 91 92 - for (i = 0; i < dc.collen; i++) 93 + for (int i = 0; i+1 < dc.collen; ++i) 94 if (!xloadcolor(i, NULL, &dc.col[i])) { 95 if (colorname[i]) 96 die("could not allocate color '%s'\n", colorname[i]); 97 else 98 die("could not allocate color %d\n", i); 99 } 100 + if (dc.collen) // cannot die, as the color is already loaded. 101 + xloadcolor(focused ?bg :bgUnfocused, NULL, &dc.col[defaultbg]); 102 103 - /* set alpha value of bg color */ 104 - if (opt_alpha) 105 - alpha = strtof(opt_alpha, NULL); 106 - dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha); 107 - dc.col[defaultbg].pixel &= 0x00FFFFFF; 108 - dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24; 109 + xloadalpha(); 110 loaded = 1; 111 } 112 113 @@ -1747,12 +1752,22 @@ focus(XEvent *ev) 114 xseturgency(0); 115 if (IS_SET(MODE_FOCUS)) 116 ttywrite("\033[I", 3, 0); 117 + if (!focused) { 118 + focused = 1; 119 + xloadcols(); 120 + redraw(); 121 + } 122 } else { 123 if (xw.ime.xic) 124 XUnsetICFocus(xw.ime.xic); 125 win.mode &= ~MODE_FOCUSED; 126 if (IS_SET(MODE_FOCUS)) 127 ttywrite("\033[O", 3, 0); 128 + if (focused) { 129 + focused = 0; 130 + xloadcols(); 131 + redraw(); 132 + } 133 } 134 } 135 136 @@ -2065,6 +2080,7 @@ run: 137 XSetLocaleModifiers(""); 138 cols = MAX(cols, 1); 139 rows = MAX(rows, 1); 140 + defaultbg = MAX(LEN(colorname), 256); 141 tnew(cols, rows); 142 xinit(cols, rows); 143 xsetenv(); 144 -- 145 2.27.0 146