Debian Codegraph @

src/keysym.c

raw
1 /*
2 * For MIT-open-group:
3 * Copyright 1985, 1987, 1990, 1998 The Open Group
4 *
5 * For MIT:
6 * Copyright © 2009 Dan Nicholson
7 *
8 * SPDX-License-Identifier: MIT-open-group AND MIT
9 */
10
11 #include "config.h"
12
13 #include <assert.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16
17 #include "xkbcommon/xkbcommon-keysyms.h"
18 #include "xkbcommon/xkbcommon.h"
19 #include "utils.h"
20 #include "utils-numbers.h"
21 #include "keysym.h"
22 #include "ks_tables.h"
23
24 static ssize_t
25 find_keysym_index(xkb_keysym_t ks)
26 {
27 /* Lower bound:
28 * keysym_to_name[0].keysym
29 * == XKB_KEYSYM_MIN_EXPLICIT == XKB_KEYSYM_MIN == 0
30 * No need to check: xkb_keysym_t is unsigned.
31 *
32 * Upper bound:
33 * keysym_to_name[ARRAY_SIZE(keysym_to_name) - 1].keysym
34 * == XKB_KEYSYM_MAX_EXPLICIT <= XKB_KEYSYM_MAX
35 */
36 if (ks > XKB_KEYSYM_MAX_EXPLICIT)
37 return -1;
38
39 ssize_t lo = 0, hi = ARRAY_SIZE(keysym_to_name) - 1;
40 while (hi >= lo) {
41 ssize_t mid = (lo + hi) / 2;
42 if (ks > keysym_to_name[mid].keysym) {
43 lo = mid + 1;
44 } else if (ks < keysym_to_name[mid].keysym) {
45 hi = mid - 1;
46 } else {
47 return mid;
48 }
49 }
50
51 return -1;
52 }
53
54 #define get_name_by_index(index) (keysym_names + (index))
55
56 static inline const char *
57 get_name(const struct name_keysym *entry)
58 {
59 return get_name_by_index(entry->offset);
60 }
61
62 /* Unnamed Unicode codepoint. */
63 static inline int
64 get_unicode_name(xkb_keysym_t ks, char *buffer, size_t size)
65 {
66 return snprintf(buffer, size, "U%04"PRIX32, ks & UINT32_C(0xffffff));
67 }
68
69 int
70 xkb_keysym_get_name(xkb_keysym_t ks, char *buffer, size_t size)
71 {
72 if (ks > XKB_KEYSYM_MAX) {
73 snprintf(buffer, size, "Invalid");
74 return -1;
75 }
76
77 ssize_t index = find_keysym_index(ks);
78 if (index != -1)
79 return snprintf(buffer, size, "%s", get_name(&keysym_to_name[index]));
80
81 /*
82 * Unnamed Unicode codepoint.
83 * • Keysyms in the range [XKB_KEYSYM_UNICODE_OFFSET, XKB_KEYSYM_UNICODE_MIN [
84 * do not use the Unicode notation for backward compatibility.
85 * • Keysyms are not normalized: e.g. given a Unicode keysym, calling
86 * `xkb_utf32_to_keysym` with the corresponding code point may return a
87 * different keysym or fail (e.g. surrogates are invalid in UTF-32).
88 */
89 if (ks >= XKB_KEYSYM_UNICODE_MIN && ks <= XKB_KEYSYM_UNICODE_MAX)
90 return get_unicode_name(ks, buffer, size);
91
92 /* Unnamed, non-Unicode, symbol (shouldn't generally happen). */
93 return snprintf(buffer, size, "0x%08x", ks);
94 }
95
96 bool
97 xkb_keysym_is_assigned(xkb_keysym_t ks)
98 {
99 return (XKB_KEYSYM_UNICODE_MIN <= ks && ks <= XKB_KEYSYM_UNICODE_MAX) ||
100 find_keysym_index(ks) != -1;
101 }
102
103 int
104 xkb_keysym_get_explicit_names(xkb_keysym_t ks, const char **buffer, size_t size)
105 {
106 if (ks > XKB_KEYSYM_MAX)
107 return -1;
108 const ssize_t index = find_keysym_index(ks);
109 if (index < 0)
110 return 0;
111 const uint16_t canonical = keysym_to_name[index].offset;
112 if (size > 0)
113 buffer[0] = get_name(&keysym_to_name[index]);
114 int count = 1;
115 for (size_t pos = 0; pos < ARRAY_SIZE(name_to_keysym); pos++) {
116 if (name_to_keysym[pos].keysym == ks &&
117 name_to_keysym[pos].offset != canonical) {
118 if ((size_t) count < size) {
119 buffer[count] = get_name(&name_to_keysym[pos]);
120 }
121 count++;
122 }
123 }
124 return count;
125 }
126
127 struct xkb_keysym_iterator {
128 bool explicit; /* If true, traverse only explicitly named keysyms */
129 int32_t index; /* Current index in `keysym_to_name` */
130 xkb_keysym_t keysym; /* Current keysym */
131 };
132
133 struct xkb_keysym_iterator*
134 xkb_keysym_iterator_new(bool iterate_only_explicit_keysyms)
135 {
136 struct xkb_keysym_iterator* iter = calloc(1, sizeof(*iter));
137 iter->explicit = iterate_only_explicit_keysyms;
138 iter->index = -1;
139 iter->keysym = XKB_KEYSYM_UNICODE_MAX;
140 return iter;
141 }
142
143 struct xkb_keysym_iterator*
144 xkb_keysym_iterator_unref(struct xkb_keysym_iterator *iter)
145 {
146 /*
147 * [NOTE] If we ever make this API public, add:
148 * - an `refcnt` member,
149 * - `xkb_keysym_iterator_ref()`,
150 * - `assert(!iter || iter->refcnt > 0)`.
151 */
152 if (iter)
153 free(iter);
154 return NULL;
155 }
156
157 xkb_keysym_t
158 xkb_keysym_iterator_get_keysym(struct xkb_keysym_iterator *iter)
159 {
160 return iter->keysym;
161 }
162
163 bool
164 xkb_keysym_iterator_is_explicitly_named(struct xkb_keysym_iterator *iter)
165 {
166 return iter->index >= 0 &&
167 iter->index < (int32_t)ARRAY_SIZE(keysym_to_name) &&
168 (iter->explicit ||
169 iter->keysym == keysym_to_name[iter->index].keysym);
170 }
171
172 int
173 xkb_keysym_iterator_get_name(struct xkb_keysym_iterator *iter,
174 char *buffer, size_t size)
175 {
176 if (iter->index < 0 || iter->index >= (int32_t)ARRAY_SIZE(keysym_to_name))
177 return -1;
178 if (iter->explicit || iter->keysym == keysym_to_name[iter->index].keysym)
179 return snprintf(buffer, size, "%s",
180 get_name(&keysym_to_name[iter->index]));
181 return get_unicode_name(iter->keysym, buffer, size);
182 }
183
184 /* Iterate over the *assigned* keysyms.
185 *
186 * Use:
187 *
188 * ```c
189 * struct xkb_keysym_iterator *iter = xkb_keysym_iterator_new(true);
190 * while (xkb_keysym_iterator_next(iter)) {
191 * ...
192 * }
193 * iter = xkb_keysym_iterator_unref(iter);
194 * ```
195 */
196 bool
197 xkb_keysym_iterator_next(struct xkb_keysym_iterator *iter)
198 {
199 if (iter->index >= (int32_t)ARRAY_SIZE(keysym_to_name) - 1)
200 return false;
201
202 /* Next keysym */
203 if (iter->explicit || iter->keysym >= XKB_KEYSYM_UNICODE_MAX ||
204 keysym_to_name[iter->index + 1].keysym < XKB_KEYSYM_UNICODE_MIN) {
205 /* Explicitly named keysyms only */
206 iter->keysym = keysym_to_name[++iter->index].keysym;
207 assert(iter->explicit ||
208 iter->keysym <= XKB_KEYSYM_UNICODE_MIN ||
209 iter->keysym >= XKB_KEYSYM_UNICODE_MAX);
210 } else {
211 /* Unicode keysyms
212 * NOTE: Unicode keysyms are within keysym_to_name keysyms range. */
213 if (iter->keysym >= keysym_to_name[iter->index].keysym)
214 iter->index++;
215 if (iter->keysym >= XKB_KEYSYM_UNICODE_MIN) {
216 /* Continue Unicode keysyms */
217 iter->keysym++;
218 } else {
219 /* Start Unicode keysyms */
220 iter->keysym = XKB_KEYSYM_UNICODE_MIN;
221 }
222 }
223 return true;
224 }
225
226 /* Parse the numeric part of a 0xXXXX and UXXXX keysym. */
227 static bool
228 parse_keysym_hex(const char *s, uint32_t *out)
229 {
230 /* We expect a NULL-terminated string of length up to 8 */
231 const int count = parse_hex_to_uint32_t(s, 8, out);
232 /* Check that some value was parsed and we reached the end of the string */
233 return (count > 0 && s[count] == '\0');
234 }
235
236 xkb_keysym_t
237 xkb_keysym_from_name(const char *name, enum xkb_keysym_flags flags)
238 {
239 const struct name_keysym *entry = NULL;
240 char *tmp;
241 uint32_t val;
242 bool icase = (flags & XKB_KEYSYM_CASE_INSENSITIVE);
243
244 if (flags & ~XKB_KEYSYM_CASE_INSENSITIVE)
245 return XKB_KEY_NoSymbol;
246
247 /*
248 * We need to !icase case to be fast, for e.g. Compose file parsing.
249 * So do it in a fast path.
250 */
251 if (!icase) {
252 size_t pos = keysym_name_perfect_hash(name);
253 if (pos < ARRAY_SIZE(name_to_keysym)) {
254 const char *s = get_name(&name_to_keysym[pos]);
255 if (strcmp(name, s) == 0)
256 return name_to_keysym[pos].keysym;
257 }
258 }
259 /*
260 * Find the correct keysym for case-insensitive match.
261 *
262 * The name_to_keysym table is sorted by istrcmp(). So the binary
263 * search may return _any_ of all possible case-insensitive duplicates. The
264 * duplicates are sorted so that the "best" case-insensitive match comes
265 * last. So the code searches the entry and all next entries that match by
266 * case-insensitive comparison and returns the "best" case-insensitive match.
267 *
268 * The "best" case-insensitive match is the lower-case keysym name. Most
269 * keysyms names that only differ by letter-case are keysyms that are
270 * available as “small” and “big” variants. For example:
271 *
272 * - Bicameral scripts: Lower-case and upper-case variants,
273 * e.g. KEY_a and KEY_A.
274 * - Non-bicameral scripts: e.g. KEY_kana_a and KEY_kana_A.
275 *
276 * There are some exceptions, e.g. `XF86Screensaver` and `XF86ScreenSaver`.
277 */
278 else {
279 int32_t lo = 0, hi = ARRAY_SIZE(name_to_keysym) - 1;
280 while (hi >= lo) {
281 int32_t mid = (lo + hi) / 2;
282 int cmp = istrcmp(name, get_name(&name_to_keysym[mid]));
283 if (cmp > 0) {
284 lo = mid + 1;
285 } else if (cmp < 0) {
286 hi = mid - 1;
287 } else {
288 entry = &name_to_keysym[mid];
289 break;
290 }
291 }
292 if (entry) {
293 const struct name_keysym *last;
294 last = name_to_keysym + ARRAY_SIZE(name_to_keysym) - 1;
295 /* Keep going until we reach end of array
296 * or non case-insensitve match */
297 while (entry < last &&
298 istrcmp(get_name(entry + 1), get_name(entry)) == 0) {
299 entry++;
300 }
301 return entry->keysym;
302 }
303 }
304
305 if (*name == 'U' || (icase && *name == 'u')) {
306 /* Parse Unicode notation */
307 if (!parse_keysym_hex(&name[1], &val))
308 return XKB_KEY_NoSymbol;
309 return (val > 0xff && val <= 0x10ffff)
310 /*
311 * No normalization.
312 *
313 * NOTE: It allows surrogates, as we are dealing with Unicode
314 * *code points* here, not Unicode *scalars*.
315 */
316 ? XKB_KEYSYM_UNICODE_OFFSET + val
317 /*
318 * Normalize ISO-8859-1 (Latin-1 + C0 and C1 control code)
319 *
320 * These code points require special processing to ensure
321 * backward compatibility with legacy keysyms.
322 */
323 : xkb_utf32_to_keysym(val);
324 }
325 else if (name[0] == '0' && (name[1] == 'x' || (icase && name[1] == 'X'))) {
326 /*
327 * Parse numeric hexadecimal notation without any normalization, in
328 * in order to be consistent with the keymap files parsing.
329 */
330 if (!parse_keysym_hex(&name[2], &val) || val > XKB_KEYSYM_MAX)
331 return XKB_KEY_NoSymbol;
332 return (xkb_keysym_t) val;
333 }
334
335 /* Stupid inconsistency between the headers and XKeysymDB: the former has
336 * no separating underscore, while some XF86* syms in the latter did.
337 * As a last ditch effort, try without. */
338 if (strncmp(name, "XF86_", 5) == 0 ||
339 (icase && istrncmp(name, "XF86_", 5) == 0)) {
340 xkb_keysym_t ret;
341 tmp = strdup(name);
342 if (!tmp)
343 return XKB_KEY_NoSymbol;
344 memmove(&tmp[4], &tmp[5], strlen(name) - 5 + 1);
345 ret = xkb_keysym_from_name(tmp, flags);
346 free(tmp);
347 return ret;
348 }
349
350 return XKB_KEY_NoSymbol;
351 }
352
353 /*
354 * Check whether a keysym with code "keysym" and name "name" is deprecated.
355 * • If the keysym is not deprecated itself and has no deprecated names,
356 * then return false and write NULL in "reference_name".
357 * • If there is a non-deprecated name for the given keysym, then write this
358 * name in "reference_name", else write NULL and return true.
359 * • If "name" is NULL, then returns false: the keysym itself is not deprecated.
360 * • If "name" is not NULL, then check if "name" is deprecated.
361 *
362 * WARNING: this function is unsafe because it does not test if "name" is
363 * actually a correct name for "keysym". It is intended to be used just after
364 * keysym resolution.
365 */
366 bool
367 xkb_keysym_is_deprecated(xkb_keysym_t keysym,
368 const char *name,
369 const char **reference_name)
370 {
371 if (keysym > XKB_KEYSYM_MAX) {
372 /* Invalid keysym */
373 *reference_name = NULL;
374 return false;
375 }
376 /* [WARNING] We do not check that name, if defined, is a valid for keysym */
377
378 int32_t lo = 0, hi = ARRAY_SIZE(deprecated_keysyms) - 1;
379 while (hi >= lo) {
380 int32_t mid = (lo + hi) / 2;
381 if (keysym > deprecated_keysyms[mid].keysym) {
382 lo = mid + 1;
383 } else if (keysym < deprecated_keysyms[mid].keysym) {
384 hi = mid - 1;
385 } else {
386 /* Keysym have some deprecated names */
387 if (deprecated_keysyms[mid].offset == DEPRECATED_KEYSYM) {
388 /* All names are deprecated */
389 *reference_name = NULL;
390 return true;
391 } else if (deprecated_keysyms[mid].offset == UNICODE_KEYSYM) {
392 /* All names are deprecated, but Unicode notation is valid */
393 *reference_name = NULL;
394 if (name == NULL)
395 return false;
396 if (name[0] != 'U')
397 return true;
398 /* Since the current function is internal, assume the name
399 * corresponds to the keysym value and just check its syntax. */
400 const char *start = name + 1;
401 while (is_xdigit(*start))
402 start++;
403 return *start != '\0' && start > name + 1;
404 }
405 /* There is a reference name that is not deprecated */
406 *reference_name = get_name_by_index(deprecated_keysyms[mid].offset);
407 if (name == NULL)
408 /* No name to check: indicate not deprecated */
409 return false;
410 if (deprecated_keysyms[mid].explicit_count) {
411 /* Only some explicit names are deprecated */
412 uint8_t k = deprecated_keysyms[mid].explicit_index;
413 const uint8_t k_max = deprecated_keysyms[mid].explicit_index
414 + deprecated_keysyms[mid].explicit_count;
415 /* Check every deprecated alias */
416 for (; k < k_max; k++) {
417 const char *alias =
418 get_name_by_index(explicit_deprecated_aliases[k]);
419 if (strcmp(name, alias) == 0)
420 return true;
421 }
422 return false;
423 } else {
424 /* All names but the reference one are deprecated */
425 return strcmp(name, *reference_name) != 0;
426 }
427 }
428 }
429
430 /* Keysym has no deprecated names */
431 *reference_name = NULL;
432 return false;
433 }
434
435 bool
436 xkb_keysym_is_keypad(xkb_keysym_t keysym)
437 {
438 return keysym >= XKB_KEY_KP_Space && keysym <= XKB_KEY_KP_Equal;
439 }
440
441
442 bool
443 xkb_keysym_is_modifier(xkb_keysym_t keysym)
444 {
445 return
446 (keysym >= XKB_KEY_Shift_L && keysym <= XKB_KEY_Hyper_R) ||
447 (keysym >= XKB_KEY_ISO_Lock && keysym <= XKB_KEY_ISO_Level5_Lock) ||
448 keysym == XKB_KEY_Mode_switch ||
449 keysym == XKB_KEY_Num_Lock;
450 }