configure: prefer libvorbis to libvorbisidec/tremor
[mplayer/greg.git] / m_option.h
blob58deb9be8a9206ba920d3395637b20979516d89b
1 /*
2 * This file is part of MPlayer.
4 * MPlayer is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * MPlayer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef MPLAYER_M_OPTION_H
20 #define MPLAYER_M_OPTION_H
22 #include <string.h>
23 #include <stddef.h>
25 /// \defgroup Options
26 /// m_option allows to parse, print and copy data of various types.
27 /// It is the base of the \ref OptionsStruct, \ref Config and
28 /// \ref Properties APIs.
29 ///@{
31 /// \file m_option.h
33 /// \ingroup OptionTypes
34 typedef struct m_option_type m_option_type_t;
35 typedef struct m_option m_option_t;
36 struct m_struct_st;
38 /// \defgroup OptionTypes Options types
39 /// \ingroup Options
40 ///@{
42 ///////////////////////////// Options types declarations ////////////////////////////
44 // Simple types
45 extern const m_option_type_t m_option_type_flag;
46 extern const m_option_type_t m_option_type_int;
47 extern const m_option_type_t m_option_type_int64;
48 extern const m_option_type_t m_option_type_intpair;
49 extern const m_option_type_t m_option_type_float;
50 extern const m_option_type_t m_option_type_double;
51 extern const m_option_type_t m_option_type_string;
52 extern const m_option_type_t m_option_type_string_list;
53 extern const m_option_type_t m_option_type_position;
54 extern const m_option_type_t m_option_type_time;
55 extern const m_option_type_t m_option_type_time_size;
56 extern const m_option_type_t m_option_type_choice;
58 extern const m_option_type_t m_option_type_print;
59 extern const m_option_type_t m_option_type_print_indirect;
60 extern const m_option_type_t m_option_type_print_func;
61 extern const m_option_type_t m_option_type_subconfig;
62 extern const m_option_type_t m_option_type_imgfmt;
63 extern const m_option_type_t m_option_type_afmt;
65 // Func-based types
66 extern const m_option_type_t m_option_type_func_param;
67 extern const m_option_type_t m_option_type_func;
69 /// Callback used to reset func options.
70 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
72 /// Callback used by m_option_type_func_full options.
73 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, const char *);
75 /// Callback used by m_option_type_func_param options.
76 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
78 /// Callback used by m_option_type_func options.
79 typedef int (*m_opt_func_t)(const m_option_t *);
81 // Backwards compatibility
82 typedef m_opt_default_func_t cfg_default_func_t;
83 typedef m_opt_func_full_t cfg_func_arg_param_t;
84 typedef m_opt_func_param_t cfg_func_param_t;
85 typedef m_opt_func_t cfg_func_t;
87 #define END_AT_NONE 0
88 #define END_AT_TIME 1
89 #define END_AT_SIZE 2
90 typedef struct {
91 double pos;
92 int type;
93 } m_time_size_t;
95 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
96 typedef struct {
97 /// Pointer to an array of pointer to some object type description struct.
98 void** list;
99 /// Offset of the object type name (char*) in the description struct.
100 void* name_off;
101 /// Offset of the object type info string (char*) in the description struct.
102 void* info_off;
103 /// \brief Offset of the object type parameter description (\ref m_struct_st)
104 /// in the description struct.
105 void* desc_off;
106 } m_obj_list_t;
108 /// The data type used by \ref m_option_type_obj_settings_list.
109 typedef struct m_obj_settings {
110 /// Type of the object.
111 char* name;
112 /// NULL terminated array of parameter/value pairs.
113 char** attribs;
114 } m_obj_settings_t;
116 /// A parser to set up a list of objects.
117 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
118 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
119 * the available object types.
121 extern const m_option_type_t m_option_type_obj_settings_list;
123 /// Extra definition needed for \ref m_option_type_obj_presets options.
124 typedef struct {
125 /// Description of the struct holding the presets.
126 struct m_struct_st* in_desc;
127 /// Description of the struct that should be set by the presets.
128 struct m_struct_st* out_desc;
129 /// Pointer to an array of structs defining the various presets.
130 void* presets;
131 /// Offset of the preset's name inside the in_struct.
132 void* name_off;
133 } m_obj_presets_t;
135 /// Set several fields in a struct at once.
136 /** For this two struct descriptions are used. One for the struct holding the
137 * preset and one for the struct beeing set. Every field present in both
138 * structs will be copied from the preset struct to the destination one.
139 * The option priv field (\ref m_option::priv) must point to a correctly
140 * filled \ref m_obj_presets_t.
142 extern const m_option_type_t m_option_type_obj_presets;
144 /// Parse an URL into a struct.
145 /** The option priv field (\ref m_option::priv) must point to a
146 * \ref m_struct_st describing which fields of the URL must be used.
148 extern const m_option_type_t m_option_type_custom_url;
150 /// Extra definition needed for \ref m_option_type_obj_params options.
151 typedef struct {
152 /// Field descriptions.
153 const struct m_struct_st* desc;
154 /// Field separator to use.
155 char separator;
156 } m_obj_params_t;
158 /// Parse a set of parameters.
159 /** Parameters are separated by the given separator and each one
160 * successively sets a field from the struct. The option priv field
161 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
163 extern const m_option_type_t m_option_type_obj_params;
165 typedef struct {
166 int start;
167 int end;
168 } m_span_t;
169 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
170 extern const m_obj_params_t m_span_params_def;
172 struct m_opt_choice_alternatives {
173 char *name;
174 int value;
178 // FIXME: backward compatibility
179 #define CONF_TYPE_FLAG (&m_option_type_flag)
180 #define CONF_TYPE_INT (&m_option_type_int)
181 #define CONF_TYPE_INT64 (&m_option_type_int64)
182 #define CONF_TYPE_FLOAT (&m_option_type_float)
183 #define CONF_TYPE_DOUBLE (&m_option_type_double)
184 #define CONF_TYPE_STRING (&m_option_type_string)
185 #define CONF_TYPE_FUNC (&m_option_type_func)
186 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
187 #define CONF_TYPE_PRINT (&m_option_type_print)
188 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
189 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
190 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
191 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
192 #define CONF_TYPE_POSITION (&m_option_type_position)
193 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
194 #define CONF_TYPE_AFMT (&m_option_type_afmt)
195 #define CONF_TYPE_SPAN (&m_option_type_span)
196 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
197 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
198 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
199 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
200 #define CONF_TYPE_TIME (&m_option_type_time)
201 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
203 /////////////////////////////////////////////////////////////////////////////////////////////
205 /// Option type description
206 struct m_option_type {
207 const char* name;
208 /// Syntax description, etc
209 const char* comments;
210 /// Size needed for the data.
211 unsigned int size;
212 /// See \ref OptionTypeFlags.
213 unsigned int flags;
215 /// Parse the data from a string.
216 /** It is the only required function, all others can be NULL.
218 * \param opt The option that is parsed.
219 * \param name The full option name.
220 * \param param The parameter to parse.
221 * \param dst Pointer to the memory where the data should be written.
222 * If NULL the parameter validity should still be checked.
223 * \param src Source of the option, see \ref OptionParserModes.
224 * \return On error a negative value is returned, on success the number of arguments
225 * consumed. For details see \ref OptionParserReturn.
227 int (*parse)(const m_option_t* opt,const char *name, const char *param, void* dst, int src);
229 /// Print back a value in string form.
230 /** \param opt The option to print.
231 * \param val Pointer to the memory holding the data to be printed.
232 * \return An allocated string containing the text value or (void*)-1
233 * on error.
235 char* (*print)(const m_option_t* opt, const void* val);
237 /** \name
238 * These functions are called to save/set/restore the status of the
239 * variables. The difference between the 3 only matters for types like
240 * \ref m_option_type_func where 'setting' needs to do more than just
241 * copying some data.
243 //@{
245 /// Update a save slot (dst) from the current value in the program (src).
246 /** \param opt The option to copy.
247 * \param dst Pointer to the destination memory.
248 * \param src Pointer to the source memory.
250 void (*save)(const m_option_t* opt,void* dst, const void* src);
252 /// Set the value in the program (dst) from a save slot.
253 /** \param opt The option to copy.
254 * \param dst Pointer to the destination memory.
255 * \param src Pointer to the source memory.
257 void (*set)(const m_option_t* opt,void* dst, const void* src);
259 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
260 /** \param opt The option to copy.
261 * \param dst Pointer to the destination memory.
262 * \param src Pointer to the source memory.
264 void (*copy)(const m_option_t* opt,void* dst, const void* src);
265 //@}
267 /// Free the data allocated for a save slot.
268 /** This is only needed for dynamic types like strings.
269 * \param dst Pointer to the data, usually a pointer that should be freed and
270 * set to NULL.
272 void (*free)(void* dst);
275 ///@}
277 /// Option description
278 /** \ingroup Options
280 struct m_option {
281 /// Option name.
282 const char *name;
284 /// Reserved for higher level APIs, it shouldn't be used by parsers.
285 /** The suboption parser and func types do use it. They should instead
286 * use the priv field but this was inherited from older versions of the
287 * config code.
289 void *p;
291 /// Option type.
292 const m_option_type_t* type;
294 /// See \ref OptionFlags.
295 unsigned int flags;
297 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
298 /// also be set.
299 double min;
301 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
302 /// also be set.
303 double max;
305 /// Type dependent data (for all kinds of extended settings).
306 /** This used to be a function pointer to hold a 'reverse to defaults' func.
307 * Now it can be used to pass any type of extra args needed by the parser.
308 * Passing a 'default func' is still valid for all func based option types.
310 void* priv;
312 int new;
314 int offset;
318 /// \defgroup OptionFlags Option flags
319 ///@{
321 /// The option has a minimum set in \ref m_option::min.
322 #define M_OPT_MIN (1<<0)
324 /// The option has a maximum set in \ref m_option::max.
325 #define M_OPT_MAX (1<<1)
327 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
328 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
330 /// The option is forbidden in config files.
331 #define M_OPT_NOCFG (1<<2)
333 /// The option is forbidden on the command line.
334 #define M_OPT_NOCMD (1<<3)
336 /// The option is global in the \ref Config.
337 /** It won't be saved on push and the command line parser will set it when
338 * it's parsed (i.e. it won't be set later)
339 * e.g options : -v, -quiet
341 #define M_OPT_GLOBAL (1<<4)
343 /// The \ref Config won't save this option on push.
344 /** It won't be saved on push but the command line parser will add it with
345 * its entry (i.e. it may be set later)
346 * e.g options : -include
348 #define M_OPT_NOSAVE (1<<5)
350 /// The option should be set during command line pre-parsing
351 #define M_OPT_PRE_PARSE (1<<6)
353 /// \defgroup OldOptionFlags Backward compatibility
355 /// These are kept for compatibility with older code.
356 /// @{
357 #define CONF_MIN M_OPT_MIN
358 #define CONF_MAX M_OPT_MAX
359 #define CONF_RANGE M_OPT_RANGE
360 #define CONF_NOCFG M_OPT_NOCFG
361 #define CONF_NOCMD M_OPT_NOCMD
362 #define CONF_GLOBAL M_OPT_GLOBAL
363 #define CONF_NOSAVE M_OPT_NOSAVE
364 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
365 ///@}
367 ///@}
369 /// \defgroup OptionTypeFlags Option type flags
370 /// \ingroup OptionTypes
372 /// These flags are used to describe special parser capabilities or behavior.
374 ///@{
376 /// Suboption parser flag.
377 /** When this flag is set, m_option::p should point to another m_option
378 * array. Only the parse function will be called. If dst is set, it should
379 * create/update an array of char* containg opt/val pairs. The options in
380 * the child array will then be set automatically by the \ref Config.
381 * Also note that suboptions may be directly accessed by using
382 * -option:subopt blah.
384 #define M_OPT_TYPE_HAS_CHILD (1<<0)
386 /// Wildcard matching flag.
387 /** If set the option type has a use for option names ending with a *
388 * (used for -aa*), this only affects the option name matching.
390 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
392 /// Dynamic data type.
393 /** This flag indicates that the data is dynamically allocated (m_option::p
394 * points to a pointer). It enables a little hack in the \ref Config wich
395 * replaces the initial value of such variables with a dynamic copy in case
396 * the initial value is statically allocated (pretty common with strings).
398 #define M_OPT_TYPE_DYNAMIC (1<<2)
400 /// Indirect option type.
401 /** If this is set the parse function doesn't directly return
402 * the wanted thing. Options use this if for some reasons they have to wait
403 * until the set call to be able to correctly set the target var.
404 * So for those types new values must first be parsed, then set to the target
405 * var. If this flag isn't set then new values can be parsed directly to the
406 * target var. It's used by the callback-based options as the callback call
407 * may append later on.
409 #define M_OPT_TYPE_INDIRECT (1<<3)
411 ///@}
413 ///////////////////////////// Parser flags ////////////////////////////////////////
415 /// \defgroup OptionParserModes Option parser modes
416 /// \ingroup Options
418 /// Some parsers behave differently depending on the mode passed in the src
419 /// parameter of m_option_type::parse. For example the flag type doesn't take
420 /// an argument when parsing from the command line.
421 ///@{
423 /// Set when parsing from a config file.
424 #define M_CONFIG_FILE 0
425 /// Set when parsing command line arguments.
426 #define M_COMMAND_LINE 1
427 /// Set when pre-parsing the command line
428 #define M_COMMAND_LINE_PRE_PARSE 2
430 ///@}
432 /// \defgroup OptionParserReturn Option parser return code
433 /// \ingroup Options
435 /// On success parsers return the number of arguments consumed: 0 or 1.
437 /// To indicate that MPlayer should exit without playing anything,
438 /// parsers return M_OPT_EXIT minus the number of parameters they
439 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
441 /// On error one of the following (negative) error codes is returned:
442 ///@{
444 /// For use by higher level APIs when the option name is invalid.
445 #define M_OPT_UNKNOWN -1
447 /// Returned when a parameter is needed but wasn't provided.
448 #define M_OPT_MISSING_PARAM -2
450 /// Returned when the given parameter couldn't be parsed.
451 #define M_OPT_INVALID -3
453 /// \brief Returned if the value is "out of range". The exact meaning may
454 /// vary from type to type.
455 #define M_OPT_OUT_OF_RANGE -4
457 /// Returned if the parser failed for any other reason than a bad parameter.
458 #define M_OPT_PARSER_ERR -5
460 /// Returned when MPlayer should exit. Used by various help stuff.
461 /** M_OPT_EXIT must be the lowest number on this list.
463 #define M_OPT_EXIT -6
465 /// \defgroup OldOptionParserReturn Backward compatibility
467 /// These are kept for compatibility with older code.
469 ///@{
470 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
471 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
472 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
473 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
474 ///@}
476 ///@}
478 /// Find the option matching the given name in the list.
479 /** \ingroup Options
480 * This function takes the possible wildcards into account (see
481 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
483 * \param list Pointer to an array of \ref m_option.
484 * \param name Name of the option.
485 * \return The matching option or NULL.
487 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
489 static inline void *m_option_get_ptr(const struct m_option *opt,
490 void *optstruct)
492 return opt->new ? (char *) optstruct + opt->offset : opt->p;
495 /// Helper to parse options, see \ref m_option_type::parse.
496 inline static int
497 m_option_parse(const m_option_t* opt,const char *name, const char *param, void* dst, int src) {
498 return opt->type->parse(opt,name,param,dst,src);
501 /// Helper to print options, see \ref m_option_type::print.
502 inline static char*
503 m_option_print(const m_option_t* opt, const void* val_ptr) {
504 if(opt->type->print)
505 return opt->type->print(opt,val_ptr);
506 else
507 return (char*)-1;
510 /// Helper around \ref m_option_type::copy.
511 inline static void
512 m_option_copy(const m_option_t* opt,void* dst, const void* src) {
513 if(opt->type->copy)
514 opt->type->copy(opt,dst,src);
515 else if(opt->type->size > 0)
516 memcpy(dst,src,opt->type->size);
519 /// Helper around \ref m_option_type::free.
520 inline static void
521 m_option_free(const m_option_t* opt,void* dst) {
522 if(opt->type->free)
523 opt->type->free(dst);
526 /*@}*/
529 * Parse a string as a timestamp.
531 * @param[in] str the string to parse.
532 * @param[out] time parsed time.
533 * @param[in] endchar return an error of the next character after the
534 * timestamp is neither nul nor endchar.
535 * @return Number of chars in the timestamp.
537 int parse_timestring(const char *str, double *time, char endchar);
539 #define OPT_FLAG_ON(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 0, 1, NULL, 1, offsetof(struct MPOpts, varname)}
540 #define OPT_FLAG_OFF(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 1, 0, NULL, 1, offsetof(struct MPOpts, varname)}
541 #define OPT_MAKE_FLAGS(optname, varname, flags) OPT_FLAG_ON(optname, varname, flags), OPT_FLAG_OFF("no" optname, varname, flags)
542 #define OPT_FLAG_CONSTANTS(optname, varname, flags, offvalue, value) {optname, NULL, &m_option_type_flag, flags, offvalue, value, NULL, 1, offsetof(struct MPOpts, varname)}
543 #define OPT_STRINGLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
544 #define OPT_INT(optname, varname, flags) {optname, NULL, &m_option_type_int, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
545 #define OPT_INTRANGE(optname, varname, flags, min, max) {optname, NULL, &m_option_type_int, (flags)|CONF_RANGE, min, max, NULL, 1, offsetof(struct MPOpts, varname)}
546 #define OPT_INTPAIR(optname, varname, flags) {optname, NULL, &m_option_type_intpair, (flags), 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
547 #define OPT_FLOATRANGE(optname, varname, flags, min, max) {optname, NULL, &m_option_type_float, (flags)|CONF_RANGE, min, max, NULL, 1, offsetof(struct MPOpts, varname)}
548 #define OPT_STRING(optname, varname, flags) {optname, NULL, &m_option_type_string, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
549 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) {optname, NULL, &m_option_type_obj_settings_list, flags, 0, 0, objlist, 1, offsetof(struct MPOpts, varname)}
550 #define OPT_AUDIOFORMAT(optname, varname, flags) {optname, NULL, &m_option_type_afmt, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
551 #define OPT_HELPER_REMOVEPAREN(...) __VA_ARGS__
552 #define OPT_CHOICE(optname, varname, flags, choices) {optname, NULL, &m_option_type_choice, flags, 0, 0, &(const struct m_opt_choice_alternatives[]){OPT_HELPER_REMOVEPAREN choices, {NULL}}, 1, offsetof(struct MPOpts, varname)}
554 #endif /* MPLAYER_M_OPTION_H */