Rename XKBcommonint.h to xkb-priv.h and use it
[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 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <X11/keysym.h>
32
33 #include "xkb-priv.h"
34 #include "ks_tables.h"
35
36 _X_EXPORT void
37 xkb_keysym_to_string(xkb_keysym_t ks, char *buffer, size_t size)
38 {
39     int i, n, h, idx;
40     const unsigned char *entry;
41     unsigned char val1, val2, val3, val4;
42
43     if ((ks & ((unsigned long) ~0x1fffffff)) != 0) {
44         snprintf(buffer, size, "Invalid");
45         return;
46     }
47
48     /* Not listed in keysymdef.h for hysterical raisins. */
49     if (ks == XKB_KEYSYM_NO_SYMBOL) {
50         snprintf(buffer, size, "NoSymbol");
51         return;
52     }
53
54     /* Try to find it in our hash table. */
55     if (ks <= 0x1fffffff) {
56         val1 = ks >> 24;
57         val2 = (ks >> 16) & 0xff;
58         val3 = (ks >> 8) & 0xff;
59         val4 = ks & 0xff;
60         i = ks % VTABLESIZE;
61         h = i + 1;
62         n = VMAXHASH;
63
64         while ((idx = hashKeysym[i])) {
65             entry = &_XkeyTable[idx];
66
67             if ((entry[0] == val1) && (entry[1] == val2) &&
68                 (entry[2] == val3) && (entry[3] == val4)) {
69                 snprintf(buffer, size, "%s", entry + 4);
70                 return;
71             }
72
73             if (!--n)
74                 break;
75
76             i += h;
77             if (i >= VTABLESIZE)
78                 i -= VTABLESIZE;
79         }
80     }
81
82     if (ks >= 0x01000100 && ks <= 0x0110ffff)
83         /* Unnamed Unicode codepoint. */
84         snprintf(buffer, size, "U%lx", ks & 0xffffffUL);
85     else
86         /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
87         snprintf(buffer, size, "0x%08x", ks);
88 }
89
90 _X_EXPORT xkb_keysym_t
91 xkb_string_to_keysym(const char *s)
92 {
93     int i, n, h, c, idx;
94     uint32_t sig = 0;
95     const char *p = s;
96     char *tmp;
97     const unsigned char *entry;
98     unsigned char sig1, sig2;
99     xkb_keysym_t val;
100
101     while ((c = *p++))
102         sig = (sig << 1) + c;
103
104     i = sig % KTABLESIZE;
105     h = i + 1;
106     sig1 = (sig >> 8) & 0xff;
107     sig2 = sig & 0xff;
108     n = KMAXHASH;
109
110     while ((idx = hashString[i])) {
111         entry = &_XkeyTable[idx];
112
113         if ((entry[0] == sig1) && (entry[1] == sig2) &&
114             !strcmp(s, (const char *)entry + 6))
115         {
116             val = (entry[2] << 24) | (entry[3] << 16) |
117                   (entry[4] << 8)  | entry[5];
118             if (!val)
119                 val = XK_VoidSymbol;
120             return val;
121         }
122
123         if (!--n)
124             break;
125
126         i += h;
127         if (i >= KTABLESIZE)
128             i -= KTABLESIZE;
129     }
130
131     if (*s == 'U') {
132         val = strtoul(&s[1], &tmp, 16);
133         if (tmp && *tmp != '\0')
134             return XKB_KEYSYM_NO_SYMBOL;
135
136         if (val < 0x20 || (val > 0x7e && val < 0xa0))
137             return XKB_KEYSYM_NO_SYMBOL;
138         if (val < 0x100)
139             return val;
140         if (val > 0x10ffff)
141             return XKB_KEYSYM_NO_SYMBOL;
142         return val | 0x01000000;
143     }
144     else if (s[0] == '0' && s[1] == 'x') {
145         val = strtoul(&s[2], &tmp, 16);
146         if (tmp && *tmp != '\0')
147             return XKB_KEYSYM_NO_SYMBOL;
148
149         return val;
150     }
151
152     /* Stupid inconsistency between the headers and XKeysymDB: the former has
153      * no separating underscore, while some XF86* syms in the latter did.
154      * As a last ditch effort, try without. */
155     if (strncmp(s, "XF86_", 5) == 0) {
156         xkb_keysym_t ret;
157         tmp = strdup(s);
158         if (!tmp)
159             return XKB_KEYSYM_NO_SYMBOL;
160         memmove(&tmp[4], &tmp[5], strlen(s) - 5 + 1);
161         ret = xkb_string_to_keysym(tmp);
162         free(tmp);
163         return ret;
164     }
165
166     return XKB_KEYSYM_NO_SYMBOL;
167 }