sites

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

dwm-alpha-20230401-348f655.diff (9760B)


      1 From ad5887df95fda706291c81ee143d0786a1717b12 Mon Sep 17 00:00:00 2001
      2 From: getimiskon <getimiskon@disroot.org>
      3 Date: Sat, 1 Apr 2023 16:22:01 +0300
      4 Subject: [PATCH] Allow dwm to have translucent bars, while keeping all the
      5  text on it opaque, just like the alpha-patch for st. Updated for 348f655.
      6 
      7 ---
      8  config.def.h |  7 +++++++
      9  config.mk    |  2 +-
     10  drw.c        | 26 ++++++++++++-----------
     11  drw.h        |  9 +++++---
     12  dwm.c        | 59 ++++++++++++++++++++++++++++++++++++++++++++++------
     13  5 files changed, 81 insertions(+), 22 deletions(-)
     14 
     15 diff --git a/config.def.h b/config.def.h
     16 index 9efa774..8b3789a 100644
     17 --- a/config.def.h
     18 +++ b/config.def.h
     19 @@ -12,11 +12,18 @@ static const char col_gray2[]       = "#444444";
     20  static const char col_gray3[]       = "#bbbbbb";
     21  static const char col_gray4[]       = "#eeeeee";
     22  static const char col_cyan[]        = "#005577";
     23 +static const unsigned int baralpha = 0xd0;
     24 +static const unsigned int borderalpha = OPAQUE;
     25  static const char *colors[][3]      = {
     26  	/*               fg         bg         border   */
     27  	[SchemeNorm] = { col_gray3, col_gray1, col_gray2 },
     28  	[SchemeSel]  = { col_gray4, col_cyan,  col_cyan  },
     29  };
     30 +static const unsigned int alphas[][3]      = {
     31 +    /*               fg      bg        border*/
     32 +    [SchemeNorm] = { OPAQUE, baralpha, borderalpha },
     33 +	[SchemeSel]  = { OPAQUE, baralpha, borderalpha },
     34 +};
     35  
     36  /* tagging */
     37  static const char *tags[] = { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
     38 diff --git a/config.mk b/config.mk
     39 index ba64d3d..d609c42 100644
     40 --- a/config.mk
     41 +++ b/config.mk
     42 @@ -23,7 +23,7 @@ FREETYPEINC = /usr/include/freetype2
     43  
     44  # includes and libs
     45  INCS = -I${X11INC} -I${FREETYPEINC}
     46 -LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS}
     47 +LIBS = -L${X11LIB} -lX11 ${XINERAMALIBS} ${FREETYPELIBS} -lXrender
     48  
     49  # flags
     50  CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
     51 diff --git a/drw.c b/drw.c
     52 index a58a2b4..d18e8d8 100644
     53 --- a/drw.c
     54 +++ b/drw.c
     55 @@ -61,7 +61,7 @@ utf8decode(const char *c, long *u, size_t clen)
     56  }
     57  
     58  Drw *
     59 -drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
     60 +drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
     61  {
     62  	Drw *drw = ecalloc(1, sizeof(Drw));
     63  
     64 @@ -70,8 +70,11 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
     65  	drw->root = root;
     66  	drw->w = w;
     67  	drw->h = h;
     68 -	drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
     69 -	drw->gc = XCreateGC(dpy, root, 0, NULL);
     70 +	drw->visual = visual;
     71 +	drw->depth = depth;
     72 +	drw->cmap = cmap;
     73 +	drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
     74 +	drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
     75  	XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
     76  
     77  	return drw;
     78 @@ -87,7 +90,7 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h)
     79  	drw->h = h;
     80  	if (drw->drawable)
     81  		XFreePixmap(drw->dpy, drw->drawable);
     82 -	drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
     83 +	drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
     84  }
     85  
     86  void
     87 @@ -181,21 +184,22 @@ drw_fontset_free(Fnt *font)
     88  }
     89  
     90  void
     91 -drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
     92 +drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha)
     93  {
     94  	if (!drw || !dest || !clrname)
     95  		return;
     96  
     97 -	if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
     98 -	                       DefaultColormap(drw->dpy, drw->screen),
     99 +	if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
    100  	                       clrname, dest))
    101  		die("error, cannot allocate color '%s'", clrname);
    102 +
    103 +    dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24);
    104  }
    105  
    106  /* Wrapper to create color schemes. The caller has to call free(3) on the
    107   * returned color scheme when done using it. */
    108  Clr *
    109 -drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
    110 +drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount)
    111  {
    112  	size_t i;
    113  	Clr *ret;
    114 @@ -205,7 +209,7 @@ drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
    115  		return NULL;
    116  
    117  	for (i = 0; i < clrcount; i++)
    118 -		drw_clr_create(drw, &ret[i], clrnames[i]);
    119 +		drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
    120  	return ret;
    121  }
    122  
    123 @@ -263,9 +267,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
    124  	} else {
    125  		XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
    126  		XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
    127 -		d = XftDrawCreate(drw->dpy, drw->drawable,
    128 -		                  DefaultVisual(drw->dpy, drw->screen),
    129 -		                  DefaultColormap(drw->dpy, drw->screen));
    130 +		d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
    131  		x += lpad;
    132  		w -= lpad;
    133  	}
    134 diff --git a/drw.h b/drw.h
    135 index 6471431..2143533 100644
    136 --- a/drw.h
    137 +++ b/drw.h
    138 @@ -20,6 +20,9 @@ typedef struct {
    139  	Display *dpy;
    140  	int screen;
    141  	Window root;
    142 +    Visual *visual;
    143 +    unsigned int depth;
    144 +    Colormap cmap;
    145  	Drawable drawable;
    146  	GC gc;
    147  	Clr *scheme;
    148 @@ -27,7 +30,7 @@ typedef struct {
    149  } Drw;
    150  
    151  /* Drawable abstraction */
    152 -Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
    153 +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap);
    154  void drw_resize(Drw *drw, unsigned int w, unsigned int h);
    155  void drw_free(Drw *drw);
    156  
    157 @@ -39,8 +42,8 @@ unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int
    158  void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
    159  
    160  /* Colorscheme abstraction */
    161 -void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
    162 -Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
    163 +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha);
    164 +Clr *drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount);
    165  
    166  /* Cursor abstraction */
    167  Cur *drw_cur_create(Drw *drw, int shape);
    168 diff --git a/dwm.c b/dwm.c
    169 index c2bd871..3b34de8 100644
    170 --- a/dwm.c
    171 +++ b/dwm.c
    172 @@ -56,6 +56,7 @@
    173  #define HEIGHT(X)               ((X)->h + 2 * (X)->bw)
    174  #define TAGMASK                 ((1 << LENGTH(tags)) - 1)
    175  #define TEXTW(X)                (drw_fontset_getwidth(drw, (X)) + lrpad)
    176 +#define OPAQUE                  0xffU
    177  
    178  /* enums */
    179  enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */
    180 @@ -232,6 +233,7 @@ static Monitor *wintomon(Window w);
    181  static int xerror(Display *dpy, XErrorEvent *ee);
    182  static int xerrordummy(Display *dpy, XErrorEvent *ee);
    183  static int xerrorstart(Display *dpy, XErrorEvent *ee);
    184 +static void xinitvisual();
    185  static void zoom(const Arg *arg);
    186  
    187  /* variables */
    188 @@ -268,6 +270,11 @@ static Drw *drw;
    189  static Monitor *mons, *selmon;
    190  static Window root, wmcheckwin;
    191  
    192 +static int useargb = 0;
    193 +static Visual *visual;
    194 +static int depth;
    195 +static Colormap cmap;
    196 +
    197  /* configuration, allows nested code to access above variables */
    198  #include "config.h"
    199  
    200 @@ -1558,7 +1565,8 @@ setup(void)
    201  	sw = DisplayWidth(dpy, screen);
    202  	sh = DisplayHeight(dpy, screen);
    203  	root = RootWindow(dpy, screen);
    204 -	drw = drw_create(dpy, screen, root, sw, sh);
    205 +	xinitvisual();
    206 +	drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap);
    207  	if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
    208  		die("no fonts could be loaded.");
    209  	lrpad = drw->fonts->h;
    210 @@ -1586,7 +1594,7 @@ setup(void)
    211  	/* init appearance */
    212  	scheme = ecalloc(LENGTH(colors), sizeof(Clr *));
    213  	for (i = 0; i < LENGTH(colors); i++)
    214 -		scheme[i] = drw_scm_create(drw, colors[i], 3);
    215 +		scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3);
    216  	/* init bars */
    217  	updatebars();
    218  	updatestatus();
    219 @@ -1813,16 +1821,18 @@ updatebars(void)
    220  	Monitor *m;
    221  	XSetWindowAttributes wa = {
    222  		.override_redirect = True,
    223 -		.background_pixmap = ParentRelative,
    224 +		.background_pixel = 0,
    225 +		.border_pixel = 0,
    226 +		.colormap = cmap,
    227  		.event_mask = ButtonPressMask|ExposureMask
    228  	};
    229  	XClassHint ch = {"dwm", "dwm"};
    230  	for (m = mons; m; m = m->next) {
    231  		if (m->barwin)
    232  			continue;
    233 -		m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen),
    234 -				CopyFromParent, DefaultVisual(dpy, screen),
    235 -				CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa);
    236 +		m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth,
    237 +				InputOutput, visual,
    238 +				CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa);
    239  		XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor);
    240  		XMapRaised(dpy, m->barwin);
    241  		XSetClassHint(dpy, m->barwin, &ch);
    242 @@ -2120,6 +2130,43 @@ xerrorstart(Display *dpy, XErrorEvent *ee)
    243  	return -1;
    244  }
    245  
    246 +void
    247 +xinitvisual()
    248 +{
    249 +    XVisualInfo *infos;
    250 +	XRenderPictFormat *fmt;
    251 +	int nitems;
    252 +	int i;
    253 +
    254 +	XVisualInfo tpl = {
    255 +        .screen = screen,
    256 +		.depth = 32,
    257 +		.class = TrueColor
    258 +	};
    259 +	long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
    260 +
    261 +	infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
    262 +	visual = NULL;
    263 +	for(i = 0; i < nitems; i ++) {
    264 +        fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
    265 +		if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
    266 +            visual = infos[i].visual;
    267 +			depth = infos[i].depth;
    268 +			cmap = XCreateColormap(dpy, root, visual, AllocNone);
    269 +			useargb = 1;
    270 +			break;
    271 +        }
    272 +    }
    273 +
    274 +	XFree(infos);
    275 +
    276 +	if (! visual) {
    277 +        visual = DefaultVisual(dpy, screen);
    278 +		depth = DefaultDepth(dpy, screen);
    279 +		cmap = DefaultColormap(dpy, screen);
    280 +    }
    281 +}
    282 +
    283  void
    284  zoom(const Arg *arg)
    285  {
    286 -- 
    287 2.40.0
    288