Move utils.{c,h} to be used by the entire project
[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 "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     xkb_led_index_t led;
47
48     if (!state->group && !state->mods && !state->leds) {
49         fprintf(stderr, "\tno state\n");
50         return;
51     }
52
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))
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                 xkb_state_group_index_is_active(state, group, XKB_STATE_EFFECTIVE) ?
60                     "effective " : "",
61                 xkb_state_group_index_is_active(state, group, XKB_STATE_DEPRESSED) ?
62                     "depressed " : "",
63                 xkb_state_group_index_is_active(state, group, XKB_STATE_LATCHED) ?
64                     "latched " : "",
65                 xkb_state_group_index_is_active(state, group, XKB_STATE_LOCKED) ?
66                     "locked " : "");
67     }
68
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))
71             continue;
72         fprintf(stderr, "\tmod %s (%d): %s%s%s\n",
73                 xkb_map_mod_get_name(state->xkb, mod),
74                 mod,
75                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_DEPRESSED) ?
76                     "depressed " : "",
77                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LATCHED) ?
78                     "latched " : "",
79                 xkb_state_mod_index_is_active(state, mod, XKB_STATE_LOCKED) ?
80                     "locked " : "");
81     }
82
83     for (led = 0; led < xkb_map_num_leds(state->xkb); led++) {
84         if (!xkb_state_led_index_is_active(state, led))
85             continue;
86         fprintf(stderr, "\tled %s (%d): active\n",
87                 xkb_map_led_get_name(state->xkb, led),
88                 led);
89     }
90 }
91
92 static void
93 test_update_key(struct xkb_desc *xkb)
94 {
95     struct xkb_state *state = xkb_state_new(xkb);
96     xkb_keysym_t *syms;
97     int num_syms;
98
99     assert(state);
100
101     /* LCtrl down */
102     xkb_state_update_key(state, KEY_LEFTCTRL + EVDEV_OFFSET, XKB_KEY_DOWN);
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, XKB_KEY_DOWN);
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, XKB_KEY_UP);
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, XKB_KEY_UP);
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, XKB_KEY_DOWN);
133     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
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     assert(xkb_state_led_name_is_active(state, "Caps Lock"));
139     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
140     assert(num_syms == 1 && syms[0] == XK_Q);
141
142     /* Caps unlocked */
143     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
144     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
145     assert(!xkb_state_mod_name_is_active(state, "Caps Lock",
146                                          XKB_STATE_EFFECTIVE));
147     assert(!xkb_state_led_name_is_active(state, "Caps Lock"));
148     num_syms = xkb_key_get_syms(state, KEY_Q + EVDEV_OFFSET, &syms);
149     assert(num_syms == 1 && syms[0] == XK_q);
150
151     xkb_state_unref(state);
152 }
153
154 static void
155 test_serialisation(struct xkb_desc *xkb)
156 {
157     struct xkb_state *state = xkb_state_new(xkb);
158     xkb_mod_mask_t base_mods;
159     xkb_mod_mask_t latched_mods;
160     xkb_mod_mask_t locked_mods;
161     xkb_mod_mask_t effective_mods;
162     xkb_mod_index_t caps, shift, ctrl;
163     xkb_group_index_t base_group = 0;
164     xkb_group_index_t latched_group = 0;
165     xkb_group_index_t locked_group = 0;
166
167     assert(state);
168
169     caps = xkb_map_mod_get_index(state->xkb, "Caps Lock");
170     assert(caps != XKB_MOD_INVALID);
171     shift = xkb_map_mod_get_index(state->xkb, "Shift");
172     assert(shift != XKB_MOD_INVALID);
173     ctrl = xkb_map_mod_get_index(state->xkb, "Control");
174     assert(ctrl != XKB_MOD_INVALID);
175
176     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_DOWN);
177     xkb_state_update_key(state, KEY_CAPSLOCK + EVDEV_OFFSET, XKB_KEY_UP);
178     base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
179     assert(base_mods == 0);
180     latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
181     assert(latched_mods == 0);
182     locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
183     assert(locked_mods == (1 << caps));
184     effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
185     assert(effective_mods == locked_mods);
186
187     xkb_state_update_key(state, KEY_LEFTSHIFT + EVDEV_OFFSET, XKB_KEY_DOWN);
188     base_mods = xkb_state_serialise_mods(state, XKB_STATE_DEPRESSED);
189     assert(base_mods == (1 << shift));
190     latched_mods = xkb_state_serialise_mods(state, XKB_STATE_LATCHED);
191     assert(latched_mods == 0);
192     locked_mods = xkb_state_serialise_mods(state, XKB_STATE_LOCKED);
193     assert(locked_mods == (1 << caps));
194     effective_mods = xkb_state_serialise_mods(state, XKB_STATE_EFFECTIVE);
195     assert(effective_mods == (base_mods | locked_mods));
196
197     base_mods |= (1 << ctrl);
198     xkb_state_update_mask(state, base_mods, latched_mods, locked_mods,
199                           base_group, latched_group, locked_group);
200
201     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_DEPRESSED));
202     assert(xkb_state_mod_index_is_active(state, ctrl, XKB_STATE_EFFECTIVE));
203
204     xkb_state_unref(state);
205 }
206
207 int
208 main(int argc, char *argv[])
209 {
210     struct xkb_rule_names rmlvo;
211     struct xkb_desc *xkb;
212
213     rmlvo.rules = "evdev";
214     rmlvo.model = "pc104";
215     rmlvo.layout = "us";
216     rmlvo.variant = NULL;
217     rmlvo.options = NULL;
218
219     xkb = xkb_map_new_from_names(&rmlvo);
220     assert(xkb);
221
222     test_update_key(xkb);
223     test_serialisation(xkb);
224
225     xkb_map_unref(xkb);
226 }