aboutsummaryrefslogtreecommitdiff
path: root/keep/manual-programs/suckless/st-flexipatch/patch/netwmicon.c
blob: 6ae0533611954a1b72652acec120dbbba32375cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <gd.h>

void
setnetwmicon(void)
{
	/* use a png-image to set _NET_WM_ICON */
	FILE* file = fopen(ICON, "r");
	if (file) {
		/* load image in rgba-format */
		const gdImagePtr icon_rgba = gdImageCreateFromPng(file);
		fclose(file);
		/* declare icon-variable which will store the image in bgra-format */
		const int width  = gdImageSX(icon_rgba);
		const int height = gdImageSY(icon_rgba);
		const int icon_n = width * height + 2;
		long icon_bgra[icon_n];
		/* set width and height of the icon */
		int i = 0;
		icon_bgra[i++] = width;
		icon_bgra[i++] = height;
		/* rgba -> bgra */
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				const int pixel_rgba = gdImageGetPixel(icon_rgba, x, y);
				unsigned char *pixel_bgra = (unsigned char *) &icon_bgra[i++];
				pixel_bgra[0] = gdImageBlue(icon_rgba, pixel_rgba);
				pixel_bgra[1] = gdImageGreen(icon_rgba, pixel_rgba);
				pixel_bgra[2] = gdImageRed(icon_rgba, pixel_rgba);
				/* scale alpha from 0-127 to 0-255 */
				const unsigned char alpha = 127 - gdImageAlpha(icon_rgba, pixel_rgba);
				pixel_bgra[3] = alpha == 127 ? 255 : alpha * 2;
			}
		}
		gdImageDestroy(icon_rgba);
		/* set _NET_WM_ICON */
		xw.netwmicon = XInternAtom(xw.dpy, "_NET_WM_ICON", False);
		XChangeProperty(xw.dpy, xw.win, xw.netwmicon, XA_CARDINAL, 32,
		                PropModeReplace, (uchar *) icon_bgra, icon_n);
	}
}