[wiki] [sites] [dwm][patch][winicon] Bug fixes and optimizations || AdamYuan

From: <git_AT_suckless.org>
Date: Tue, 13 Jul 2021 05:17:57 +0200

commit cefaf8b9617b8e557909c686da06fb96a1a54245
Author: AdamYuan <y13916619121_AT_126.com>
Date: Tue Jul 13 11:17:12 2021 +0800

    [dwm][patch][winicon] Bug fixes and optimizations
    
    Fixed potential memory leak when imlib_create_image() fails
    
    Implemented pre-multiplying alpha channel to speedup blending in drw_img()

diff --git a/dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.2.diff b/dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.2.diff
index becd45cd..b73cc11e 100644
--- a/dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.2.diff
+++ b/dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.2.diff
_AT_@ -30,7 +30,7 @@ index 6d36cb7..c5d08de 100644
  # flags
  CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=2 -DVERSION=\"${VERSION}\" ${XINERAMAFLAGS}
 diff --git a/drw.c b/drw.c
-index 8fd1ca4..91454e1 100644
+index 8fd1ca4..bb6cfab 100644
 --- a/drw.c
 +++ b/drw.c
 _AT_@ -2,6 +2,7 @@
_AT_@ -41,16 +41,15 @@ index 8fd1ca4..91454e1 100644
  #include <X11/Xlib.h>
  #include <X11/Xft/Xft.h>
  
-_AT_@ -378,6 +379,27 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
+_AT_@ -378,6 +379,26 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned int h, unsigned int lp
          return x + (render ? w : 0);
  }
  
-+static uint32_t blend32(uint32_t p1, uint32_t p2) {
-+ uint32_t a = p2 >> 24;
-+ uint32_t na = 255 - a;
-+ uint32_t rb = ((na * (p1 & 0x00ff00ffu)) + (a * (p2 & 0x00ff00ffu))) >> 8;
-+ uint32_t ag = (na * ((p1 & 0xff00ff00u) >> 8)) + (a * (0x01000000u | ((p2 & 0x0000ff00u) >> 8)));
-+ return ((rb & 0x00ff00ffu) | (ag & 0xff00ff00u));
++static uint32_t blend(uint32_t p1rb, uint32_t p1g, uint32_t p2) {
++ uint8_t a = p2 >> 24u;
++ uint32_t rb = (p2 & 0xFF00FFu) + ( (a * p1rb) >> 8u );
++ uint32_t g = (p2 & 0x00FF00u) + ( (a * p1g) >> 8u );
++ return (rb & 0xFF00FFu) | (g & 0x00FF00u);
 +}
 +
 +void
_AT_@ -58,12 +57,12 @@ index 8fd1ca4..91454e1 100644
 +{
 + if (!drw || !drw->scheme)
 + return;
-+ uint32_t *data = (uint32_t *)img->data, bt = drw->scheme[ColBg].pixel;
++ uint32_t *data = (uint32_t *)img->data, p = drw->scheme[ColBg].pixel, prb = p & 0xFF00FFu, pg = p & 0x00FF00u;
 + int icsz = img->width * img->height, i;
-+ for (i = 0; i < icsz; ++i) tmp[i] = blend32(bt, data[i]);
-+ img->data = (char *)tmp;
++ for (i = 0; i < icsz; ++i) tmp[i] = blend(prb, pg, data[i]);
++ img->data = (char *) tmp;
 + XPutImage(drw->dpy, drw->drawable, drw->gc, img, 0, 0, x, y, img->width, img->height);
-+ img->data = (char *)data;
++ img->data = (char *) data;
 +}
 +
  void
_AT_@ -82,7 +81,7 @@ index 4bcd5ad..07b6433 100644
  /* Map functions */
  void drw_map(Drw *drw, Window win, int x, int y, unsigned int w, unsigned int h);
 diff --git a/dwm.c b/dwm.c
-index 4465af1..b7690f8 100644
+index 4465af1..173504b 100644
 --- a/dwm.c
 +++ b/dwm.c
 _AT_@ -28,6 +28,8 @@
_AT_@ -154,10 +153,17 @@ index 4465af1..b7690f8 100644
                          if (m->sel->isfloating)
                                  drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
                  } else {
-_AT_@ -899,6 +908,78 @@ getstate(Window w)
+_AT_@ -899,6 +908,87 @@ getstate(Window w)
          return result;
  }
  
