]> git.draconx.ca Git - rrace.git/blob - src/ewmhicon.c
Allow compilation without X11.
[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 #include "ewmhicon.h"
29
30 static unsigned long scale16to8(unsigned x)
31 {
32         return x*0xfful / 0xffff;
33 }
34
35 static unsigned long wm_pixel(const XColor *c)
36 {
37         return 0xff000000
38              | scale16to8(c->red) << 16
39              | scale16to8(c->green) << 8
40              | scale16to8(c->blue);
41 }
42
43 /*
44  * The 16x16 icon is drawn with 1px shadow, 6x6 tiles, with 1 pixel cropped off
45  * all the edge tiles
46  */
47 void ewmh_tile16(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
48 {
49         int out_x, out_y, out_w, x, y;
50         unsigned long row[6];
51
52         out_x = tile_x * 11 / 2;
53         out_y = tile_y * 11 / 2;
54         out_w = (5 + (tile_x == 1)) * sizeof row[0];
55
56         for (y = 0+(tile_y == 0); y < 6-(tile_y == 2); y++) {
57                 for (x = 0+(tile_x == 0); x < 6-(tile_x==2); x++) {
58                         if (x == 0 || y == 0)
59                                 row[x] = wm_pixel(&c[COLOUR_LIGHT]);
60                         else if (x == 5 || y == 5)
61                                 row[x] = wm_pixel(&c[COLOUR_DARK]);
62                         else
63                                 row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
64                 }
65                 memcpy(&icon[16 * out_y++ + out_x], &row[tile_x == 0], out_w);
66         }
67 }
68
69 /*
70  * The 24x24 icon is drawn with 1px shadow and 8x8 tiles.
71  */
72 void ewmh_tile24(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
73 {
74         int out_x, out_y, x, y;
75         unsigned long row[8];
76
77         out_x = tile_x * 8;
78         out_y = tile_y * 8;
79
80         for (y = 0; y < 8; y++) {
81                 for (x = 0; x < 8; x++) {
82                         if (x == 0 || y == 0)
83                                 row[x] = wm_pixel(&c[COLOUR_LIGHT]);
84                         else if (x == 7 || y == 7)
85                                 row[x] = wm_pixel(&c[COLOUR_DARK]);
86                         else
87                                 row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
88                 }
89                 memcpy(&icon[24 * out_y++ + out_x], row, sizeof row);
90         }
91 }
92
93 /*
94  * The 32x32 icon is drawn with 1px shadow with slightly uneven tiles on
95  * an 11-10-11 pixel grid.
96  */
97 void ewmh_tile32(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
98 {
99         int out_x, out_y, out_w, out_h, x, y;
100         unsigned long row[11];
101
102         out_x = 10*tile_x + (tile_x > 0);
103         out_y = 10*tile_y + (tile_y > 0);
104         out_w = 10 + (tile_x != 1);
105         out_h = 10 + (tile_y != 1);
106
107         for (y = 0; y < out_h; y++) {
108                 for (x = 0; x < out_w; x++) {
109                         if (x == 0 || y == 0)
110                                 row[x] = wm_pixel(&c[COLOUR_LIGHT]);
111                         else if (x == out_w-1 || y == out_h-1)
112                                 row[x] = wm_pixel(&c[COLOUR_DARK]);
113                         else
114                                 row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
115                 }
116                 memcpy(&icon[32 * out_y++ + out_x], row, out_w * sizeof row[0]);
117         }
118 }
119
120 /*
121  * The 48x48 icon is drawn with 2px shadow and 16x16 tiles.
122  */
123 void ewmh_tile48(unsigned long *icon, const XColor *c, int tile_x, int tile_y)
124 {
125         int out_x, out_y, x, y;
126         unsigned long row[16];
127
128         out_x = tile_x * 16;
129         out_y = tile_y * 16;
130
131         for (y = 0; y < 16; y++) {
132                 for (x = 0; x < 16; x++) {
133                         if (x == 0 || y == 0)
134                                 row[x] = wm_pixel(&c[COLOUR_LIGHT]);
135                         else if (x == 15 || y == 15)
136                                 row[x] = wm_pixel(&c[COLOUR_DARK]);
137                         else if (x == 1 || y == 1)
138                                 row[x] = wm_pixel(&c[COLOUR_LIGHT]);
139                         else if (x == 14 || y == 14)
140                                 row[x] = wm_pixel(&c[COLOUR_DARK]);
141                         else
142                                 row[x] = wm_pixel(&c[COLOUR_PRIMARY]);
143                 }
144                 memcpy(&icon[48 * out_y++ + out_x], row, sizeof row);
145         }
146 }
147
148 void *ewmh_icon_alloc(unsigned long **sizes)
149 {
150         unsigned long *buf;
151
152         buf = calloc(sizeof *buf, EWMH_ICON_NELEM);
153         if (buf) {
154                 sizes[ICON_16x16] = buf;
155                 *sizes[ICON_16x16]++ = 16;
156                 *sizes[ICON_16x16]++ = 16;
157
158                 sizes[ICON_24x24] = sizes[ICON_16x16] + 16*16;
159                 *sizes[ICON_24x24]++ = 24;
160                 *sizes[ICON_24x24]++ = 24;
161
162                 sizes[ICON_32x32] = sizes[ICON_24x24] + 24*24;
163                 *sizes[ICON_32x32]++ = 32;
164                 *sizes[ICON_32x32]++ = 32;
165
166                 sizes[ICON_48x48] = sizes[ICON_32x32] + 32*32;
167                 *sizes[ICON_48x48]++ = 48;
168                 *sizes[ICON_48x48]++ = 48;
169         }
170
171         return buf;
172 }
173
174 #if !X_DISPLAY_MISSING
175
176 /*
177  * EWMH-supporting window managers that handle _NET_WM_ICON add this atom to
178  * the _NET_SUPPORTED list on the root window.  Look for that and return 1
179  * if it is found, or 0 otherwise.
180  */
181 int ewmh_probe_wm_icon(Widget shell)
182 {
183         Display *display = XtDisplay(shell);
184         Screen *screen = XtScreen(shell);
185         Window root = RootWindowOfScreen(screen);
186         Atom net_supported, net_wm_icon, type;
187
188         unsigned long offset = 0, i, nitems, bytes_after, *props;
189         unsigned char *prop_return;
190         int format;
191
192         net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
193         net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
194         do {
195                 XGetWindowProperty(display, root, net_supported, offset, 10,
196                                             0, XA_ATOM, &type, &format,
197                                             &nitems, &bytes_after,
198                                             &prop_return);
199
200                 if (format != 32 || type != XA_ATOM)
201                         break;
202                 offset += nitems;
203
204                 props = (void *)prop_return;
205                 for (i = 0; i < nitems; i++) {
206                         if (props[i] == net_wm_icon) {
207                                 return 1;
208                         }
209                 }
210         } while (nitems > 0 && bytes_after > 0);
211
212         return 0;
213 }
214
215 #endif