Debian Codegraph @

test/extensions-directories.c

raw
1 /*
2 * Copyright © 2025 Pierre Le Marre <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include "config.h"
7
8 #include <assert.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "xkbcommon/xkbcommon-keysyms.h"
13 #include "xkbcommon/xkbcommon.h"
14 #include "evdev-scancodes.h"
15 #include "test.h"
16 #include "context.h"
17 #include "keymap.h"
18 #include "utils.h"
19
20 #ifdef HAS_XKBREGISTRY
21 #include "xkbcommon/xkbregistry.h"
22 #include <libxml/parser.h>
23 #include <libxml/tree.h>
24 #endif
25
26 #ifdef HAS_XKBREGISTRY
27 static struct rxkb_layout *
28 fetch_layout(struct rxkb_context *ctx, const char *layout, const char *variant)
29 {
30 struct rxkb_layout *l = rxkb_layout_first(ctx);
31 while (l) {
32 const char *v = rxkb_layout_get_variant(l);
33
34 if (streq(rxkb_layout_get_name(l), layout) &&
35 ((v == NULL && variant == NULL) ||
36 (v != NULL && variant != NULL && streq(v, variant))))
37 return rxkb_layout_ref(l);
38 l = rxkb_layout_next(l);
39 }
40 return NULL;
41 }
42 #endif
43
44 static void
45 test_layouts(const char* xkb_root, bool update_output_files)
46 {
47 struct xkb_context *ctx;
48 struct xkb_keymap *keymap;
49 const xkb_keysym_t *keysyms;
50
51 char *unversioned_extensions_path = test_get_path("extensions/without-rules");
52 assert(unversioned_extensions_path);
53 char *versioned_extensions_path = test_get_path("extensions/without-rules-2");
54 assert(versioned_extensions_path);
55 setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", unversioned_extensions_path, 1);
56 setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", versioned_extensions_path, 1);
57 ctx = xkb_context_new(XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
58 assert(ctx);
59
60 char buf[PATH_MAX] = "";
61 assert(xkb_context_num_include_paths(ctx) == 4);
62 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
63 versioned_extensions_path, "p1"));
64 assert(strcmp(xkb_context_include_path_get(ctx, 0), buf) == 0);
65 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
66 versioned_extensions_path, "p2"));
67 assert(strcmp(xkb_context_include_path_get(ctx, 1), buf) == 0);
68 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
69 unversioned_extensions_path, "p3"));
70 assert(strcmp(xkb_context_include_path_get(ctx, 2), buf) == 0);
71 assert(strcmp(xkb_context_include_path_get(ctx, 3), xkb_root) == 0);
72
73 free(unversioned_extensions_path);
74 free(versioned_extensions_path);
75
76 /* New layouts (example from documentation: “Packaging keyboard layouts”) */
77 keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
78 "evdev", "pc104", "a,b,c", NULL, NULL);
79 assert(keymap);
80 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
81 0, 0, &keysyms));
82 assert(keysyms[0] == XKB_KEY_Greek_alpha); /* from versioned dir */
83 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
84 1, 0, &keysyms));
85 assert(keysyms[0] == XKB_KEY_aacute); /* from versioned dir */
86 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
87 2, 0, &keysyms));
88 assert(keysyms[0] == XKB_KEY_adiaeresis); /* from unversioned dir */
89 xkb_keymap_unref(keymap);
90
91 xkb_context_unref(ctx);
92
93 #ifdef HAS_XKBREGISTRY
94 struct rxkb_context *rctx = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
95 assert(rctx);
96 assert(rxkb_context_parse(rctx, "evdev"));
97 struct {
98 const char *layout;
99 const char *variant;
100 const char *description;
101 enum rxkb_popularity popularity;
102 } registry_tests[] = {
103 {
104 .layout = "a",
105 .variant = NULL,
106 .description = "A",
107 .popularity = RXKB_POPULARITY_STANDARD,
108 },
109 {
110 .layout = "b",
111 .variant = NULL,
112 .description = "B",
113 .popularity = RXKB_POPULARITY_EXOTIC,
114 },
115 {
116 .layout = "c",
117 .variant = NULL,
118 .description = "C",
119 .popularity = RXKB_POPULARITY_STANDARD,
120 },
121 };
122
123 for (unsigned int t = 0; t < ARRAY_SIZE(registry_tests); t++) {
124 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
125 struct rxkb_layout * const l = fetch_layout(
126 rctx, registry_tests[t].layout, registry_tests[t].variant
127 );
128 assert(l);
129 assert(streq_null(rxkb_layout_get_description(l),
130 registry_tests[t].description));
131 assert(rxkb_layout_get_popularity(l) == registry_tests[t].popularity);
132 rxkb_layout_unref(l);
133 }
134
135 rxkb_context_unref(rctx);
136 #endif
137 }
138
139 #ifdef HAS_XKBREGISTRY
140 static struct rxkb_option *
141 fetch_option(struct rxkb_context *ctx, const char *grp, const char *opt)
142 {
143 struct rxkb_option_group *g = rxkb_option_group_first(ctx);
144 while (g) {
145 if (streq(grp, rxkb_option_group_get_name(g))) {
146 struct rxkb_option *o = rxkb_option_first(g);
147
148 while (o) {
149 if (streq(opt, rxkb_option_get_name(o)))
150 return rxkb_option_ref(o);
151 o = rxkb_option_next(o);
152 }
153 }
154 g = rxkb_option_group_next(g);
155 }
156 return NULL;
157 }
158 #endif
159
160 static void
161 test_options(const char *xkb_root, bool update_output_files)
162 {
163 struct xkb_context *ctx;
164 struct xkb_keymap *keymap;
165 const xkb_keysym_t *keysyms;
166
167 char *unversioned_extensions_path = test_get_path("extensions/with-rules");
168 assert(unversioned_extensions_path);
169 char *versioned_extensions_path = test_get_path("extensions/with-rules-2");
170 assert(versioned_extensions_path);
171 setenv("XKB_CONFIG_UNVERSIONED_EXTENSIONS_PATH", unversioned_extensions_path, 1);
172 setenv("XKB_CONFIG_VERSIONED_EXTENSIONS_PATH", versioned_extensions_path, 1);
173 ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
174 assert(ctx);
175
176 char buf[PATH_MAX] = "";
177 assert(xkb_context_num_include_paths(ctx) == 4);
178 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
179 versioned_extensions_path, "p1"));
180 assert(strcmp(xkb_context_include_path_get(ctx, 0), buf) == 0);
181 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
182 versioned_extensions_path, "p2"));
183 assert(strcmp(xkb_context_include_path_get(ctx, 1), buf) == 0);
184 assert(snprintf_safe(buf, sizeof(buf), "%s/%s",
185 unversioned_extensions_path, "p3"));
186 assert(strcmp(xkb_context_include_path_get(ctx, 2), buf) == 0);
187 assert(strcmp(xkb_context_include_path_get(ctx, 3), xkb_root) == 0);
188
189 free(unversioned_extensions_path);
190 free(versioned_extensions_path);
191
192 /* New options */
193 keymap = test_compile_rules(ctx, XKB_KEYMAP_FORMAT_TEXT_V1,
194 "evdev", "pc104", "cz,ca,de", NULL,
195 "opt:1,opt:2,opt:3!2");
196 assert(keymap);
197 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
198 0, 0, &keysyms));
199 assert(keysyms[0] == XKB_KEY_Greek_alpha);
200 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
201 0, 0, &keysyms));
202 assert(keysyms[0] == XKB_KEY_sacute);
203 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
204 1, 0, &keysyms));
205 assert(keysyms[0] == XKB_KEY_adiaeresis);
206 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
207 1, 0, &keysyms));
208 assert(keysyms[0] == XKB_KEY_sacute);
209 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_A,
210 2, 0, &keysyms));
211 assert(keysyms[0] == XKB_KEY_a);
212 assert(1 == xkb_keymap_key_get_syms_by_level(keymap, EVDEV_OFFSET + KEY_S,
213 2, 0, &keysyms));
214 assert(keysyms[0] == XKB_KEY_sacute);
215 xkb_keymap_unref(keymap);
216
217 xkb_context_unref(ctx);
218
219 #ifdef HAS_XKBREGISTRY
220
221 struct rxkb_context *rctx = rxkb_context_new(RXKB_CONTEXT_LOAD_EXOTIC_RULES);
222 assert(rctx);
223 assert(rxkb_context_parse(rctx, "evdev"));
224 struct {
225 const char *group;
226 const char *option;
227 const char *description;
228 enum rxkb_popularity popularity;
229 } registry_tests[] = {
230 {
231 .group = "opt",
232 .option = "opt:1",
233 .description = "1",
234 .popularity = RXKB_POPULARITY_STANDARD,
235 },
236 {
237 .group = "opt",
238 .option = "opt:2",
239 .description = "2",
240 .popularity = RXKB_POPULARITY_EXOTIC,
241 },
242 {
243 .group = "opt",
244 .option = "opt:3",
245 .description = "3",
246 .popularity = RXKB_POPULARITY_STANDARD,
247 },
248 };
249
250 for (unsigned int t = 0; t < ARRAY_SIZE(registry_tests); t++) {
251 fprintf(stderr, "------\n*** %s: #%u ***\n", __func__, t);
252 struct rxkb_option *const opt = fetch_option(
253 rctx, registry_tests[t].group, registry_tests[t].option
254 );
255 assert(opt);
256 assert(streq_null(rxkb_option_get_description(opt),
257 registry_tests[t].description));
258 assert(rxkb_option_get_popularity(opt) == registry_tests[t].popularity);
259 rxkb_option_unref(opt);
260 }
261
262 rxkb_context_unref(rctx);
263 #endif
264 }
265
266 int
267 main(int argc, char *argv[])
268 {
269 test_init();
270
271 bool update_output_files = false;
272 if (argc > 1) {
273 if (streq(argv[1], "update")) {
274 /* Update files with *obtained* results */
275 update_output_files = true;
276 } else {
277 fprintf(stderr, "ERROR: unsupported argument: \"%s\".\n", argv[1]);
278 exit(EXIT_FAILURE);
279 }
280 }
281
282 unsetenv("HOME");
283 unsetenv("XDG_CONFIG_HOME");
284 unsetenv("XDG_CONFIG_DIR");
285 setenv("XKB_CONFIG_EXTRA_PATH", "¡SKIP!", 1);
286
287 char * const xkb_root = test_get_path("");
288 assert(xkb_root);
289 setenv("XKB_CONFIG_ROOT", xkb_root, 1);
290
291 test_layouts(xkb_root, update_output_files);
292 test_options(xkb_root, update_output_files);
293
294 free(xkb_root);
295 return EXIT_SUCCESS;
296 }