sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-alphaFocusHighlight-20200216-26cdfeb.diff (6510B)


      1 From 7538b0b641fa8291b98f65ecc5d140e0e9cc0028 Mon Sep 17 00:00:00 2001
      2 From: Julius Huelsmann <juliusHuelsmann@gmail.com>
      3 Date: Sun, 16 Feb 2020 18:36:56 +0100
      4 Subject: [PATCH] alpha_focus: port patch
      5 
      6 ---
      7  config.def.h |  7 +++++-
      8  config.mk    |  2 +-
      9  st.h         |  2 ++
     10  x.c          | 61 +++++++++++++++++++++++++++++++++++++++++++---------
     11  4 files changed, 60 insertions(+), 12 deletions(-)
     12 
     13 diff --git a/config.def.h b/config.def.h
     14 index 546edda..6c6b928 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 a1928ca..2009c33 100644
     60 --- a/st.h
     61 +++ b/st.h
     62 @@ -121,3 +121,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 1f62129..6d6751d 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 @@ -105,6 +106,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 @@ -242,6 +244,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 @@ -250,6 +253,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 @@ -719,7 +723,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 @@ -757,6 +761,20 @@ xloadcolor(int i, const char *name, Color *ncolor)
    114  	return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
    115  }
    116  
    117 +void
    118 +xloadalpha(void)
    119 +{
    120 +    /* set alpha value of bg color */
    121 +    if (opt_alpha)
    122 +        alpha = strtof(opt_alpha, NULL);
    123 +
    124 +    float const usedAlpha = focused ? alpha : alphaUnfocussed;
    125 +
    126 +    dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha);
    127 +    dc.col[defaultbg].pixel &= 0x00FFFFFF;
    128 +    dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24;
    129 +}
    130 +
    131  void
    132  xloadcols(void)
    133  {
    134 @@ -779,6 +797,8 @@ xloadcols(void)
    135  			else
    136  				die("could not allocate color %d\n", i);
    137  		}
    138 +
    139 +	xloadalpha();
    140  	loaded = 1;
    141  }
    142  
    143 @@ -1089,11 +1109,23 @@ xinit(int cols, int rows)
    144  	Window parent;
    145  	pid_t thispid = getpid();
    146  	XColor xmousefg, xmousebg;
    147 +	XWindowAttributes attr;
    148 +	XVisualInfo vis;
    149  
    150  	if (!(xw.dpy = XOpenDisplay(NULL)))
    151  		die("can't open display\n");
    152  	xw.scr = XDefaultScreen(xw.dpy);
    153 -	xw.vis = XDefaultVisual(xw.dpy, xw.scr);
    154 +
    155 +	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
    156 +		parent = XRootWindow(xw.dpy, xw.scr);
    157 +		xw.depth = 32;
    158 +	} else {
    159 +		XGetWindowAttributes(xw.dpy, parent, &attr);
    160 +		xw.depth = attr.depth;
    161 +	}
    162 +
    163 +	XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
    164 +	xw.vis = vis.visual;
    165  
    166  	/* font */
    167  	if (!FcInit())
    168 @@ -1103,7 +1135,7 @@ xinit(int cols, int rows)
    169  	xloadfonts(usedfont, 0);
    170  
    171  	/* colors */
    172 -	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
    173 +	xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
    174  	xloadcols();
    175  
    176  	/* adjust fixed window geometry */
    177 @@ -1123,19 +1155,15 @@ xinit(int cols, int rows)
    178  		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
    179  	xw.attrs.colormap = xw.cmap;
    180  
    181 -	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
    182 -		parent = XRootWindow(xw.dpy, xw.scr);
    183  	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
    184 -			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
    185 +			win.w, win.h, 0, xw.depth, InputOutput,
    186  			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
    187  			| CWEventMask | CWColormap, &xw.attrs);
    188  
    189  	memset(&gcvalues, 0, sizeof(gcvalues));
    190  	gcvalues.graphics_exposures = False;
    191 -	dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
    192 -			&gcvalues);
    193 -	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    194 -			DefaultDepth(xw.dpy, xw.scr));
    195 +	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
    196 +	dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
    197  	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
    198  	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
    199  
    200 @@ -1716,12 +1744,22 @@ focus(XEvent *ev)
    201  		xseturgency(0);
    202  		if (IS_SET(MODE_FOCUS))
    203  			ttywrite("\033[I", 3, 0);
    204 +		if (!focused) {
    205 +			focused = true;
    206 +			xloadalpha();
    207 +			redraw();
    208 +		}
    209  	} else {
    210  		if (xw.ime.xic)
    211  			XUnsetICFocus(xw.ime.xic);
    212  		win.mode &= ~MODE_FOCUSED;
    213  		if (IS_SET(MODE_FOCUS))
    214  			ttywrite("\033[O", 3, 0);
    215 +		if (focused) {
    216 +			focused = false;
    217 +			xloadalpha();
    218 +			redraw();
    219 +		}
    220  	}
    221  }
    222  
    223 @@ -1980,6 +2018,9 @@ main(int argc, char *argv[])
    224  	case 'a':
    225  		allowaltscreen = 0;
    226  		break;
    227 +	case 'A':
    228 +		opt_alpha = EARGF(usage());
    229 +		break;
    230  	case 'c':
    231  		opt_class = EARGF(usage());
    232  		break;
    233 -- 
    234 2.25.0
    235