xref: /aosp_15_r20/external/libxkbcommon/src/context.c (revision 2b949d0487e80d67f1fda82db69e101e761f8064)
1*2b949d04SAndroid Build Coastguard Worker /*
2*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Intel Corporation
3*2b949d04SAndroid Build Coastguard Worker  * Copyright © 2012 Ran Benita
4*2b949d04SAndroid Build Coastguard Worker  *
5*2b949d04SAndroid Build Coastguard Worker  * Permission is hereby granted, free of charge, to any person obtaining a
6*2b949d04SAndroid Build Coastguard Worker  * copy of this software and associated documentation files (the "Software"),
7*2b949d04SAndroid Build Coastguard Worker  * to deal in the Software without restriction, including without limitation
8*2b949d04SAndroid Build Coastguard Worker  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*2b949d04SAndroid Build Coastguard Worker  * and/or sell copies of the Software, and to permit persons to whom the
10*2b949d04SAndroid Build Coastguard Worker  * Software is furnished to do so, subject to the following conditions:
11*2b949d04SAndroid Build Coastguard Worker  *
12*2b949d04SAndroid Build Coastguard Worker  * The above copyright notice and this permission notice (including the next
13*2b949d04SAndroid Build Coastguard Worker  * paragraph) shall be included in all copies or substantial portions of the
14*2b949d04SAndroid Build Coastguard Worker  * Software.
15*2b949d04SAndroid Build Coastguard Worker  *
16*2b949d04SAndroid Build Coastguard Worker  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*2b949d04SAndroid Build Coastguard Worker  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*2b949d04SAndroid Build Coastguard Worker  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19*2b949d04SAndroid Build Coastguard Worker  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*2b949d04SAndroid Build Coastguard Worker  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21*2b949d04SAndroid Build Coastguard Worker  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22*2b949d04SAndroid Build Coastguard Worker  * DEALINGS IN THE SOFTWARE.
23*2b949d04SAndroid Build Coastguard Worker  *
24*2b949d04SAndroid Build Coastguard Worker  * Author: Daniel Stone <[email protected]>
25*2b949d04SAndroid Build Coastguard Worker  */
26*2b949d04SAndroid Build Coastguard Worker 
27*2b949d04SAndroid Build Coastguard Worker #include "config.h"
28*2b949d04SAndroid Build Coastguard Worker 
29*2b949d04SAndroid Build Coastguard Worker #include <sys/types.h>
30*2b949d04SAndroid Build Coastguard Worker #include <sys/stat.h>
31*2b949d04SAndroid Build Coastguard Worker #include <errno.h>
32*2b949d04SAndroid Build Coastguard Worker #ifdef _MSC_VER
33*2b949d04SAndroid Build Coastguard Worker # include <direct.h>
34*2b949d04SAndroid Build Coastguard Worker # include <io.h>
35*2b949d04SAndroid Build Coastguard Worker # ifndef S_ISDIR
36*2b949d04SAndroid Build Coastguard Worker #  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
37*2b949d04SAndroid Build Coastguard Worker # endif
38*2b949d04SAndroid Build Coastguard Worker #else
39*2b949d04SAndroid Build Coastguard Worker # include <unistd.h>
40*2b949d04SAndroid Build Coastguard Worker #endif
41*2b949d04SAndroid Build Coastguard Worker 
42*2b949d04SAndroid Build Coastguard Worker #include "xkbcommon/xkbcommon.h"
43*2b949d04SAndroid Build Coastguard Worker #include "utils.h"
44*2b949d04SAndroid Build Coastguard Worker #include "context.h"
45*2b949d04SAndroid Build Coastguard Worker 
46*2b949d04SAndroid Build Coastguard Worker /**
47*2b949d04SAndroid Build Coastguard Worker  * Append one directory to the context's include path.
48*2b949d04SAndroid Build Coastguard Worker  */
49*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT int
xkb_context_include_path_append(struct xkb_context * ctx,const char * path)50*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
51*2b949d04SAndroid Build Coastguard Worker {
52*2b949d04SAndroid Build Coastguard Worker     struct stat stat_buf;
53*2b949d04SAndroid Build Coastguard Worker     int err = ENOMEM;
54*2b949d04SAndroid Build Coastguard Worker     char *tmp;
55*2b949d04SAndroid Build Coastguard Worker 
56*2b949d04SAndroid Build Coastguard Worker     tmp = strdup(path);
57*2b949d04SAndroid Build Coastguard Worker     if (!tmp)
58*2b949d04SAndroid Build Coastguard Worker         goto err;
59*2b949d04SAndroid Build Coastguard Worker 
60*2b949d04SAndroid Build Coastguard Worker     err = stat(path, &stat_buf);
61*2b949d04SAndroid Build Coastguard Worker     if (err != 0) {
62*2b949d04SAndroid Build Coastguard Worker         err = errno;
63*2b949d04SAndroid Build Coastguard Worker         goto err;
64*2b949d04SAndroid Build Coastguard Worker     }
65*2b949d04SAndroid Build Coastguard Worker     if (!S_ISDIR(stat_buf.st_mode)) {
66*2b949d04SAndroid Build Coastguard Worker         err = ENOTDIR;
67*2b949d04SAndroid Build Coastguard Worker         goto err;
68*2b949d04SAndroid Build Coastguard Worker     }
69*2b949d04SAndroid Build Coastguard Worker 
70*2b949d04SAndroid Build Coastguard Worker     if (!check_eaccess(path, R_OK | X_OK)) {
71*2b949d04SAndroid Build Coastguard Worker         err = EACCES;
72*2b949d04SAndroid Build Coastguard Worker         goto err;
73*2b949d04SAndroid Build Coastguard Worker     }
74*2b949d04SAndroid Build Coastguard Worker 
75*2b949d04SAndroid Build Coastguard Worker     darray_append(ctx->includes, tmp);
76*2b949d04SAndroid Build Coastguard Worker     log_dbg(ctx, "Include path added: %s\n", tmp);
77*2b949d04SAndroid Build Coastguard Worker 
78*2b949d04SAndroid Build Coastguard Worker     return 1;
79*2b949d04SAndroid Build Coastguard Worker 
80*2b949d04SAndroid Build Coastguard Worker err:
81*2b949d04SAndroid Build Coastguard Worker     darray_append(ctx->failed_includes, tmp);
82*2b949d04SAndroid Build Coastguard Worker     log_dbg(ctx, "Include path failed: %s (%s)\n", tmp, strerror(err));
83*2b949d04SAndroid Build Coastguard Worker     return 0;
84*2b949d04SAndroid Build Coastguard Worker }
85*2b949d04SAndroid Build Coastguard Worker 
86*2b949d04SAndroid Build Coastguard Worker const char *
xkb_context_include_path_get_extra_path(struct xkb_context * ctx)87*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_get_extra_path(struct xkb_context *ctx)
88*2b949d04SAndroid Build Coastguard Worker {
89*2b949d04SAndroid Build Coastguard Worker     const char *extra = secure_getenv("XKB_CONFIG_EXTRA_PATH");
90*2b949d04SAndroid Build Coastguard Worker     return extra ? extra : DFLT_XKB_CONFIG_EXTRA_PATH;
91*2b949d04SAndroid Build Coastguard Worker }
92*2b949d04SAndroid Build Coastguard Worker 
93*2b949d04SAndroid Build Coastguard Worker const char *
xkb_context_include_path_get_system_path(struct xkb_context * ctx)94*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_get_system_path(struct xkb_context *ctx)
95*2b949d04SAndroid Build Coastguard Worker {
96*2b949d04SAndroid Build Coastguard Worker     const char *root = secure_getenv("XKB_CONFIG_ROOT");
97*2b949d04SAndroid Build Coastguard Worker     return root ? root : DFLT_XKB_CONFIG_ROOT;
98*2b949d04SAndroid Build Coastguard Worker }
99*2b949d04SAndroid Build Coastguard Worker 
100*2b949d04SAndroid Build Coastguard Worker /**
101*2b949d04SAndroid Build Coastguard Worker  * Append the default include directories to the context.
102*2b949d04SAndroid Build Coastguard Worker  */
103*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT int
xkb_context_include_path_append_default(struct xkb_context * ctx)104*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_append_default(struct xkb_context *ctx)
105*2b949d04SAndroid Build Coastguard Worker {
106*2b949d04SAndroid Build Coastguard Worker     const char *home, *xdg, *root, *extra;
107*2b949d04SAndroid Build Coastguard Worker     char *user_path;
108*2b949d04SAndroid Build Coastguard Worker     int ret = 0;
109*2b949d04SAndroid Build Coastguard Worker 
110*2b949d04SAndroid Build Coastguard Worker     home = secure_getenv("HOME");
111*2b949d04SAndroid Build Coastguard Worker 
112*2b949d04SAndroid Build Coastguard Worker     xdg = secure_getenv("XDG_CONFIG_HOME");
113*2b949d04SAndroid Build Coastguard Worker     if (xdg != NULL) {
114*2b949d04SAndroid Build Coastguard Worker         user_path = asprintf_safe("%s/xkb", xdg);
115*2b949d04SAndroid Build Coastguard Worker         if (user_path) {
116*2b949d04SAndroid Build Coastguard Worker             ret |= xkb_context_include_path_append(ctx, user_path);
117*2b949d04SAndroid Build Coastguard Worker             free(user_path);
118*2b949d04SAndroid Build Coastguard Worker         }
119*2b949d04SAndroid Build Coastguard Worker     } else if (home != NULL) {
120*2b949d04SAndroid Build Coastguard Worker         /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
121*2b949d04SAndroid Build Coastguard Worker         user_path = asprintf_safe("%s/.config/xkb", home);
122*2b949d04SAndroid Build Coastguard Worker         if (user_path) {
123*2b949d04SAndroid Build Coastguard Worker             ret |= xkb_context_include_path_append(ctx, user_path);
124*2b949d04SAndroid Build Coastguard Worker             free(user_path);
125*2b949d04SAndroid Build Coastguard Worker         }
126*2b949d04SAndroid Build Coastguard Worker     }
127*2b949d04SAndroid Build Coastguard Worker 
128*2b949d04SAndroid Build Coastguard Worker     if (home != NULL) {
129*2b949d04SAndroid Build Coastguard Worker         user_path = asprintf_safe("%s/.xkb", home);
130*2b949d04SAndroid Build Coastguard Worker         if (user_path) {
131*2b949d04SAndroid Build Coastguard Worker             ret |= xkb_context_include_path_append(ctx, user_path);
132*2b949d04SAndroid Build Coastguard Worker             free(user_path);
133*2b949d04SAndroid Build Coastguard Worker         }
134*2b949d04SAndroid Build Coastguard Worker     }
135*2b949d04SAndroid Build Coastguard Worker 
136*2b949d04SAndroid Build Coastguard Worker     extra = xkb_context_include_path_get_extra_path(ctx);
137*2b949d04SAndroid Build Coastguard Worker     ret |= xkb_context_include_path_append(ctx, extra);
138*2b949d04SAndroid Build Coastguard Worker     root = xkb_context_include_path_get_system_path(ctx);
139*2b949d04SAndroid Build Coastguard Worker     ret |= xkb_context_include_path_append(ctx, root);
140*2b949d04SAndroid Build Coastguard Worker 
141*2b949d04SAndroid Build Coastguard Worker     return ret;
142*2b949d04SAndroid Build Coastguard Worker }
143*2b949d04SAndroid Build Coastguard Worker 
144*2b949d04SAndroid Build Coastguard Worker /**
145*2b949d04SAndroid Build Coastguard Worker  * Remove all entries in the context's include path.
146*2b949d04SAndroid Build Coastguard Worker  */
147*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_include_path_clear(struct xkb_context * ctx)148*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_clear(struct xkb_context *ctx)
149*2b949d04SAndroid Build Coastguard Worker {
150*2b949d04SAndroid Build Coastguard Worker     char **path;
151*2b949d04SAndroid Build Coastguard Worker 
152*2b949d04SAndroid Build Coastguard Worker     darray_foreach(path, ctx->includes)
153*2b949d04SAndroid Build Coastguard Worker         free(*path);
154*2b949d04SAndroid Build Coastguard Worker     darray_free(ctx->includes);
155*2b949d04SAndroid Build Coastguard Worker 
156*2b949d04SAndroid Build Coastguard Worker     darray_foreach(path, ctx->failed_includes)
157*2b949d04SAndroid Build Coastguard Worker         free(*path);
158*2b949d04SAndroid Build Coastguard Worker     darray_free(ctx->failed_includes);
159*2b949d04SAndroid Build Coastguard Worker }
160*2b949d04SAndroid Build Coastguard Worker 
161*2b949d04SAndroid Build Coastguard Worker /**
162*2b949d04SAndroid Build Coastguard Worker  * xkb_context_include_path_clear() + xkb_context_include_path_append_default()
163*2b949d04SAndroid Build Coastguard Worker  */
164*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT int
xkb_context_include_path_reset_defaults(struct xkb_context * ctx)165*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_reset_defaults(struct xkb_context *ctx)
166*2b949d04SAndroid Build Coastguard Worker {
167*2b949d04SAndroid Build Coastguard Worker     xkb_context_include_path_clear(ctx);
168*2b949d04SAndroid Build Coastguard Worker     return xkb_context_include_path_append_default(ctx);
169*2b949d04SAndroid Build Coastguard Worker }
170*2b949d04SAndroid Build Coastguard Worker 
171*2b949d04SAndroid Build Coastguard Worker /**
172*2b949d04SAndroid Build Coastguard Worker  * Returns the number of entries in the context's include path.
173*2b949d04SAndroid Build Coastguard Worker  */
174*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT unsigned int
xkb_context_num_include_paths(struct xkb_context * ctx)175*2b949d04SAndroid Build Coastguard Worker xkb_context_num_include_paths(struct xkb_context *ctx)
176*2b949d04SAndroid Build Coastguard Worker {
177*2b949d04SAndroid Build Coastguard Worker     return darray_size(ctx->includes);
178*2b949d04SAndroid Build Coastguard Worker }
179*2b949d04SAndroid Build Coastguard Worker 
180*2b949d04SAndroid Build Coastguard Worker /**
181*2b949d04SAndroid Build Coastguard Worker  * Returns the given entry in the context's include path, or NULL if an
182*2b949d04SAndroid Build Coastguard Worker  * invalid index is passed.
183*2b949d04SAndroid Build Coastguard Worker  */
184*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT const char *
xkb_context_include_path_get(struct xkb_context * ctx,unsigned int idx)185*2b949d04SAndroid Build Coastguard Worker xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
186*2b949d04SAndroid Build Coastguard Worker {
187*2b949d04SAndroid Build Coastguard Worker     if (idx >= xkb_context_num_include_paths(ctx))
188*2b949d04SAndroid Build Coastguard Worker         return NULL;
189*2b949d04SAndroid Build Coastguard Worker 
190*2b949d04SAndroid Build Coastguard Worker     return darray_item(ctx->includes, idx);
191*2b949d04SAndroid Build Coastguard Worker }
192*2b949d04SAndroid Build Coastguard Worker 
193*2b949d04SAndroid Build Coastguard Worker /**
194*2b949d04SAndroid Build Coastguard Worker  * Take a new reference on the context.
195*2b949d04SAndroid Build Coastguard Worker  */
196*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_context *
xkb_context_ref(struct xkb_context * ctx)197*2b949d04SAndroid Build Coastguard Worker xkb_context_ref(struct xkb_context *ctx)
198*2b949d04SAndroid Build Coastguard Worker {
199*2b949d04SAndroid Build Coastguard Worker     ctx->refcnt++;
200*2b949d04SAndroid Build Coastguard Worker     return ctx;
201*2b949d04SAndroid Build Coastguard Worker }
202*2b949d04SAndroid Build Coastguard Worker 
203*2b949d04SAndroid Build Coastguard Worker /**
204*2b949d04SAndroid Build Coastguard Worker  * Drop an existing reference on the context, and free it if the refcnt is
205*2b949d04SAndroid Build Coastguard Worker  * now 0.
206*2b949d04SAndroid Build Coastguard Worker  */
207*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_unref(struct xkb_context * ctx)208*2b949d04SAndroid Build Coastguard Worker xkb_context_unref(struct xkb_context *ctx)
209*2b949d04SAndroid Build Coastguard Worker {
210*2b949d04SAndroid Build Coastguard Worker     if (!ctx || --ctx->refcnt > 0)
211*2b949d04SAndroid Build Coastguard Worker         return;
212*2b949d04SAndroid Build Coastguard Worker 
213*2b949d04SAndroid Build Coastguard Worker     free(ctx->x11_atom_cache);
214*2b949d04SAndroid Build Coastguard Worker     xkb_context_include_path_clear(ctx);
215*2b949d04SAndroid Build Coastguard Worker     atom_table_free(ctx->atom_table);
216*2b949d04SAndroid Build Coastguard Worker     free(ctx);
217*2b949d04SAndroid Build Coastguard Worker }
218*2b949d04SAndroid Build Coastguard Worker 
219*2b949d04SAndroid Build Coastguard Worker static const char *
log_level_to_prefix(enum xkb_log_level level)220*2b949d04SAndroid Build Coastguard Worker log_level_to_prefix(enum xkb_log_level level)
221*2b949d04SAndroid Build Coastguard Worker {
222*2b949d04SAndroid Build Coastguard Worker     switch (level) {
223*2b949d04SAndroid Build Coastguard Worker     case XKB_LOG_LEVEL_DEBUG:
224*2b949d04SAndroid Build Coastguard Worker         return "xkbcommon: DEBUG: ";
225*2b949d04SAndroid Build Coastguard Worker     case XKB_LOG_LEVEL_INFO:
226*2b949d04SAndroid Build Coastguard Worker         return "xkbcommon: INFO: ";
227*2b949d04SAndroid Build Coastguard Worker     case XKB_LOG_LEVEL_WARNING:
228*2b949d04SAndroid Build Coastguard Worker         return "xkbcommon: WARNING: ";
229*2b949d04SAndroid Build Coastguard Worker     case XKB_LOG_LEVEL_ERROR:
230*2b949d04SAndroid Build Coastguard Worker         return "xkbcommon: ERROR: ";
231*2b949d04SAndroid Build Coastguard Worker     case XKB_LOG_LEVEL_CRITICAL:
232*2b949d04SAndroid Build Coastguard Worker         return "xkbcommon: CRITICAL: ";
233*2b949d04SAndroid Build Coastguard Worker     default:
234*2b949d04SAndroid Build Coastguard Worker         return NULL;
235*2b949d04SAndroid Build Coastguard Worker     }
236*2b949d04SAndroid Build Coastguard Worker }
237*2b949d04SAndroid Build Coastguard Worker 
238*2b949d04SAndroid Build Coastguard Worker ATTR_PRINTF(3, 0) static void
default_log_fn(struct xkb_context * ctx,enum xkb_log_level level,const char * fmt,va_list args)239*2b949d04SAndroid Build Coastguard Worker default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
240*2b949d04SAndroid Build Coastguard Worker                const char *fmt, va_list args)
241*2b949d04SAndroid Build Coastguard Worker {
242*2b949d04SAndroid Build Coastguard Worker     const char *prefix = log_level_to_prefix(level);
243*2b949d04SAndroid Build Coastguard Worker 
244*2b949d04SAndroid Build Coastguard Worker     if (prefix)
245*2b949d04SAndroid Build Coastguard Worker         fprintf(stderr, "%s", prefix);
246*2b949d04SAndroid Build Coastguard Worker     vfprintf(stderr, fmt, args);
247*2b949d04SAndroid Build Coastguard Worker }
248*2b949d04SAndroid Build Coastguard Worker 
249*2b949d04SAndroid Build Coastguard Worker static enum xkb_log_level
log_level(const char * level)250*2b949d04SAndroid Build Coastguard Worker log_level(const char *level) {
251*2b949d04SAndroid Build Coastguard Worker     char *endptr;
252*2b949d04SAndroid Build Coastguard Worker     enum xkb_log_level lvl;
253*2b949d04SAndroid Build Coastguard Worker 
254*2b949d04SAndroid Build Coastguard Worker     errno = 0;
255*2b949d04SAndroid Build Coastguard Worker     lvl = strtol(level, &endptr, 10);
256*2b949d04SAndroid Build Coastguard Worker     if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
257*2b949d04SAndroid Build Coastguard Worker         return lvl;
258*2b949d04SAndroid Build Coastguard Worker     if (istreq_prefix("crit", level))
259*2b949d04SAndroid Build Coastguard Worker         return XKB_LOG_LEVEL_CRITICAL;
260*2b949d04SAndroid Build Coastguard Worker     if (istreq_prefix("err", level))
261*2b949d04SAndroid Build Coastguard Worker         return XKB_LOG_LEVEL_ERROR;
262*2b949d04SAndroid Build Coastguard Worker     if (istreq_prefix("warn", level))
263*2b949d04SAndroid Build Coastguard Worker         return XKB_LOG_LEVEL_WARNING;
264*2b949d04SAndroid Build Coastguard Worker     if (istreq_prefix("info", level))
265*2b949d04SAndroid Build Coastguard Worker         return XKB_LOG_LEVEL_INFO;
266*2b949d04SAndroid Build Coastguard Worker     if (istreq_prefix("debug", level) || istreq_prefix("dbg", level))
267*2b949d04SAndroid Build Coastguard Worker         return XKB_LOG_LEVEL_DEBUG;
268*2b949d04SAndroid Build Coastguard Worker 
269*2b949d04SAndroid Build Coastguard Worker     return XKB_LOG_LEVEL_ERROR;
270*2b949d04SAndroid Build Coastguard Worker }
271*2b949d04SAndroid Build Coastguard Worker 
272*2b949d04SAndroid Build Coastguard Worker static int
log_verbosity(const char * verbosity)273*2b949d04SAndroid Build Coastguard Worker log_verbosity(const char *verbosity) {
274*2b949d04SAndroid Build Coastguard Worker     char *endptr;
275*2b949d04SAndroid Build Coastguard Worker     int v;
276*2b949d04SAndroid Build Coastguard Worker 
277*2b949d04SAndroid Build Coastguard Worker     errno = 0;
278*2b949d04SAndroid Build Coastguard Worker     v = strtol(verbosity, &endptr, 10);
279*2b949d04SAndroid Build Coastguard Worker     if (errno == 0)
280*2b949d04SAndroid Build Coastguard Worker         return v;
281*2b949d04SAndroid Build Coastguard Worker 
282*2b949d04SAndroid Build Coastguard Worker     return 0;
283*2b949d04SAndroid Build Coastguard Worker }
284*2b949d04SAndroid Build Coastguard Worker 
285*2b949d04SAndroid Build Coastguard Worker /**
286*2b949d04SAndroid Build Coastguard Worker  * Create a new context.
287*2b949d04SAndroid Build Coastguard Worker  */
288*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT struct xkb_context *
xkb_context_new(enum xkb_context_flags flags)289*2b949d04SAndroid Build Coastguard Worker xkb_context_new(enum xkb_context_flags flags)
290*2b949d04SAndroid Build Coastguard Worker {
291*2b949d04SAndroid Build Coastguard Worker     const char *env;
292*2b949d04SAndroid Build Coastguard Worker     struct xkb_context *ctx = calloc(1, sizeof(*ctx));
293*2b949d04SAndroid Build Coastguard Worker 
294*2b949d04SAndroid Build Coastguard Worker     if (!ctx)
295*2b949d04SAndroid Build Coastguard Worker         return NULL;
296*2b949d04SAndroid Build Coastguard Worker 
297*2b949d04SAndroid Build Coastguard Worker     ctx->refcnt = 1;
298*2b949d04SAndroid Build Coastguard Worker     ctx->log_fn = default_log_fn;
299*2b949d04SAndroid Build Coastguard Worker     ctx->log_level = XKB_LOG_LEVEL_ERROR;
300*2b949d04SAndroid Build Coastguard Worker     ctx->log_verbosity = 0;
301*2b949d04SAndroid Build Coastguard Worker 
302*2b949d04SAndroid Build Coastguard Worker     /* Environment overwrites defaults. */
303*2b949d04SAndroid Build Coastguard Worker     env = secure_getenv("XKB_LOG_LEVEL");
304*2b949d04SAndroid Build Coastguard Worker     if (env)
305*2b949d04SAndroid Build Coastguard Worker         xkb_context_set_log_level(ctx, log_level(env));
306*2b949d04SAndroid Build Coastguard Worker 
307*2b949d04SAndroid Build Coastguard Worker     env = secure_getenv("XKB_LOG_VERBOSITY");
308*2b949d04SAndroid Build Coastguard Worker     if (env)
309*2b949d04SAndroid Build Coastguard Worker         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
310*2b949d04SAndroid Build Coastguard Worker 
311*2b949d04SAndroid Build Coastguard Worker     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
312*2b949d04SAndroid Build Coastguard Worker         !xkb_context_include_path_append_default(ctx)) {
313*2b949d04SAndroid Build Coastguard Worker         log_err(ctx, "failed to add default include path %s\n",
314*2b949d04SAndroid Build Coastguard Worker                 DFLT_XKB_CONFIG_ROOT);
315*2b949d04SAndroid Build Coastguard Worker         xkb_context_unref(ctx);
316*2b949d04SAndroid Build Coastguard Worker         return NULL;
317*2b949d04SAndroid Build Coastguard Worker     }
318*2b949d04SAndroid Build Coastguard Worker 
319*2b949d04SAndroid Build Coastguard Worker     ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
320*2b949d04SAndroid Build Coastguard Worker 
321*2b949d04SAndroid Build Coastguard Worker     ctx->atom_table = atom_table_new();
322*2b949d04SAndroid Build Coastguard Worker     if (!ctx->atom_table) {
323*2b949d04SAndroid Build Coastguard Worker         xkb_context_unref(ctx);
324*2b949d04SAndroid Build Coastguard Worker         return NULL;
325*2b949d04SAndroid Build Coastguard Worker     }
326*2b949d04SAndroid Build Coastguard Worker 
327*2b949d04SAndroid Build Coastguard Worker     ctx->x11_atom_cache = NULL;
328*2b949d04SAndroid Build Coastguard Worker 
329*2b949d04SAndroid Build Coastguard Worker     return ctx;
330*2b949d04SAndroid Build Coastguard Worker }
331*2b949d04SAndroid Build Coastguard Worker 
332*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_set_log_fn(struct xkb_context * ctx,void (* log_fn)(struct xkb_context * ctx,enum xkb_log_level level,const char * fmt,va_list args))333*2b949d04SAndroid Build Coastguard Worker xkb_context_set_log_fn(struct xkb_context *ctx,
334*2b949d04SAndroid Build Coastguard Worker                        void (*log_fn)(struct xkb_context *ctx,
335*2b949d04SAndroid Build Coastguard Worker                                       enum xkb_log_level level,
336*2b949d04SAndroid Build Coastguard Worker                                       const char *fmt, va_list args))
337*2b949d04SAndroid Build Coastguard Worker {
338*2b949d04SAndroid Build Coastguard Worker     ctx->log_fn = (log_fn ? log_fn : default_log_fn);
339*2b949d04SAndroid Build Coastguard Worker }
340*2b949d04SAndroid Build Coastguard Worker 
341*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT enum xkb_log_level
xkb_context_get_log_level(struct xkb_context * ctx)342*2b949d04SAndroid Build Coastguard Worker xkb_context_get_log_level(struct xkb_context *ctx)
343*2b949d04SAndroid Build Coastguard Worker {
344*2b949d04SAndroid Build Coastguard Worker     return ctx->log_level;
345*2b949d04SAndroid Build Coastguard Worker }
346*2b949d04SAndroid Build Coastguard Worker 
347*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_set_log_level(struct xkb_context * ctx,enum xkb_log_level level)348*2b949d04SAndroid Build Coastguard Worker xkb_context_set_log_level(struct xkb_context *ctx, enum xkb_log_level level)
349*2b949d04SAndroid Build Coastguard Worker {
350*2b949d04SAndroid Build Coastguard Worker     ctx->log_level = level;
351*2b949d04SAndroid Build Coastguard Worker }
352*2b949d04SAndroid Build Coastguard Worker 
353*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT int
xkb_context_get_log_verbosity(struct xkb_context * ctx)354*2b949d04SAndroid Build Coastguard Worker xkb_context_get_log_verbosity(struct xkb_context *ctx)
355*2b949d04SAndroid Build Coastguard Worker {
356*2b949d04SAndroid Build Coastguard Worker     return ctx->log_verbosity;
357*2b949d04SAndroid Build Coastguard Worker }
358*2b949d04SAndroid Build Coastguard Worker 
359*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_set_log_verbosity(struct xkb_context * ctx,int verbosity)360*2b949d04SAndroid Build Coastguard Worker xkb_context_set_log_verbosity(struct xkb_context *ctx, int verbosity)
361*2b949d04SAndroid Build Coastguard Worker {
362*2b949d04SAndroid Build Coastguard Worker     ctx->log_verbosity = verbosity;
363*2b949d04SAndroid Build Coastguard Worker }
364*2b949d04SAndroid Build Coastguard Worker 
365*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void *
xkb_context_get_user_data(struct xkb_context * ctx)366*2b949d04SAndroid Build Coastguard Worker xkb_context_get_user_data(struct xkb_context *ctx)
367*2b949d04SAndroid Build Coastguard Worker {
368*2b949d04SAndroid Build Coastguard Worker     if (ctx)
369*2b949d04SAndroid Build Coastguard Worker         return ctx->user_data;
370*2b949d04SAndroid Build Coastguard Worker     return NULL;
371*2b949d04SAndroid Build Coastguard Worker }
372*2b949d04SAndroid Build Coastguard Worker 
373*2b949d04SAndroid Build Coastguard Worker XKB_EXPORT void
xkb_context_set_user_data(struct xkb_context * ctx,void * user_data)374*2b949d04SAndroid Build Coastguard Worker xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
375*2b949d04SAndroid Build Coastguard Worker {
376*2b949d04SAndroid Build Coastguard Worker     ctx->user_data = user_data;
377*2b949d04SAndroid Build Coastguard Worker }
378