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;
47 if (!state->group && !state->mods) {
48 fprintf(stderr, "\tno state\n");
52 for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
53 if (group != state->group && group != state->base_group &&
54 group != state->latched_group && group != state->locked_group)
56 fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
57 xkb_map_group_get_name(state->xkb, group),
59 (state->group == group) ? "effective " : "",
60 (state->base_group == group) ? "depressed " : "",
61 (state->latched_group == group) ? "latched " : "",
62 (state->locked_group == group) ? "locked " : "");
65 for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
66 if (!(state->mods & (1 << mod)))
68 fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
69 xkb_map_mod_get_name(state->xkb, mod),
71 (state->base_mods & (1 << mod)) ? "depressed " : "",
72 (state->latched_mods & (1 << mod)) ? "latched " : "",
73 (state->locked_mods & (1 << mod)) ? "locked " : "");
78 main(int argc, char *argv[])
80 struct xkb_rule_names rmlvo;
82 struct xkb_state *state;
86 rmlvo.rules = "evdev";
87 rmlvo.model = "pc104";
92 xkb = xkb_compile_keymap_from_rules(&rmlvo);
95 fprintf(stderr, "Failed to compile keymap\n");
99 state = xkb_state_new(xkb);
102 xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
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, 1);
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, 0);
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, 0);
128 assert(!xkb_state_mod_name_is_active(state, "Mod1",
129 XKB_STATE_EFFECTIVE));
132 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
133 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
134 fprintf(stderr, "dumping state for Caps Lock:\n");
136 assert(xkb_state_mod_name_is_active(state, "Caps Lock",
138 num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
139 assert(num_syms == 1 && syms[0] == XK_Q);
142 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 1);
143 xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, 0);
144 assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
145 XKB_STATE_EFFECTIVE));
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);
150 xkb_free_keymap(xkb);