Add state serialisation API 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
diff --git a/include/xkbcommon/xkbcommon.h b/include/xkbcommon/xkbcommon.h
index 18441c0..b7259e6 100644
--- a/include/xkbcommon/xkbcommon.h
+++ b/include/xkbcommon/xkbcommon.h
@@ -63,6 +63,7 @@ THE USE OR PERFORMANCE OF THIS SOFTWARE.
typedef uint32_t xkb_keycode_t;
typedef uint32_t xkb_keysym_t;
typedef uint32_t xkb_mod_index_t;
+typedef uint32_t xkb_mod_mask_t;
typedef uint32_t xkb_group_index_t;
typedef uint32_t xkb_led_index_t;
@@ -742,6 +743,52 @@ enum xkb_state_component {
};
/**
+ * 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
+ * then use the serialisation to feed in to their own xkb_state.
+ *
+ * All parameters must always be passed, or the resulting state may be
+ * incoherent.
+ *
+ * The serialisation is lossy and will not survive round trips; it must only
+ * be used to feed slave state objects, and must not be used to update the
+ * master state.
+ *
+ * Please do not use this unless you fit the description above.
+ */
+_X_EXPORT void
+xkb_state_update_mask(struct xkb_state *state,
+ xkb_mod_mask_t base_mods,
+ xkb_mod_mask_t latched_mods,
+ xkb_mod_mask_t locked_mods,
+ xkb_group_index_t base_group,
+ xkb_group_index_t latched_group,
+ xkb_group_index_t locked_group);
+
+/**
+ * The counterpart to xkb_state_update_mask, to be used on the server side
+ * of serialisation. Returns a xkb_mod_mask_t representing the given
+ * component(s) of the state.
+ *
+ * This function should not be used in regular clients; please use the
+ * xkb_state_mod_*_is_active or xkb_state_foreach_active_mod API instead.
+ *
+ * Can return NULL on failure.
+ */
+_X_EXPORT xkb_mod_mask_t
+xkb_state_serialise_mods(struct xkb_state *state,
+ enum xkb_state_component component);
+
+/**
+ * The group equivalent of xkb_state_serialise_mods: please see its
+ * documentation.
+ */
+_X_EXPORT xkb_group_index_t
+xkb_state_serialise_group(struct xkb_state *state,
+ enum xkb_state_component component);
+
+/**
* 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.
diff --git a/src/state.c b/src/state.c
index ca87673..613bae2 100644
--- a/src/state.c
+++ b/src/state.c
@@ -499,14 +499,12 @@ xkb_state_led_update_all(struct xkb_state *state)
}
/**
- * Given a particular key event, updates the state structure to reflect the
- * new modifiers.
+ * Calculates the derived state (effective mods/group and LEDs) from an
+ * up-to-date xkb_state.
*/
-void
-xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down)
+static void
+xkb_state_update_derived(struct xkb_state *state)
{
- xkb_filter_apply_all(state, key, down);
-
state->mods = (state->base_mods | state->latched_mods | state->locked_mods);
/* FIXME: Clamp/wrap locked_group */
state->group = state->locked_group + state->base_group +
@@ -517,6 +515,101 @@ xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down)
}
/**
+ * Given a particular key event, updates the state structure to reflect the
+ * new modifiers.
+ */
+void
+xkb_state_update_key(struct xkb_state *state, xkb_keycode_t key, int down)
+{
+ xkb_filter_apply_all(state, key, down);
+ xkb_state_update_derived(state);
+}
+
+/**
+ * Updates the state from a set of explicit masks as gained from
+ * xkb_state_serialise_mods and xkb_state_serialise_groups. As noted in the
+ * documentation for these functions in xkbcommon.h, this round-trip is
+ * lossy, and should only be used to update a slave state mirroring the
+ * master, e.g. in a client/server window system.
+ */
+void
+xkb_state_update_mask(struct xkb_state *state,
+ xkb_mod_mask_t base_mods,
+ xkb_mod_mask_t latched_mods,
+ xkb_mod_mask_t locked_mods,
+ xkb_group_index_t base_group,
+ xkb_group_index_t latched_group,
+ xkb_group_index_t locked_group)
+{
+ xkb_mod_mask_t mod;
+
+ state->base_mods = 0;
+ state->latched_mods = 0;
+ state->locked_mods = 0;
+ for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
+ xkb_mod_mask_t idx = (1 << mod);
+ if (base_mods & idx)
+ state->base_mods |= idx;
+ if (latched_mods & idx)
+ state->latched_mods |= idx;
+ if (locked_mods & idx)
+ state->locked_mods |= idx;
+ }
+
+ state->base_group = base_group;
+ state->latched_group = latched_group;
+ state->locked_group = locked_group;
+
+ xkb_state_update_derived(state);
+}
+
+/**
+ * Serialises the requested modifier state into an xkb_mod_mask_t, with all
+ * the same disclaimers as in xkb_state_update_mask.
+ */
+xkb_mod_mask_t
+xkb_state_serialise_mods(struct xkb_state *state,
+ enum xkb_state_component type)
+{
+ xkb_mod_mask_t ret = 0;
+
+ if (type == XKB_STATE_EFFECTIVE)
+ return state->mods;
+
+ if (type & XKB_STATE_DEPRESSED)
+ ret |= state->base_mods;
+ if (type & XKB_STATE_LATCHED)
+ ret |= state->latched_mods;
+ if (type & XKB_STATE_LOCKED)
+ ret |= state->locked_mods;
+
+ return ret;
+}
+
+/**
+ * Serialises the requested group state, with all the same disclaimers as
+ * in xkb_state_update_mask.
+ */
+xkb_group_index_t
+xkb_state_serialise_group(struct xkb_state *state,
+ enum xkb_state_component type)
+{
+ xkb_group_index_t ret = 0;
+
+ if (type == XKB_STATE_EFFECTIVE)
+ return state->group;
+
+ if (type & XKB_STATE_DEPRESSED)
+ ret += state->base_group;
+ if (type & XKB_STATE_LATCHED)
+ ret += state->latched_group;
+ if (type & XKB_STATE_LOCKED)
+ ret += state->locked_group;
+
+ return ret;
+}
+
+/**
* Returns 1 if the given modifier is active with the specified type(s), 0 if
* not, or -1 if the modifier is invalid.
*/
diff --git a/test/state.c b/test/state.c
index c320440..000e47f 100644
--- a/test/state.c
+++ b/test/state.c
@@ -89,29 +89,14 @@ print_state(struct xkb_state *state)
}
}
-int
-main(int argc, char *argv[])
+static void
+test_update_key(struct xkb_desc *xkb)
{
- struct xkb_rule_names rmlvo;
- struct xkb_desc *xkb;
- struct xkb_state *state;
- int num_syms;
+ struct xkb_state *state = xkb_state_new(xkb);
xkb_keysym_t *syms;
+ int num_syms;
- rmlvo.rules = "evdev";
- rmlvo.model = "pc104";
- rmlvo.layout = "us";
- rmlvo.variant = NULL;
- rmlvo.options = NULL;
-
- xkb = xkb_map_new_from_names(&rmlvo);
-
- if (!xkb) {
- fprintf(stderr, "Failed to compile keymap\n");
- exit(1);
- }
-
- state = xkb_state_new(xkb);
+ assert(state);
/* LCtrl down */
xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
@@ -164,7 +149,78 @@ main(int argc, char *argv[])
assert(num_syms == 1 && syms[0] == XK_q);
xkb_state_unref(state);
- xkb_map_unref(xkb);
+}
+
+static void
+test_serialisation(struct xkb_desc *xkb)
+{
+ struct xkb_state *state = xkb_state_new(xkb);
+ xkb_mod_mask_t base_mods;
+ xkb_mod_mask_t latched_mods;
+ xkb_mod_mask_t locked_mods;
+ xkb_mod_mask_t effective_mods;
+ xkb_mod_index_t caps, shift, ctrl;
+ xkb_group_index_t base_group = 0;
+ xkb_group_index_t latched_group = 0;
+ xkb_group_index_t locked_group = 0;
+
+ assert(state);
+
+ caps = xkb_map_mod_get_index(state->xkb, "Caps Lock");
+ assert(caps != XKB_MOD_INVALID);
+ shift = xkb_map_mod_get_index(state->xkb, "Shift");
+ assert(shift != XKB_MOD_INVALID);
+ ctrl = xkb_map_mod_get_index(state->xkb, "Control");
+ assert(ctrl != XKB_MOD_INVALID);
- return 0;
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
+ xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
+ base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
+ assert(base_mods == 0);
+ latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
+ assert(latched_mods == 0);
+ locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
+ assert(locked_mods == (1 << caps));
+ effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
+ assert(effective_mods == locked_mods);
+
+ xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, 1);
+ base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
+ assert(base_mods == (1 << shift));
+ latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
+ assert(latched_mods == 0);
+ locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
+ assert(locked_mods == (1 << caps));
+ effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
+ assert(effective_mods == (base_mods | locked_mods));
+
+ base_mods |= (1 << ctrl);
+ xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
+ base_group, latched_group, locked_group);
+
+ assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
+ assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
+
+ xkb_state_unref(state);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct xkb_rule_names rmlvo;
+ struct xkb_desc *xkb;
+
+ rmlvo.rules = "evdev";
+ rmlvo.model = "pc104";
+ rmlvo.layout = "us";
+ rmlvo.variant = NULL;
+ rmlvo.options = NULL;
+
+ xkb = xkb_map_new_from_names(&rmlvo);
+ assert(xkb);
+
+ test_update_key(xkb);
+ test_serialisation(xkb);
+
+ xkb_map_unref(xkb);
}