sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-netwmicon-0.8.5-v2.diff (4200B)


      1 From 8b0128e8b295fc97bfa3bc5fb4b5e64856d4e3cb Mon Sep 17 00:00:00 2001
      2 From: Aleksandrs Stier <aleks.stier@icloud.com>
      3 Date: Sat, 4 Jun 2022 01:24:07 +0200
      4 Subject: [PATCH] Set _NET_WM_ICON with a png-image
      5 
      6 ---
      7  Makefile  |  3 +++
      8  config.mk |  6 ++++--
      9  st.h      |  2 ++
     10  x.c       | 37 ++++++++++++++++++++++++++++++++++++-
     11  4 files changed, 45 insertions(+), 3 deletions(-)
     12 
     13 diff --git a/Makefile b/Makefile
     14 index 470ac86..96e27e3 100644
     15 --- a/Makefile
     16 +++ b/Makefile
     17 @@ -49,9 +49,12 @@ install: st
     18  	chmod 644 $(DESTDIR)$(MANPREFIX)/man1/st.1
     19  	tic -sx st.info
     20  	@echo Please see the README file regarding the terminfo entry of st.
     21 +	mkdir -p $(DESTDIR)$(ICONPREFIX)
     22 +	[ -f $(ICONNAME) ] && cp -f $(ICONNAME) $(DESTDIR)$(ICONPREFIX) || :
     23  
     24  uninstall:
     25  	rm -f $(DESTDIR)$(PREFIX)/bin/st
     26  	rm -f $(DESTDIR)$(MANPREFIX)/man1/st.1
     27 +	rm -f $(DESTDIR)$(ICONPREFIX)/$(ICONNAME)
     28  
     29  .PHONY: all options clean dist install uninstall
     30 diff --git a/config.mk b/config.mk
     31 index 4c4c5d5..f8fc780 100644
     32 --- a/config.mk
     33 +++ b/config.mk
     34 @@ -6,6 +6,8 @@ VERSION = 0.8.5
     35  # paths
     36  PREFIX = /usr/local
     37  MANPREFIX = $(PREFIX)/share/man
     38 +ICONPREFIX = $(PREFIX)/share/pixmaps
     39 +ICONNAME = st.png
     40  
     41  X11INC = /usr/X11R6/include
     42  X11LIB = /usr/X11R6/lib
     43 @@ -16,12 +18,12 @@ PKG_CONFIG = pkg-config
     44  INCS = -I$(X11INC) \
     45         `$(PKG_CONFIG) --cflags fontconfig` \
     46         `$(PKG_CONFIG) --cflags freetype2`
     47 -LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft \
     48 +LIBS = -L$(X11LIB) -lm -lrt -lX11 -lutil -lXft -lgd \
     49         `$(PKG_CONFIG) --libs fontconfig` \
     50         `$(PKG_CONFIG) --libs freetype2`
     51  
     52  # flags
     53 -STCPPFLAGS = -DVERSION=\"$(VERSION)\" -D_XOPEN_SOURCE=600
     54 +STCPPFLAGS = -DVERSION=\"$(VERSION)\" -DICON=\"$(ICONPREFIX)/$(ICONNAME)\" -D_XOPEN_SOURCE=600
     55  STCFLAGS = $(INCS) $(STCPPFLAGS) $(CPPFLAGS) $(CFLAGS)
     56  STLDFLAGS = $(LIBS) $(LDFLAGS)
     57  
     58 diff --git a/st.h b/st.h
     59 index 519b9bd..c10af86 100644
     60 --- a/st.h
     61 +++ b/st.h
     62 @@ -3,6 +3,8 @@
     63  #include <stdint.h>
     64  #include <sys/types.h>
     65  
     66 +#include <gd.h>
     67 +
     68  /* macros */
     69  #define MIN(a, b)		((a) < (b) ? (a) : (b))
     70  #define MAX(a, b)		((a) < (b) ? (b) : (a))
     71 diff --git a/x.c b/x.c
     72 index 8a16faa..169e833 100644
     73 --- a/x.c
     74 +++ b/x.c
     75 @@ -93,7 +93,7 @@ typedef struct {
     76  	Window win;
     77  	Drawable buf;
     78  	GlyphFontSpec *specbuf; /* font spec buffer used for rendering */
     79 -	Atom xembed, wmdeletewin, netwmname, netwmiconname, netwmpid;
     80 +	Atom xembed, wmdeletewin, netwmname, netwmicon, netwmiconname, netwmpid;
     81  	struct {
     82  		XIM xim;
     83  		XIC xic;
     84 @@ -1204,6 +1204,41 @@ xinit(int cols, int rows)
     85  	xw.netwmiconname = XInternAtom(xw.dpy, "_NET_WM_ICON_NAME", False);
     86  	XSetWMProtocols(xw.dpy, xw.win, &xw.wmdeletewin, 1);
     87  
     88 +	/* use a png-image to set _NET_WM_ICON */
     89 +	FILE* file = fopen(ICON, "r");
     90 +	if (file) {
     91 +		/* load image in rgba-format */
     92 +		const gdImagePtr icon_rgba = gdImageCreateFromPng(file);
     93 +		fclose(file);
     94 +		/* declare icon-variable which will store the image in argb-format */
     95 +		const int width  = gdImageSX(icon_rgba);
     96 +		const int height = gdImageSY(icon_rgba);
     97 +		const int icon_n = width * height + 2;
     98 +		long icon_argb[icon_n];
     99 +		/* set width and height of the icon */
    100 +		int i = 0;
    101 +		icon_argb[i++] = width;
    102 +		icon_argb[i++] = height;
    103 +		/* rgba -> argb */
    104 +		for (int y = 0; y < height; y++) {
    105 +			for (int x = 0; x < width; x++) {
    106 +				const int pixel_rgba = gdImageGetPixel(icon_rgba, x, y);
    107 +				unsigned char *pixel_argb = (unsigned char *) &icon_argb[i++];
    108 +				pixel_argb[0] = gdImageBlue(icon_rgba, pixel_rgba);
    109 +				pixel_argb[1] = gdImageGreen(icon_rgba, pixel_rgba);
    110 +				pixel_argb[2] = gdImageRed(icon_rgba, pixel_rgba);
    111 +				/* scale alpha from 0-127 to 0-255 */
    112 +				const unsigned char alpha = 127 - gdImageAlpha(icon_rgba, pixel_rgba);
    113 +				pixel_argb[3] = alpha == 127 ? 255 : alpha * 2;
    114 +			}
    115 +		}
    116 +		gdImageDestroy(icon_rgba);
    117 +		/* set _NET_WM_ICON */
    118 +		xw.netwmicon = XInternAtom(xw.dpy, "_NET_WM_ICON", False);
    119 +		XChangeProperty(xw.dpy, xw.win, xw.netwmicon, XA_CARDINAL, 32,
    120 +				PropModeReplace, (uchar *) icon_argb, icon_n);
    121 +	}
    122 +
    123  	xw.netwmpid = XInternAtom(xw.dpy, "_NET_WM_PID", False);
    124  	XChangeProperty(xw.dpy, xw.win, xw.netwmpid, XA_CARDINAL, 32,
    125  			PropModeReplace, (uchar *)&thispid, 1);
    126 -- 
    127 2.36.1
    128