sites

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

st-focus-20200530-43a395a.diff (6927B)


      1 From dfbb24bf5e02c9e365d4c0fe46aa8cbe27aed92f 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  config.mk    |  2 +-
      9  st.h         |  1 +
     10  x.c          | 72 +++++++++++++++++++++++++++++++++++++++-------------
     11  4 files changed, 61 insertions(+), 19 deletions(-)
     12 
     13 diff --git a/config.def.h b/config.def.h
     14 index 0895a1f..577d1f1 100644
     15 --- a/config.def.h
     16 +++ b/config.def.h
     17 @@ -84,6 +84,9 @@ char *termname = "st-256color";
     18   */
     19  unsigned int tabspaces = 8;
     20  
     21 +/* bg opacity */
     22 +float alpha = 0.8, alphaUnfocused = 0.6;
     23 +
     24  /* Terminal colors (16 first used in escape sequence) */
     25  static const char *colorname[] = {
     26  	/* 8 normal colors */
     27 @@ -111,6 +114,7 @@ static const char *colorname[] = {
     28  	/* more colors can be added after 255 to use with DefaultXX */
     29  	"#cccccc",
     30  	"#555555",
     31 +	"black",
     32  };
     33  
     34  
     35 @@ -122,6 +126,7 @@ unsigned int defaultfg = 7;
     36  unsigned int defaultbg = 0;
     37  static unsigned int defaultcs = 256;
     38  static unsigned int defaultrcs = 257;
     39 +unsigned int bg = 17, bgUnfocused = 16;
     40  
     41  /*
     42   * Default shape of cursor
     43 diff --git a/config.mk b/config.mk
     44 index beafc35..ddc65ae 100644
     45 --- a/config.mk
     46 +++ b/config.mk
     47 @@ -16,7 +16,7 @@ PKG_CONFIG = pkg-config
     48  INCS = -I$(X11INC) \
     49         `$(PKG_CONFIG) --cflags fontconfig` \
     50         `$(PKG_CONFIG) --cflags freetype2`
     51 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
     52 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\
     53         `$(PKG_CONFIG) --libs fontconfig` \
     54         `$(PKG_CONFIG) --libs freetype2`
     55  
     56 diff --git a/st.h b/st.h
     57 index d978458..b5f1cf6 100644
     58 --- a/st.h
     59 +++ b/st.h
     60 @@ -122,3 +122,4 @@ extern char *termname;
     61  extern unsigned int tabspaces;
     62  extern unsigned int defaultfg;
     63  extern unsigned int defaultbg;
     64 +extern float alpha, alphaUnfocused;
     65 diff --git a/x.c b/x.c
     66 index e5f1737..a0c3223 100644
     67 --- a/x.c
     68 +++ b/x.c
     69 @@ -105,6 +105,7 @@ typedef struct {
     70  	XSetWindowAttributes attrs;
     71  	int scr;
     72  	int isfixed; /* is fixed geometry? */
     73 +	int depth; /* bit depth */
     74  	int l, t; /* left and top offset */
     75  	int gm; /* geometry mask */
     76  } XWindow;
     77 @@ -243,6 +244,7 @@ static char *usedfont = NULL;
     78  static double usedfontsize = 0;
     79  static double defaultfontsize = 0;
     80  
     81 +static char *opt_alpha = NULL;
     82  static char *opt_class = NULL;
     83  static char **opt_cmd  = NULL;
     84  static char *opt_embed = NULL;
     85 @@ -252,6 +254,8 @@ static char *opt_line  = NULL;
     86  static char *opt_name  = NULL;
     87  static char *opt_title = NULL;
     88  
     89 +static int focused = 0;
     90 +
     91  static int oldbutton = 3; /* button event on startup: 3 = release */
     92  
     93  void
     94 @@ -734,7 +738,7 @@ xresize(int col, int row)
     95  
     96  	XFreePixmap(xw.dpy, xw.buf);
     97  	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
     98 -			DefaultDepth(xw.dpy, xw.scr));
     99 +			xw.depth);
    100  	XftDrawChange(xw.draw, xw.buf);
    101  	xclear(0, 0, win.w, win.h);
    102  
    103 @@ -772,28 +776,38 @@ xloadcolor(int i, const char *name, Color *ncolor)
    104  	return XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, ncolor);
    105  }
    106  
    107 +void
    108 +xloadalpha(void)
    109 +{
    110 +	float const usedAlpha = focused ? alpha : alphaUnfocused;
    111 +	if (opt_alpha) alpha = strtof(opt_alpha, NULL);
    112 +	dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * usedAlpha);
    113 +	dc.col[defaultbg].pixel &= 0x00FFFFFF;
    114 +	dc.col[defaultbg].pixel |= (unsigned char)(0xff * usedAlpha) << 24;
    115 +}
    116 +
    117  void
    118  xloadcols(void)
    119  {
    120 -	int i;
    121  	static int loaded;
    122  	Color *cp;
    123  
    124 -	if (loaded) {
    125 -		for (cp = dc.col; cp < &dc.col[dc.collen]; ++cp)
    126 -			XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
    127 -	} else {
    128 -		dc.collen = MAX(LEN(colorname), 256);
    129 -		dc.col = xmalloc(dc.collen * sizeof(Color));
    130 +	if (!loaded) {
    131 +		dc.collen = 1 + (defaultbg = MAX(LEN(colorname), 256));
    132 +		dc.col = xmalloc((dc.collen) * sizeof(Color));
    133  	}
    134  
    135 -	for (i = 0; i < dc.collen; i++)
    136 +	for (int i = 0; i+1 < dc.collen; ++i)
    137  		if (!xloadcolor(i, NULL, &dc.col[i])) {
    138  			if (colorname[i])
    139  				die("could not allocate color '%s'\n", colorname[i]);
    140  			else
    141  				die("could not allocate color %d\n", i);
    142  		}
    143 +	if (dc.collen) // cannot die, as the color is already loaded.
    144 +		xloadcolor(focused ?bg :bgUnfocused, NULL, &dc.col[defaultbg]);
    145 +
    146 +	xloadalpha();
    147  	loaded = 1;
    148  }
    149  
    150 @@ -1103,11 +1117,23 @@ xinit(int cols, int rows)
    151  	Window parent;
    152  	pid_t thispid = getpid();
    153  	XColor xmousefg, xmousebg;
    154 +	XWindowAttributes attr;
    155 +	XVisualInfo vis;
    156  
    157  	if (!(xw.dpy = XOpenDisplay(NULL)))
    158  		die("can't open display\n");
    159  	xw.scr = XDefaultScreen(xw.dpy);
    160 -	xw.vis = XDefaultVisual(xw.dpy, xw.scr);
    161 +
    162 +	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
    163 +		parent = XRootWindow(xw.dpy, xw.scr);
    164 +		xw.depth = 32;
    165 +	} else {
    166 +		XGetWindowAttributes(xw.dpy, parent, &attr);
    167 +		xw.depth = attr.depth;
    168 +	}
    169 +
    170 +	XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
    171 +	xw.vis = vis.visual;
    172  
    173  	/* font */
    174  	if (!FcInit())
    175 @@ -1117,7 +1143,7 @@ xinit(int cols, int rows)
    176  	xloadfonts(usedfont, 0);
    177  
    178  	/* colors */
    179 -	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
    180 +	xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
    181  	xloadcols();
    182  
    183  	/* adjust fixed window geometry */
    184 @@ -1137,19 +1163,15 @@ xinit(int cols, int rows)
    185  		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
    186  	xw.attrs.colormap = xw.cmap;
    187  
    188 -	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
    189 -		parent = XRootWindow(xw.dpy, xw.scr);
    190  	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
    191 -			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
    192 +			win.w, win.h, 0, xw.depth, InputOutput,
    193  			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
    194  			| CWEventMask | CWColormap, &xw.attrs);
    195  
    196  	memset(&gcvalues, 0, sizeof(gcvalues));
    197  	gcvalues.graphics_exposures = False;
    198 -	dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
    199 -			&gcvalues);
    200 -	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    201 -			DefaultDepth(xw.dpy, xw.scr));
    202 +	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
    203 +	dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
    204  	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
    205  	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
    206  
    207 @@ -1730,12 +1752,22 @@ focus(XEvent *ev)
    208  		xseturgency(0);
    209  		if (IS_SET(MODE_FOCUS))
    210  			ttywrite("\033[I", 3, 0);
    211 +		if (!focused) {
    212 +			focused = 1;
    213 +			xloadcols();
    214 +			redraw();
    215 +		}
    216  	} else {
    217  		if (xw.ime.xic)
    218  			XUnsetICFocus(xw.ime.xic);
    219  		win.mode &= ~MODE_FOCUSED;
    220  		if (IS_SET(MODE_FOCUS))
    221  			ttywrite("\033[O", 3, 0);
    222 +		if (focused) {
    223 +			focused = 0;
    224 +			xloadcols();
    225 +			redraw();
    226 +		}
    227  	}
    228  }
    229  
    230 @@ -1994,6 +2026,9 @@ main(int argc, char *argv[])
    231  	case 'a':
    232  		allowaltscreen = 0;
    233  		break;
    234 +	case 'A':
    235 +		opt_alpha = EARGF(usage());
    236 +		break;
    237  	case 'c':
    238  		opt_class = EARGF(usage());
    239  		break;
    240 @@ -2045,6 +2080,7 @@ run:
    241  	XSetLocaleModifiers("");
    242  	cols = MAX(cols, 1);
    243  	rows = MAX(rows, 1);
    244 +	defaultbg = MAX(LEN(colorname), 256);
    245  	tnew(cols, rows);
    246  	xinit(cols, rows);
    247  	xsetenv();
    248 -- 
    249 2.27.0
    250