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>
31 #include "xkbcommon/xkbcommon.h"
34 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
35 * keycode set (where ESC is 9). */
36 #define EVDEV_OFFSET 8
39 print_state(struct xkb_state *state)
41 struct xkb_keymap *keymap;
42 xkb_group_index_t group;
46 group = xkb_state_serialize_group(state, XKB_STATE_EFFECTIVE);
47 mod = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
48 /* led = xkb_state_serialize_leds(state, XKB_STATE_EFFECTIVE); */
49 if (!group && !mod /* && !led */) {
50 fprintf(stderr, "\tno state\n");
54 keymap = xkb_state_get_map(state);
56 for (group = 0; group < xkb_map_num_groups(keymap); group++) {
57 if (xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) != 1)
59 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
60 xkb_map_group_get_name(keymap, group),
62 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) > 0 ?
64 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) > 0 ?
66 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) > 0 ?
68 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) > 0 ?
72 for (mod = 0; mod < xkb_map_num_mods(keymap); mod++) {
73 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
75 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
76 xkb_map_mod_get_name(keymap, mod),
78 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) > 0 ?
80 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) > 0 ?
82 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) > 0 ?
86 for (led = 0; led < xkb_map_num_leds(keymap); led++) {
87 if (!xkb_state_led_index_is_active(state, led))
89 fprintf(stderr, "\tled %s (%d): active\n",
90 xkb_map_led_get_name(keymap, led),
96 test_update_key(struct xkb_keymap *keymap)
98 struct xkb_state *state = xkb_state_new(keymap);
99 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_map_mod_get_index(keymap, XKB_MOD_NAME_CTRL),
127 xkb_map_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_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_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
176 assert(num_syms == 1 && syms[0] == XKB_KEY_q);
178 xkb_state_unref(state);
182 test_serialisation(struct xkb_keymap *keymap)
184 struct xkb_state *state = xkb_state_new(keymap);
185 xkb_mod_mask_t base_mods;
186 xkb_mod_mask_t latched_mods;
187 xkb_mod_mask_t locked_mods;
188 xkb_mod_mask_t effective_mods;
189 xkb_mod_index_t caps, shift, ctrl;
190 xkb_group_index_t base_group = 0;
191 xkb_group_index_t latched_group = 0;
192 xkb_group_index_t locked_group = 0;
196 caps = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
197 assert(caps != XKB_MOD_INVALID);
198 shift = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
199 assert(shift != XKB_MOD_INVALID);
200 ctrl = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
201 assert(ctrl != XKB_MOD_INVALID);
203 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
204 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
205 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
206 assert(base_mods == 0);
207 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
208 assert(latched_mods == 0);
209 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
210 assert(locked_mods == (1 << caps));
211 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
212 assert(effective_mods == locked_mods);
214 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
215 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
216 assert(base_mods == (1 << shift));
217 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
218 assert(latched_mods == 0);
219 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
220 assert(locked_mods == (1 << caps));
221 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
222 assert(effective_mods == (base_mods | locked_mods));
224 base_mods |= (1 << ctrl);
225 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
226 base_group, latched_group, locked_group);
228 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
229 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
231 xkb_state_unref(state);
235 test_repeat(struct xkb_keymap *keymap)
237 assert(!xkb_key_repeats(keymap, KEY_LEFTSHIFT + 8));
238 assert(xkb_key_repeats(keymap, KEY_A + 8));
239 assert(xkb_key_repeats(keymap, KEY_8 + 8));
240 assert(xkb_key_repeats(keymap, KEY_DOWN + 8));
241 assert(xkb_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
245 test_consume(struct xkb_keymap *keymap)
247 struct xkb_state *state = xkb_state_new(keymap);
248 xkb_mod_index_t alt, shift;
253 alt = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_ALT);
254 assert(alt != XKB_MOD_INVALID);
255 shift = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
256 assert(shift != XKB_MOD_INVALID);
258 xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
259 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
260 xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
262 fprintf(stderr, "dumping state for Alt-Shift-+\n");
265 mask = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
266 assert(mask == ((1 << alt) | (1 << shift)));
267 mask = xkb_key_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
269 assert(mask == (1 << alt));
271 xkb_state_unref(state);
277 struct xkb_context *context = test_get_context();
278 struct xkb_keymap *keymap;
282 keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
285 test_update_key(keymap);
286 test_serialisation(keymap);
288 test_consume(keymap);
290 xkb_map_unref(keymap);
291 xkb_context_unref(context);