]> git.draconx.ca Git - rrace.git/blob - src/ewmhicon.c
Separate EWMH colour conversion from icon generation.
[rrace.git] / src / ewmhicon.c
1 /*
2  * _NET_WM_ICON helpers for slide puzzle game
3  * Copyright © 2022-2023 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 #include <stdint.h>
24
25 #if !X_DISPLAY_MISSING
26 #  include <X11/Intrinsic.h>
27 #  include <X11/Xatom.h>
28 #endif
29
30 #include "ewmhicon.h"
31 #include "colour.h"
32 #include "icon.h"
33
34 enum { ICON_16x16, ICON_24x24, ICON_32x32, ICON_48x48, ICON_MAX };
35
36 static unsigned long scale16to8(uint_least32_t x)
37 {
38         return x*0xff / 0xffff;
39 }
40
41 static unsigned long wm_pixel(const XColor *c)
42 {
43         uint_least32_t t = -1;
44
45         t = (t << 8) + scale16to8(c->red);
46         t = (t << 8) + scale16to8(c->green);
47         t = (t << 8) + scale16to8(c->blue);
48
49         return t & 0xffffffff;
50 }
51
52 void ewmh_icon_prepare_cmap(XColor *map, unsigned n)
53 {
54         unsigned i;
55
56         for (i = 0; i < n; i++) {
57                 map[i].pixel = wm_pixel(&map[i]);
58         }
59 }
60
61 static void do_remap(void *icon, unsigned area, const XColor *map)
62 {
63         unsigned char *index = icon;
64         unsigned long *argb = icon;
65         unsigned i;
66
67         area--;
68         for (i = 0; i <= area; i++) {
69                 argb[area-i] = (map + index[area-i])->pixel;
70         }
71 }
72
73 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
74 {
75         static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
76         unsigned long *work, *ret;
77         unsigned i, j, fullsize, size;
78
79         work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
80         if (!work)
81                 return NULL;
82
83         for (i = fullsize = 0; i < ICON_MAX; i++) {
84                 work += fullsize;
85
86                 size = dims[i];
87                 *work++ = size;
88                 *work++ = size;
89
90                 for (j = 0; j < 9; j++) {
91                         unsigned x = j%3, y = j/3;
92
93                         switch (i) {
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;
98                         }
99                 }
100
101                 fullsize = size*size;
102                 do_remap(work, fullsize, map);
103         }
104
105         return ret;
106 }
107
108 #if !X_DISPLAY_MISSING
109
110 /*
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.
114  */
115 int ewmh_probe_wm_icon(Widget shell)
116 {
117         Display *display = XtDisplay(shell);
118         Screen *screen = XtScreen(shell);
119         Window root = RootWindowOfScreen(screen);
120         Atom net_supported, net_wm_icon, type;
121
122         unsigned long offset = 0, i, nitems, bytes_after, *props;
123         unsigned char *prop_return;
124         int format;
125
126         net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
127         net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
128         do {
129                 XGetWindowProperty(display, root, net_supported, offset, 10,
130                                             0, XA_ATOM, &type, &format,
131                                             &nitems, &bytes_after,
132                                             &prop_return);
133
134                 if (format != 32 || type != XA_ATOM)
135                         break;
136                 offset += nitems;
137
138                 props = (void *)prop_return;
139                 for (i = 0; i < nitems; i++) {
140                         if (props[i] == net_wm_icon) {
141                                 XFree(props);
142                                 return 1;
143                         }
144                 }
145                 XFree(props);
146         } while (nitems > 0 && bytes_after > 0);
147
148         return 0;
149 }
150
151 #endif