sites

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

st-alpha-changealpha-20230519-b44f2ad.diff (6001B)


      1 diff --git a/st/config.def.h b/st_patched/config.def.h
      2 index 91ab8ca..59fc2a9 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -93,6 +93,10 @@ char *termname = "st-256color";
      6   */
      7  unsigned int tabspaces = 8;
      8  
      9 +/* bg opacity */
     10 +float alpha = 0.8;
     11 +float alpha_def;
     12 +
     13  /* Terminal colors (16 first used in escape sequence) */
     14  static const char *colorname[] = {
     15  	/* 8 normal colors */
     16 @@ -201,6 +205,9 @@ static Shortcut shortcuts[] = {
     17  	{ TERMMOD,              XK_Y,           selpaste,       {.i =  0} },
     18  	{ ShiftMask,            XK_Insert,      selpaste,       {.i =  0} },
     19  	{ TERMMOD,              XK_Num_Lock,    numlock,        {.i =  0} },
     20 +	{ MODKEY,               XK_bracketleft, chgalpha,       {.f = -1} }, /* Decrease opacity */
     21 +	{ MODKEY|ShiftMask,     XK_braceright,  chgalpha,       {.f = +1} }, /* Increase opacity */
     22 +	{ MODKEY,               XK_bracketright,chgalpha,       {.f =  0} }, /* Reset opacity */
     23  };
     24  
     25  /*
     26 diff --git a/st/st.h b/st_patched/st.h
     27 index fd3b0d8..cda8c13 100644
     28 --- a/st.h
     29 +++ b/st.h
     30 @@ -124,3 +124,4 @@ extern unsigned int tabspaces;
     31  extern unsigned int defaultfg;
     32  extern unsigned int defaultbg;
     33  extern unsigned int defaultcs;
     34 +extern float alpha, alpha_def;
     35 diff --git a/st/config.mk b/st_patched/config.mk
     36 index 1e306f8..47c615e 100644
     37 --- a/config.mk
     38 +++ b/config.mk
     39 @@ -16,7 +16,7 @@ PKG_CONFIG = pkg-config
     40  INCS = -I$(X11INC) \
     41         `$(PKG_CONFIG) --cflags fontconfig` \
     42         `$(PKG_CONFIG) --cflags freetype2`
     43 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
     44 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lXrender\
     45         `$(PKG_CONFIG) --libs fontconfig` \
     46         `$(PKG_CONFIG) --libs freetype2`
     47  
     48 diff --git a/st/x.c b/st_patched/x.c
     49 index aa09997..3b05a55 100644
     50 --- a/x.c
     51 +++ b/x.c
     52 @@ -59,6 +59,7 @@ static void zoom(const Arg *);
     53  static void zoomabs(const Arg *);
     54  static void zoomreset(const Arg *);
     55  static void ttysend(const Arg *);
     56 +static void chgalpha(const Arg *);
     57  
     58  /* config.h for applying patches and the configuration. */
     59  #include "config.h"
     60 @@ -105,6 +106,7 @@ typedef struct {
     61  	XSetWindowAttributes attrs;
     62  	int scr;
     63  	int isfixed; /* is fixed geometry? */
     64 +	int depth; /* bit depth */
     65  	int l, t; /* left and top offset */
     66  	int gm; /* geometry mask */
     67  } XWindow;
     68 @@ -243,6 +245,7 @@ static char *usedfont = NULL;
     69  static double usedfontsize = 0;
     70  static double defaultfontsize = 0;
     71  
     72 +static char *opt_alpha = NULL;
     73  static char *opt_class = NULL;
     74  static char **opt_cmd  = NULL;
     75  static char *opt_embed = NULL;
     76 @@ -752,7 +755,7 @@ xresize(int col, int row)
     77  
     78  	XFreePixmap(xw.dpy, xw.buf);
     79  	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
     80 -			DefaultDepth(xw.dpy, xw.scr));
     81 +			xw.depth);
     82  	XftDrawChange(xw.draw, xw.buf);
     83  	xclear(0, 0, win.w, win.h);
     84  
     85 @@ -812,6 +815,13 @@ xloadcols(void)
     86  			else
     87  				die("could not allocate color %d\n", i);
     88  		}
     89 +
     90 +	/* set alpha value of bg color */
     91 +	if (opt_alpha)
     92 +		alpha = strtof(opt_alpha, NULL);
     93 +	dc.col[defaultbg].color.alpha = (unsigned short)(0xffff * alpha);
     94 +	dc.col[defaultbg].pixel &= 0x00FFFFFF;
     95 +	dc.col[defaultbg].pixel |= (unsigned char)(0xff * alpha) << 24;
     96  	loaded = 1;
     97  }
     98  
     99 @@ -1134,11 +1144,23 @@ xinit(int cols, int rows)
    100  	Window parent;
    101  	pid_t thispid = getpid();
    102  	XColor xmousefg, xmousebg;
    103 +	XWindowAttributes attr;
    104 +	XVisualInfo vis;
    105  
    106  	if (!(xw.dpy = XOpenDisplay(NULL)))
    107  		die("can't open display\n");
    108  	xw.scr = XDefaultScreen(xw.dpy);
    109 -	xw.vis = XDefaultVisual(xw.dpy, xw.scr);
    110 +
    111 +	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0)))) {
    112 +		parent = XRootWindow(xw.dpy, xw.scr);
    113 +		xw.depth = 32;
    114 +	} else {
    115 +		XGetWindowAttributes(xw.dpy, parent, &attr);
    116 +		xw.depth = attr.depth;
    117 +	}
    118 +
    119 +	XMatchVisualInfo(xw.dpy, xw.scr, xw.depth, TrueColor, &vis);
    120 +	xw.vis = vis.visual;
    121  
    122  	/* font */
    123  	if (!FcInit())
    124 @@ -1147,8 +1169,11 @@ xinit(int cols, int rows)
    125  	usedfont = (opt_font == NULL)? font : opt_font;
    126  	xloadfonts(usedfont, 0);
    127  
    128 +   /* Backup default alpha value */
    129 +   alpha_def = alpha;
    130 +
    131  	/* colors */
    132 -	xw.cmap = XDefaultColormap(xw.dpy, xw.scr);
    133 +	xw.cmap = XCreateColormap(xw.dpy, parent, xw.vis, None);
    134  	xloadcols();
    135  
    136  	/* adjust fixed window geometry */
    137 @@ -1168,19 +1193,15 @@ xinit(int cols, int rows)
    138  		| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
    139  	xw.attrs.colormap = xw.cmap;
    140  
    141 -	if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
    142 -		parent = XRootWindow(xw.dpy, xw.scr);
    143  	xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
    144 -			win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
    145 +			win.w, win.h, 0, xw.depth, InputOutput,
    146  			xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
    147  			| CWEventMask | CWColormap, &xw.attrs);
    148  
    149  	memset(&gcvalues, 0, sizeof(gcvalues));
    150  	gcvalues.graphics_exposures = False;
    151 -	dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
    152 -			&gcvalues);
    153 -	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
    154 -			DefaultDepth(xw.dpy, xw.scr));
    155 +	xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h, xw.depth);
    156 +	dc.gc = XCreateGC(xw.dpy, xw.buf, GCGraphicsExposures, &gcvalues);
    157  	XSetForeground(xw.dpy, dc.gc, dc.col[defaultbg].pixel);
    158  	XFillRectangle(xw.dpy, xw.buf, dc.gc, 0, 0, win.w, win.h);
    159  
    160 @@ -1371,6 +1392,24 @@ xmakeglyphfontspecs(XftGlyphFontSpec *specs, const Glyph *glyphs, int len, int x
    161  	return numspecs;
    162  }
    163  
    164 +void
    165 +chgalpha(const Arg *arg)
    166 +{
    167 +   if (arg->f == -1.0f && alpha >= 0.1f)
    168 +      alpha -= 0.1f;
    169 +   else if (arg->f == 1.0f && alpha < 1.0f)
    170 +      alpha += 0.1f;
    171 +   else if (arg->f == 0.0f)
    172 +      alpha = alpha_def;
    173 +   else
    174 +      return;
    175 +
    176 +   dc.col[defaultbg].color.alpha = (unsigned short)(0xFFFF * alpha);
    177 +   /* Required to remove artifacting from borderpx */
    178 +   cresize(0, 0);
    179 +   redraw();
    180 +}
    181 +
    182  void
    183  xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, int y)
    184  {
    185 @@ -2038,6 +2077,9 @@ main(int argc, char *argv[])
    186  	case 'a':
    187  		allowaltscreen = 0;
    188  		break;
    189 +	case 'A':
    190 +		opt_alpha = EARGF(usage());
    191 +		break;
    192  	case 'c':
    193  		opt_class = EARGF(usage());
    194  		break;