st-alphaFocusHighlight-20191218-2b8333f.diff (8880B)
1 From 49fbc3f0a3d3524fa37ba4c0129425394b84feed Mon Sep 17 00:00:00 2001 2 From: Julius Huelsmann <juliusHuelsmann@gmail.com> 3 Date: Wed, 6 Nov 2019 21:59:28 +0100 4 Subject: [PATCH 1/2] [PATCH:FOCUS]: first version 5 6 --- 7 config.def.h | 7 ++++++- 8 config.mk | 2 +- 9 st.h | 2 ++ 10 x.c | 59 +++++++++++++++++++++++++++++++++++++++------------- 11 4 files changed, 54 insertions(+), 16 deletions(-) 12 13 diff --git a/config.def.h b/config.def.h 14 index 6ebea98..16f1ebd 100644 15 --- a/config.def.h 16 +++ b/config.def.h 17 @@ -82,6 +82,10 @@ char *termname = "st-256color"; 18 */ 19 unsigned int tabspaces = 8; 20 21 +/* bg opacity */ 22 +float alpha = 0.8; //< alpha value used when the window is focused. 23 +float alphaUnfocussed = 0.6; //< alpha value used when the focus is lost 24 + 25 /* Terminal colors (16 first used in escape sequence) */ 26 static const char *colorname[] = { 27 /* 8 normal colors */ 28 @@ -109,6 +113,7 @@ static const char *colorname[] = { 29 /* more colors can be added after 255 to use with DefaultXX */ 30 "#cccccc", 31 "#555555", 32 + "black", 33 }; 34 35 36 @@ -117,7 +122,7 @@ static const char *colorname[] = { 37 * foreground, background, cursor, reverse cursor 38 */ 39 unsigned int defaultfg = 7; 40 -unsigned int defaultbg = 0; 41 +unsigned int defaultbg = 258; 42 static unsigned int defaultcs = 256; 43 static unsigned int defaultrcs = 257; 44 45 diff --git a/config.mk b/config.mk 46 index 0cbb002..1d2f0e2 100644 47 --- a/config.mk 48 +++ b/config.mk 49 @@ -16,7 +16,7 @@ PKG_CONFIG = pkg-config 50 INCS = -I$(X11INC) \ 51 `$(PKG_CONFIG) --cflags fontconfig` \ 52 `$(PKG_CONFIG) --cflags freetype2` 53 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \ 54 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\ 55 `$(PKG_CONFIG) --libs fontconfig` \ 56 `$(PKG_CONFIG) --libs freetype2` 57 58 diff --git a/st.h b/st.h 59 index 4da3051..0bc69f8 100644 60 --- a/st.h 61 +++ b/st.h 62 @@ -120,3 +120,5 @@ extern char *termname; 63 extern unsigned int tabspaces; 64 extern unsigned int defaultfg; 65 extern unsigned int defaultbg; 66 +extern float alpha; 67 +extern float alphaUnfocussed; 68 diff --git a/x.c b/x.c 69 index 5828a3b..45bc960 100644 70 --- a/x.c 71 +++ b/x.c 72 @@ -4,6 +4,7 @@ 73 #include <limits.h> 74 #include <locale.h> 75 #include <signal.h> 76 +#include <stdbool.h> 77 #include <sys/select.h> 78 #include <time.h> 79 #include <unistd.h> 80 @@ -98,6 +99,7 @@ typedef struct { 81 XSetWindowAttributes attrs; 82 int scr; 83 int isfixed; /* is fixed geometry? */ 84 + int depth; /* bit depth */ 85 int l, t; /* left and top offset */ 86 int gm; /* geometry mask */ 87 } XWindow; 88 @@ -233,6 +235,7 @@ static char *usedfont = NULL; 89 static double usedfontsize = 0; 90 static double defaultfontsize = 0; 91 92 +static char *opt_alpha = NULL; 93 static char *opt_class = NULL; 94 static char **opt_cmd = NULL; 95 static char *opt_embed = NULL; 96 @@ -241,6 +244,7 @@ static char *opt_io = NULL; 97 static char *opt_line = NULL; 98 static char *opt_name = NULL; 99 static char *opt_title = NULL; 100 +static bool focused = true; 101 102 static int oldbutton = 3; /* button event on startup: 3 = release */ 103 104 @@ -692,7 +696,7 @@ xresize(int col, int row) 105 106 XFreePixmap(xw.dpy, xw.buf); 107 xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, 108 - DefaultDepth(xw.dpy, xw.scr)); 109 + xw.depth); 110 XftDrawChange(xw.draw, xw.buf); 111 xclear(0, 0, win.w, win.h); 112 113 @@ -752,6 +756,14 @@ xloadcols(void) 114 else 115 die("could not allocate color %d\n", i); 116 } 117 + 118 + /* set alpha value of bg color */ 119 + if (opt_alpha) 120 + alpha = strtof(opt_alpha, NULL); 121 + float const usedAlpha = focused ? alpha : alphaUnfocussed; 122 + dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha); 123 + dc.col[defaultbg].pixel &= 0x00FFFFFF; 124 + dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24; 125 loaded = 1; 126 } 127 128 @@ -1044,11 +1056,23 @@ xinit(int cols, int rows) 129 Window parent; 130 pid_t thispid = getpid(); 131 XColor xmousefg, xmousebg; 132 + XWindowAttributes attr; 133 + XVisualInfo vis; 134 135 if (!(xw.dpy = XOpenDisplay(NULL))) 136 die("can't open display\n"); 137 xw.scr = XDefaultScreen(xw.dpy); 138 - xw.vis = XDefaultVisual(xw.dpy, xw.scr); 139 + 140 + if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) { 141 + parent = XRootWindow(xw.dpy, xw.scr); 142 + xw.depth = 32; 143 + } else { 144 + XGetWindowAttributes(xw.dpy, parent, &attr); 145 + xw.depth = attr.depth; 146 + } 147 + 148 + XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis); 149 + xw.vis = vis.visual; 150 151 /* font */ 152 if (!FcInit()) 153 @@ -1058,7 +1082,7 @@ xinit(int cols, int rows) 154 xloadfonts(usedfont, 0); 155 156 /* colors */ 157 - xw.cmap = XDefaultColormap(xw.dpy, xw.scr); 158 + xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None); 159 xloadcols(); 160 161 /* adjust fixed window geometry */ 162 @@ -1078,19 +1102,15 @@ xinit(int cols, int rows) 163 | ButtonMotionMask | ButtonPressMask | ButtonReleaseMask; 164 xw.attrs.colormap = xw.cmap; 165 166 - if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) 167 - parent = XRootWindow(xw.dpy, xw.scr); 168 xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t, 169 - win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput, 170 + win.w, win.h, 0, xw.depth, InputOutput, 171 xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity 172 | CWEventMask | CWColormap, &xw.attrs); 173 174 memset(&gcvalues, 0, sizeof(gcvalues)); 175 gcvalues.graphics_exposures = False; 176 - dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures, 177 - &gcvalues); 178 - xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, 179 - DefaultDepth(xw.dpy, xw.scr)); 180 + xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth); 181 + dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues); 182 XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel); 183 XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h); 184 185 @@ -1663,13 +1683,21 @@ focus(XEvent *ev) 186 XSetICFocus(xw.xic); 187 win.mode |= MODE_FOCUSED; 188 xseturgency(0); 189 - if (IS_SET(MODE_FOCUS)) 190 - ttywrite("\033[I", 3, 0); 191 + if (IS_SET(MODE_FOCUS)) { ttywrite("\033[I", 3, 0); } 192 + if (!focused) { 193 + focused = true; 194 + xloadcols(); 195 + redraw(); 196 + } 197 } else { 198 XUnsetICFocus(xw.xic); 199 win.mode &= ~MODE_FOCUSED; 200 - if (IS_SET(MODE_FOCUS)) 201 - ttywrite("\033[O", 3, 0); 202 + if (IS_SET(MODE_FOCUS)) { ttywrite("\033[O", 3, 0); } 203 + if (focused) { 204 + focused = false; 205 + xloadcols(); 206 + redraw(); 207 + } 208 } 209 } 210 211 @@ -1925,6 +1953,9 @@ main(int argc, char *argv[]) 212 case 'a': 213 allowaltscreen = 0; 214 break; 215 + case 'A': 216 + opt_alpha = EARGF(usage()); 217 + break; 218 case 'c': 219 opt_class = EARGF(usage()); 220 break; 221 -- 222 2.24.1 223 224 225 From b442038e2f28b427f044a6bbd80481251ab532ad Mon Sep 17 00:00:00 2001 226 From: "gloom @ t440p" <gloom@t440p> 227 Date: Tue, 17 Dec 2019 18:43:31 +0800 228 Subject: [PATCH 2/2] merge: fix: do not reset terminal colors when 229 focus/unfocus 230 231 when using scripts which changes shell's 232 default ANSI colors like base16-shell 233 then focus/unfocus should not reset 234 foreground colors to default ones 235 --- 236 x.c | 34 ++++++++++++++++++++++------------ 237 1 file changed, 22 insertions(+), 12 deletions(-) 238 239 diff --git a/x.c b/x.c 240 index 45bc960..7482adb 100644 241 --- a/x.c 242 +++ b/x.c 243 @@ -734,6 +734,20 @@ xloadcolor(int i, const char *name, Color *ncolor) 244 return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor); 245 } 246 247 +void 248 +xloadalpha(void) 249 +{ 250 + /* set alpha value of bg color */ 251 + if (opt_alpha) 252 + alpha = strtof(opt_alpha, NULL); 253 + 254 + float const usedAlpha = focused ? alpha : alphaUnfocussed; 255 + 256 + dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha); 257 + dc.col[defaultbg].pixel &= 0x00FFFFFF; 258 + dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24; 259 +} 260 + 261 void 262 xloadcols(void) 263 { 264 @@ -749,7 +763,7 @@ xloadcols(void) 265 dc.col = xmalloc(dc.collen * sizeof(Color)); 266 } 267 268 - for (i = 0; i < dc.collen; i++) 269 + for (i = 0; i < dc.collen; i++) 270 if (!xloadcolor(i, NULL, &dc.col[i])) { 271 if (colorname[i]) 272 die("could not allocate color '%s'\n", colorname[i]); 273 @@ -757,13 +771,7 @@ xloadcols(void) 274 die("could not allocate color %d\n", i); 275 } 276 277 - /* set alpha value of bg color */ 278 - if (opt_alpha) 279 - alpha = strtof(opt_alpha, NULL); 280 - float const usedAlpha = focused ? alpha : alphaUnfocussed; 281 - dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha); 282 - dc.col[defaultbg].pixel &= 0x00FFFFFF; 283 - dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24; 284 + xloadalpha(); 285 loaded = 1; 286 } 287 288 @@ -1683,19 +1691,21 @@ focus(XEvent *ev) 289 XSetICFocus(xw.xic); 290 win.mode |= MODE_FOCUSED; 291 xseturgency(0); 292 - if (IS_SET(MODE_FOCUS)) { ttywrite("\033[I", 3, 0); } 293 + if (IS_SET(MODE_FOCUS)) 294 + ttywrite("\033[I", 3, 0); 295 if (!focused) { 296 focused = true; 297 - xloadcols(); 298 + xloadalpha(); 299 redraw(); 300 } 301 } else { 302 XUnsetICFocus(xw.xic); 303 win.mode &= ~MODE_FOCUSED; 304 - if (IS_SET(MODE_FOCUS)) { ttywrite("\033[O", 3, 0); } 305 + if (IS_SET(MODE_FOCUS)) 306 + ttywrite("\033[O", 3, 0); 307 if (focused) { 308 focused = false; 309 - xloadcols(); 310 + xloadalpha(); 311 redraw(); 312 } 313 } 314 -- 315 2.24.1 316