Keysyms: improve generator (#364)
[platform/upstream/libxkbcommon.git] / src / context.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2012 Ran Benita
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * 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
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Author: Daniel Stone <daniel@fooishbar.org>
25  */
26
27 #include "config.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32
33 #include "xkbcommon/xkbcommon.h"
34 #include "utils.h"
35 #include "context.h"
36
37
38 /**
39  * Append one directory to the context's include path.
40  */
41 XKB_EXPORT int
42 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
43 {
44     struct stat stat_buf;
45     int err = ENOMEM;
46     char *tmp;
47
48     tmp = strdup(path);
49     if (!tmp)
50         goto err;
51
52     err = stat(path, &stat_buf);
53     if (err != 0) {
54         err = errno;
55         goto err;
56     }
57     if (!S_ISDIR(stat_buf.st_mode)) {
58         err = ENOTDIR;
59         goto err;
60     }
61
62     if (!check_eaccess(path, R_OK | X_OK)) {
63         err = EACCES;
64         goto err;
65     }
66
67     darray_append(ctx->includes, tmp);
68     log_dbg(ctx, "Include path added: %s\n", tmp);
69
70     return 1;
71
72 err:
73     darray_append(ctx->failed_includes, tmp);
74     log_dbg(ctx, "Include path failed: %s (%s)\n", tmp, strerror(err));
75     return 0;
76 }
77
78 const char *
79 xkb_context_include_path_get_extra_path(struct xkb_context *ctx)
80 {
81     const char *extra = xkb_context_getenv(ctx, "XKB_CONFIG_EXTRA_PATH");
82     return extra ? extra : DFLT_XKB_CONFIG_EXTRA_PATH;
83 }
84
85 const char *
86 xkb_context_include_path_get_system_path(struct xkb_context *ctx)
87 {
88     const char *root = xkb_context_getenv(ctx, "XKB_CONFIG_ROOT");
89     return root ? root : DFLT_XKB_CONFIG_ROOT;
90 }
91
92 /**
93  * Append the default include directories to the context.
94  */
95 XKB_EXPORT int
96 xkb_context_include_path_append_default(struct xkb_context *ctx)
97 {
98     const char *home, *xdg, *root, *extra;
99     char *user_path;
100     int ret = 0;
101
102     home = xkb_context_getenv(ctx, "HOME");
103
104     xdg = xkb_context_getenv(ctx, "XDG_CONFIG_HOME");
105     if (xdg != NULL) {
106         user_path = asprintf_safe("%s/xkb", xdg);
107         if (user_path) {
108             ret |= xkb_context_include_path_append(ctx, user_path);
109             free(user_path);
110         }
111     } else if (home != NULL) {
112         /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
113         user_path = asprintf_safe("%s/.config/xkb", home);
114         if (user_path) {
115             ret |= xkb_context_include_path_append(ctx, user_path);
116             free(user_path);
117         }
118     }
119
120     if (home != NULL) {
121         user_path = asprintf_safe("%s/.xkb", home);
122         if (user_path) {
123             ret |= xkb_context_include_path_append(ctx, user_path);
124             free(user_path);
125         }
126     }
127
128     extra = xkb_context_include_path_get_extra_path(ctx);
129     ret |= xkb_context_include_path_append(ctx, extra);
130     root = xkb_context_include_path_get_system_path(ctx);
131     ret |= xkb_context_include_path_append(ctx, root);
132
133     return ret;
134 }
135
136 /**
137  * Remove all entries in the context's include path.
138  */
139 XKB_EXPORT void
140 xkb_context_include_path_clear(struct xkb_context *ctx)
141 {
142     char **path;
143
144     darray_foreach(path, ctx->includes)
145         free(*path);
146     darray_free(ctx->includes);
147
148     darray_foreach(path, ctx->failed_includes)
149         free(*path);
150     darray_free(ctx->failed_includes);
151 }
152
153 /**
154  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
155  */
156 XKB_EXPORT int
157 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
158 {
159     xkb_context_include_path_clear(ctx);
160     return xkb_context_include_path_append_default(ctx);
161 }
162
163 /**
164  * Returns the number of entries in the context's include path.
165  */
166 XKB_EXPORT unsigned int
167 xkb_context_num_include_paths(struct xkb_context *ctx)
168 {
169     return darray_size(ctx->includes);
170 }
171
172 /**
173  * Returns the given entry in the context's include path, or NULL if an
174  * invalid index is passed.
175  */
176 XKB_EXPORT const char *
177 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
178 {
179     if (idx >= xkb_context_num_include_paths(ctx))
180         return NULL;
181
182     return darray_item(ctx->includes, idx);
183 }
184
185 /**
186  * Take a new reference on the context.
187  */
188 XKB_EXPORT struct xkb_context *
189 xkb_context_ref(struct xkb_context *ctx)
190 {
191     ctx->refcnt++;
192     return ctx;
193 }
194
195 /**
196  * Drop an existing reference on the context, and free it if the refcnt is
197  * now 0.
198  */
199 XKB_EXPORT void
200 xkb_context_unref(struct xkb_context *ctx)
201 {
202     if (!ctx || --ctx->refcnt > 0)
203         return;
204
205     free(ctx->x11_atom_cache);
206     xkb_context_include_path_clear(ctx);
207     atom_table_free(ctx->atom_table);
208     free(ctx);
209 }
210
211 static const char *
212 log_level_to_prefix(enum xkb_log_level level)
213 {
214     switch (level) {
215     case XKB_LOG_LEVEL_DEBUG:
216         return "xkbcommon: DEBUG: ";
217     case XKB_LOG_LEVEL_INFO:
218         return "xkbcommon: INFO: ";
219     case XKB_LOG_LEVEL_WARNING:
220         return "xkbcommon: WARNING: ";
221     case XKB_LOG_LEVEL_ERROR:
222         return "xkbcommon: ERROR: ";
223     case XKB_LOG_LEVEL_CRITICAL:
224         return "xkbcommon: CRITICAL: ";
225     default:
226         return NULL;
227     }
228 }
229
230 ATTR_PRINTF(3, 0) static void
231 default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
232                const char *fmt, va_list args)
233 {
234     const char *prefix = log_level_to_prefix(level);
235
236     if (prefix)
237         fprintf(stderr, "%s", prefix);
238     vfprintf(stderr, fmt, args);
239 }
240
241 static enum xkb_log_level
242 log_level(const char *level) {
243     char *endptr;
244     enum xkb_log_level lvl;
245
246     errno = 0;
247     lvl = strtol(level, &endptr, 10);
248     if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
249         return lvl;
250     if (istreq_prefix("crit", level))
251         return XKB_LOG_LEVEL_CRITICAL;
252     if (istreq_prefix("err", level))
253         return XKB_LOG_LEVEL_ERROR;
254     if (istreq_prefix("warn", level))
255         return XKB_LOG_LEVEL_WARNING;
256     if (istreq_prefix("info", level))
257         return XKB_LOG_LEVEL_INFO;
258     if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
259         return XKB_LOG_LEVEL_DEBUG;
260
261     return XKB_LOG_LEVEL_ERROR;
262 }
263
264 static int
265 log_verbosity(const char *verbosity) {
266     char *endptr;
267     int v;
268
269     errno = 0;
270     v = strtol(verbosity, &endptr, 10);
271     if (errno == 0)
272         return v;
273
274     return 0;
275 }
276
277 /**
278  * Create a new context.
279  */
280 XKB_EXPORT struct xkb_context *
281 xkb_context_new(enum xkb_context_flags flags)
282 {
283     const char *env;
284     struct xkb_context *ctx = calloc(1, sizeof(*ctx));
285
286     if (!ctx)
287         return NULL;
288
289     ctx->refcnt = 1;
290     ctx->log_fn = default_log_fn;
291     ctx->log_level = XKB_LOG_LEVEL_ERROR;
292     ctx->log_verbosity = 0;
293     ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
294     ctx->use_secure_getenv = !(flags & XKB_CONTEXT_NO_SECURE_GETENV);
295
296     /* Environment overwrites defaults. */
297     env = xkb_context_getenv(ctx, "XKB_LOG_LEVEL");
298     if (env)
299         xkb_context_set_log_level(ctx, log_level(env));
300
301     env = xkb_context_getenv(ctx, "XKB_LOG_VERBOSITY");
302     if (env)
303         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
304
305     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
306         !xkb_context_include_path_append_default(ctx)) {
307         log_err(ctx, "failed to add default include path %s\n",
308                 DFLT_XKB_CONFIG_ROOT);
309         xkb_context_unref(ctx);
310         return NULL;
311     }
312
313     ctx->atom_table = atom_table_new();
314     if (!ctx->atom_table) {
315         xkb_context_unref(ctx);
316         return NULL;
317     }
318
319     ctx->x11_atom_cache = NULL;
320
321     return ctx;
322 }
323
324 XKB_EXPORT void
325 xkb_context_set_log_fn(struct xkb_context *ctx,
326                        void (*log_fn)(struct xkb_context *ctx,
327                                       enum xkb_log_level level,
328                                       const char *fmt, va_list args))
329 {
330     ctx->log_fn = (log_fn ? log_fn : default_log_fn);
331 }
332
333 XKB_EXPORT enum xkb_log_level
334 xkb_context_get_log_level(struct xkb_context *ctx)
335 {
336     return ctx->log_level;
337 }
338
339 XKB_EXPORT void
340 xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
341 {
342     ctx->log_level = level;
343 }
344
345 XKB_EXPORT int
346 xkb_context_get_log_verbosity(struct xkb_context *ctx)
347 {
348     return ctx->log_verbosity;
349 }
350
351 XKB_EXPORT void
352 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
353 {
354     ctx->log_verbosity = verbosity;
355 }
356
357 XKB_EXPORT void *
358 xkb_context_get_user_data(struct xkb_context *ctx)
359 {
360     if (ctx)
361         return ctx->user_data;
362     return NULL;
363 }
364
365 XKB_EXPORT void
366 xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
367 {
368     ctx->user_data = user_data;
369 }