2 * Copyright © 2012 Intel Corporation
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:
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
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.
23 * Author: Daniel Stone <daniel@fooishbar.org>
26 #include <sys/types.h>
41 ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx, int priority,
42 const char *fmt, va_list args);
47 darray(char *) includes;
48 darray(char *) failed_includes;
50 /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
53 struct atom_table *atom_table;
57 * Append one directory to the context's include path.
60 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
70 err = stat(path, &stat_buf);
73 if (!S_ISDIR(stat_buf.st_mode))
76 #if defined(HAVE_EACCESS)
77 if (eaccess(path, R_OK | X_OK) != 0)
79 #elif defined(HAVE_EUIDACCESS)
80 if (euidaccess(path, R_OK | X_OK) != 0)
84 darray_append(ctx->includes, tmp);
88 darray_append(ctx->failed_includes, tmp);
93 * Append the default include directories to the context.
96 xkb_context_include_path_append_default(struct xkb_context *ctx)
103 ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
105 home = getenv("HOME");
108 err = asprintf(&user_path, "%s/.xkb", home);
111 ret |= xkb_context_include_path_append(ctx, user_path);
118 * Remove all entries in the context's include path.
121 xkb_context_include_path_clear(struct xkb_context *ctx)
125 darray_foreach(path, ctx->includes)
127 darray_free(ctx->includes);
129 darray_foreach(path, ctx->failed_includes)
131 darray_free(ctx->failed_includes);
135 * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
138 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
140 xkb_context_include_path_clear(ctx);
141 return xkb_context_include_path_append_default(ctx);
145 * Returns the number of entries in the context's include path.
147 XKB_EXPORT unsigned int
148 xkb_context_num_include_paths(struct xkb_context *ctx)
150 return darray_size(ctx->includes);
154 * Returns the given entry in the context's include path, or NULL if an
155 * invalid index is passed.
157 XKB_EXPORT const char *
158 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
160 if (idx >= xkb_context_num_include_paths(ctx))
163 return darray_item(ctx->includes, idx);
167 xkb_context_take_file_id(struct xkb_context *ctx)
169 return ctx->file_id++;
173 * Take a new reference on the context.
175 XKB_EXPORT struct xkb_context *
176 xkb_context_ref(struct xkb_context *ctx)
183 * Drop an existing reference on the context, and free it if the refcnt is
187 xkb_context_unref(struct xkb_context *ctx)
189 if (--ctx->refcnt > 0)
192 xkb_context_include_path_clear(ctx);
193 atom_table_free(ctx->atom_table);
198 priority_to_prefix(int priority)
212 return "Internal error:";
218 ATTR_PRINTF(3, 0) static void
219 default_log_fn(struct xkb_context *ctx, int priority,
220 const char *fmt, va_list args)
222 const char *prefix = priority_to_prefix(priority);
225 fprintf(stderr, "%-15s", prefix);
226 vfprintf(stderr, fmt, args);
230 log_priority(const char *priority) {
235 prio = strtol(priority, &endptr, 10);
236 if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
238 if (strncasecmp(priority, "err", 3) == 0)
240 if (strncasecmp(priority, "warn", 4) == 0)
242 if (strncasecmp(priority, "info", 4) == 0)
244 if (strncasecmp(priority, "debug", 5) == 0)
251 log_verbosity(const char *verbosity) {
256 v = strtol(verbosity, &endptr, 10);
264 * Create a new context.
266 XKB_EXPORT struct xkb_context *
267 xkb_context_new(enum xkb_context_flags flags)
270 struct xkb_context *ctx = calloc(1, sizeof(*ctx));
276 ctx->log_fn = default_log_fn;
277 ctx->log_priority = LOG_ERR;
278 ctx->log_verbosity = 0;
280 /* Environment overwrites defaults. */
281 env = getenv("XKB_LOG");
283 xkb_set_log_priority(ctx, log_priority(env));
285 env = getenv("XKB_VERBOSITY");
287 xkb_set_log_verbosity(ctx, log_verbosity(env));
289 if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
290 !xkb_context_include_path_append_default(ctx)) {
291 log_err(ctx, "failed to add default include path %s\n",
292 DFLT_XKB_CONFIG_ROOT);
293 xkb_context_unref(ctx);
297 ctx->atom_table = atom_table_new();
298 if (!ctx->atom_table) {
299 xkb_context_unref(ctx);
307 xkb_atom_intern(struct xkb_context *ctx, const char *string)
309 return atom_intern(ctx->atom_table, string, false);
313 xkb_atom_steal(struct xkb_context *ctx, char *string)
315 return atom_intern(ctx->atom_table, string, true);
319 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
321 return atom_strdup(ctx->atom_table, atom);
325 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
327 return atom_text(ctx->atom_table, atom);
331 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...)
336 ctx->log_fn(ctx, priority, fmt, args);
341 xkb_set_log_fn(struct xkb_context *ctx,
342 void (*log_fn)(struct xkb_context *ctx, int priority,
343 const char *fmt, va_list args))
345 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
349 xkb_get_log_priority(struct xkb_context *ctx)
351 return ctx->log_priority;
355 xkb_set_log_priority(struct xkb_context *ctx, int priority)
357 ctx->log_priority = priority;
361 xkb_get_log_verbosity(struct xkb_context *ctx)
363 return ctx->log_verbosity;
367 xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity)
369 ctx->log_verbosity = verbosity;
373 xkb_get_user_data(struct xkb_context *ctx)
376 return ctx->user_data;
381 xkb_set_user_data(struct xkb_context *ctx, void *user_data)
383 ctx->user_data = user_data;