2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2012 Ran Benita
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:
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
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.
24 * Author: Daniel Stone <daniel@fooishbar.org>
27 #include <sys/types.h>
32 #include "xkbcommon/xkbcommon.h"
39 ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
40 enum xkb_log_level level,
41 const char *fmt, va_list args);
42 enum xkb_log_level log_level;
46 struct xkb_rule_names names_dflt;
48 darray(char *) includes;
49 darray(char *) failed_includes;
51 struct atom_table *atom_table;
53 /* Buffer for the *Text() functions. */
54 char text_buffer[2048];
57 unsigned int use_environment_names : 1;
61 * Append one directory to the context's include path.
64 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
74 err = stat(path, &stat_buf);
77 if (!S_ISDIR(stat_buf.st_mode))
80 #if defined(HAVE_EACCESS)
81 if (eaccess(path, R_OK | X_OK) != 0)
83 #elif defined(HAVE_EUIDACCESS)
84 if (euidaccess(path, R_OK | X_OK) != 0)
88 darray_append(ctx->includes, tmp);
92 darray_append(ctx->failed_includes, tmp);
97 * Append the default include directories to the context.
100 xkb_context_include_path_append_default(struct xkb_context *ctx)
107 ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
109 home = getenv("HOME");
112 err = asprintf(&user_path, "%s/.xkb", home);
115 ret |= xkb_context_include_path_append(ctx, user_path);
122 * Remove all entries in the context's include path.
125 xkb_context_include_path_clear(struct xkb_context *ctx)
129 darray_foreach(path, ctx->includes)
131 darray_free(ctx->includes);
133 darray_foreach(path, ctx->failed_includes)
135 darray_free(ctx->failed_includes);
139 * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
142 xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
144 xkb_context_include_path_clear(ctx);
145 return xkb_context_include_path_append_default(ctx);
149 * Returns the number of entries in the context's include path.
151 XKB_EXPORT unsigned int
152 xkb_context_num_include_paths(struct xkb_context *ctx)
154 return darray_size(ctx->includes);
158 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
160 return darray_size(ctx->failed_includes);
164 * Returns the given entry in the context's include path, or NULL if an
165 * invalid index is passed.
167 XKB_EXPORT const char *
168 xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
170 if (idx >= xkb_context_num_include_paths(ctx))
173 return darray_item(ctx->includes, idx);
177 xkb_context_failed_include_path_get(struct xkb_context *ctx,
180 if (idx >= xkb_context_num_failed_include_paths(ctx))
183 return darray_item(ctx->failed_includes, idx);
187 * Take a new reference on the context.
189 XKB_EXPORT struct xkb_context *
190 xkb_context_ref(struct xkb_context *ctx)
197 * Drop an existing reference on the context, and free it if the refcnt is
201 xkb_context_unref(struct xkb_context *ctx)
203 if (!ctx || --ctx->refcnt > 0)
206 xkb_context_include_path_clear(ctx);
207 atom_table_free(ctx->atom_table);
212 log_level_to_prefix(enum xkb_log_level 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: ";
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)
234 const char *prefix = log_level_to_prefix(level);
237 fprintf(stderr, "%s", prefix);
238 vfprintf(stderr, fmt, args);
241 static enum xkb_log_level
242 log_level(const char *level) {
244 enum xkb_log_level lvl;
247 lvl = strtol(level, &endptr, 10);
248 if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
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;
261 return XKB_LOG_LEVEL_ERROR;
265 log_verbosity(const char *verbosity) {
270 v = strtol(verbosity, &endptr, 10);
277 #ifndef DEFAULT_XKB_VARIANT
278 #define DEFAULT_XKB_VARIANT NULL
281 #ifndef DEFAULT_XKB_OPTIONS
282 #define DEFAULT_XKB_OPTIONS NULL
286 * Create a new context.
288 XKB_EXPORT struct xkb_context *
289 xkb_context_new(enum xkb_context_flags flags)
292 struct xkb_context *ctx = calloc(1, sizeof(*ctx));
298 ctx->log_fn = default_log_fn;
299 ctx->log_level = XKB_LOG_LEVEL_ERROR;
300 ctx->log_verbosity = 0;
302 /* Environment overwrites defaults. */
303 env = getenv("XKB_LOG_LEVEL");
305 xkb_context_set_log_level(ctx, log_level(env));
307 env = getenv("XKB_LOG_VERBOSITY");
309 xkb_context_set_log_verbosity(ctx, log_verbosity(env));
311 if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
312 !xkb_context_include_path_append_default(ctx)) {
313 log_err(ctx, "failed to add default include path %s\n",
314 DFLT_XKB_CONFIG_ROOT);
315 xkb_context_unref(ctx);
319 ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
321 ctx->atom_table = atom_table_new();
322 if (!ctx->atom_table) {
323 xkb_context_unref(ctx);
331 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
333 return atom_lookup(ctx->atom_table, string, strlen(string));
337 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
339 return atom_intern(ctx->atom_table, string, len, false);
343 xkb_atom_steal(struct xkb_context *ctx, char *string)
345 return atom_intern(ctx->atom_table, string, strlen(string), true);
349 xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
351 return atom_strdup(ctx->atom_table, atom);
355 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
357 return atom_text(ctx->atom_table, atom);
361 xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
362 const char *fmt, ...)
367 ctx->log_fn(ctx, level, fmt, args);
372 xkb_context_set_log_fn(struct xkb_context *ctx,
373 void (*log_fn)(struct xkb_context *ctx,
374 enum xkb_log_level level,
375 const char *fmt, va_list args))
377 ctx->log_fn = (log_fn ? log_fn : default_log_fn);
380 XKB_EXPORT enum xkb_log_level
381 xkb_context_get_log_level(struct xkb_context *ctx)
383 return ctx->log_level;
387 xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
389 ctx->log_level = level;
393 xkb_context_get_log_verbosity(struct xkb_context *ctx)
395 return ctx->log_verbosity;
399 xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
401 ctx->log_verbosity = verbosity;
405 xkb_context_get_user_data(struct xkb_context *ctx)
408 return ctx->user_data;
413 xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
415 ctx->user_data = user_data;
419 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
423 if (size >= sizeof(ctx->text_buffer))
426 if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
429 rtrn = &ctx->text_buffer[ctx->text_next];
430 ctx->text_next += size;
436 xkb_context_get_default_rules(struct xkb_context *ctx)
438 const char *env = NULL;
440 if (ctx->use_environment_names)
441 env = getenv("XKB_DEFAULT_RULES");
443 return env ? env : DEFAULT_XKB_RULES;
447 xkb_context_get_default_model(struct xkb_context *ctx)
449 const char *env = NULL;
451 if (ctx->use_environment_names)
452 env = getenv("XKB_DEFAULT_MODEL");
454 return env ? env : DEFAULT_XKB_MODEL;
458 xkb_context_get_default_layout(struct xkb_context *ctx)
460 const char *env = NULL;
462 if (ctx->use_environment_names)
463 env = getenv("XKB_DEFAULT_LAYOUT");
465 return env ? env : DEFAULT_XKB_LAYOUT;
469 xkb_context_get_default_variant(struct xkb_context *ctx)
471 const char *env = NULL;
472 const char *layout = getenv("XKB_DEFAULT_VARIANT");
474 /* We don't want to inherit the variant if they haven't also set a
475 * layout, since they're so closely paired. */
476 if (layout && ctx->use_environment_names)
477 env = getenv("XKB_DEFAULT_VARIANT");
479 return env ? env : DEFAULT_XKB_VARIANT;
483 xkb_context_get_default_options(struct xkb_context *ctx)
485 const char *env = NULL;
487 if (ctx->use_environment_names)
488 env = getenv("XKB_DEFAULT_OPTIONS");
490 return env ? env : DEFAULT_XKB_OPTIONS;