2 * _NET_WM_ICON helpers for slide puzzle game
3 * Copyright © 2022 Nick Bowler
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 #if !X_DISPLAY_MISSING
25 # include <X11/Intrinsic.h>
26 # include <X11/Xatom.h>
33 enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
36 * Allocate storage for the EWMH _NET_WM_ICON array. The sizes array is
37 * populated with pointers to the beginning of each icon's pixel data. For
38 * example, sizes[ICON_24x24] points to the first pixel of the 24x24 image.
40 * The returned value can then be passed to XChangeProperty to set the icon,
41 * (use EWMH_ICON_NELEM for the number of elements) and must be freed by the
44 static void *ewmh_icon_alloc(unsigned long **sizes)
48 buf = calloc(sizeof *buf, EWMH_ICON_NELEM);
50 sizes[ICON_16x16] = buf;
51 *sizes[ICON_16x16]++ = 16;
52 *sizes[ICON_16x16]++ = 16;
54 sizes[ICON_24x24] = sizes[ICON_16x16] + 16*16;
55 *sizes[ICON_24x24]++ = 24;
56 *sizes[ICON_24x24]++ = 24;
58 sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
59 *sizes[ICON_32x32]++ = 32;
60 *sizes[ICON_32x32]++ = 32;
62 sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
63 *sizes[ICON_48x48]++ = 48;
64 *sizes[ICON_48x48]++ = 48;
70 static unsigned long scale16to8(unsigned x)
72 return x*0xfful / 0xffff;
75 static unsigned long wm_pixel(const XColor *c)
78 | scale16to8(c->red) << 16
79 | scale16to8(c->green) << 8
80 | scale16to8(c->blue);
83 static void do_remap(void *icon, unsigned size, const XColor *map)
85 unsigned char *index = icon;
86 unsigned long *argb = icon;
87 unsigned i, count = size*size;
89 for (i = 0; i < count; i++) {
90 argb[count-i-1] = wm_pixel(map + index[count-i-1]);
94 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
96 unsigned long *buf, *sizes[ICON_MAX];
99 buf = ewmh_icon_alloc(sizes);
103 for (i = 0; i < 9; i++) {
104 unsigned x = i%3, y = i/3;
106 icon_tile16((void *)sizes[ICON_16x16], seq[i], x, y);
107 icon_tile24((void *)sizes[ICON_24x24], seq[i], x, y);
108 icon_tile32((void *)sizes[ICON_32x32], seq[i], x, y);
109 icon_tile48((void *)sizes[ICON_48x48], seq[i], x, y);
112 for (i = 0; i < ICON_MAX; i++) {
113 /* Produces the sequence 16, 24, 32, 48 */
114 unsigned size = 16 + (1u<<i)/2 * 8;
116 do_remap(sizes[i], size, map);
122 #if !X_DISPLAY_MISSING
125 * EWMH-supporting window managers that handle _NET_WM_ICON add this atom to
126 * the _NET_SUPPORTED list on the root window. Look for that and return 1
127 * if it is found, or 0 otherwise.
129 int ewmh_probe_wm_icon(Widget shell)
131 Display *display = XtDisplay(shell);
132 Screen *screen = XtScreen(shell);
133 Window root = RootWindowOfScreen(screen);
134 Atom net_supported, net_wm_icon, type;
136 unsigned long offset = 0, i, nitems, bytes_after, *props;
137 unsigned char *prop_return;
140 net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
141 net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
143 XGetWindowProperty(display, root, net_supported, offset, 10,
144 0, XA_ATOM, &type, &format,
145 &nitems, &bytes_after,
148 if (format != 32 || type != XA_ATOM)
152 props = (void *)prop_return;
153 for (i = 0; i < nitems; i++) {
154 if (props[i] == net_wm_icon) {
160 } while (nitems > 0 && bytes_after > 0);