Python: make ruff & black happy
[platform/upstream/libxkbcommon.git] / test / rulescomp.c
1 /*
2  * Copyright © 2009 Dan Nicholson
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
24 #include "config.h"
25
26 #include "evdev-scancodes.h"
27 #include "test.h"
28
29 static int
30 test_rmlvo_va(struct xkb_context *context, const char *rules,
31               const char *model, const char *layout,
32               const char *variant, const char *options, va_list ap)
33 {
34     struct xkb_keymap *keymap;
35     int ret;
36
37     keymap = test_compile_rules(context, rules, model, layout, variant,
38                                 options);
39     if (!keymap)
40         return 0;
41
42     fprintf(stderr, "Compiled '%s' '%s' '%s' '%s' '%s'\n",
43             strnull(rules), strnull(model), strnull(layout),
44             strnull(variant), strnull(options));
45
46     ret = test_key_seq_va(keymap, ap);
47
48     xkb_keymap_unref(keymap);
49
50     return ret;
51 }
52
53 static int
54 test_rmlvo(struct xkb_context *context, const char *rules,
55            const char *model, const char *layout, const char *variant,
56            const char *options, ...)
57 {
58     va_list ap;
59     int ret;
60
61     va_start(ap, options);
62     ret = test_rmlvo_va(context, rules, model, layout, variant, options, ap);
63     va_end(ap);
64
65     return ret;
66 }
67
68 static int
69 test_rmlvo_env(struct xkb_context *ctx, const char *rules, const char *model,
70                const char *layout, const char *variant, const char *options,
71                ...)
72 {
73     va_list ap;
74     int ret;
75
76     va_start (ap, options);
77
78     if (!isempty(rules))
79         setenv("XKB_DEFAULT_RULES", rules, 1);
80     else
81         unsetenv("XKB_DEFAULT_RULES");
82
83     if (!isempty(model))
84         setenv("XKB_DEFAULT_MODEL", model, 1);
85     else
86         unsetenv("XKB_DEFAULT_MODEL");
87
88     if (!isempty(layout))
89         setenv("XKB_DEFAULT_LAYOUT", layout, 1);
90     else
91         unsetenv("XKB_DEFAULT_LAYOUT");
92
93     if (!isempty(variant))
94         setenv("XKB_DEFAULT_VARIANT", variant, 1);
95     else
96         unsetenv("XKB_DEFAULT_VARIANT");
97
98     if (!isempty(options))
99         setenv("XKB_DEFAULT_OPTIONS", options, 1);
100     else
101         unsetenv("XKB_DEFAULT_OPTIONS");
102
103     ret = test_rmlvo_va(ctx, NULL, NULL, NULL, NULL, NULL, ap);
104
105     va_end(ap);
106
107     return ret;
108 }
109
110 int
111 main(int argc, char *argv[])
112 {
113     struct xkb_context *ctx = test_get_context(CONTEXT_ALLOW_ENVIRONMENT_NAMES);
114
115     assert(ctx);
116
117 #define KS(name) xkb_keysym_from_name(name, 0)
118
119     assert(test_rmlvo(ctx, "evdev", "pc105", "us,il,ru,ca", ",,,multix", "grp:alts_toggle,ctrl:nocaps,compose:rwin",
120                       KEY_Q,          BOTH, XKB_KEY_q,                    NEXT,
121                       KEY_LEFTALT,    DOWN, XKB_KEY_Alt_L,                NEXT,
122                       KEY_RIGHTALT,   DOWN, XKB_KEY_ISO_Next_Group,       NEXT,
123                       KEY_RIGHTALT,   UP,   XKB_KEY_ISO_Level3_Shift,     NEXT,
124                       KEY_LEFTALT,    UP,   XKB_KEY_Alt_L,                NEXT,
125                       KEY_Q,          BOTH, XKB_KEY_slash,                NEXT,
126                       KEY_LEFTSHIFT,  DOWN, XKB_KEY_Shift_L,              NEXT,
127                       KEY_Q,          BOTH, XKB_KEY_Q,                    NEXT,
128                       KEY_RIGHTMETA,  BOTH, XKB_KEY_Multi_key,            FINISH));
129     assert(test_rmlvo(ctx, "evdev",  "pc105", "us,in", "", "grp:alts_toggle",
130                       KEY_A,          BOTH, XKB_KEY_a,                    NEXT,
131                       KEY_LEFTALT,    DOWN, XKB_KEY_Alt_L,                NEXT,
132                       KEY_RIGHTALT,   DOWN, XKB_KEY_ISO_Next_Group,       NEXT,
133                       KEY_RIGHTALT,   UP,   XKB_KEY_ISO_Level3_Shift,     NEXT,
134                       KEY_LEFTALT,    UP,   XKB_KEY_Alt_L,                NEXT,
135                       KEY_A,          BOTH, KS("U094b"),                  FINISH));
136     assert(test_rmlvo(ctx, "evdev", "pc105", "us", "intl", "",
137                       KEY_GRAVE,      BOTH,  XKB_KEY_dead_grave,          FINISH));
138     assert(test_rmlvo(ctx, "evdev", "evdev", "us", "intl", "grp:alts_toggle",
139                       KEY_GRAVE,      BOTH,  XKB_KEY_dead_grave,          FINISH));
140
141     /* 20 is not a legal group; make sure this is handled gracefully. */
142     assert(test_rmlvo(ctx, "evdev", "", "us:20", "", "",
143                       KEY_A,          BOTH, XKB_KEY_a,                    FINISH));
144
145     /* Don't choke on missing values in RMLVO. Should just skip them.
146        Currently generates us,us,ca. */
147     assert(test_rmlvo(ctx, "evdev", "", "us,,ca", "", "grp:alts_toggle",
148                       KEY_A,          BOTH, XKB_KEY_a,                    NEXT,
149                       KEY_LEFTALT,    DOWN, XKB_KEY_Alt_L,                NEXT,
150                       KEY_RIGHTALT,   DOWN, XKB_KEY_ISO_Next_Group,       NEXT,
151                       KEY_RIGHTALT,   UP,   XKB_KEY_ISO_Next_Group,       NEXT,
152                       KEY_LEFTALT,    UP,   XKB_KEY_Alt_L,                NEXT,
153                       KEY_LEFTALT,    DOWN, XKB_KEY_Alt_L,                NEXT,
154                       KEY_RIGHTALT,   DOWN, XKB_KEY_ISO_Next_Group,       NEXT,
155                       KEY_RIGHTALT,   UP,   XKB_KEY_ISO_Level3_Shift,     NEXT,
156                       KEY_LEFTALT,    UP,   XKB_KEY_Alt_L,                NEXT,
157                       KEY_APOSTROPHE, BOTH, XKB_KEY_dead_grave,           FINISH));
158
159     assert(test_rmlvo(ctx, "", "", "", "", "",
160                       KEY_A,          BOTH, XKB_KEY_a,                    FINISH));
161
162     assert(!test_rmlvo(ctx, "does-not-exist", "", "", "", "",
163                        KEY_A,          BOTH, XKB_KEY_a,                   FINISH));
164
165     assert(test_rmlvo_env(ctx, "evdev", "", "us", "", "",
166                           KEY_A,          BOTH, XKB_KEY_a,                FINISH));
167     assert(test_rmlvo_env(ctx, "evdev", "", "us", "", "ctrl:nocaps",
168                           KEY_CAPSLOCK,   BOTH, XKB_KEY_Control_L,        FINISH));
169
170     /* Ignores multix and generates us,ca. */
171     assert(test_rmlvo_env(ctx, "evdev", "", "us,ca", ",,,multix", "grp:alts_toggle",
172                           KEY_A,          BOTH, XKB_KEY_a,                NEXT,
173                           KEY_LEFTALT,    DOWN, XKB_KEY_Alt_L,            NEXT,
174                           KEY_RIGHTALT,   DOWN, XKB_KEY_ISO_Next_Group,   NEXT,
175                           KEY_RIGHTALT,   UP,   XKB_KEY_ISO_Level3_Shift, NEXT,
176                           KEY_LEFTALT,    UP,   XKB_KEY_Alt_L,            NEXT,
177                           KEY_GRAVE,      UP,   XKB_KEY_numbersign,       FINISH));
178
179     assert(!test_rmlvo_env(ctx, "broken", "what-on-earth", "invalid", "", "",
180                            KEY_A,          BOTH, XKB_KEY_a,               FINISH));
181
182     /* Ensure a keymap with an empty xkb_keycodes compiles fine. */
183     assert(test_rmlvo_env(ctx, "base", "empty", "empty", "", "",
184                           KEY_A,          BOTH, XKB_KEY_NoSymbol,         FINISH));
185
186     /* Has an illegal escape sequence, but shouldn't fail. */
187     assert(test_rmlvo_env(ctx, "evdev", "", "cz", "bksl", "",
188                           KEY_A,          BOTH, XKB_KEY_a,                FINISH));
189
190     xkb_context_unref(ctx);
191
192     ctx = test_get_context(0);
193     assert(test_rmlvo_env(ctx, "broken", "but", "ignored", "per", "ctx flags",
194                           KEY_A,          BOTH, XKB_KEY_a,                FINISH));
195
196     /* Test response to invalid flags. */
197     {
198         struct xkb_rule_names rmlvo = { NULL };
199         assert(!xkb_keymap_new_from_names(ctx, &rmlvo, -1));
200         assert(!xkb_keymap_new_from_names(ctx, &rmlvo, 5453));
201     }
202
203     xkb_context_unref(ctx);
204     return 0;
205 }