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