wlt: fix shl_hook API changes
[platform/upstream/kmscon.git] / src / conf.h
1 /*
2  * Configuration Parsers
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 /*
27  * Configuration
28  * This provides generic command-line argument and configuration file parsers
29  * which can be used by different applications that are part of this
30  * distribution.
31  */
32
33 #ifndef CONFIG_CONFIG_H
34 #define CONFIG_CONFIG_H
35
36 #include <stdbool.h>
37 #include <stdlib.h>
38 #include "shl_misc.h"
39
40 struct conf_type;
41 struct conf_option;
42 struct conf_ctx;
43
44 /* Conf Types */
45
46 #define CONF_HAS_ARG            0x0001
47
48 struct conf_type {
49         unsigned int flags;
50         void (*set_default) (struct conf_option *opt);
51         void (*free) (struct conf_option *opt);
52         int (*parse) (struct conf_option *opt, bool on, const char *arg);
53         int (*copy) (struct conf_option *opt, const struct conf_option *src);
54 };
55
56 /*
57  * Bool: expects "mem" to point to a "bool"
58  * Initial state is "false".
59  */
60
61 extern const struct conf_type conf_bool;
62
63 /*
64  * Int: expects "mem" to point to an "int"
65  * Initial state is "0".
66  */
67
68 extern const struct conf_type conf_int;
69
70 /*
71  * Uint: expects "mem" to point to an "uint"
72  * Initial state is "0"
73  */
74
75 extern const struct conf_type conf_uint;
76
77 /*
78  * String: expects "mem" to point to an "char*"
79  * Initial state is NULL. Memory is allocated by the parser and a string is
80  * always zero-terminated.
81  */
82
83 extern const struct conf_type conf_string;
84
85 /*
86  * Stringlist: expects "mem" to point to an "char**"
87  * Initial state is NULL. The list is NULL-terminated and each entry is a
88  * zero-terminated string. Memory is allocated by the parser.
89  */
90
91 extern const struct conf_type conf_string_list;
92
93 /*
94  * Grabs: expects "mem" to point to an "struct conf_grab*"
95  * Initial state is NULL. See below for the type definition. The memory for the
96  * type is allocated by the parser.
97  * Two small helpers are available to ease the use.
98  */
99
100 extern const struct conf_type conf_grab;
101
102 struct conf_grab {
103         unsigned int num;
104         unsigned int *mods;
105         unsigned int *num_syms;
106         uint32_t **keysyms;
107 };
108
109 static inline bool conf_grab_matches(const struct conf_grab *grab,
110                                      unsigned int ev_mods,
111                                      unsigned int ev_num_syms,
112                                      const uint32_t *ev_syms)
113 {
114         return shl_grab_has_match(ev_mods, ev_num_syms, ev_syms,
115                                   grab->num, grab->mods, grab->num_syms,
116                                   grab->keysyms);
117 }
118
119 #define CONF_SINGLE_GRAB(_mods, _sym) { \
120                 .num = 1, \
121                 .mods = (unsigned int[]) { (_mods) }, \
122                 .num_syms = (unsigned int[]) { 1 }, \
123                 .keysyms = (uint32_t*[]) { (uint32_t[]) { (_sym) } }, \
124         }
125
126 /*
127  * Configuration Context
128  * A configuration context is initialized with an array of config-options and
129  * then can be used to parse different sources. The backing memory is managed by
130  * the user, not by this context.
131  * All options are set to their default values on startup and reset.
132  */
133
134 struct conf_ctx;
135
136 int conf_ctx_new(struct conf_ctx **out, const struct conf_option *opts,
137                  size_t onum, void *mem);
138 void conf_ctx_free(struct conf_ctx *ctx);
139 void conf_ctx_reset(struct conf_ctx *ctx);
140 void *conf_ctx_get_mem(struct conf_ctx *ctx);
141
142 int conf_ctx_parse_ctx(struct conf_ctx *ctx, const struct conf_ctx *src);
143 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv);
144 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...);
145
146 /*
147  * Configuration Options
148  * A configuration option specifies the name of the option, the type, the
149  * backing memory, the default value and more. Each option is represented by
150  * this structure.
151  */
152
153 #define CONF_DONE               0x0001
154 #define CONF_LOCKED             0x0002
155
156 struct conf_option {
157         unsigned int flags;
158         char short_name;
159         const char *long_name;
160         const struct conf_type *type;
161         int (*aftercheck) (struct conf_option *opt, int argc,
162                            char **argv, int idx);
163         int (*copy) (struct conf_option *opt, const struct conf_option *src);
164         int (*file) (struct conf_option *opt, bool on, const char *arg);
165         void *mem;
166         void *def;
167 };
168
169 #define CONF_OPTION(_flags, _short, _long, _type, _aftercheck, _copy, _file, _mem, _def) \
170         { _flags, _short, "no-" _long, _type, _aftercheck, _copy, _file, _mem, _def }
171
172 #define CONF_OPTION_BOOL_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
173         CONF_OPTION(0, _short, _long, &conf_bool, _aftercheck, _copy, _file, _mem, (void*)(long)_def)
174 #define CONF_OPTION_BOOL(_short, _long, _mem, _def) \
175         CONF_OPTION_BOOL_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
176
177 #define CONF_OPTION_INT_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
178         CONF_OPTION(0, _short, _long, &conf_int, _aftercheck, _copy, _file, _mem, (void*)(long)_def)
179 #define CONF_OPTION_INT(_short, _long, _mem, _def) \
180         CONF_OPTION_INT_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
181
182 #define CONF_OPTION_UINT_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
183         CONF_OPTION(0, _short, _long, &conf_uint, _aftercheck, _copy, _file, _mem, (void*)(unsigned long)_def)
184 #define CONF_OPTION_UINT(_short, _long, _mem, _def) \
185         CONF_OPTION_UINT_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
186
187 #define CONF_OPTION_STRING_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
188         CONF_OPTION(0, _short, _long, &conf_string, _aftercheck, _copy, _file, _mem, _def)
189 #define CONF_OPTION_STRING(_short, _long, _mem, _def) \
190         CONF_OPTION_STRING_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
191
192 #define CONF_OPTION_STRING_LIST_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
193         CONF_OPTION(0, _short, _long, &conf_string_list, _aftercheck, _copy, _file, _mem, _def)
194 #define CONF_OPTION_STRING_LIST(_short, _long, _mem, _def) \
195         CONF_OPTION_STRING_LIST_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
196
197 #define CONF_OPTION_GRAB_FULL(_short, _long, _aftercheck, _copy, _file, _mem, _def) \
198         CONF_OPTION(0, _short, _long, &conf_grab, _aftercheck, _copy, _file, _mem, _def)
199 #define CONF_OPTION_GRAB(_short, _long, _mem, _def) \
200         CONF_OPTION_GRAB_FULL(_short, _long, NULL, NULL, NULL, _mem, _def)
201
202 #endif /* CONFIG_CONFIG_H */