x11/keymap: fix case with no actions
[platform/upstream/libxkbcommon.git] / src / state.c
index 0c86a34..b269e6d 100644 (file)
@@ -59,6 +59,8 @@
  *   - messages (very unlikely)
  */
 
+#include "config.h"
+
 #include "keymap.h"
 #include "keysym.h"
 #include "utf8.h"
@@ -116,17 +118,6 @@ struct xkb_state {
     struct xkb_keymap *keymap;
 };
 
-/*
- * If the virtual modifiers are not bound to anything, the entry
- * is not active and should be skipped. xserver does this with
- * cached entry->active field.
- */
-static bool
-entry_is_active(const struct xkb_key_type_entry *entry)
-{
-    return entry->mods.mods == 0 || entry->mods.mask != 0;
-}
-
 static const struct xkb_key_type_entry *
 get_entry_for_mods(const struct xkb_key_type *type, xkb_mod_mask_t mods)
 {
@@ -264,6 +255,21 @@ xkb_filter_new(struct xkb_state *state)
 
 /***====================================================================***/
 
+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)
 {
@@ -282,15 +288,15 @@ xkb_filter_group_set_func(struct xkb_state *state,
 {
     if (key != filter->key) {
         filter->action.group.flags &= ~ACTION_LOCK_CLEAR;
-        return true;
+        return XKB_FILTER_CONTINUE;
     }
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return false;
+        return XKB_FILTER_CONSUME;
     }
     else if (--filter->refcnt > 0) {
-        return false;
+        return XKB_FILTER_CONSUME;
     }
 
     state->components.base_group = filter->priv;
@@ -299,7 +305,7 @@ xkb_filter_group_set_func(struct xkb_state *state,
         state->components.locked_group = 0;
 
     filter->func = NULL;
-    return true;
+    return XKB_FILTER_CONTINUE;
 }
 
 static void
@@ -318,17 +324,17 @@ xkb_filter_group_lock_func(struct xkb_state *state,
                            enum xkb_key_direction direction)
 {
     if (key != filter->key)
-        return true;
+        return XKB_FILTER_CONTINUE;
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return false;
+        return XKB_FILTER_CONSUME;
     }
     if (--filter->refcnt > 0)
-        return false;
+        return XKB_FILTER_CONSUME;
 
     filter->func = NULL;
-    return true;
+    return XKB_FILTER_CONTINUE;
 }
 
 static void
@@ -345,15 +351,15 @@ xkb_filter_mod_set_func(struct xkb_state *state,
 {
     if (key != filter->key) {
         filter->action.mods.flags &= ~ACTION_LOCK_CLEAR;
-        return true;
+        return XKB_FILTER_CONTINUE;
     }
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return false;
+        return XKB_FILTER_CONSUME;
     }
     else if (--filter->refcnt > 0) {
-        return false;
+        return XKB_FILTER_CONSUME;
     }
 
     state->clear_mods = filter->action.mods.mods.mask;
@@ -361,7 +367,7 @@ xkb_filter_mod_set_func(struct xkb_state *state,
         state->components.locked_mods &= ~filter->action.mods.mods.mask;
 
     filter->func = NULL;
-    return true;
+    return XKB_FILTER_CONTINUE;
 }
 
 static void
@@ -381,21 +387,21 @@ xkb_filter_mod_lock_func(struct xkb_state *state,
                          enum xkb_key_direction direction)
 {
     if (key != filter->key)
-        return true;
+        return XKB_FILTER_CONTINUE;
 
     if (direction == XKB_KEY_DOWN) {
         filter->refcnt++;
-        return false;
+        return XKB_FILTER_CONSUME;
     }
     if (--filter->refcnt > 0)
-        return false;
+        return XKB_FILTER_CONSUME;
 
     state->clear_mods |= filter->action.mods.mods.mask;
     if (!(filter->action.mods.flags & ACTION_LOCK_NO_UNLOCK))
         state->components.locked_mods &= ~filter->priv;
 
     filter->func = NULL;
-    return true;
+    return XKB_FILTER_CONTINUE;
 }
 
 enum xkb_key_latch_state {
@@ -459,14 +465,14 @@ xkb_filter_mod_latch_func(struct xkb_state *state,
             filter->key = key;
             state->components.latched_mods &= ~filter->action.mods.mods.mask;
             /* XXX beep beep! */
-            return false;
+            return XKB_FILTER_CONSUME;
         }
         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? */
             state->components.latched_mods &= ~filter->action.mods.mods.mask;
             filter->func = NULL;
-            return true;
+            return XKB_FILTER_CONTINUE;
         }
     }
     else if (direction == XKB_KEY_UP && key == filter->key) {
@@ -506,7 +512,7 @@ xkb_filter_mod_latch_func(struct xkb_state *state,
 
     filter->priv = latch;
 
-    return true;
+    return XKB_FILTER_CONTINUE;
 }
 
 static const struct {
@@ -538,17 +544,19 @@ xkb_filter_apply_all(struct xkb_state *state,
 {
     struct xkb_filter *filter;
     const union xkb_action *action;
-    bool send = true;
+    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(state, filter, key, direction) && send;
-    }
 
-    if (!send || direction == XKB_KEY_UP)
+        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);
@@ -859,7 +867,7 @@ err:
 }
 
 /*
- * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Lock_Modifier
+ * 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)
@@ -873,7 +881,7 @@ should_do_caps_transformation(struct xkb_state *state, xkb_keycode_t kc)
 }
 
 /*
- * http://www.x.org/releases/current/doc/kbproto/xkbproto.html#Interpreting_the_Control_Modifier
+ * 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)
@@ -1356,7 +1364,7 @@ key_get_consumed(struct xkb_state *state, const struct xkb_key *key,
             if (XkbLevelsSameSyms(level, no_mods_level))
                 continue;
 
-            if (entry == matching_entry || my_popcount(entry->mods.mask) == 1)
+            if (entry == matching_entry || one_bit_set(entry->mods.mask))
                 consumed |= entry->mods.mask & ~entry->preserve.mask;
         }
         break;