Add return value the xkb_keysym_get_name
[platform/upstream/libxkbcommon.git] / src / keysym.c
1 /*
2  * Copyright 1985, 1987, 1990, 1998  The Open Group
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20  *
21  * Except as contained in this notice, the names of the authors or their
22  * institutions shall not be used in advertising or otherwise to promote the
23  * sale, use or other dealings in this Software without prior written
24  * authorization from the authors.
25  */
26
27 /*
28  * Copyright © 2009 Dan Nicholson
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a
31  * copy of this software and associated documentation files (the "Software"),
32  * to deal in the Software without restriction, including without limitation
33  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34  * and/or sell copies of the Software, and to permit persons to whom the
35  * Software is furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice (including the next
38  * paragraph) shall be included in all copies or substantial portions of the
39  * Software.
40  *
41  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
42  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
43  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
44  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
46  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
47  * DEALINGS IN THE SOFTWARE.
48  */
49
50 #include "xkbcommon/xkbcommon.h"
51 #include "utils.h"
52 #include "ks_tables.h"
53 #include "keysym.h"
54
55 XKB_EXPORT int
56 xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
57 {
58     int i, n, h, idx;
59     const unsigned char *entry;
60     unsigned char val1, val2, val3, val4;
61
62     if ((ks & ((unsigned long) ~0x1fffffff)) != 0) {
63         snprintf(buffer, size, "Invalid");
64         return -1;
65     }
66
67     /* Try to find it in our hash table. */
68     if (ks <= 0x1fffffff) {
69         val1 = ks >> 24;
70         val2 = (ks >> 16) & 0xff;
71         val3 = (ks >> 8) & 0xff;
72         val4 = ks & 0xff;
73         i = ks % VTABLESIZE;
74         h = i + 1;
75         n = VMAXHASH;
76
77         while ((idx = hashKeysym[i])) {
78             entry = &_XkeyTable[idx];
79
80             if ((entry[0] == val1) && (entry[1] == val2) &&
81                 (entry[2] == val3) && (entry[3] == val4)) {
82                 return snprintf(buffer, size, "%s", entry + 4);
83             }
84
85             if (!--n)
86                 break;
87
88             i += h;
89             if (i >= VTABLESIZE)
90                 i -= VTABLESIZE;
91         }
92     }
93
94     if (ks >= 0x01000100 && ks <= 0x0110ffff)
95         /* Unnamed Unicode codepoint. */
96         return snprintf(buffer, size, "U%lx", ks & 0xffffffUL);
97
98     /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
99     return snprintf(buffer, size, "0x%08x", ks);
100 }
101
102 XKB_EXPORT xkb_keysym_t
103 xkb_keysym_from_name(const char *s)
104 {
105     int i, n, h, c, idx;
106     uint32_t sig = 0;
107     const char *p = s;
108     char *tmp;
109     const unsigned char *entry;
110     unsigned char sig1, sig2;
111     xkb_keysym_t val;
112
113     while ((c = *p++))
114         sig = (sig << 1) + c;
115
116     i = sig % KTABLESIZE;
117     h = i + 1;
118     sig1 = (sig >> 8) & 0xff;
119     sig2 = sig & 0xff;
120     n = KMAXHASH;
121
122     while ((idx = hashString[i])) {
123         entry = &_XkeyTable[idx];
124
125         if ((entry[0] == sig1) && (entry[1] == sig2) &&
126             streq(s, (const char *) entry + 6)) {
127             val = (entry[2] << 24) | (entry[3] << 16) |
128                   (entry[4] << 8) | entry[5];
129             if (!val)
130                 val = XKB_KEY_VoidSymbol;
131             return val;
132         }
133
134         if (!--n)
135             break;
136
137         i += h;
138         if (i >= KTABLESIZE)
139             i -= KTABLESIZE;
140     }
141
142     if (*s == 'U') {
143         val = strtoul(&s[1], &tmp, 16);
144         if (tmp && *tmp != '\0')
145             return XKB_KEY_NoSymbol;
146
147         if (val < 0x20 || (val > 0x7e && val < 0xa0))
148             return XKB_KEY_NoSymbol;
149         if (val < 0x100)
150             return val;
151         if (val > 0x10ffff)
152             return XKB_KEY_NoSymbol;
153         return val | 0x01000000;
154     }
155     else if (s[0] == '0' && s[1] == 'x') {
156         val = strtoul(&s[2], &tmp, 16);
157         if (tmp && *tmp != '\0')
158             return XKB_KEY_NoSymbol;
159
160         return val;
161     }
162
163     /* Stupid inconsistency between the headers and XKeysymDB: the former has
164      * no separating underscore, while some XF86* syms in the latter did.
165      * As a last ditch effort, try without. */
166     if (strncmp(s, "XF86_", 5) == 0) {
167         xkb_keysym_t ret;
168         tmp = strdup(s);
169         if (!tmp)
170             return XKB_KEY_NoSymbol;
171         memmove(&tmp[4], &tmp[5], strlen(s) - 5 + 1);
172         ret = xkb_keysym_from_name(tmp);
173         free(tmp);
174         return ret;
175     }
176
177     return XKB_KEY_NoSymbol;
178 }
179
180 enum keysym_case {
181     NONE,
182     LOWERCASE,
183     UPPERCASE,
184 };
185
186 static enum keysym_case
187 keysym_get_case(xkb_keysym_t ks)
188 {
189     unsigned set = (ks & (~0xff)) >> 8;
190
191     switch (set) {
192     case 0: /* latin 1 */
193         if ((ks >= XKB_KEY_A && ks <= XKB_KEY_Z) ||
194             (ks >= XKB_KEY_Agrave && ks <= XKB_KEY_THORN && ks !=
195              XKB_KEY_multiply))
196             return UPPERCASE;
197         if ((ks >= XKB_KEY_a && ks <= XKB_KEY_z) ||
198             (ks >= XKB_KEY_agrave && ks <= XKB_KEY_ydiaeresis))
199             return LOWERCASE;
200         break;
201
202     case 1: /* latin 2 */
203         if ((ks >= XKB_KEY_Aogonek && ks <= XKB_KEY_Zabovedot && ks !=
204              XKB_KEY_breve) ||
205             (ks >= XKB_KEY_Racute && ks <= XKB_KEY_Tcedilla))
206             return UPPERCASE;
207         if ((ks >= XKB_KEY_aogonek && ks <= XKB_KEY_zabovedot && ks !=
208              XKB_KEY_caron) ||
209             (ks >= XKB_KEY_racute && ks <= XKB_KEY_tcedilla))
210             return LOWERCASE;
211         break;
212
213     case 2: /* latin 3 */
214         if ((ks >= XKB_KEY_Hstroke && ks <= XKB_KEY_Jcircumflex) ||
215             (ks >= XKB_KEY_Cabovedot && ks <= XKB_KEY_Scircumflex))
216             return UPPERCASE;
217         if ((ks >= XKB_KEY_hstroke && ks <= XKB_KEY_jcircumflex) ||
218             (ks >= XKB_KEY_cabovedot && ks <= XKB_KEY_scircumflex))
219             return LOWERCASE;
220         break;
221
222     case 3: /* latin 4 */
223         if ((ks >= XKB_KEY_Rcedilla && ks <= XKB_KEY_Tslash) ||
224             (ks == XKB_KEY_ENG) ||
225             (ks >= XKB_KEY_Amacron && ks <= XKB_KEY_Umacron))
226             return UPPERCASE;
227         if ((ks >= XKB_KEY_rcedilla && ks <= XKB_KEY_tslash) ||
228             (ks == XKB_KEY_eng) ||
229             (ks >= XKB_KEY_amacron && ks <= XKB_KEY_umacron))
230             return LOWERCASE;
231         break;
232
233     case 6: /* Cyrillic */
234         if ((ks >= XKB_KEY_Serbian_DJE && ks <= XKB_KEY_Serbian_DZE) ||
235             (ks >= XKB_KEY_Cyrillic_YU && ks <= XKB_KEY_Cyrillic_HARDSIGN))
236             return UPPERCASE;
237         if ((ks >= XKB_KEY_Serbian_dje && ks <= XKB_KEY_Serbian_dze) ||
238             (ks >= XKB_KEY_Cyrillic_yu && ks <= XKB_KEY_Cyrillic_hardsign))
239             return LOWERCASE;
240         break;
241
242     case 7: /* Greek */
243         if ((ks >= XKB_KEY_Greek_ALPHAaccent &&
244              ks <= XKB_KEY_Greek_OMEGAaccent) ||
245             (ks >= XKB_KEY_Greek_ALPHA && ks <= XKB_KEY_Greek_OMEGA))
246             return UPPERCASE;
247         if ((ks >= XKB_KEY_Greek_alphaaccent &&
248              ks <= XKB_KEY_Greek_omegaaccent) ||
249             (ks >= XKB_KEY_Greek_alpha && ks <= XKB_KEY_Greek_OMEGA))
250             return LOWERCASE;
251         break;
252
253     case 18: /* latin 8 */
254         if ((ks == XKB_KEY_Wcircumflex) ||
255             (ks == XKB_KEY_Ycircumflex) ||
256             (ks == XKB_KEY_Babovedot) ||
257             (ks == XKB_KEY_Dabovedot) ||
258             (ks == XKB_KEY_Fabovedot) ||
259             (ks == XKB_KEY_Mabovedot) ||
260             (ks == XKB_KEY_Pabovedot) ||
261             (ks == XKB_KEY_Sabovedot) ||
262             (ks == XKB_KEY_Tabovedot) ||
263             (ks == XKB_KEY_Wdiaeresis) ||
264             (ks == XKB_KEY_Ygrave))
265             return UPPERCASE;
266         if ((ks == XKB_KEY_wcircumflex) ||
267             (ks == XKB_KEY_ycircumflex) ||
268             (ks == XKB_KEY_babovedot) ||
269             (ks == XKB_KEY_dabovedot) ||
270             (ks == XKB_KEY_fabovedot) ||
271             (ks == XKB_KEY_mabovedot) ||
272             (ks == XKB_KEY_pabovedot) ||
273             (ks == XKB_KEY_sabovedot) ||
274             (ks == XKB_KEY_tabovedot) ||
275             (ks == XKB_KEY_wdiaeresis) ||
276             (ks == XKB_KEY_ygrave))
277             return LOWERCASE;
278         break;
279
280     case 19: /* latin 9 */
281         if (ks == XKB_KEY_OE || ks == XKB_KEY_Ydiaeresis)
282             return UPPERCASE;
283         if (ks == XKB_KEY_oe)
284             return LOWERCASE;
285         break;
286     }
287
288     return NONE;
289 }
290
291 bool
292 xkb_keysym_is_lower(xkb_keysym_t keysym)
293 {
294     return keysym_get_case(keysym) == LOWERCASE;
295 }
296
297 bool
298 xkb_keysym_is_upper(xkb_keysym_t keysym)
299 {
300     return keysym_get_case(keysym) == UPPERCASE;
301 }
302
303 bool
304 xkb_keysym_is_keypad(xkb_keysym_t keysym)
305 {
306     return keysym >= XKB_KEY_KP_Space && keysym <= XKB_KEY_KP_Equal;
307 }