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