cast a function's return value
[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 #include "messages-codes.h"
31
32 struct xkb_context {
33     int refcnt;
34
35     ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
36                                      enum xkb_log_level level,
37                                      const char *fmt, va_list args);
38     enum xkb_log_level log_level;
39     int log_verbosity;
40     void *user_data;
41
42     struct xkb_rule_names names_dflt;
43
44     darray(char *) includes;
45     darray(char *) failed_includes;
46
47     struct atom_table *atom_table;
48
49     /* Used and allocated by xkbcommon-x11, free()d with the context. */
50     void *x11_atom_cache;
51
52     /* Buffer for the *Text() functions. */
53     char text_buffer[2048];
54     size_t text_next;
55
56     unsigned int use_environment_names : 1;
57     unsigned int use_secure_getenv : 1;
58 };
59
60 char *
61 xkb_context_getenv(struct xkb_context *ctx, const char *name);
62
63 unsigned int
64 xkb_context_num_failed_include_paths(struct xkb_context *ctx);
65
66 const char *
67 xkb_context_failed_include_path_get(struct xkb_context *ctx,
68                                     unsigned int idx);
69
70 const char *
71 xkb_context_include_path_get_extra_path(struct xkb_context *ctx);
72
73 const char *
74 xkb_context_include_path_get_system_path(struct xkb_context *ctx);
75
76 /*
77  * Returns XKB_ATOM_NONE if @string was not previously interned,
78  * otherwise returns the atom.
79  */
80 xkb_atom_t
81 xkb_atom_lookup(struct xkb_context *ctx, const char *string);
82
83 xkb_atom_t
84 xkb_atom_intern(struct xkb_context *ctx, const char *string, size_t len);
85
86 #define xkb_atom_intern_literal(ctx, literal) \
87     xkb_atom_intern((ctx), (literal), sizeof(literal) - 1)
88
89 /**
90  * If @string is dynamically allocated, NUL-terminated, free'd immediately
91  * after being interned, and not used afterwards, use this function
92  * instead of xkb_atom_intern to avoid some unnecessary allocations.
93  * The caller should not use or free the passed in string afterwards.
94  */
95 xkb_atom_t
96 xkb_atom_steal(struct xkb_context *ctx, char *string);
97
98 const char *
99 xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom);
100
101 char *
102 xkb_context_get_buffer(struct xkb_context *ctx, size_t size);
103
104 ATTR_PRINTF(4, 5) void
105 xkb_log(struct xkb_context *ctx, enum xkb_log_level level, int verbosity,
106         const char *fmt, ...);
107
108 void
109 xkb_context_sanitize_rule_names(struct xkb_context *ctx,
110                                 struct xkb_rule_names *rmlvo);
111
112 /*
113  * Macro sorcery: PREPEND_MESSAGE_ID enables the log functions to format messages
114  * with the message ID only if the ID is not 0 (XKB_LOG_MESSAGE_NO_ID).
115  * This avoid checking the ID value at run time.
116  *
117  * The trick resides in CHECK_ID:
118  * • CHECK_ID(0) expands to:
119  *   ‣ SECOND(MATCH0, WITH_ID, unused)
120  *   ‣ SECOND(unused,WITHOUT_ID, WITH_ID, unused)
121  *   ‣ WITHOUT_ID
122  * • CHECK_ID(123) expands to:
123  *   ‣ SECOND(MATCH123, WITH_ID, unused)
124  *   ‣ WITH_ID
125 */
126 #define EXPAND(...)              __VA_ARGS__ /* needed for MSVC compatibility */
127
128 #define JOIN_EXPAND(a, b)        a##b
129 #define JOIN(a, b)               JOIN_EXPAND(a, b)
130
131 #define SECOND_EXPAND(a, b, ...) b
132 #define SECOND(...)              EXPAND(SECOND_EXPAND(__VA_ARGS__))
133
134 #define MATCH0                   unused,WITHOUT_ID
135 #define CHECK_ID(value)          SECOND(JOIN(MATCH, value), WITH_ID, unused)
136
137 #define FORMAT_MESSAGE_WITHOUT_ID(id, fmt) fmt
138 #define FORMAT_MESSAGE_WITH_ID(id, fmt)    "[XKB-%03d] " fmt, id
139 #define PREPEND_MESSAGE_ID(id, fmt) JOIN(FORMAT_MESSAGE_, CHECK_ID(id))(id, fmt)
140
141 /*
142  * The format is not part of the argument list in order to avoid the
143  * "ISO C99 requires rest arguments to be used" warning when only the
144  * format is supplied without arguments. Not supplying it would still
145  * result in an error, though.
146  */
147 #define xkb_log_with_code(ctx, level, verbosity, msg_id, fmt, ...) \
148     xkb_log(ctx, level, verbosity, PREPEND_MESSAGE_ID(msg_id, fmt), ##__VA_ARGS__)
149 #define log_dbg(ctx, id, ...) \
150     xkb_log_with_code((ctx), XKB_LOG_LEVEL_DEBUG, 0, id, __VA_ARGS__)
151 #define log_info(ctx, id, ...) \
152     xkb_log_with_code((ctx), XKB_LOG_LEVEL_INFO, 0, id, __VA_ARGS__)
153 #define log_warn(ctx, id, ...) \
154     xkb_log_with_code((ctx), XKB_LOG_LEVEL_WARNING, 0, id, __VA_ARGS__)
155 #define log_err(ctx, id, ...) \
156     xkb_log_with_code((ctx), XKB_LOG_LEVEL_ERROR, 0, id, __VA_ARGS__)
157 #define log_wsgo(ctx, id, ...) \
158     xkb_log_with_code((ctx), XKB_LOG_LEVEL_CRITICAL, 0, id, __VA_ARGS__)
159 #define log_vrb(ctx, vrb, id, ...) \
160     xkb_log_with_code((ctx), XKB_LOG_LEVEL_WARNING, (vrb), id, __VA_ARGS__)
161
162 /*
163  * Variants which are prefixed by the name of the function they're
164  * called from.
165  * Here we must have the silly 1 variant.
166  */
167 #define log_err_func(ctx, fmt, ...) \
168     log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "%s: " fmt, __func__, __VA_ARGS__)
169 #define log_err_func1(ctx, fmt) \
170     log_err(ctx, XKB_LOG_MESSAGE_NO_ID, "%s: " fmt, __func__)
171
172 #endif