++static uint32_t prealpha(uint32_t p) {
++ uint8_t a = p >> 24u;
++ uint32_t rb = (a * (p & 0xFF00FFu)) >> 8u;
++ uint32_t g = (a * (p & 0x00FF00u)) >> 8u;
++ return (rb & 0xFF00FFu) | (g & 0x00FF00u) | ((~a) << 24u);
++}
++
 +XImage *
 +geticonprop(Window win)
 +{
_AT_@ -195,7 +201,7 @@ index 4465af1..b7690f8 100644
 +
 + w = *(bstp - 2); h = *(bstp - 1);
 +
-+ int icw, ich;
++ int icw, ich, icsz;
 + if (w <= h) {
 + ich = ICONSIZE; icw = w * ICONSIZE / h;
 + if (icw < 1) icw = 1;
_AT_@ -204,36 +210,38 @@ index 4465af1..b7690f8 100644
 + icw = ICONSIZE; ich = h * ICONSIZE / w;
 + if (ich < 1) ich = 1;
 + }
++ icsz = icw * ich;
 +
-+ unsigned char *icbuf = malloc(icw * ich << 2); if(!icbuf) { XFree(p); return NULL; }
++ int i;
 +#if ULONG_MAX > UINT32_MAX
-+ int i, sz = w * h;
++ int sz = w * h;
 + uint32_t *bstp32 = (uint32_t *)bstp;
 + for (i = 0; i < sz; ++i) bstp32[i] = bstp[i];
 +#endif
-+ if (w == icw && h == ich) memcpy(icbuf, bstp, icw * ich << 2);
++ uint32_t *icbuf = malloc(icsz << 2); if(!icbuf) { XFree(p); return NULL; }
++ if (w == icw && h == ich) memcpy(icbuf, bstp, icsz << 2);
 + else {
 + Imlib_Image origin = imlib_create_image_using_data(w, h, (DATA32 *)bstp);
-+ if (!origin) { XFree(p); return NULL; }
++ if (!origin) { XFree(p); free(icbuf); return NULL; }
 + imlib_context_set_image(origin);
 + imlib_image_set_has_alpha(1);
 + Imlib_Image scaled = imlib_create_cropped_scaled_image(0, 0, w, h, icw, ich);
 + imlib_free_image_and_decache();
-+ if (!scaled) { XFree(p); return NULL; }
++ if (!scaled) { XFree(p); free(icbuf); return NULL; }
 + imlib_context_set_image(scaled);
 + imlib_image_set_has_alpha(1);
-+ memcpy(icbuf, imlib_image_get_data_for_reading_only(), icw * ich << 2);
++ memcpy(icbuf, imlib_image_get_data_for_reading_only(), icsz << 2);
 + imlib_free_image_and_decache();
 + }
 + XFree(p);
-+
++ for (i = 0; i < icsz; ++i) icbuf[i] = prealpha(icbuf[i]);
 + return XCreateImage(dpy, DefaultVisual(dpy, screen), DefaultDepth(dpy, screen), ZPixmap, 0, (char *)icbuf, icw, ich, 32, 0);
 +}
 +
  int
  gettextprop(Window w, Atom atom, char *text, unsigned int size)
  {
-_AT_@ -1030,6 +1111,8 @@ manage(Window w, XWindowAttributes *wa)
+_AT_@ -1030,6 +1120,8 @@ manage(Window w, XWindowAttributes *wa)
          c->h = c->oldh = wa->height;
          c->oldbw = wa->border_width;
  
_AT_@ -242,7 +250,7 @@ index 4465af1..b7690f8 100644
          updatetitle(c);
          if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
                  c->mon = t->mon;
-_AT_@ -1240,6 +1323,11 @@ propertynotify(XEvent *e)
+_AT_@ -1240,6 +1332,11 @@ propertynotify(XEvent *e)
                          if (c == c->mon->sel)
                                  drawbar(c->mon);
                  }
_AT_@ -254,7 +262,7 @@ index 4465af1..b7690f8 100644
                  if (ev->atom == netatom[NetWMWindowType])
                          updatewindowtype(c);
          }
-_AT_@ -1556,6 +1644,7 @@ setup(void)
+_AT_@ -1556,6 +1653,7 @@ setup(void)
          netatom[NetActiveWindow] = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False);
          netatom[NetSupported] = XInternAtom(dpy, "_NET_SUPPORTED", False);
          netatom[NetWMName] = XInternAtom(dpy, "_NET_WM_NAME", False);
_AT_@ -262,7 +270,7 @@ index 4465af1..b7690f8 100644
          netatom[NetWMState] = XInternAtom(dpy, "_NET_WM_STATE", False);
          netatom[NetWMCheck] = XInternAtom(dpy, "_NET_SUPPORTING_WM_CHECK", False);
          netatom[NetWMFullscreen] = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
-_AT_@ -1746,6 +1835,15 @@ toggleview(const Arg *arg)
+_AT_@ -1746,6 +1844,15 @@ toggleview(const Arg *arg)
          }
  }
  
_AT_@ -278,7 +286,7 @@ index 4465af1..b7690f8 100644
  void
  unfocus(Client *c, int setfocus)
  {
-_AT_@ -1767,6 +1865,7 @@ unmanage(Client *c, int destroyed)
+_AT_@ -1767,6 +1874,7 @@ unmanage(Client *c, int destroyed)
  
          detach(c);
          detachstack(c);
_AT_@ -286,7 +294,7 @@ index 4465af1..b7690f8 100644
          if (!destroyed) {
                  wc.border_width = c->oldbw;
                  XGrabServer(dpy); /* avoid race conditions */
-_AT_@ -2001,6 +2100,13 @@ updatetitle(Client *c)
+_AT_@ -2001,6 +2109,13 @@ updatetitle(Client *c)
                  strcpy(c->name, broken);
  }
  
diff --git a/dwm.suckless.org/patches/winicon/index.md b/dwm.suckless.org/patches/winicon/index.md
index a05789ad..6271f070 100644
--- a/dwm.suckless.org/patches/winicon/index.md
+++ b/dwm.suckless.org/patches/winicon/index.md
_AT_@ -27,7 +27,7 @@ Configuration
 
 Download
 --------
-* [dwm-winicon-6.2-v1.2.diff](dwm-winicon-6.2-v1.2.diff) (2021-07-11)
+* [dwm-winicon-6.2-v1.2.diff](dwm-winicon-6.2-v1.2.diff) (2021-07-13)
 
 Author
 ------
Received on Tue Jul 13 2021 - 05:17:57 CEST

This archive was generated by hypermail 2.3.0 : Tue Jul 13 2021 - 05:24:42 CEST