Add keycode min/max and iteration API Add three new pieces of API: - xkb_keymap_min_keycode does what it says on the tin - xkb_keymap_max_keycode likewise - xkb_keymap_key_for_each calls the provided function once for every valid key in the keymap 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
diff --git a/src/keymap.c b/src/keymap.c
index 55000f4..8205bab 100644
--- a/src/keymap.c
+++ b/src/keymap.c
@@ -484,6 +484,28 @@ err:
return 0;
}
+XKB_EXPORT xkb_keycode_t
+xkb_keymap_min_keycode(struct xkb_keymap *keymap)
+{
+ return keymap->min_key_code;
+}
+
+XKB_EXPORT xkb_keycode_t
+xkb_keymap_max_keycode(struct xkb_keymap *keymap)
+{
+ return keymap->max_key_code;
+}
+
+XKB_EXPORT void
+xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
+ void *data)
+{
+ struct xkb_key *key;
+
+ xkb_foreach_key(key, keymap)
+ iter(keymap, key->keycode, data);
+}
+
/**
* Simple boolean specifying whether or not the key should repeat.
*/
diff --git a/test/state.c b/test/state.c
index 34da201..3521d66 100644
--- a/test/state.c
+++ b/test/state.c
@@ -331,6 +331,28 @@ test_consume(struct xkb_keymap *keymap)
xkb_state_unref(state);
}
+static void
+key_iter(struct xkb_keymap *keymap, xkb_keycode_t key, void *data)
+{
+ int *counter = (int *) data;
+
+ assert(*counter == key);
+ (*counter)++;
+}
+
+static void
+test_range(struct xkb_keymap *keymap)
+{
+ int counter;
+
+ assert(xkb_keymap_min_keycode(keymap) == 9);
+ assert(xkb_keymap_max_keycode(keymap) == 253);
+
+ counter = xkb_keymap_min_keycode(keymap);
+ xkb_keymap_key_for_each(keymap, key_iter, &counter);
+ assert(counter == xkb_keymap_max_keycode(keymap) + 1);
+}
+
int
main(void)
{
@@ -351,6 +373,7 @@ main(void)
test_serialisation(keymap);
test_repeat(keymap);
test_consume(keymap);
+ test_range(keymap);
xkb_keymap_unref(keymap);
xkb_context_unref(context);
diff --git a/xkbcommon/xkbcommon.h b/xkbcommon/xkbcommon.h
index a2aecfb..ff2ce68 100644
--- a/xkbcommon/xkbcommon.h
+++ b/xkbcommon/xkbcommon.h
@@ -812,6 +812,46 @@ xkb_keymap_get_as_string(struct xkb_keymap *keymap,
*/
/**
+ * Get the minimum keycode in the keymap.
+ *
+ * @sa xkb_keycode_t
+ * @memberof xkb_keymap
+ */
+xkb_keycode_t
+xkb_keymap_min_keycode(struct xkb_keymap *keymap);
+
+/**
+ * Get the maximum keycode in the keymap.
+ *
+ * @sa xkb_keycode_t
+ * @memberof xkb_keymap
+ */
+xkb_keycode_t
+xkb_keymap_max_keycode(struct xkb_keymap *keymap);
+
+/**
+ * The iterator used by xkb_keymap_key_for_each().
+ *
+ * @sa xkb_keymap_key_for_each
+ * @memberof xkb_keymap
+ */
+typedef void
+(*xkb_keymap_key_iter_t)(struct xkb_keymap *keymap, xkb_keycode_t key,
+ void *data);
+
+/**
+ * Run a specified function for every valid keycode in the keymap. If a
+ * keymap is sparse, this function may be called fewer than
+ * (max_keycode - min_keycode + 1) times.
+ *
+ * @sa xkb_keymap_min_keycode() xkb_keymap_max_keycode() xkb_keycode_t
+ * @memberof xkb_keymap
+ */
+void
+xkb_keymap_key_for_each(struct xkb_keymap *keymap, xkb_keymap_key_iter_t iter,
+ void *data);
+
+/**
* Get the number of modifiers in the keymap.
*
* @sa xkb_mod_index_t