st-netwmicon-0.8.5-v2.diff 4.1 KB

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