Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

277 lines
9.1 KiB

  1. From 4958fda4cfc1c788443fd880ec6bc18b6380c1a2 Mon Sep 17 00:00:00 2001
  2. From: Tait Hoyem <44244401+TTWNO@users.noreply.github.com>
  3. Date: Thu, 22 Aug 2019 15:39:48 +0000
  4. Subject: [PATCH] Add alpha support
  5. ---
  6. config.def.h | 9 ++++++++
  7. config.mk | 2 +-
  8. dmenu.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++----
  9. drw.c | 25 +++++++++++-----------
  10. drw.h | 9 +++++---
  11. 5 files changed, 85 insertions(+), 20 deletions(-)
  12. diff --git a/config.def.h b/config.def.h
  13. index 1edb647..595cc41 100644
  14. --- a/config.def.h
  15. +++ b/config.def.h
  16. @@ -6,6 +6,8 @@ static int topbar = 1; /* -b option; if 0, dmenu appears a
  17. static const char *fonts[] = {
  18. "monospace:size=10"
  19. };
  20. +static const unsigned int bgalpha = 0xe0;
  21. +static const unsigned int fgalpha = OPAQUE;
  22. static const char *prompt = NULL; /* -p option; prompt to the left of input field */
  23. static const char *colors[SchemeLast][2] = {
  24. /* fg bg */
  25. @@ -13,6 +15,13 @@ static const char *colors[SchemeLast][2] = {
  26. [SchemeSel] = { "#eeeeee", "#005577" },
  27. [SchemeOut] = { "#000000", "#00ffff" },
  28. };
  29. +static const unsigned int alphas[SchemeLast][2] = {
  30. + /* fgalpha bgalphga */
  31. + [SchemeNorm] = { fgalpha, bgalpha },
  32. + [SchemeSel] = { fgalpha, bgalpha },
  33. + [SchemeOut] = { fgalpha, bgalpha },
  34. +};
  35. +
  36. /* -l option; if nonzero, dmenu uses vertical list with given number of lines */
  37. static unsigned int lines = 0;
  38. diff --git a/config.mk b/config.mk
  39. index 0929b4a..260eeae 100644
  40. --- a/config.mk
  41. +++ b/config.mk
  42. @@ -20,7 +20,7 @@ FREETYPEINC = /usr/include/freetype2
  43. # includes and libs
  44. INCS = -I$(X11INC) -I$(FREETYPEINC)
  45. -LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS)
  46. +LIBS = -L$(X11LIB) -lX11 $(XINERAMALIBS) $(FREETYPELIBS) -lXrender
  47. # flags
  48. CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION=\"$(VERSION)\" $(XINERAMAFLAGS)
  49. diff --git a/dmenu.c b/dmenu.c
  50. index 65f25ce..f88a205 100644
  51. --- a/dmenu.c
  52. +++ b/dmenu.c
  53. @@ -25,6 +25,9 @@
  54. #define LENGTH(X) (sizeof X / sizeof X[0])
  55. #define TEXTW(X) (drw_fontset_getwidth(drw, (X)) + lrpad)
  56. +/* define opaqueness */
  57. +#define OPAQUE 0xFFU
  58. +
  59. /* enums */
  60. enum { SchemeNorm, SchemeSel, SchemeOut, SchemeLast }; /* color schemes */
  61. @@ -51,6 +54,10 @@ static Window root, parentwin, win;
  62. static XIC xic;
  63. static Drw *drw;
  64. +static int usergb = 0;
  65. +static Visual *visual;
  66. +static int depth;
  67. +static Colormap cmap;
  68. static Clr *scheme[SchemeLast];
  69. #include "config.h"
  70. @@ -58,6 +65,46 @@ static Clr *scheme[SchemeLast];
  71. static int (*fstrncmp)(const char *, const char *, size_t) = strncmp;
  72. static char *(*fstrstr)(const char *, const char *) = strstr;
  73. +
  74. +static void
  75. +xinitvisual()
  76. +{
  77. + XVisualInfo *infos;
  78. + XRenderPictFormat *fmt;
  79. + int nitems;
  80. + int i;
  81. +
  82. + XVisualInfo tpl = {
  83. + .screen = screen,
  84. + .depth = 32,
  85. + .class = TrueColor
  86. + };
  87. +
  88. + long masks = VisualScreenMask | VisualDepthMask | VisualClassMask;
  89. +
  90. + infos = XGetVisualInfo(dpy, masks, &tpl, &nitems);
  91. + visual = NULL;
  92. +
  93. + for (i = 0; i < nitems; i++){
  94. + fmt = XRenderFindVisualFormat(dpy, infos[i].visual);
  95. + if (fmt->type == PictTypeDirect && fmt->direct.alphaMask) {
  96. + visual = infos[i].visual;
  97. + depth = infos[i].depth;
  98. + cmap = XCreateColormap(dpy, root, visual, AllocNone);
  99. + usergb = 1;
  100. + break;
  101. + }
  102. + }
  103. +
  104. + XFree(infos);
  105. +
  106. + if (! visual) {
  107. + visual = DefaultVisual(dpy, screen);
  108. + depth = DefaultDepth(dpy, screen);
  109. + cmap = DefaultColormap(dpy, screen);
  110. + }
  111. +}
  112. +
  113. static void
  114. appenditem(struct item *item, struct item **list, struct item **last)
  115. {
  116. @@ -602,7 +649,7 @@ setup(void)
  117. #endif
  118. /* init appearance */
  119. for (j = 0; j < SchemeLast; j++)
  120. - scheme[j] = drw_scm_create(drw, colors[j], 2);
  121. + scheme[j] = drw_scm_create(drw, colors[j], alphas[j], 2);
  122. clip = XInternAtom(dpy, "CLIPBOARD", False);
  123. utf8 = XInternAtom(dpy, "UTF8_STRING", False);
  124. @@ -657,11 +704,15 @@ setup(void)
  125. /* create menu window */
  126. swa.override_redirect = True;
  127. swa.background_pixel = scheme[SchemeNorm][ColBg].pixel;
  128. + swa.border_pixel = 0;
  129. + swa.colormap = cmap;
  130. swa.event_mask = ExposureMask | KeyPressMask | VisibilityChangeMask;
  131. win = XCreateWindow(dpy, parentwin, x, y, mw, mh, 0,
  132. - CopyFromParent, CopyFromParent, CopyFromParent,
  133. - CWOverrideRedirect | CWBackPixel | CWEventMask, &swa);
  134. + depth, InputOutput, visual,
  135. + CWOverrideRedirect | CWBackPixel | CWColormap | CWEventMask | CWBorderPixel, &swa);
  136. XSetClassHint(dpy, win, &ch);
  137. @@ -747,7 +798,8 @@ main(int argc, char *argv[])
  138. if (!XGetWindowAttributes(dpy, parentwin, &wa))
  139. die("could not get embedding window attributes: 0x%lx",
  140. parentwin);
  141. - drw = drw_create(dpy, screen, root, wa.width, wa.height);
  142. + xinitvisual();
  143. + drw = drw_create(dpy, screen, root, wa.width, wa.height, visual, depth, cmap);
  144. if (!drw_fontset_create(drw, fonts, LENGTH(fonts)))
  145. die("no fonts could be loaded.");
  146. lrpad = drw->fonts->h;
  147. diff --git a/drw.c b/drw.c
  148. index 8fd1ca4..88eb9d8 100644
  149. --- a/drw.c
  150. +++ b/drw.c
  151. @@ -61,7 +61,7 @@ utf8decode(const char *c, long *u, size_t clen)
  152. }
  153. Drw *
  154. -drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h)
  155. +drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap)
  156. {
  157. Drw *drw = ecalloc(1, sizeof(Drw));
  158. @@ -70,8 +70,11 @@ drw_create(Display *dpy, int screen, Window root, unsigned int w, unsigned int h
  159. drw->root = root;
  160. drw->w = w;
  161. drw->h = h;
  162. - drw->drawable = XCreatePixmap(dpy, root, w, h, DefaultDepth(dpy, screen));
  163. - drw->gc = XCreateGC(dpy, root, 0, NULL);
  164. + drw->visual = visual;
  165. + drw->depth = depth;
  166. + drw->cmap = cmap;
  167. + drw->drawable = XCreatePixmap(dpy, root, w, h, depth);
  168. + drw->gc = XCreateGC(dpy, drw->drawable, 0, NULL);
  169. XSetLineAttributes(dpy, drw->gc, 1, LineSolid, CapButt, JoinMiter);
  170. return drw;
  171. @@ -87,7 +90,7 @@ drw_resize(Drw *drw, unsigned int w, unsigned int h)
  172. drw->h = h;
  173. if (drw->drawable)
  174. XFreePixmap(drw->dpy, drw->drawable);
  175. - drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, DefaultDepth(drw->dpy, drw->screen));
  176. + drw->drawable = XCreatePixmap(drw->dpy, drw->root, w, h, drw->depth);
  177. }
  178. void
  179. @@ -193,21 +196,21 @@ drw_fontset_free(Fnt *font)
  180. }
  181. void
  182. -drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
  183. +drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha)
  184. {
  185. if (!drw || !dest || !clrname)
  186. return;
  187. - if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
  188. - DefaultColormap(drw->dpy, drw->screen),
  189. + if (!XftColorAllocName(drw->dpy, drw->visual, drw->cmap,
  190. clrname, dest))
  191. die("error, cannot allocate color '%s'", clrname);
  192. + dest->pixel = (dest->pixel & 0x00FFFFFFFU) | alpha << 24;
  193. }
  194. /* Wrapper to create color schemes. The caller has to call free(3) on the
  195. * returned color scheme when done using it. */
  196. Clr *
  197. -drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  198. +drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount)
  199. {
  200. size_t i;
  201. Clr *ret;
  202. @@ -217,7 +220,7 @@ drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount)
  203. return NULL;
  204. for (i = 0; i < clrcount; i++)
  205. - drw_clr_create(drw, &ret[i], clrnames[i]);
  206. + drw_clr_create(drw, &ret[i], clrnames[i], alphas[i]);
  207. return ret;
  208. }
  209. @@ -273,9 +276,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
  210. } else {
  211. XSetForeground(drw->dpy, drw->gc, drw->scheme[invert ? ColFg : ColBg].pixel);
  212. XFillRectangle(drw->dpy, drw->drawable, drw->gc, x, y, w, h);
  213. - d = XftDrawCreate(drw->dpy, drw->drawable,
  214. - DefaultVisual(drw->dpy, drw->screen),
  215. - DefaultColormap(drw->dpy, drw->screen));
  216. + d = XftDrawCreate(drw->dpy, drw->drawable, drw->visual, drw->cmap);
  217. x += lpad;
  218. w -= lpad;
  219. }
  220. diff --git a/drw.h b/drw.h
  221. index 4c67419..4f66f0d 100644
  222. --- a/drw.h
  223. +++ b/drw.h
  224. @@ -20,6 +20,9 @@ typedef struct {
  225. Display *dpy;
  226. int screen;
  227. Window root;
  228. + Visual *visual;
  229. + unsigned int depth;
  230. + Colormap cmap;
  231. Drawable drawable;
  232. GC gc;
  233. Clr *scheme;
  234. @@ -27,7 +30,7 @@ typedef struct {
  235. } Drw;
  236. /* Drawable abstraction */
  237. -Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h);
  238. +Drw *drw_create(Display *dpy, int screen, Window win, unsigned int w, unsigned int h, Visual *visual, unsigned int depth, Colormap cmap);
  239. void drw_resize(Drw *drw, unsigned int w, unsigned int h);
  240. void drw_free(Drw *drw);
  241. @@ -38,8 +41,8 @@ unsigned int drw_fontset_getwidth(Drw *drw, const char *text);
  242. void drw_font_getexts(Fnt *font, const char *text, unsigned int len, unsigned int *w, unsigned int *h);
  243. /* Colorscheme abstraction */
  244. -void drw_clr_create(Drw *drw, Clr *dest, const char *clrname);
  245. -Clr *drw_scm_create(Drw *drw, const char *clrnames[], size_t clrcount);
  246. +void drw_clr_create(Drw *drw, Clr *dest, const char *clrname, unsigned int alpha);
  247. +Clr *drw_scm_create(Drw *drw, const char *clrnames[], const unsigned int alphas[], size_t clrcount);
  248. /* Cursor abstraction */
  249. Cur *drw_cur_create(Drw *drw, int shape);
  250. --
  251. 2.23.0