[wiki] [sites] [dwm][patch][winicon] Fixed potential crashes, updated the guide || AdamYuan

From: <git_AT_suckless.org>
Date: Fri, 23 Jul 2021 06:23:23 +0200

commit 614f47272b4759fd722fc1f7095ebbc9061190c4
Author: AdamYuan <y13916619121_AT_126.com>
Date: Fri Jul 23 12:21:49 2021 +0800

    [dwm][patch][winicon] Fixed potential crashes, updated the guide
    
    Fix crashes when receiving false icon data with _NET_WM_ICON
    
    Add a guide on supporting alpha patch

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.3.diff
similarity index 87%
rename from dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.2.diff
rename to dwm.suckless.org/patches/winicon/dwm-winicon-6.2-v1.3.diff
index b73cc11e..34373a07 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.3.diff
_AT_@ -81,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..173504b 100644
+index 4465af1..850dbbf 100644
 --- a/dwm.c
 +++ b/dwm.c
 _AT_@ -28,6 +28,8 @@
_AT_@ -153,7 +153,7 @@ index 4465af1..173504b 100644
                          if (m->sel->isfloating)
                                  drw_rect(drw, x + boxs, boxs, boxw, boxw, m->sel->isfixed, 0);
                  } else {
-_AT_@ -899,6 +908,87 @@ getstate(Window w)
+_AT_@ -899,6 +908,85 @@ getstate(Window w)
          return result;
  }
  
_AT_@ -174,49 +174,47 @@ index 4465af1..173504b 100644
 + if (XGetWindowProperty(dpy, win, netatom[NetWMIcon], 0L, LONG_MAX, False, AnyPropertyType,
 + &real, &format, &n, &extra, (unsigned char **)&p) != Success)
 + return NULL;
-+ if (n == 0) { XFree(p); return NULL; }
++ if (n == 0 || format != 32) { XFree(p); return NULL; }
 +
-+ unsigned long *bstp = NULL, w, h;
++ unsigned long *bstp = NULL;
++ uint32_t w, h, sz;
 +
 + {
 + const unsigned long *end = p + n;
 + unsigned long *i;
-+ int bstd = INT_MAX, d, m;
-+ for (i = p; i < end; ) {
-+ w = *i++; h = *i++;
-+ m = w > h ? w : h;
-+ if (m >= ICONSIZE && (d = m - ICONSIZE) < bstd) { bstd = d; bstp = i; }
-+ i += (w * h);
++ uint32_t bstd = UINT32_MAX, d, m;
++ for (i = p; i + 1 < end; ) {
++ if ((w = *i++) > UINT16_MAX || (h = *i++) > UINT16_MAX) { XFree(p); return NULL; }
++ m = w > h ? w : h; sz = w * h;
++ if ((i += sz) <= end && m >= ICONSIZE && (d = m - ICONSIZE) < bstd) { bstd = d; bstp = i - sz; }
 + }
 + if (!bstp) {
-+ for (i = p; i < end; ) {
-+ w = *i++; h = *i++;
-+ m = w > h ? w : h;
-+ if ((d = ICONSIZE - m) < bstd) { bstd = d; bstp = i; }
-+ i += (w * h);
++ for (i = p; i + 1 < end; ) {
++ if ((w = *i++) > UINT16_MAX || (h = *i++) > UINT16_MAX) { XFree(p); return NULL; }
++ m = w > h ? w : h; sz = w * h;
++ if ((i += sz) <= end && (d = ICONSIZE - m) < bstd) { bstd = d; bstp = i - sz; }
 + }
 + }
 + if (!bstp) { XFree(p); return NULL; }
 + }
 +
-+ w = *(bstp - 2); h = *(bstp - 1);
++ if ((w = *(bstp - 2)) == 0 || (h = *(bstp - 1)) == 0) { XFree(p); return NULL; }
 +
-+ int icw, ich, icsz;
++ uint32_t icw, ich, icsz;
 + if (w <= h) {
 + ich = ICONSIZE; icw = w * ICONSIZE / h;
-+ if (icw < 1) icw = 1;
++ if (icw == 0) icw = 1;
 + }
 + else {
 + icw = ICONSIZE; ich = h * ICONSIZE / w;
-+ if (ich < 1) ich = 1;
++ if (ich == 0) ich = 1;
 + }
 + icsz = icw * ich;
 +
-+ int i;
++ uint32_t i;
 +#if ULONG_MAX > UINT32_MAX
-+ int sz = w * h;
 + uint32_t *bstp32 = (uint32_t *)bstp;
-+ for (i = 0; i < sz; ++i) bstp32[i] = bstp[i];
++ for (sz = w * h, i = 0; i < sz; ++i) bstp32[i] = bstp[i];
 +#endif
 + uint32_t *icbuf = malloc(icsz << 2); if(!icbuf) { XFree(p); return NULL; }
 + if (w == icw && h == ich) memcpy(icbuf, bstp, icsz << 2);
_AT_@ -241,7 +239,7 @@ index 4465af1..173504b 100644
  int
  gettextprop(Window w, Atom atom, char *text, unsigned int size)
  {
-_AT_@ -1030,6 +1120,8 @@ manage(Window w, XWindowAttributes *wa)
+_AT_@ -1030,6 +1118,8 @@ manage(Window w, XWindowAttributes *wa)
          c->h = c->oldh = wa->height;
          c->oldbw = wa->border_width;
  
_AT_@ -250,7 +248,7 @@ index 4465af1..173504b 100644
          updatetitle(c);
          if (XGetTransientForHint(dpy, w, &trans) && (t = wintoclient(trans))) {
                  c->mon = t->mon;
-_AT_@ -1240,6 +1332,11 @@ propertynotify(XEvent *e)
+_AT_@ -1240,6 +1330,11 @@ propertynotify(XEvent *e)
                          if (c == c->mon->sel)
                                  drawbar(c->mon);
                  }
_AT_@ -262,7 +260,7 @@ index 4465af1..173504b 100644
                  if (ev->atom == netatom[NetWMWindowType])
                          updatewindowtype(c);
          }
-_AT_@ -1556,6 +1653,7 @@ setup(void)
+_AT_@ -1556,6 +1651,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_@ -270,7 +268,7 @@ index 4465af1..173504b 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 +1844,15 @@ toggleview(const Arg *arg)
+_AT_@ -1746,6 +1842,15 @@ toggleview(const Arg *arg)
          }
  }
  
