Keysyms: improve generator (#364)
[platform/upstream/libxkbcommon.git] / src / state.c
index 312813b..b269e6d 100644 (file)
@@ -26,6 +26,7 @@
 
 /*
  * Copyright © 2012 Intel Corporation
+ * Copyright © 2012 Ran Benita <ran234@gmail.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  *   - messages (very unlikely)
  */
 
-#include <assert.h>
-#include <stdarg.h>
+#include "config.h"
 
-#include "xkb-priv.h"
+#include "keymap.h"
+#include "keysym.h"
+#include "utf8.h"
 
 struct xkb_filter {
-    struct xkb_state *state;
     union xkb_action action;
-    xkb_keycode_t kc;
+    const struct xkb_key *key;
     uint32_t priv;
-    int (*func)(struct xkb_filter *filter, xkb_keycode_t kc,
-                enum xkb_key_direction direction);
+    bool (*func)(struct xkb_state *state,
+                 struct xkb_filter *filter,
+                 const struct xkb_key *key,
+                 enum xkb_key_direction direction);
     int refcnt;
-    struct xkb_filter *next;
 };
 
-struct xkb_state {
-    xkb_group_index_t base_group; /**< depressed */
-    xkb_group_index_t latched_group;
-    xkb_group_index_t locked_group;
-    xkb_group_index_t group; /**< effective */
+struct state_components {
+    /* These may be negative, because of -1 group actions. */
+    int32_t base_group; /**< depressed */
+    int32_t latched_group;
+    int32_t locked_group;
+    xkb_layout_index_t group; /**< effective */
 
     xkb_mod_mask_t base_mods; /**< depressed */
     xkb_mod_mask_t latched_mods;
     xkb_mod_mask_t locked_mods;
     xkb_mod_mask_t mods; /**< effective */
 
+    xkb_led_mask_t leds;
+};
+
+struct xkb_state {
+    /*
+     * Before updating the state, we keep a copy of just this struct. This
+     * allows us to report which components of the state have changed.
+     */
+    struct state_components components;
+
     /*
      * At each event, we accumulate all the needed modifications to the base
      * modifiers, and apply them at the end. These keep track of this state.
      */
     xkb_mod_mask_t set_mods;
     xkb_mod_mask_t clear_mods;
+
     /*
      * We mustn't clear a base modifier if there's another depressed key
      * which affects it, e.g. given this sequence
      * < Left Shift down, Right Shift down, Left Shift Up >
      * the modifier should still be set. This keeps the count.
      */
-    int16_t mod_key_count[sizeof(xkb_mod_mask_t) * 8];
-
-    uint32_t leds;
+    int16_t mod_key_count[XKB_MAX_MODS];
 
     int refcnt;
     darray(struct xkb_filter) filters;
     struct xkb_keymap *keymap;
 };
 
-static union xkb_action *
-xkb_key_get_action(struct xkb_state *state, xkb_keycode_t kc)
+static const struct xkb_key_type_entry *
+get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
 {
-    xkb_group_index_t group;
-    xkb_level_index_t level;
-    struct xkb_key *key = NULL;
+    for (unsigned i = 0; i < type->num_entries; i++)
+        if (entry_is_active(&type->entries[i]) &&
+            type->entries[i].mods.mask == mods)
+            return &type->entries[i];
+    return NULL;
+}
 
-    if (XkbKeycodeInRange(state->keymap, kc))
-        key = XkbKey(state->keymap, kc);
+static const struct xkb_key_type_entry *
+get_entry_for_key_state(struct xkb_state *state, const struct xkb_key *key,
+                        xkb_layout_index_t group)
+{
+    const struct xkb_key_type *type = key->groups[group].type;
+    xkb_mod_mask_t active_mods = state->components.mods & type->mods.mask;
+    return get_entry_for_mods(type, active_mods);
+}
 
-    if (!key || !XkbKeyHasActions(key)) {
-        static union xkb_action fake;
-        memset(&fake, 0, sizeof(fake));
-        fake.type = XkbSA_NoAction;
-        return &fake;
+/**
+ * Returns the level to use for the given key and state, or
+ * XKB_LEVEL_INVALID.
+ */
+XKB_EXPORT xkb_level_index_t
+xkb_state_key_get_level(struct xkb_state *state, xkb_keycode_t kc,
+                        xkb_layout_index_t layout)
+{
+    const struct xkb_key *key = XkbKey(state->keymap, kc);
+    const struct xkb_key_type_entry *entry;
+
+    if (!key || layout >= key->num_groups)
+        return XKB_LEVEL_INVALID;
+
+    /* If we don't find an explicit match the default is 0. */
+    entry = get_entry_for_key_state(state, key, layout);
+    if (!entry)
+        return 0;
+
+    return entry->level;
+}
+
+xkb_layout_index_t
+XkbWrapGroupIntoRange(int32_t group,
+                      xkb_layout_index_t num_groups,
+                      enum xkb_range_exceed_type out_of_range_group_action,
+                      xkb_layout_index_t out_of_range_group_number)
+{
+    if (num_groups == 0)
+        return XKB_LAYOUT_INVALID;
+
+    if (group >= 0 && (xkb_layout_index_t) group < num_groups)
+        return group;
+
+    switch (out_of_range_group_action) {
+    case RANGE_REDIRECT:
+        if (out_of_range_group_number >= num_groups)
+            return 0;
+        return out_of_range_group_number;
+
+    case RANGE_SATURATE:
+        if (group < 0)
+            return 0;
+        else
+            return num_groups - 1;
+
+    case RANGE_WRAP:
+    default:
+        /*
+         * C99 says a negative dividend in a modulo operation always
+         * gives a negative result.
+         */
+        if (group < 0)
+            return ((int) num_groups + (group % (int) num_groups));
+        else
+            return group % num_groups;
     }
+}
+
+/**
+ * Returns the layout to use for the given key and state, taking
+ * wrapping/clamping/etc into account, or XKB_LAYOUT_INVALID.
+ */
+XKB_EXPORT xkb_layout_index_t
+xkb_state_key_get_layout(struct xkb_state *state, xkb_keycode_t kc)
+{
+    const struct xkb_key *key = XkbKey(state->keymap, kc);
+
+    if (!key)
+        return XKB_LAYOUT_INVALID;
+
+    return XkbWrapGroupIntoRange(state->components.group, key->num_groups,
+                                 key->out_of_range_group_action,
+                                 key->out_of_range_group_number);
+}
+
+static const union xkb_action *
+xkb_key_get_action(struct xkb_state *state, const struct xkb_key *key)
+{
+    static const union xkb_action dummy = { .type = ACTION_TYPE_NONE };
+
+    xkb_layout_index_t layout;
+    xkb_level_index_t level;
 
-    group = xkb_key_get_group(state, kc);
-    level = xkb_key_get_level(state, kc, group);
+    layout = xkb_state_key_get_layout(state, key->keycode);
+    if (layout == XKB_LAYOUT_INVALID)
+        return &dummy;
 
-    return XkbKeyActionEntry(state->keymap, key, group, level);
+    level = xkb_state_key_get_level(state, key->keycode, layout);
+    if (level == XKB_LEVEL_INVALID)
+        return &dummy;
+
+    return &key->groups[layout].levels[level].action;
 }
 
 static struct xkb_filter *
 xkb_filter_new(struct xkb_state *state)
 {
-    int old_size = darray_size(state->filters);
     struct xkb_filter *filter = NULL, *iter;
 
     darray_foreach(iter, state->filters) {
@@ -144,185 +246,162 @@ xkb_filter_new(struct xkb_state *state)
 
     if (!filter) {
         darray_resize0(state->filters, darray_size(state->filters) + 1);
-        filter = &darray_item(state->filters, old_size);
+        filter = &darray_item(state->filters, darray_size(state->filters) -1);
     }
 
-    filter->state = state;
     filter->refcnt = 1;
     return filter;
 }
 
 /***====================================================================***/
 
-static int
-xkb_filter_group_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
+enum xkb_filter_result {
+    /*
+     * The event is consumed by the filters.
+     *
+     * An event is always processed by all filters, but any filter can
+     * prevent it from being processed further by consuming it.
+     */
+    XKB_FILTER_CONSUME,
+    /*
+     * The event may continue to be processed as far as this filter is
+     * concerned.
+     */
+    XKB_FILTER_CONTINUE,
+};
+
+static void
+xkb_filter_group_set_new(struct xkb_state *state, struct xkb_filter *filter)
+{
+    filter->priv = state->components.base_group;
+    if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
+        state->components.base_group = filter->action.group.group;
+    else
+        state->components.base_group += filter->action.group.group;
+}
+
+static bool
+xkb_filter_group_set_func(struct xkb_state *state,
+                          struct xkb_filter *filter,
+                          const struct xkb_key *key,
                           enum xkb_key_direction direction)
 {
-    if (kc != filter->kc) {
-        filter->action.group.flags &= ~XkbSA_ClearLocks;
-        return 1;
+    if (key != filter->key) {
+        filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
+        return XKB_FILTER_CONTINUE;
     }
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
     else if (--filter->refcnt > 0) {
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
 
-    if (filter->action.group.flags & XkbSA_GroupAbsolute)
-        filter->state->base_group = filter->action.group.group;
-    else
-        filter->state->base_group = -filter->action.group.group;
-    if (filter->action.group.flags & XkbSA_ClearLocks)
-        filter->state->locked_group = 0;
+    state->components.base_group = filter->priv;
 
-    filter->func = NULL;
+    if (filter->action.group.flags & ACTION_LOCK_CLEAR)
+        state->components.locked_group = 0;
 
-    return 1;
+    filter->func = NULL;
+    return XKB_FILTER_CONTINUE;
 }
 
-static int
-xkb_filter_group_set_new(struct xkb_state *state, xkb_keycode_t kc,
-                         union xkb_action *action)
+static void
+xkb_filter_group_lock_new(struct xkb_state *state, struct xkb_filter *filter)
 {
-    struct xkb_filter *filter = xkb_filter_new(state);
-
-    if (!filter) /* WSGO */
-        return -1;
-    filter->kc = kc;
-    filter->func = xkb_filter_group_set_func;
-    filter->action = *action;
-
-    if (action->group.flags & XkbSA_GroupAbsolute) {
-        filter->action.group.group = filter->state->base_group;
-        filter->state->base_group = action->group.group;
-    }
-    else {
-        filter->state->base_group += action->group.group;
-    }
-
-    return 1;
+    if (filter->action.group.flags & ACTION_ABSOLUTE_SWITCH)
+        state->components.locked_group = filter->action.group.group;
+    else
+        state->components.locked_group += filter->action.group.group;
 }
 
-static int
-xkb_filter_group_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
+static bool
+xkb_filter_group_lock_func(struct xkb_state *state,
+                           struct xkb_filter *filter,
+                           const struct xkb_key *key,
                            enum xkb_key_direction direction)
 {
-    if (kc != filter->kc)
-        return 1;
+    if (key != filter->key)
+        return XKB_FILTER_CONTINUE;
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
     if (--filter->refcnt > 0)
-        return 0;
+        return XKB_FILTER_CONSUME;
 
     filter->func = NULL;
-    return 1;
+    return XKB_FILTER_CONTINUE;
 }
 
-static int
-xkb_filter_group_lock_new(struct xkb_state *state, xkb_keycode_t kc,
-                          union xkb_action *action)
+static void
+xkb_filter_mod_set_new(struct xkb_state *state, struct xkb_filter *filter)
 {
-    struct xkb_filter *filter = xkb_filter_new(state);
-
-    if (!filter)
-        return 0;
-
-    filter->kc = kc;
-    filter->func = xkb_filter_group_lock_func;
-    filter->action = *action;
-
-    if (action->group.flags & XkbSA_GroupAbsolute)
-        filter->state->locked_group = action->group.group;
-    else
-        filter->state->locked_group += action->group.group;
-
-    return 1;
+    state->set_mods = filter->action.mods.mods.mask;
 }
 
-static int
-xkb_filter_mod_set_func(struct xkb_filter *filter, xkb_keycode_t kc,
+static bool
+xkb_filter_mod_set_func(struct xkb_state *state,
+                        struct xkb_filter *filter,
+                        const struct xkb_key *key,
                         enum xkb_key_direction direction)
 {
-    if (kc != filter->kc) {
-        filter->action.mods.flags &= ~XkbSA_ClearLocks;
-        return 1;
+    if (key != filter->key) {
+        filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
+        return XKB_FILTER_CONTINUE;
     }
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
     else if (--filter->refcnt > 0) {
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
 
-    filter->state->clear_mods = filter->action.mods.mods.mask;
-    if (filter->action.mods.flags & XkbSA_ClearLocks)
-        filter->state->locked_mods &= ~filter->action.mods.mods.mask;
+    state->clear_mods = filter->action.mods.mods.mask;
+    if (filter->action.mods.flags & ACTION_LOCK_CLEAR)
+        state->components.locked_mods &= ~filter->action.mods.mods.mask;
 
     filter->func = NULL;
-
-    return 1;
+    return XKB_FILTER_CONTINUE;
 }
 
-static int
-xkb_filter_mod_set_new(struct xkb_state *state, xkb_keycode_t kc,
-                       union xkb_action *action)
+static void
+xkb_filter_mod_lock_new(struct xkb_state *state, struct xkb_filter *filter)
 {
-    struct xkb_filter *filter = xkb_filter_new(state);
-
-    if (!filter) /* WSGO */
-        return -1;
-    filter->kc = kc;
-    filter->func = xkb_filter_mod_set_func;
-    filter->action = *action;
-
-    filter->state->set_mods = action->mods.mods.mask;
-
-    return 1;
+    filter->priv = (state->components.locked_mods &
+                    filter->action.mods.mods.mask);
+    state->set_mods |= filter->action.mods.mods.mask;
+    if (!(filter->action.mods.flags & ACTION_LOCK_NO_LOCK))
+        state->components.locked_mods |= filter->action.mods.mods.mask;
 }
 
-static int
-xkb_filter_mod_lock_func(struct xkb_filter *filter, xkb_keycode_t kc,
+static bool
+xkb_filter_mod_lock_func(struct xkb_state *state,
+                         struct xkb_filter *filter,
+                         const struct xkb_key *key,
                          enum xkb_key_direction direction)
 {
-    if (kc != filter->kc)
-        return 1;
+    if (key != filter->key)
+        return XKB_FILTER_CONTINUE;
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return 0;
+        return XKB_FILTER_CONSUME;
     }
     if (--filter->refcnt > 0)
-        return 0;
+        return XKB_FILTER_CONSUME;
 
-    filter->state->locked_mods &= ~filter->priv;
-    filter->func = NULL;
-    return 1;
-}
-
-static int
-xkb_filter_mod_lock_new(struct xkb_state *state, xkb_keycode_t kc,
-                        union xkb_action *action)
-{
-    struct xkb_filter *filter = xkb_filter_new(state);
+    state->clear_mods |= filter->action.mods.mods.mask;
+    if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
+        state->components.locked_mods &= ~filter->priv;
 
-    if (!filter) /* WSGO */
-        return 0;
-
-    filter->kc = kc;
-    filter->func = xkb_filter_mod_lock_func;
-    filter->action = *action;
-    filter->priv = state->locked_mods & action->mods.mods.mask;
-    state->locked_mods |= action->mods.mods.mask;
-
-    return 1;
+    filter->func = NULL;
+    return XKB_FILTER_CONTINUE;
 }
 
 enum xkb_key_latch_state {
@@ -331,8 +410,34 @@ enum xkb_key_latch_state {
     LATCH_PENDING,
 };
 
-static int
-xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
+static bool
+xkb_action_breaks_latch(const union xkb_action *action)
+{
+    switch (action->type) {
+    case ACTION_TYPE_NONE:
+    case ACTION_TYPE_PTR_BUTTON:
+    case ACTION_TYPE_PTR_LOCK:
+    case ACTION_TYPE_CTRL_SET:
+    case ACTION_TYPE_CTRL_LOCK:
+    case ACTION_TYPE_SWITCH_VT:
+    case ACTION_TYPE_TERMINATE:
+        return true;
+    default:
+        return false;
+    }
+}
+
+static void
+xkb_filter_mod_latch_new(struct xkb_state *state, struct xkb_filter *filter)
+{
+    filter->priv = LATCH_KEY_DOWN;
+    state->set_mods = filter->action.mods.mods.mask;
+}
+
+static bool
+xkb_filter_mod_latch_func(struct xkb_state *state,
+                          struct xkb_filter *filter,
+                          const struct xkb_key *key,
                           enum xkb_key_direction direction)
 {
     enum xkb_key_latch_state latch = filter->priv;
@@ -342,57 +447,58 @@ xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
          * keypress, then either break the latch if any random key is pressed,
          * or promote it to a lock or plain base set if it's the same
          * modifier. */
-        union xkb_action *action = xkb_key_get_action(filter->state, kc);
-        if (action->type == XkbSA_LatchMods &&
+        const union xkb_action *action = xkb_key_get_action(state, key);
+        if (action->type == ACTION_TYPE_MOD_LATCH &&
             action->mods.flags == filter->action.mods.flags &&
             action->mods.mods.mask == filter->action.mods.mods.mask) {
             filter->action = *action;
-            if (filter->action.mods.flags & XkbSA_LatchToLock) {
-                filter->action.type = XkbSA_LockMods;
+            if (filter->action.mods.flags & ACTION_LATCH_TO_LOCK) {
+                filter->action.type = ACTION_TYPE_MOD_LOCK;
                 filter->func = xkb_filter_mod_lock_func;
-                filter->state->locked_mods |= filter->action.mods.mods.mask;
+                state->components.locked_mods |= filter->action.mods.mods.mask;
             }
             else {
-                filter->action.type = XkbSA_SetMods;
+                filter->action.type = ACTION_TYPE_MOD_SET;
                 filter->func = xkb_filter_mod_set_func;
-                filter->state->set_mods = filter->action.mods.mods.mask;
+                state->set_mods = filter->action.mods.mods.mask;
             }
-            filter->kc = kc;
-            filter->state->latched_mods &= ~filter->action.mods.mods.mask;
+            filter->key = key;
+            state->components.latched_mods &= ~filter->action.mods.mods.mask;
             /* XXX beep beep! */
-            return 0;
+            return XKB_FILTER_CONSUME;
         }
-        else if (((1 << action->type) & XkbSA_BreakLatch)) {
+        else if (xkb_action_breaks_latch(action)) {
             /* XXX: This may be totally broken, we might need to break the
              *      latch in the next run after this press? */
-            filter->state->latched_mods &= ~filter->action.mods.mods.mask;
+            state->components.latched_mods &= ~filter->action.mods.mods.mask;
             filter->func = NULL;
-            return 1;
+            return XKB_FILTER_CONTINUE;
         }
     }
-    else if (direction == XKB_KEY_UP && kc == filter->kc) {
+    else if (direction == XKB_KEY_UP && key == filter->key) {
         /* Our key got released.  If we've set it to clear locks, and we
          * currently have the same modifiers locked, then release them and
          * don't actually latch.  Else we've actually hit the latching
          * stage, so set PENDING and move our modifier from base to
          * latched. */
         if (latch == NO_LATCH ||
-            ((filter->action.mods.flags & XkbSA_ClearLocks) &&
-             (filter->state->locked_mods & filter->action.mods.mods.mask) ==
+            ((filter->action.mods.flags & ACTION_LOCK_CLEAR) &&
+             (state->components.locked_mods & filter->action.mods.mods.mask) ==
              filter->action.mods.mods.mask)) {
             /* XXX: We might be a bit overenthusiastic about clearing
              *      mods other filters have set here? */
             if (latch == LATCH_PENDING)
-                filter->state->latched_mods &= ~filter->action.mods.mods.mask;
+                state->components.latched_mods &=
+                    ~filter->action.mods.mods.mask;
             else
-                filter->state->clear_mods = filter->action.mods.mods.mask;
-            filter->state->locked_mods &= ~filter->action.mods.mods.mask;
+                state->clear_mods = filter->action.mods.mods.mask;
+            state->components.locked_mods &= ~filter->action.mods.mods.mask;
             filter->func = NULL;
         }
         else {
             latch = LATCH_PENDING;
-            filter->state->clear_mods = filter->action.mods.mods.mask;
-            filter->state->latched_mods |= filter->action.mods.mods.mask;
+            state->clear_mods = filter->action.mods.mods.mask;
+            state->components.latched_mods |= filter->action.mods.mods.mask;
             /* XXX beep beep! */
         }
     }
@@ -406,27 +512,25 @@ xkb_filter_mod_latch_func(struct xkb_filter *filter, xkb_keycode_t kc,
 
     filter->priv = latch;
 
-    return 1;
+    return XKB_FILTER_CONTINUE;
 }
 
-static int
-xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
-                         union xkb_action *action)
-{
-    struct xkb_filter *filter = xkb_filter_new(state);
-    enum xkb_key_latch_state latch = LATCH_KEY_DOWN;
-
-    if (!filter) /* WSGO */
-        return -1;
-    filter->kc = kc;
-    filter->priv = latch;
-    filter->func = xkb_filter_mod_latch_func;
-    filter->action = *action;
-
-    filter->state->set_mods = action->mods.mods.mask;
-
-    return 1;
-}
+static const struct {
+    void (*new)(struct xkb_state *state, struct xkb_filter *filter);
+    bool (*func)(struct xkb_state *state, struct xkb_filter *filter,
+                 const struct xkb_key *key, enum xkb_key_direction direction);
+} filter_action_funcs[_ACTION_TYPE_NUM_ENTRIES] = {
+    [ACTION_TYPE_MOD_SET]    = { xkb_filter_mod_set_new,
+                                 xkb_filter_mod_set_func },
+    [ACTION_TYPE_MOD_LATCH]  = { xkb_filter_mod_latch_new,
+                                 xkb_filter_mod_latch_func },
+    [ACTION_TYPE_MOD_LOCK]   = { xkb_filter_mod_lock_new,
+                                 xkb_filter_mod_lock_func },
+    [ACTION_TYPE_GROUP_SET]  = { xkb_filter_group_set_new,
+                                 xkb_filter_group_set_func },
+    [ACTION_TYPE_GROUP_LOCK] = { xkb_filter_group_lock_new,
+                                 xkb_filter_group_lock_func },
+};
 
 /**
  * Applies any relevant filters to the key, first from the list of filters
@@ -434,49 +538,47 @@ xkb_filter_mod_latch_new(struct xkb_state *state, xkb_keycode_t kc,
  * apply a new filter from the key action.
  */
 static void
-xkb_filter_apply_all(struct xkb_state *state, xkb_keycode_t kc,
+xkb_filter_apply_all(struct xkb_state *state,
+                     const struct xkb_key *key,
                      enum xkb_key_direction direction)
 {
     struct xkb_filter *filter;
-    union xkb_action *act = NULL;
-    int send = 1;
+    const union xkb_action *action;
+    bool consumed;
 
     /* First run through all the currently active filters and see if any of
-     * them have claimed this event. */
+     * them have consumed this event. */
+    consumed = false;
     darray_foreach(filter, state->filters) {
         if (!filter->func)
             continue;
-        send &= filter->func(filter, kc, direction);
+
+        if (filter->func(state, filter, key, direction) == XKB_FILTER_CONSUME)
+            consumed = true;
     }
+    if (consumed || direction == XKB_KEY_UP)
+        return;
+
+    action = xkb_key_get_action(state, key);
 
-    if (!send || direction == XKB_KEY_UP)
+    /*
+     * It's possible for the keymap to set action->type explicitly, like so:
+     *     interpret XF86_Next_VMode {
+     *         action = Private(type=0x86, data="+VMode");
+     *     };
+     * We don't handle those.
+     */
+    if (action->type >= _ACTION_TYPE_NUM_ENTRIES)
         return;
 
-    act = xkb_key_get_action(state, kc);
-    switch (act->type) {
-    case XkbSA_SetMods:
-        send = xkb_filter_mod_set_new(state, kc, act);
-        break;
-    case XkbSA_LatchMods:
-        send = xkb_filter_mod_latch_new(state, kc, act);
-        break;
-    case XkbSA_LockMods:
-        send = xkb_filter_mod_lock_new(state, kc, act);
-        break;
-    case XkbSA_SetGroup:
-        send = xkb_filter_group_set_new(state, kc, act);
-        break;
-#if 0
-    case XkbSA_LatchGroup:
-        send = xkb_filter_mod_latch_new(state, key, act);
-        break;
-#endif
-    case XkbSA_LockGroup:
-        send = xkb_filter_group_lock_new(state, kc, act);
-        break;
-    }
+    if (!filter_action_funcs[action->type].new)
+        return;
 
-    return;
+    filter = xkb_filter_new(state);
+    filter->key = key;
+    filter->func = filter_action_funcs[action->type].func;
+    filter->action = *action;
+    filter_action_funcs[action->type].new(state, filter);
 }
 
 XKB_EXPORT struct xkb_state *
@@ -484,15 +586,12 @@ xkb_state_new(struct xkb_keymap *keymap)
 {
     struct xkb_state *ret;
 
-    if (!keymap)
-        return NULL;
-
     ret = calloc(sizeof(*ret), 1);
     if (!ret)
         return NULL;
 
     ret->refcnt = 1;
-    ret->keymap = xkb_map_ref(keymap);
+    ret->keymap = xkb_keymap_ref(keymap);
 
     return ret;
 }
@@ -507,18 +606,16 @@ xkb_state_ref(struct xkb_state *state)
 XKB_EXPORT void
 xkb_state_unref(struct xkb_state *state)
 {
-    state->refcnt--;
-    assert(state->refcnt >= 0);
-    if (state->refcnt > 0)
+    if (!state || --state->refcnt > 0)
         return;
 
-    xkb_map_unref(state->keymap);
+    xkb_keymap_unref(state->keymap);
     darray_free(state->filters);
     free(state);
 }
 
 XKB_EXPORT struct xkb_keymap *
-xkb_state_get_map(struct xkb_state *state)
+xkb_state_get_keymap(struct xkb_state *state)
 {
     return state->keymap;
 }
@@ -529,45 +626,50 @@ xkb_state_get_map(struct xkb_state *state)
 static void
 xkb_state_led_update_all(struct xkb_state *state)
 {
-    xkb_led_index_t led;
-
-    state->leds = 0;
+    xkb_led_index_t idx;
+    const struct xkb_led *led;
+
+    state->components.leds = 0;
+
+    xkb_leds_enumerate(idx, led, state->keymap) {
+        xkb_mod_mask_t mod_mask = 0;
+        xkb_layout_mask_t group_mask = 0;
+
+        if (led->which_mods != 0 && led->mods.mask != 0) {
+            if (led->which_mods & XKB_STATE_MODS_EFFECTIVE)
+                mod_mask |= state->components.mods;
+            if (led->which_mods & XKB_STATE_MODS_DEPRESSED)
+                mod_mask |= state->components.base_mods;
+            if (led->which_mods & XKB_STATE_MODS_LATCHED)
+                mod_mask |= state->components.latched_mods;
+            if (led->which_mods & XKB_STATE_MODS_LOCKED)
+                mod_mask |= state->components.locked_mods;
+
+            if (led->mods.mask & mod_mask) {
+                state->components.leds |= (1u << idx);
+                continue;
+            }
+        }
 
-    for (led = 0; led < XkbNumIndicators; led++) {
-        struct xkb_indicator_map *map = &state->keymap->indicators[led];
-        uint32_t mod_mask = 0;
-        uint32_t group_mask = 0;
+        if (led->which_groups != 0 && led->groups != 0) {
+            if (led->which_groups & XKB_STATE_LAYOUT_EFFECTIVE)
+                group_mask |= (1u << state->components.group);
+            if (led->which_groups & XKB_STATE_LAYOUT_DEPRESSED)
+                group_mask |= (1u << state->components.base_group);
+            if (led->which_groups & XKB_STATE_LAYOUT_LATCHED)
+                group_mask |= (1u << state->components.latched_group);
+            if (led->which_groups & XKB_STATE_LAYOUT_LOCKED)
+                group_mask |= (1u << state->components.locked_group);
+
+            if (led->groups & group_mask) {
+                state->components.leds |= (1u << idx);
+                continue;
+            }
+        }
 
-        if (!map->which_mods && !map->which_groups && !map->ctrls)
+        if (led->ctrls & state->keymap->enabled_ctrls) {
+            state->components.leds |= (1u << idx);
             continue;
-
-        if (map->which_mods) {
-            if (map->which_mods & XkbIM_UseBase)
-                mod_mask |= state->base_mods;
-            if (map->which_mods & XkbIM_UseLatched)
-                mod_mask |= state->latched_mods;
-            if (map->which_mods & XkbIM_UseLocked)
-                mod_mask |= state->locked_mods;
-            if (map->which_mods & XkbIM_UseEffective)
-                mod_mask |= state->mods;
-            if ((map->mods.mask & mod_mask))
-                state->leds |= (1 << led);
-        }
-        else if (map->which_groups) {
-            if (map->which_mods & XkbIM_UseBase)
-                group_mask |= (1 << state->base_group);
-            if (map->which_mods & XkbIM_UseLatched)
-                group_mask |= (1 << state->latched_group);
-            if (map->which_mods & XkbIM_UseLocked)
-                group_mask |= (1 << state->locked_group);
-            if (map->which_mods & XkbIM_UseEffective)
-                group_mask |= (1 << state->group);
-            if ((map->groups & group_mask))
-                state->leds |= (1 << led);
-        }
-        else if (map->ctrls) {
-            if ((map->ctrls & state->keymap->enabled_ctrls))
-                state->leds |= (1 << led);
         }
     }
 }
@@ -579,36 +681,86 @@ xkb_state_led_update_all(struct xkb_state *state)
 static void
 xkb_state_update_derived(struct xkb_state *state)
 {
-    state->mods =
-        (state->base_mods | state->latched_mods | state->locked_mods);
-    /* FIXME: Clamp/wrap locked_group */
-    state->group = state->locked_group + state->base_group +
-                   state->latched_group;
-    /* FIXME: Clamp/wrap effective group */
+    xkb_layout_index_t wrapped;
+
+    state->components.mods = (state->components.base_mods |
+                              state->components.latched_mods |
+                              state->components.locked_mods);
+
+    /* TODO: Use groups_wrap control instead of always RANGE_WRAP. */
+
+    wrapped = XkbWrapGroupIntoRange(state->components.locked_group,
+                                    state->keymap->num_groups,
+                                    RANGE_WRAP, 0);
+    state->components.locked_group =
+        (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
+
+    wrapped = XkbWrapGroupIntoRange(state->components.base_group +
+                                    state->components.latched_group +
+                                    state->components.locked_group,
+                                    state->keymap->num_groups,
+                                    RANGE_WRAP, 0);
+    state->components.group =
+        (wrapped == XKB_LAYOUT_INVALID ? 0 : wrapped);
 
     xkb_state_led_update_all(state);
 }
 
+static enum xkb_state_component
+get_state_component_changes(const struct state_components *a,
+                            const struct state_components *b)
+{
+    xkb_mod_mask_t mask = 0;
+
+    if (a->group != b->group)
+        mask |= XKB_STATE_LAYOUT_EFFECTIVE;
+    if (a->base_group != b->base_group)
+        mask |= XKB_STATE_LAYOUT_DEPRESSED;
+    if (a->latched_group != b->latched_group)
+        mask |= XKB_STATE_LAYOUT_LATCHED;
+    if (a->locked_group != b->locked_group)
+        mask |= XKB_STATE_LAYOUT_LOCKED;
+    if (a->mods != b->mods)
+        mask |= XKB_STATE_MODS_EFFECTIVE;
+    if (a->base_mods != b->base_mods)
+        mask |= XKB_STATE_MODS_DEPRESSED;
+    if (a->latched_mods != b->latched_mods)
+        mask |= XKB_STATE_MODS_LATCHED;
+    if (a->locked_mods != b->locked_mods)
+        mask |= XKB_STATE_MODS_LOCKED;
+    if (a->leds != b->leds)
+        mask |= XKB_STATE_LEDS;
+
+    return mask;
+}
+
 /**
  * Given a particular key event, updates the state structure to reflect the
  * new modifiers.
  */
-XKB_EXPORT void
+XKB_EXPORT enum xkb_state_component
 xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
                      enum xkb_key_direction direction)
 {
     xkb_mod_index_t i;
     xkb_mod_mask_t bit;
+    struct state_components prev_components;
+    const struct xkb_key *key = XkbKey(state->keymap, kc);
+
+    if (!key)
+        return 0;
+
+    prev_components = state->components;
 
     state->set_mods = 0;
     state->clear_mods = 0;
 
-    xkb_filter_apply_all(state, kc, direction);
+    xkb_filter_apply_all(state, key, direction);
 
     for (i = 0, bit = 1; state->set_mods; i++, bit <<= 1) {
         if (state->set_mods & bit) {
             state->mod_key_count[i]++;
-            state->base_mods |= bit;
+            state->components.base_mods |= bit;
             state->set_mods &= ~bit;
         }
     }
@@ -617,7 +769,7 @@ xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
         if (state->clear_mods & bit) {
             state->mod_key_count[i]--;
             if (state->mod_key_count[i] <= 0) {
-                state->base_mods &= ~bit;
+                state->components.base_mods &= ~bit;
                 state->mod_key_count[i] = 0;
             }
             state->clear_mods &= ~bit;
@@ -625,6 +777,8 @@ xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
     }
 
     xkb_state_update_derived(state);
+
+    return get_state_component_changes(&prev_components, &state->components);
 }
 
 /**
@@ -634,35 +788,274 @@ xkb_state_update_key(struct xkb_state *state, xkb_keycode_t kc,
  * lossy, and should only be used to update a slave state mirroring the
  * master, e.g. in a client/server window system.
  */
-XKB_EXPORT void
+XKB_EXPORT enum xkb_state_component
 xkb_state_update_mask(struct xkb_state *state,
                       xkb_mod_mask_t base_mods,
                       xkb_mod_mask_t latched_mods,
                       xkb_mod_mask_t locked_mods,
-                      xkb_group_index_t base_group,
-                      xkb_group_index_t latched_group,
-                      xkb_group_index_t locked_group)
-{
-    xkb_mod_mask_t mod;
-
-    state->base_mods = 0;
-    state->latched_mods = 0;
-    state->locked_mods = 0;
-    for (mod = 0; mod < xkb_map_num_mods(state->keymap); mod++) {
-        xkb_mod_mask_t idx = (1 << mod);
-        if (base_mods & idx)
-            state->base_mods |= idx;
-        if (latched_mods & idx)
-            state->latched_mods |= idx;
-        if (locked_mods & idx)
-            state->locked_mods |= idx;
+                      xkb_layout_index_t base_group,
+                      xkb_layout_index_t latched_group,
+                      xkb_layout_index_t locked_group)
+{
+    struct state_components prev_components;
+    xkb_mod_mask_t mask;
+
+    prev_components = state->components;
+
+    /* Only include modifiers which exist in the keymap. */
+    mask = (xkb_mod_mask_t) ((1ull << xkb_keymap_num_mods(state->keymap)) - 1u);
+
+    state->components.base_mods = base_mods & mask;
+    state->components.latched_mods = latched_mods & mask;
+    state->components.locked_mods = locked_mods & mask;
+
+    /* Make sure the mods are fully resolved - since we get arbitrary
+     * input, they might not be.
+     *
+     * It might seem more reasonable to do this only for components.mods
+     * in xkb_state_update_derived(), rather than for each component
+     * seperately.  That would allow to distinguish between "really"
+     * depressed mods (would be in MODS_DEPRESSED) and indirectly
+     * depressed to to a mapping (would only be in MODS_EFFECTIVE).
+     * However, the traditional behavior of xkb_state_update_key() is that
+     * if a vmod is depressed, its mappings are depressed with it; so we're
+     * expected to do the same here.  Also, LEDs (usually) look if a real
+     * mod is locked, not just effective; otherwise it won't be lit.
+     *
+     * We OR here because mod_mask_get_effective() drops vmods. */
+    state->components.base_mods |=
+        mod_mask_get_effective(state->keymap, state->components.base_mods);
+    state->components.latched_mods |=
+        mod_mask_get_effective(state->keymap, state->components.latched_mods);
+    state->components.locked_mods |=
+        mod_mask_get_effective(state->keymap, state->components.locked_mods);
+
+    state->components.base_group = base_group;
+    state->components.latched_group = latched_group;
+    state->components.locked_group = locked_group;
+
+    xkb_state_update_derived(state);
+
+    return get_state_component_changes(&prev_components, &state->components);
+}
+
+/**
+ * Provides the symbols to use for the given key and state.  Returns the
+ * number of symbols pointed to in syms_out.
+ */
+XKB_EXPORT int
+xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t kc,
+                       const xkb_keysym_t **syms_out)
+{
+    xkb_layout_index_t layout;
+    xkb_level_index_t level;
+
+    layout = xkb_state_key_get_layout(state, kc);
+    if (layout == XKB_LAYOUT_INVALID)
+        goto err;
+
+    level = xkb_state_key_get_level(state, kc, layout);
+    if (level == XKB_LEVEL_INVALID)
+        goto err;
+
+    return xkb_keymap_key_get_syms_by_level(state->keymap, kc, layout, level,
+                                            syms_out);
+
+err:
+    *syms_out = NULL;
+    return 0;
+}
+
+/*
+ * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
+ */
+static bool
+should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
+{
+    xkb_mod_index_t caps =
+        xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CAPS);
+
+    return
+        xkb_state_mod_index_is_active(state, caps, XKB_STATE_MODS_EFFECTIVE) > 0 &&
+        xkb_state_mod_index_is_consumed(state, kc, caps) == 0;
+}
+
+/*
+ * https://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
+ */
+static bool
+should_do_ctrl_transformation(struct xkb_state *state, xkb_keycode_t kc)
+{
+    xkb_mod_index_t ctrl =
+        xkb_keymap_mod_get_index(state->keymap, XKB_MOD_NAME_CTRL);
+
+    return
+        xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_MODS_EFFECTIVE) > 0 &&
+        xkb_state_mod_index_is_consumed(state, kc, ctrl) == 0;
+}
+
+/* Verbatim from libX11:src/xkb/XKBBind.c */
+static char
+XkbToControl(char ch)
+{
+    char c = ch;
+
+    if ((c >= '@' && c < '\177') || c == ' ')
+        c &= 0x1F;
+    else if (c == '2')
+        c = '\000';
+    else if (c >= '3' && c <= '7')
+        c -= ('3' - '\033');
+    else if (c == '8')
+        c = '\177';
+    else if (c == '/')
+        c = '_' & 0x1F;
+    return c;
+}
+
+/**
+ * Provides either exactly one symbol, or XKB_KEY_NoSymbol.
+ */
+XKB_EXPORT xkb_keysym_t
+xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t kc)
+{
+    const xkb_keysym_t *syms;
+    xkb_keysym_t sym;
+    int num_syms;
+
+    num_syms = xkb_state_key_get_syms(state, kc, &syms);
+    if (num_syms != 1)
+        return XKB_KEY_NoSymbol;
+
+    sym = syms[0];
+
+    if (should_do_caps_transformation(state, kc))
+        sym = xkb_keysym_to_upper(sym);
+
+    return sym;
+}
+
+/*
+ * The caps and ctrl transformations require some special handling,
+ * so we cannot simply use xkb_state_get_one_sym() for them.
+ * In particular, if Control is set, we must try very hard to find
+ * some layout in which the keysym is ASCII and thus can be (maybe)
+ * converted to a control character. libX11 allows to disable this
+ * behavior with the XkbLC_ControlFallback (see XkbSetXlibControls(3)),
+ * but it is enabled by default, yippee.
+ */
+static xkb_keysym_t
+get_one_sym_for_string(struct xkb_state *state, xkb_keycode_t kc)
+{
+    xkb_level_index_t level;
+    xkb_layout_index_t layout, num_layouts;
+    const xkb_keysym_t *syms;
+    int nsyms;
+    xkb_keysym_t sym;
+
+    layout = xkb_state_key_get_layout(state, kc);
+    num_layouts = xkb_keymap_num_layouts_for_key(state->keymap, kc);
+    level = xkb_state_key_get_level(state, kc, layout);
+    if (layout == XKB_LAYOUT_INVALID || num_layouts == 0 ||
+        level == XKB_LEVEL_INVALID)
+        return XKB_KEY_NoSymbol;
+
+    nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
+                                             layout, level, &syms);
+    if (nsyms != 1)
+        return XKB_KEY_NoSymbol;
+    sym = syms[0];
+
+    if (should_do_ctrl_transformation(state, kc) && sym > 127u) {
+        for (xkb_layout_index_t i = 0; i < num_layouts; i++) {
+            level = xkb_state_key_get_level(state, kc, i);
+            if (level == XKB_LEVEL_INVALID)
+                continue;
+
+            nsyms = xkb_keymap_key_get_syms_by_level(state->keymap, kc,
+                                                     i, level, &syms);
+            if (nsyms == 1 && syms[0] <= 127u) {
+                sym = syms[0];
+                break;
+            }
+        }
     }
 
-    state->base_group = base_group;
-    state->latched_group = latched_group;
-    state->locked_group = locked_group;
+    if (should_do_caps_transformation(state, kc)) {
+        sym = xkb_keysym_to_upper(sym);
+    }
 
-    xkb_state_update_derived(state);
+    return sym;
+}
+
+XKB_EXPORT int
+xkb_state_key_get_utf8(struct xkb_state *state, xkb_keycode_t kc,
+                       char *buffer, size_t size)
+{
+    xkb_keysym_t sym;
+    const xkb_keysym_t *syms;
+    int nsyms;
+    int offset;
+    char tmp[7];
+
+    sym = get_one_sym_for_string(state, kc);
+    if (sym != XKB_KEY_NoSymbol) {
+        nsyms = 1; syms = &sym;
+    }
+    else {
+        nsyms = xkb_state_key_get_syms(state, kc, &syms);
+    }
+
+    /* Make sure not to truncate in the middle of a UTF-8 sequence. */
+    offset = 0;
+    for (int i = 0; i < nsyms; i++) {
+        int ret = xkb_keysym_to_utf8(syms[i], tmp, sizeof(tmp));
+        if (ret <= 0)
+            goto err_bad;
+
+        ret--;
+        if ((size_t) (offset + ret) <= size)
+            memcpy(buffer + offset, tmp, ret);
+        offset += ret;
+    }
+
+    if ((size_t) offset >= size)
+        goto err_trunc;
+    buffer[offset] = '\0';
+
+    if (!is_valid_utf8(buffer, offset))
+        goto err_bad;
+
+    if (offset == 1 && (unsigned int) buffer[0] <= 127u &&
+        should_do_ctrl_transformation(state, kc))
+        buffer[0] = XkbToControl(buffer[0]);
+
+    return offset;
+
+err_trunc:
+    if (size > 0)
+        buffer[size - 1] = '\0';
+    return offset;
+
+err_bad:
+    if (size > 0)
+        buffer[0] = '\0';
+    return 0;
+}
+
+XKB_EXPORT uint32_t
+xkb_state_key_get_utf32(struct xkb_state *state, xkb_keycode_t kc)
+{
+    xkb_keysym_t sym;
+    uint32_t cp;
+
+    sym = get_one_sym_for_string(state, kc);
+    cp = xkb_keysym_to_utf32(sym);
+
+    if (cp <= 127u && should_do_ctrl_transformation(state, kc))
+        cp = (uint32_t) XkbToControl((char) cp);
+
+    return cp;
 }
 
 /**
@@ -675,15 +1068,15 @@ xkb_state_serialize_mods(struct xkb_state *state,
 {
     xkb_mod_mask_t ret = 0;
 
-    if (type == XKB_STATE_EFFECTIVE)
-        return state->mods;
+    if (type & XKB_STATE_MODS_EFFECTIVE)
+        return state->components.mods;
 
-    if (type & XKB_STATE_DEPRESSED)
-        ret |= state->base_mods;
-    if (type & XKB_STATE_LATCHED)
-        ret |= state->latched_mods;
-    if (type & XKB_STATE_LOCKED)
-        ret |= state->locked_mods;
+    if (type & XKB_STATE_MODS_DEPRESSED)
+        ret |= state->components.base_mods;
+    if (type & XKB_STATE_MODS_LATCHED)
+        ret |= state->components.latched_mods;
+    if (type & XKB_STATE_MODS_LOCKED)
+        ret |= state->components.locked_mods;
 
     return ret;
 }
@@ -692,26 +1085,48 @@ xkb_state_serialize_mods(struct xkb_state *state,
  * Serialises the requested group state, with all the same disclaimers as
  * in xkb_state_update_mask.
  */
-XKB_EXPORT xkb_group_index_t
-xkb_state_serialize_group(struct xkb_state *state,
-                          enum xkb_state_component type)
+XKB_EXPORT xkb_layout_index_t
+xkb_state_serialize_layout(struct xkb_state *state,
+                           enum xkb_state_component type)
 {
-    xkb_group_index_t ret = 0;
+    xkb_layout_index_t ret = 0;
 
-    if (type == XKB_STATE_EFFECTIVE)
-        return state->group;
+    if (type & XKB_STATE_LAYOUT_EFFECTIVE)
+        return state->components.group;
 
-    if (type & XKB_STATE_DEPRESSED)
-        ret += state->base_group;
-    if (type & XKB_STATE_LATCHED)
-        ret += state->latched_group;
-    if (type & XKB_STATE_LOCKED)
-        ret += state->locked_group;
+    if (type & XKB_STATE_LAYOUT_DEPRESSED)
+        ret += state->components.base_group;
+    if (type & XKB_STATE_LAYOUT_LATCHED)
+        ret += state->components.latched_group;
+    if (type & XKB_STATE_LAYOUT_LOCKED)
+        ret += state->components.locked_group;
 
     return ret;
 }
 
 /**
+ * Gets a modifier mask and returns the resolved effective mask; this
+ * is needed because some modifiers can also map to other modifiers, e.g.
+ * the "NumLock" modifier usually also sets the "Mod2" modifier.
+ */
+xkb_mod_mask_t
+mod_mask_get_effective(struct xkb_keymap *keymap, xkb_mod_mask_t mods)
+{
+    const struct xkb_mod *mod;
+    xkb_mod_index_t i;
+    xkb_mod_mask_t mask;
+
+    /* The effective mask is only real mods for now. */
+    mask = mods & MOD_REAL_MASK_ALL;
+
+    xkb_mods_enumerate(i, mod, &keymap->mods)
+        if (mods & (1u << i))
+            mask |= mod->mapping;
+
+    return mask;
+}
+
+/**
  * Returns 1 if the given modifier is active with the specified type(s), 0 if
  * not, or -1 if the modifier is invalid.
  */
@@ -720,40 +1135,31 @@ xkb_state_mod_index_is_active(struct xkb_state *state,
                               xkb_mod_index_t idx,
                               enum xkb_state_component type)
 {
-    int ret = 0;
-
-    if (idx >= xkb_map_num_mods(state->keymap))
+    if (idx >= xkb_keymap_num_mods(state->keymap))
         return -1;
 
-    if (type & XKB_STATE_DEPRESSED)
-        ret |= (state->base_mods & (1 << idx));
-    if (type & XKB_STATE_LATCHED)
-        ret |= (state->latched_mods & (1 << idx));
-    if (type & XKB_STATE_LOCKED)
-        ret |= (state->locked_mods & (1 << idx));
-
-    return ret;
+    return !!(xkb_state_serialize_mods(state, type) & (1u << idx));
 }
 
 /**
  * Helper function for xkb_state_mod_indices_are_active and
  * xkb_state_mod_names_are_active.
  */
-static int
-match_mod_masks(struct xkb_state *state, enum xkb_state_match match,
-                uint32_t wanted)
+static bool
+match_mod_masks(struct xkb_state *state,
+                enum xkb_state_component type,
+                enum xkb_state_match match,
+                xkb_mod_mask_t wanted)
 {
-    uint32_t active = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
+    xkb_mod_mask_t active = xkb_state_serialize_mods(state, type);
 
     if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
-        return 0;
+        return false;
 
     if (match & XKB_STATE_MATCH_ANY)
-        return !!(active & wanted);
-    else
-        return (active & wanted) == wanted;
+        return active & wanted;
 
-    return 0;
+    return (active & wanted) == wanted;
 }
 
 /**
@@ -767,26 +1173,27 @@ xkb_state_mod_indices_are_active(struct xkb_state *state,
                                  ...)
 {
     va_list ap;
-    xkb_mod_index_t idx = 0;
-    uint32_t wanted = 0;
+    xkb_mod_mask_t wanted = 0;
     int ret = 0;
+    xkb_mod_index_t num_mods = xkb_keymap_num_mods(state->keymap);
 
     va_start(ap, match);
     while (1) {
-        idx = va_arg(ap, xkb_mod_index_t);
-        if (idx == XKB_MOD_INVALID ||
-            idx >= xkb_map_num_mods(state->keymap)) {
+        xkb_mod_index_t idx = va_arg(ap, xkb_mod_index_t);
+        if (idx == XKB_MOD_INVALID)
+            break;
+        if (idx >= num_mods) {
             ret = -1;
             break;
         }
-        wanted |= (1 << idx);
+        wanted |= (1u << idx);
     }
     va_end(ap);
 
     if (ret == -1)
         return ret;
 
-    return match_mod_masks(state, match, wanted);
+    return match_mod_masks(state, type, match, wanted);
 }
 
 /**
@@ -797,7 +1204,7 @@ XKB_EXPORT int
 xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
                              enum xkb_state_component type)
 {
-    xkb_mod_index_t idx = xkb_map_mod_get_index(state->keymap, name);
+    xkb_mod_index_t idx = xkb_keymap_mod_get_index(state->keymap, name);
 
     if (idx == XKB_MOD_INVALID)
         return -1;
@@ -809,36 +1216,35 @@ xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
  * Returns 1 if the modifiers are active with the specified type(s), 0 if
  * not, or -1 if any of the modifiers are invalid.
  */
-XKB_EXPORT int
+XKB_EXPORT ATTR_NULL_SENTINEL int
 xkb_state_mod_names_are_active(struct xkb_state *state,
                                enum xkb_state_component type,
                                enum xkb_state_match match,
                                ...)
 {
     va_list ap;
-    xkb_mod_index_t idx = 0;
-    const char *str;
-    uint32_t wanted = 0;
+    xkb_mod_mask_t wanted = 0;
     int ret = 0;
 
     va_start(ap, match);
     while (1) {
-        str = va_arg(ap, const char *);
+        xkb_mod_index_t idx;
+        const char *str = va_arg(ap, const char *);
         if (str == NULL)
             break;
-        idx = xkb_map_mod_get_index(state->keymap, str);
+        idx = xkb_keymap_mod_get_index(state->keymap, str);
         if (idx == XKB_MOD_INVALID) {
             ret = -1;
             break;
         }
-        wanted |= (1 << idx);
+        wanted |= (1u << idx);
     }
     va_end(ap);
 
     if (ret == -1)
         return ret;
 
-    return match_mod_masks(state, match, wanted);
+    return match_mod_masks(state, type, match, wanted);
 }
 
 /**
@@ -846,21 +1252,23 @@ xkb_state_mod_names_are_active(struct xkb_state *state,
  * not, or -1 if the group is invalid.
  */
 XKB_EXPORT int
-xkb_state_group_index_is_active(struct xkb_state *state,
-                                xkb_group_index_t idx,
+xkb_state_layout_index_is_active(struct xkb_state *state,
+                                xkb_layout_index_t idx,
                                 enum xkb_state_component type)
 {
     int ret = 0;
 
-    if (idx >= xkb_map_num_groups(state->keymap))
+    if (idx >= state->keymap->num_groups)
         return -1;
 
-    if (type & XKB_STATE_DEPRESSED)
-        ret |= (state->base_group == idx);
-    if (type & XKB_STATE_LATCHED)
-        ret |= (state->latched_group == idx);
-    if (type & XKB_STATE_LOCKED)
-        ret |= (state->locked_group == idx);
+    if (type & XKB_STATE_LAYOUT_EFFECTIVE)
+        ret |= (state->components.group == idx);
+    if (type & XKB_STATE_LAYOUT_DEPRESSED)
+        ret |= (state->components.base_group == (int32_t) idx);
+    if (type & XKB_STATE_LAYOUT_LATCHED)
+        ret |= (state->components.latched_group == (int32_t) idx);
+    if (type & XKB_STATE_LAYOUT_LOCKED)
+        ret |= (state->components.locked_group == (int32_t) idx);
 
     return ret;
 }
@@ -870,15 +1278,15 @@ xkb_state_group_index_is_active(struct xkb_state *state,
  * not, or -1 if the modifier is invalid.
  */
 XKB_EXPORT int
-xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
-                               enum xkb_state_component type)
+xkb_state_layout_name_is_active(struct xkb_state *state, const char *name,
+                                enum xkb_state_component type)
 {
-    xkb_group_index_t idx = xkb_map_group_get_index(state->keymap, name);
+    xkb_layout_index_t idx = xkb_keymap_layout_get_index(state->keymap, name);
 
-    if (idx == XKB_GROUP_INVALID)
+    if (idx == XKB_LAYOUT_INVALID)
         return -1;
 
-    return xkb_state_group_index_is_active(state, idx, type);
+    return xkb_state_layout_index_is_active(state, idx, type);
 }
 
 /**
@@ -887,10 +1295,11 @@ xkb_state_group_name_is_active(struct xkb_state *state, const char *name,
 XKB_EXPORT int
 xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
 {
-    if (idx >= xkb_map_num_leds(state->keymap))
+    if (idx >= state->keymap->num_leds ||
+        state->keymap->leds[idx].name == XKB_ATOM_NONE)
         return -1;
 
-    return !!(state->leds & (1 << idx));
+    return !!(state->components.leds & (1u << idx));
 }
 
 /**
@@ -899,10 +1308,130 @@ xkb_state_led_index_is_active(struct xkb_state *state, xkb_led_index_t idx)
 XKB_EXPORT int
 xkb_state_led_name_is_active(struct xkb_state *state, const char *name)
 {
-    xkb_led_index_t idx = xkb_map_led_get_index(state->keymap, name);
+    xkb_led_index_t idx = xkb_keymap_led_get_index(state->keymap, name);
 
     if (idx == XKB_LED_INVALID)
         return -1;
 
     return xkb_state_led_index_is_active(state, idx);
 }
+
+/**
+ * See:
+ * - XkbTranslateKeyCode(3), mod_rtrn return value, from libX11.
+ * - MyEnhancedXkbTranslateKeyCode(), a modification of the above, from GTK+.
+ */
+static xkb_mod_mask_t
+key_get_consumed(struct xkb_state *state, const struct xkb_key *key,
+                 enum xkb_consumed_mode mode)
+{
+    const struct xkb_key_type *type;
+    const struct xkb_key_type_entry *matching_entry;
+    xkb_mod_mask_t preserve = 0;
+    xkb_layout_index_t group;
+    xkb_mod_mask_t consumed = 0;
+
+    group = xkb_state_key_get_layout(state, key->keycode);
+    if (group == XKB_LAYOUT_INVALID)
+        return 0;
+
+    type = key->groups[group].type;
+
+    matching_entry = get_entry_for_key_state(state, key, group);
+    if (matching_entry)
+        preserve = matching_entry->preserve.mask;
+
+    switch (mode) {
+    case XKB_CONSUMED_MODE_XKB:
+        consumed = type->mods.mask;
+        break;
+
+    case XKB_CONSUMED_MODE_GTK: {
+        const struct xkb_key_type_entry *no_mods_entry;
+        xkb_level_index_t no_mods_leveli;
+        const struct xkb_level *no_mods_level, *level;
+
+        no_mods_entry = get_entry_for_mods(type, 0);
+        no_mods_leveli = no_mods_entry ? no_mods_entry->level : 0;
+        no_mods_level = &key->groups[group].levels[no_mods_leveli];
+
+        for (unsigned i = 0; i < type->num_entries; i++) {
+            const struct xkb_key_type_entry *entry = &type->entries[i];
+            if (!entry_is_active(entry))
+                continue;
+
+            level = &key->groups[group].levels[entry->level];
+            if (XkbLevelsSameSyms(level, no_mods_level))
+                continue;
+
+            if (entry == matching_entry || one_bit_set(entry->mods.mask))
+                consumed |= entry->mods.mask & ~entry->preserve.mask;
+        }
+        break;
+    }
+    }
+
+    return consumed & ~preserve;
+}
+
+XKB_EXPORT int
+xkb_state_mod_index_is_consumed2(struct xkb_state *state, xkb_keycode_t kc,
+                                 xkb_mod_index_t idx,
+                                 enum xkb_consumed_mode mode)
+{
+    const struct xkb_key *key = XkbKey(state->keymap, kc);
+
+    if (!key || idx >= xkb_keymap_num_mods(state->keymap))
+        return -1;
+
+    return !!((1u << idx) & key_get_consumed(state, key, mode));
+}
+
+XKB_EXPORT int
+xkb_state_mod_index_is_consumed(struct xkb_state *state, xkb_keycode_t kc,
+                                xkb_mod_index_t idx)
+{
+    return xkb_state_mod_index_is_consumed2(state, kc, idx,
+                                            XKB_CONSUMED_MODE_XKB);
+}
+
+XKB_EXPORT xkb_mod_mask_t
+xkb_state_mod_mask_remove_consumed(struct xkb_state *state, xkb_keycode_t kc,
+                                   xkb_mod_mask_t mask)
+{
+    const struct xkb_key *key = XkbKey(state->keymap, kc);
+
+    if (!key)
+        return 0;
+
+    return mask & ~key_get_consumed(state, key, XKB_CONSUMED_MODE_XKB);
+}
+
+XKB_EXPORT xkb_mod_mask_t
+xkb_state_key_get_consumed_mods2(struct xkb_state *state, xkb_keycode_t kc,
+                                 enum xkb_consumed_mode mode)
+{
+    const struct xkb_key *key;
+
+    switch (mode) {
+    case XKB_CONSUMED_MODE_XKB:
+    case XKB_CONSUMED_MODE_GTK:
+        break;
+    default:
+        log_err_func(state->keymap->ctx,
+                     "unrecognized consumed modifiers mode: %d\n", mode);
+        return 0;
+    }
+
+    key = XkbKey(state->keymap, kc);
+    if (!key)
+        return 0;
+
+    return key_get_consumed(state, key, mode);
+}
+
+XKB_EXPORT xkb_mod_mask_t
+xkb_state_key_get_consumed_mods(struct xkb_state *state, xkb_keycode_t kc)
+{
+    return xkb_state_key_get_consumed_mods2(state, kc, XKB_CONSUMED_MODE_XKB);
+}