Add multiple modifier state matching API Two new calls allow users to test the exact modifier state, including verifying that no other modifiers but the ones you wanted are down. 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index 0527143..dac14ec 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -489,6 +489,22 @@ enum xkb_state_component {
};
/**
+ * Match flags for xkb_state_mod_indices_are_active and
+ * xkb_state_mod_names_are_active, specifying how the conditions for a
+ * successful match. XKB_STATE_MATCH_NON_EXCLUSIVE is bitmaskable with
+ * the other modes.
+ */
+enum xkb_state_match {
+ /** Returns true if any of the modifiers are active. */
+ XKB_STATE_MATCH_ANY = (1 << 0),
+ /** Returns true if all of the modifiers are active. */
+ XKB_STATE_MATCH_ALL = (1 << 1),
+ /** Makes matching non-exclusive, i.e. will not return false if a
+ * modifier not specified in the arguments is active. */
+ XKB_STATE_MATCH_NON_EXCLUSIVE = (1 << 16),
+};
+
+/**
* Updates a state object from a set of explicit masks. This entrypoint is
* really only for window systems and the like, where a master process
* holds an xkb_state, then serialises it over a wire protocol, and clients
@@ -537,13 +553,25 @@ xkb_state_serialise_group(struct xkb_state *state,
/**
* Returns 1 if the modifier specified by 'name' is active in the manner
* specified by 'type', 0 if it is unset, or -1 if the modifier does not
- * exist in the current map.
+ * exist in the map.
*/
int
xkb_state_mod_name_is_active(struct xkb_state *state, const char *name,
enum xkb_state_component type);
/**
+ * Returns 1 if the modifiers specified by the varargs (treated as
+ * NULL-terminated pointers to strings) are active in the manner
+ * specified by 'match', 0 otherwise, or -1 if any of the modifiers
+ * do not exist in the map.
+ */
+int
+xkb_state_mod_names_are_active(struct xkb_state *state,
+ enum xkb_state_component type,
+ enum xkb_state_match match,
+ ...);
+
+/**
* Returns 1 if the modifier specified by 'idx' is active in the manner
* specified by 'type', 0 if it is unset, or -1 if the modifier does not
* exist in the current map.
@@ -553,6 +581,18 @@ xkb_state_mod_index_is_active(struct xkb_state *state, xkb_mod_index_t idx,
enum xkb_state_component type);
/**
+ * Returns 1 if the modifiers specified by the varargs (treated as
+ * xkb_mod_index_t, terminated with XKB_MOD_INVALID) are active in the manner
+ * specified by 'match' and 'type', 0 otherwise, or -1 if the modifier does not
+ * exist in the current map.
+ */
+int
+xkb_state_mod_indices_are_active(struct xkb_state *state,
+ enum xkb_state_component type,
+ enum xkb_state_match match,
+ ...);
+
+/**
* Returns 1 if the group specified by 'name' is active in the manner
* specified by 'type', 0 if it is unset, or -1 if the group does not
* exist in the current map.
diff --git a/src/state.c b/src/state.c
index 8bfe30d..5d731db 100644
--- a/src/state.c
+++ b/src/state.c
@@ -59,6 +59,7 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
+#include <stdarg.h>
#include "xkb-priv.h"
@@ -684,6 +685,59 @@ xkb_state_mod_index_is_active(struct xkb_state *state,
}
/**
+ * 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)
+{
+ uint32_t active = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
+
+ if (!(match & XKB_STATE_MATCH_NON_EXCLUSIVE) && (active & ~wanted))
+ return 0;
+
+ if (match & XKB_STATE_MATCH_ANY)
+ return !!(active & wanted);
+ else
+ return (active & wanted) == wanted;
+
+ return 0;
+}
+
+/**
+ * Returns 1 if the modifiers are active with the specified type(s), 0 if
+ * not, or -1 if any of the modifiers are invalid.
+ */
+_X_EXPORT int
+xkb_state_mod_indices_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;
+ uint32_t wanted = 0;
+ int ret = 0;
+
+ 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->xkb)) {
+ ret = -1;
+ break;
+ }
+ wanted |= (1 << idx);
+ }
+ va_end(ap);
+
+ if (ret == -1)
+ return ret;
+
+ return match_mod_masks(state, match, wanted);
+}
+
+/**
* Returns 1 if the given modifier is active with the specified type(s), 0 if
* not, or -1 if the modifier is invalid.
*/
@@ -700,6 +754,42 @@ 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.
+ */
+_X_EXPORT 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;
+ int ret = 0;
+
+ va_start(ap, match);
+ while (1) {
+ str = va_arg(ap, const char *);
+ if (str == NULL)
+ break;
+ idx = xkb_map_mod_get_index(state->xkb, str);
+ if (idx == XKB_MOD_INVALID) {
+ ret = -1;
+ break;
+ }
+ wanted |= (1 << idx);
+ }
+ va_end(ap);
+
+ if (ret == -1)
+ return ret;
+
+ return match_mod_masks(state, match, wanted);
+}
+
+/**
* Returns 1 if the given group is active with the specified type(s), 0 if
* not, or -1 if the group is invalid.
*/
diff --git a/test/state.c b/test/state.c
index c081ba6..d43d25e 100644
--- a/test/state.c
+++ b/test/state.c
@@ -112,6 +112,20 @@ test_update_key(struct xkb_keymap *xkb)
XKB_STATE_DEPRESSED));
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
XKB_STATE_DEPRESSED));
+ assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
+ XKB_STATE_MATCH_ALL,
+ XKB_MOD_NAME_CTRL,
+ XKB_MOD_NAME_ALT,
+ NULL));
+ assert(!xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
+ XKB_STATE_MATCH_ALL,
+ XKB_MOD_NAME_ALT,
+ NULL));
+ assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
+ (XKB_STATE_MATCH_ANY |
+ XKB_STATE_MATCH_NON_EXCLUSIVE),
+ XKB_MOD_NAME_ALT,
+ NULL));
/* RAlt down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
@@ -121,6 +135,11 @@ test_update_key(struct xkb_keymap *xkb)
XKB_STATE_EFFECTIVE));
assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
XKB_STATE_DEPRESSED));
+ assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
+ XKB_STATE_MATCH_ANY,
+ XKB_MOD_NAME_CTRL,
+ XKB_MOD_NAME_ALT,
+ NULL));
/* none down */
xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);