st-flexipatch

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

netwmicon_ff.c (1277B)


      1 void
      2 setnetwmicon(void)
      3 {
      4 	/* use a farbfeld image to set _NET_WM_ICON */
      5 	FILE* file = fopen(ICON, "r");
      6 	if (file) {
      7 		unsigned char buf[16] = {0};
      8 
      9 		int hasdata = fread(buf,1,16,file);
     10 		if (memcmp(buf,"farbfeld",8)) {
     11 			fprintf(stderr,"netwmicon: file %s is not a farbfeld image\n", ICON);
     12 			fclose(file);
     13 			return;
     14 		}
     15 
     16 		/* declare icon-variable which will store the image in bgra-format */
     17 		const int width=(buf[8]<<24)|(buf[9]<<16)|(buf[10]<<8)|buf[11];
     18 		const int height=(buf[12]<<24)|(buf[13]<<16)|(buf[14]<<8)|buf[15];
     19 		const int icon_n = width * height + 2;
     20 		long icon_bgra[icon_n];
     21 
     22 		/* set width and height of the icon */
     23 		int i = 0;
     24 		icon_bgra[i++] = width;
     25 		icon_bgra[i++] = height;
     26 
     27 		/* rgba -> bgra */
     28 		for (int y = 0; y < height && hasdata; y++) {
     29 			for (int x = 0; x < width && hasdata; x++) {
     30 				unsigned char *pixel_bgra = (unsigned char *) &icon_bgra[i++];
     31 				hasdata = fread(buf,1,8,file);
     32 				pixel_bgra[0] = buf[4];
     33 				pixel_bgra[1] = buf[2];
     34 				pixel_bgra[2] = buf[0];
     35 				pixel_bgra[3] = buf[6];
     36 			}
     37 		}
     38 
     39 		/* set _NET_WM_ICON */
     40 		xw.netwmicon = XInternAtom(xw.dpy, "_NET_WM_ICON", False);
     41 		XChangeProperty(xw.dpy, xw.win, xw.netwmicon, XA_CARDINAL, 32,
     42 				PropModeReplace, (uchar *) icon_bgra, icon_n);
     43 
     44 		fclose(file);
     45 	}
     46 }