Round out new state API
[platform/upstream/libxkbcommon.git] / test / state.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <X11/X.h>
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"
36
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
40
41 static void
42 print_state(struct xkb_state *state)
43 {
44     xkb_group_index_t group;
45     xkb_mod_index_t mod;
46
47     if (!state->group && !state->mods) {
48         fprintf(stderr, "\tno state\n");
49         return;
50     }
51
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)
55             continue;
56         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
57                 xkb_map_group_get_name(state->xkb, group),
58                 group,
59                 (state->group == group) ? "effective " : "",
60                 (state->base_group == group) ? "depressed " : "",
61                 (state->latched_group == group) ? "latched " : "",
62                 (state->locked_group == group) ? "locked " : "");
63     }
64
65     for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
66         if (!(state->mods & (1 << mod)))
67             continue;
68         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
69                 xkb_map_mod_get_name(state->xkb, mod),
70                 mod,
71                 (state->base_mods & (1 << mod)) ? "depressed " : "",
72                 (state->latched_mods & (1 << mod)) ? "latched " : "",
73                 (state->locked_mods & (1 << mod)) ? "locked " : "");
74     }
75 }
76
77 int
78 main(int argc, char *argv[])
79 {
80     struct xkb_rule_names rmlvo;
81     struct xkb_desc *xkb;
82     struct xkb_state *state;
83     int num_syms;
84     xkb_keysym_t *syms;
85
86     rmlvo.rules = "evdev";
87     rmlvo.model = "pc104";
88     rmlvo.layout = "us";
89     rmlvo.variant = NULL;
90     rmlvo.options = NULL;
91
92     xkb = xkb_compile_keymap_from_rules(&rmlvo);
93
94     if (!xkb) {
95         fprintf(stderr, "Failed to compile keymap\n");
96         exit(1);
97     }
98
99     state = xkb_state_new(xkb);
100
101     /* LCtrl down */
102     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 1);
103     fprintf(stderr, "dumping state for LCtrl down:\n");
104     print_state(state);
105     assert(xkb_state_mod_name_is_active(state, "Control",
106                                         XKB_STATE_DEPRESSED));
107
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");
111     print_state(state);
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));
116
117     /* RAlt down */
118     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, 0);
119     fprintf(stderr, "dumping state for RAlt down:\n");
120     print_state(state);
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));
125
126     /* none down */
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));
130
131     /* Caps locked */
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");
135     print_state(state);
136     assert(xkb_state_mod_name_is_active(state, "Caps Lock",
137                                         XKB_STATE_LOCKED));
138     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
139     assert(num_syms == 1 && syms[0] == XK_Q);
140
141     /* Caps unlocked */
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);
148
149     xkb_state_unref(state);
150     xkb_free_keymap(xkb);
151
152     return 0;
153 }