]> git.draconx.ca Git - rrace.git/blob - src/ewmhicon.c
Make EWMH icon generation more abstract.
[rrace.git] / src / ewmhicon.c
1 /*
2  * _NET_WM_ICON helpers for slide puzzle game
3  * Copyright © 2022 Nick Bowler
4  *
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.
9  *
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.
14  *
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/>.
17  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #if !X_DISPLAY_MISSING
25 #  include <X11/Intrinsic.h>
26 #  include <X11/Xatom.h>
27 #endif
28
29 #include "ewmhicon.h"
30 #include "colour.h"
31 #include "icon.h"
32
33 enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
34
35 /*
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.
39  *
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
42  * caller.
43  */
44 static void *ewmh_icon_alloc(unsigned long **sizes)
45 {
46         unsigned long *buf;
47
48         buf = calloc(sizeof *buf, EWMH_ICON_NELEM);
49         if (buf) {
50                 sizes[ICON_16x16] = buf;
51                 *sizes[ICON_16x16]++ = 16;
52                 *sizes[ICON_16x16]++ = 16;
53
54                 sizes[ICON_24x24] = sizes[ICON_16x16] + 16*16;
55                 *sizes[ICON_24x24]++ = 24;
56                 *sizes[ICON_24x24]++ = 24;
57
58                 sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
59                 *sizes[ICON_32x32]++ = 32;
60                 *sizes[ICON_32x32]++ = 32;
61
62                 sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
63                 *sizes[ICON_48x48]++ = 48;
64                 *sizes[ICON_48x48]++ = 48;
65         }
66
67         return buf;
68 }
69
70 static unsigned long scale16to8(unsigned x)
71 {
72         return x*0xfful / 0xffff;
73 }
74
75 static unsigned long wm_pixel(const XColor *c)
76 {
77         return 0xff000000
78              | scale16to8(c->red) << 16
79              | scale16to8(c->green) << 8
80              | scale16to8(c->blue);
81 }
82
83 static void do_remap(void *icon, unsigned size, const XColor *map)
84 {
85         unsigned char *index = icon;
86         unsigned long *argb = icon;
87         unsigned i, count = size*size;
88
89         for (i = 0; i < count; i++) {
90                 argb[count-i-1] = wm_pixel(map + index[count-i-1]);
91         }
92 }
93
94 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
95 {
96         unsigned long *buf, *sizes[ICON_MAX];
97         unsigned i, j;
98
99         buf = ewmh_icon_alloc(sizes);
100         if (!buf)
101                 return NULL;
102
103         for (i = 0; i < 9; i++) {
104                 unsigned x = i%3, y = i/3;
105
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);
110         }
111
112         for (i = 0; i < ICON_MAX; i++) {
113                 /* Produces the sequence 16, 24, 32, 48 */
114                 unsigned size = 16 + (1u<<i)/2 * 8;
115
116                 do_remap(sizes[i], size, map);
117         }
118
119         return buf;
120 }
121
122 #if !X_DISPLAY_MISSING
123
124 /*
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.
128  */
129 int ewmh_probe_wm_icon(Widget shell)
130 {
131         Display *display = XtDisplay(shell);
132         Screen *screen = XtScreen(shell);
133         Window root = RootWindowOfScreen(screen);
134         Atom net_supported, net_wm_icon, type;
135
136         unsigned long offset = 0, i, nitems, bytes_after, *props;
137         unsigned char *prop_return;
138         int format;
139
140         net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
141         net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
142         do {
143                 XGetWindowProperty(display, root, net_supported, offset, 10,
144                                             0, XA_ATOM, &type, &format,
145                                             &nitems, &bytes_after,
146                                             &prop_return);
147
148                 if (format != 32 || type != XA_ATOM)
149                         break;
150                 offset += nitems;
151
152                 props = (void *)prop_return;
153                 for (i = 0; i < nitems; i++) {
154                         if (props[i] == net_wm_icon) {
155                                 return 1;
156                         }
157                 }
158         } while (nitems > 0 && bytes_after > 0);
159
160         return 0;
161 }
162
163 #endif