[PATCH libxkbcommon] Add xkb_key_get_action API

Ran Benita ran234 at gmail.com
Fri Mar 16 10:04:15 PDT 2012


By which one can tell when to switch groups, etc.

Signed-off-by: Ran Benita <ran234 at gmail.com>
---
This would allow me to delete some duplicate code in the application
and would make me a happy camper for a while.
Wasn't sure about a couple of things:
- Should the by_level API be exposed (the _syms one seems to be non-static
  but not exported).
- Should the API allow for returning multiple actions per keypress (as the
  _syms API seems to suggest will be done for keysyms)?

 include/xkbcommon/xkbcommon.h |    4 ++++
 src/map.c                     |   35 +++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index 2092680..9bf5e0b 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -554,6 +554,10 @@ _X_EXPORT unsigned int
 xkb_key_get_syms(struct xkb_desc *xkb, struct xkb_state *state,
                  xkb_keycode_t key, xkb_keysym_t **syms_out);
 
+_X_EXPORT union xkb_action *
+xkb_key_get_action(struct xkb_desc *xkb, struct xkb_state *state,
+                   xkb_keycode_t key);
+
 _XFUNCPROTOEND
 
 #endif /* _XKBCOMMON_H_ */
diff --git a/src/map.c b/src/map.c
index c54f227..40d7e8f 100644
--- a/src/map.c
+++ b/src/map.c
@@ -164,3 +164,38 @@ err:
     *syms_out = NULL;
     return 0;
 }
+
+/**
+ * As below, but takes an explicit group/level rather than state.
+ */
+static union xkb_action *
+xkb_key_get_action_by_level(struct xkb_desc *xkb, xkb_keycode_t key,
+                            unsigned int group, unsigned int level)
+{
+    return XkbKeyActionEntry(xkb, key, level, group);
+}
+
+/**
+ * Returns the action associated with the given key and state, or NULL if
+ * there are none.
+ */
+union xkb_action *
+xkb_key_get_action(struct xkb_desc *xkb, struct xkb_state *state,
+                   xkb_keycode_t key)
+{
+    int group;
+    int level;
+
+    if (!xkb || !state || key < xkb->min_key_code || key > xkb->max_key_code)
+        return NULL;
+
+    group = xkb_key_get_group(xkb, state, key);
+    if (group == -1)
+        return NULL;
+
+    level = xkb_key_get_level(xkb, state, key, group);
+    if (level == -1)
+        return NULL;
+
+    return xkb_key_get_action_by_level(xkb, key, group, level);
+}
-- 
1.7.9.4



More information about the xorg-devel mailing list