Add pre-defined names database
[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
32 #include "xkbcommon/xkbcommon.h"
33 #include "xkbcommon/xkbcommon-names.h"
34 #include "xkb-priv.h"
35
36 /* Offset between evdev keycodes (where KEY_ESCAPE is 1), and the evdev XKB
37  * keycode set (where ESC is 9). */
38 #define EVDEV_OFFSET 8
39
40 static void
41 print_state(struct xkb_state *state)
42 {
43     xkb_group_index_t group;
44     xkb_mod_index_t mod;
45     xkb_led_index_t led;
46
47     if (!state->group && !state->mods && !state->leds) {
48         fprintf(stderr, "\tno state\n");
49         return;
50     }
51
52     for (group = 0; group < xkb_map_num_groups(state->xkb); group++) {
53         if (!xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE))
54             continue;
55         fprintf(stderr, "\tgroup %s (%d): %s%s%s%s\n",
56                 xkb_map_group_get_name(state->xkb, group),
57                 group,
58                 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
59                     "effective " : "",
60                 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
61                     "depressed " : "",
62                 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
63                     "latched " : "",
64                 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
65                     "locked " : "");
66     }
67
68     for (mod = 0; mod < xkb_map_num_mods(state->xkb); mod++) {
69         if (!xkb_state_mod_index_is_active(state, mod, XKB_STATE_EFFECTIVE))
70             continue;
71         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
72                 xkb_map_mod_get_name(state->xkb, mod),
73                 mod,
74                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
75                     "depressed " : "",
76                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
77                     "latched " : "",
78                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
79                     "locked " : "");
80     }
81
82     for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
83         if (!xkb_state_led_index_is_active(state, led))
84             continue;
85         fprintf(stderr, "\tled %s (%d): active\n",
86                 xkb_map_led_get_name(state->xkb, led),
87                 led);
88     }
89 }
90
91 static void
92 test_update_key(struct xkb_keymap *xkb)
93 {
94     struct xkb_state *state = xkb_state_new(xkb);
95     const xkb_keysym_t *syms;
96     int num_syms;
97
98     assert(state);
99
100     /* LCtrl down */
101     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
102     fprintf(stderr, "dumping state for LCtrl down:\n");
103     print_state(state);
104     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
105                                         XKB_STATE_DEPRESSED));
106
107     /* LCtrl + RAlt down */
108     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_DOWN);
109     fprintf(stderr, "dumping state for LCtrl + RAlt down:\n");
110     print_state(state);
111     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
112                                         XKB_STATE_DEPRESSED));
113     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
114                                         XKB_STATE_DEPRESSED));
115
116     /* RAlt down */
117     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_UP);
118     fprintf(stderr, "dumping state for RAlt down:\n");
119     print_state(state);
120     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
121                                          XKB_STATE_EFFECTIVE));
122     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
123                                         XKB_STATE_DEPRESSED));
124
125     /* none down */
126     xkb_state_update_key(state, KEY_RIGHTALT + EVDEV_OFFSET, XKB_KEY_UP);
127     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
128                                          XKB_STATE_EFFECTIVE));
129
130     /* Caps locked */
131     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
132     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
133     fprintf(stderr, "dumping state for Caps Lock:\n");
134     print_state(state);
135     assert(xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
136                                         XKB_STATE_LOCKED));
137     assert(xkb_state_led_name_is_active(state, XKB_MOD_NAME_CAPS));
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, XKB_KEY_DOWN);
143     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
144     assert(!xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CAPS,
145                                          XKB_STATE_EFFECTIVE));
146     assert(!xkb_state_led_name_is_active(state, XKB_MOD_NAME_CAPS));
147     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
148     assert(num_syms == 1 && syms[0] == XK_q);
149
150     xkb_state_unref(state);
151 }
152
153 static void
154 test_serialisation(struct xkb_keymap *xkb)
155 {
156     struct xkb_state *state = xkb_state_new(xkb);
157     xkb_mod_mask_t base_mods;
158     xkb_mod_mask_t latched_mods;
159     xkb_mod_mask_t locked_mods;
160     xkb_mod_mask_t effective_mods;
161     xkb_mod_index_t caps, shift, ctrl;
162     xkb_group_index_t base_group = 0;
163     xkb_group_index_t latched_group = 0;
164     xkb_group_index_t locked_group = 0;
165
166     assert(state);
167
168     caps = xkb_map_mod_get_index(state->xkb, XKB_MOD_NAME_CAPS);
169     assert(caps != XKB_MOD_INVALID);
170     shift = xkb_map_mod_get_index(state->xkb, XKB_MOD_NAME_SHIFT);
171     assert(shift != XKB_MOD_INVALID);
172     ctrl = xkb_map_mod_get_index(state->xkb, XKB_MOD_NAME_CTRL);
173     assert(ctrl != XKB_MOD_INVALID);
174
175     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
176     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
177     base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
178     assert(base_mods == 0);
179     latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
180     assert(latched_mods == 0);
181     locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
182     assert(locked_mods == (1 << caps));
183     effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
184     assert(effective_mods == locked_mods);
185
186     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
187     base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
188     assert(base_mods == (1 << shift));
189     latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
190     assert(latched_mods == 0);
191     locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
192     assert(locked_mods == (1 << caps));
193     effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
194     assert(effective_mods == (base_mods | locked_mods));
195
196     base_mods |= (1 << ctrl);
197     xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
198                           base_group, latched_group, locked_group);
199
200     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
201     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
202
203     xkb_state_unref(state);
204 }
205
206 int
207 main(void)
208 {
209     struct xkb_context *context;
210     struct xkb_keymap *xkb;
211     struct xkb_rule_names rmlvo = {
212         .rules = "evdev",
213         .model = "pc104",
214         .layout = "us",
215         .variant = NULL,
216         .options = NULL,
217     };
218
219     context = xkb_context_new(0);
220     assert(context);
221
222     xkb = xkb_map_new_from_names(context, &rmlvo, 0);
223     assert(xkb);
224
225     test_update_key(xkb);
226     test_serialisation(xkb);
227
228     xkb_map_unref(xkb);
229     xkb_context_unref(context);
230 }