Commit 87b0765fde631036d193775028389ddfdf859b2e

Ran Benita 2018-08-21T09:05:09

Merge pull request #68 from whot/wip/xkeyboard-config-tester xkeyboard-config combination tester

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
diff --git a/Makefile.am b/Makefile.am
index a7492d0..3f53b03 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -219,6 +219,7 @@ build_run_tests = \
 	test/compose
 build_only_tests = \
 	test/rmlvo-to-kccgst \
+	test/rmlvo-to-keymap \
 	test/print-compiled-keymap
 run_only_tests = \
 	test/symbols-leak-test.bash
@@ -240,6 +241,7 @@ test_state_LDADD = $(TESTS_LDADD)
 test_keyseq_LDADD = $(TESTS_LDADD)
 test_rulescomp_LDADD = $(TESTS_LDADD)
 test_rmlvo_to_kccgst_LDADD = $(TESTS_LDADD)
+test_rmlvo_to_keymap_LDADD = $(TESTS_LDADD)
 test_print_compiled_keymap_LDADD = $(TESTS_LDADD)
 test_compose_LDADD = $(TESTS_LDADD) $(RT_LIBS)
 
diff --git a/meson.build b/meson.build
index 92a7cb8..4e22025 100644
--- a/meson.build
+++ b/meson.build
@@ -392,6 +392,7 @@ executable('fuzz-compose', 'fuzz/compose/target.c', dependencies: test_dep)
 
 # Demo programs.
 executable('rmlvo-to-kccgst', 'test/rmlvo-to-kccgst.c', dependencies: test_dep)
+executable('rmlvo-to-keymap', 'test/rmlvo-to-keymap.c', dependencies: test_dep)
 executable('print-compiled-keymap', 'test/print-compiled-keymap.c', dependencies: test_dep)
 if cc.has_header('linux/input.h')
     executable('interactive-evdev', 'test/interactive-evdev.c', dependencies: test_dep)
