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