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