]> git.draconx.ca Git - rrace.git/blob - src/ewmhicon.c
Performance improvements for runtime 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
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 static unsigned long scale16to8(unsigned x)
36 {
37         return x*0xfful / 0xffff;
38 }
39
40 static unsigned long wm_pixel(const XColor *c)
41 {
42         return 0xff000000
43              | scale16to8(c->red) << 16
44              | scale16to8(c->green) << 8
45              | scale16to8(c->blue);
46 }
47
48 static void do_remap(void *icon, unsigned area, const XColor *map)
49 {
50         unsigned char *index = icon;
51         unsigned long *argb = icon;
52         unsigned i;
53
54         area--;
55         for (i = 0; i <= area; i++) {
56                 argb[area-i] = wm_pixel(map + index[area-i]);
57         }
58 }
59
60 void *ewmh_icon_generate(const unsigned long *seq, const XColor *map)
61 {
62         static const unsigned char dims[ICON_MAX] = { 16, 24, 32, 48 };
63         unsigned long *work, *ret;
64         unsigned i, j, fullsize, size;
65
66         work = ret = malloc(sizeof *work * EWMH_ICON_NELEM);
67         if (!work)
68                 return NULL;
69
70         for (i = fullsize = 0; i < ICON_MAX; i++) {
71                 work += fullsize;
72
73                 size = dims[i];
74                 *work++ = size;
75                 *work++ = size;
76
77                 for (j = 0; j < 9; j++) {
78                         unsigned x = j%3, y = j/3;
79
80                         switch (i) {
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;
85                         }
86                 }
87
88                 fullsize = size*size;
89                 do_remap(work, fullsize, map);
90         }
91
92         return ret;
93 }
94
95 #if !X_DISPLAY_MISSING
96
97 /*
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.
101  */
102 int ewmh_probe_wm_icon(Widget shell)
103 {
104         Display *display = XtDisplay(shell);
105         Screen *screen = XtScreen(shell);
106         Window root = RootWindowOfScreen(screen);
107         Atom net_supported, net_wm_icon, type;
108
109         unsigned long offset = 0, i, nitems, bytes_after, *props;
110         unsigned char *prop_return;
111         int format;
112
113         net_wm_icon = XInternAtom(display, "_NET_WM_ICON", 0);
114         net_supported = XInternAtom(display, "_NET_SUPPORTED", 0);
115         do {
116                 XGetWindowProperty(display, root, net_supported, offset, 10,
117                                             0, XA_ATOM, &type, &format,
118                                             &nitems, &bytes_after,
119                                             &prop_return);
120
121                 if (format != 32 || type != XA_ATOM)
122                         break;
123                 offset += nitems;
124
125                 props = (void *)prop_return;
126                 for (i = 0; i < nitems; i++) {
127                         if (props[i] == net_wm_icon) {
128                                 XFree(props);
129                                 return 1;
130                         }
131                 }
132                 XFree(props);
133         } while (nitems > 0 && bytes_after > 0);
134
135         return 0;
136 }
137
138 #endif