| 1 |
/* |
| 2 |
* Copyright © 2025 Pierre Le Marre <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "config.h" |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
|
| 11 |
#include "keymap.h" |
| 12 |
#include "src/messages-codes.h" |
| 13 |
#include "src/utils.h" |
| 14 |
#include "xkbcommon/xkbcommon.h" |
| 15 |
#include "keymap-compare.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* NOTE: these keymap comparison utilities are pretty basic and require that |
| 19 |
* items have the *same order* in the compared keymaps. |
| 20 |
*/ |
| 21 |
|
| 22 |
/* TODO: enable to relax the order on some items, such as: aliases, etc. */ |
| 23 |
|
| 24 |
|
| 25 |
static bool |
| 26 |
keymap_compare_mods(struct xkb_context *ctx, |
| 27 |
const struct xkb_keymap *keymap1, |
| 28 |
const struct xkb_keymap *keymap2) |
| 29 |
{ |
| 30 |
bool identical = true; |
| 31 |
/* Check that the keymaps have the same virtual modifiers */ |
| 32 |
|
| 33 |
/* Check common modifiers*/ |
| 34 |
const xkb_mod_index_t mod_max = MIN(keymap1->mods.num_mods, |
| 35 |
keymap2->mods.num_mods); |
| 36 |
for (xkb_mod_index_t mod = 0; mod < mod_max; mod++) { |
| 37 |
const struct xkb_mod * const mod1 = &keymap1->mods.mods[mod]; |
| 38 |
const struct xkb_mod * const mod2 = &keymap2->mods.mods[mod]; |
| 39 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 40 |
const char * const name1 = xkb_atom_text(keymap1->ctx, mod1->name); |
| 41 |
const char * const name2 = xkb_atom_text(keymap2->ctx, mod2->name); |
| 42 |
if (!streq_null(name1, name2)) { |
| 43 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 44 |
"Modifier #%"PRIu32" names do not match: " |
| 45 |
"\"%s\" != \"%s\"\n", mod, name1, name2); |
| 46 |
identical = false; |
| 47 |
} |
| 48 |
if (mod1->type != mod2->type) { |
| 49 |
/* Unlikely, only for completeness */ |
| 50 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 51 |
"Modifier #%"PRIu32" types do not match: " |
| 52 |
"%d != %d\n", mod, mod1->type, mod2->type); |
| 53 |
identical = false; |
| 54 |
} |
| 55 |
if (mod1->mapping != mod2->mapping) { |
| 56 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 57 |
"Modifier #%"PRIu32" mappings do not match: " |
| 58 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 59 |
mod, mod1->mapping, mod2->mapping); |
| 60 |
identical = false; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
/* Check non-common modifiers */ |
| 65 |
if (keymap1->mods.num_mods != keymap2->mods.num_mods) { |
| 66 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 67 |
"Modifiers counts do not match: %"PRIu32" != %"PRIu32"\n", |
| 68 |
keymap1->mods.num_mods, keymap2->mods.num_mods); |
| 69 |
identical = false; |
| 70 |
} |
| 71 |
|
| 72 |
return identical; |
| 73 |
} |
| 74 |
|
| 75 |
static bool |
| 76 |
keymap_compare_keycodes(struct xkb_context *ctx, |
| 77 |
const struct xkb_keymap *keymap1, |
| 78 |
const struct xkb_keymap *keymap2) |
| 79 |
{ |
| 80 |
bool identical = true; |
| 81 |
|
| 82 |
/* |
| 83 |
* Check keycode range |
| 84 |
*/ |
| 85 |
if (keymap1->num_keys != keymap2->num_keys) { |
| 86 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 87 |
"Keycodes counts do not match: %"PRIu32" != %"PRIu32"\n", |
| 88 |
keymap1->num_keys, keymap2->num_keys); |
| 89 |
identical = false; |
| 90 |
} |
| 91 |
if (keymap1->min_key_code != keymap2->min_key_code) { |
| 92 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 93 |
"Min keycodes do not match: %"PRIu32" != %"PRIu32"\n", |
| 94 |
keymap1->min_key_code, keymap2->min_key_code); |
| 95 |
identical = false; |
| 96 |
} |
| 97 |
if (keymap1->max_key_code != keymap2->max_key_code) { |
| 98 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 99 |
"Low keycodes counts do not match: %"PRIu32" != %"PRIu32"\n", |
| 100 |
keymap1->num_keys_low, keymap2->num_keys_low); |
| 101 |
identical = false; |
| 102 |
} |
| 103 |
if (keymap1->max_key_code != keymap2->max_key_code) { |
| 104 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 105 |
"Max keycodes do not match: %"PRIu32" != %"PRIu32"\n", |
| 106 |
keymap1->min_key_code, keymap2->min_key_code); |
| 107 |
identical = false; |
| 108 |
} |
| 109 |
|
| 110 |
/* Check common keys */ |
| 111 |
const xkb_keycode_t k_max = MIN(keymap1->num_keys, keymap2->num_keys); |
| 112 |
for (xkb_keycode_t k = 0; k < k_max; k++) { |
| 113 |
const struct xkb_key * const key1 = &keymap1->keys[k]; |
| 114 |
const struct xkb_key * const key2 = &keymap1->keys[k]; |
| 115 |
if (key1->keycode != key2->keycode) { |
| 116 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 117 |
"Key #%"PRIu32" keycodes do not match: " |
| 118 |
"%"PRIx32" != %"PRIx32"\n", k, key1->keycode, key2->keycode); |
| 119 |
identical = false; |
| 120 |
/* It does not make sense to compare further properties */ |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
const xkb_keycode_t kc = key1->keycode; |
| 125 |
|
| 126 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 127 |
const char * const name1 = xkb_atom_text(keymap1->ctx, key1->name); |
| 128 |
const char * const name2 = xkb_atom_text(keymap2->ctx, key2->name); |
| 129 |
if (!streq_null(name1, name2)) { |
| 130 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 131 |
"Key 0x%"PRIx32" names do not match: " |
| 132 |
"\"%s\" != \"%s\"\n", kc, name1, name2); |
| 133 |
identical = false; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/* Check common aliases */ |
| 138 |
const darray_size_t a_max = MIN(keymap1->num_key_aliases, |
| 139 |
keymap2->num_key_aliases); |
| 140 |
for (darray_size_t a = 0; a < a_max; a++) { |
| 141 |
const struct xkb_key_alias * const entry1 = &keymap1->key_aliases[a]; |
| 142 |
const struct xkb_key_alias * const entry2 = &keymap2->key_aliases[a]; |
| 143 |
|
| 144 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 145 |
const char * const alias1 = xkb_atom_text(keymap1->ctx, entry1->alias); |
| 146 |
const char * const alias2 = xkb_atom_text(keymap2->ctx, entry2->alias); |
| 147 |
if (!streq_null(alias1, alias2)) { |
| 148 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 149 |
"Alias #%u names do not match: \"%s\" != \"%s\"\n", |
| 150 |
a, alias1, alias2); |
| 151 |
identical = false; |
| 152 |
} |
| 153 |
|
| 154 |
const char * const real1 = xkb_atom_text(keymap1->ctx, entry1->real); |
| 155 |
const char * const real2 = xkb_atom_text(keymap2->ctx, entry2->real); |
| 156 |
if (!streq_null(real1, real2)) { |
| 157 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 158 |
"Alias #%u \"%s\" target do not match: \"%s\" != \"%s\"\n", |
| 159 |
a, alias1, real1, real2); |
| 160 |
identical = false; |
| 161 |
} |
| 162 |
} |
| 163 |
if (keymap1->num_key_aliases != keymap2->num_key_aliases) { |
| 164 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 165 |
"Aliases count do not match: %u != %u\n", |
| 166 |
keymap1->num_key_aliases, keymap2->num_key_aliases); |
| 167 |
identical = false; |
| 168 |
} |
| 169 |
|
| 170 |
return identical; |
| 171 |
} |
| 172 |
|
| 173 |
static bool |
| 174 |
keymap_compare_leds(struct xkb_context *ctx, |
| 175 |
const struct xkb_keymap *keymap1, |
| 176 |
const struct xkb_keymap *keymap2) |
| 177 |
{ |
| 178 |
bool identical = true; |
| 179 |
|
| 180 |
/* Check common LEDs */ |
| 181 |
xkb_led_index_t led_max = MIN(keymap1->num_leds, keymap2->num_leds); |
| 182 |
for (xkb_led_index_t led = 0; led < led_max; led++) { |
| 183 |
const struct xkb_led * const led1 = &keymap1->leds[led]; |
| 184 |
const struct xkb_led * const led2 = &keymap2->leds[led]; |
| 185 |
|
| 186 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 187 |
const char * const name1 = xkb_atom_text(keymap1->ctx, led1->name); |
| 188 |
const char * const name2 = xkb_atom_text(keymap2->ctx, led2->name); |
| 189 |
if (!streq_null(name1, name2)) { |
| 190 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 191 |
"LED #%"PRIu32" names do not match: \"%s\" != \"%s\"\n", |
| 192 |
led, name1, name2); |
| 193 |
identical = false; |
| 194 |
} |
| 195 |
|
| 196 |
if (led1->which_groups != led2->which_groups) { |
| 197 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 198 |
"LED #%"PRIu32" \"%s\" `which_groups` do not match: " |
| 199 |
"0x%x != 0x%x\n", |
| 200 |
led, name1, led1->which_groups, led2->which_groups); |
| 201 |
identical = false; |
| 202 |
} |
| 203 |
|
| 204 |
if (led1->groups != led2->groups) { |
| 205 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 206 |
"LED #%"PRIu32" \"%s\" `groups` do not match: " |
| 207 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 208 |
led, name1, led1->groups, led2->groups); |
| 209 |
identical = false; |
| 210 |
} |
| 211 |
|
| 212 |
if (led1->which_mods != led2->which_mods) { |
| 213 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 214 |
"LED #%"PRIu32" \"%s\" `which_mods` do not match: " |
| 215 |
"0x%x != 0x%x\n", |
| 216 |
led, name1, led1->which_mods, led2->which_mods); |
| 217 |
identical = false; |
| 218 |
} |
| 219 |
|
| 220 |
if (led1->mods.mods != led2->mods.mods) { |
| 221 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 222 |
"LED #%"PRIu32" \"%s\" `mods` do not match: " |
| 223 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 224 |
led, name1, led1->mods.mods, led2->mods.mods); |
| 225 |
identical = false; |
| 226 |
} |
| 227 |
|
| 228 |
if (led1->ctrls != led2->ctrls) { |
| 229 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 230 |
"LED #%"PRIu32" \"%s\" `ctrls` do not match: " |
| 231 |
"0x%x != 0x%x\n", |
| 232 |
led, name1, led1->ctrls, led2->ctrls); |
| 233 |
identical = false; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (keymap1->num_leds != keymap2->num_leds) { |
| 238 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 239 |
"LEDs count do not match: %"PRIu32" != %"PRIu32"\n", |
| 240 |
keymap1->num_leds, keymap2->num_leds); |
| 241 |
identical = false; |
| 242 |
} |
| 243 |
|
| 244 |
return identical; |
| 245 |
} |
| 246 |
|
| 247 |
static bool |
| 248 |
compare_types(struct xkb_context *ctx, |
| 249 |
const struct xkb_keymap *keymap1, const struct xkb_keymap *keymap2, |
| 250 |
const struct xkb_key_type *type1, const struct xkb_key_type *type2) |
| 251 |
{ |
| 252 |
bool identical = true; |
| 253 |
|
| 254 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 255 |
const char * const name1 = xkb_atom_text(keymap1->ctx, type1->name); |
| 256 |
const char * const name2 = xkb_atom_text(keymap2->ctx, type2->name); |
| 257 |
if (!streq_null(name1, name2)) { |
| 258 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 259 |
"Key type names do not match: \"%s\" != \"%s\"\n", |
| 260 |
name1, name2); |
| 261 |
identical = false; |
| 262 |
} |
| 263 |
|
| 264 |
/* From here we will use the name of `type1` as the reference */ |
| 265 |
|
| 266 |
if (type1->mods.mods != type2->mods.mods) { |
| 267 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 268 |
"Key type \"%s\" mods do not match: " |
| 269 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 270 |
name1, type1->mods.mods, type2->mods.mods); |
| 271 |
/* No point to check further properties */ |
| 272 |
return false; |
| 273 |
} |
| 274 |
|
| 275 |
if (type1->num_levels != type2->num_levels) { |
| 276 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 277 |
"Key type \"%s\" levels count do not match: " |
| 278 |
"%"PRIu32" != %"PRIu32"\n", |
| 279 |
name1, type1->num_levels, type2->num_levels); |
| 280 |
/* No point to check further properties */ |
| 281 |
return false; |
| 282 |
} |
| 283 |
|
| 284 |
if (type1->num_level_names != type2->num_level_names) { |
| 285 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 286 |
"Key type \"%s\" level names count do not match: " |
| 287 |
"%"PRIu32" != %"PRIu32"\n", |
| 288 |
name1, type1->num_level_names, type2->num_level_names); |
| 289 |
identical = false; |
| 290 |
} else { |
| 291 |
for (xkb_level_index_t l = 0; l < type1->num_level_names; l++) { |
| 292 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 293 |
const char * const lname1 = xkb_atom_text(keymap1->ctx, |
| 294 |
type1->level_names[l]); |
| 295 |
const char * const lname2 = xkb_atom_text(keymap2->ctx, |
| 296 |
type2->level_names[l]); |
| 297 |
if (!streq_null(lname1, lname2)) { |
| 298 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 299 |
"Key type \"%s\" level #%"PRIu32" names do not match: " |
| 300 |
"\"%s\" != \"%s\"\n", |
| 301 |
name1, l, lname1, lname2); |
| 302 |
identical = false; |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if (type1->num_entries != type2->num_entries) { |
| 308 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 309 |
"Key type \"%s\" entries count do not match: " |
| 310 |
"%"PRIu32" != %"PRIu32"\n", |
| 311 |
name1, type1->num_entries, type2->num_entries); |
| 312 |
identical = false; |
| 313 |
} else { |
| 314 |
for (darray_size_t e = 0; e < type1->num_entries; e++) { |
| 315 |
const struct xkb_key_type_entry * const entry1 = &type1->entries[e]; |
| 316 |
const struct xkb_key_type_entry * const entry2 = &type2->entries[e]; |
| 317 |
if (entry1->level != entry2->level) { |
| 318 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 319 |
"Key type \"%s\" entry #%u levels do not match: " |
| 320 |
"%"PRIu32" != %"PRIu32"\n", |
| 321 |
name1, e, entry1->level, entry2->level); |
| 322 |
identical = false; |
| 323 |
} |
| 324 |
if (entry1->mods.mods != entry2->mods.mods) { |
| 325 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 326 |
"Key type \"%s\" entry #%u mods do not match: " |
| 327 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 328 |
name1, e, entry1->mods.mods, entry2->mods.mods); |
| 329 |
identical = false; |
| 330 |
} |
| 331 |
if (entry1->preserve.mods != entry2->preserve.mods) { |
| 332 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 333 |
"Key type \"%s\" entry #%u preserve do not match: " |
| 334 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 335 |
name1, e, entry1->preserve.mods, entry2->preserve.mods); |
| 336 |
identical = false; |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
return identical; |
| 342 |
} |
| 343 |
|
| 344 |
static bool |
| 345 |
keymap_compare_types(struct xkb_context *ctx, |
| 346 |
const struct xkb_keymap *keymap1, const struct xkb_keymap *keymap2) |
| 347 |
{ |
| 348 |
bool identical = true; |
| 349 |
|
| 350 |
/* Compare common types */ |
| 351 |
const darray_size_t t_max = MIN(keymap1->num_types, keymap2->num_types); |
| 352 |
for (darray_size_t t = 0; t < t_max; t++) { |
| 353 |
identical = compare_types(ctx, keymap1, keymap2, |
| 354 |
&keymap1->types[t], &keymap2->types[t]) |
| 355 |
&& identical; |
| 356 |
} |
| 357 |
|
| 358 |
if (keymap1->num_types != keymap2->num_types) { |
| 359 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 360 |
"Key types counts do not match: %u != %u\n", |
| 361 |
keymap1->num_types, keymap2->num_types); |
| 362 |
identical = false; |
| 363 |
} |
| 364 |
|
| 365 |
return identical; |
| 366 |
} |
| 367 |
|
| 368 |
static bool |
| 369 |
compare_groups(struct xkb_context *ctx, |
| 370 |
const struct xkb_keymap *keymap1, const struct xkb_keymap *keymap2, |
| 371 |
xkb_keycode_t kc, xkb_layout_index_t g, |
| 372 |
const struct xkb_group *group1, const struct xkb_group *group2) |
| 373 |
{ |
| 374 |
/* TODO: repeatedly compare the same key types is inefficient!! */ |
| 375 |
if (!compare_types(ctx, keymap1, keymap2, group1->type, group2->type)) { |
| 376 |
const char * const name1 = xkb_atom_text(keymap1->ctx, |
| 377 |
group1->type->name); |
| 378 |
const char * const name2 = xkb_atom_text(keymap2->ctx, |
| 379 |
group2->type->name); |
| 380 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 381 |
"Key 0x%"PRIx32"/group %"PRIu32" types do not match: " |
| 382 |
"\"%s\" != \"%s\"\n", kc, g, name1, name2); |
| 383 |
/* No relevant to compare groups with different types */ |
| 384 |
return false; |
| 385 |
} |
| 386 |
assert(group1->type->num_levels == group2->type->num_levels); |
| 387 |
|
| 388 |
bool identical = true; |
| 389 |
for (xkb_level_index_t l = 0; l < group1->type->num_levels; l++) { |
| 390 |
const struct xkb_level * const level1 = &group1->levels[l]; |
| 391 |
const struct xkb_level * const level2 = &group2->levels[l]; |
| 392 |
|
| 393 |
/* Keysyms */ |
| 394 |
if (level1->num_syms != level2->num_syms) { |
| 395 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 396 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 397 |
"keysyms count do not match: %u != %u\n", |
| 398 |
kc, g, l, level1->num_syms, level2->num_syms); |
| 399 |
identical = false; |
| 400 |
} else { |
| 401 |
if (level1->num_syms > 1) { |
| 402 |
for (xkb_keysym_count_t k = 0; k < level1->num_syms; k++) { |
| 403 |
if (level1->s.syms[k] == level2->s.syms[k]) |
| 404 |
continue; |
| 405 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 406 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 407 |
"keysyms #%u do not match: " |
| 408 |
"0x%"PRIx32" != 0x%"PRIx32"\n", |
| 409 |
kc, g, l, k, level1->s.syms[k], level2->s.syms[k]); |
| 410 |
identical = false; |
| 411 |
} |
| 412 |
} else if (level1->s.sym != level2->s.sym) { |
| 413 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 414 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 415 |
"keysyms do not match: 0x%"PRIx32" != 0x%"PRIx32"\n", |
| 416 |
kc, g, l, level1->s.sym, level2->s.sym); |
| 417 |
identical = false; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
/* Actions */ |
| 422 |
if (level1->num_actions != level2->num_actions) { |
| 423 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 424 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 425 |
"actions count do not match: %u != %u\n", |
| 426 |
kc, g, l, level1->num_actions, level2->num_actions); |
| 427 |
identical = false; |
| 428 |
} else { |
| 429 |
if (level1->num_actions > 1) { |
| 430 |
for (xkb_action_count_t a = 0; a < level1->num_actions; a++) { |
| 431 |
if (action_equal(&level1->a.actions[a], &level2->a.actions[a])) |
| 432 |
continue; |
| 433 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 434 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 435 |
"actions #%u do not match\n", kc, g, l, a); |
| 436 |
identical = false; |
| 437 |
} |
| 438 |
} else if (level1->num_actions == 1 && |
| 439 |
!action_equal(&level1->a.action, &level2->a.action)) { |
| 440 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 441 |
"Key 0x%"PRIx32"/group %"PRIu32"/level %"PRIu32" " |
| 442 |
"actions do not match\n", kc, g, l); |
| 443 |
identical = false; |
| 444 |
} |
| 445 |
} |
| 446 |
} |
| 447 |
return identical; |
| 448 |
} |
| 449 |
|
| 450 |
static bool |
| 451 |
keymap_compare_symbols(struct xkb_context *ctx, |
| 452 |
const struct xkb_keymap *keymap1, |
| 453 |
const struct xkb_keymap *keymap2) |
| 454 |
{ |
| 455 |
bool identical = true; |
| 456 |
|
| 457 |
/* |
| 458 |
* Check groups |
| 459 |
*/ |
| 460 |
if (keymap1->num_groups != keymap2->num_groups) { |
| 461 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 462 |
"Group counts do not match: %"PRIu32" != %"PRIu32"\n", |
| 463 |
keymap1->num_groups, keymap2->num_groups); |
| 464 |
identical = false; |
| 465 |
} |
| 466 |
|
| 467 |
if (keymap1->num_group_names != keymap2->num_group_names) { |
| 468 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 469 |
"Group name counts do not match: %"PRIu32" != %"PRIu32"\n", |
| 470 |
keymap1->num_group_names, keymap2->num_group_names); |
| 471 |
identical = false; |
| 472 |
} else { |
| 473 |
for (xkb_layout_index_t g = 0; g < keymap1->num_group_names; g++) { |
| 474 |
/* NOTE: cannot compare atoms: keymaps may use different contexts */ |
| 475 |
const char * const name1 = xkb_atom_text(keymap1->ctx, |
| 476 |
keymap1->group_names[g]); |
| 477 |
const char * const name2 = xkb_atom_text(keymap2->ctx, |
| 478 |
keymap2->group_names[g]); |
| 479 |
if (!streq_null(name1, name2)) { |
| 480 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 481 |
"Group #%"PRIu32" names do not match: " |
| 482 |
"\"%s\" != \"%s\"\n", g, name1, name2); |
| 483 |
identical = false; |
| 484 |
} |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
/* |
| 489 |
* Check common keycodes |
| 490 |
*/ |
| 491 |
const xkb_keycode_t k_max = MIN(keymap1->num_keys, keymap2->num_keys); |
| 492 |
for (xkb_keycode_t k = 0; k < k_max; k++) { |
| 493 |
const struct xkb_key * const key1 = &keymap1->keys[k]; |
| 494 |
const struct xkb_key * const key2 = &keymap1->keys[k]; |
| 495 |
if (key1->keycode != key2->keycode) { |
| 496 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 497 |
"Key #%"PRIu32" keycodes do not match: " |
| 498 |
"%"PRIx32" != %"PRIx32"\n", k, key1->keycode, key2->keycode); |
| 499 |
identical = false; |
| 500 |
/* It does not make sense to compare further properties */ |
| 501 |
continue; |
| 502 |
} |
| 503 |
|
| 504 |
const xkb_keycode_t kc = key1->keycode; |
| 505 |
|
| 506 |
/* NOTE: key name checked in `compare_keycodes()` */ |
| 507 |
|
| 508 |
if (key1->modmap != key2->modmap) { |
| 509 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 510 |
"Key 0x%"PRIx32" modmap do not match: " |
| 511 |
"0x%"PRIx32" != 0x%"PRIx32"\n", kc, key1->modmap, key2->modmap); |
| 512 |
identical = false; |
| 513 |
} |
| 514 |
if (key1->vmodmap != key2->vmodmap) { |
| 515 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 516 |
"Key 0x%"PRIx32" vmodmap do not match: " |
| 517 |
"0x%"PRIx32" != 0x%"PRIx32"\n", kc, key1->vmodmap, key2->vmodmap); |
| 518 |
identical = false; |
| 519 |
} |
| 520 |
if (key1->repeats != key2->repeats) { |
| 521 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 522 |
"Key 0x%"PRIx32" repeats do not match: " |
| 523 |
"%d != %d\n", kc, key1->repeats, key2->repeats); |
| 524 |
identical = false; |
| 525 |
} |
| 526 |
if (key1->out_of_range_group_action != key2->out_of_range_group_action || |
| 527 |
key1->out_of_range_group_number != key2->out_of_range_group_number) { |
| 528 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 529 |
"Key 0x%"PRIx32" out-of-range do not match: " |
| 530 |
"%d != %d or %"PRIu32" != %"PRIu32"\n", kc, |
| 531 |
key1->out_of_range_group_action, |
| 532 |
key2->out_of_range_group_action, |
| 533 |
key1->out_of_range_group_number, |
| 534 |
key2->out_of_range_group_number); |
| 535 |
identical = false; |
| 536 |
} |
| 537 |
if (key1->num_groups != key2->num_groups) { |
| 538 |
log_err(ctx, XKB_LOG_MESSAGE_NO_ID, |
| 539 |
"Key 0x%"PRIx32" groups counts do not match: " |
| 540 |
"%"PRIu32" != %"PRIu32"\n", kc, |
| 541 |
key1->num_groups, key2->num_groups); |
| 542 |
identical = false; |
| 543 |
} |
| 544 |
const xkb_layout_index_t g_max = MIN(key1->num_groups, key2->num_groups); |
| 545 |
for (xkb_layout_index_t g = 0; g < g_max; g++) { |
| 546 |
identical = compare_groups(ctx, keymap1, keymap2, kc, g, |
| 547 |
&key1->groups[g], &key2->groups[g]) |
| 548 |
&& identical; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
return identical; |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Compare two keymaps. |
| 557 |
* |
| 558 |
* This is primarily aimed for testing if two keymaps compiled from different |
| 559 |
* sources have the same properties. |
| 560 |
* |
| 561 |
* Return `true` if the properties to compare are identical, `false` otherwise. |
| 562 |
*/ |
| 563 |
bool |
| 564 |
xkb_keymap_compare(struct xkb_context *ctx, |
| 565 |
const struct xkb_keymap *keymap1, const struct xkb_keymap *keymap2, |
| 566 |
enum xkb_keymap_compare_property properties) |
| 567 |
{ |
| 568 |
bool identical = true; |
| 569 |
|
| 570 |
if (properties & XKB_KEYMAP_CMP_MODS) { |
| 571 |
identical = keymap_compare_mods(ctx, keymap1, keymap2) && identical; |
| 572 |
} |
| 573 |
|
| 574 |
if (properties & XKB_KEYMAP_CMP_TYPES) { |
| 575 |
identical = keymap_compare_types(ctx, keymap1, keymap2) && identical; |
| 576 |
} |
| 577 |
|
| 578 |
if (properties & XKB_KEYMAP_CMP_LEDS) { |
| 579 |
identical = keymap_compare_leds(ctx, keymap1, keymap2) && identical; |
| 580 |
} |
| 581 |
|
| 582 |
if (properties & XKB_KEYMAP_CMP_KEYCODES) { |
| 583 |
identical = keymap_compare_keycodes(ctx, keymap1, keymap2) && identical; |
| 584 |
} |
| 585 |
|
| 586 |
if (properties & XKB_KEYMAP_CMP_SYMBOLS) { |
| 587 |
identical = keymap_compare_symbols(ctx, keymap1, keymap2) && identical; |
| 588 |
} |
| 589 |
|
| 590 |
return identical; |
| 591 |
} |