Add xkb_map_mod_mask_remove_consumed
[platform/upstream/libxkbcommon.git] / test / common.c
1 /*
2  * Copyright 2009 Dan Nicholson
3  * Copyright © 2012 Daniel Stone
4  * Copyright © 2012 Ran Benita
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Except as contained in this notice, the names of the authors or their
24  * institutions shall not be used in advertising or otherwise to promote the
25  * sale, use or other dealings in this Software without prior written
26  * authorization from the authors.
27  */
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include "xkbcommon/xkbcommon.h"
40 #include "test.h"
41
42 const char *
43 test_get_path(const char *path_rel)
44 {
45     static char path[PATH_MAX];
46     const char *srcdir = getenv("srcdir");
47
48     snprintf(path, PATH_MAX - 1,
49              "%s/test/data/%s", srcdir ? srcdir : ".",
50              path_rel ? path_rel : "");
51
52     return path;
53 }
54
55 char *
56 test_read_file(const char *path_rel)
57 {
58     struct stat info;
59     char *ret, *tmp;
60     int fd, count, remaining;
61
62     fd = open(test_get_path(path_rel), O_RDONLY);
63     if (fd < 0)
64         return NULL;
65
66     if (fstat(fd, &info) != 0) {
67         close(fd);
68         return NULL;
69     }
70
71     ret = malloc(info.st_size + 1);
72     if (!ret) {
73         close(fd);
74         return NULL;
75     }
76
77     remaining = info.st_size;
78     tmp = ret;
79     while ((count = read(fd, tmp, remaining))) {
80         remaining -= count;
81         tmp += count;
82     }
83     ret[info.st_size] = '\0';
84     close(fd);
85
86     if (remaining != 0) {
87         free(ret);
88         return NULL;
89     }
90
91     return ret;
92 }
93
94 struct xkb_context *
95 test_get_context(void)
96 {
97     struct xkb_context *ctx = xkb_context_new(XKB_CONTEXT_NO_DEFAULT_INCLUDES);
98
99     if (!ctx)
100         return NULL;
101
102     xkb_context_include_path_append(ctx, test_get_path(""));
103
104     xkb_set_log_priority(ctx, XKB_LOG_LEVEL_DEBUG);
105     xkb_set_log_verbosity(ctx, 101);
106
107     return ctx;
108 }
109
110 struct xkb_keymap *
111 test_compile_file(struct xkb_context *context, const char *path_rel)
112 {
113     struct xkb_keymap *keymap;
114     FILE *file;
115     const char *path = test_get_path(path_rel);
116
117     file = fopen(path, "r");
118     if (!file) {
119         fprintf(stderr, "Failed to open path: %s\n", path);
120         return NULL;
121     }
122     assert(file != NULL);
123
124     keymap = xkb_map_new_from_file(context, file,
125                                    XKB_KEYMAP_FORMAT_TEXT_V1, 0);
126     fclose(file);
127
128     if (!keymap) {
129         fprintf(stderr, "Failed to compile path: %s\n", path);
130         return NULL;
131     }
132
133     fprintf(stderr, "Successfully compiled path: %s\n", path);
134
135     return keymap;
136 }
137
138 struct xkb_keymap *
139 test_compile_string(struct xkb_context *context, const char *string)
140 {
141     struct xkb_keymap *keymap;
142
143     keymap = xkb_map_new_from_string(context, string,
144                                      XKB_KEYMAP_FORMAT_TEXT_V1, 0);
145     if (!keymap) {
146         fprintf(stderr, "Failed to compile string\n");
147         return NULL;
148     }
149
150     return keymap;
151 }
152
153 struct xkb_keymap *
154 test_compile_rules(struct xkb_context *context, const char *rules,
155                    const char *model, const char *layout,
156                    const char *variant, const char *options)
157 {
158     struct xkb_keymap *keymap;
159     struct xkb_rule_names rmlvo = {
160         .rules = rules,
161         .model = model,
162         .layout = layout,
163         .variant = variant,
164         .options = options
165     };
166
167     keymap = xkb_map_new_from_names(context, &rmlvo, 0);
168     if (!keymap) {
169         fprintf(stderr,
170                 "Failed to compile RMLVO: '%s', '%s', '%s', '%s', '%s'\n",
171                 rules, model, layout, variant, options);
172         return NULL;
173     }
174
175     return keymap;
176 }