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