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))
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) ?
64 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
66 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
68 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
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) ?
80 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
82 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
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));
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));
117 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
118 XKB_STATE_DEPRESSED));
119 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
124 assert(!xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
128 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
129 (XKB_STATE_MATCH_ANY |
130 XKB_STATE_MATCH_NON_EXCLUSIVE),
135 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
136 fprintf(stderr, "dumping state for RAlt down:\n");
138 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
139 XKB_STATE_EFFECTIVE));
140 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
141 XKB_STATE_DEPRESSED));
142 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
149 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
150 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
151 XKB_STATE_EFFECTIVE));
154 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
155 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
156 fprintf(stderr, "dumping state for Caps Lock:\n");
158 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
160 assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS));
161 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
162 assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
165 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
166 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
167 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
168 XKB_STATE_EFFECTIVE));
169 assert(!xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS));
170 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
171 assert(num_syms == 1 && syms[0] == XKB_KEY_q);
173 xkb_state_unref(state);
177 test_serialisation(struct xkb_keymap *keymap)
179 struct xkb_state *state = xkb_state_new(keymap);
180 xkb_mod_mask_t base_mods;
181 xkb_mod_mask_t latched_mods;
182 xkb_mod_mask_t locked_mods;
183 xkb_mod_mask_t effective_mods;
184 xkb_mod_index_t caps, shift, ctrl;
185 xkb_group_index_t base_group = 0;
186 xkb_group_index_t latched_group = 0;
187 xkb_group_index_t locked_group = 0;
191 caps = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
192 assert(caps != XKB_MOD_INVALID);
193 shift = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
194 assert(shift != XKB_MOD_INVALID);
195 ctrl = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
196 assert(ctrl != XKB_MOD_INVALID);
198 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
199 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
200 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
201 assert(base_mods == 0);
202 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
203 assert(latched_mods == 0);
204 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
205 assert(locked_mods == (1 << caps));
206 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
207 assert(effective_mods == locked_mods);
209 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
210 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
211 assert(base_mods == (1 << shift));
212 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
213 assert(latched_mods == 0);
214 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
215 assert(locked_mods == (1 << caps));
216 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
217 assert(effective_mods == (base_mods | locked_mods));
219 base_mods |= (1 << ctrl);
220 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
221 base_group, latched_group, locked_group);
223 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
224 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
226 xkb_state_unref(state);
230 test_repeat(struct xkb_keymap *keymap)
232 assert(!xkb_key_repeats(keymap, KEY_LEFTSHIFT + 8));
233 assert(xkb_key_repeats(keymap, KEY_A + 8));
234 assert(xkb_key_repeats(keymap, KEY_8 + 8));
235 assert(xkb_key_repeats(keymap, KEY_DOWN + 8));
236 assert(xkb_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
240 test_consume(struct xkb_keymap *keymap)
242 struct xkb_state *state = xkb_state_new(keymap);
243 xkb_mod_index_t alt, shift;
248 alt = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_ALT);
249 assert(alt != XKB_MOD_INVALID);
250 shift = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
251 assert(shift != XKB_MOD_INVALID);
253 xkb_state_update_key(state, KEY_LEFTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
254 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
255 xkb_state_update_key(state, KEY_EQUAL + EVDEV_OFFSET, XKB_KEY_DOWN);
257 fprintf(stderr, "dumping state for Alt-Shift-+\n");
260 mask = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
261 assert(mask == ((1 << alt) | (1 << shift)));
262 mask = xkb_key_mod_mask_remove_consumed(state, KEY_EQUAL + EVDEV_OFFSET,
264 assert(mask == (1 << alt));
266 xkb_state_unref(state);
272 struct xkb_context *context = test_get_context();
273 struct xkb_keymap *keymap;
277 keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
280 test_update_key(keymap);
281 test_serialisation(keymap);
283 test_consume(keymap);
285 xkb_map_unref(keymap);
286 xkb_context_unref(context);