st-flexipatch

My st-flexipatch configuration
git clone git://git.ethandl.dev/st-flexipatch
Log | Files | Refs | README | LICENSE

netwmicon.c (1391B)


      1 #include <gd.h>
      2 
      3 void
      4 setnetwmicon(void)
      5 {
      6 	/* use a png-image to set _NET_WM_ICON */
      7 	FILE* file = fopen(ICON, "r");
      8 	if (file) {
      9 		/* load image in rgba-format */
     10 		const gdImagePtr icon_rgba = gdImageCreateFromPng(file);
     11 		fclose(file);
     12 		/* declare icon-variable which will store the image in bgra-format */
     13 		const int width  = gdImageSX(icon_rgba);
     14 		const int height = gdImageSY(icon_rgba);
     15 		const int icon_n = width * height + 2;
     16 		long icon_bgra[icon_n];
     17 		/* set width and height of the icon */
     18 		int i = 0;
     19 		icon_bgra[i++] = width;
     20 		icon_bgra[i++] = height;
     21 		/* rgba -> bgra */
     22 		for (int y = 0; y < height; y++) {
     23 			for (int x = 0; x < width; x++) {
     24 				const int pixel_rgba = gdImageGetPixel(icon_rgba, x, y);
     25 				unsigned char *pixel_bgra = (unsigned char *) &icon_bgra[i++];
     26 				pixel_bgra[0] = gdImageBlue(icon_rgba, pixel_rgba);
     27 				pixel_bgra[1] = gdImageGreen(icon_rgba, pixel_rgba);
     28 				pixel_bgra[2] = gdImageRed(icon_rgba, pixel_rgba);
     29 				/* scale alpha from 0-127 to 0-255 */
     30 				const unsigned char alpha = 127 - gdImageAlpha(icon_rgba, pixel_rgba);
     31 				pixel_bgra[3] = alpha == 127 ? 255 : alpha * 2;
     32 			}
     33 		}
     34 		gdImageDestroy(icon_rgba);
     35 		/* set _NET_WM_ICON */
     36 		xw.netwmicon = XInternAtom(xw.dpy, "_NET_WM_ICON", False);
     37 		XChangeProperty(xw.dpy, xw.win, xw.netwmicon, XA_CARDINAL, 32,
     38 		                PropModeReplace, (uchar *) icon_bgra, icon_n);
     39 	}
     40 }