Use xkb_contexts in keymap compilation
[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/keysym.h>
30 #include <linux/input.h>
31 #include "xkbcommon/xkbcommon.h"
32 #include "utils.h"
33 #include "XKBcommonint.h"
34
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
38
39 static void
40 print_state(struct xkb_state *state)
41 {
42     xkb_group_index_t group;
43     xkb_mod_index_t mod;
44     xkb_led_index_t led;
45
46     if (!state->group && !state->mods && !state->leds) {
47         fprintf(stderr, "\tno state\n");
48         return;
49     }
50
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))
53             continue;
54         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
55                 xkb_map_group_get_name(state->xkb, group),
56                 group,
57                 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
58                     "effective " : "",
59                 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
60                     "depressed " : "",
61                 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
62                     "latched " : "",
63                 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
64                     "locked " : "");
65     }
66
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))
69             continue;
70         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
71                 xkb_map_mod_get_name(state->xkb, mod),
72                 mod,
73                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
74                     "depressed " : "",
75                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
76                     "latched " : "",
77                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
78                     "locked " : "");
79     }
80
81     for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
82         if (!xkb_state_led_index_is_active(state, led))
83             continue;
84         fprintf(stderr, "\tled %s (%d): active\n",
85                 xkb_map_led_get_name(state->xkb, led),
86                 led);
87     }
88 }
89
90 static void
91 test_update_key(struct xkb_desc *xkb)
92 {
93     struct xkb_state *state = xkb_state_new(xkb);
94     xkb_keysym_t *syms;
95     int num_syms;
96
97     assert(state);
98
99     /* LCtrl down */
100     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
101     fprintf(stderr, "dumping state for LCtrl down:\n");
102     print_state(state);
103     assert(xkb_state_mod_name_is_active(state, "Control",
104                                         XKB_STATE_DEPRESSED));
105
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");
109     print_state(state);
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));
114
115     /* RAlt down */
116     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
117     fprintf(stderr, "dumping state for RAlt down:\n");
118     print_state(state);
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));
123
124     /* none down */
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));
128
129     /* Caps locked */
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");
133     print_state(state);
134     assert(xkb_state_mod_name_is_active(state, "Caps Lock",
135                                         XKB_STATE_LOCKED));
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);
139
140     /* Caps unlocked */
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);
148
149     xkb_state_unref(state);
150 }
151
152 static void
153 test_serialisation(struct xkb_desc *xkb)
154 {
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;
164
165     assert(state);
166
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);
173
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);
184
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));
194
195     base_mods |= (1 << ctrl);
196     xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
197                           base_group, latched_group, locked_group);
198
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));
201
202     xkb_state_unref(state);
203 }
204
205 int
206 main(int argc, char *argv[])
207 {
208     struct xkb_context *context;
209     struct xkb_desc *xkb;
210     struct xkb_rule_names rmlvo;
211
212     rmlvo.rules = "evdev";
213     rmlvo.model = "pc104";
214     rmlvo.layout = "us";
215     rmlvo.variant = NULL;
216     rmlvo.options = NULL;
217
218     context = xkb_context_new();
219     assert(context);
220
221     xkb = xkb_map_new_from_names(context, &rmlvo);
222     assert(xkb);
223
224     test_update_key(xkb);
225     test_serialisation(xkb);
226
227     xkb_map_unref(xkb);
228     xkb_context_unref(context);
229 }