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 <X11/keysym.h>
30 #include <linux/input.h>
32 #include "xkbcommon/xkbcommon.h"
33 #include "XKBcommonint.h"
35 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
36 * keycode set (where ESC is 9). */
37 #define EVDEV_OFFSET 8
40 print_state(struct xkb_state *state)
42 xkb_group_index_t group;
46 if (!state->group && !state->mods && !state->leds) {
47 fprintf(stderr, "\tno state\n");
51 for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
52 if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
54 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
55 xkb_map_group_get_name(state->xkb, group),
57 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
59 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
61 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
63 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
67 for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
68 if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
70 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
71 xkb_map_mod_get_name(state->xkb, mod),
73 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
75 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
77 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
81 for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
82 if (!xkb_state_led_index_is_active(state, led))
84 fprintf(stderr, "\tled %s (%d): active\n",
85 xkb_map_led_get_name(state->xkb, led),
91 test_update_key(struct xkb_keymap *xkb)
93 struct xkb_state *state = xkb_state_new(xkb);
94 const xkb_keysym_t *syms;
100 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
101 fprintf(stderr, "dumping state for LCtrl down:\n");
103 assert(xkb_state_mod_name_is_active(state, "Control",
104 XKB_STATE_DEPRESSED));
106 /* LCtrl + RAlt down */
107 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
108 fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
110 assert(xkb_state_mod_name_is_active(state, "Control",
111 XKB_STATE_DEPRESSED));
112 assert(xkb_state_mod_name_is_active(state, "Mod1",
113 XKB_STATE_DEPRESSED));
116 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
117 fprintf(stderr, "dumping state for RAlt down:\n");
119 assert(!xkb_state_mod_name_is_active(state, "Control",
120 XKB_STATE_EFFECTIVE));
121 assert(xkb_state_mod_name_is_active(state, "Mod1",
122 XKB_STATE_DEPRESSED));
125 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
126 assert(!xkb_state_mod_name_is_active(state, "Mod1",
127 XKB_STATE_EFFECTIVE));
130 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
131 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
132 fprintf(stderr, "dumping state for Caps Lock:\n");
134 assert(xkb_state_mod_name_is_active(state, "Caps Lock",
136 assert(xkb_state_led_name_is_active(state, "Caps Lock"));
137 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
138 assert(num_syms == 1 && syms[0] == XK_Q);
141 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
142 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
143 assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
144 XKB_STATE_EFFECTIVE));
145 assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
146 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
147 assert(num_syms == 1 && syms[0] == XK_q);
149 xkb_state_unref(state);
153 test_serialisation(struct xkb_keymap *xkb)
155 struct xkb_state *state = xkb_state_new(xkb);
156 xkb_mod_mask_t base_mods;
157 xkb_mod_mask_t latched_mods;
158 xkb_mod_mask_t locked_mods;
159 xkb_mod_mask_t effective_mods;
160 xkb_mod_index_t caps, shift, ctrl;
161 xkb_group_index_t base_group = 0;
162 xkb_group_index_t latched_group = 0;
163 xkb_group_index_t locked_group = 0;
167 caps = xkb_map_mod_get_index(state->xkb, "Caps Lock");
168 assert(caps != XKB_MOD_INVALID);
169 shift = xkb_map_mod_get_index(state->xkb, "Shift");
170 assert(shift != XKB_MOD_INVALID);
171 ctrl = xkb_map_mod_get_index(state->xkb, "Control");
172 assert(ctrl != XKB_MOD_INVALID);
174 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
175 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
176 base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
177 assert(base_mods == 0);
178 latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
179 assert(latched_mods == 0);
180 locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
181 assert(locked_mods == (1 << caps));
182 effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
183 assert(effective_mods == locked_mods);
185 xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
186 base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
187 assert(base_mods == (1 << shift));
188 latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
189 assert(latched_mods == 0);
190 locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
191 assert(locked_mods == (1 << caps));
192 effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
193 assert(effective_mods == (base_mods | locked_mods));
195 base_mods |= (1 << ctrl);
196 xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
197 base_group, latched_group, locked_group);
199 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
200 assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
202 xkb_state_unref(state);
208 struct xkb_context *context;
209 struct xkb_keymap *xkb;
210 struct xkb_rule_names rmlvo = {
218 context = xkb_context_new();
221 xkb = xkb_map_new_from_names(context, &rmlvo);
224 test_update_key(xkb);
225 test_serialisation(xkb);
228 xkb_context_unref(context);