Hash :
1c352199
Author :
Date :
2020-09-07T11:35:22
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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
/*
* Copyright © 2012 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Author: Daniel Stone <daniel@fooishbar.org>
*/
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "evdev-scancodes.h"
#include "test.h"
/* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
* keycode set (where ESC is 9). */
#define EVDEV_OFFSET 8
static void
print_state(struct xkb_state *state)
{
struct xkb_keymap *keymap;
xkb_layout_index_t group;
xkb_mod_index_t mod;
xkb_led_index_t led;
group = xkb_state_serialize_layout(state, XKB_STATE_LAYOUT_EFFECTIVE);
mod = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
/* led = xkb_state_serialize_leds(state, XKB_STATE_LEDS); */
if (!group && !mod /* && !led */) {
fprintf(stderr, "\tno state\n");
return;
}
keymap = xkb_state_get_keymap(state);
for (group = 0; group < xkb_keymap_num_layouts(keymap); group++) {
if (xkb_state_layout_index_is_active(state, group,
XKB_STATE_LAYOUT_EFFECTIVE |
XKB_STATE_LAYOUT_DEPRESSED |
XKB_STATE_LAYOUT_LATCHED |
XKB_STATE_LAYOUT_LOCKED) <= 0)
continue;
fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
xkb_keymap_layout_get_name(keymap, group),
group,
xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_EFFECTIVE) > 0 ?
"effective " : "",
xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_DEPRESSED) > 0 ?
"depressed " : "",
xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_LATCHED) > 0 ?
"latched " : "",
xkb_state_layout_index_is_active(state, group, XKB_STATE_LAYOUT_LOCKED) > 0 ?
"locked " : "");
}
for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
if (xkb_state_mod_index_is_active(state, mod,
XKB_STATE_MODS_EFFECTIVE |
XKB_STATE_MODS_DEPRESSED |
XKB_STATE_MODS_LATCHED |
XKB_STATE_MODS_LOCKED) <= 0)
continue;
fprintf(stderr, "\tmod %s (%d): %s%s%s%s\n",
xkb_keymap_mod_get_name(keymap, mod),
mod,
xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_EFFECTIVE) > 0 ?
"effective " : "",
xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_DEPRESSED) > 0 ?
"depressed " : "",
xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_LATCHED) > 0 ?
"latched " : "",
xkb_state_mod_index_is_active(state, mod, XKB_STATE_MODS_LOCKED) > 0 ?
"locked " : "");
}
for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
if (xkb_state_led_index_is_active(state, led) <= 0)
continue;
fprintf(stderr, "\tled %s (%d): active\n",
xkb_keymap_led_get_name(keymap, led),
led);
}
}
static void
test_update_key(struct xkb_keymap *keymap)
{
struct xkb_state *state = xkb_state_new(keymap);
const xkb_keysym_t *syms;
xkb_keysym_t one_sym;
int num_syms;
assert(state);
/* LCtrl down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
fprintf(stderr, "dumping state for LCtrl down:\n");
print_state(state);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
XKB_STATE_MODS_DEPRESSED) > 0);
/* LCtrl + RAlt down */
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
print_state(state);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
XKB_STATE_MODS_DEPRESSED) > 0);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
XKB_STATE_MODS_DEPRESSED) > 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
XKB_STATE_MATCH_ALL,
XKB_MOD_NAME_CTRL,
XKB_MOD_NAME_ALT,
NULL) > 0);
assert(xkb_state_mod_indices_are_active(state, XKB_STATE_MODS_DEPRESSED,
XKB_STATE_MATCH_ALL,
xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL),
xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT),
XKB_MOD_INVALID) > 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
XKB_STATE_MATCH_ALL,
XKB_MOD_NAME_ALT,
NULL) == 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
XKB_STATE_MATCH_ALL |
XKB_STATE_MATCH_NON_EXCLUSIVE,
XKB_MOD_NAME_ALT,
NULL) > 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
(XKB_STATE_MATCH_ANY |
XKB_STATE_MATCH_NON_EXCLUSIVE),
XKB_MOD_NAME_ALT,
NULL) > 0);
/* RAlt down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for RAlt down:\n");
print_state(state);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
XKB_STATE_MODS_DEPRESSED) > 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_DEPRESSED,
XKB_STATE_MATCH_ANY,
XKB_MOD_NAME_CTRL,
XKB_MOD_NAME_ALT,
NULL) > 0);
assert(xkb_state_mod_names_are_active(state, XKB_STATE_MODS_LATCHED,
XKB_STATE_MATCH_ANY,
XKB_MOD_NAME_CTRL,
XKB_MOD_NAME_ALT,
NULL) == 0);
/* none down */
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
XKB_STATE_MODS_EFFECTIVE) == 0);
/* Caps locked */
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
XKB_STATE_MODS_DEPRESSED) > 0);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for Caps Lock:\n");
print_state(state);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
XKB_STATE_MODS_DEPRESSED) == 0);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
XKB_STATE_MODS_LOCKED) > 0);
assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
/* Num Lock locked */
xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_UP);
fprintf(stderr, "dumping state for Caps Lock + Num Lock:\n");
print_state(state);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
XKB_STATE_MODS_LOCKED) > 0);
assert(xkb_state_mod_name_is_active(state, "Mod2",
XKB_STATE_MODS_LOCKED) > 0);
num_syms = xkb_state_key_get_syms(state, KEY_KP1 + EVDEV_OFFSET, &syms);
assert(num_syms == 1 && syms[0] == XKB_KEY_KP_1);
assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_NUM) > 0);
/* Num Lock unlocked */
xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_NUMLOCK + EVDEV_OFFSET, XKB_KEY_UP);
/* Switch to group 2 */
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_led_name_is_active(state, "Group 2") > 0);
assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_NUM) == 0);
/* Switch back to group 1. */
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
/* Caps unlocked */
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) == 0);
num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
assert(num_syms == 1 && syms[0] == XKB_KEY_q);
/* Multiple symbols */
num_syms = xkb_state_key_get_syms(state, KEY_6 + EVDEV_OFFSET, &syms);
assert(num_syms == 5 &&
syms[0] == XKB_KEY_H && syms[1] == XKB_KEY_E &&
syms[2] == XKB_KEY_L && syms[3] == XKB_KEY_L &&
syms[4] == XKB_KEY_O);
one_sym = xkb_state_key_get_one_sym(state, KEY_6 + EVDEV_OFFSET);
assert(one_sym == XKB_KEY_NoSymbol);
xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_UP);
one_sym = xkb_state_key_get_one_sym(state, KEY_5 + EVDEV_OFFSET);
assert(one_sym == XKB_KEY_5);
xkb_state_unref(state);
}
static void
test_serialisation(struct xkb_keymap *keymap)
{
struct xkb_state *state = xkb_state_new(keymap);
xkb_mod_mask_t base_mods;
xkb_mod_mask_t latched_mods;
xkb_mod_mask_t locked_mods;
xkb_mod_mask_t effective_mods;
xkb_mod_index_t caps, shift, ctrl;
xkb_layout_index_t base_group = 0;
xkb_layout_index_t latched_group = 0;
xkb_layout_index_t locked_group = 0;
assert(state);
caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
assert(caps != XKB_MOD_INVALID);
shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
assert(shift != XKB_MOD_INVALID);
ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
assert(ctrl != XKB_MOD_INVALID);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == 0);
latched_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
assert(latched_mods == 0);
locked_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
assert(locked_mods == (1U << caps));
effective_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
assert(effective_mods == locked_mods);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
base_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED);
assert(base_mods == (1U << shift));
latched_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED);
assert(latched_mods == 0);
locked_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED);
assert(locked_mods == (1U << caps));
effective_mods = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
assert(effective_mods == (base_mods | locked_mods));
base_mods |= (1U << ctrl);
xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
base_group, latched_group, locked_group);
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_DEPRESSED) > 0);
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
xkb_state_unref(state);
}
static void
test_update_mask_mods(struct xkb_keymap *keymap)
{
struct xkb_state *state = xkb_state_new(keymap);
xkb_mod_index_t caps, shift, num, alt, mod1, mod2;
enum xkb_state_component changed;
assert(state);
caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
assert(caps != XKB_MOD_INVALID);
shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
assert(shift != XKB_MOD_INVALID);
num = xkb_keymap_mod_get_index(keymap, "NumLock");
assert(num != XKB_MOD_INVALID);
alt = xkb_keymap_mod_get_index(keymap, "Alt");
assert(alt != XKB_MOD_INVALID);
mod1 = xkb_keymap_mod_get_index(keymap, "Mod1");
assert(mod1 != XKB_MOD_INVALID);
mod2 = xkb_keymap_mod_get_index(keymap, "Mod2");
assert(mod2 != XKB_MOD_INVALID);
changed = xkb_state_update_mask(state, 1 << caps, 0, 0, 0, 0, 0);
assert(changed == (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_EFFECTIVE));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) ==
(1u << caps));
changed = xkb_state_update_mask(state, (1 << caps), 0, (1 << shift), 0, 0, 0);
assert(changed == (XKB_STATE_MODS_LOCKED | XKB_STATE_MODS_EFFECTIVE |
XKB_STATE_LEDS));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) ==
((1u << caps) | (1u << shift)));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED) ==
(1u << caps));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_LATCHED) == 0);
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED) ==
(1u << shift));
changed = xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
assert(changed == (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LOCKED |
XKB_STATE_MODS_EFFECTIVE | XKB_STATE_LEDS));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) == 0);
changed = xkb_state_update_mask(state, (1 << alt), 0, 0, 0, 0, 0);
assert(changed == (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_EFFECTIVE));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) ==
((1u << alt) | (1u << mod1)));
changed = xkb_state_update_mask(state, 0, 0, (1 << num), 0, 0, 0);
assert(changed == (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LOCKED |
XKB_STATE_MODS_EFFECTIVE | XKB_STATE_LEDS));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) ==
((1u << num) | (1u << mod2)));
xkb_state_update_mask(state, 0, 0, 0, 0, 0, 0);
changed = xkb_state_update_mask(state, (1 << mod2), 0, (1 << num), 0, 0, 0);
assert(changed == (XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LOCKED |
XKB_STATE_MODS_EFFECTIVE | XKB_STATE_LEDS));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE) ==
((1u << mod2) | (1u << num)));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_DEPRESSED) ==
(1u << mod2));
assert(xkb_state_serialize_mods(state, XKB_STATE_MODS_LOCKED) ==
((1u << num) | (1u << mod2)));
xkb_state_unref(state);
}
static void
test_repeat(struct xkb_keymap *keymap)
{
assert(!xkb_keymap_key_repeats(keymap, KEY_LEFTSHIFT + 8));
assert(xkb_keymap_key_repeats(keymap, KEY_A + 8));
assert(xkb_keymap_key_repeats(keymap, KEY_8 + 8));
assert(xkb_keymap_key_repeats(keymap, KEY_DOWN + 8));
assert(xkb_keymap_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
}
static void
test_consume(struct xkb_keymap *keymap)
{
struct xkb_state *state;
xkb_mod_index_t alt, shift, caps, ctrl, mod5;
xkb_mod_mask_t mask;
state = xkb_state_new(keymap);
assert(state);
alt = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
assert(alt != XKB_MOD_INVALID);
shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
assert(shift != XKB_MOD_INVALID);
caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
assert(caps != XKB_MOD_INVALID);
ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
assert(ctrl != XKB_MOD_INVALID);
mod5 = xkb_keymap_mod_get_index(keymap, "Mod5");
assert(mod5 != XKB_MOD_INVALID);
/* Test remove_consumed() */
xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
fprintf(stderr, "dumping state for Alt-Shift-+\n");
print_state(state);
mask = xkb_state_serialize_mods(state, XKB_STATE_MODS_EFFECTIVE);
assert(mask == ((1U << alt) | (1U << shift)));
mask = xkb_state_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
mask);
assert(mask == (1U << alt));
/* Test get_consumed_mods() */
mask = xkb_state_key_get_consumed_mods(state, KEY_EQUAL + EVDEV_OFFSET);
assert(mask == (1U << shift));
mask = xkb_state_key_get_consumed_mods(state, KEY_ESC + EVDEV_OFFSET);
assert(mask == 0);
xkb_state_unref(state);
/* Test is_consumed() - simple ALPHABETIC type. */
state = xkb_state_new(keymap);
assert(state);
mask = xkb_state_key_get_consumed_mods(state, KEY_A + EVDEV_OFFSET);
assert(mask == ((1U << shift) | (1U << caps)));
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, caps) > 0);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, shift) > 0);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, caps) > 0);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, shift) > 0);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, caps) > 0);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, shift) > 0);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, caps) > 0);
assert(xkb_state_mod_index_is_consumed(state, KEY_A + EVDEV_OFFSET, shift) > 0);
xkb_state_unref(state);
/* More complicated - CTRL+ALT */
state = xkb_state_new(keymap);
assert(state);
mask = xkb_state_key_get_consumed_mods(state, KEY_F1 + EVDEV_OFFSET);
assert(mask == ((1U << shift) | (1U << alt) | (1U << ctrl) | (1U << mod5)));
/* Shift is preserved. */
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
mask = xkb_state_key_get_consumed_mods(state, KEY_F1 + EVDEV_OFFSET);
assert(mask == ((1U << alt) | (1U << ctrl) | (1U << mod5)));
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
mask = xkb_state_key_get_consumed_mods(state, KEY_F1 + EVDEV_OFFSET);
assert(mask == ((1U << shift) | (1U << alt) | (1U << ctrl) | (1U << mod5)));
xkb_state_unref(state);
/* Test XKB_CONSUMED_MODE_GTK, CTRL+ALT */
state = xkb_state_new(keymap);
assert(state);
mask = xkb_state_key_get_consumed_mods2(state, KEY_F1 + EVDEV_OFFSET,
XKB_CONSUMED_MODE_GTK);
assert(mask == 0);
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
mask = xkb_state_key_get_consumed_mods2(state, KEY_F1 + EVDEV_OFFSET,
XKB_CONSUMED_MODE_GTK);
assert(mask == 0);
xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
mask = xkb_state_key_get_consumed_mods2(state, KEY_F1 + EVDEV_OFFSET,
XKB_CONSUMED_MODE_GTK);
assert(mask == ((1U << alt) | (1U << ctrl)));
xkb_state_unref(state);
/* Test XKB_CONSUMED_MODE_GTK, Simple Shift */
state = xkb_state_new(keymap);
assert(state);
mask = xkb_state_key_get_consumed_mods2(state, KEY_A + EVDEV_OFFSET,
XKB_CONSUMED_MODE_GTK);
assert(mask == ((1U << shift) | (1U << caps)));
xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
mask = xkb_state_key_get_consumed_mods2(state, KEY_A + EVDEV_OFFSET,
XKB_CONSUMED_MODE_GTK);
assert(mask == ((1U << shift) | (1U << caps)));
xkb_state_unref(state);
}
static void
key_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
{
xkb_keycode_t *counter = data;
assert(*counter == key);
(*counter)++;
}
static void
test_range(struct xkb_keymap *keymap)
{
xkb_keycode_t counter;
assert(xkb_keymap_min_keycode(keymap) == 9);
assert(xkb_keymap_max_keycode(keymap) == 569);
counter = xkb_keymap_min_keycode(keymap);
xkb_keymap_key_for_each(keymap, key_iter, &counter);
assert(counter == xkb_keymap_max_keycode(keymap) + 1);
}
static void
test_caps_keysym_transformation(struct xkb_keymap *keymap)
{
struct xkb_state *state = xkb_state_new(keymap);
xkb_mod_index_t caps, shift;
int nsyms;
xkb_keysym_t sym;
const xkb_keysym_t *syms;
assert(state);
/* See xkb_state_key_get_one_sym() for what's this all about. */
caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
assert(caps != XKB_MOD_INVALID && shift != XKB_MOD_INVALID);
assert(xkb_state_key_get_layout(state, KEY_A + 8) == 0);
assert(xkb_state_key_get_layout(state, KEY_SEMICOLON + 8) == 0);
/* Without caps, no transformation. */
assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 0);
sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
assert(sym == XKB_KEY_a);
assert(xkb_state_key_get_level(state, KEY_SEMICOLON + 8, 0) == 0);
sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
assert(sym == XKB_KEY_eacute);
nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
/* With shift, no transformation (only different level). */
xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_DOWN);
assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) > 0);
assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 1);
sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
assert(sym == XKB_KEY_A);
sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
assert(sym == XKB_KEY_odiaeresis);
nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
assert(nsyms == 1 && syms[0] == XKB_KEY_odiaeresis);
xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_UP);
assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
/* With caps, transform in same level, only with _get_one_sym(). */
xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_UP);
assert(xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0);
assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
assert(xkb_state_key_get_level(state, KEY_A + 8, 0) == 1);
sym = xkb_state_key_get_one_sym(state, KEY_A + 8);
assert(sym == XKB_KEY_A);
assert(xkb_state_key_get_level(state, KEY_SEMICOLON + 8, 0) == 0);
sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + 8);
assert(sym == XKB_KEY_Eacute);
nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + 8, &syms);
assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
xkb_state_update_key(state, KEY_LEFTSHIFT + 8, XKB_KEY_UP);
assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 0);
xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_CAPSLOCK + 8, XKB_KEY_UP);
xkb_state_unref(state);
}
static void
test_get_utf8_utf32(struct xkb_keymap *keymap)
{
char buf[256];
struct xkb_state *state = xkb_state_new(keymap);
assert(state);
#define TEST_KEY(key, expected_utf8, expected_utf32) do { \
assert(xkb_state_key_get_utf8(state, key + 8, NULL, 0) == strlen(expected_utf8)); \
assert(xkb_state_key_get_utf8(state, key + 8, buf, sizeof(buf)) == strlen(expected_utf8)); \
assert(memcmp(buf, expected_utf8, sizeof(expected_utf8)) == 0); \
assert(xkb_state_key_get_utf32(state, key + 8) == expected_utf32); \
} while (0)
/* Simple ASCII. */
TEST_KEY(KEY_A, "a", 0x61);
TEST_KEY(KEY_ESC, "\x1B", 0x1B);
TEST_KEY(KEY_1, "1", 0x31);
/* Invalid. */
TEST_KEY(XKB_KEYCODE_INVALID - 8, "", 0);
TEST_KEY(300, "", 0);
/* No string. */
TEST_KEY(KEY_LEFTCTRL, "", 0);
TEST_KEY(KEY_NUMLOCK, "", 0);
/* Multiple keysyms. */
TEST_KEY(KEY_6, "HELLO", 0);
TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
/* Check truncation. */
memset(buf, 'X', sizeof(buf));
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 0) == strlen("HELLO"));
assert(memcmp(buf, "X", 1) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 1) == strlen("HELLO"));
assert(memcmp(buf, "", 1) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 2) == strlen("HELLO"));
assert(memcmp(buf, "H", 2) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 3) == strlen("HELLO"));
assert(memcmp(buf, "HE", 3) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 5) == strlen("HELLO"));
assert(memcmp(buf, "HELL", 5) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 6) == strlen("HELLO"));
assert(memcmp(buf, "HELLO", 6) == 0);
assert(xkb_state_key_get_utf8(state, KEY_6 + 8, buf, 7) == strlen("HELLO"));
assert(memcmp(buf, "HELLO\0X", 7) == 0);
/* Switch to ru layout */
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_key_get_layout(state, KEY_A + 8) == 1);
/* Non ASCII. */
TEST_KEY(KEY_ESC, "\x1B", 0x1B);
TEST_KEY(KEY_A, "ф", 0x0444);
TEST_KEY(KEY_Z, "я", 0x044F);
/* Switch back to us layout */
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_key_get_layout(state, KEY_A + 8) == 0);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
TEST_KEY(KEY_A, "A", 0x41);
TEST_KEY(KEY_ESC, "\x1B", 0x1B);
TEST_KEY(KEY_1, "!", 0x21);
xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
TEST_KEY(KEY_6, "HELLO", 0);
TEST_KEY(KEY_7, "YES THIS IS DOG", 0);
xkb_state_unref(state);
}
static void
test_ctrl_string_transformation(struct xkb_keymap *keymap)
{
char buf[256];
struct xkb_state *state = xkb_state_new(keymap);
xkb_mod_index_t ctrl;
assert(state);
/* See xkb_state_key_get_utf8() for what's this all about. */
ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
assert(ctrl != XKB_MOD_INVALID);
/* First without. */
TEST_KEY(KEY_A, "a", 0x61);
TEST_KEY(KEY_B, "b", 0x62);
TEST_KEY(KEY_C, "c", 0x63);
TEST_KEY(KEY_ESC, "\x1B", 0x1B);
TEST_KEY(KEY_1, "1", 0x31);
/* And with. */
xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
TEST_KEY(KEY_A, "\x01", 0x01);
TEST_KEY(KEY_B, "\x02", 0x02);
TEST_KEY(KEY_C, "\x03", 0x03);
TEST_KEY(KEY_ESC, "\x1B", 0x1B);
TEST_KEY(KEY_1, "1", 0x31);
xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
/* Switch to ru layout */
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_DOWN);
xkb_state_update_key(state, KEY_COMPOSE + EVDEV_OFFSET, XKB_KEY_UP);
assert(xkb_state_key_get_layout(state, KEY_A + 8) == 1);
/* Non ASCII. */
xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0);
TEST_KEY(KEY_A, "\x01", 0x01);
TEST_KEY(KEY_B, "\x02", 0x02);
xkb_state_update_key(state, KEY_RIGHTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
xkb_state_unref(state);
}
int
main(void)
{
struct xkb_context *context = test_get_context(0);
struct xkb_keymap *keymap;
assert(context);
/* Make sure these are allowed. */
xkb_context_unref(NULL);
xkb_keymap_unref(NULL);
xkb_state_unref(NULL);
keymap = test_compile_rules(context, "evdev", "pc104", "us,ru", NULL, "grp:menu_toggle");
assert(keymap);
test_update_key(keymap);
test_serialisation(keymap);
test_update_mask_mods(keymap);
test_repeat(keymap);
test_consume(keymap);
test_range(keymap);
test_get_utf8_utf32(keymap);
test_ctrl_string_transformation(keymap);
xkb_keymap_unref(keymap);
keymap = test_compile_rules(context, "evdev", NULL, "ch", "fr", NULL);
assert(keymap);
test_caps_keysym_transformation(keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
}