Configure github pages
[platform/upstream/libxkbcommon.git] / test / keymap.c
1 /*
2  * Copyright © 2016 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: Mike Blumenkrantz <zmike@osg.samsung.com>
24  */
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "test.h"
33
34 static void
35 test_garbage_key(void)
36 {
37     struct xkb_context *context = test_get_context(0);
38     struct xkb_keymap *keymap;
39     xkb_keycode_t kc;
40     int nsyms;
41     const xkb_keysym_t *syms;
42     const xkb_layout_index_t first_layout = 0;
43     xkb_level_index_t nlevels;
44
45     assert(context);
46
47     keymap = test_compile_rules(context, NULL, NULL, "garbage", NULL, NULL);
48     assert(keymap);
49
50     /* TLDE uses the 'us' sym on the first level and is thus [grave, exclam] */
51     kc = xkb_keymap_key_by_name(keymap, "TLDE");
52     assert(kc != XKB_KEYCODE_INVALID);
53     nlevels = xkb_keymap_num_levels_for_key(keymap, kc, first_layout);
54     assert(nlevels == 2);
55     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &syms);
56     assert(nsyms == 1);
57     assert(*syms == XKB_KEY_grave); /* fallback from 'us' */
58     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 1, &syms);
59     assert(nsyms == 1);
60     assert(*syms == XKB_KEY_exclam);
61
62     /* AE13 has no 'us' fallback and ends up as [NoSymbol, asciitilde] */
63     kc = xkb_keymap_key_by_name(keymap, "AE13");
64     assert(kc != XKB_KEYCODE_INVALID);
65     nlevels = xkb_keymap_num_levels_for_key(keymap, kc, first_layout);
66     assert(nlevels == 2);
67     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 0, &syms);
68     assert(nsyms == 0);
69     nsyms = xkb_keymap_key_get_syms_by_level(keymap, kc, first_layout, 1, &syms);
70     assert(nsyms == 1);
71     assert(*syms == XKB_KEY_asciitilde);
72
73     xkb_keymap_unref(keymap);
74     xkb_context_unref(context);
75 }
76
77 static void
78 test_keymap(void)
79 {
80     struct xkb_context *context = test_get_context(0);
81     struct xkb_keymap *keymap;
82     xkb_keycode_t kc;
83     const char *keyname;
84     xkb_mod_mask_t masks_out[4] = { 0, 0, 0, 0 };
85     size_t mask_count;
86     xkb_mod_mask_t shift_mask;
87     xkb_mod_mask_t lock_mask;
88     xkb_mod_mask_t mod2_mask;
89
90     assert(context);
91
92     keymap = test_compile_rules(context, "evdev", "pc104", "us,ru", NULL, "grp:menu_toggle");
93     assert(keymap);
94
95     kc = xkb_keymap_key_by_name(keymap, "AE09");
96     assert(kc != XKB_KEYCODE_INVALID);
97     keyname = xkb_keymap_key_get_name(keymap, kc);
98     assert(streq(keyname, "AE09"));
99
100     kc = xkb_keymap_key_by_name(keymap, "COMP");
101     assert(kc != XKB_KEYCODE_INVALID);
102     keyname = xkb_keymap_key_get_name(keymap, kc);
103     assert(streq(keyname, "COMP"));
104
105     kc = xkb_keymap_key_by_name(keymap, "MENU");
106     assert(kc != XKB_KEYCODE_INVALID);
107     keyname = xkb_keymap_key_get_name(keymap, kc);
108     assert(streq(keyname, "COMP"));
109
110     kc = xkb_keymap_key_by_name(keymap, "AC01");
111     assert(kc != XKB_KEYCODE_INVALID);
112
113     // AC01 level 0 ('a') requires no modifiers on us-pc104
114     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
115     assert(mask_count == 1);
116     assert(masks_out[0] == 0);
117
118     shift_mask = 1 << xkb_keymap_mod_get_index(keymap, "Shift");
119     lock_mask = 1 << xkb_keymap_mod_get_index(keymap, "Lock");
120     mod2_mask = 1 << xkb_keymap_mod_get_index(keymap, "Mod2");
121
122     // AC01 level 1 ('A') requires either Shift or Lock modifiers on us-pc104
123     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 1, masks_out, 4);
124     assert(mask_count == 2);
125     assert(masks_out[0] == shift_mask);
126     assert(masks_out[1] == lock_mask);
127
128     kc = xkb_keymap_key_by_name(keymap, "KP1");
129
130     // KP1 level 0 ('End') requires no modifiers or Shift+Mod2 on us-pc104
131     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
132     assert(mask_count == 2);
133     assert(masks_out[0] == 0);
134     assert(masks_out[1] == (shift_mask | mod2_mask));
135
136     // KP1 level 1 ('1') requires either Shift or Mod2 modifiers on us-pc104
137     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 1, masks_out, 4);
138     assert(mask_count == 2);
139     assert(masks_out[0] == shift_mask);
140     assert(masks_out[1] == mod2_mask);
141
142     // Return key is not affected by modifiers on us-pc104
143     kc = xkb_keymap_key_by_name(keymap, "RTRN");
144     mask_count = xkb_keymap_key_get_mods_for_level(keymap, kc, 0, 0, masks_out, 4);
145     assert(mask_count == 1);
146     assert(masks_out[0] == 0);
147
148     xkb_keymap_unref(keymap);
149     xkb_context_unref(context);
150 }
151
152 int
153 main(void)
154 {
155     test_garbage_key();
156     test_keymap();
157
158     return 0;
159 }