+ 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 + EVDEV_OFFSET) == 0);
+ assert(xkb_state_key_get_layout(state, KEY_SEMICOLON + EVDEV_OFFSET) == 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 + EVDEV_OFFSET, 0) == 0);
+ sym = xkb_state_key_get_one_sym(state, KEY_A + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_a);
+ assert(xkb_state_key_get_level(state, KEY_SEMICOLON + EVDEV_OFFSET, 0) == 0);
+ sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_eacute);
+ nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + EVDEV_OFFSET, &syms);
+ assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
+
+ /* With shift, no transformation (only different level). */
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, 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 + EVDEV_OFFSET, 0) == 1);
+ sym = xkb_state_key_get_one_sym(state, KEY_A + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_A);
+ sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_odiaeresis);
+ nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + EVDEV_OFFSET, &syms);
+ assert(nsyms == 1 && syms[0] == XKB_KEY_odiaeresis);
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, 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 + EVDEV_OFFSET, XKB_KEY_DOWN);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 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 + EVDEV_OFFSET, 0) == 1);
+ sym = xkb_state_key_get_one_sym(state, KEY_A + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_A);
+ assert(xkb_state_key_get_level(state, KEY_SEMICOLON + EVDEV_OFFSET, 0) == 0);
+ sym = xkb_state_key_get_one_sym(state, KEY_SEMICOLON + EVDEV_OFFSET);
+ assert(sym == XKB_KEY_Eacute);
+ nsyms = xkb_state_key_get_syms(state, KEY_SEMICOLON + EVDEV_OFFSET, &syms);
+ assert(nsyms == 1 && syms[0] == XKB_KEY_eacute);
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_UP);
+ assert(xkb_state_mod_index_is_active(state, shift, XKB_STATE_MODS_EFFECTIVE) == 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);
+
+ 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 + EVDEV_OFFSET, NULL, 0) == strlen(expected_utf8)); \
+ assert(xkb_state_key_get_utf8(state, key + EVDEV_OFFSET, buf, sizeof(buf)) == strlen(expected_utf8)); \
+ assert(memcmp(buf, expected_utf8, sizeof(expected_utf8)) == 0); \
+ assert(xkb_state_key_get_utf32(state, key + EVDEV_OFFSET) == 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 + EVDEV_OFFSET, buf, 0) == strlen("HELLO"));
+ assert(memcmp(buf, "X", 1) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, buf, 1) == strlen("HELLO"));
+ assert(memcmp(buf, "", 1) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, buf, 2) == strlen("HELLO"));
+ assert(memcmp(buf, "H", 2) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, buf, 3) == strlen("HELLO"));
+ assert(memcmp(buf, "HE", 3) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, buf, 5) == strlen("HELLO"));
+ assert(memcmp(buf, "HELL", 5) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, buf, 6) == strlen("HELLO"));
+ assert(memcmp(buf, "HELLO", 6) == 0);
+ assert(xkb_state_key_get_utf8(state, KEY_6 + EVDEV_OFFSET, 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 + EVDEV_OFFSET) == 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 + EVDEV_OFFSET) == 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 + EVDEV_OFFSET) == 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);