Move include path from X11/extensions/ to xkbcommon/
[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 "xkbcommon/xkbcommon.h"
35 #include <stdlib.h>
36 #include <string.h>
37
38 #include "ks_tables.h"
39
40 void
41 xkb_keysym_to_string(uint32_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 == NoSymbol) {
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 uint32_t
95 xkb_string_to_keysym(const char *s)
96 {
97     int i, n, h, c, idx;
98     unsigned long sig = 0;
99     const char *p = s;
100     const unsigned char *entry;
101     unsigned char sig1, sig2;
102     uint32_t val;
103
104     while ((c = *p++))
105         sig = (sig << 1) + c;
106
107     i = sig % KTABLESIZE;
108     h = i + 1;
109     sig1 = (sig >> 8) & 0xff;
110     sig2 = sig & 0xff;
111     n = KMAXHASH;
112
113     while ((idx = hashString[i])) {
114         entry = &_XkeyTable[idx];
115
116         if ((entry[0] == sig1) && (entry[1] == sig2) &&
117             !strcmp(s, (char *)entry + 6))
118         {
119             val = (entry[2] << 24) | (entry[3] << 16) |
120                   (entry[4] << 8)  | entry[5];
121             if (!val)
122                 val = XK_VoidSymbol;
123             return val;
124         }
125
126         if (!--n)
127             break;
128
129         i += h;
130         if (i >= KTABLESIZE)
131             i -= KTABLESIZE;
132     }
133
134     if (*s == 'U') {
135         val = strtoul(&s[1], NULL, 16);
136
137         if (val < 0x20 || (val > 0x7e && val < 0xa0))
138             return NoSymbol;
139         if (val < 0x100)
140             return val;
141         if (val > 0x10ffff)
142             return NoSymbol;
143         return val | 0x01000000;
144     }
145     else if (s[0] == '0' && s[1] == 'x') {
146         return strtoul(&s[2], NULL, 16);
147     }
148
149     return NoSymbol;
150 }