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;
49 /* xkbcomp needs to assign sequential IDs to XkbFile's it creates. */
52 struct atom_table *atom_table;
56 * Append one directory to the context's include path.
59 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
65 err = stat(path, &stat_buf);
68 if (!S_ISDIR(stat_buf.st_mode))
71 #if defined(HAVE_EACCESS)
72 if (eaccess(path, R_OK | X_OK) != 0)
74 #elif defined(HAVE_EUIDACCESS)
75 if (euidaccess(path, R_OK | X_OK) != 0)
83 darray_append(ctx->includes, tmp);
88 * Append the default include directories to the context.
91 xkb_context_include_path_append_default(struct xkb_context *ctx)
98 ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
100 home = getenv("HOME");
103 err = asprintf(&user_path, "%s/.xkb", home);
106 ret |= xkb_context_include_path_append(ctx, user_path);
113 * Remove all entries in the context's include path.
116 xkb_context_include_path_clear(struct xkb_context *ctx)
120 darray_foreach(path, ctx->includes)
123 darray_free(ctx->includes);
127 * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
130 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
132 xkb_context_include_path_clear(ctx);
133 return xkb_context_include_path_append_default(ctx);
137 * Returns the number of entries in the context's include path.
139 XKB_EXPORT unsigned int
140 xkb_context_num_include_paths(struct xkb_context *ctx)
142 return darray_size(ctx->includes);
146 * Returns the given entry in the context's include path, or NULL if an
147 * invalid index is passed.
149 XKB_EXPORT const char *
150 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
152 if (idx >= xkb_context_num_include_paths(ctx))
155 return darray_item(ctx->includes, idx);
159 xkb_context_take_file_id(struct xkb_context *ctx)
161 return ctx->file_id++;
165 * Take a new reference on the context.
167 XKB_EXPORT struct xkb_context *
168 xkb_context_ref(struct xkb_context *ctx)
175 * Drop an existing reference on the context, and free it if the refcnt is
179 xkb_context_unref(struct xkb_context *ctx)
181 if (--ctx->refcnt > 0)
184 xkb_context_include_path_clear(ctx);
185 atom_table_free(ctx->atom_table);
190 priority_to_prefix(int priority)
204 return "Internal error:";
210 ATTR_PRINTF(3, 0) static void
211 default_log_fn(struct xkb_context *ctx, int priority,
212 const char *fmt, va_list args)
214 const char *prefix = priority_to_prefix(priority);
217 fprintf(stderr, "%-15s", prefix);
218 vfprintf(stderr, fmt, args);
222 log_priority(const char *priority) {
227 prio = strtol(priority, &endptr, 10);
228 if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
230 if (strncasecmp(priority, "err", 3) == 0)
232 if (strncasecmp(priority, "warn", 4) == 0)
234 if (strncasecmp(priority, "info", 4) == 0)
236 if (strncasecmp(priority, "debug", 5) == 0)
243 log_verbosity(const char *verbosity) {
248 v = strtol(verbosity, &endptr, 10);
256 * Create a new context.
258 XKB_EXPORT struct xkb_context *
259 xkb_context_new(enum xkb_context_flags flags)
262 struct xkb_context *ctx = calloc(1, sizeof(*ctx));
268 ctx->log_fn = default_log_fn;
269 ctx->log_priority = LOG_ERR;
270 ctx->log_verbosity = 0;
272 /* Environment overwrites defaults. */
273 env = getenv("XKB_LOG");
275 xkb_set_log_priority(ctx, log_priority(env));
277 env = getenv("XKB_VERBOSITY");
279 xkb_set_log_verbosity(ctx, log_verbosity(env));
281 if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
282 !xkb_context_include_path_append_default(ctx)) {
283 log_err(ctx, "failed to add default include path %s\n",
284 DFLT_XKB_CONFIG_ROOT);
285 xkb_context_unref(ctx);
289 ctx->atom_table = atom_table_new();
290 if (!ctx->atom_table) {
291 xkb_context_unref(ctx);
299 xkb_atom_intern(struct xkb_context *ctx, const char *string)
301 return atom_intern(ctx->atom_table, string, false);
305 xkb_atom_steal(struct xkb_context *ctx, char *string)
307 return atom_intern(ctx->atom_table, string, true);
311 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
313 return atom_strdup(ctx->atom_table, atom);
317 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
319 return atom_text(ctx->atom_table, atom);
323 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...)
328 ctx->log_fn(ctx, priority, fmt, args);
333 xkb_set_log_fn(struct xkb_context *ctx,
334 void (*log_fn)(struct xkb_context *ctx, int priority,
335 const char *fmt, va_list args))
337 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
341 xkb_get_log_priority(struct xkb_context *ctx)
343 return ctx->log_priority;
347 xkb_set_log_priority(struct xkb_context *ctx, int priority)
349 ctx->log_priority = priority;
353 xkb_get_log_verbosity(struct xkb_context *ctx)
355 return ctx->log_verbosity;
359 xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity)
361 ctx->log_verbosity = verbosity;
365 xkb_get_user_data(struct xkb_context *ctx)
368 return ctx->user_data;
373 xkb_set_user_data(struct xkb_context *ctx, void *user_data)
375 ctx->user_data = user_data;