| 1 |
/* |
| 2 |
* Copyright (c) 1994 by Silicon Graphics Computer Systems, Inc. |
| 3 |
* SPDX-License-Identifier: HPND |
| 4 |
*/ |
| 5 |
|
| 6 |
#include "config.h" |
| 7 |
|
| 8 |
#include <assert.h> |
| 9 |
#include <limits.h> |
| 10 |
#include <stdint.h> |
| 11 |
#include <stdlib.h> |
| 12 |
#include <string.h> |
| 13 |
|
| 14 |
#include "context.h" |
| 15 |
#include "messages-codes.h" |
| 16 |
#include "xkbcommon/xkbcommon.h" |
| 17 |
#include "xkbcomp-priv.h" |
| 18 |
#include "atom.h" |
| 19 |
#include "darray.h" |
| 20 |
#include "keymap.h" |
| 21 |
#include "text.h" |
| 22 |
#include "expr.h" |
| 23 |
#include "include.h" |
| 24 |
#include "util-mem.h" |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
xkb_keycode_t keycode; |
| 28 |
xkb_atom_t name; |
| 29 |
} HighKeycodeEntry; |
| 30 |
|
| 31 |
typedef struct { |
| 32 |
/** Minimum keycode */ |
| 33 |
xkb_keycode_t min; |
| 34 |
/** |
| 35 |
* keycode -> name mapping, stored contiguously |
| 36 |
* keycode <= XKB_KEYCODE_MAX_CONTIGUOUS |
| 37 |
*/ |
| 38 |
darray(xkb_atom_t) low; |
| 39 |
/** |
| 40 |
* keycode -> name mapping, sorted entries, possibly noncontiguous keycodes |
| 41 |
* keycode > XKB_KEYCODE_MAX_CONTIGUOUS |
| 42 |
*/ |
| 43 |
darray(HighKeycodeEntry) high; |
| 44 |
/** |
| 45 |
* name -> keycode mapping |
| 46 |
*/ |
| 47 |
darray(KeycodeMatch) names; |
| 48 |
} KeycodeStore; |
| 49 |
|
| 50 |
static inline void |
| 51 |
keycode_store_init(KeycodeStore *store) |
| 52 |
{ |
| 53 |
darray_init(store->low); |
| 54 |
darray_init(store->high); |
| 55 |
darray_init(store->names); |
| 56 |
static_assert(XKB_KEYCODE_INVALID > XKB_KEYCODE_MAX, |
| 57 |
"Hey, you can't be changing stuff like that!"); |
| 58 |
store->min = XKB_KEYCODE_INVALID; |
| 59 |
} |
| 60 |
|
| 61 |
static inline void |
| 62 |
keycode_store_free(KeycodeStore *store) |
| 63 |
{ |
| 64 |
darray_free(store->low); |
| 65 |
darray_free(store->high); |
| 66 |
darray_free(store->names); |
| 67 |
} |
| 68 |
|
| 69 |
static inline void |
| 70 |
keycode_store_update_key(KeycodeStore *store, KeycodeMatch match, xkb_atom_t name) |
| 71 |
{ |
| 72 |
if (unlikely(!match.found || match.is_alias)) { |
| 73 |
return; |
| 74 |
} else if (match.key.low) { |
| 75 |
/* assume valid index */ |
| 76 |
assert(match.key.index < darray_size(store->low)); |
| 77 |
darray_item(store->low, match.key.index) = name; |
| 78 |
} else { |
| 79 |
/* assume valid index */ |
| 80 |
assert(match.key.index < darray_size(store->high)); |
| 81 |
darray_item(store->high, match.key.index).name = name; |
| 82 |
} |
| 83 |
|
| 84 |
/* Names lookup */ |
| 85 |
if (name >= darray_size(store->names)) { |
| 86 |
darray_resize0(store->names, name + 1); |
| 87 |
} |
| 88 |
darray_item(store->names, name) = match; |
| 89 |
} |
| 90 |
|
| 91 |
static bool |
| 92 |
keycode_store_insert_key(KeycodeStore *store, xkb_keycode_t kc, xkb_atom_t name) |
| 93 |
{ |
| 94 |
if (name >= darray_size(store->names)) { |
| 95 |
darray_resize0(store->names, name + 1); |
| 96 |
} |
| 97 |
if (kc <= XKB_KEYCODE_MAX_CONTIGUOUS) { |
| 98 |
/* Low keycode */ |
| 99 |
if (kc >= (xkb_keycode_t) darray_size(store->low)) |
| 100 |
darray_resize0(store->low, kc + 1); |
| 101 |
darray_item(store->low, kc) = name; |
| 102 |
if (kc < store->min) |
| 103 |
store->min = kc; |
| 104 |
darray_item(store->names, name) = (KeycodeMatch) { |
| 105 |
.key = { |
| 106 |
.found = true, |
| 107 |
.low = true, |
| 108 |
.is_alias = false, |
| 109 |
.index = kc |
| 110 |
} |
| 111 |
}; |
| 112 |
} else { |
| 113 |
/* High keycode: insert into a sorted list */ |
| 114 |
const darray_size_t idx = darray_size(store->high); |
| 115 |
if (idx && darray_item(store->high, idx - 1).keycode > kc) { |
| 116 |
/* |
| 117 |
* Slow path: need to sort the keys. Since we maintain the list |
| 118 |
* sorted, we simply need to look for the insertion index. |
| 119 |
*/ |
| 120 |
darray_size_t lower = 0; |
| 121 |
darray_size_t upper = idx; |
| 122 |
while (lower < upper) { |
| 123 |
const darray_size_t mid = lower + (upper - 1 - lower) / 2; |
| 124 |
const HighKeycodeEntry * const entry = |
| 125 |
&darray_item(store->high, mid); |
| 126 |
if (entry->keycode < kc) { |
| 127 |
lower = mid + 1; |
| 128 |
} else if (entry->keycode > kc) { |
| 129 |
upper = mid; |
| 130 |
} else { |
| 131 |
/* Unreachable: there is no repetition in the list */ |
| 132 |
assert(entry->keycode != kc); |
| 133 |
} |
| 134 |
} |
| 135 |
assert(lower < idx); |
| 136 |
assert(darray_item(store->high, lower).keycode > kc); |
| 137 |
|
| 138 |
/* Update references to entries that will be moved */ |
| 139 |
HighKeycodeEntry *entry; |
| 140 |
darray_foreach_from(entry, store->high, lower) { |
| 141 |
darray_item(store->names, entry->name).key.index++; |
| 142 |
} |
| 143 |
|
| 144 |
darray_insert(store->high, lower, |
| 145 |
(HighKeycodeEntry){.keycode = kc, .name = name}); |
| 146 |
darray_item(store->names, name) = (KeycodeMatch) { |
| 147 |
.key = { |
| 148 |
.found = true, |
| 149 |
.low = false, |
| 150 |
.is_alias = false, |
| 151 |
.index = lower |
| 152 |
} |
| 153 |
}; |
| 154 |
} else { |
| 155 |
/* Fast path: no need to sort */ |
| 156 |
darray_append(store->high, |
| 157 |
(HighKeycodeEntry){.keycode = kc, .name = name}); |
| 158 |
darray_item(store->names, name) = (KeycodeMatch) { |
| 159 |
.key = { |
| 160 |
.found = true, |
| 161 |
.low = false, |
| 162 |
.is_alias = false, |
| 163 |
.index = idx |
| 164 |
} |
| 165 |
}; |
| 166 |
} |
| 167 |
if (darray_empty(store->low)) |
| 168 |
store->min = darray_item(store->high, 0).keycode; |
| 169 |
} |
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
static inline bool |
| 174 |
keycode_store_insert_alias(KeycodeStore *store, xkb_atom_t alias, xkb_atom_t real) |
| 175 |
{ |
| 176 |
if (alias >= darray_size(store->names)) { |
| 177 |
darray_resize0(store->names, alias + 1); |
| 178 |
} |
| 179 |
darray_item(store->names, alias) = (KeycodeMatch) { |
| 180 |
.alias = { |
| 181 |
.found = true, |
| 182 |
.is_alias = true, |
| 183 |
.real = real |
| 184 |
} |
| 185 |
}; |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
static inline bool |
| 190 |
keycode_store_update_alias(KeycodeStore *store, xkb_atom_t alias, xkb_atom_t real) |
| 191 |
{ |
| 192 |
darray_item(store->names, alias).alias.real = real; |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
static inline void |
| 197 |
keycode_store_delete_name(const KeycodeStore *store, xkb_atom_t name) |
| 198 |
{ |
| 199 |
darray_item(store->names, name).found = false; |
| 200 |
} |
| 201 |
|
| 202 |
static void |
| 203 |
keycode_store_delete_key(KeycodeStore *store, const KeycodeMatch match) |
| 204 |
{ |
| 205 |
if (unlikely(!match.found || match.is_alias)) { |
| 206 |
return; |
| 207 |
} else if (match.key.low) { |
| 208 |
/* assume valid index */ |
| 209 |
assert(match.key.index < darray_size(store->low)); |
| 210 |
darray_item(store->names, |
| 211 |
darray_item(store->low, match.key.index)).found = false; |
| 212 |
if (match.key.index + 1u == darray_size(store->low)) { |
| 213 |
/* Highest low keycode: shrink */ |
| 214 |
if (store->min == match.key.index) { |
| 215 |
/* No low keycode left */ |
| 216 |
darray_size(store->low) = 0; |
| 217 |
} else { |
| 218 |
/* Look for previous defined low keycode */ |
| 219 |
for (darray_size_t idx = match.key.index; idx > 0; idx--) { |
| 220 |
if (darray_item(store->low, idx - 1) != XKB_ATOM_NONE) { |
| 221 |
darray_size(store->low) = idx; |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
} else { |
| 227 |
/* Lower keycode: reset */ |
| 228 |
darray_item(store->low, match.key.index) = XKB_ATOM_NONE; |
| 229 |
} |
| 230 |
} else { |
| 231 |
assert(match.key.index < darray_size(store->high)); /* assume valid index */ |
| 232 |
darray_item(store->names, |
| 233 |
darray_item(store->high, match.key.index).name).found = false; |
| 234 |
darray_remove(store->high, match.key.index); |
| 235 |
/* Update LUT indexes of high codes after the deleted one, if any */ |
| 236 |
KeycodeMatch *entry; |
| 237 |
darray_foreach(entry, store->names) { |
| 238 |
if (entry->found && !entry->is_alias && !entry->key.low && |
| 239 |
entry->key.index > match.key.index) { |
| 240 |
entry->key.index--; |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
/* Update bounds */ |
| 246 |
if (darray_empty(store->low)) { |
| 247 |
store->min = (darray_empty(store->high)) |
| 248 |
? XKB_KEYCODE_INVALID |
| 249 |
: darray_item(store->high, 0).keycode; |
| 250 |
} else { |
| 251 |
for (xkb_keycode_t kc = store->min; kc < darray_size(store->low); kc++) { |
| 252 |
if (darray_item(store->low, kc) != XKB_ATOM_NONE) { |
| 253 |
store->min = kc; |
| 254 |
break; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
static inline xkb_keycode_t |
| 261 |
keycode_store_get_keycode(const KeycodeStore *store, KeycodeMatch match) |
| 262 |
{ |
| 263 |
if (!match.found || match.is_alias) { |
| 264 |
return XKB_KEYCODE_INVALID; |
| 265 |
} else if (match.key.low) { |
| 266 |
/* assume valid index */ |
| 267 |
assert(match.key.index < darray_size(store->low)); |
| 268 |
return (xkb_keycode_t) match.key.index; |
| 269 |
} else { |
| 270 |
/* assume valid index */ |
| 271 |
assert(match.key.index < darray_size(store->high)); |
| 272 |
return darray_item(store->high, match.key.index).keycode; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static inline xkb_atom_t |
| 277 |
keycode_store_get_key_name(const KeycodeStore *store, KeycodeMatch match) |
| 278 |
{ |
| 279 |
if (!match.found || match.is_alias) { |
| 280 |
return XKB_ATOM_NONE; |
| 281 |
} else if (match.key.low) { |
| 282 |
/* assume valid index */ |
| 283 |
assert(match.key.index < darray_size(store->low)); |
| 284 |
return darray_item(store->low, match.key.index); |
| 285 |
} else { |
| 286 |
/* assume valid index */ |
| 287 |
assert(match.key.index < darray_size(store->high)); |
| 288 |
return darray_item(store->high, match.key.index).name; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
static KeycodeMatch |
| 293 |
keycode_store_lookup_keycode(const KeycodeStore *store, xkb_keycode_t kc) |
| 294 |
{ |
| 295 |
/* Low keycodes */ |
| 296 |
if (kc < (xkb_keycode_t) darray_size(store->low)) { |
| 297 |
return (KeycodeMatch) { |
| 298 |
.key = { |
| 299 |
.found = true, |
| 300 |
.low = true, |
| 301 |
.is_alias = false, |
| 302 |
.index = kc |
| 303 |
} |
| 304 |
}; |
| 305 |
} else if (kc <= XKB_KEYCODE_MAX_CONTIGUOUS) { |
| 306 |
return (KeycodeMatch) { .found = false }; |
| 307 |
} |
| 308 |
|
| 309 |
/* High keycodes: use binary search */ |
| 310 |
darray_size_t lower = 0; |
| 311 |
darray_size_t upper = darray_size(store->high); |
| 312 |
while (lower < upper) { |
| 313 |
const darray_size_t mid = lower + (upper - 1 - lower) / 2; |
| 314 |
HighKeycodeEntry * const entry = &darray_item(store->high, mid); |
| 315 |
if (entry->keycode < kc) { |
| 316 |
lower = mid + 1; |
| 317 |
} else if (entry->keycode > kc) { |
| 318 |
upper = mid; |
| 319 |
} else { |
| 320 |
return (KeycodeMatch) { |
| 321 |
.key = { |
| 322 |
.found = true, |
| 323 |
.low = false, |
| 324 |
.is_alias = false, |
| 325 |
.index = mid |
| 326 |
} |
| 327 |
}; |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
return (KeycodeMatch) { .found = false }; |
| 332 |
} |
| 333 |
|
| 334 |
static KeycodeMatch |
| 335 |
keycode_store_lookup_name(const KeycodeStore *store, xkb_atom_t name) |
| 336 |
{ |
| 337 |
if (name >= darray_size(store->names)) { |
| 338 |
return (KeycodeMatch) { .found = false, .is_alias = false }; |
| 339 |
} else { |
| 340 |
return darray_item(store->names, name); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/***====================================================================***/ |
| 345 |
|
| 346 |
typedef struct { |
| 347 |
enum merge_mode merge; |
| 348 |
|
| 349 |
xkb_atom_t name; |
| 350 |
} LedNameInfo; |
| 351 |
|
| 352 |
typedef struct { |
| 353 |
char *name; |
| 354 |
int errorCount; |
| 355 |
unsigned int include_depth; |
| 356 |
|
| 357 |
KeycodeStore keycodes; |
| 358 |
LedNameInfo led_names[XKB_MAX_LEDS]; |
| 359 |
xkb_led_index_t num_led_names; |
| 360 |
|
| 361 |
struct xkb_context *ctx; |
| 362 |
} KeyNamesInfo; |
| 363 |
|
| 364 |
/***====================================================================***/ |
| 365 |
|
| 366 |
static LedNameInfo * |
| 367 |
FindLedByName(KeyNamesInfo *info, xkb_atom_t name, |
| 368 |
xkb_led_index_t *idx_out) |
| 369 |
{ |
| 370 |
for (xkb_led_index_t idx = 0; idx < info->num_led_names; idx++) { |
| 371 |
LedNameInfo *ledi = &info->led_names[idx]; |
| 372 |
if (ledi->name == name) { |
| 373 |
*idx_out = idx; |
| 374 |
return ledi; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return NULL; |
| 379 |
} |
| 380 |
|
| 381 |
static bool |
| 382 |
AddLedName(KeyNamesInfo *info, bool same_file, |
| 383 |
LedNameInfo *new, xkb_led_index_t new_idx, bool report) |
| 384 |
{ |
| 385 |
xkb_led_index_t old_idx; |
| 386 |
LedNameInfo *old; |
| 387 |
const bool replace = (new->merge != MERGE_AUGMENT); |
| 388 |
|
| 389 |
/* Check if LED with the same name already exists. */ |
| 390 |
old = FindLedByName(info, new->name, &old_idx); |
| 391 |
if (old) { |
| 392 |
if (old_idx == new_idx) { |
| 393 |
if (report) |
| 394 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 395 |
"Multiple indicators named \"%s\"; " |
| 396 |
"Identical definitions ignored\n", |
| 397 |
xkb_atom_text(info->ctx, new->name)); |
| 398 |
return true; |
| 399 |
} |
| 400 |
|
| 401 |
if (report) { |
| 402 |
xkb_led_index_t use = (replace ? new_idx + 1 : old_idx + 1); |
| 403 |
xkb_led_index_t ignore = (replace ? old_idx + 1 : new_idx + 1); |
| 404 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 405 |
"Multiple indicators named %s; Using %u, ignoring %u\n", |
| 406 |
xkb_atom_text(info->ctx, new->name), use, ignore); |
| 407 |
} |
| 408 |
|
| 409 |
if (replace) { |
| 410 |
/* Unset previous */ |
| 411 |
old->name = XKB_ATOM_NONE; |
| 412 |
} else { |
| 413 |
return true; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
if (new_idx >= info->num_led_names) |
| 418 |
info->num_led_names = new_idx + 1; |
| 419 |
|
| 420 |
/* Check if LED with the same index already exists. */ |
| 421 |
old = &info->led_names[new_idx]; |
| 422 |
if (old->name != XKB_ATOM_NONE) { |
| 423 |
if (report) { |
| 424 |
const xkb_atom_t use = (replace ? new->name : old->name); |
| 425 |
const xkb_atom_t ignore = (replace ? old->name : new->name); |
| 426 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 427 |
"Multiple names for indicator %u; " |
| 428 |
"Using %s, ignoring %s\n", new_idx + 1, |
| 429 |
xkb_atom_text(info->ctx, use), |
| 430 |
xkb_atom_text(info->ctx, ignore)); |
| 431 |
} |
| 432 |
|
| 433 |
if (replace) |
| 434 |
*old = *new; |
| 435 |
|
| 436 |
return true; |
| 437 |
} |
| 438 |
|
| 439 |
*old = *new; |
| 440 |
return true; |
| 441 |
} |
| 442 |
|
| 443 |
static void |
| 444 |
ClearKeyNamesInfo(KeyNamesInfo *info) |
| 445 |
{ |
| 446 |
free(info->name); |
| 447 |
keycode_store_free(&info->keycodes); |
| 448 |
} |
| 449 |
|
| 450 |
static void |
| 451 |
InitKeyNamesInfo(KeyNamesInfo *info, struct xkb_context *ctx, |
| 452 |
unsigned int include_depth) |
| 453 |
{ |
| 454 |
memset(info, 0, sizeof(*info)); |
| 455 |
info->ctx = ctx; |
| 456 |
info->include_depth = include_depth; |
| 457 |
keycode_store_init(&info->keycodes); |
| 458 |
} |
| 459 |
|
| 460 |
static bool |
| 461 |
AddKeyName(KeyNamesInfo *info, xkb_keycode_t kc, xkb_atom_t name, |
| 462 |
enum merge_mode merge, bool report) |
| 463 |
{ |
| 464 |
#ifdef NDEBUG |
| 465 |
const |
| 466 |
#endif |
| 467 |
KeycodeMatch match_name = keycode_store_lookup_name(&info->keycodes, name); |
| 468 |
if (match_name.found) { |
| 469 |
const bool clobber = (merge != MERGE_AUGMENT); |
| 470 |
|
| 471 |
if (match_name.is_alias) { |
| 472 |
/* |
| 473 |
* There is already an alias with this name. |
| 474 |
* |
| 475 |
* Contrary to Xorg’s xkbcomp, keys and aliases share the same |
| 476 |
* namespace. So we need to resolve name conflicts as they arise, |
| 477 |
* while xkbcomp will resolve them just before copying aliases into |
| 478 |
* the keymap. |
| 479 |
*/ |
| 480 |
|
| 481 |
if (report) { |
| 482 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_NAME, |
| 483 |
"Key name %s already assigned to an alias; " |
| 484 |
"Using %s, ignoring %s\n", |
| 485 |
KeyNameText(info->ctx, name), |
| 486 |
(clobber ? "key" : "alias"), |
| 487 |
(clobber ? "alias" : "key")); |
| 488 |
} |
| 489 |
|
| 490 |
if (clobber) { |
| 491 |
/* |
| 492 |
* Override the alias. If there is a conflict with the keycode |
| 493 |
* afterwards, the old key entry will be also overriden thanks |
| 494 |
* to “clobber”. |
| 495 |
*/ |
| 496 |
keycode_store_delete_name(&info->keycodes, name); |
| 497 |
#ifndef NDEBUG |
| 498 |
match_name.found = false; |
| 499 |
#endif |
| 500 |
} else { |
| 501 |
return true; |
| 502 |
} |
| 503 |
} else { |
| 504 |
const xkb_keycode_t old_kc = |
| 505 |
keycode_store_get_keycode(&info->keycodes, match_name); |
| 506 |
assert(old_kc != XKB_KEYCODE_INVALID); |
| 507 |
if (old_kc != kc) { |
| 508 |
/* There is already a different key with this name. */ |
| 509 |
|
| 510 |
if (report) { |
| 511 |
const xkb_keycode_t use = (clobber) ? kc : old_kc; |
| 512 |
const xkb_keycode_t ignore = (clobber) ? old_kc : kc; |
| 513 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_NAME, |
| 514 |
"Key name %s assigned to multiple keys; " |
| 515 |
"Using %"PRIu32", ignoring %"PRIu32"\n", |
| 516 |
KeyNameText(info->ctx, name), use, ignore); |
| 517 |
} |
| 518 |
|
| 519 |
if (clobber) { |
| 520 |
/* Remove conflicting key name mapping */ |
| 521 |
keycode_store_delete_key(&info->keycodes, match_name); |
| 522 |
} else { |
| 523 |
return true; |
| 524 |
} |
| 525 |
} |
| 526 |
} |
| 527 |
} |
| 528 |
|
| 529 |
const KeycodeMatch match_kc = keycode_store_lookup_keycode(&info->keycodes, kc); |
| 530 |
const xkb_atom_t old_name = keycode_store_get_key_name(&info->keycodes, match_kc); |
| 531 |
if (old_name != XKB_ATOM_NONE) { |
| 532 |
/* There is already a key with this keycode. */ |
| 533 |
|
| 534 |
if (old_name == name) { |
| 535 |
assert(keycode_store_get_keycode(&info->keycodes, match_name) == kc); |
| 536 |
if (report) |
| 537 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 538 |
"Multiple identical key name definitions; " |
| 539 |
"Later occurrences of \"%s = %"PRIu32"\" ignored\n", |
| 540 |
KeyNameText(info->ctx, old_name), kc); |
| 541 |
return true; |
| 542 |
} |
| 543 |
|
| 544 |
const bool clobber = (merge != MERGE_AUGMENT); |
| 545 |
if (report) { |
| 546 |
const char* const kname = KeyNameText(info->ctx, name); |
| 547 |
const char* const old_kname = KeyNameText(info->ctx, old_name); |
| 548 |
const char* const use = (clobber) ? kname : old_kname; |
| 549 |
const char* const ignore = (clobber) ? old_kname : kname; |
| 550 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 551 |
"Multiple names for keycode %"PRIu32"; " |
| 552 |
"Using %s, ignoring %s\n", kc, use, ignore); |
| 553 |
} |
| 554 |
if (clobber) { |
| 555 |
keycode_store_delete_name(&info->keycodes, old_name); |
| 556 |
keycode_store_update_key(&info->keycodes, match_kc, name); |
| 557 |
} |
| 558 |
} else { |
| 559 |
/* No previous keycode */ |
| 560 |
if (!keycode_store_insert_key(&info->keycodes, kc, name)) { |
| 561 |
log_err(info->ctx, XKB_ERROR_ALLOCATION_ERROR, |
| 562 |
"Cannot add keycode\n"); |
| 563 |
return false; |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
return true; |
| 568 |
} |
| 569 |
|
| 570 |
/***====================================================================***/ |
| 571 |
|
| 572 |
static bool |
| 573 |
HandleAliasDef(KeyNamesInfo *info, const KeyAliasDef *def, bool report); |
| 574 |
|
| 575 |
static void |
| 576 |
MergeKeycodeStores(KeyNamesInfo *into, KeyNamesInfo *from, |
| 577 |
enum merge_mode merge, bool report) |
| 578 |
{ |
| 579 |
if (darray_empty(into->keycodes.low) && darray_empty(into->keycodes.high) && |
| 580 |
darray_empty(into->keycodes.names)) { |
| 581 |
/* Fast path: steal “from” */ |
| 582 |
into->keycodes = from->keycodes; |
| 583 |
darray_init(from->keycodes.low); |
| 584 |
darray_init(from->keycodes.high); |
| 585 |
darray_init(from->keycodes.names); |
| 586 |
} else { |
| 587 |
/* Slow path: check for conflicts */ |
| 588 |
|
| 589 |
/* Low keycodes */ |
| 590 |
for (xkb_keycode_t kc = from->keycodes.min; |
| 591 |
kc < (xkb_keycode_t) darray_size(from->keycodes.low); |
| 592 |
kc++) { |
| 593 |
/* Skip undefined keycodes */ |
| 594 |
const xkb_atom_t name = darray_item(from->keycodes.low, kc); |
| 595 |
if (name == XKB_ATOM_NONE) |
| 596 |
continue; |
| 597 |
|
| 598 |
if (!AddKeyName(into, kc, name, merge, report)) |
| 599 |
into->errorCount++; |
| 600 |
} |
| 601 |
|
| 602 |
/* High keycodes */ |
| 603 |
const HighKeycodeEntry *new; |
| 604 |
darray_foreach(new, from->keycodes.high) { |
| 605 |
assert(new->name != XKB_ATOM_NONE); |
| 606 |
if (!AddKeyName(into, new->keycode, new->name, merge, report)) |
| 607 |
into->errorCount++; |
| 608 |
} |
| 609 |
|
| 610 |
/* Aliases. */ |
| 611 |
KeycodeMatch *match; |
| 612 |
xkb_atom_t alias; |
| 613 |
darray_enumerate(alias, match, from->keycodes.names) { |
| 614 |
if (!match->found || !match->is_alias) |
| 615 |
continue; |
| 616 |
|
| 617 |
const KeyAliasDef def = { |
| 618 |
.merge = merge, |
| 619 |
.alias = alias, |
| 620 |
.real = match->alias.real |
| 621 |
}; |
| 622 |
if (!HandleAliasDef(into, &def, report)) |
| 623 |
into->errorCount++; |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
static void |
| 629 |
MergeIncludedKeycodes(KeyNamesInfo *into, KeyNamesInfo *from, |
| 630 |
enum merge_mode merge, bool report) |
| 631 |
{ |
| 632 |
if (from->errorCount > 0) { |
| 633 |
into->errorCount += from->errorCount; |
| 634 |
return; |
| 635 |
} |
| 636 |
|
| 637 |
if (into->name == NULL) { |
| 638 |
into->name = steal(&from->name); |
| 639 |
} |
| 640 |
|
| 641 |
/* Merge key names. */ |
| 642 |
MergeKeycodeStores(into, from, merge, report); |
| 643 |
|
| 644 |
/* Merge LED names. */ |
| 645 |
if (into->num_led_names == 0) { |
| 646 |
memcpy(into->led_names, from->led_names, |
| 647 |
sizeof(*from->led_names) * from->num_led_names); |
| 648 |
into->num_led_names = from->num_led_names; |
| 649 |
from->num_led_names = 0; |
| 650 |
} |
| 651 |
else { |
| 652 |
for (xkb_led_index_t idx = 0; idx < from->num_led_names; idx++) { |
| 653 |
LedNameInfo *ledi = &from->led_names[idx]; |
| 654 |
|
| 655 |
if (ledi->name == XKB_ATOM_NONE) |
| 656 |
continue; |
| 657 |
|
| 658 |
ledi->merge = merge; |
| 659 |
if (!AddLedName(into, false, ledi, idx, report)) |
| 660 |
into->errorCount++; |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
static void |
| 666 |
HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file); |
| 667 |
|
| 668 |
static bool |
| 669 |
HandleIncludeKeycodes(KeyNamesInfo *info, IncludeStmt *include, bool report) |
| 670 |
{ |
| 671 |
KeyNamesInfo included; |
| 672 |
|
| 673 |
if (ExceedsIncludeMaxDepth(info->ctx, info->include_depth)) { |
| 674 |
info->errorCount += 10; |
| 675 |
return false; |
| 676 |
} |
| 677 |
|
| 678 |
InitKeyNamesInfo(&included, info->ctx, 0 /* unused */); |
| 679 |
included.name = steal(&include->stmt); |
| 680 |
|
| 681 |
for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) { |
| 682 |
KeyNamesInfo next_incl; |
| 683 |
XkbFile *file; |
| 684 |
|
| 685 |
char path[PATH_MAX]; |
| 686 |
file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_KEYCODES, |
| 687 |
path, sizeof(path)); |
| 688 |
if (!file) { |
| 689 |
info->errorCount += 10; |
| 690 |
ClearKeyNamesInfo(&included); |
| 691 |
return false; |
| 692 |
} |
| 693 |
|
| 694 |
InitKeyNamesInfo(&next_incl, info->ctx, info->include_depth + 1); |
| 695 |
|
| 696 |
HandleKeycodesFile(&next_incl, file); |
| 697 |
|
| 698 |
MergeIncludedKeycodes(&included, &next_incl, stmt->merge, report); |
| 699 |
|
| 700 |
ClearKeyNamesInfo(&next_incl); |
| 701 |
FreeXkbFile(file); |
| 702 |
} |
| 703 |
|
| 704 |
MergeIncludedKeycodes(info, &included, include->merge, report); |
| 705 |
ClearKeyNamesInfo(&included); |
| 706 |
|
| 707 |
return (info->errorCount == 0); |
| 708 |
} |
| 709 |
|
| 710 |
static bool |
| 711 |
HandleKeycodeDef(KeyNamesInfo *info, KeycodeDef *stmt, bool report) |
| 712 |
{ |
| 713 |
if (stmt->value < 0 || stmt->value > XKB_KEYCODE_MAX) { |
| 714 |
log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 715 |
"Illegal keycode %"PRId64": must be between 0..%u; " |
| 716 |
"Key ignored\n", stmt->value, XKB_KEYCODE_MAX); |
| 717 |
return false; |
| 718 |
} |
| 719 |
|
| 720 |
return AddKeyName(info, (xkb_keycode_t) stmt->value, |
| 721 |
stmt->name, stmt->merge, report); |
| 722 |
} |
| 723 |
|
| 724 |
static bool |
| 725 |
HandleAliasDef(KeyNamesInfo *info, const KeyAliasDef *def, bool report) |
| 726 |
{ |
| 727 |
const KeycodeMatch match_name = keycode_store_lookup_name(&info->keycodes, |
| 728 |
def->alias); |
| 729 |
if (match_name.found) { |
| 730 |
const bool clobber = (def->merge != MERGE_AUGMENT); |
| 731 |
if (match_name.is_alias) { |
| 732 |
if (def->real == match_name.alias.real) { |
| 733 |
if (report) |
| 734 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_NAME, |
| 735 |
"Alias of %s for %s declared more than once; " |
| 736 |
"First definition ignored\n", |
| 737 |
KeyNameText(info->ctx, def->alias), |
| 738 |
KeyNameText(info->ctx, def->real)); |
| 739 |
} else { |
| 740 |
const xkb_atom_t use = (clobber) |
| 741 |
? def->real |
| 742 |
: match_name.alias.real; |
| 743 |
const xkb_atom_t ignore = (clobber) |
| 744 |
? match_name.alias.real |
| 745 |
: def->real; |
| 746 |
|
| 747 |
if (report) |
| 748 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_NAME, |
| 749 |
"Multiple definitions for alias %s; " |
| 750 |
"Using %s, ignoring %s\n", |
| 751 |
KeyNameText(info->ctx, def->alias), |
| 752 |
KeyNameText(info->ctx, use), |
| 753 |
KeyNameText(info->ctx, ignore)); |
| 754 |
|
| 755 |
keycode_store_update_alias(&info->keycodes, def->alias, use); |
| 756 |
} |
| 757 |
return true; |
| 758 |
} else { |
| 759 |
/* There is already a real key with this name. |
| 760 |
* |
| 761 |
* Contrary to Xorg’s xkbcomp, keys and aliases share the same |
| 762 |
* namespace. So we need to resolve name conflicts as they arise, |
| 763 |
* while xkbcomp will resolve them just before copying aliases into |
| 764 |
* the keymap. |
| 765 |
* |
| 766 |
* Also contrary to xkbcomp, we enable aliases to override keys. |
| 767 |
*/ |
| 768 |
if (report) { |
| 769 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_NAME, |
| 770 |
"Alias name %s already assigned to a real key; " |
| 771 |
"Using %s, ignoring %s\n", |
| 772 |
KeyNameText(info->ctx, def->alias), |
| 773 |
(clobber ? "alias" : "key"), |
| 774 |
(clobber ? "key" : "alias")); |
| 775 |
} |
| 776 |
|
| 777 |
if (clobber) { |
| 778 |
/* |
| 779 |
* Note that we override the key even if the alias is proved |
| 780 |
* invalid afterwards. This would be a bug in the keycodes |
| 781 |
* files or rules, not libxkbcommon. |
| 782 |
*/ |
| 783 |
keycode_store_delete_key(&info->keycodes, match_name); |
| 784 |
} else { |
| 785 |
return true; |
| 786 |
} |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
return keycode_store_insert_alias(&info->keycodes, def->alias, def->real); |
| 791 |
} |
| 792 |
|
| 793 |
static bool |
| 794 |
HandleKeyNameVar(KeyNamesInfo *info, VarDef *stmt) |
| 795 |
{ |
| 796 |
const char *elem, *field; |
| 797 |
ExprDef *arrayNdx; |
| 798 |
|
| 799 |
if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, &arrayNdx)) |
| 800 |
return false; |
| 801 |
|
| 802 |
if (elem) { |
| 803 |
log_err(info->ctx, XKB_ERROR_GLOBAL_DEFAULTS_WRONG_SCOPE, |
| 804 |
"Cannot set global defaults for \"%s\" element; " |
| 805 |
"Assignment to \"%s.%s\" ignored\n", elem, elem, field); |
| 806 |
return false; |
| 807 |
} |
| 808 |
|
| 809 |
if (!istreq(field, "minimum") && !istreq(field, "maximum")) { |
| 810 |
log_err(info->ctx, XKB_ERROR_UNKNOWN_DEFAULT_FIELD, |
| 811 |
"Default defined for unknown field \"%s\"; Ignored\n", field); |
| 812 |
return false; |
| 813 |
} |
| 814 |
|
| 815 |
/* We ignore explicit min/max statements, we always use computed. */ |
| 816 |
return true; |
| 817 |
} |
| 818 |
|
| 819 |
static bool |
| 820 |
HandleLedNameDef(KeyNamesInfo *info, LedNameDef *def, bool report) |
| 821 |
{ |
| 822 |
if (def->ndx < 1 || def->ndx > XKB_MAX_LEDS) { |
| 823 |
info->errorCount++; |
| 824 |
log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 825 |
"Illegal indicator index (%"PRId64") specified; " |
| 826 |
"must be between 1 .. %u; Ignored\n", def->ndx, XKB_MAX_LEDS); |
| 827 |
return false; |
| 828 |
} |
| 829 |
|
| 830 |
xkb_atom_t name = XKB_ATOM_NONE; |
| 831 |
if (!ExprResolveString(info->ctx, def->name, &name)) { |
| 832 |
char buf[20]; |
| 833 |
snprintf(buf, sizeof(buf), "%"PRId64, def->ndx); |
| 834 |
info->errorCount++; |
| 835 |
return ReportBadType(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE, |
| 836 |
"indicator", "name", buf, "string"); |
| 837 |
} |
| 838 |
|
| 839 |
LedNameInfo ledi = {.merge = def->merge, .name = name}; |
| 840 |
return AddLedName(info, true, &ledi, (xkb_led_index_t) def->ndx - 1, report); |
| 841 |
} |
| 842 |
|
| 843 |
static void |
| 844 |
HandleKeycodesFile(KeyNamesInfo *info, XkbFile *file) |
| 845 |
{ |
| 846 |
bool ok; |
| 847 |
|
| 848 |
/* Conflicts in the same file probably require more attention than conflicts |
| 849 |
* with included files. */ |
| 850 |
const int verbosity = xkb_context_get_log_verbosity(info->ctx); |
| 851 |
const bool report_same_file = verbosity > 0; |
| 852 |
const bool report_include = verbosity > 7; |
| 853 |
|
| 854 |
free(info->name); |
| 855 |
info->name = strdup_safe(file->name); |
| 856 |
|
| 857 |
for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) { |
| 858 |
switch (stmt->type) { |
| 859 |
case STMT_INCLUDE: |
| 860 |
ok = HandleIncludeKeycodes(info, (IncludeStmt *) stmt, report_include); |
| 861 |
break; |
| 862 |
case STMT_KEYCODE: |
| 863 |
ok = HandleKeycodeDef(info, (KeycodeDef *) stmt, report_same_file); |
| 864 |
break; |
| 865 |
case STMT_ALIAS: |
| 866 |
ok = HandleAliasDef(info, (KeyAliasDef *) stmt, report_same_file); |
| 867 |
break; |
| 868 |
case STMT_VAR: |
| 869 |
ok = HandleKeyNameVar(info, (VarDef *) stmt); |
| 870 |
break; |
| 871 |
case STMT_LED_NAME: |
| 872 |
ok = HandleLedNameDef(info, (LedNameDef *) stmt, report_same_file); |
| 873 |
break; |
| 874 |
default: |
| 875 |
log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 876 |
"Keycode files may define key and indicator names only; " |
| 877 |
"Ignoring %s\n", stmt_type_to_string(stmt->type)); |
| 878 |
ok = false; |
| 879 |
break; |
| 880 |
} |
| 881 |
|
| 882 |
if (!ok) |
| 883 |
info->errorCount++; |
| 884 |
|
| 885 |
if (info->errorCount > 10) { |
| 886 |
log_err(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 887 |
"Abandoning keycodes file \"%s\"\n", |
| 888 |
safe_map_name(file)); |
| 889 |
break; |
| 890 |
} |
| 891 |
} |
| 892 |
} |
| 893 |
|
| 894 |
/***====================================================================***/ |
| 895 |
|
| 896 |
static bool |
| 897 |
CopyKeyNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info) |
| 898 |
{ |
| 899 |
if (darray_empty(info->keycodes.low) && darray_empty(info->keycodes.high)) { |
| 900 |
/* If the keymap has no keys, let’s just use the safest pair we know. */ |
| 901 |
assert(info->keycodes.min == XKB_KEYCODE_INVALID); |
| 902 |
keymap->min_key_code = 8; |
| 903 |
static_assert(255 < XKB_KEYCODE_MAX_CONTIGUOUS, ""); |
| 904 |
keymap->max_key_code = 255; |
| 905 |
keymap->num_keys = keymap->num_keys_low = keymap->max_key_code + 1; |
| 906 |
} else { |
| 907 |
assert(info->keycodes.min <= XKB_KEYCODE_MAX); |
| 908 |
keymap->min_key_code = info->keycodes.min; |
| 909 |
keymap->max_key_code = (darray_empty(info->keycodes.high)) |
| 910 |
? darray_size(info->keycodes.low) - 1 |
| 911 |
: darray_item( |
| 912 |
info->keycodes.high, |
| 913 |
darray_size(info->keycodes.high) - 1 |
| 914 |
).keycode; |
| 915 |
keymap->num_keys_low = darray_size(info->keycodes.low); |
| 916 |
keymap->num_keys = keymap->num_keys_low |
| 917 |
+ darray_size(info->keycodes.high); |
| 918 |
} |
| 919 |
|
| 920 |
struct xkb_key * const keys = calloc(keymap->num_keys, sizeof(*keys)); |
| 921 |
if (!keys) { |
| 922 |
keymap->num_keys = 0; |
| 923 |
keymap->min_key_code = keymap->max_key_code = XKB_KEYCODE_INVALID; |
| 924 |
return false; |
| 925 |
} |
| 926 |
|
| 927 |
/* Low keycodes */ |
| 928 |
for (xkb_keycode_t kc = keymap->min_key_code; kc < keymap->num_keys_low; kc++) |
| 929 |
keys[kc].keycode = kc; |
| 930 |
|
| 931 |
for (xkb_keycode_t kc = info->keycodes.min; |
| 932 |
kc < (xkb_keycode_t) darray_size(info->keycodes.low); |
| 933 |
kc++) |
| 934 |
keys[kc].name = darray_item(info->keycodes.low, kc); |
| 935 |
|
| 936 |
/* High keycodes */ |
| 937 |
xkb_keycode_t idx = keymap->num_keys_low; /* First high keycode index */ |
| 938 |
const HighKeycodeEntry *entry; |
| 939 |
darray_foreach(entry, info->keycodes.high) { |
| 940 |
assert(entry->name != XKB_ATOM_NONE); |
| 941 |
keys[idx].keycode = entry->keycode; |
| 942 |
keys[idx].name = entry->name; |
| 943 |
idx++; |
| 944 |
} |
| 945 |
|
| 946 |
keymap->keys = keys; |
| 947 |
return true; |
| 948 |
} |
| 949 |
|
| 950 |
static bool |
| 951 |
CopyKeycodeNameLUT(struct xkb_keymap *keymap, KeyNamesInfo *info) |
| 952 |
{ |
| 953 |
KeycodeMatch *match; |
| 954 |
xkb_atom_t name; |
| 955 |
darray_enumerate(name, match, info->keycodes.names) { |
| 956 |
if (!match->found) |
| 957 |
continue; |
| 958 |
|
| 959 |
if (match->is_alias) { |
| 960 |
/* |
| 961 |
* Do some sanity checking on the aliases. We can’t do it before |
| 962 |
* because keys and their aliases may be added out-of-order. |
| 963 |
*/ |
| 964 |
|
| 965 |
/* Check that ->real is a key. */ |
| 966 |
const KeycodeMatch match_real = |
| 967 |
keycode_store_lookup_name(&info->keycodes, match->alias.real); |
| 968 |
if (!match_real.found) { |
| 969 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_DETAILED, |
| 970 |
XKB_WARNING_UNDEFINED_KEYCODE, |
| 971 |
"Attempt to alias %s to non-existent key %s; Ignored\n", |
| 972 |
KeyNameText(info->ctx, name), |
| 973 |
KeyNameText(info->ctx, match->alias.real)); |
| 974 |
match->found = false; |
| 975 |
continue; |
| 976 |
} |
| 977 |
assert(!match_real.is_alias); |
| 978 |
} else if (!match->key.low) { |
| 979 |
/* Update to final index in keymap::keys */ |
| 980 |
match->key.index += keymap->num_keys_low; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
darray_shrink(info->keycodes.names); |
| 985 |
keymap->num_key_names = darray_size(info->keycodes.names); |
| 986 |
darray_steal(info->keycodes.names, &keymap->key_names, NULL); |
| 987 |
darray_init(info->keycodes.names); |
| 988 |
return true; |
| 989 |
} |
| 990 |
|
| 991 |
static bool |
| 992 |
CopyLedNamesToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info) |
| 993 |
{ |
| 994 |
keymap->num_leds = info->num_led_names; |
| 995 |
for (xkb_led_index_t idx = 0; idx < info->num_led_names; idx++) { |
| 996 |
LedNameInfo *ledi = &info->led_names[idx]; |
| 997 |
|
| 998 |
if (ledi->name == XKB_ATOM_NONE) |
| 999 |
continue; |
| 1000 |
|
| 1001 |
keymap->leds[idx].name = ledi->name; |
| 1002 |
} |
| 1003 |
|
| 1004 |
return true; |
| 1005 |
} |
| 1006 |
|
| 1007 |
static bool |
| 1008 |
CopyKeyNamesInfoToKeymap(struct xkb_keymap *keymap, KeyNamesInfo *info) |
| 1009 |
{ |
| 1010 |
/* This function trashes keymap on error, but that's OK. */ |
| 1011 |
if (!CopyKeyNamesToKeymap(keymap, info) || |
| 1012 |
!CopyKeycodeNameLUT(keymap, info) || |
| 1013 |
!CopyLedNamesToKeymap(keymap, info)) |
| 1014 |
return false; |
| 1015 |
|
| 1016 |
keymap->keycodes_section_name = strdup_safe(info->name); |
| 1017 |
XkbEscapeMapName(keymap->keycodes_section_name); |
| 1018 |
return true; |
| 1019 |
} |
| 1020 |
|
| 1021 |
/***====================================================================***/ |
| 1022 |
|
| 1023 |
bool |
| 1024 |
CompileKeycodes(XkbFile *file, struct xkb_keymap *keymap) |
| 1025 |
{ |
| 1026 |
KeyNamesInfo info; |
| 1027 |
|
| 1028 |
InitKeyNamesInfo(&info, keymap->ctx, 0); |
| 1029 |
|
| 1030 |
if (file != NULL) |
| 1031 |
HandleKeycodesFile(&info, file); |
| 1032 |
|
| 1033 |
if (info.errorCount != 0) |
| 1034 |
goto err_info; |
| 1035 |
|
| 1036 |
if (!CopyKeyNamesInfoToKeymap(keymap, &info)) |
| 1037 |
goto err_info; |
| 1038 |
|
| 1039 |
ClearKeyNamesInfo(&info); |
| 1040 |
return true; |
| 1041 |
|
| 1042 |
err_info: |
| 1043 |
ClearKeyNamesInfo(&info); |
| 1044 |
return false; |
| 1045 |
} |