3bcfd8f496cedc1fc7a43eb12305df306ae3aae1
[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 <X11/X.h>
32 #include <X11/keysymdef.h>
33 #include "xkbmisc.h"
34 #include "X11/extensions/XKBcommon.h"
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "ks_tables.h"
39
40 char *
41 XkbcKeysymToString(KeySym ks)
42 {
43     int i, n, h, idx;
44     const unsigned char *entry;
45     static char ret[11];
46     unsigned char val1, val2, val3, val4;
47
48     if ((ks & ((unsigned long) ~0x1fffffff)) != 0)
49         return NULL;
50
51     /* Not listed in keysymdef.h for hysterical raisins. */
52     if (ks == NoSymbol) {
53         sprintf(ret, "NoSymbol");
54         return ret;
55     }
56
57     if (ks <= 0x1fffffff) {
58         val1 = ks >> 24;
59         val2 = (ks >> 16) & 0xff;
60         val3 = (ks >> 8) & 0xff;
61         val4 = ks & 0xff;
62         i = ks % VTABLESIZE;
63         h = i + 1;
64         n = VMAXHASH;
65
66         while ((idx = hashKeysym[i])) {
67             entry = &_XkeyTable[idx];
68
69             if ((entry[0] == val1) && (entry[1] == val2) &&
70                 (entry[2] == val3) && (entry[3] == val4))
71                 return ((char *)entry + 4);
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         KeySym val = ks & 0xffffff;
84         char *s;
85         int i;
86
87         if (val & 0xff0000)
88             i = 10;
89         else
90             i = 6;
91
92         s = malloc(i);
93         if (!s)
94             return s;
95
96         i--;
97         s[i--] = '\0';
98         for (; i; i--) {
99             val1 = val & 0xf;
100             val >>= 4;
101             if (val1 < 10)
102                 s[i] = '0' + val1;
103             else
104                 s[i] = 'A' + val1 - 10;
105         }
106         s[i] = 'U';
107         return s;
108     }
109
110     return NULL;
111 }
112
113 KeySym
114 XkbcStringToKeysym(const char *s)
115 {
116     int i, n, h, c, idx;
117     unsigned long sig = 0;
118     const char *p = s;
119     const unsigned char *entry;
120     unsigned char sig1, sig2;
121     KeySym val;
122
123     while ((c = *p++))
124         sig = (sig << 1) + c;
125
126     i = sig % KTABLESIZE;
127     h = i + 1;
128     sig1 = (sig >> 8) & 0xff;
129     sig2 = sig & 0xff;
130     n = KMAXHASH;
131
132     while ((idx = hashString[i])) {
133         entry = &_XkeyTable[idx];
134
135         if ((entry[0] == sig1) && (entry[1] == sig2) &&
136             !strcmp(s, (char *)entry + 6))
137         {
138             val = (entry[2] << 24) | (entry[3] << 16) |
139                   (entry[4] << 8)  | entry[5];
140             if (!val)
141                 val = XK_VoidSymbol;
142             return val;
143         }
144
145         if (!--n)
146             break;
147
148         i += h;
149         if (i >= KTABLESIZE)
150             i -= KTABLESIZE;
151     }
152
153     if (*s == 'U') {
154         val = 0;
155
156         for (p = &s[1]; *p; p++) {
157             c = *p;
158
159             if ('0' <= c && c <= '9')
160                 val = (val << 4) + c - '0';
161             else if ('a' <= c && c <= 'f')
162                 val = (val << 4) + c - 'a' + 10;
163             else if ('A' <= c && c <= 'F')
164                 val = (val << 4) + c - 'A' + 10;
165             else
166                 return NoSymbol;
167
168             if (val > 0x10ffff)
169                 return NoSymbol;
170         }
171
172         if (val < 0x20 || (val > 0x7e && val < 0xa0))
173             return NoSymbol;
174         if (val < 0x100)
175             return val;
176         return val | 0x01000000;
177     }
178
179     return NoSymbol;
180 }