_AT_@ -286,7 +284,7 @@ index 4465af1..173504b 100644
  void
  unfocus(Client *c, int setfocus)
  {
-_AT_@ -1767,6 +1874,7 @@ unmanage(Client *c, int destroyed)
+_AT_@ -1767,6 +1872,7 @@ unmanage(Client *c, int destroyed)
  
          detach(c);
          detachstack(c);
_AT_@ -294,7 +292,7 @@ index 4465af1..173504b 100644
          if (!destroyed) {
                  wc.border_width = c->oldbw;
                  XGrabServer(dpy); /* avoid race conditions */
-_AT_@ -2001,6 +2109,13 @@ updatetitle(Client *c)
+_AT_@ -2001,6 +2107,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 6271f070..a3dac4e8 100644
--- a/dwm.suckless.org/patches/winicon/index.md
+++ b/dwm.suckless.org/patches/winicon/index.md
_AT_@ -7,7 +7,7 @@ Description
 
 ![screenshots](screenshots.png)
 
-It is recommended to enable the compiler flag: **-march=native** to gain better performance.
+It is recommended to enable the compiler optimization flags: **-O3** and **-march=native** to enable auto loop vectorize, which leads to better performance.
 
 The patch is being managed and developed on this GitHub [repo](https://github.com/AdamYuan/dwm-winicon). If you discover any bugs or have any idea to optimize it, feel free to create an issue there.
 
_AT_@ -27,7 +27,42 @@ Configuration
 
 Download
 --------
-* [dwm-winicon-6.2-v1.2.diff](dwm-winicon-6.2-v1.2.diff) (2021-07-13)
+* [dwm-winicon-6.2-v1.3.diff](dwm-winicon-6.2-v1.3.diff) (2021-07-23)
+
+Alpha Patch
+-----------
+If you also use [alpha patch](https://dwm.suckless.org/patches/alpha/), some changes are needed to make this patch work properly.
+
+After applying both patches,
+* change the last return statement in **geticonprop** function (dwm.c) to
+
+ return XCreateImage(drw->dpy, drw->visual, drw->depth, ZPixmap, 0, (char *)icbuf, icw, ich, 32, 0);
+
+* change **drw_img** and **blend** function (drw.c) to
+
+ inline static uint8_t div255(uint16_t x) { return (x*0x8081u) >> 23u; }
+ inline static uint32_t blend(uint32_t p1rb, uint32_t p1g, uint8_t p1a, 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) | div255(~a * 255u + a * p1a) << 24u;
+ }
+
+ void
+ drw_img(Drw *drw, int x, int y, XImage *img, uint32_t *tmp)
+ {
+ if (!drw || !drw->scheme)
+ return;
+ uint32_t *data = (uint32_t *)img->data, p = drw->scheme[ColBg].pixel,
+ prb = p & 0xFF00FFu, pg = p & 0x00FF00u;
+ uint8_t pa = p >> 24u;
+ int icsz = img->width * img->height, i;
+ for (i = 0; i < icsz; ++i) tmp[i] = blend(prb, pg, pa, 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;
+ }
 
 Author
 ------
Received on Fri Jul 23 2021 - 06:23:23 CEST

This archive was generated by hypermail 2.3.0 : Fri Jul 23 2021 - 06:24:43 CEST