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"
34 #include "xkbcomp/utils.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 main(int argc, char *argv[])
95 struct xkb_rule_names rmlvo;
97 struct xkb_state *state;
101 rmlvo.rules = "evdev";
102 rmlvo.model = "pc104";
104 rmlvo.variant = NULL;
105 rmlvo.options = NULL;
107 xkb = xkb_compile_keymap_from_rules(&rmlvo);
110 fprintf(stderr, "Failed to compile keymap\n");
114 state = xkb_state_new(xkb);
117 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
118 fprintf(stderr, "dumping state for LCtrl down:\n");
120 assert(xkb_state_mod_name_is_active(state, "Control",
121 XKB_STATE_DEPRESSED));
123 /* LCtrl + RAlt down */
124 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 1);
125 fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
127 assert(xkb_state_mod_name_is_active(state, "Control",
128 XKB_STATE_DEPRESSED));
129 assert(xkb_state_mod_name_is_active(state, "Mod1",
130 XKB_STATE_DEPRESSED));
133 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0);
134 fprintf(stderr, "dumping state for RAlt down:\n");
136 assert(!xkb_state_mod_name_is_active(state, "Control",
137 XKB_STATE_EFFECTIVE));
138 assert(xkb_state_mod_name_is_active(state, "Mod1",
139 XKB_STATE_DEPRESSED));
142 xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, 0);
143 assert(!xkb_state_mod_name_is_active(state, "Mod1",
144 XKB_STATE_EFFECTIVE));
147 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
148 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
149 fprintf(stderr, "dumping state for Caps Lock:\n");
151 assert(xkb_state_mod_name_is_active(state, "Caps Lock",
153 assert(xkb_state_led_name_is_active(state, "Caps Lock"));
154 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
155 assert(num_syms == 1 && syms[0] == XK_Q);
158 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
159 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
160 assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
161 XKB_STATE_EFFECTIVE));
162 assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
163 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
164 assert(num_syms == 1 && syms[0] == XK_q);
166 xkb_state_unref(state);
167 xkb_free_keymap(xkb);