2 * Copyright © 2012 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 * Author: Daniel Stone <daniel@fooishbar.org>
29 #include <linux/input.h>
33 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
34 * keycode set (where ESC is 9). */
35 #define EVDEV_OFFSET 8
38 print_state(struct xkb_state *state)
40 struct xkb_keymap *keymap;
41 xkb_layout_index_t group;
45 group = xkb_state_serialize_layout(state, XKB_STATE_EFFECTIVE);
46 mod = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
47 /* led = xkb_state_serialize_leds(state, XKB_STATE_EFFECTIVE); */
48 if (!group && !mod /* && !led */) {
49 fprintf(stderr, "\tno state\n");
53 keymap = xkb_state_get_keymap(state);
55 for (group = 0; group < xkb_keymap_num_layouts(keymap); group++) {
56 if (xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE) != 1)
58 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
59 xkb_keymap_layout_get_name(keymap, group),
61 xkb_state_layout_index_is_active(state, group, XKB_STATE_EFFECTIVE) > 0 ?
63 xkb_state_layout_index_is_active(state, group, XKB_STATE_DEPRESSED) > 0 ?
65 xkb_state_layout_index_is_active(state, group, XKB_STATE_LATCHED) > 0 ?
67 xkb_state_layout_index_is_active(state, group, XKB_STATE_LOCKED) > 0 ?
71 for (mod = 0; mod < xkb_keymap_num_mods(keymap); mod++) {
72 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
74 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
75 xkb_keymap_mod_get_name(keymap, mod),
77 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) > 0 ?
79 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) > 0 ?
81 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) > 0 ?
85 for (led = 0; led < xkb_keymap_num_leds(keymap); led++) {
86 if (!xkb_state_led_index_is_active(state, led))
88 fprintf(stderr, "\tled %s (%d): active\n",
89 xkb_keymap_led_get_name(keymap, led),
95 test_update_key(struct xkb_keymap *keymap)
97 struct xkb_state *state = xkb_state_new(keymap);
98 const xkb_keysym_t *syms;
105 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
106 fprintf(stderr, "dumping state for LCtrl down:\n");
108 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
109 XKB_STATE_DEPRESSED) > 0);
111 /* LCtrl + RAlt down */
112 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
113 fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
115 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
116 XKB_STATE_DEPRESSED) > 0);
117 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
118 XKB_STATE_DEPRESSED) > 0);
119 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
124 assert(xkb_state_mod_indices_are_active(state, XKB_STATE_DEPRESSED,
126 xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL),
127 xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT),
128 XKB_MOD_INVALID) > 0);
129 assert(!xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
133 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
134 (XKB_STATE_MATCH_ANY |
135 XKB_STATE_MATCH_NON_EXCLUSIVE),
140 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
141 fprintf(stderr, "dumping state for RAlt down:\n");
143 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
144 XKB_STATE_EFFECTIVE) > 0);
145 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
146 XKB_STATE_DEPRESSED) > 0);
147 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
154 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
155 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
156 XKB_STATE_EFFECTIVE));
159 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
160 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
161 fprintf(stderr, "dumping state for Caps Lock:\n");
163 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
164 XKB_STATE_LOCKED) > 0);
165 assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
166 num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
167 assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
170 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
171 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
172 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
173 XKB_STATE_EFFECTIVE) > 0);
174 assert(!xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS) > 0);
175 num_syms = xkb_state_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
176 assert(num_syms == 1 && syms[0] == XKB_KEY_q);
178 /* Multiple symbols */
179 num_syms = xkb_state_key_get_syms(state, KEY_6 + EVDEV_OFFSET, &syms);
180 assert(num_syms == 5 &&
181 syms[0] == XKB_KEY_H && syms[1] == XKB_KEY_E &&
182 syms[2] == XKB_KEY_L && syms[3] == XKB_KEY_L &&
183 syms[4] == XKB_KEY_O);
184 one_sym = xkb_state_key_get_one_sym(state, KEY_6 + EVDEV_OFFSET);
185 assert(one_sym == XKB_KEY_NoSymbol);
186 xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_DOWN);
187 xkb_state_update_key(state, KEY_6 + EVDEV_OFFSET, XKB_KEY_UP);
189 one_sym = xkb_state_key_get_one_sym(state, KEY_5 + EVDEV_OFFSET);
190 assert(one_sym == XKB_KEY_5);
192 xkb_state_unref(state);
196 test_serialisation(struct xkb_keymap *keymap)
198 struct xkb_state *state = xkb_state_new(keymap);
199 xkb_mod_mask_t base_mods;
200 xkb_mod_mask_t latched_mods;
201 xkb_mod_mask_t locked_mods;
202 xkb_mod_mask_t effective_mods;
203 xkb_mod_index_t caps, shift, ctrl;
204 xkb_layout_index_t base_group = 0;
205 xkb_layout_index_t latched_group = 0;
206 xkb_layout_index_t locked_group = 0;
210 caps = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
211 assert(caps != XKB_MOD_INVALID);
212 shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
213 assert(shift != XKB_MOD_INVALID);
214 ctrl = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
215 assert(ctrl != XKB_MOD_INVALID);
217 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
218 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
219 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
220 assert(base_mods == 0);
221 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
222 assert(latched_mods == 0);
223 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
224 assert(locked_mods == (1 << caps));
225 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
226 assert(effective_mods == locked_mods);
228 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
229 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
230 assert(base_mods == (1 << shift));
231 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
232 assert(latched_mods == 0);
233 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
234 assert(locked_mods == (1 << caps));
235 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
236 assert(effective_mods == (base_mods | locked_mods));
238 base_mods |= (1 << ctrl);
239 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
240 base_group, latched_group, locked_group);
242 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
243 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
245 xkb_state_unref(state);
249 test_repeat(struct xkb_keymap *keymap)
251 assert(!xkb_keymap_key_repeats(keymap, KEY_LEFTSHIFT + 8));
252 assert(xkb_keymap_key_repeats(keymap, KEY_A + 8));
253 assert(xkb_keymap_key_repeats(keymap, KEY_8 + 8));
254 assert(xkb_keymap_key_repeats(keymap, KEY_DOWN + 8));
255 assert(xkb_keymap_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
259 test_consume(struct xkb_keymap *keymap)
261 struct xkb_state *state = xkb_state_new(keymap);
262 xkb_mod_index_t alt, shift;
267 alt = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_ALT);
268 assert(alt != XKB_MOD_INVALID);
269 shift = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
270 assert(shift != XKB_MOD_INVALID);
272 xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
273 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
274 xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
276 fprintf(stderr, "dumping state for Alt-Shift-+\n");
279 mask = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
280 assert(mask == ((1 << alt) | (1 << shift)));
281 mask = xkb_state_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
283 assert(mask == (1 << alt));
285 xkb_state_unref(state);
291 struct xkb_context *context = test_get_context();
292 struct xkb_keymap *keymap;
296 keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
299 test_update_key(keymap);
300 test_serialisation(keymap);
302 test_consume(keymap);
304 xkb_keymap_unref(keymap);
305 xkb_context_unref(context);