488bfad6aa7133d83dc38fe4e40b31efe32873f5
[platform/upstream/libxkbcommon.git] / src / context.h
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author: Daniel Stone <daniel@fooishbar.org>
24  */
25
26 #ifndef CONTEXT_H
27 #define CONTEXT_H
28
29 #include "atom.h"
30
31 struct xkb_context {
32     int refcnt;
33
34     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
35                                      enum xkb_log_level level,
36                                      const char *fmt, va_list args);
37     enum xkb_log_level log_level;
38     int log_verbosity;
39     void *user_data;
40
41     struct xkb_rule_names names_dflt;
42
43     darray(char *) includes;
44     darray(char *) failed_includes;
45
46     struct atom_table *atom_table;
47
48     /* Used and allocated by xkbcommon-x11, free()d with the context. */
49     void *x11_atom_cache;
50
51     /* Buffer for the *Text() functions. */
52     char text_buffer[2048];
53     size_t text_next;
54
55     unsigned int use_environment_names : 1;
56     unsigned int use_secure_getenv : 1;
57 };
58
59 char *
60 xkb_context_getenv(struct xkb_context *ctx, const char *name);
61
62 unsigned int
63 xkb_context_num_failed_include_paths(struct xkb_context *ctx);
64
65 const char *
66 xkb_context_failed_include_path_get(struct xkb_context *ctx,
67                                     unsigned int idx);
68
69 const char *
70 xkb_context_include_path_get_extra_path(struct xkb_context *ctx);
71
72 const char *
73 xkb_context_include_path_get_system_path(struct xkb_context *ctx);
74
75 /*
76  * Returns XKB_ATOM_NONE if @string was not previously interned,
77  * otherwise returns the atom.
78  */
79 xkb_atom_t
80 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
81
82 xkb_atom_t
83 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len);
84
85 #define xkb_atom_intern_literal(ctx, literal) \
86     xkb_atom_intern((ctx), (literal), sizeof(literal) - 1)
87
88 /**
89  * If @string is dynamically allocated, NUL-terminated, free'd immediately
90  * after being interned, and not used afterwards, use this function
91  * instead of xkb_atom_intern to avoid some unnecessary allocations.
92  * The caller should not use or free the passed in string afterwards.
93  */
94 xkb_atom_t
95 xkb_atom_steal(struct xkb_context *ctx, char *string);
96
97 const char *
98 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
99
100 char *
101 xkb_context_get_buffer(struct xkb_context *ctx, size_t size);
102
103 ATTR_PRINTF(4, 5) void
104 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
105         const char *fmt, ...);
106
107 void
108 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
109                                 struct xkb_rule_names *rmlvo);
110
111 /*
112  * The format is not part of the argument list in order to avoid the
113  * "ISO C99 requires rest arguments to be used" warning when only the
114  * format is supplied without arguments. Not supplying it would still
115  * result in an error, though.
116  */
117 #define log_dbg(ctx, ...) \
118     xkb_log((ctx), XKB_LOG_LEVEL_DEBUG, 0, __VA_ARGS__)
119 #define log_info(ctx, ...) \
120     xkb_log((ctx), XKB_LOG_LEVEL_INFO, 0, __VA_ARGS__)
121 #define log_warn(ctx, ...) \
122     xkb_log((ctx), XKB_LOG_LEVEL_WARNING, 0,  __VA_ARGS__)
123 #define log_err(ctx, ...) \
124     xkb_log((ctx), XKB_LOG_LEVEL_ERROR, 0,  __VA_ARGS__)
125 #define log_wsgo(ctx, ...) \
126     xkb_log((ctx), XKB_LOG_LEVEL_CRITICAL, 0, __VA_ARGS__)
127 #define log_vrb(ctx, vrb, ...) \
128     xkb_log((ctx), XKB_LOG_LEVEL_WARNING, (vrb), __VA_ARGS__)
129
130 /*
131  * Variants which are prefixed by the name of the function they're
132  * called from.
133  * Here we must have the silly 1 variant.
134  */
135 #define log_err_func(ctx, fmt, ...) \
136     log_err(ctx, "%s: " fmt, __func__, __VA_ARGS__)
137 #define log_err_func1(ctx, fmt) \
138     log_err(ctx, "%s: " fmt, __func__)
139
140 #endif