From e9cfb59a5d7cea3438045c47e9b2b820cd3173e9 Mon Sep 17 00:00:00 2001 From: bakkeby Date: Fri, 29 May 2020 17:01:40 +0200 Subject: [PATCH] Adding alpha patch --- README.md | 9 +++-- config.def.h | 14 ++++++++ config.mk | 5 ++- dmenu.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++--- drw.c | 40 ++++++++++++++++++++++ drw.h | 14 ++++++++ patches.def.h | 6 ++++ 7 files changed, 174 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ba9564a..4351a3f 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ Similar to [dwm-flexipatch](https://github.com/bakkeby/dwm-flexipatch) this dmenu 4.9 (750b30, 2019-03-03) project has a different take on patching. It uses preprocessor directives to decide whether or not to include a patch during build time. Essentially this means that this build, for better or worse, contains both the patched _and_ the original code. The aim being that you can select which patches to include and the build will contain that code and nothing more. -For example to include the `border` patch then you would only need to flip this setting from 0 to 1 in [patches.h](https://github.com/bakkeby/dmenu-flexipatch/blob/master/patches.def.h): +For example to include the `alpha` patch then you would only need to flip this setting from 0 to 1 in [patches.h](https://github.com/bakkeby/dmenu-flexipatch/blob/master/patches.def.h): ```c -#define BORDER_PATCH 1 +#define ALPHA_PATCH 1 ``` Once you have found out what works for you and what doesn't then you should be in a better position to choose patches should you want to start patching from scratch. @@ -15,6 +15,8 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) ### Changelog: +2020-05-29 - Added the alpha patch + 2020-04-05 - Added fuzzyhighlight patch 2020-02-09 - Added revised border patch (adding command line parameter for setting border width) @@ -27,6 +29,9 @@ Refer to [https://tools.suckless.org/dmenu/](https://tools.suckless.org/dmenu/) ### Patches included: + - [alpha](https://github.com/bakkeby/patches/blob/master/dmenu/dmenu-alpha-4.9_20190303_190303.diff) + - adds transparency for the dmenu window + - [border](http://tools.suckless.org/dmenu/patches/border/) - adds a border around the dmenu window diff --git a/config.def.h b/config.def.h index 4c9b0d1..02b88ab 100644 --- a/config.def.h +++ b/config.def.h @@ -2,6 +2,9 @@ /* Default settings; can be overriden by command line. */ static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ +#if ALPHA_PATCH +static double opacity = 1.0; /* -o option; defines alpha translucency */ +#endif // ALPHA_PATCH #if FUZZYMATCH_PATCH static int fuzzy = 1; /* -F option; if 0, dmenu doesn't use fuzzy matching */ #endif // FUZZYMATCH_PATCH @@ -25,6 +28,17 @@ static const char *fonts[] = "monospace:size=10" }; static const char *prompt = NULL; /* -p option; prompt to the left of input field */ + +#if ALPHA_PATCH +static const unsigned int baralpha = 0xd0; +static const unsigned int borderalpha = OPAQUE; +static const unsigned int alphas[][3] = { + /* fg bg border */ + [SchemeNorm] = { OPAQUE, baralpha, borderalpha }, + [SchemeSel] = { OPAQUE, baralpha, borderalpha }, +}; +#endif // ALPHA_PATCH + #if XRESOURCES_PATCH static char *colors[][2] = #else diff --git a/config.mk b/config.mk index 428778a..b79896e 100644 --- a/config.mk +++ b/config.mk @@ -18,9 +18,12 @@ FREETYPEINC = /usr/include/freetype2 # OpenBSD (uncomment) #FREETYPEINC = $(X11INC)/freetype2 +# Uncomment this for the alpha patch / ALPHA_PATCH +# XRENDER = -lXrender + # includes and libs INCS = -I$(X11INC) -I$(FREETYPEINC) -LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lm -lXrender +LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lm $(XRENDER) # flags CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS) diff --git a/dmenu.c b/dmenu.c index 6ae4416..89a6779 100644 --- a/dmenu.c +++ b/dmenu.c @@ -25,6 +25,10 @@ * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) #define LENGTH(X) (sizeof X / sizeof X[0]) #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) +#if ALPHA_PATCH +#define OPAQUE 0xffU +#define OPACITY "_NET_WM_WINDOW_OPACITY" +#endif // ALPHA_PATCH /* enums */ enum { @@ -78,13 +82,20 @@ static int use_text_input = 0; #endif // PRINTINPUTTEXT_PATCH static Atom clip, utf8; -#if WMTYPE_PATCH +#if WMTYPE_PATCH || ALPHA_PATCH static Atom type, dock; -#endif // WMTYPE_PATCH +#endif // WMTYPE_PATCH | ALPHA_PATCH static Display *dpy; static Window root, parentwin, win; static XIC xic; +#if ALPHA_PATCH +static int useargb = 0; +static Visual *visual; +static int depth; +static Colormap cmap; +#endif // ALPHA_PATCH + static Drw *drw; static Clr *scheme[SchemeLast]; @@ -109,6 +120,9 @@ static size_t nextrune(int inc); static void movewordedge(int dir); static void keypress(XKeyEvent *ev); static void paste(void); +#if ALPHA_PATCH +static void xinitvisual(void); +#endif // ALPHA_PATCH static void readstdin(void); static void run(void); static void setup(void); @@ -779,6 +793,45 @@ paste(void) drawmenu(); } +#if ALPHA_PATCH +static void +xinitvisual() +{ + XVisualInfo *infos; + XRenderPictFormat *fmt; + int nitems; + int i; + + XVisualInfo tpl = { + .screen = screen, + .depth = 32, + .class = TrueColor + }; + long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; + + infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); + visual = NULL; + for(i = 0; i < nitems; i ++) { + fmt = XRenderFindVisualFormat(dpy, infos[i].visual); + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { + visual = infos[i].visual; + depth = infos[i].depth; + cmap = XCreateColormap(dpy, root, visual, AllocNone); + useargb = 1; + break; + } + } + + XFree(infos); + + if (! visual) { + visual = DefaultVisual(dpy, screen); + depth = DefaultDepth(dpy, screen); + cmap = DefaultColormap(dpy, screen); + } +} +#endif // ALPHA_PATCH + #if !NON_BLOCKING_STDIN_PATCH static void readstdin(void) @@ -882,18 +935,26 @@ setup(void) /* init appearance */ #if XRESOURCES_PATCH for (j = 0; j < SchemeLast; j++) + #if ALPHA_PATCH + scheme[j] = drw_scm_create(drw, (const char**)colors[j], alphas[j], 2); + #else scheme[j] = drw_scm_create(drw, (const char**)colors[j], 2); + #endif // ALPHA_PATCH for (j = 0; j < SchemeOut; ++j) for (i = 0; i < 2; ++i) free(colors[j][i]); #else for (j = 0; j < SchemeLast; j++) + #if ALPHA_PATCH + scheme[j] = drw_scm_create(drw, colors[j], alphas[j], 2); + #else scheme[j] = drw_scm_create(drw, colors[j], 2); + #endif // ALPHA_PATCH #endif // XRESOURCES_PATCH clip = XInternAtom(dpy, "CLIPBOARD", False); utf8 = XInternAtom(dpy, "UTF8_STRING", False); - #if WMTYPE_PATCH + #if WMTYPE_PATCH || ALPHA_PATCH type = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE", False); dock = XInternAtom(dpy, "_NET_WM_WINDOW_TYPE_DOCK", False); #endif // WMTYPE_PATCH @@ -1000,7 +1061,12 @@ setup(void) /* create menu window */ swa.override_redirect = True; + #if ALPHA_PATCH + swa.background_pixel = 0; + swa.colormap = cmap; + #else swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; + #endif // ALPHA_PATCH swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask #if MOUSE_SUPPORT_PATCH | ButtonPressMask @@ -1011,8 +1077,14 @@ setup(void) #else win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0, #endif // BORDER_PATCH + #if ALPHA_PATCH + depth, InputOutput, visual, + CWOverrideRedirect|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask, &swa + #else CopyFromParent, CopyFromParent, CopyFromParent, - CWOverrideRedirect | CWBackPixel | CWEventMask, &swa); + CWOverrideRedirect | CWBackPixel | CWEventMask, &swa + #endif // ALPHA_PATCH + ); #if BORDER_PATCH if (border_width) XSetWindowBorder(dpy, win, scheme[SchemeSel][ColBg].pixel); @@ -1078,9 +1150,12 @@ usage(void) #endif // REJECTNOMATCH_PATCH "] [-l lines] [-p prompt] [-fn font] [-m monitor]" "\n [-nb color] [-nf color] [-sb color] [-sf color] [-w windowid]" - #if BORDER_PATCH || INITIALTEXT_PATCH || LINE_HEIGHT_PATCH || NAVHISTORY_PATCH || XYW_PATCH + #if ALPHA_PATCH || BORDER_PATCH || INITIALTEXT_PATCH || LINE_HEIGHT_PATCH || NAVHISTORY_PATCH || XYW_PATCH "\n " #endif + #if ALPHA_PATCH + " [ -o opacity]" + #endif // ALPHA_PATCH #if BORDER_PATCH " [-bw width]" #endif // BORDER_PATCH @@ -1177,6 +1252,10 @@ main(int argc, char *argv[]) #endif // XYW_PATCH else if (!strcmp(argv[i], "-m")) mon = atoi(argv[++i]); + #if ALPHA_PATCH + else if (!strcmp(argv[i], "-o")) /* opacity */ + opacity = atof(argv[++i]); + #endif // ALPHA_PATCH else if (!strcmp(argv[i], "-p")) /* adds prompt to left of input field */ prompt = argv[++i]; else if (!strcmp(argv[i], "-fn")) /* font or font set */ @@ -1232,7 +1311,12 @@ main(int argc, char *argv[]) die("could not get embedding window attributes: 0x%lx", parentwin); + #if ALPHA_PATCH + xinitvisual(); + drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); + #else drw = drw_create(dpy, screen, root, wa.width, wa.height); + #endif // ALPHA_PATCH #if XRESOURCES_PATCH readxresources(); if (!drw_fontset_create(drw, (const char**)fonts, LENGTH(fonts))) diff --git a/drw.c b/drw.c index e73ba4f..7656600 100644 --- a/drw.c +++ b/drw.c @@ -62,7 +62,11 @@ utf8decode(const char *c, long *u, size_t clen) } Drw * +#if ALPHA_PATCH +drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap) +#else drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) +#endif // ALPHA_PATCH { Drw *drw = ecalloc(1, sizeof(Drw)); @@ -71,8 +75,16 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h drw->root = root; drw->w = w; drw->h = h; + #if ALPHA_PATCH + drw->visual = visual; + drw->depth = depth; + drw->cmap = cmap; + drw->drawable = XCreatePixmap(dpy, root, w, h, depth); + drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL); + #else drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); drw->gc = XCreateGC(dpy, root, 0, NULL); + #endif // ALPHA_PATCH XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); return drw; @@ -88,7 +100,11 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h) drw->h = h; if (drw->drawable) XFreePixmap(drw->dpy, drw->drawable); + #if ALPHA_PATCH + drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth); + #else drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen)); + #endif // ALPHA_PATCH } void @@ -194,21 +210,37 @@ drw_fontset_free(Fnt *font) } void +#if ALPHA_PATCH +drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha) +#else drw_clr_create(Drw *drw, Clr *dest, const char *clrname) +#endif // ALPHA_PATCH { if (!drw || !dest || !clrname) return; + #if ALPHA_PATCH + if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap, + clrname, dest)) + die("error, cannot allocate color '%s'", clrname); + + dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24); + #else if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen), DefaultColormap(drw->dpy, drw->screen), clrname, dest)) die("error, cannot allocate color '%s'", clrname); + #endif // ALPHA_PATCH } /* Wrapper to create color schemes. The caller has to call free(3) on the * returned color scheme when done using it. */ Clr * +#if ALPHA_PATCH +drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount) +#else drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) +#endif // ALPHA_PATCH { size_t i; Clr *ret; @@ -218,7 +250,11 @@ drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) return NULL; for (i = 0; i < clrcount; i++) + #if ALPHA_PATCH + drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]); + #else drw_clr_create(drw, &ret[i], clrnames[i]); + #endif // ALPHA_PATCH return ret; } @@ -274,9 +310,13 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp } else { XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); + #if ALPHA_PATCH + d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap); + #else d = XftDrawCreate(drw->dpy, drw->drawable, DefaultVisual(drw->dpy, drw->screen), DefaultColormap(drw->dpy, drw->screen)); + #endif // ALPHA_PATCH x += lpad; w -= lpad; } diff --git a/drw.h b/drw.h index 0859579..bd357c8 100644 --- a/drw.h +++ b/drw.h @@ -20,6 +20,11 @@ typedef struct { Display *dpy; int screen; Window root; + #if ALPHA_PATCH + Visual *visual; + unsigned int depth; + Colormap cmap; + #endif // ALPHA_PATCH Drawable drawable; GC gc; Clr *scheme; @@ -27,7 +32,11 @@ typedef struct { } Drw; /* Drawable abstraction */ +#if ALPHA_PATCH +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap); +#else Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); +#endif // ALPHA_PATCH void drw_resize(Drw *drw, unsigned int w, unsigned int h); void drw_free(Drw *drw); @@ -38,8 +47,13 @@ unsigned int drw_fontset_getwidth(Drw *drw, const char *text); void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); /* Colorscheme abstraction */ +#if ALPHA_PATCH +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha); +Clr *drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount); +#else void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); +#endif // ALPHA_PATCH /* Cursor abstraction */ Cur *drw_cur_create(Drw *drw, int shape); diff --git a/patches.def.h b/patches.def.h index ac14c58..0d15f06 100644 --- a/patches.def.h +++ b/patches.def.h @@ -1,5 +1,11 @@ /* Patches */ +/* The alpha patch adds transparency for the dmenu window. + * You need to uncomment the corresponding line in config.mk to use the -lXrender library + * when including this patch. + */ +#define ALPHA_PATCH 0 + /* This patch adds a border around the dmenu window. It is intended to be used with the center * or xyw patches, to make the menu stand out from similarly coloured windows. * http://tools.suckless.org/dmenu/patches/border/