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)
97 (void) xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
99 home = getenv("HOME");
102 err = asprintf(&user_path, "%s/.xkb", home);
105 (void) xkb_context_include_path_append(ctx, user_path);
112 * Remove all entries in the context's include path.
115 xkb_context_include_path_clear(struct xkb_context *ctx)
119 darray_foreach(path, ctx->includes)
122 darray_free(ctx->includes);
126 * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
129 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
131 xkb_context_include_path_clear(ctx);
132 return xkb_context_include_path_append_default(ctx);
136 * Returns the number of entries in the context's include path.
138 XKB_EXPORT unsigned int
139 xkb_context_num_include_paths(struct xkb_context *ctx)
141 return darray_size(ctx->includes);
145 * Returns the given entry in the context's include path, or NULL if an
146 * invalid index is passed.
148 XKB_EXPORT const char *
149 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
151 if (idx >= xkb_context_num_include_paths(ctx))
154 return darray_item(ctx->includes, idx);
158 xkb_context_take_file_id(struct xkb_context *ctx)
160 return ctx->file_id++;
164 * Take a new reference on the context.
166 XKB_EXPORT struct xkb_context *
167 xkb_context_ref(struct xkb_context *ctx)
174 * Drop an existing reference on the context, and free it if the refcnt is
178 xkb_context_unref(struct xkb_context *ctx)
180 if (--ctx->refcnt > 0)
183 xkb_context_include_path_clear(ctx);
184 atom_table_free(ctx->atom_table);
189 priority_to_prefix(int priority)
203 return "Internal error:";
209 ATTR_PRINTF(3, 0) static void
210 default_log_fn(struct xkb_context *ctx, int priority,
211 const char *fmt, va_list args)
213 const char *prefix = priority_to_prefix(priority);
216 fprintf(stderr, "%-15s", prefix);
217 vfprintf(stderr, fmt, args);
221 log_priority(const char *priority) {
226 prio = strtol(priority, &endptr, 10);
227 if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
229 if (strncasecmp(priority, "err", 3) == 0)
231 if (strncasecmp(priority, "warn", 4) == 0)
233 if (strncasecmp(priority, "info", 4) == 0)
235 if (strncasecmp(priority, "debug", 5) == 0)
242 log_verbosity(const char *verbosity) {
247 v = strtol(verbosity, &endptr, 10);
255 * Create a new context.
257 XKB_EXPORT struct xkb_context *
258 xkb_context_new(enum xkb_context_flags flags)
261 struct xkb_context *ctx = calloc(1, sizeof(*ctx));
267 ctx->log_fn = default_log_fn;
268 ctx->log_priority = LOG_ERR;
269 ctx->log_verbosity = 0;
271 /* Environment overwrites defaults. */
272 env = getenv("XKB_LOG");
274 xkb_set_log_priority(ctx, log_priority(env));
276 env = getenv("XKB_VERBOSITY");
278 xkb_set_log_verbosity(ctx, log_verbosity(env));
280 if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
281 !xkb_context_include_path_append_default(ctx)) {
282 xkb_context_unref(ctx);
286 ctx->atom_table = atom_table_new();
287 if (!ctx->atom_table) {
288 xkb_context_unref(ctx);
296 xkb_atom_intern(struct xkb_context *ctx, const char *string)
298 return atom_intern(ctx->atom_table, string);
302 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
304 return atom_strdup(ctx->atom_table, atom);
308 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
310 return atom_text(ctx->atom_table, atom);
314 xkb_log(struct xkb_context *ctx, int priority, const char *fmt, ...)
319 /* NOTE: This test will be removed in a few commits. */
321 ctx->log_fn(ctx, priority, fmt, args);
323 default_log_fn(ctx, priority, fmt, args);
328 xkb_set_log_fn(struct xkb_context *ctx,
329 void (*log_fn)(struct xkb_context *ctx, int priority,
330 const char *fmt, va_list args))
332 ctx->log_fn = log_fn;
336 xkb_get_log_priority(struct xkb_context *ctx)
338 return ctx->log_priority;
342 xkb_set_log_priority(struct xkb_context *ctx, int priority)
344 ctx->log_priority = priority;
348 xkb_get_log_verbosity(struct xkb_context *ctx)
350 return ctx->log_verbosity;
354 xkb_set_log_verbosity(struct xkb_context *ctx, int verbosity)
356 ctx->log_verbosity = verbosity;
360 xkb_get_user_data(struct xkb_context *ctx)
363 return ctx->user_data;
368 xkb_set_user_data(struct xkb_context *ctx, void *user_data)
370 ctx->user_data = user_data;