Add xkb_state_key_get_one_sym The trivial wrapper around xkb_state_key_get_syms that every user to date has implemented. Signed-off-by: Daniel Stone <daniel@fooishbar.org>
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
diff --git a/src/state.c b/src/state.c
index 9fa57ff..f22c7a7 100644
--- a/src/state.c
+++ b/src/state.c
@@ -766,6 +766,22 @@ err:
}
/**
+ * 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)
+{
+ xkb_keysym_t *syms;
+ int num_syms;
+
+ num_syms = xkb_state_key_get_syms(state, kc, &syms);
+ if (num_syms != 1)
+ return XKB_KEY_NoSymbol;
+
+ return syms[0];
+}
+
+/**
* Serialises the requested modifier state into an xkb_mod_mask_t, with all
* the same disclaimers as in xkb_state_update_mask.
*/
diff --git a/test/state.c b/test/state.c
index cdb7583..3da4c99 100644
--- a/test/state.c
+++ b/test/state.c
@@ -96,6 +96,7 @@ 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);
@@ -174,6 +175,20 @@ test_update_key(struct xkb_keymap *keymap)
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);
}
diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h
index 1049ca5..a6b0fa8 100644
--- a/xkbcommon/xkbcommon.h
+++ b/xkbcommon/xkbcommon.h
@@ -725,6 +725,13 @@ xkb_state_key_get_syms(struct xkb_state *state, xkb_keycode_t key,
const xkb_keysym_t **syms_out);
/**
+ * As with xkb_state_key_get_syms, but either returns exactly keysym, or
+ * XKB_KEY_NoSymbol if there are either zero or more than one symbols.
+ */
+xkb_keysym_t
+xkb_state_key_get_one_sym(struct xkb_state *state, xkb_keycode_t key);
+
+/**
* Returns the layout number that would be active for a particular key with
* the given state.
*/