Git Product home page Git Product logo

Comments (13)

AdamYuan avatar AdamYuan commented on July 21, 2024 1

Oh, you might forget to cancel the comment on the memcpy(icbuf, imlib_image_... line. That's where the image data is generated, or the image buffer will remains random

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024 1

Also try this new version of the geticonprop function, which can avoid crashing when receiving false data.

#define MAXICONAXIS 0xffffu
XImage *
geticonprop(Window win)
{
	int format;
	unsigned long n, extra, *p = NULL;
	Atom real;

	if (XGetWindowProperty(dpy, win, netatom[NetWMIcon], 0L, LONG_MAX, False, AnyPropertyType, 
						   &real, &format, &n, &extra, (unsigned char **)&p) != Success)
		return NULL; 
	if (n == 0 || format != 32) { XFree(p); return NULL; }

	unsigned long *bstp = NULL, w, h, sz;

	{
		const unsigned long *end = p + n;
		unsigned long *i;
		int bstd = INT_MAX, d, m;
		for (i = p; i + 1 < end; ) {
			if ((w = *i++) > MAXICONAXIS || (h = *i++) > MAXICONAXIS) break;
			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 + 1 < end; ) {
				if ((w = *i++) > MAXICONAXIS || (h = *i++) > MAXICONAXIS) break;
				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);

	int icw, ich, icsz;
	if (w <= h) {
		ich = ICONSIZE; icw = w * ICONSIZE / h;
		if (icw < 1) icw = 1;
	}
	else {
		icw = ICONSIZE; ich = h * ICONSIZE / w;
		if (ich < 1) ich = 1;
	}
	icsz = icw * ich;

	int i;
#if ULONG_MAX > UINT32_MAX
	uint32_t *bstp32 = (uint32_t *)bstp;
	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);
	else {
		Imlib_Image origin = imlib_create_image_using_data(w, h, (DATA32 *)bstp);
		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); free(icbuf); return NULL; }
		imlib_context_set_image(scaled);
		imlib_image_set_has_alpha(1);
		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(drw->dpy, drw->visual, drw->depth, ZPixmap, 0, (char *)icbuf, icw, ich, 32, 0);
}

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

You mean making dwm bar transparent while keeping the icons opaque ?

from dwm-winicon.

explosion-mental avatar explosion-mental commented on July 21, 2024

Yes, using this patch makes the bar transparent.

pd: the littke i know is that the rectangle that XCreateImage, correctly poiting to visual and depth made by xinitvisual(), seems to be always transparent. On the alpha array i put everything to 255 or 0xffU but still the icon has transparency. I also suspect this could be from imlib_image_set_alpha() which causes a doble layer or something?..

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

@explosion-mental
after applying both patches

  1. 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);
  1. change drw_img and blend function (drw.c) to
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) | ((~a * 255u + a * p1a) / 255u) << 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;
}

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

These modifications seem to work :D (at least on my laptop), adding the additional blend on alpha channel. This won't be a big deal if enabling -march=native and -O3, which automatically optimize it to use SIMD

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

some further optimizations:

inline static uint8_t div255(uint32_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;
}

from dwm-winicon.

explosion-mental avatar explosion-mental commented on July 21, 2024

Okay this is weird. Maybe I should have added images.
Before:

screen1

After the patches:

screen2

Did I miss something?

Edit: On the first image is where i applied you win-icon repo changes and for some reason the icon remain transparent. The second is after changing the code you send.

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

can you show me the DWM code of the second screenshots?

from dwm-winicon.

explosion-mental avatar explosion-mental commented on July 21, 2024

dwm.c
dwmC

drw.c
drawC

from dwm-winicon.

explosion-mental avatar explosion-mental commented on July 21, 2024

yikes I don't know why i did that. Thanks seems to be working fine!
icons

from dwm-winicon.

explosion-mental avatar explosion-mental commented on July 21, 2024

Yes works fine, thank you very much!

from dwm-winicon.

AdamYuan avatar AdamYuan commented on July 21, 2024

you're welcome :D
Also feel free to make an issue if you discover any bugs, crashes or memory leaks, since it's hard to cover all the situations by myself.

from dwm-winicon.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.