Remove all non-public API from XKBcommon.h header
[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     unsigned char val1, val2, val3, val4;
46
47     if (!ks || (ks & ((unsigned long) ~0x1fffffff)) != 0)
48         return NULL;
49
50     if (ks == XK_VoidSymbol)
51         ks = 0;
52
53     if (ks <= 0x1fffffff) {
54         val1 = ks >> 24;
55         val2 = (ks >> 16) & 0xff;
56         val3 = (ks >> 8) & 0xff;
57         val4 = ks & 0xff;
58         i = ks % VTABLESIZE;
59         h = i + 1;
60         n = VMAXHASH;
61
62         while ((idx = hashKeysym[i])) {
63             entry = &_XkeyTable[idx];
64
65             if ((entry[0] == val1) && (entry[1] == val2) &&
66                 (entry[2] == val3) && (entry[3] == val4))
67                 return ((char *)entry + 4);
68
69             if (!--n)
70                 break;
71
72             i += h;
73             if (i >= VTABLESIZE)
74                 i -= VTABLESIZE;
75         }
76     }
77
78     if (ks >= 0x01000100 && ks <= 0x0110ffff) {
79         KeySym val = ks & 0xffffff;
80         char *s;
81         int i;
82
83         if (val & 0xff0000)
84             i = 10;
85         else
86             i = 6;
87
88         s = malloc(i);
89         if (!s)
90             return s;
91
92         i--;
93         s[i--] = '\0';
94         for (; i; i--) {
95             val1 = val & 0xf;
96             val >>= 4;
97             if (val1 < 10)
98                 s[i] = '0' + val1;
99             else
100                 s[i] = 'A' + val1 - 10;
101         }
102         s[i] = 'U';
103         return s;
104     }
105
106     return NULL;
107 }
108
109 KeySym
110 XkbcStringToKeysym(const char *s)
111 {
112     int i, n, h, c, idx;
113     unsigned long sig = 0;
114     const char *p = s;
115     const unsigned char *entry;
116     unsigned char sig1, sig2;
117     KeySym val;
118
119     while ((c = *p++))
120         sig = (sig << 1) + c;
121
122     i = sig % KTABLESIZE;
123     h = i + 1;
124     sig1 = (sig >> 8) & 0xff;
125     sig2 = sig & 0xff;
126     n = KMAXHASH;
127
128     while ((idx = hashString[i])) {
129         entry = &_XkeyTable[idx];
130
131         if ((entry[0] == sig1) && (entry[1] == sig2) &&
132             !strcmp(s, (char *)entry + 6))
133         {
134             val = (entry[2] << 24) | (entry[3] << 16) |
135                   (entry[4] << 8)  | entry[5];
136             if (!val)
137                 val = XK_VoidSymbol;
138             return val;
139         }
140
141         if (!--n)
142             break;
143
144         i += h;
145         if (i >= KTABLESIZE)
146             i -= KTABLESIZE;
147     }
148
149     if (*s == 'U') {
150         val = 0;
151
152         for (p = &s[1]; *p; p++) {
153             c = *p;
154
155             if ('0' <= c && c <= '9')
156                 val = (val << 4) + c - '0';
157             else if ('a' <= c && c <= 'f')
158                 val = (val << 4) + c - 'a' + 10;
159             else if ('A' <= c && c <= 'F')
160                 val = (val << 4) + c - 'A' + 10;
161             else
162                 return NoSymbol;
163
164             if (val > 0x10ffff)
165                 return NoSymbol;
166         }
167
168         if (val < 0x20 || (val > 0x7e && val < 0xa0))
169             return NoSymbol;
170         if (val < 0x100)
171             return val;
172         return val | 0x01000000;
173     }
174
175     return NoSymbol;
176 }