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>
29 #include <sys/types.h>
34 #include "xkbcommon/xkbcommon.h"
39 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
41 return darray_size(ctx->failed_includes);
45 xkb_context_failed_include_path_get(struct xkb_context *ctx,
48 if (idx >= xkb_context_num_failed_include_paths(ctx))
51 return darray_item(ctx->failed_includes, idx);
55 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
57 return atom_intern(ctx->atom_table, string, strlen(string), false);
61 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
63 return atom_intern(ctx->atom_table, string, len, true);
67 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
69 return atom_text(ctx->atom_table, atom);
73 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
78 if (ctx->log_level < level || ctx->log_verbosity < verbosity)
82 ctx->log_fn(ctx, level, fmt, args);
87 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
91 if (size >= sizeof(ctx->text_buffer))
94 if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
97 rtrn = &ctx->text_buffer[ctx->text_next];
98 ctx->text_next += size;
103 #ifndef DEFAULT_XKB_VARIANT
104 #define DEFAULT_XKB_VARIANT NULL
107 #ifndef DEFAULT_XKB_OPTIONS
108 #define DEFAULT_XKB_OPTIONS NULL
112 xkb_context_get_default_rules(struct xkb_context *ctx)
114 const char *env = NULL;
116 if (ctx->use_environment_names)
117 env = secure_getenv("XKB_DEFAULT_RULES");
119 return env ? env : DEFAULT_XKB_RULES;
123 xkb_context_get_default_model(struct xkb_context *ctx)
125 const char *env = NULL;
127 if (ctx->use_environment_names)
128 env = secure_getenv("XKB_DEFAULT_MODEL");
130 return env ? env : DEFAULT_XKB_MODEL;
134 xkb_context_get_default_layout(struct xkb_context *ctx)
136 const char *env = NULL;
138 if (ctx->use_environment_names)
139 env = secure_getenv("XKB_DEFAULT_LAYOUT");
141 return env ? env : DEFAULT_XKB_LAYOUT;
145 xkb_context_get_default_variant(struct xkb_context *ctx)
147 const char *env = NULL;
148 const char *layout = secure_getenv("XKB_DEFAULT_LAYOUT");
150 /* We don't want to inherit the variant if they haven't also set a
151 * layout, since they're so closely paired. */
152 if (layout && ctx->use_environment_names)
153 env = secure_getenv("XKB_DEFAULT_VARIANT");
155 return env ? env : DEFAULT_XKB_VARIANT;
159 xkb_context_get_default_options(struct xkb_context *ctx)
161 const char *env = NULL;
163 if (ctx->use_environment_names)
164 env = secure_getenv("XKB_DEFAULT_OPTIONS");
166 return env ? env : DEFAULT_XKB_OPTIONS;
170 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
171 struct xkb_rule_names *rmlvo)
173 if (isempty(rmlvo->rules))
174 rmlvo->rules = xkb_context_get_default_rules(ctx);
175 if (isempty(rmlvo->model))
176 rmlvo->model = xkb_context_get_default_model(ctx);
177 /* Layout and variant are tied together, so don't try to use one from
178 * the caller and one from the environment. */
179 if (isempty(rmlvo->layout)) {
180 rmlvo->layout = xkb_context_get_default_layout(ctx);
181 rmlvo->variant = xkb_context_get_default_variant(ctx);
183 /* Options can be empty, so respect that if passed in. */
184 if (rmlvo->options == NULL)
185 rmlvo->options = xkb_context_get_default_options(ctx);