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
29 /* Constructs hash tables for XStringToKeysym and XKeysymToString. */
33 #include <X11/keysymdef.h>
38 typedef uint32_t Signature;
50 static char tab[KTNUM];
51 static unsigned short offsets[KTNUM];
52 static unsigned short indexes[KTNUM];
53 static KeySym values[KTNUM];
56 * XFree86 special action keys - for some reason, these have an
57 * underscore after the XF86 prefix.
59 static const char *xf86_special_keys[] = {
80 is_xf86_special(const char *key)
82 const char **s = xf86_special_keys;
84 if (strcmp(key, *s) == 0)
92 get_keysym(const char *buf, char *key, int ndx)
94 if (sscanf(buf, "#define XK_%127s 0x%lx", key, &info[ndx].val) != 2)
100 get_keysym_alias(const char *buf, char *key, int ndx)
105 if (sscanf(buf, "#define XK_%127s XK_%127s", key, alias) != 2)
108 for (i = ndx - 1; i >= 0; i--) {
109 if (strcmp(info[i].name, alias) == 0) {
110 info[ndx].val = info[i].val;
115 /* Didn't find a match */
116 fprintf(stderr, "can't find matching definition %s for keysym %s\n",
122 get_xf86_keysym(const char *buf, char *key, size_t len, int ndx)
126 if (sscanf(buf, "#define XF86XK_%127s 0x%lx", tmp, &info[ndx].val) != 2)
129 /* Prepend XF86 or XF86_ to the key */
130 snprintf(key, len, "XF86%s%s", is_xf86_special(tmp) ? "_" : "", tmp);
136 get_xf86_keysym_alias(const char *buf, char *key, size_t len, int ndx)
139 char alias[128], ktmp[128], atmp[128];
141 /* Try to handle both XF86XK and XK aliases */
142 if (sscanf(buf, "#define XF86XK_%127s XF86XK_%127s", ktmp, atmp) == 2) {
143 /* Prepend XF86 to the key and alias */
144 snprintf(key, len, "XF86%s%s", is_xf86_special(ktmp) ? "_" : "",
146 snprintf(alias, sizeof(alias), "XF86%s%s",
147 is_xf86_special(atmp) ? "_" : "", atmp);
149 if (sscanf(buf, "#define XF86XK_%127s XK_%127s", ktmp, alias) != 2)
151 /* Prepend XF86 to the key */
152 snprintf(key, len, "XF86%s%s", is_xf86_special(ktmp) ? "_" : "",
156 for (i = ndx - 1; i >= 0; i--) {
157 if (strcmp(info[i].name, alias) == 0) {
158 info[ndx].val = info[i].val;
163 /* Didn't find a match */
164 fprintf(stderr, "can't find matching definition %s for keysym %s\n",
170 main(int argc, char *argv[])
187 for (l = 1; l < argc; l++) {
188 fptr = fopen(argv[l], "r");
190 fprintf(stderr, "couldn't open %s\n", argv[l]);
194 while (fgets(buf, sizeof(buf), fptr)) {
197 /* Manage keysyms from keysymdef.h */
198 ret = get_keysym(buf, key, ksnum);
200 ret = get_keysym_alias(buf, key, ksnum);
205 /* Manage keysyms from XF86keysym.h */
207 ret = get_xf86_keysym(buf, key, sizeof(key), ksnum);
209 ret = get_xf86_keysym_alias(buf, key, sizeof(key), ksnum);
214 if (info[ksnum].val > 0x1fffffff) {
216 "ignoring illegal keysym (%s), remove it from .h file!\n",
220 name = malloc((unsigned)strlen(key) + 1);
222 fprintf(stderr, "makekeys: out of memory!\n");
225 (void)strcpy(name, key);
226 info[ksnum].name = name;
228 if (ksnum == KTNUM) {
229 fprintf(stderr, "makekeys: too many keysyms!\n");
237 /* Special case NoSymbol. */
238 info[ksnum].name = strdup("NoSymbol");
239 info[ksnum].val = 0L;
242 printf("/* This file is generated from keysymdef.h. */\n");
243 printf("/* Do not edit. */\n");
246 best_max_rehash = ksnum;
248 for (z = ksnum; z < KTNUM; z++) {
250 for (name = tab, i = z; --i >= 0;)
252 for (i = 0; i < ksnum; i++) {
255 while ((c = *name++))
256 sig = (sig << 1) + c;
258 for (k = 0; tab[j]; k++) {
269 if (max_rehash < MIN_REHASH) {
270 if (max_rehash < best_max_rehash) {
271 best_max_rehash = max_rehash;
275 if (num_found >= MATCHES)
282 printf("#ifndef KS_TABLES_H\n");
283 printf("#define KS_TABLES_H\n\n");
284 printf("static const unsigned char _XkeyTable[] = {\n");
286 fprintf(stderr, "makekeys: failed to find small enough hash!\n"
287 "Try increasing KTNUM in makekeys.c\n");
292 for (i = 0; i < ksnum; i++) {
295 while ((c = *name++))
296 sig = (sig << 1) + c;
306 printf("0x%.2"PRIx32", 0x%.2"PRIx32", 0x%.2lx, 0x%.2lx, 0x%.2lx, 0x%.2lx, ",
307 (sig >> 8) & 0xff, sig & 0xff,
308 (val >> 24) & 0xff, (val >> 16) & 0xff,
309 (val >> 8) & 0xff, val & 0xff);
310 for (name = info[i].name, k += 7; (c = *name++); k++)
312 printf((i == (ksnum-1)) ? "0\n" : "0,\n");
316 printf("#define KTABLESIZE %d\n", z);
317 printf("#define KMAXHASH %d\n", best_max_rehash + 1);
319 printf("static const unsigned short hashString[KTABLESIZE] = {\n");
320 for (i = 0; i < z;) {
321 printf("0x%.4x", offsets[i]);
325 printf((i & 7) ? ", " : ",\n");
330 best_max_rehash = ksnum;
332 for (z = ksnum; z < KTNUM; z++) {
334 for (name = tab, i = z; --i >= 0;)
336 for (i = 0; i < ksnum; i++) {
339 for (k = 0; tab[j]; k++) {
340 if (values[j] == val)
354 if (max_rehash < MIN_REHASH) {
355 if (max_rehash < best_max_rehash) {
356 best_max_rehash = max_rehash;
360 if (num_found >= MATCHES)
368 fprintf(stderr, "makekeys: failed to find small enough hash!\n"
369 "Try increasing KTNUM in makekeys.c\n");
372 for (i = z; --i >= 0;)
374 for (i = 0; i < ksnum; i++) {
378 if (values[j] == val)
384 offsets[j] = indexes[i] + 2;
389 printf("#define VTABLESIZE %d\n", z);
390 printf("#define VMAXHASH %d\n", best_max_rehash + 1);
392 printf("static const unsigned short hashKeysym[VTABLESIZE] = {\n");
393 for (i = 0; i < z;) {
394 printf("0x%.4x", offsets[i]);
398 printf((i & 7) ? ", " : ",\n");
402 printf("\n#endif /* KS_TABLES_H */\n");