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