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/>.
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 };
35 static unsigned long scale16to8(unsigned x)
37 return x*0xfful / 0xffff;
40 static unsigned long wm_pixel(const XColor *c)
43 | scale16to8(c->red) << 16
44 | scale16to8(c->green) << 8
45 | scale16to8(c->blue);
48 static void do_remap(void *icon, unsigned area, const XColor *map)
50 unsigned char *index = icon;
51 unsigned long *argb = icon;
55 for (i = 0; i <= area; i++) {
56 argb[area-i] = wm_pixel(map + index[area-i]);
60 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
62 static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
63 unsigned long *work, *ret;
64 unsigned i, j, fullsize, size;
66 work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
70 for (i = fullsize = 0; i < ICON_MAX; i++) {
77 for (j = 0; j < 9; j++) {
78 unsigned x = j%3, y = j/3;
81 case 0: icon_tile16((void *)work, seq[j], x, y); break;
82 case 1: icon_tile24((void *)work, seq[j], x, y); break;
83 case 2: icon_tile32((void *)work, seq[j], x, y); break;
84 case 3: icon_tile48((void *)work, seq[j], x, y); break;
89 do_remap(work, fullsize, map);
95 #if !X_DISPLAY_MISSING
98 * EWMH-supporting window managers that handle _NET_WM_ICON add this atom to
99 * the _NET_SUPPORTED list on the root window. Look for that and return 1
100 * if it is found, or 0 otherwise.
102 int ewmh_probe_wm_icon(Widget shell)
104 Display *display = XtDisplay(shell);
105 Screen *screen = XtScreen(shell);
106 Window root = RootWindowOfScreen(screen);
107 Atom net_supported, net_wm_icon, type;
109 unsigned long offset = 0, i, nitems, bytes_after, *props;
110 unsigned char *prop_return;
113 net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
114 net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
116 XGetWindowProperty(display, root, net_supported, offset, 10,
117 0, XA_ATOM, &type, &format,
118 &nitems, &bytes_after,
121 if (format != 32 || type != XA_ATOM)
125 props = (void *)prop_return;
126 for (i = 0; i < nitems; i++) {
127 if (props[i] == net_wm_icon) {
133 } while (nitems > 0 && bytes_after > 0);