Sync with latest FFmpeg changes.
[mplayer/glamo.git] / m_option.h
blobe12c91bed8bcd8ffc012ebefb6e4d455dfb68811
1 #ifndef MPLAYER_M_OPTION_H
2 #define MPLAYER_M_OPTION_H
4 #include <string.h>
6 /// \defgroup Options
7 /// m_option allows to parse, print and copy data of various types.
8 /// It is the base of the \ref OptionsStruct, \ref Config and
9 /// \ref Properties APIs.
10 ///@{
12 /// \file m_option.h
14 /// \ingroup OptionTypes
15 typedef struct m_option_type m_option_type_t;
16 typedef struct m_option m_option_t;
17 struct m_struct_st;
19 /// \defgroup OptionTypes Options types
20 /// \ingroup Options
21 ///@{
23 ///////////////////////////// Options types declarations ////////////////////////////
25 // Simple types
26 extern const m_option_type_t m_option_type_flag;
27 extern const m_option_type_t m_option_type_int;
28 extern const m_option_type_t m_option_type_int64;
29 extern const m_option_type_t m_option_type_float;
30 extern const m_option_type_t m_option_type_double;
31 extern const m_option_type_t m_option_type_string;
32 extern const m_option_type_t m_option_type_string_list;
33 extern const m_option_type_t m_option_type_position;
34 extern const m_option_type_t m_option_type_time;
35 extern const m_option_type_t m_option_type_time_size;
37 extern const m_option_type_t m_option_type_print;
38 extern const m_option_type_t m_option_type_print_indirect;
39 extern const m_option_type_t m_option_type_print_func;
40 extern const m_option_type_t m_option_type_subconfig;
41 extern const m_option_type_t m_option_type_imgfmt;
42 extern const m_option_type_t m_option_type_afmt;
44 // Func-based types
45 extern const m_option_type_t m_option_type_func_full;
46 extern const m_option_type_t m_option_type_func_param;
47 extern const m_option_type_t m_option_type_func;
49 /// Callback used to reset func options.
50 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
52 /// Callback used by m_option_type_func_full options.
53 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, char *);
55 /// Callback used by m_option_type_func_param options.
56 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
58 /// Callback used by m_option_type_func options.
59 typedef int (*m_opt_func_t)(const m_option_t *);
61 // Backwards compatibility
62 typedef m_opt_default_func_t cfg_default_func_t;
63 typedef m_opt_func_full_t cfg_func_arg_param_t;
64 typedef m_opt_func_param_t cfg_func_param_t;
65 typedef m_opt_func_t cfg_func_t;
67 #define END_AT_NONE 0
68 #define END_AT_TIME 1
69 #define END_AT_SIZE 2
70 typedef struct {
71 double pos;
72 int type;
73 } m_time_size_t;
75 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
76 typedef struct {
77 /// Pointer to an array of pointer to some object type description struct.
78 void** list;
79 /// Offset of the object type name (char*) in the description struct.
80 void* name_off;
81 /// Offset of the object type info string (char*) in the description struct.
82 void* info_off;
83 /// \brief Offset of the object type parameter description (\ref m_struct_st)
84 /// in the description struct.
85 void* desc_off;
86 } m_obj_list_t;
88 /// The data type used by \ref m_option_type_obj_settings_list.
89 typedef struct m_obj_settings {
90 /// Type of the object.
91 char* name;
92 /// NULL terminated array of parameter/value pairs.
93 char** attribs;
94 } m_obj_settings_t;
96 /// A parser to set up a list of objects.
97 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
98 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
99 * the available object types.
101 extern const m_option_type_t m_option_type_obj_settings_list;
103 /// Extra definition needed for \ref m_option_type_obj_presets options.
104 typedef struct {
105 /// Description of the struct holding the presets.
106 struct m_struct_st* in_desc;
107 /// Description of the struct that should be set by the presets.
108 struct m_struct_st* out_desc;
109 /// Pointer to an array of structs defining the various presets.
110 void* presets;
111 /// Offset of the preset's name inside the in_struct.
112 void* name_off;
113 } m_obj_presets_t;
115 /// Set several fields in a struct at once.
116 /** For this two struct descriptions are used. One for the struct holding the
117 * preset and one for the struct beeing set. Every field present in both
118 * structs will be copied from the preset struct to the destination one.
119 * The option priv field (\ref m_option::priv) must point to a correctly
120 * filled \ref m_obj_presets_t.
122 extern const m_option_type_t m_option_type_obj_presets;
124 /// Parse an URL into a struct.
125 /** The option priv field (\ref m_option::priv) must point to a
126 * \ref m_struct_st describing which fields of the URL must be used.
128 extern const m_option_type_t m_option_type_custom_url;
130 /// Extra definition needed for \ref m_option_type_obj_params options.
131 typedef struct {
132 /// Field descriptions.
133 const struct m_struct_st* desc;
134 /// Field separator to use.
135 char separator;
136 } m_obj_params_t;
138 /// Parse a set of parameters.
139 /** Parameters are separated by the given separator and each one
140 * successively sets a field from the struct. The option priv field
141 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
143 extern const m_option_type_t m_option_type_obj_params;
145 typedef struct {
146 int start;
147 int end;
148 } m_span_t;
149 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
150 extern const m_obj_params_t m_span_params_def;
153 // FIXME: backward compatibility
154 #define CONF_TYPE_FLAG (&m_option_type_flag)
155 #define CONF_TYPE_INT (&m_option_type_int)
156 #define CONF_TYPE_INT64 (&m_option_type_int64)
157 #define CONF_TYPE_FLOAT (&m_option_type_float)
158 #define CONF_TYPE_DOUBLE (&m_option_type_double)
159 #define CONF_TYPE_STRING (&m_option_type_string)
160 #define CONF_TYPE_FUNC (&m_option_type_func)
161 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
162 #define CONF_TYPE_PRINT (&m_option_type_print)
163 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
164 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
165 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
166 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
167 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
168 #define CONF_TYPE_POSITION (&m_option_type_position)
169 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
170 #define CONF_TYPE_AFMT (&m_option_type_afmt)
171 #define CONF_TYPE_SPAN (&m_option_type_span)
172 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
173 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
174 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
175 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
176 #define CONF_TYPE_TIME (&m_option_type_time)
177 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
179 /////////////////////////////////////////////////////////////////////////////////////////////
181 /// Option type description
182 struct m_option_type {
183 char* name;
184 /// Syntax description, etc
185 char* comments;
186 /// Size needed for the data.
187 unsigned int size;
188 /// See \ref OptionTypeFlags.
189 unsigned int flags;
191 /// Parse the data from a string.
192 /** It is the only required function, all others can be NULL.
194 * \param opt The option that is parsed.
195 * \param name The full option name.
196 * \param param The parameter to parse.
197 * \param dst Pointer to the memory where the data should be written.
198 * If NULL the parameter validity should still be checked.
199 * \param src Source of the option, see \ref OptionParserModes.
200 * \return On error a negative value is returned, on success the number of arguments
201 * consumed. For details see \ref OptionParserReturn.
203 int (*parse)(const m_option_t* opt,const char *name, char *param, void* dst, int src);
205 /// Print back a value in string form.
206 /** \param opt The option to print.
207 * \param val Pointer to the memory holding the data to be printed.
208 * \return An allocated string containing the text value or (void*)-1
209 * on error.
211 char* (*print)(const m_option_t* opt, const void* val);
213 /** \name
214 * These functions are called to save/set/restore the status of the
215 * variables. The difference between the 3 only matters for types like
216 * \ref m_option_type_func where 'setting' needs to do more than just
217 * copying some data.
219 //@{
221 /// Update a save slot (dst) from the current value in the program (src).
222 /** \param opt The option to copy.
223 * \param dst Pointer to the destination memory.
224 * \param src Pointer to the source memory.
226 void (*save)(const m_option_t* opt,void* dst, void* src);
228 /// Set the value in the program (dst) from a save slot.
229 /** \param opt The option to copy.
230 * \param dst Pointer to the destination memory.
231 * \param src Pointer to the source memory.
233 void (*set)(const m_option_t* opt,void* dst, void* src);
235 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
236 /** \param opt The option to copy.
237 * \param dst Pointer to the destination memory.
238 * \param src Pointer to the source memory.
240 void (*copy)(const m_option_t* opt,void* dst, void* src);
241 //@}
243 /// Free the data allocated for a save slot.
244 /** This is only needed for dynamic types like strings.
245 * \param dst Pointer to the data, usually a pointer that should be freed and
246 * set to NULL.
248 void (*free)(void* dst);
251 ///@}
253 /// Option description
254 /** \ingroup Options
256 struct m_option {
257 /// Option name.
258 const char *name;
260 /// Reserved for higher level APIs, it shouldn't be used by parsers.
261 /** The suboption parser and func types do use it. They should instead
262 * use the priv field but this was inherited from older versions of the
263 * config code.
265 void *p;
267 /// Option type.
268 const m_option_type_t* type;
270 /// See \ref OptionFlags.
271 unsigned int flags;
273 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
274 /// also be set.
275 double min;
277 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
278 /// also be set.
279 double max;
281 /// Type dependent data (for all kinds of extended settings).
282 /** This used to be a function pointer to hold a 'reverse to defaults' func.
283 * Now it can be used to pass any type of extra args needed by the parser.
284 * Passing a 'default func' is still valid for all func based option types.
286 void* priv;
290 /// \defgroup OptionFlags Option flags
291 ///@{
293 /// The option has a minimum set in \ref m_option::min.
294 #define M_OPT_MIN (1<<0)
296 /// The option has a maximum set in \ref m_option::max.
297 #define M_OPT_MAX (1<<1)
299 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
300 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
302 /// The option is forbidden in config files.
303 #define M_OPT_NOCFG (1<<2)
305 /// The option is forbidden on the command line.
306 #define M_OPT_NOCMD (1<<3)
308 /// The option is global in the \ref Config.
309 /** It won't be saved on push and the command line parser will set it when
310 * it's parsed (i.e. it won't be set later)
311 * e.g options : -v, -quiet
313 #define M_OPT_GLOBAL (1<<4)
315 /// The \ref Config won't save this option on push.
316 /** It won't be saved on push but the command line parser will add it with
317 * its entry (i.e. it may be set later)
318 * e.g options : -include
320 #define M_OPT_NOSAVE (1<<5)
322 /// \brief The \ref Config will emulate the old behavior by pushing the
323 /// option only if it was set by the user.
324 #define M_OPT_OLD (1<<6)
326 /// The option should be set during command line pre-parsing
327 #define M_OPT_PRE_PARSE (1<<7)
329 /// \defgroup OldOptionFlags Backward compatibility
331 /// These are kept for compatibility with older code.
332 /// @{
333 #define CONF_MIN M_OPT_MIN
334 #define CONF_MAX M_OPT_MAX
335 #define CONF_RANGE M_OPT_RANGE
336 #define CONF_NOCFG M_OPT_NOCFG
337 #define CONF_NOCMD M_OPT_NOCMD
338 #define CONF_GLOBAL M_OPT_GLOBAL
339 #define CONF_NOSAVE M_OPT_NOSAVE
340 #define CONF_OLD M_OPT_OLD
341 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
342 ///@}
344 ///@}
346 /// \defgroup OptionTypeFlags Option type flags
347 /// \ingroup OptionTypes
349 /// These flags are used to describe special parser capabilities or behavior.
351 ///@{
353 /// Suboption parser flag.
354 /** When this flag is set, m_option::p should point to another m_option
355 * array. Only the parse function will be called. If dst is set, it should
356 * create/update an array of char* containg opt/val pairs. The options in
357 * the child array will then be set automatically by the \ref Config.
358 * Also note that suboptions may be directly accessed by using
359 * -option:subopt blah.
361 #define M_OPT_TYPE_HAS_CHILD (1<<0)
363 /// Wildcard matching flag.
364 /** If set the option type has a use for option names ending with a *
365 * (used for -aa*), this only affects the option name matching.
367 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
369 /// Dynamic data type.
370 /** This flag indicates that the data is dynamically allocated (m_option::p
371 * points to a pointer). It enables a little hack in the \ref Config wich
372 * replaces the initial value of such variables with a dynamic copy in case
373 * the initial value is statically allocated (pretty common with strings).
375 #define M_OPT_TYPE_DYNAMIC (1<<2)
377 /// Indirect option type.
378 /** If this is set the parse function doesn't directly return
379 * the wanted thing. Options use this if for some reasons they have to wait
380 * until the set call to be able to correctly set the target var.
381 * So for those types new values must first be parsed, then set to the target
382 * var. If this flag isn't set then new values can be parsed directly to the
383 * target var. It's used by the callback-based options as the callback call
384 * may append later on.
386 #define M_OPT_TYPE_INDIRECT (1<<3)
388 ///@}
390 ///////////////////////////// Parser flags ////////////////////////////////////////
392 /// \defgroup OptionParserModes Option parser modes
393 /// \ingroup Options
395 /// Some parsers behave differently depending on the mode passed in the src
396 /// parameter of m_option_type::parse. For example the flag type doesn't take
397 /// an argument when parsing from the command line.
398 ///@{
400 /// Set when parsing from a config file.
401 #define M_CONFIG_FILE 0
402 /// Set when parsing command line arguments.
403 #define M_COMMAND_LINE 1
404 /// Set when pre-parsing the command line
405 #define M_COMMAND_LINE_PRE_PARSE 2
407 ///@}
409 /// \defgroup OptionParserReturn Option parser return code
410 /// \ingroup Options
412 /// On success parsers return the number of arguments consumed: 0 or 1.
414 /// To indicate that MPlayer should exit without playing anything,
415 /// parsers return M_OPT_EXIT minus the number of parameters they
416 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
418 /// On error one of the following (negative) error codes is returned:
419 ///@{
421 /// For use by higher level APIs when the option name is invalid.
422 #define M_OPT_UNKNOWN -1
424 /// Returned when a parameter is needed but wasn't provided.
425 #define M_OPT_MISSING_PARAM -2
427 /// Returned when the given parameter couldn't be parsed.
428 #define M_OPT_INVALID -3
430 /// \brief Returned if the value is "out of range". The exact meaning may
431 /// vary from type to type.
432 #define M_OPT_OUT_OF_RANGE -4
434 /// Returned if the parser failed for any other reason than a bad parameter.
435 #define M_OPT_PARSER_ERR -5
437 /// Returned when MPlayer should exit. Used by various help stuff.
438 /** M_OPT_EXIT must be the lowest number on this list.
440 #define M_OPT_EXIT -6
442 /// \defgroup OldOptionParserReturn Backward compatibility
444 /// These are kept for compatibility with older code.
446 ///@{
447 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
448 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
449 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
450 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
451 ///@}
453 ///@}
455 /// Find the option matching the given name in the list.
456 /** \ingroup Options
457 * This function takes the possible wildcards into account (see
458 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
460 * \param list Pointer to an array of \ref m_option.
461 * \param name Name of the option.
462 * \return The matching option or NULL.
464 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
466 /// Helper to parse options, see \ref m_option_type::parse.
467 inline static int
468 m_option_parse(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
469 return opt->type->parse(opt,name,param,dst,src);
472 /// Helper to print options, see \ref m_option_type::print.
473 inline static char*
474 m_option_print(const m_option_t* opt, const void* val_ptr) {
475 if(opt->type->print)
476 return opt->type->print(opt,val_ptr);
477 else
478 return (char*)-1;
481 /// Helper around \ref m_option_type::save.
482 inline static void
483 m_option_save(const m_option_t* opt,void* dst, void* src) {
484 if(opt->type->save)
485 opt->type->save(opt,dst,src);
488 /// Helper around \ref m_option_type::set.
489 inline static void
490 m_option_set(const m_option_t* opt,void* dst, void* src) {
491 if(opt->type->set)
492 opt->type->set(opt,dst,src);
495 /// Helper around \ref m_option_type::copy.
496 inline static void
497 m_option_copy(const m_option_t* opt,void* dst, void* src) {
498 if(opt->type->copy)
499 opt->type->copy(opt,dst,src);
500 else if(opt->type->size > 0)
501 memcpy(dst,src,opt->type->size);
504 /// Helper around \ref m_option_type::free.
505 inline static void
506 m_option_free(const m_option_t* opt,void* dst) {
507 if(opt->type->free)
508 opt->type->free(dst);
511 /*@}*/
513 #endif /* MPLAYER_M_OPTION_H */