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>
30 #include <X11/Xdefs.h>
31 #include <X11/keysym.h>
32 #include <linux/input.h>
33 #include "xkbcommon/xkbcommon.h"
35 #include "XKBcommonint.h"
37 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
38 * keycode set (where ESC is 9). */
39 #define EVDEV_OFFSET 8
42 print_state(struct xkb_state *state)
44 xkb_group_index_t group;
48 if (!state->group && !state->mods && !state->leds) {
49 fprintf(stderr, "\tno state\n");
53 for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
54 if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
56 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
57 xkb_map_group_get_name(state->xkb, group),
59 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
61 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
63 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
65 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
69 for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
70 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
72 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
73 xkb_map_mod_get_name(state->xkb, mod),
75 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
77 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
79 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
83 for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
84 if (!xkb_state_led_index_is_active(state, led))
86 fprintf(stderr, "\tled %s (%d): active\n",
87 xkb_map_led_get_name(state->xkb, led),
93 test_update_key(struct xkb_desc *xkb)
95 struct xkb_state *state = xkb_state_new(xkb);
102 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
103 fprintf(stderr, "dumping state for LCtrl down:\n");
105 assert(xkb_state_mod_name_is_active(state, "Control",
106 XKB_STATE_DEPRESSED));
108 /* LCtrl + RAlt down */
109 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
110 fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
112 assert(xkb_state_mod_name_is_active(state, "Control",
113 XKB_STATE_DEPRESSED));
114 assert(xkb_state_mod_name_is_active(state, "Mod1",
115 XKB_STATE_DEPRESSED));
118 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
119 fprintf(stderr, "dumping state for RAlt down:\n");
121 assert(!xkb_state_mod_name_is_active(state, "Control",
122 XKB_STATE_EFFECTIVE));
123 assert(xkb_state_mod_name_is_active(state, "Mod1",
124 XKB_STATE_DEPRESSED));
127 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
128 assert(!xkb_state_mod_name_is_active(state, "Mod1",
129 XKB_STATE_EFFECTIVE));
132 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
133 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
134 fprintf(stderr, "dumping state for Caps Lock:\n");
136 assert(xkb_state_mod_name_is_active(state, "Caps Lock",
138 assert(xkb_state_led_name_is_active(state, "Caps Lock"));
139 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
140 assert(num_syms == 1 && syms[0] == XK_Q);
143 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
144 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
145 assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
146 XKB_STATE_EFFECTIVE));
147 assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
148 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
149 assert(num_syms == 1 && syms[0] == XK_q);
151 xkb_state_unref(state);
155 test_serialisation(struct xkb_desc *xkb)
157 struct xkb_state *state = xkb_state_new(xkb);
158 xkb_mod_mask_t base_mods;
159 xkb_mod_mask_t latched_mods;
160 xkb_mod_mask_t locked_mods;
161 xkb_mod_mask_t effective_mods;
162 xkb_mod_index_t caps, shift, ctrl;
163 xkb_group_index_t base_group = 0;
164 xkb_group_index_t latched_group = 0;
165 xkb_group_index_t locked_group = 0;
169 caps = xkb_map_mod_get_index(state->xkb, "Caps Lock");
170 assert(caps != XKB_MOD_INVALID);
171 shift = xkb_map_mod_get_index(state->xkb, "Shift");
172 assert(shift != XKB_MOD_INVALID);
173 ctrl = xkb_map_mod_get_index(state->xkb, "Control");
174 assert(ctrl != XKB_MOD_INVALID);
176 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
177 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
178 base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
179 assert(base_mods == 0);
180 latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
181 assert(latched_mods == 0);
182 locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
183 assert(locked_mods == (1 << caps));
184 effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
185 assert(effective_mods == locked_mods);
187 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
188 base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
189 assert(base_mods == (1 << shift));
190 latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
191 assert(latched_mods == 0);
192 locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
193 assert(locked_mods == (1 << caps));
194 effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
195 assert(effective_mods == (base_mods | locked_mods));
197 base_mods |= (1 << ctrl);
198 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
199 base_group, latched_group, locked_group);
201 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
202 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
204 xkb_state_unref(state);
208 main(int argc, char *argv[])
210 struct xkb_rule_names rmlvo;
211 struct xkb_desc *xkb;
213 rmlvo.rules = "evdev";
214 rmlvo.model = "pc104";
216 rmlvo.variant = NULL;
217 rmlvo.options = NULL;
219 xkb = xkb_map_new_from_names(&rmlvo);
222 test_update_key(xkb);
223 test_serialisation(xkb);