3 * Copyright 1990, 1998 The Open Group
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of The Open Group shall
23 * not be used in advertising or otherwise to promote the sale, use or
24 * other dealings in this Software without prior written authorization
25 * from The Open Group.
30 * Constructs hash tables for xkb_keysym_to_string and
31 * xkb_string_from_keysym.
34 #include "xkbcommon/xkbcommon.h"
41 typedef uint32_t Signature;
53 static char tab[KTNUM];
54 static unsigned short offsets[KTNUM];
55 static unsigned short indexes[KTNUM];
56 static xkb_keysym_t values[KTNUM];
60 parse_line(const char *buf, char *key, xkb_keysym_t *val, char *prefix)
65 /* See if we can catch a straight XK_foo 0x1234-style definition first;
66 * the trickery around tmp is to account for prefices. */
67 i = sscanf(buf, "#define %127s 0x%" SCNx32, key, val);
68 if (i == 2 && strncmp(key, "XKB_KEY_", 8) == 0) {
70 memmove(key, key + 8, strlen(key + 8) + 1);
74 i = sscanf(buf, "#define %127s %127s", key, alias);
76 fprintf(stderr, "can't parse keysym definition: %s", buf);
82 main(int argc, char *argv[])
95 char key[128], prefix[128];
98 for (l = 1; l < argc; l++) {
99 fptr = fopen(argv[l], "r");
101 fprintf(stderr, "couldn't open %s\n", argv[l]);
105 while (fgets(buf, sizeof(buf), fptr)) {
106 if (!parse_line(buf, key, &val, prefix))
109 if (val == XKB_KEY_VoidSymbol)
111 if (val > 0x1fffffff) {
112 fprintf(stderr, "ignoring illegal keysym (%s, %" PRIx32 ")\n",
118 name = malloc(strlen(prefix) + strlen(key) + 1);
120 fprintf(stderr, "makekeys: out of memory!\n");
123 sprintf(name, "%s%s", prefix, key);
124 info[ksnum].name = name;
125 info[ksnum].val = val;
127 if (ksnum == KTNUM) {
128 fprintf(stderr, "makekeys: too many keysyms!\n");
136 printf("/* This file is generated from keysymdef.h. */\n");
137 printf("/* Do not edit. */\n");
140 best_max_rehash = ksnum;
142 for (z = ksnum; z < KTNUM; z++) {
144 for (name = tab, i = z; --i >= 0; )
146 for (i = 0; i < ksnum; i++) {
149 while ((c = *name++))
150 sig = (sig << 1) + c;
152 for (k = 0; tab[j]; k++) {
163 if (max_rehash < MIN_REHASH) {
164 if (max_rehash < best_max_rehash) {
165 best_max_rehash = max_rehash;
169 if (num_found >= MATCHES)
176 printf("#ifndef KS_TABLES_H\n");
177 printf("#define KS_TABLES_H\n\n");
178 printf("static const unsigned char _XkeyTable[] = {\n");
180 fprintf(stderr, "makekeys: failed to find small enough hash!\n"
181 "Try increasing KTNUM in makekeys.c\n");
186 for (i = 0; i < ksnum; i++) {
189 while ((c = *name++))
190 sig = (sig << 1) + c;
200 printf("0x%.2" PRIx32 ", 0x%.2" PRIx32 ", 0x%.2" PRIx32 ", "
201 "0x%.2" PRIx32 ", 0x%.2" PRIx32 ", 0x%.2" PRIx32 ", ",
202 (sig >> 8) & 0xff, sig & 0xff, (val >> 24) & 0xff,
203 (val >> 16) & 0xff, (val >> 8) & 0xff, val & 0xff);
204 for (name = info[i].name, k += 7; (c = *name++); k++)
206 printf((i == (ksnum - 1)) ? "0\n" : "0,\n");
210 printf("#define KTABLESIZE %d\n", z);
211 printf("#define KMAXHASH %d\n", best_max_rehash + 1);
213 printf("static const unsigned short hashString[KTABLESIZE] = {\n");
214 for (i = 0; i < z; ) {
215 printf("0x%.4x", offsets[i]);
219 printf((i & 7) ? ", " : ",\n");
224 best_max_rehash = ksnum;
226 for (z = ksnum; z < KTNUM; z++) {
228 for (name = tab, i = z; --i >= 0; )
230 for (i = 0; i < ksnum; i++) {
233 for (k = 0; tab[j]; k++) {
234 if (values[j] == val)
248 if (max_rehash < MIN_REHASH) {
249 if (max_rehash < best_max_rehash) {
250 best_max_rehash = max_rehash;
254 if (num_found >= MATCHES)
262 fprintf(stderr, "makekeys: failed to find small enough hash!\n"
263 "Try increasing KTNUM in makekeys.c\n");
266 for (i = z; --i >= 0; )
268 for (i = 0; i < ksnum; i++) {
272 if (values[j] == val)
278 offsets[j] = indexes[i] + 2;
283 printf("#define VTABLESIZE %d\n", z);
284 printf("#define VMAXHASH %d\n", best_max_rehash + 1);
286 printf("static const unsigned short hashKeysym[VTABLESIZE] = {\n");
287 for (i = 0; i < z; ) {
288 printf("0x%.4x", offsets[i]);
292 printf((i & 7) ? ", " : ",\n");
296 printf("\n#endif /* KS_TABLES_H */\n");
298 for (i = 0; i < ksnum; i++)