Create window from arbitrary pixmap as background

Lucas de Sena lucas at seninha.org
Sat Jul 20 17:58:58 UTC 2024


Hi, I am using libX11/Xlib C bindings to write a routine that, given two
arbitrary pixmaps (one of them is a bitmap mask), creates an icon window
with the pixmap as background pixmap and Xshape'd with the bitmap as
shape bounding.

First, I was using XCreateWindow(3) with a CWBackPixmap attribute.
However, I got BadMatch error from the CreateWindow request.  That is
probably because of window's and pixmap's unmatching depths.

Then, I tried to create the icon window using the pixmap's depth, but a
BadMatch error still occurs.  That is probably because the default visual
does not support the given depth.  If that is the case, I do not know how
to get a proper visual.

Here is my current (broken) solution in C99:

	Display *display;
	Pixmap icon, mask;
	Window win;
	unsigned int width, height, depth;
	char const **xpmdata;

	if (icon == None) {
		/* fallback to default icon; that works */
		XpmAttributes xpmattr = { 0 };
		int status;

		status = XpmCreatePixmapFromData(
			display, DefaultRootWindow(display),
			xpmdata, &icon, &mask, &xpmattr
		);
		if (status != XpmSuccess || icon == None)
			return None;
		width = xpmattr.width;
		height = xpmattr.height;
		depth = xpmattr.depth;
	} else {
		Status success;

		success = XGetGeometry(
			display, icon, &(Window){0}, &(int){0},
			&width, &height, &(unsigned){0}, &depth
		);
		if (!success)
			return None;
	}
	win = XCreateWindow(
		display, root, 0, 0, width, height, 0,
		depth, InputOutput, CopyFromParent,
		CWBackPixmap | CWOverrideRedirect,
		&(XSetWindowAttributes){
			.background_pixmap = icon,
			.override_redirect = True,
		}
	);
	XShapeCombineMask(display, win, ShapeBounding, 0, 0, mask, ShapeSet);
	XFreePixmap(display, icon);
	XFreePixmap(display, mask);
	return win;
	
What is the proper solution?

I checked how a few iconifying window managers deal with clients
providing icon pixmap and mask on XWMHints(3).  But as far as I could
understand, fvwm and twm ignores pixmaps with non-default depths; and
windowmaker creates an XImage from the pixmap and then creates a
default-depth pixmap back from it.  Is the latter approach the most
correct (or only) one?

-- 
Lucas de Sena


More information about the xorg mailing list