@@ -428,6 +429,15 @@ You can disable the Wayland demo programs with -Denable-wayland=false.''')
     executable('interactive-wayland', 'test/interactive-wayland.c', xdg_shell_sources, dependencies: [test_dep, wayland_client_dep])
 endif
 
+# xkeyboard-config "verifier"
+xkct_config = configuration_data()
+xkct_config.set('MESON_BUILD_ROOT', meson.build_root())
+xkct_config.set('XKB_CONFIG_ROOT', XKBCONFIGROOT)
+configure_file(input: 'test/xkeyboard-config-test.py.in',
+               output: 'xkeyboard-config-test',
+               configuration: xkct_config,
+               install: false)
+
 
 # Benchmarks.
 libxkbcommon_bench_internal = static_library(
diff --git a/test/.gitignore b/test/.gitignore
index 1be56ad..def76f0 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -13,6 +13,7 @@ keyseq
 log
 interactive-evdev
 rmlvo-to-kccgst
+rmlvo-to-keymap
 print-compiled-keymap
 atom
 x11
diff --git a/test/common.c b/test/common.c
index 1ee34e3..c909707 100644
--- a/test/common.c
+++ b/test/common.c
@@ -161,6 +161,9 @@ test_get_path(const char *path_rel)
     size_t path_len;
     const char *srcdir = getenv("top_srcdir");
 
+    if (path_rel[0] == '/')
+        return strdup(path_rel);
+
     path_len = strlen(srcdir ? srcdir : ".") +
                strlen(path_rel ? path_rel : "") + 12;
     path = malloc(path_len);
diff --git a/test/print-compiled-keymap.c b/test/print-compiled-keymap.c
index 7e57fdd..6829eac 100644
--- a/test/print-compiled-keymap.c
+++ b/test/print-compiled-keymap.c
@@ -32,56 +32,32 @@ main(int argc, char *argv[])
     int opt;
     struct xkb_context *ctx;
     struct xkb_keymap *keymap;
-    const char *rules = NULL;
-    const char *model = NULL;
-    const char *layout = NULL;
-    const char *variant = NULL;
-    const char *options = NULL;
     const char *keymap_path = NULL;
     char *dump;
 
-    while ((opt = getopt(argc, argv, "r:m:l:v:o:k:h")) != -1) {
+    while ((opt = getopt(argc, argv, "h")) != -1) {
         switch (opt) {
-        case 'r':
-            rules = optarg;
-            break;
-        case 'm':
-            model = optarg;
-            break;
-        case 'l':
-            layout = optarg;
-            break;
-        case 'v':
-            variant = optarg;
-            break;
-        case 'o':
-            options = optarg;
-            break;
-        case 'k':
-            keymap_path = optarg;
-            break;
         case 'h':
         case '?':
-            fprintf(stderr, "Usage: %s [-r <rules>] [-m <model>] "
-                    "[-l <layout>] [-v <variant>] [-o <options>]\n",
-                    argv[0]);
-            fprintf(stderr, "   or: %s -k <path to keymap file>\n",
-                    argv[0]);
+            fprintf(stderr, "Usage: %s <path to keymap file>\n", argv[0]);
             exit(EXIT_FAILURE);
         }
     }
 
+    if (optind >= argc) {
+        fprintf(stderr, "Error: missing path to keymap file\n");
+        exit(EXIT_FAILURE);
+    }
+
+    keymap_path = argv[optind];
+
     ctx = test_get_context(0);
     if (!ctx) {
         fprintf(stderr, "Couldn't create xkb context\n");
         goto err_out;
     }
 
-    if (keymap_path)
-        keymap = test_compile_file(ctx, keymap_path);
-    else
-        keymap = test_compile_rules(ctx, rules, model, layout, variant,
-                                    options);
+    keymap = test_compile_file(ctx, keymap_path);
     if (!keymap) {
         fprintf(stderr, "Couldn't create xkb keymap\n");
         goto err_ctx;
diff --git a/test/rmlvo-to-keymap.c b/test/rmlvo-to-keymap.c
new file mode 100644
index 0000000..ed48e49
--- /dev/null
+++ b/test/rmlvo-to-keymap.c
@@ -0,0 +1,141 @@
+/*
+ * Copyright © 2018 Red Hat, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include <assert.h>
+#include <errno.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include "xkbcommon/xkbcommon.h"
+
+static bool print = false;
+
+static void
+usage(void)
+{
+    printf("Usage: %s [--print] [--rules <rules>] [--layout <layout>] [--variant <variant>] [--options <option>]\n",
+           program_invocation_short_name);
+    printf("This tool tests the compilation from RMLVO to a keymap.\n");
+    printf("--print  print the resulting keymap\n");
+}
+
+static bool
+parse_options(int argc, char **argv, struct xkb_rule_names *names)
+{
+    enum options {
+        OPT_PRINT,
+        OPT_RULES,
+        OPT_MODEL,
+        OPT_LAYOUT,
+        OPT_VARIANT,
+        OPT_OPTION,
+    };
+    static struct option opts[] = {
+        {"help",        no_argument,            0, 'h'},
+        {"print",       no_argument,            0, OPT_PRINT},
+        {"rules",       required_argument,      0, OPT_RULES},
+        {"model",       required_argument,      0, OPT_MODEL},
+        {"layout",      required_argument,      0, OPT_LAYOUT},
+        {"variant",     required_argument,      0, OPT_VARIANT},
+        {"options",     required_argument,      0, OPT_OPTION},
+        {0, 0, 0, 0},
+    };
+
+    while (1) {
+        int c;
+        int option_index = 0;
+        c = getopt_long(argc, argv, "h", opts, &option_index);
+        if (c == -1)
+            break;
+
+        switch (c) {
+        case 'h':
+            usage();
+            exit(0);
+        case OPT_PRINT:
+            print = true;
+            break;
+        case OPT_RULES:
+            names->rules = optarg;
+            break;
+        case OPT_MODEL:
+            names->model = optarg;
+            break;
+        case OPT_LAYOUT:
+            names->layout = optarg;
+            break;
+        case OPT_VARIANT:
+            names->variant = optarg;
+            break;
+        case OPT_OPTION:
+            names->options = optarg;
+            break;
+        default:
+            usage();
+            exit(1);
+        }
+
+    }
+
+    return true;
+}
+
+int
+main(int argc, char **argv)
+{
+    struct xkb_context *ctx;
+    struct xkb_keymap *keymap;
+    struct xkb_rule_names names = {
+        .rules = NULL,
+        .model = NULL,
+        .layout = NULL,
+        .variant = NULL,
+        .options = NULL,
+    };
+    int rc;
+
+    if (argc <= 1) {
+        usage();
+        return 1;
+    }
+
+    if (!parse_options(argc, argv, &names))
+        return 1;
+
+    ctx = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+    assert(ctx);
+
+    keymap = xkb_keymap_new_from_names(ctx, &names, XKB_KEYMAP_COMPILE_NO_FLAGS);
+    rc = (keymap == NULL);
+
+    if (rc == 0 && print)
+        printf("%s\n", xkb_keymap_get_as_string(keymap,
+                                                XKB_KEYMAP_FORMAT_TEXT_V1));
+
+    xkb_keymap_unref(keymap);
+    xkb_context_unref(ctx);
+
+    return rc;
+}
diff --git a/test/xkeyboard-config-test.py.in b/test/xkeyboard-config-test.py.in
new file mode 100755
index 0000000..345ebd5
--- /dev/null
+++ b/test/xkeyboard-config-test.py.in
@@ -0,0 +1,129 @@
+#!/usr/bin/env python
+import sys
+import subprocess
+import os
+import xml.etree.ElementTree as ET
+
+
+verbose = True
+
+DEFAULT_RULES_XML = '@XKB_CONFIG_ROOT@/rules/evdev.xml'
+
+# Meson needs to fill this in so we can call the tool in the buildir.
+EXTRA_PATH='@MESON_BUILD_ROOT@'
+os.environ['PATH'] = ':'.join([EXTRA_PATH, os.getenv('PATH')])
+
+
+# The function generating the progress bar (if any).
+progress_bar = lambda x, desc: x
+if os.isatty(sys.stdout.fileno()):
+    try:
+        from tqdm import tqdm
+        progress_bar = tqdm
+
+        verbose = False
+    except ImportError:
+        pass
+
+
+def xkbcommontool(r='evdev', m='pc105', l='us', v=None, o=None):
+    args = [
+        'rmlvo-to-keymap',
+        '--rules', r,
+        '--model', m,
+        '--layout', l,
+    ]
+    if v is not None:
+        args += ['--variant', v]
+    if o is not None:
+        args += ['--options', o]
+
+    if verbose:
+        print(':: {}'.format(' '.join(args)))
+
+    try:
+        output = subprocess.check_output(args, stderr=subprocess.STDOUT)
+        if verbose:
+            print(output.decode('utf-8'))
+    except subprocess.CalledProcessError as err:
+        print('ERROR: Failed to compile: {}'.format(' '.join(args)))
+        print(err.output.decode('utf-8'))
+        sys.exit(1)
+
+
+def xkbcomp(r='evdev', m='pc105', l='us', v='', o=''):
+    args = ['setxkbmap', '-print']
+    if r is not None:
+        args.append('-rules')
+        args.append('{}'.format(r))
+    if m is not None:
+        args.append('-model')
+        args.append('{}'.format(m))
+    if l is not None:
+        args.append('-layout')
+        args.append('{}'.format(l))
+    if o is not None:
+        args.append('-option')
+        args.append('{}'.format(o))
+
+    if verbose:
+        print(':: {}'.format(' '.join(args)))
+
+    try:
+        xkbcomp_args = ['xkbcomp', '-xkb', '-', '-']
+
+        setxkbmap = subprocess.Popen(args, stdout=subprocess.PIPE)
+        xkbcomp = subprocess.Popen(xkbcomp_args, stdin=setxkbmap.stdout,
+                             stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        setxkbmap.stdout.close()
+        stdout, stderr = xkbcomp.communicate()
+        if xkbcomp.returncode != 0:
+            print('ERROR: Failed to compile: {}'.format(' '.join(args)))
+        if xkbcomp.returncode != 0 or verbose:
+            print(stdout.decode('utf-8'))
+            print(stderr.decode('utf-8'))
+
+    # This catches setxkbmap errors.
+    except subprocess.CalledProcessError as err:
+        print('ERROR: Failed to compile: {}'.format(' '.join(args)))
+        print(err.output.decode('utf-8'))
+
+
+def parse(root):
+    layouts = root.findall('layoutList/layout')
+
+    options = [
+        e.text
+        for e in root.findall('optionList/group/option/configItem/name')
+    ]
+
+    # Switch this to xkbcomp if needed.
+    tool = xkbcommontool
+    # tool = xkbcomp
+
+    for l in progress_bar(layouts, 'layout '):
+        layout = l.find('configItem/name').text
+        tool(l=layout)
+
+        variants = l.findall('variantList/variant')
+        for v in progress_bar(variants, 'variant'):
+            variant = v.find('configItem/name').text
+            tool(l=layout, v=variant)
+
+            for option in progress_bar(options, 'option '):
+                tool(l=layout, v=variant, o=option)
+
+
+def main(args):
+    try:
+        path = args[1]
+    except IndexError:
+        path = DEFAULT_RULES_XML
+
+    with open(path) as f:
+        root = ET.fromstring(f.read())
+        parse(root)
+
+
+if __name__ == '__main__':
+    main(sys.argv)