uvtd: vt: implement VT_GETMODE/SETMODE ioctl state-tracking
[platform/upstream/kmscon.git] / src / conf.h
1 /*
2  * Configuration Parsers
3  *
4  * Copyright (c) 2012-2013 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. It provides most basic types but can be extended on the fly
31  * with more advanced types.
32  */
33
34 #ifndef CONF_CONF_H
35 #define CONF_CONF_H
36
37 #include <stdbool.h>
38 #include <stdlib.h>
39 #include "shl_misc.h"
40
41 struct conf_type;
42 struct conf_option;
43 struct conf_ctx;
44
45 /* Conf Types */
46
47 #define CONF_HAS_ARG            0x0001
48
49 struct conf_type {
50         unsigned int flags;
51         void (*set_default) (struct conf_option *opt);
52         void (*free) (struct conf_option *opt);
53         int (*parse) (struct conf_option *opt, bool on, const char *arg);
54         int (*copy) (struct conf_option *opt, const struct conf_option *src);
55 };
56
57 /*
58  * Bool: expects "mem" to point to a "bool"
59  * Initial state is "false".
60  */
61
62 extern const struct conf_type conf_bool;
63
64 /*
65  * Int: expects "mem" to point to an "int"
66  * Initial state is "0".
67  */
68
69 extern const struct conf_type conf_int;
70
71 /*
72  * Uint: expects "mem" to point to an "uint"
73  * Initial state is "0"
74  */
75
76 extern const struct conf_type conf_uint;
77
78 /*
79  * String: expects "mem" to point to an "char*"
80  * Initial state is NULL. Memory is allocated by the parser and a string is
81  * always zero-terminated.
82  */
83
84 extern const struct conf_type conf_string;
85
86 /*
87  * Stringlist: expects "mem" to point to an "char**"
88  * Initial state is NULL. The list is NULL-terminated and each entry is a
89  * zero-terminated string. Memory is allocated by the parser.
90  */
91
92 extern const struct conf_type conf_string_list;
93
94 /*
95  * Grabs: expects "mem" to point to an "struct conf_grab*"
96  * Initial state is NULL. See below for the type definition. The memory for the
97  * type is allocated by the parser.
98  * Two small helpers are available to ease the use.
99  */
100
101 extern const struct conf_type conf_grab;
102
103 struct conf_grab {
104         unsigned int num;
105         unsigned int *mods;
106         unsigned int *num_syms;
107         uint32_t **keysyms;
108 };
109
110 static inline bool conf_grab_matches(const struct conf_grab *grab,
111                                      unsigned int ev_mods,
112                                      unsigned int ev_num_syms,
113                                      const uint32_t *ev_syms)
114 {
115         return shl_grab_has_match(ev_mods, ev_num_syms, ev_syms,
116                                   grab->num, grab->mods, grab->num_syms,
117                                   grab->keysyms);
118 }
119
120 #define CONF_SINGLE_GRAB(_mods, _sym) { \
121                 .num = 1, \
122                 .mods = (unsigned int[]) { (_mods) }, \
123                 .num_syms = (unsigned int[]) { 1 }, \
124                 .keysyms = (uint32_t*[]) { (uint32_t[]) { (_sym) } }, \
125         }
126
127 /*
128  * Configuration Context
129  * A configuration context is initialized with an array of config-options and
130  * then can be used to parse different sources. The backing memory is managed by
131  * the user, not by this context.
132  * All options are set to their default values on startup and reset.
133  */
134
135 struct conf_ctx;
136
137 int conf_ctx_new(struct conf_ctx **out, const struct conf_option *opts,
138                  size_t onum, void *mem);
139 void conf_ctx_free(struct conf_ctx *ctx);
140 void conf_ctx_reset(struct conf_ctx *ctx);
141 void *conf_ctx_get_mem(struct conf_ctx *ctx);
142
143 int conf_ctx_parse_ctx(struct conf_ctx *ctx, const struct conf_ctx *src);
144 int conf_ctx_parse_argv(struct conf_ctx *ctx, int argc, char **argv);
145 int conf_ctx_parse_file(struct conf_ctx *ctx, const char *format, ...);
146
147 /*
148  * Configuration Options
149  * A configuration option specifies the name of the option, the type, the
150  * backing memory, the default value and more. Each option is represented by
151  * this structure.
152  */
153
154 #define CONF_LOCKED             0x0001
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 /* CONF_CONF_H */