2 * _NET_WM_ICON helpers for slide puzzle game
3 * Copyright © 2022-2023 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/>.
25 #if !X_DISPLAY_MISSING
26 # include <X11/Intrinsic.h>
27 # include <X11/Xatom.h>
34 enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
36 static unsigned long scale16to8(uint_least32_t x)
38 return x*0xff / 0xffff;
41 static unsigned long wm_pixel(const XColor *c)
43 uint_least32_t t = -1;
45 t = (t << 8) + scale16to8(c->red);
46 t = (t << 8) + scale16to8(c->green);
47 t = (t << 8) + scale16to8(c->blue);
49 return t & 0xffffffff;
52 void ewmh_icon_prepare_cmap(XColor *map, unsigned n)
56 for (i = 0; i < n; i++) {
57 map[i].pixel = wm_pixel(&map[i]);
61 static void do_remap(void *icon, unsigned area, const XColor *map)
63 unsigned char *index = icon;
64 unsigned long *argb = icon;
68 for (i = 0; i <= area; i++) {
69 argb[area-i] = (map + index[area-i])->pixel;
73 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
75 static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
76 unsigned long *work, *ret;
77 unsigned i, j, fullsize, size;
79 work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
83 for (i = fullsize = 0; i < ICON_MAX; i++) {
90 for (j = 0; j < 9; j++) {
91 unsigned x = j%3, y = j/3;
94 case 0: icon_tile16((void *)work, seq[j], x, y); break;
95 case 1: icon_tile24((void *)work, seq[j], x, y); break;
96 case 2: icon_tile32((void *)work, seq[j], x, y); break;
97 case 3: icon_tile48((void *)work, seq[j], x, y); break;
101 fullsize = size*size;
102 do_remap(work, fullsize, map);
108 #if !X_DISPLAY_MISSING
111 * EWMH-supporting window managers that handle _NET_WM_ICON add this atom to
112 * the _NET_SUPPORTED list on the root window. Look for that and return 1
113 * if it is found, or 0 otherwise.
115 int ewmh_probe_wm_icon(Widget shell)
117 Display *display = XtDisplay(shell);
118 Screen *screen = XtScreen(shell);
119 Window root = RootWindowOfScreen(screen);
120 Atom net_supported, net_wm_icon, type;
122 unsigned long offset = 0, i, nitems, bytes_after, *props;
123 unsigned char *prop_return;
126 net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
127 net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
129 XGetWindowProperty(display, root, net_supported, offset, 10,
130 0, XA_ATOM, &type, &format,
131 &nitems, &bytes_after,
134 if (format != 32 || type != XA_ATOM)
138 props = (void *)prop_return;
139 for (i = 0; i < nitems; i++) {
140 if (props[i] == net_wm_icon) {
146 } while (nitems > 0 && bytes_after > 0);