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