dwm-alpha-20201019-61bb8b2.diff (9738B)
1 From 51f9c34480b984e261a738e5295f518b42c2f29c Mon Sep 17 00:00:00 2001 2 From: Petrus Karell <pk@petruskarell.fi> 3 Date: Mon, 19 Oct 2020 19:15:24 +0300 4 Subject: [PATCH] Allow dwm to have translucent bars, while keeping all 5 the text on it opaque, just like the alpha-patch for st. Updated for 61bb8b2 6 7 --- 8 config.def.h | 7 ++++++ 9 config.mk | 2 +- 10 drw.c | 26 ++++++++++++----------- 11 drw.h | 9 +++++--- 12 dwm.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++------ 13 5 files changed, 82 insertions(+), 22 deletions(-) 14 15 diff --git a/config.def.h b/config.def.h 16 index 1c0b587..4f68fe8 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 7084c33..21b5404 100644 40 --- a/config.mk 41 +++ b/config.mk 42 @@ -22,7 +22,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_POSIX_C_SOURCE=200809L -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS} 51 diff --git a/drw.c b/drw.c 52 index 4cdbcbe..fe3aadd 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 @@ -194,21 +197,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 @@ -218,7 +222,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 @@ -274,9 +278,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 4bcd5ad..a56f523 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 @@ -38,8 +41,8 @@ unsigned int drw_fontset_getwidth(Drw *drw, const char *text); 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 664c527..d8005c7 100644 170 --- a/dwm.c 171 +++ b/dwm.c 172 @@ -57,6 +57,8 @@ 173 #define TAGMASK ((1 << LENGTH(tags)) - 1) 174 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 175 176 +#define OPAQUE 0xffU 177 + 178 /* enums */ 179 enum { CurNormal, CurResize, CurMove, CurLast }; /* cursor */ 180 enum { SchemeNorm, SchemeSel }; /* color schemes */ 181 @@ -233,6 +235,7 @@ static Monitor *wintomon(Window w); 182 static int xerror(Display *dpy, XErrorEvent *ee); 183 static int xerrordummy(Display *dpy, XErrorEvent *ee); 184 static int xerrorstart(Display *dpy, XErrorEvent *ee); 185 +static void xinitvisual(); 186 static void zoom(const Arg *arg); 187 188 /* variables */ 189 @@ -269,6 +272,11 @@ static Drw *drw; 190 static Monitor *mons, *selmon; 191 static Window root, wmcheckwin; 192 193 +static int useargb = 0; 194 +static Visual *visual; 195 +static int depth; 196 +static Colormap cmap; 197 + 198 /* configuration, allows nested code to access above variables */ 199 #include "config.h" 200 201 @@ -1542,7 +1550,8 @@ setup(void) 202 sw = DisplayWidth(dpy, screen); 203 sh = DisplayHeight(dpy, screen); 204 root = RootWindow(dpy, screen); 205 - drw = drw_create(dpy, screen, root, sw, sh); 206 + xinitvisual(); 207 + drw = drw_create(dpy, screen, root, sw, sh, visual, depth, cmap); 208 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 209 die("no fonts could be loaded."); 210 lrpad = drw->fonts->h; 211 @@ -1570,7 +1579,7 @@ setup(void) 212 /* init appearance */ 213 scheme = ecalloc(LENGTH(colors), sizeof(Clr *)); 214 for (i = 0; i < LENGTH(colors); i++) 215 - scheme[i] = drw_scm_create(drw, colors[i], 3); 216 + scheme[i] = drw_scm_create(drw, colors[i], alphas[i], 3); 217 /* init bars */ 218 updatebars(); 219 updatestatus(); 220 @@ -1807,16 +1816,18 @@ updatebars(void) 221 Monitor *m; 222 XSetWindowAttributes wa = { 223 .override_redirect = True, 224 - .background_pixmap = ParentRelative, 225 + .background_pixel = 0, 226 + .border_pixel = 0, 227 + .colormap = cmap, 228 .event_mask = ButtonPressMask|ExposureMask 229 }; 230 XClassHint ch = {"dwm", "dwm"}; 231 for (m = mons; m; m = m->next) { 232 if (m->barwin) 233 continue; 234 - m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, DefaultDepth(dpy, screen), 235 - CopyFromParent, DefaultVisual(dpy, screen), 236 - CWOverrideRedirect|CWBackPixmap|CWEventMask, &wa); 237 + m->barwin = XCreateWindow(dpy, root, m->wx, m->by, m->ww, bh, 0, depth, 238 + InputOutput, visual, 239 + CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &wa); 240 XDefineCursor(dpy, m->barwin, cursor[CurNormal]->cursor); 241 XMapRaised(dpy, m->barwin); 242 XSetClassHint(dpy, m->barwin, &ch); 243 @@ -2113,6 +2124,43 @@ xerrorstart(Display *dpy, XErrorEvent *ee) 244 return -1; 245 } 246 247 +void 248 +xinitvisual() 249 +{ 250 + XVisualInfo *infos; 251 + XRenderPictFormat *fmt; 252 + int nitems; 253 + int i; 254 + 255 + XVisualInfo tpl = { 256 + .screen = screen, 257 + .depth = 32, 258 + .class = TrueColor 259 + }; 260 + long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; 261 + 262 + infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); 263 + visual = NULL; 264 + for(i = 0; i < nitems; i ++) { 265 + fmt = XRenderFindVisualFormat(dpy, infos[i].visual); 266 + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 267 + visual = infos[i].visual; 268 + depth = infos[i].depth; 269 + cmap = XCreateColormap(dpy, root, visual, AllocNone); 270 + useargb = 1; 271 + break; 272 + } 273 + } 274 + 275 + XFree(infos); 276 + 277 + if (! visual) { 278 + visual = DefaultVisual(dpy, screen); 279 + depth = DefaultDepth(dpy, screen); 280 + cmap = DefaultColormap(dpy, screen); 281 + } 282 +} 283 + 284 void 285 zoom(const Arg *arg) 286 { 287 -- 288 2.28.0 289