Remove Xfuncproto.h and XKB.h from xkbcommon/xkbcommon.h
[platform/upstream/libxkbcommon.git] / src / keysym.c
1 /*
2 Copyright 1985, 1987, 1990, 1998  The Open Group
3 Copyright 2008  Dan Nicholson
4
5 Permission is hereby granted, free of charge, to any person obtaining a
6 copy of this software and associated documentation files (the "Software"),
7 to deal in the Software without restriction, including without limitation
8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 and/or sell copies of the Software, and to permit persons to whom the
10 Software is furnished to do so, subject to the following conditions:
11
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
19 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the names of the authors or their
23 institutions shall not be used in advertising or otherwise to promote the
24 sale, use or other dealings in this Software without prior written
25 authorization from the authors.
26 */
27
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <X11/keysymdef.h>
35
36 #include "ks_tables.h"
37 #include "xkbmisc.h"
38 #include "xkbcommon/xkbcommon.h"
39
40 _X_EXPORT void
41 xkb_keysym_to_string(xkb_keysym_t ks, char *buffer, size_t size)
42 {
43     int i, n, h, idx;
44     const unsigned char *entry;
45     unsigned char val1, val2, val3, val4;
46
47     if ((ks & ((unsigned long) ~0x1fffffff)) != 0) {
48         snprintf(buffer, size, "Invalid");
49         return;
50     }
51
52     /* Not listed in keysymdef.h for hysterical raisins. */
53     if (ks == XKB_KEYSYM_NO_SYMBOL) {
54         snprintf(buffer, size, "NoSymbol");
55         return;
56     }
57
58     /* Try to find it in our hash table. */
59     if (ks <= 0x1fffffff) {
60         val1 = ks >> 24;
61         val2 = (ks >> 16) & 0xff;
62         val3 = (ks >> 8) & 0xff;
63         val4 = ks & 0xff;
64         i = ks % VTABLESIZE;
65         h = i + 1;
66         n = VMAXHASH;
67
68         while ((idx = hashKeysym[i])) {
69             entry = &_XkeyTable[idx];
70
71             if ((entry[0] == val1) && (entry[1] == val2) &&
72                 (entry[2] == val3) && (entry[3] == val4)) {
73                 snprintf(buffer, size, "%s", entry + 4);
74                 return;
75             }
76
77             if (!--n)
78                 break;
79
80             i += h;
81             if (i >= VTABLESIZE)
82                 i -= VTABLESIZE;
83         }
84     }
85
86     if (ks >= 0x01000100 && ks <= 0x0110ffff)
87         /* Unnamed Unicode codepoint. */
88         snprintf(buffer, size, "U%lx", ks & 0xffffffUL);
89     else
90         /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
91         snprintf(buffer, size, "0x%08x", ks);
92 }
93
94 _X_EXPORT xkb_keysym_t
95 xkb_string_to_keysym(const char *s)
96 {
97     int i, n, h, c, idx;
98     uint32_t sig = 0;
99     const char *p = s;
100     char *tmp;
101     const unsigned char *entry;
102     unsigned char sig1, sig2;
103     xkb_keysym_t val;
104
105     while ((c = *p++))
106         sig = (sig << 1) + c;
107
108     i = sig % KTABLESIZE;
109     h = i + 1;
110     sig1 = (sig >> 8) & 0xff;
111     sig2 = sig & 0xff;
112     n = KMAXHASH;
113
114     while ((idx = hashString[i])) {
115         entry = &_XkeyTable[idx];
116
117         if ((entry[0] == sig1) && (entry[1] == sig2) &&
118             !strcmp(s, (const char *)entry + 6))
119         {
120             val = (entry[2] << 24) | (entry[3] << 16) |
121                   (entry[4] << 8)  | entry[5];
122             if (!val)
123                 val = XK_VoidSymbol;
124             return val;
125         }
126
127         if (!--n)
128             break;
129
130         i += h;
131         if (i >= KTABLESIZE)
132             i -= KTABLESIZE;
133     }
134
135     if (*s == 'U') {
136         val = strtoul(&s[1], &tmp, 16);
137         if (tmp && *tmp != '\0')
138             return XKB_KEYSYM_NO_SYMBOL;
139
140         if (val < 0x20 || (val > 0x7e && val < 0xa0))
141             return XKB_KEYSYM_NO_SYMBOL;
142         if (val < 0x100)
143             return val;
144         if (val > 0x10ffff)
145             return XKB_KEYSYM_NO_SYMBOL;
146         return val | 0x01000000;
147     }
148     else if (s[0] == '0' && s[1] == 'x') {
149         val = strtoul(&s[2], &tmp, 16);
150         if (tmp && *tmp != '\0')
151             return XKB_KEYSYM_NO_SYMBOL;
152
153         return val;
154     }
155
156     /* Stupid inconsistency between the headers and XKeysymDB: the former has
157      * no separating underscore, while some XF86* syms in the latter did.
158      * As a last ditch effort, try without. */
159     if (strncmp(s, "XF86_", 5) == 0) {
160         xkb_keysym_t ret;
161         tmp = strdup(s);
162         if (!tmp)
163             return XKB_KEYSYM_NO_SYMBOL;
164         memmove(&tmp[4], &tmp[5], strlen(s) - 5 + 1);
165         ret = xkb_string_to_keysym(tmp);
166         free(tmp);
167         return ret;
168     }
169
170     return XKB_KEYSYM_NO_SYMBOL;
171 }