Hash :
9ede705b
Author :
Date :
2025-04-13T09:50:18
state: Capitalization transformation in xkb_state_key_get_syms
Previously `xkb_state_key_get_syms()` did not perform capitalization
tranformation, while `xkb_state_key_get_one_sym()` does perform it.
This is unfortunate if we want to promote the use of multiple keysyms
per levels.
The API make it difficult to change now though: we return a pointer to
an immutable array rather than filling a buffer. While we could use an
internal buffer in `xkb_state`, this option would limit the API to
*sequential* calls of `xkb_state_key_get_syms()` or require some buffer
handling (e.g. rotation).
Instead we now store the capitalization directly in `xkb_level`. We
modified `xkb_level` like so (see below for discussion about the size):
```diff
struct xkb_level {
- unsigned int num_syms;
+ uint16_t num_syms;
- unsigned int num_actions;
+ uint16_t num_actions;
+ union {
+ /** num_syms == 1: Upper keysym */
+ xkb_keysym_t upper;
+ /** num_syms > 1: Indicate if `syms` contains the upper case
+ * keysyms after the lower ones. */
+ bool has_upper;
+ };
union {
xkb_keysym_t sym; /* num_syms == 1 */
xkb_keysym_t *syms; /* num_syms > 1 */
} s;
union {
union xkb_action action; /* num_actions == 1 */
union xkb_action *actions; /* num_actions > 1 */
} a;
};
```
- When `level.num_syms` <= 1, we store the upper keysym in `level.upper`.
- Else if there no cased syms, we set `level.has_upper` to false.
- Else if there are some cased syms, we set `level.has_upper`` to `true`
and we double the original size of `level.s.syms`, but *without*
modifying `level.num_syms`. We then append the transformed keysyms
right after the original ones, so that we can access them by a simple
pointer operation: `level.s.syms + level.num_syms`.
The memory footprint is *unchanged*, thanks to the reduced fields for
actions and keysyms counts.
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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
/*
* For HPND:
* Copyright (c) 1993 by Silicon Graphics Computer Systems, Inc.
*
* For MIT:
* Copyright © 2012 Intel Corporation
* Copyright © 2012 Ran Benita <ran234@gmail.com>
*
* SPDX-License-Identifier: HPND AND MIT
*
* Author: Daniel Stone <daniel@fooishbar.org>
*/
#include "config.h"
#include <assert.h>
#include <stdint.h>
#include "keymap.h"
#include "text.h"
struct xkb_keymap *
xkb_keymap_ref(struct xkb_keymap *keymap)
{
keymap->refcnt++;
return keymap;
}
void
clear_level(struct xkb_level *leveli)
{
if (leveli->num_syms > 1)
free(leveli->s.syms);
if (leveli->num_actions > 1)
free(leveli->a.actions);
}
static void
clear_interpret(struct xkb_sym_interpret *interp)
{
if (interp->num_actions > 1)
free(interp->a.actions);
}
void
xkb_keymap_unref(struct xkb_keymap *keymap)
{
if (!keymap || --keymap->refcnt > 0)
return;
if (keymap->keys) {
struct xkb_key *key;
xkb_keys_foreach(key, keymap) {
if (key->groups) {
for (unsigned i = 0; i < key->num_groups; i++) {
if (key->groups[i].levels) {
for (xkb_level_index_t j = 0;
j < XkbKeyNumLevels(key, i);
j++)
clear_level(&key->groups[i].levels[j]);
free(key->groups[i].levels);
}
}
free(key->groups);
}
}
free(keymap->keys);
}
if (keymap->types) {
for (unsigned i = 0; i < keymap->num_types; i++) {
free(keymap->types[i].entries);
free(keymap->types[i].level_names);
}
free(keymap->types);
}
for (unsigned int k = 0; k < keymap->num_sym_interprets; k++) {
clear_interpret(&keymap->sym_interprets[k]);
}
free(keymap->sym_interprets);
free(keymap->key_aliases);
free(keymap->group_names);
free(keymap->keycodes_section_name);
free(keymap->symbols_section_name);
free(keymap->types_section_name);
free(keymap->compat_section_name);
xkb_context_unref(keymap->ctx);
free(keymap);
}
static const struct xkb_keymap_format_ops *
get_keymap_format_ops(enum xkb_keymap_format format)
{
static const struct xkb_keymap_format_ops *keymap_format_ops[] = {
[XKB_KEYMAP_FORMAT_TEXT_V1] = &text_v1_keymap_format_ops,
};
if ((int) format < 0 || (int) format >= (int) ARRAY_SIZE(keymap_format_ops))
return NULL;
return keymap_format_ops[(int) format];
}
struct xkb_keymap *
xkb_keymap_new_from_names(struct xkb_context *ctx,
const struct xkb_rule_names *rmlvo_in,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
struct xkb_rule_names rmlvo;
const enum xkb_keymap_format format = XKB_KEYMAP_FORMAT_TEXT_V1;
const struct xkb_keymap_format_ops *ops;
ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_names) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}
if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}
keymap = xkb_keymap_new(ctx, format, flags);
if (!keymap)
return NULL;
if (rmlvo_in)
rmlvo = *rmlvo_in;
else
memset(&rmlvo, 0, sizeof(rmlvo));
xkb_context_sanitize_rule_names(ctx, &rmlvo);
if (!ops->keymap_new_from_names(keymap, &rmlvo)) {
xkb_keymap_unref(keymap);
return NULL;
}
return keymap;
}
struct xkb_keymap *
xkb_keymap_new_from_string(struct xkb_context *ctx,
const char *string,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
return xkb_keymap_new_from_buffer(ctx, string, strlen(string),
format, flags);
}
struct xkb_keymap *
xkb_keymap_new_from_buffer(struct xkb_context *ctx,
const char *buffer, size_t length,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
const struct xkb_keymap_format_ops *ops;
ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_string) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}
if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}
if (!buffer) {
log_err_func1(ctx, XKB_LOG_MESSAGE_NO_ID,
"no buffer specified\n");
return NULL;
}
keymap = xkb_keymap_new(ctx, format, flags);
if (!keymap)
return NULL;
/* Allow a zero-terminated string as a buffer */
if (length > 0 && buffer[length - 1] == '\0')
length--;
if (!ops->keymap_new_from_string(keymap, buffer, length)) {
xkb_keymap_unref(keymap);
return NULL;
}
return keymap;
}
struct xkb_keymap *
xkb_keymap_new_from_file(struct xkb_context *ctx,
FILE *file,
enum xkb_keymap_format format,
enum xkb_keymap_compile_flags flags)
{
struct xkb_keymap *keymap;
const struct xkb_keymap_format_ops *ops;
ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_new_from_file) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}
if (flags & ~(XKB_KEYMAP_COMPILE_NO_FLAGS)) {
log_err_func(ctx, XKB_LOG_MESSAGE_NO_ID,
"unrecognized flags: %#x\n", flags);
return NULL;
}
if (!file) {
log_err_func1(ctx, XKB_LOG_MESSAGE_NO_ID,
"no file specified\n");
return NULL;
}
keymap = xkb_keymap_new(ctx, format, flags);
if (!keymap)
return NULL;
if (!ops->keymap_new_from_file(keymap, file)) {
xkb_keymap_unref(keymap);
return NULL;
}
return keymap;
}
char *
xkb_keymap_get_as_string(struct xkb_keymap *keymap,
enum xkb_keymap_format format)
{
const struct xkb_keymap_format_ops *ops;
if (format == XKB_KEYMAP_USE_ORIGINAL_FORMAT)
format = keymap->format;
ops = get_keymap_format_ops(format);
if (!ops || !ops->keymap_get_as_string) {
log_err_func(keymap->ctx, XKB_LOG_MESSAGE_NO_ID,
"unsupported keymap format: %d\n", format);
return NULL;
}
return ops->keymap_get_as_string(keymap);
}
/**
* Returns the total number of modifiers active in the keymap.
*/
xkb_mod_index_t
xkb_keymap_num_mods(struct xkb_keymap *keymap)
{
return keymap->mods.num_mods;
}
/**
* Return the name for a given modifier.
*/
const char *
xkb_keymap_mod_get_name(struct xkb_keymap *keymap, xkb_mod_index_t idx)
{
if (idx >= keymap->mods.num_mods)
return NULL;
return xkb_atom_text(keymap->ctx, keymap->mods.mods[idx].name);
}
/**
* Returns the index for a named modifier.
*/
xkb_mod_index_t
xkb_keymap_mod_get_index(struct xkb_keymap *keymap, const char *name)
{
xkb_atom_t atom;
atom = xkb_atom_lookup(keymap->ctx, name);
if (atom == XKB_ATOM_NONE)
return XKB_MOD_INVALID;
return XkbModNameToIndex(&keymap->mods, atom, MOD_BOTH);
}
/**
* Return the total number of active groups in the keymap.
*/
xkb_layout_index_t
xkb_keymap_num_layouts(struct xkb_keymap *keymap)
{
return keymap->num_groups;
}
/**
* Returns the name for a given group.
*/
const char *
xkb_keymap_layout_get_name(struct xkb_keymap *keymap, xkb_layout_index_t idx)
{
if (idx >= keymap->num_group_names)
return NULL;
return xkb_atom_text(keymap->ctx, keymap->group_names[idx]);
}
/**
* Returns the index for a named layout.
*/
xkb_layout_index_t
xkb_keymap_layout_get_index(struct xkb_keymap *keymap, const char *name)
{
xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
xkb_layout_index_t i;
if (atom == XKB_ATOM_NONE)
return XKB_LAYOUT_INVALID;
for (i = 0; i < keymap->num_group_names; i++)
if (keymap->group_names[i] == atom)
return i;
return XKB_LAYOUT_INVALID;
}
/**
* Returns the number of layouts active for a particular key.
*/
xkb_layout_index_t
xkb_keymap_num_layouts_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
return 0;
return key->num_groups;
}
/**
* Returns the number of levels active for a particular key and layout.
*/
xkb_level_index_t
xkb_keymap_num_levels_for_key(struct xkb_keymap *keymap, xkb_keycode_t kc,
xkb_layout_index_t layout)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
return 0;
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups don't fit");
layout = XkbWrapGroupIntoRange((int32_t) layout, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
if (layout == XKB_LAYOUT_INVALID)
return 0;
return XkbKeyNumLevels(key, layout);
}
/**
* Return the total number of LEDs in the keymap.
*/
xkb_led_index_t
xkb_keymap_num_leds(struct xkb_keymap *keymap)
{
return keymap->num_leds;
}
/**
* Returns the name for a given LED.
*/
const char *
xkb_keymap_led_get_name(struct xkb_keymap *keymap, xkb_led_index_t idx)
{
if (idx >= keymap->num_leds)
return NULL;
return xkb_atom_text(keymap->ctx, keymap->leds[idx].name);
}
/**
* Returns the index for a named LED.
*/
xkb_led_index_t
xkb_keymap_led_get_index(struct xkb_keymap *keymap, const char *name)
{
xkb_atom_t atom = xkb_atom_lookup(keymap->ctx, name);
xkb_led_index_t i;
const struct xkb_led *led;
if (atom == XKB_ATOM_NONE)
return XKB_LED_INVALID;
xkb_leds_enumerate(i, led, keymap)
if (led->name == atom)
return i;
return XKB_LED_INVALID;
}
size_t
xkb_keymap_key_get_mods_for_level(struct xkb_keymap *keymap,
xkb_keycode_t kc,
xkb_layout_index_t layout,
xkb_level_index_t level,
xkb_mod_mask_t *masks_out,
size_t masks_size)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
return 0;
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups don't fit");
layout = XkbWrapGroupIntoRange((int32_t) layout, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
if (layout == XKB_LAYOUT_INVALID)
return 0;
if (level >= XkbKeyNumLevels(key, layout))
return 0;
const struct xkb_key_type *type = key->groups[layout].type;
size_t count = 0;
/*
* If the active set of modifiers doesn't match any explicit entry of
* the key type, the resulting level is 0 (i.e. Level 1).
* So, if we are asked to find the modifiers for level==0, we can offer
* an ~infinite supply, which is not very workable.
* What we do instead, is special case the empty set of modifiers for
* this purpose. If the empty set isn't explicit mapped to a level, we
* take it to map to Level 1.
* This is almost always what we want. If applicable, given it priority
* over other ways to generate the level.
*/
if (level == 0) {
bool empty_mapped = false;
for (unsigned i = 0; i < type->num_entries && count < masks_size; i++)
if (entry_is_active(&type->entries[i]) &&
type->entries[i].mods.mask == 0) {
empty_mapped = true;
break;
}
if (!empty_mapped && count < masks_size) {
masks_out[count++] = 0;
}
}
/* Now search explicit mappings. */
for (unsigned i = 0; i < type->num_entries && count < masks_size; i++) {
if (entry_is_active(&type->entries[i]) &&
type->entries[i].level == level) {
masks_out[count++] = type->entries[i].mods.mask;
}
}
return count;
}
struct xkb_level *
xkb_keymap_key_get_level(struct xkb_keymap *keymap, const struct xkb_key *key,
xkb_layout_index_t layout, xkb_level_index_t level)
{
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups don't fit");
layout = XkbWrapGroupIntoRange((int32_t) layout, key->num_groups,
key->out_of_range_group_action,
key->out_of_range_group_number);
if (layout == XKB_LAYOUT_INVALID)
return NULL;
if (level >= XkbKeyNumLevels(key, layout))
return NULL;
return &key->groups[layout].levels[level];
}
/**
* As below, but takes an explicit layout/level rather than state.
*/
int
xkb_keymap_key_get_syms_by_level(struct xkb_keymap *keymap,
xkb_keycode_t kc,
xkb_layout_index_t layout,
xkb_level_index_t level,
const xkb_keysym_t **syms_out)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
goto err;
const struct xkb_level* const leveli =
xkb_keymap_key_get_level(keymap, key, layout, level);
if (!leveli)
goto err;
const xkb_keysym_count_t num_syms = leveli->num_syms;
if (num_syms == 0)
goto err;
if (num_syms == 1)
*syms_out = &leveli->s.sym;
else
*syms_out = leveli->s.syms;
return (int) num_syms;
err:
*syms_out = NULL;
return 0;
}
xkb_keycode_t
xkb_keymap_min_keycode(struct xkb_keymap *keymap)
{
return keymap->min_key_code;
}
xkb_keycode_t
xkb_keymap_max_keycode(struct xkb_keymap *keymap)
{
return keymap->max_key_code;
}
void
xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
void *data)
{
struct xkb_key *key;
xkb_keys_foreach(key, keymap)
iter(keymap, key->keycode, data);
}
const char *
xkb_keymap_key_get_name(struct xkb_keymap *keymap, xkb_keycode_t kc)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
return NULL;
return xkb_atom_text(keymap->ctx, key->name);
}
xkb_keycode_t
xkb_keymap_key_by_name(struct xkb_keymap *keymap, const char *name)
{
struct xkb_key *key;
xkb_atom_t atom;
atom = xkb_atom_lookup(keymap->ctx, name);
if (atom) {
xkb_atom_t ratom = XkbResolveKeyAlias(keymap, atom);
if (ratom)
atom = ratom;
}
if (!atom)
return XKB_KEYCODE_INVALID;
xkb_keys_foreach(key, keymap) {
if (key->name == atom)
return key->keycode;
}
return XKB_KEYCODE_INVALID;
}
/**
* Simple boolean specifying whether or not the key should repeat.
*/
int
xkb_keymap_key_repeats(struct xkb_keymap *keymap, xkb_keycode_t kc)
{
const struct xkb_key *key = XkbKey(keymap, kc);
if (!key)
return 0;
return key->repeats;
}