| 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 <limits.h> |
| 9 |
|
| 10 |
#include "xkbcommon/xkbcommon.h" |
| 11 |
#include "xkbcomp-priv.h" |
| 12 |
#include "text.h" |
| 13 |
#include "vmod.h" |
| 14 |
#include "expr.h" |
| 15 |
#include "include.h" |
| 16 |
#include "utils.h" |
| 17 |
#include "util-mem.h" |
| 18 |
|
| 19 |
enum type_field { |
| 20 |
TYPE_FIELD_MASK = (1 << 0), |
| 21 |
TYPE_FIELD_MAP = (1 << 1), |
| 22 |
TYPE_FIELD_PRESERVE = (1 << 2), |
| 23 |
TYPE_FIELD_LEVEL_NAME = (1 << 3), |
| 24 |
}; |
| 25 |
|
| 26 |
typedef struct { |
| 27 |
enum type_field defined; |
| 28 |
enum merge_mode merge; |
| 29 |
|
| 30 |
xkb_atom_t name; |
| 31 |
xkb_mod_mask_t mods; |
| 32 |
xkb_level_index_t num_levels; |
| 33 |
darray(struct xkb_key_type_entry) entries; |
| 34 |
darray(xkb_atom_t) level_names; |
| 35 |
} KeyTypeInfo; |
| 36 |
|
| 37 |
typedef struct { |
| 38 |
char *name; |
| 39 |
int errorCount; |
| 40 |
unsigned int include_depth; |
| 41 |
|
| 42 |
darray(KeyTypeInfo) types; |
| 43 |
struct xkb_mod_set mods; |
| 44 |
|
| 45 |
struct xkb_context *ctx; |
| 46 |
} KeyTypesInfo; |
| 47 |
|
| 48 |
/***====================================================================***/ |
| 49 |
|
| 50 |
static inline const char * |
| 51 |
MapEntryTxt(KeyTypesInfo *info, struct xkb_key_type_entry *entry) |
| 52 |
{ |
| 53 |
return ModMaskText(info->ctx, MOD_BOTH, &info->mods, entry->mods.mods); |
| 54 |
} |
| 55 |
|
| 56 |
static inline const char * |
| 57 |
TypeTxt(KeyTypesInfo *info, KeyTypeInfo *type) |
| 58 |
{ |
| 59 |
return xkb_atom_text(info->ctx, type->name); |
| 60 |
} |
| 61 |
|
| 62 |
static inline const char * |
| 63 |
TypeMaskTxt(KeyTypesInfo *info, KeyTypeInfo *type) |
| 64 |
{ |
| 65 |
return ModMaskText(info->ctx, MOD_BOTH, &info->mods, type->mods); |
| 66 |
} |
| 67 |
|
| 68 |
static inline bool |
| 69 |
ReportTypeShouldBeArray(KeyTypesInfo *info, KeyTypeInfo *type, |
| 70 |
const char *field) |
| 71 |
{ |
| 72 |
return ReportShouldBeArray(info->ctx, "key type", field, |
| 73 |
TypeTxt(info, type)); |
| 74 |
} |
| 75 |
|
| 76 |
static inline bool |
| 77 |
ReportTypeBadType(KeyTypesInfo *info, xkb_message_code_t code, |
| 78 |
KeyTypeInfo *type, const char *field, const char *wanted) |
| 79 |
{ |
| 80 |
return ReportBadType(info->ctx, code, "key type", field, |
| 81 |
TypeTxt(info, type), wanted); |
| 82 |
} |
| 83 |
|
| 84 |
/***====================================================================***/ |
| 85 |
|
| 86 |
static void |
| 87 |
InitKeyTypesInfo(KeyTypesInfo *info, struct xkb_context *ctx, |
| 88 |
unsigned int include_depth, |
| 89 |
const struct xkb_mod_set *mods) |
| 90 |
{ |
| 91 |
memset(info, 0, sizeof(*info)); |
| 92 |
info->ctx = ctx; |
| 93 |
info->include_depth = include_depth; |
| 94 |
InitVMods(&info->mods, mods, include_depth > 0); |
| 95 |
} |
| 96 |
|
| 97 |
static void |
| 98 |
ClearKeyTypeInfo(KeyTypeInfo *type) |
| 99 |
{ |
| 100 |
darray_free(type->entries); |
| 101 |
darray_free(type->level_names); |
| 102 |
} |
| 103 |
|
| 104 |
static void |
| 105 |
ClearKeyTypesInfo(KeyTypesInfo *info) |
| 106 |
{ |
| 107 |
free(info->name); |
| 108 |
KeyTypeInfo *type; |
| 109 |
darray_foreach(type, info->types) |
| 110 |
ClearKeyTypeInfo(type); |
| 111 |
darray_free(info->types); |
| 112 |
} |
| 113 |
|
| 114 |
static KeyTypeInfo * |
| 115 |
FindMatchingKeyType(KeyTypesInfo *info, xkb_atom_t name) |
| 116 |
{ |
| 117 |
KeyTypeInfo *old; |
| 118 |
|
| 119 |
darray_foreach(old, info->types) |
| 120 |
if (old->name == name) |
| 121 |
return old; |
| 122 |
|
| 123 |
return NULL; |
| 124 |
} |
| 125 |
|
| 126 |
static bool |
| 127 |
AddKeyType(KeyTypesInfo *info, KeyTypeInfo *new, bool same_file) |
| 128 |
{ |
| 129 |
KeyTypeInfo *old; |
| 130 |
const int verbosity = xkb_context_get_log_verbosity(info->ctx); |
| 131 |
|
| 132 |
old = FindMatchingKeyType(info, new->name); |
| 133 |
if (old) { |
| 134 |
if (new->merge != MERGE_AUGMENT) { |
| 135 |
if ((same_file && verbosity > 0) || verbosity > 9) { |
| 136 |
log_warn(info->ctx, |
| 137 |
XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS, |
| 138 |
"Multiple definitions of the %s key type; " |
| 139 |
"Earlier definition ignored\n", |
| 140 |
xkb_atom_text(info->ctx, new->name)); |
| 141 |
} |
| 142 |
|
| 143 |
ClearKeyTypeInfo(old); |
| 144 |
*old = *new; |
| 145 |
darray_init(new->entries); |
| 146 |
darray_init(new->level_names); |
| 147 |
return true; |
| 148 |
} |
| 149 |
|
| 150 |
if (same_file) |
| 151 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_DETAILED, |
| 152 |
XKB_WARNING_CONFLICTING_KEY_TYPE_DEFINITIONS, |
| 153 |
"Multiple definitions of the %s key type; " |
| 154 |
"Later definition ignored\n", |
| 155 |
xkb_atom_text(info->ctx, new->name)); |
| 156 |
|
| 157 |
ClearKeyTypeInfo(new); |
| 158 |
return true; |
| 159 |
} |
| 160 |
|
| 161 |
darray_append(info->types, *new); |
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
/***====================================================================***/ |
| 166 |
|
| 167 |
static void |
| 168 |
MergeIncludedKeyTypes(KeyTypesInfo *into, KeyTypesInfo *from, |
| 169 |
enum merge_mode merge) |
| 170 |
{ |
| 171 |
if (from->errorCount > 0) { |
| 172 |
into->errorCount += from->errorCount; |
| 173 |
return; |
| 174 |
} |
| 175 |
|
| 176 |
MergeModSets(into->ctx, &into->mods, &from->mods, merge); |
| 177 |
|
| 178 |
if (into->name == NULL) { |
| 179 |
into->name = steal(&from->name); |
| 180 |
} |
| 181 |
|
| 182 |
if (darray_empty(into->types)) { |
| 183 |
into->types = from->types; |
| 184 |
/* Types stolen via shallow copy, so reinitialize the array */ |
| 185 |
darray_init(from->types); |
| 186 |
} |
| 187 |
else { |
| 188 |
KeyTypeInfo *type; |
| 189 |
darray_foreach(type, from->types) { |
| 190 |
type->merge = merge; |
| 191 |
if (!AddKeyType(into, type, false)) |
| 192 |
into->errorCount++; |
| 193 |
} |
| 194 |
/* Types were either shallow copied or reinitialized individually |
| 195 |
in `AddKeyType`, so we only need to free the array */ |
| 196 |
darray_free(from->types); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
static void |
| 201 |
HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file); |
| 202 |
|
| 203 |
static bool |
| 204 |
HandleIncludeKeyTypes(KeyTypesInfo *info, IncludeStmt *include) |
| 205 |
{ |
| 206 |
KeyTypesInfo included; |
| 207 |
|
| 208 |
if (ExceedsIncludeMaxDepth(info->ctx, info->include_depth)) { |
| 209 |
info->errorCount += 10; |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
InitKeyTypesInfo(&included, info->ctx, info->include_depth + 1, |
| 214 |
&info->mods); |
| 215 |
included.name = steal(&include->stmt); |
| 216 |
|
| 217 |
for (IncludeStmt *stmt = include; stmt; stmt = stmt->next_incl) { |
| 218 |
KeyTypesInfo next_incl; |
| 219 |
XkbFile *file; |
| 220 |
|
| 221 |
char path[PATH_MAX]; |
| 222 |
file = ProcessIncludeFile(info->ctx, stmt, FILE_TYPE_TYPES, |
| 223 |
path, sizeof(path)); |
| 224 |
if (!file) { |
| 225 |
info->errorCount += 10; |
| 226 |
ClearKeyTypesInfo(&included); |
| 227 |
return false; |
| 228 |
} |
| 229 |
|
| 230 |
InitKeyTypesInfo(&next_incl, info->ctx, info->include_depth + 1, |
| 231 |
&included.mods); |
| 232 |
|
| 233 |
HandleKeyTypesFile(&next_incl, file); |
| 234 |
|
| 235 |
MergeIncludedKeyTypes(&included, &next_incl, stmt->merge); |
| 236 |
|
| 237 |
ClearKeyTypesInfo(&next_incl); |
| 238 |
FreeXkbFile(file); |
| 239 |
} |
| 240 |
|
| 241 |
MergeIncludedKeyTypes(info, &included, include->merge); |
| 242 |
ClearKeyTypesInfo(&included); |
| 243 |
|
| 244 |
return (info->errorCount == 0); |
| 245 |
} |
| 246 |
|
| 247 |
/***====================================================================***/ |
| 248 |
|
| 249 |
static bool |
| 250 |
SetModifiers(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx, |
| 251 |
ExprDef *value) |
| 252 |
{ |
| 253 |
xkb_mod_mask_t mods = 0; |
| 254 |
|
| 255 |
if (arrayNdx) |
| 256 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 257 |
"The modifiers field of a key type is not an array; " |
| 258 |
"Illegal array subscript ignored\n"); |
| 259 |
|
| 260 |
if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, &mods)) { |
| 261 |
log_err(info->ctx, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK, |
| 262 |
"Key type mask field must be a modifier mask; " |
| 263 |
"Key type definition ignored\n"); |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
if (type->defined & TYPE_FIELD_MASK) { |
| 268 |
log_warn(info->ctx, XKB_LOG_MESSAGE_NO_ID, |
| 269 |
"Multiple modifier mask definitions for key type %s; " |
| 270 |
"Using %s, ignoring %s\n", |
| 271 |
xkb_atom_text(info->ctx, type->name), |
| 272 |
TypeMaskTxt(info, type), |
| 273 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods)); |
| 274 |
return false; |
| 275 |
} |
| 276 |
|
| 277 |
type->mods = mods; |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
/***====================================================================***/ |
| 282 |
|
| 283 |
static struct xkb_key_type_entry * |
| 284 |
FindMatchingMapEntry(KeyTypeInfo *type, xkb_mod_mask_t mods) |
| 285 |
{ |
| 286 |
struct xkb_key_type_entry *entry; |
| 287 |
|
| 288 |
darray_foreach(entry, type->entries) |
| 289 |
if (entry->mods.mods == mods) |
| 290 |
return entry; |
| 291 |
|
| 292 |
return NULL; |
| 293 |
} |
| 294 |
|
| 295 |
static bool |
| 296 |
AddMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, |
| 297 |
struct xkb_key_type_entry *new, bool clobber, bool report) |
| 298 |
{ |
| 299 |
struct xkb_key_type_entry *old; |
| 300 |
|
| 301 |
old = FindMatchingMapEntry(type, new->mods.mods); |
| 302 |
if (old) { |
| 303 |
if (report && old->level != new->level) { |
| 304 |
log_warn(info->ctx, XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY, |
| 305 |
"Multiple map entries for %s in %s; " |
| 306 |
"Using %"PRIu32", ignoring %"PRIu32"\n", |
| 307 |
MapEntryTxt(info, new), TypeTxt(info, type), |
| 308 |
(clobber ? new->level : old->level) + 1, |
| 309 |
(clobber ? old->level : new->level) + 1); |
| 310 |
} |
| 311 |
else { |
| 312 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_VERBOSE, |
| 313 |
XKB_WARNING_CONFLICTING_KEY_TYPE_MAP_ENTRY, |
| 314 |
"Multiple occurrences of map[%s]= %"PRIu32" in %s; Ignored\n", |
| 315 |
MapEntryTxt(info, new), new->level + 1, |
| 316 |
TypeTxt(info, type)); |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
if (clobber) { |
| 321 |
if (new->level >= type->num_levels) |
| 322 |
type->num_levels = new->level + 1; |
| 323 |
old->level = new->level; |
| 324 |
} |
| 325 |
|
| 326 |
return true; |
| 327 |
} |
| 328 |
|
| 329 |
if (new->level >= type->num_levels) |
| 330 |
type->num_levels = new->level + 1; |
| 331 |
|
| 332 |
darray_append(type->entries, *new); |
| 333 |
return true; |
| 334 |
} |
| 335 |
|
| 336 |
static bool |
| 337 |
SetMapEntry(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx, |
| 338 |
ExprDef *value) |
| 339 |
{ |
| 340 |
struct xkb_key_type_entry entry; |
| 341 |
|
| 342 |
if (arrayNdx == NULL) |
| 343 |
return ReportTypeShouldBeArray(info, type, "map entry"); |
| 344 |
|
| 345 |
if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, |
| 346 |
&entry.mods.mods)) |
| 347 |
return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK, |
| 348 |
type, "map entry", "modifier mask"); |
| 349 |
|
| 350 |
if (entry.mods.mods & (~type->mods)) { |
| 351 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_BRIEF, |
| 352 |
XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE, |
| 353 |
"Map entry for modifiers not used by type %s; " |
| 354 |
"Using %s instead of %s\n", |
| 355 |
TypeTxt(info, type), |
| 356 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, |
| 357 |
entry.mods.mods & type->mods), |
| 358 |
MapEntryTxt(info, &entry)); |
| 359 |
entry.mods.mods &= type->mods; |
| 360 |
} |
| 361 |
|
| 362 |
if (!ExprResolveLevel(info->ctx, value, &entry.level)) { |
| 363 |
log_err(info->ctx, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL, |
| 364 |
"Level specifications in a key type must be integer; " |
| 365 |
"Ignoring malformed level specification\n"); |
| 366 |
return false; |
| 367 |
} |
| 368 |
|
| 369 |
entry.preserve.mods = 0; |
| 370 |
|
| 371 |
return AddMapEntry(info, type, &entry, true, true); |
| 372 |
} |
| 373 |
|
| 374 |
/***====================================================================***/ |
| 375 |
|
| 376 |
static bool |
| 377 |
AddPreserve(KeyTypesInfo *info, KeyTypeInfo *type, |
| 378 |
xkb_mod_mask_t mods, xkb_mod_mask_t preserve_mods) |
| 379 |
{ |
| 380 |
struct xkb_key_type_entry *entry; |
| 381 |
struct xkb_key_type_entry new; |
| 382 |
|
| 383 |
darray_foreach(entry, type->entries) { |
| 384 |
if (entry->mods.mods != mods) |
| 385 |
continue; |
| 386 |
|
| 387 |
/* Map exists without previous preserve (or "None"); override. */ |
| 388 |
if (entry->preserve.mods == 0) { |
| 389 |
entry->preserve.mods = preserve_mods; |
| 390 |
return true; |
| 391 |
} |
| 392 |
|
| 393 |
/* Map exists with same preserve; do nothing. */ |
| 394 |
if (entry->preserve.mods == preserve_mods) { |
| 395 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_VERBOSE, |
| 396 |
XKB_WARNING_DUPLICATE_ENTRY, |
| 397 |
"Identical definitions for preserve[%s] in %s; " |
| 398 |
"Ignored\n", |
| 399 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods), |
| 400 |
TypeTxt(info, type)); |
| 401 |
return true; |
| 402 |
} |
| 403 |
|
| 404 |
/* Map exists with different preserve; latter wins. */ |
| 405 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_BRIEF, |
| 406 |
XKB_WARNING_CONFLICTING_KEY_TYPE_PRESERVE_ENTRIES, |
| 407 |
"Multiple definitions for preserve[%s] in %s; " |
| 408 |
"Using %s, ignoring %s\n", |
| 409 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods), |
| 410 |
TypeTxt(info, type), |
| 411 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, preserve_mods), |
| 412 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, |
| 413 |
entry->preserve.mods)); |
| 414 |
|
| 415 |
entry->preserve.mods = preserve_mods; |
| 416 |
return true; |
| 417 |
} |
| 418 |
|
| 419 |
/* |
| 420 |
* Map does not exist, i.e. preserve[] came before map[]. |
| 421 |
* Create a map with the specified mask mapping to Level1. The level |
| 422 |
* may be overridden later with an explicit map[] statement. |
| 423 |
*/ |
| 424 |
new.level = 0; |
| 425 |
new.mods.mods = mods; |
| 426 |
new.preserve.mods = preserve_mods; |
| 427 |
darray_append(type->entries, new); |
| 428 |
return true; |
| 429 |
} |
| 430 |
|
| 431 |
static bool |
| 432 |
SetPreserve(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx, |
| 433 |
ExprDef *value) |
| 434 |
{ |
| 435 |
|
| 436 |
if (arrayNdx == NULL) |
| 437 |
return ReportTypeShouldBeArray(info, type, "preserve entry"); |
| 438 |
|
| 439 |
xkb_mod_mask_t mods = 0; |
| 440 |
if (!ExprResolveModMask(info->ctx, arrayNdx, MOD_BOTH, &info->mods, &mods)) |
| 441 |
return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK, |
| 442 |
type, "preserve entry", "modifier mask"); |
| 443 |
|
| 444 |
if (mods & ~type->mods) { |
| 445 |
const char *before, *after; |
| 446 |
|
| 447 |
before = ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods); |
| 448 |
mods &= type->mods; |
| 449 |
after = ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods); |
| 450 |
|
| 451 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_BRIEF, |
| 452 |
XKB_WARNING_UNDECLARED_MODIFIERS_IN_KEY_TYPE, |
| 453 |
"Preserve entry for modifiers not used by the %s type; " |
| 454 |
"Index %s converted to %s\n", |
| 455 |
TypeTxt(info, type), before, after); |
| 456 |
} |
| 457 |
|
| 458 |
xkb_mod_mask_t preserve_mods = 0; |
| 459 |
if (!ExprResolveModMask(info->ctx, value, MOD_BOTH, &info->mods, |
| 460 |
&preserve_mods)) { |
| 461 |
log_err(info->ctx, XKB_ERROR_UNSUPPORTED_MODIFIER_MASK, |
| 462 |
"Preserve value in a key type is not a modifier mask; " |
| 463 |
"Ignoring preserve[%s] in type %s\n", |
| 464 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods), |
| 465 |
TypeTxt(info, type)); |
| 466 |
return false; |
| 467 |
} |
| 468 |
|
| 469 |
if (preserve_mods & ~mods) { |
| 470 |
const char *before, *after; |
| 471 |
|
| 472 |
before = ModMaskText(info->ctx, MOD_BOTH, &info->mods, preserve_mods); |
| 473 |
preserve_mods &= mods; |
| 474 |
after = ModMaskText(info->ctx, MOD_BOTH, &info->mods, preserve_mods); |
| 475 |
|
| 476 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_BRIEF, |
| 477 |
XKB_WARNING_ILLEGAL_KEY_TYPE_PRESERVE_RESULT, |
| 478 |
"Illegal value for preserve[%s] in type %s; " |
| 479 |
"Converted %s to %s\n", |
| 480 |
ModMaskText(info->ctx, MOD_BOTH, &info->mods, mods), |
| 481 |
TypeTxt(info, type), before, after); |
| 482 |
} |
| 483 |
|
| 484 |
return AddPreserve(info, type, mods, preserve_mods); |
| 485 |
} |
| 486 |
|
| 487 |
/***====================================================================***/ |
| 488 |
|
| 489 |
static bool |
| 490 |
AddLevelName(KeyTypesInfo *info, KeyTypeInfo *type, |
| 491 |
xkb_level_index_t level, xkb_atom_t name, bool clobber) |
| 492 |
{ |
| 493 |
/* New name. */ |
| 494 |
if (level >= darray_size(type->level_names)) { |
| 495 |
darray_resize0(type->level_names, level + 1); |
| 496 |
goto finish; |
| 497 |
} |
| 498 |
|
| 499 |
/* Same level, same name. */ |
| 500 |
if (darray_item(type->level_names, level) == name) { |
| 501 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_VERBOSE, XKB_WARNING_DUPLICATE_ENTRY, |
| 502 |
"Duplicate names for level %"PRIu32" of key type %s; Ignored\n", |
| 503 |
level + 1, TypeTxt(info, type)); |
| 504 |
return true; |
| 505 |
} |
| 506 |
|
| 507 |
/* Same level, different name. */ |
| 508 |
if (darray_item(type->level_names, level) != XKB_ATOM_NONE) { |
| 509 |
const char *old, *new; |
| 510 |
old = xkb_atom_text(info->ctx, |
| 511 |
darray_item(type->level_names, level)); |
| 512 |
new = xkb_atom_text(info->ctx, name); |
| 513 |
log_vrb(info->ctx, XKB_LOG_VERBOSITY_BRIEF, |
| 514 |
XKB_WARNING_CONFLICTING_KEY_TYPE_LEVEL_NAMES, |
| 515 |
"Multiple names for level %"PRIu32" of key type %s; " |
| 516 |
"Using %s, ignoring %s\n", |
| 517 |
level + 1, TypeTxt(info, type), |
| 518 |
(clobber ? new : old), (clobber ? old : new)); |
| 519 |
|
| 520 |
if (!clobber) |
| 521 |
return true; |
| 522 |
} |
| 523 |
|
| 524 |
/* FIXME: What about different level, same name? */ |
| 525 |
|
| 526 |
finish: |
| 527 |
darray_item(type->level_names, level) = name; |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
static bool |
| 532 |
SetLevelName(KeyTypesInfo *info, KeyTypeInfo *type, ExprDef *arrayNdx, |
| 533 |
ExprDef *value) |
| 534 |
{ |
| 535 |
if (arrayNdx == NULL) |
| 536 |
return ReportTypeShouldBeArray(info, type, "level name"); |
| 537 |
|
| 538 |
xkb_level_index_t level = 0; |
| 539 |
if (!ExprResolveLevel(info->ctx, arrayNdx, &level)) |
| 540 |
return ReportTypeBadType(info, XKB_ERROR_UNSUPPORTED_SHIFT_LEVEL, |
| 541 |
type, "level name", "integer"); |
| 542 |
|
| 543 |
xkb_atom_t level_name = XKB_ATOM_NONE; |
| 544 |
if (!ExprResolveString(info->ctx, value, &level_name)) { |
| 545 |
log_err(info->ctx, XKB_ERROR_WRONG_FIELD_TYPE, |
| 546 |
"Non-string name for level %"PRIu32" in key type %s; " |
| 547 |
"Ignoring illegal level name definition\n", |
| 548 |
level + 1, xkb_atom_text(info->ctx, type->name)); |
| 549 |
return false; |
| 550 |
} |
| 551 |
|
| 552 |
return AddLevelName(info, type, level, level_name, true); |
| 553 |
} |
| 554 |
|
| 555 |
/***====================================================================***/ |
| 556 |
|
| 557 |
static bool |
| 558 |
SetKeyTypeField(KeyTypesInfo *info, KeyTypeInfo *type, |
| 559 |
const char *field, ExprDef *arrayNdx, ExprDef *value) |
| 560 |
{ |
| 561 |
bool ok = false; |
| 562 |
enum type_field type_field = 0; |
| 563 |
|
| 564 |
if (istreq(field, "modifiers")) { |
| 565 |
type_field = TYPE_FIELD_MASK; |
| 566 |
ok = SetModifiers(info, type, arrayNdx, value); |
| 567 |
} |
| 568 |
else if (istreq(field, "map")) { |
| 569 |
type_field = TYPE_FIELD_MAP; |
| 570 |
ok = SetMapEntry(info, type, arrayNdx, value); |
| 571 |
} |
| 572 |
else if (istreq(field, "preserve")) { |
| 573 |
type_field = TYPE_FIELD_PRESERVE; |
| 574 |
ok = SetPreserve(info, type, arrayNdx, value); |
| 575 |
} |
| 576 |
else if (istreq(field, "levelname") || istreq(field, "level_name")) { |
| 577 |
type_field = TYPE_FIELD_LEVEL_NAME; |
| 578 |
ok = SetLevelName(info, type, arrayNdx, value); |
| 579 |
} else { |
| 580 |
log_err(info->ctx, XKB_ERROR_UNKNOWN_FIELD, |
| 581 |
"Unknown field \"%s\" in key type \"%s\"; Definition ignored\n", |
| 582 |
field, TypeTxt(info, type)); |
| 583 |
} |
| 584 |
|
| 585 |
type->defined |= type_field; |
| 586 |
return ok; |
| 587 |
} |
| 588 |
|
| 589 |
static bool |
| 590 |
HandleKeyTypeBody(KeyTypesInfo *info, VarDef *def, KeyTypeInfo *type) |
| 591 |
{ |
| 592 |
bool ok = true; |
| 593 |
const char *elem, *field; |
| 594 |
ExprDef *arrayNdx; |
| 595 |
|
| 596 |
for (; def; def = (VarDef *) def->common.next) { |
| 597 |
ok = ExprResolveLhs(info->ctx, def->name, &elem, &field, |
| 598 |
&arrayNdx); |
| 599 |
if (!ok) |
| 600 |
continue; |
| 601 |
|
| 602 |
if (elem) { |
| 603 |
if (istreq(elem, "type")) { |
| 604 |
log_err(info->ctx, XKB_ERROR_INVALID_SET_DEFAULT_STATEMENT, |
| 605 |
"Support for changing the default type has been removed; " |
| 606 |
"Statement \"%s.%s\" ignored.\n", elem, field); |
| 607 |
} |
| 608 |
else { |
| 609 |
log_err(info->ctx, XKB_ERROR_GLOBAL_DEFAULTS_WRONG_SCOPE, |
| 610 |
"Cannot set global defaults for \"%s\" element within " |
| 611 |
"a key type statement: move statements to the global " |
| 612 |
"file scope. Assignment to \"%s.%s\" ignored.\n", |
| 613 |
elem, elem, field); |
| 614 |
ok = false; |
| 615 |
} |
| 616 |
continue; |
| 617 |
} |
| 618 |
|
| 619 |
ok = SetKeyTypeField(info, type, field, arrayNdx, def->value); |
| 620 |
} |
| 621 |
|
| 622 |
return ok; |
| 623 |
} |
| 624 |
|
| 625 |
static bool |
| 626 |
HandleKeyTypeDef(KeyTypesInfo *info, KeyTypeDef *def) |
| 627 |
{ |
| 628 |
KeyTypeInfo type = { |
| 629 |
.defined = 0, |
| 630 |
.merge = def->merge, |
| 631 |
.name = def->name, |
| 632 |
.mods = 0, |
| 633 |
.num_levels = 1, |
| 634 |
.entries = darray_new(), |
| 635 |
.level_names = darray_new(), |
| 636 |
}; |
| 637 |
|
| 638 |
if (!HandleKeyTypeBody(info, def->body, &type) || |
| 639 |
!AddKeyType(info, &type, true)) |
| 640 |
{ |
| 641 |
info->errorCount++; |
| 642 |
ClearKeyTypeInfo(&type); |
| 643 |
return false; |
| 644 |
} |
| 645 |
|
| 646 |
/* Type has been either stolen via shallow copy or reinitialized in |
| 647 |
`AddKeyType`: no need to free the arrays */ |
| 648 |
return true; |
| 649 |
} |
| 650 |
|
| 651 |
static bool |
| 652 |
HandleGlobalVar(KeyTypesInfo *info, VarDef *stmt) |
| 653 |
{ |
| 654 |
const char *elem, *field; |
| 655 |
ExprDef *arrayNdx; |
| 656 |
|
| 657 |
if (!ExprResolveLhs(info->ctx, stmt->name, &elem, &field, |
| 658 |
&arrayNdx)) |
| 659 |
return false; /* internal error, already reported */ |
| 660 |
|
| 661 |
if (elem && istreq(elem, "type")) { |
| 662 |
log_err(info->ctx, XKB_ERROR_WRONG_STATEMENT_TYPE, |
| 663 |
"Support for changing the default type has been removed; " |
| 664 |
"Statement ignored\n"); |
| 665 |
return true; |
| 666 |
} |
| 667 |
else if (elem) { |
| 668 |
log_err(info->ctx, XKB_ERROR_UNKNOWN_DEFAULT_FIELD, |
| 669 |
"Default defined for unknown element \"%s\"; " |
| 670 |
"Value for field \"%s.%s\" ignored\n", elem, elem, field); |
| 671 |
} |
| 672 |
else if (field) { |
| 673 |
log_err(info->ctx, XKB_ERROR_UNKNOWN_DEFAULT_FIELD, |
| 674 |
"Default defined for unknown field \"%s\"; Ignored\n", field); |
| 675 |
} |
| 676 |
|
| 677 |
return false; |
| 678 |
} |
| 679 |
|
| 680 |
static void |
| 681 |
HandleKeyTypesFile(KeyTypesInfo *info, XkbFile *file) |
| 682 |
{ |
| 683 |
bool ok; |
| 684 |
|
| 685 |
free(info->name); |
| 686 |
info->name = strdup_safe(file->name); |
| 687 |
|
| 688 |
for (ParseCommon *stmt = file->defs; stmt; stmt = stmt->next) { |
| 689 |
switch (stmt->type) { |
| 690 |
case STMT_INCLUDE: |
| 691 |
ok = HandleIncludeKeyTypes(info, (IncludeStmt *) stmt); |
| 692 |
break; |
| 693 |
case STMT_TYPE: |
| 694 |
ok = HandleKeyTypeDef(info, (KeyTypeDef *) stmt); |
| 695 |
break; |
| 696 |
case STMT_VAR: |
| 697 |
ok = HandleGlobalVar(info, (VarDef*) stmt); |
| 698 |
break; |
| 699 |
case STMT_VMOD: |
| 700 |
ok = HandleVModDef(info->ctx, &info->mods, (VModDef *) stmt); |
| 701 |
break; |
| 702 |
default: |
| 703 |
log_err(info->ctx, XKB_ERROR_WRONG_STATEMENT_TYPE, |
| 704 |
"Key type files may not include other declarations; " |
| 705 |
"Ignoring %s\n", stmt_type_to_string(stmt->type)); |
| 706 |
ok = false; |
| 707 |
break; |
| 708 |
} |
| 709 |
|
| 710 |
if (!ok) |
| 711 |
info->errorCount++; |
| 712 |
|
| 713 |
if (info->errorCount > 10) { |
| 714 |
log_err(info->ctx, XKB_ERROR_INVALID_XKB_SYNTAX, |
| 715 |
"Abandoning keytypes file \"%s\"\n", |
| 716 |
safe_map_name(file)); |
| 717 |
break; |
| 718 |
} |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/***====================================================================***/ |
| 723 |
|
| 724 |
static bool |
| 725 |
CopyKeyTypesToKeymap(struct xkb_keymap *keymap, KeyTypesInfo *info) |
| 726 |
{ |
| 727 |
const darray_size_t num_types = darray_empty(info->types) |
| 728 |
? 1 |
| 729 |
: darray_size(info->types); |
| 730 |
struct xkb_key_type *types = calloc(num_types, sizeof(*types)); |
| 731 |
if (!types) |
| 732 |
return false; |
| 733 |
|
| 734 |
/* |
| 735 |
* The following types are called “Canonical Key Types” and the XKB protocol |
| 736 |
* specifies them as mandatory in any keymap: |
| 737 |
* |
| 738 |
* - ONE_LEVEL |
| 739 |
* - TWO_LEVEL |
| 740 |
* - ALPHABETIC |
| 741 |
* - KEYPAD |
| 742 |
* |
| 743 |
* They must have specific properties defined in the appendix B of |
| 744 |
* “The X Keyboard Extension: Protocol Specification”: |
| 745 |
* https://www.x.org/releases/current/doc/kbproto/xkbproto.html#canonical_key_types |
| 746 |
* |
| 747 |
* In the Xorg ecosystem, any missing canonical type fallbacks to a default |
| 748 |
* type supplied by libX11’s `XkbInitCanonicalKeyTypes()`, e.g. in xkbcomp. |
| 749 |
* |
| 750 |
* libxkbcommon does not require these types per se: it only requires that |
| 751 |
* all *used* types — explicit (`type="…"`) or implicit (automatic types) — |
| 752 |
* are defined, with the exception that if no key type at all is defined, |
| 753 |
* then a default `ONE_LEVEL` type is provided. |
| 754 |
* |
| 755 |
* libxkbcommon also does not require any particular order of these key |
| 756 |
* types, because they are retrieved using their name instead of their index. |
| 757 |
* |
| 758 |
* Since 1.12 (31900860c65b88e4d10ad7dd00377e2815cca0f6), libxkbcommon drops |
| 759 |
* any *unused* key type at serialization by default. Some layouts with 4+ |
| 760 |
* levels may not require e.g. the `TWO_LEVEL` nor the `ALPHABETIC` types. |
| 761 |
* |
| 762 |
* In theory, libxkbcommon would not care of the presence of the canonical |
| 763 |
* key types and could delegate the property check, fallback and ordering |
| 764 |
* work to xkbcomp, as it is the case in Xorg’s Xwayland. However the |
| 765 |
* fallback implementation is buggy: |
| 766 |
* |
| 767 |
* - https://gitlab.freedesktop.org/xorg/lib/libx11/-/merge_requests/292 |
| 768 |
* - https://gitlab.freedesktop.org/xorg/xserver/-/merge_requests/2082 |
| 769 |
* |
| 770 |
* The canonical key types are always present in the keymap generated from |
| 771 |
* xkeyboard-config and custom keymaps usually include these types too. So |
| 772 |
* to circumvent the issues of Xorg, it should suffice that libxkbcommon |
| 773 |
* ensures to never discard the canonical key types, if present, and continue |
| 774 |
* to delegate the (unlikely) type fallbacks to xkbcomp. |
| 775 |
*/ |
| 776 |
|
| 777 |
/* |
| 778 |
* If no types were specified, a default ONE_LEVEL type is |
| 779 |
* used for all keys. |
| 780 |
*/ |
| 781 |
if (darray_empty(info->types)) { |
| 782 |
struct xkb_key_type *type = &types[0]; |
| 783 |
|
| 784 |
type->mods.mods = 0; |
| 785 |
type->num_levels = 1; |
| 786 |
type->entries = NULL; |
| 787 |
type->num_entries = 0; |
| 788 |
type->name = xkb_atom_intern_literal(keymap->ctx, "ONE_LEVEL"); |
| 789 |
type->level_names = NULL; |
| 790 |
type->num_level_names = 0; |
| 791 |
type->required = true; |
| 792 |
} |
| 793 |
else { |
| 794 |
/* HACK to circumvent Xorg bugs (see comment above) */ |
| 795 |
const xkb_atom_t canonical_types[] = { |
| 796 |
xkb_atom_intern_literal(keymap->ctx, "ONE_LEVEL"), |
| 797 |
xkb_atom_intern_literal(keymap->ctx, "TWO_LEVEL"), |
| 798 |
xkb_atom_intern_literal(keymap->ctx, "ALPHABETIC"), |
| 799 |
xkb_atom_intern_literal(keymap->ctx, "KEYPAD"), |
| 800 |
}; |
| 801 |
|
| 802 |
for (darray_size_t i = 0; i < num_types; i++) { |
| 803 |
KeyTypeInfo *def = &darray_item(info->types, i); |
| 804 |
struct xkb_key_type *type = &types[i]; |
| 805 |
|
| 806 |
type->name = def->name; |
| 807 |
type->mods.mods = def->mods; |
| 808 |
type->num_levels = def->num_levels; |
| 809 |
type->num_level_names = |
| 810 |
(xkb_level_index_t) darray_size(def->level_names); |
| 811 |
darray_steal(def->level_names, &type->level_names, NULL); |
| 812 |
darray_steal(def->entries, &type->entries, &type->num_entries); |
| 813 |
type->required = false; |
| 814 |
|
| 815 |
/* HACK: Never drop canonical XKB key types (see comment above) */ |
| 816 |
if (type->num_levels <= 2) { |
| 817 |
for (uint8_t t = 0; |
| 818 |
t < (uint8_t) ARRAY_SIZE(canonical_types); |
| 819 |
t++) { |
| 820 |
if (type->name == canonical_types[t]) { |
| 821 |
type->required = true; |
| 822 |
break; |
| 823 |
} |
| 824 |
} |
| 825 |
} |
| 826 |
} |
| 827 |
} |
| 828 |
|
| 829 |
keymap->types_section_name = strdup_safe(info->name); |
| 830 |
XkbEscapeMapName(keymap->types_section_name); |
| 831 |
keymap->num_types = num_types; |
| 832 |
keymap->types = types; |
| 833 |
keymap->mods = info->mods; |
| 834 |
return true; |
| 835 |
} |
| 836 |
|
| 837 |
/***====================================================================***/ |
| 838 |
|
| 839 |
bool |
| 840 |
CompileKeyTypes(XkbFile *file, struct xkb_keymap *keymap) |
| 841 |
{ |
| 842 |
KeyTypesInfo info; |
| 843 |
|
| 844 |
InitKeyTypesInfo(&info, keymap->ctx, 0, &keymap->mods); |
| 845 |
|
| 846 |
if (file != NULL) |
| 847 |
HandleKeyTypesFile(&info, file); |
| 848 |
|
| 849 |
if (info.errorCount != 0) |
| 850 |
goto err_info; |
| 851 |
|
| 852 |
if (!CopyKeyTypesToKeymap(keymap, &info)) |
| 853 |
goto err_info; |
| 854 |
|
| 855 |
ClearKeyTypesInfo(&info); |
| 856 |
return true; |
| 857 |
|
| 858 |
err_info: |
| 859 |
ClearKeyTypesInfo(&info); |
| 860 |
return false; |
| 861 |
} |