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"
32 #include "xkbcommon/xkbcommon-names.h"
36 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
37 * keycode set (where ESC is 9). */
38 #define EVDEV_OFFSET 8
41 print_state(struct xkb_state *state)
43 struct xkb_keymap *keymap;
44 xkb_group_index_t group;
48 group = xkb_state_serialize_group(state, XKB_STATE_EFFECTIVE);
49 mod = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
50 /* led = xkb_state_serialize_leds(state, XKB_STATE_EFFECTIVE); */
51 if (!group && !mod /* && !led */) {
52 fprintf(stderr, "\tno state\n");
56 keymap = xkb_state_get_map(state);
58 for (group = 0; group < xkb_map_num_groups(keymap); group++) {
59 if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
61 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
62 xkb_map_group_get_name(keymap, group),
64 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
66 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
68 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
70 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
74 for (mod = 0; mod < xkb_map_num_mods(keymap); mod++) {
75 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
77 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
78 xkb_map_mod_get_name(keymap, mod),
80 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
82 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
84 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
88 for (led = 0; led < xkb_map_num_leds(keymap); led++) {
89 if (!xkb_state_led_index_is_active(state, led))
91 fprintf(stderr, "\tled %s (%d): active\n",
92 xkb_map_led_get_name(keymap, led),
98 test_update_key(struct xkb_keymap *keymap)
100 struct xkb_state *state = xkb_state_new(keymap);
101 const xkb_keysym_t *syms;
107 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
108 fprintf(stderr, "dumping state for LCtrl down:\n");
110 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
111 XKB_STATE_DEPRESSED));
113 /* LCtrl + RAlt down */
114 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
115 fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
117 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
118 XKB_STATE_DEPRESSED));
119 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
120 XKB_STATE_DEPRESSED));
121 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
126 assert(!xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
130 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
131 (XKB_STATE_MATCH_ANY |
132 XKB_STATE_MATCH_NON_EXCLUSIVE),
137 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
138 fprintf(stderr, "dumping state for RAlt down:\n");
140 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
141 XKB_STATE_EFFECTIVE));
142 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
143 XKB_STATE_DEPRESSED));
144 assert(xkb_state_mod_names_are_active(state, XKB_STATE_DEPRESSED,
151 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
152 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
153 XKB_STATE_EFFECTIVE));
156 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
157 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
158 fprintf(stderr, "dumping state for Caps Lock:\n");
160 assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
162 assert(xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS));
163 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
164 assert(num_syms == 1 && syms[0] == XKB_KEY_Q);
167 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
168 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
169 assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
170 XKB_STATE_EFFECTIVE));
171 assert(!xkb_state_led_name_is_active(state, XKB_LED_NAME_CAPS));
172 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
173 assert(num_syms == 1 && syms[0] == XKB_KEY_q);
175 xkb_state_unref(state);
179 test_serialisation(struct xkb_keymap *keymap)
181 struct xkb_state *state = xkb_state_new(keymap);
182 xkb_mod_mask_t base_mods;
183 xkb_mod_mask_t latched_mods;
184 xkb_mod_mask_t locked_mods;
185 xkb_mod_mask_t effective_mods;
186 xkb_mod_index_t caps, shift, ctrl;
187 xkb_group_index_t base_group = 0;
188 xkb_group_index_t latched_group = 0;
189 xkb_group_index_t locked_group = 0;
193 caps = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
194 assert(caps != XKB_MOD_INVALID);
195 shift = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_SHIFT);
196 assert(shift != XKB_MOD_INVALID);
197 ctrl = xkb_map_mod_get_index(keymap, XKB_MOD_NAME_CTRL);
198 assert(ctrl != XKB_MOD_INVALID);
200 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
201 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
202 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
203 assert(base_mods == 0);
204 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
205 assert(latched_mods == 0);
206 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
207 assert(locked_mods == (1 << caps));
208 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
209 assert(effective_mods == locked_mods);
211 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
212 base_mods = xkb_state_serialize_mods(state, XKB_STATE_DEPRESSED);
213 assert(base_mods == (1 << shift));
214 latched_mods = xkb_state_serialize_mods(state, XKB_STATE_LATCHED);
215 assert(latched_mods == 0);
216 locked_mods = xkb_state_serialize_mods(state, XKB_STATE_LOCKED);
217 assert(locked_mods == (1 << caps));
218 effective_mods = xkb_state_serialize_mods(state, XKB_STATE_EFFECTIVE);
219 assert(effective_mods == (base_mods | locked_mods));
221 base_mods |= (1 << ctrl);
222 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
223 base_group, latched_group, locked_group);
225 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
226 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
228 xkb_state_unref(state);
232 test_repeat(struct xkb_keymap *keymap)
234 assert(!xkb_key_repeats(keymap, KEY_LEFTSHIFT + 8));
235 assert(xkb_key_repeats(keymap, KEY_A + 8));
236 assert(xkb_key_repeats(keymap, KEY_8 + 8));
237 assert(xkb_key_repeats(keymap, KEY_DOWN + 8));
238 assert(xkb_key_repeats(keymap, KEY_KBDILLUMDOWN + 8));
244 struct xkb_context *context = test_get_context();
245 struct xkb_keymap *keymap;
249 keymap = test_compile_rules(context, "evdev", "pc104", "us", NULL, NULL);
252 test_update_key(keymap);
253 test_serialisation(keymap);
256 xkb_map_unref(keymap);
257 xkb_context_unref(context);