e94385f6492390e956e8db45084d00c132568d93
[platform/upstream/libxkbcommon.git] / src / context-priv.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  * Copyright © 2012 Ran Benita
4  *
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:
11  *
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
14  * Software.
15  *
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.
23  *
24  * Author: Daniel Stone <daniel@fooishbar.org>
25  */
26
27 #include "config.h"
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <errno.h>
32
33 #include "xkbcommon/xkbcommon.h"
34 #include "utils.h"
35 #include "context.h"
36
37 char *
38 xkb_context_getenv(struct xkb_context *ctx, const char *name)
39 {
40     if (ctx->use_secure_getenv) {
41         return secure_getenv(name);
42     } else {
43         return getenv(name);
44     }
45 }
46
47 unsigned int
48 xkb_context_num_failed_include_paths(struct xkb_context *ctx)
49 {
50     return darray_size(ctx->failed_includes);
51 }
52
53 const char *
54 xkb_context_failed_include_path_get(struct xkb_context *ctx,
55                                     unsigned int idx)
56 {
57     if (idx >= xkb_context_num_failed_include_paths(ctx))
58         return NULL;
59
60     return darray_item(ctx->failed_includes, idx);
61 }
62
63 xkb_atom_t
64 xkb_atom_lookup(struct xkb_context *ctx, const char *string)
65 {
66     return atom_intern(ctx->atom_table, string, strlen(string), false);
67 }
68
69 xkb_atom_t
70 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len)
71 {
72     return atom_intern(ctx->atom_table, string, len, true);
73 }
74
75 const char *
76 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
77 {
78     return atom_text(ctx->atom_table, atom);
79 }
80
81 void
82 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
83         const char *fmt, ...)
84 {
85     va_list args;
86
87     if (ctx->log_level < level || ctx->log_verbosity < verbosity)
88         return;
89
90     va_start(args, fmt);
91     ctx->log_fn(ctx, level, fmt, args);
92     va_end(args);
93 }
94
95 char *
96 xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
97 {
98     char *rtrn;
99
100     if (size >= sizeof(ctx->text_buffer))
101         return NULL;
102
103     if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
104         ctx->text_next = 0;
105
106     rtrn = &ctx->text_buffer[ctx->text_next];
107     ctx->text_next += size;
108
109     return rtrn;
110 }
111
112 static const char *
113 xkb_context_get_default_rules(struct xkb_context *ctx)
114 {
115     const char *env = NULL;
116
117     if (ctx->use_environment_names)
118         env = xkb_context_getenv(ctx, "XKB_DEFAULT_RULES");
119
120     return env ? env : DEFAULT_XKB_RULES;
121 }
122
123 static const char *
124 xkb_context_get_default_model(struct xkb_context *ctx)
125 {
126     const char *env = NULL;
127
128     if (ctx->use_environment_names)
129         env = xkb_context_getenv(ctx, "XKB_DEFAULT_MODEL");
130
131     return env ? env : DEFAULT_XKB_MODEL;
132 }
133
134 static const char *
135 xkb_context_get_default_layout(struct xkb_context *ctx)
136 {
137     const char *env = NULL;
138
139     if (ctx->use_environment_names)
140         env = xkb_context_getenv(ctx, "XKB_DEFAULT_LAYOUT");
141
142     return env ? env : DEFAULT_XKB_LAYOUT;
143 }
144
145 static const char *
146 xkb_context_get_default_variant(struct xkb_context *ctx)
147 {
148     const char *env = NULL;
149     const char *layout = xkb_context_getenv(ctx, "XKB_DEFAULT_LAYOUT");
150
151     /* We don't want to inherit the variant if they haven't also set a
152      * layout, since they're so closely paired. */
153     if (layout && ctx->use_environment_names)
154         env = xkb_context_getenv(ctx, "XKB_DEFAULT_VARIANT");
155
156     return env ? env : DEFAULT_XKB_VARIANT;
157 }
158
159 static const char *
160 xkb_context_get_default_options(struct xkb_context *ctx)
161 {
162     const char *env = NULL;
163
164     if (ctx->use_environment_names)
165         env = xkb_context_getenv(ctx, "XKB_DEFAULT_OPTIONS");
166
167     return env ? env : DEFAULT_XKB_OPTIONS;
168 }
169
170 void
171 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
172                                 struct xkb_rule_names *rmlvo)
173 {
174     if (isempty(rmlvo->rules))
175         rmlvo->rules = xkb_context_get_default_rules(ctx);
176     if (isempty(rmlvo->model))
177         rmlvo->model = xkb_context_get_default_model(ctx);
178     /* Layout and variant are tied together, so don't try to use one from
179      * the caller and one from the environment. */
180     if (isempty(rmlvo->layout)) {
181         rmlvo->layout = xkb_context_get_default_layout(ctx);
182         rmlvo->variant = xkb_context_get_default_variant(ctx);
183     }
184     /* Options can be empty, so respect that if passed in. */
185     if (rmlvo->options == NULL)
186         rmlvo->options = xkb_context_get_default_options(ctx);
187 }