dmenu-alpha-20251118-8b48986.diff (9108B)
1 diff --git a/config.def.h b/config.def.h 2 index 1edb647..a2cb342 100644 3 --- a/config.def.h 4 +++ b/config.def.h 5 @@ -2,6 +2,7 @@ 6 /* Default settings; can be overriden by command line. */ 7 8 static int topbar = 1; /* -b option; if 0, dmenu appears at bottom */ 9 +static const unsigned int alpha = 0xee; /* Amount of opacity. 0xff is opaque */ 10 /* -fn option overrides fonts[0]; default X11 font or font set */ 11 static const char *fonts[] = { 12 "monospace:size=10" 13 @@ -13,6 +14,12 @@ static const char *colors[SchemeLast][2] = { 14 [SchemeSel] = { "#eeeeee", "#005577" }, 15 [SchemeOut] = { "#000000", "#00ffff" }, 16 }; 17 + 18 +static const unsigned int alphas[SchemeLast][2] = { 19 + [SchemeNorm] = { OPAQUE, alpha }, 20 + [SchemeSel] = { OPAQUE, alpha }, 21 + [SchemeOut] = { OPAQUE, alpha }, 22 +}; 23 /* -l option; if nonzero, dmenu uses vertical list with given number of lines */ 24 static unsigned int lines = 0; 25 26 diff --git a/config.mk b/config.mk 27 index dcc5bb3..2166055 100644 28 --- a/config.mk 29 +++ b/config.mk 30 @@ -21,7 +21,7 @@ FREETYPEINC = /usr/include/freetype2 31 32 # includes and libs 33 INCS = -I$(X11INC) -I$(FREETYPEINC) 34 -LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) 35 +LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lXrender 36 37 # flags 38 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS) 39 diff --git a/dmenu.c b/dmenu.c 40 index 9577b37..22e4a4b 100644 41 --- a/dmenu.c 42 +++ b/dmenu.c 43 @@ -10,10 +10,12 @@ 44 45 #include <X11/Xlib.h> 46 #include <X11/Xatom.h> 47 +#include <X11/Xproto.h> 48 #include <X11/Xutil.h> 49 #ifdef XINERAMA 50 #include <X11/extensions/Xinerama.h> 51 #endif 52 +#include <X11/extensions/Xrender.h> 53 #include <X11/Xft/Xft.h> 54 55 #include "drw.h" 56 @@ -24,6 +26,8 @@ 57 * MAX(0, MIN((y)+(h),(r).y_org+(r).height) - MAX((y),(r).y_org))) 58 #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad) 59 60 +#define OPAQUE 0xffu 61 + 62 /* enums */ 63 enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */ 64 65 @@ -52,10 +56,16 @@ static XIC xic; 66 static Drw *drw; 67 static Clr *scheme[SchemeLast]; 68 69 +static int useargb = 0; 70 +static Visual *visual; 71 +static int depth; 72 +static Colormap cmap; 73 + 74 #include "config.h" 75 76 static int (*fstrncmp)(const char *, const char *, size_t) = strncmp; 77 static char *(*fstrstr)(const char *, const char *) = strstr; 78 +static void xinitvisual(void); 79 80 static unsigned int 81 textw_clamp(const char *str, unsigned int n) 82 @@ -627,7 +637,7 @@ setup(void) 83 #endif 84 /* init appearance */ 85 for (j = 0; j < SchemeLast; j++) 86 - scheme[j] = drw_scm_create(drw, colors[j], 2); 87 + scheme[j] = drw_scm_create(drw, colors[j], 2, alphas[j]); 88 89 clip = XInternAtom(dpy, "CLIPBOARD", False); 90 utf8 = XInternAtom(dpy, "UTF8_STRING", False); 91 @@ -682,11 +692,12 @@ setup(void) 92 93 /* create menu window */ 94 swa.override_redirect = True; 95 - swa.background_pixel = scheme[SchemeNorm][ColBg].pixel; 96 + swa.border_pixel = 0; 97 + swa.colormap = cmap; 98 swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask; 99 win = XCreateWindow(dpy, root, x, y, mw, mh, 0, 100 - CopyFromParent, CopyFromParent, CopyFromParent, 101 - CWOverrideRedirect | CWBackPixel | CWEventMask, &swa); 102 + depth, CopyFromParent, visual, 103 + CWOverrideRedirect | CWBackPixel | CWBorderPixel | CWColormap | CWEventMask, &swa); 104 XSetClassHint(dpy, win, &ch); 105 106 /* input methods */ 107 @@ -771,7 +782,8 @@ main(int argc, char *argv[]) 108 if (!XGetWindowAttributes(dpy, parentwin, &wa)) 109 die("could not get embedding window attributes: 0x%lx", 110 parentwin); 111 - drw = drw_create(dpy, screen, root, wa.width, wa.height); 112 + xinitvisual(); 113 + drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap); 114 if (!drw_fontset_create(drw, fonts, LENGTH(fonts))) 115 die("no fonts could be loaded."); 116 lrpad = drw->fonts->h; 117 @@ -793,3 +805,40 @@ main(int argc, char *argv[]) 118 119 return 1; /* unreachable */ 120 } 121 + 122 +void 123 +xinitvisual(void) 124 +{ 125 + XVisualInfo *infos; 126 + XRenderPictFormat *fmt; 127 + int nitems; 128 + int i; 129 + 130 + XVisualInfo tpl = { 131 + .screen = screen, 132 + .depth = 32, 133 + .class = TrueColor 134 + }; 135 + long masks = VisualScreenMask | VisualDepthMask | VisualClassMask; 136 + 137 + infos = XGetVisualInfo(dpy, masks, &tpl, &nitems); 138 + visual = NULL; 139 + for(i = 0; i < nitems; i ++) { 140 + fmt = XRenderFindVisualFormat(dpy, infos[i].visual); 141 + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) { 142 + visual = infos[i].visual; 143 + depth = infos[i].depth; 144 + cmap = XCreateColormap(dpy, root, visual, AllocNone); 145 + useargb = 1; 146 + break; 147 + } 148 + } 149 + 150 + XFree(infos); 151 + 152 + if (!visual) { 153 + visual = DefaultVisual(dpy, screen); 154 + depth = DefaultDepth(dpy, screen); 155 + cmap = DefaultColormap(dpy, screen); 156 + } 157 +} 158 diff --git a/drw.c b/drw.c 159 index 9fdd1a4..a4a01e8 100644 160 --- a/drw.c 161 +++ b/drw.c 162 @@ -47,7 +47,7 @@ utf8decode(const char *s_in, long *u, int *err) 163 } 164 165 Drw * 166 -drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h) 167 +drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap) 168 { 169 Drw *drw = ecalloc(1, sizeof(Drw)); 170 171 @@ -56,8 +56,11 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h 172 drw->root = root; 173 drw->w = w; 174 drw->h = h; 175 - drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen)); 176 - drw->gc = XCreateGC(dpy, root, 0, NULL); 177 + drw->visual = visual; 178 + drw->depth = depth; 179 + drw->cmap = cmap; 180 + drw->drawable = XCreatePixmap(dpy, root, w, h, depth); 181 + drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL); 182 XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter); 183 184 return drw; 185 @@ -73,7 +76,7 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h) 186 drw->h = h; 187 if (drw->drawable) 188 XFreePixmap(drw->dpy, drw->drawable); 189 - drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen)); 190 + drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth); 191 } 192 193 void 194 @@ -167,20 +170,21 @@ drw_fontset_free(Fnt *font) 195 } 196 197 void 198 -drw_clr_create(Drw *drw, Clr *dest, const char *clrname) 199 +drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha) 200 { 201 if (!drw || !dest || !clrname) 202 return; 203 204 - if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen), 205 - DefaultColormap(drw->dpy, drw->screen), 206 + if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap, 207 clrname, dest)) 208 die("error, cannot allocate color '%s'", clrname); 209 + 210 + dest->pixel = (dest->pixel & 0x00ffffffU) | (alpha << 24); 211 } 212 213 /* Create color schemes. */ 214 Clr * 215 -drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) 216 +drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount, const unsigned int alphas[]) 217 { 218 size_t i; 219 Clr *ret; 220 @@ -190,7 +194,7 @@ drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount) 221 return NULL; 222 223 for (i = 0; i < clrcount; i++) 224 - drw_clr_create(drw, &ret[i], clrnames[i]); 225 + drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]); 226 return ret; 227 } 228 229 @@ -271,11 +275,9 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp 230 } else { 231 XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel); 232 XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h); 233 + d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap); 234 if (w < lpad) 235 return x + w; 236 - d = XftDrawCreate(drw->dpy, drw->drawable, 237 - DefaultVisual(drw->dpy, drw->screen), 238 - DefaultColormap(drw->dpy, drw->screen)); 239 x += lpad; 240 w -= lpad; 241 } 242 diff --git a/drw.h b/drw.h 243 index 2ed77be..5a0d7d7 100644 244 --- a/drw.h 245 +++ b/drw.h 246 @@ -20,6 +20,9 @@ typedef struct { 247 Display *dpy; 248 int screen; 249 Window root; 250 + Visual *visual; 251 + unsigned int depth; 252 + Colormap cmap; 253 Drawable drawable; 254 GC gc; 255 Clr *scheme; 256 @@ -27,7 +30,7 @@ typedef struct { 257 } Drw; 258 259 /* Drawable abstraction */ 260 -Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h); 261 +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h, Visual*, unsigned int, Colormap); 262 void drw_resize(Drw *drw, unsigned int w, unsigned int h); 263 void drw_free(Drw *drw); 264 265 @@ -39,9 +42,9 @@ unsigned int drw_fontset_getwidth_clamp(Drw *drw, const char *text, unsigned int 266 void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h); 267 268 /* Colorscheme abstraction */ 269 -void drw_clr_create(Drw *drw, Clr *dest, const char *clrname); 270 +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha); 271 void drw_clr_free(Drw *drw, Clr *c); 272 -Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount); 273 +Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount, const unsigned int alphas[]); 274 void drw_scm_free(Drw *drw, Clr *scm, size_t clrcount); 275 276 /* Cursor abstraction */