Fix dvdnav call broken in pause changes
[mplayer/glamo.git] / m_option.h
blob0cf55fbaf16206d3e609c5abb4111ba6c696ea90
1 #ifndef MPLAYER_M_OPTION_H
2 #define MPLAYER_M_OPTION_H
4 #include <string.h>
5 #include <stddef.h>
7 /// \defgroup Options
8 /// m_option allows to parse, print and copy data of various types.
9 /// It is the base of the \ref OptionsStruct, \ref Config and
10 /// \ref Properties APIs.
11 ///@{
13 /// \file m_option.h
15 /// \ingroup OptionTypes
16 typedef struct m_option_type m_option_type_t;
17 typedef struct m_option m_option_t;
18 struct m_struct_st;
20 /// \defgroup OptionTypes Options types
21 /// \ingroup Options
22 ///@{
24 ///////////////////////////// Options types declarations ////////////////////////////
26 // Simple types
27 extern const m_option_type_t m_option_type_flag;
28 extern const m_option_type_t m_option_type_int;
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_FLOAT (&m_option_type_float)
157 #define CONF_TYPE_DOUBLE (&m_option_type_double)
158 #define CONF_TYPE_STRING (&m_option_type_string)
159 #define CONF_TYPE_FUNC (&m_option_type_func)
160 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
161 #define CONF_TYPE_PRINT (&m_option_type_print)
162 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
163 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
164 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
165 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
166 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
167 #define CONF_TYPE_POSITION (&m_option_type_position)
168 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
169 #define CONF_TYPE_AFMT (&m_option_type_afmt)
170 #define CONF_TYPE_SPAN (&m_option_type_span)
171 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
172 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
173 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
174 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
175 #define CONF_TYPE_TIME (&m_option_type_time)
176 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
178 /////////////////////////////////////////////////////////////////////////////////////////////
180 /// Option type description
181 struct m_option_type {
182 char* name;
183 /// Syntax description, etc
184 char* comments;
185 /// Size needed for the data.
186 unsigned int size;
187 /// See \ref OptionTypeFlags.
188 unsigned int flags;
190 /// Parse the data from a string.
191 /** It is the only required function, all others can be NULL.
193 * \param opt The option that is parsed.
194 * \param name The full option name.
195 * \param param The parameter to parse.
196 * \param dst Pointer to the memory where the data should be written.
197 * If NULL the parameter validity should still be checked.
198 * \param src Source of the option, see \ref OptionParserModes.
199 * \return On error a negative value is returned, on success the number of arguments
200 * consumed. For details see \ref OptionParserReturn.
202 int (*parse)(const m_option_t* opt,const char *name, char *param, void* dst, int src);
204 /// Print back a value in string form.
205 /** \param opt The option to print.
206 * \param val Pointer to the memory holding the data to be printed.
207 * \return An allocated string containing the text value or (void*)-1
208 * on error.
210 char* (*print)(const m_option_t* opt, const void* val);
212 /** \name
213 * These functions are called to save/set/restore the status of the
214 * variables. The difference between the 3 only matters for types like
215 * \ref m_option_type_func where 'setting' needs to do more than just
216 * copying some data.
218 //@{
220 /// Update a save slot (dst) from the current value in the program (src).
221 /** \param opt The option to copy.
222 * \param dst Pointer to the destination memory.
223 * \param src Pointer to the source memory.
225 void (*save)(const m_option_t* opt,void* dst, void* src);
227 /// Set the value in the program (dst) from a save slot.
228 /** \param opt The option to copy.
229 * \param dst Pointer to the destination memory.
230 * \param src Pointer to the source memory.
232 void (*set)(const m_option_t* opt,void* dst, void* src);
234 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
235 /** \param opt The option to copy.
236 * \param dst Pointer to the destination memory.
237 * \param src Pointer to the source memory.
239 void (*copy)(const m_option_t* opt,void* dst, void* src);
240 //@}
242 /// Free the data allocated for a save slot.
243 /** This is only needed for dynamic types like strings.
244 * \param dst Pointer to the data, usually a pointer that should be freed and
245 * set to NULL.
247 void (*free)(void* dst);
250 ///@}
252 /// Option description
253 /** \ingroup Options
255 struct m_option {
256 /// Option name.
257 const char *name;
259 /// Reserved for higher level APIs, it shouldn't be used by parsers.
260 /** The suboption parser and func types do use it. They should instead
261 * use the priv field but this was inherited from older versions of the
262 * config code.
264 void *p;
266 /// Option type.
267 const m_option_type_t* type;
269 /// See \ref OptionFlags.
270 unsigned int flags;
272 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
273 /// also be set.
274 double min;
276 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
277 /// also be set.
278 double max;
280 /// Type dependent data (for all kinds of extended settings).
281 /** This used to be a function pointer to hold a 'reverse to defaults' func.
282 * Now it can be used to pass any type of extra args needed by the parser.
283 * Passing a 'default func' is still valid for all func based option types.
285 void* priv;
287 int new;
289 int offset;
293 /// \defgroup OptionFlags Option flags
294 ///@{
296 /// The option has a minimum set in \ref m_option::min.
297 #define M_OPT_MIN (1<<0)
299 /// The option has a maximum set in \ref m_option::max.
300 #define M_OPT_MAX (1<<1)
302 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
303 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
305 /// The option is forbidden in config files.
306 #define M_OPT_NOCFG (1<<2)
308 /// The option is forbidden on the command line.
309 #define M_OPT_NOCMD (1<<3)
311 /// The option is global in the \ref Config.
312 /** It won't be saved on push and the command line parser will set it when
313 * it's parsed (i.e. it won't be set later)
314 * e.g options : -v, -quiet
316 #define M_OPT_GLOBAL (1<<4)
318 /// The \ref Config won't save this option on push.
319 /** It won't be saved on push but the command line parser will add it with
320 * its entry (i.e. it may be set later)
321 * e.g options : -include
323 #define M_OPT_NOSAVE (1<<5)
325 /// \brief The \ref Config will emulate the old behavior by pushing the
326 /// option only if it was set by the user.
327 #define M_OPT_OLD (1<<6)
329 /// The option should be set during command line pre-parsing
330 #define M_OPT_PRE_PARSE (1<<7)
332 /// \defgroup OldOptionFlags Backward compatibility
334 /// These are kept for compatibility with older code.
335 /// @{
336 #define CONF_MIN M_OPT_MIN
337 #define CONF_MAX M_OPT_MAX
338 #define CONF_RANGE M_OPT_RANGE
339 #define CONF_NOCFG M_OPT_NOCFG
340 #define CONF_NOCMD M_OPT_NOCMD
341 #define CONF_GLOBAL M_OPT_GLOBAL
342 #define CONF_NOSAVE M_OPT_NOSAVE
343 #define CONF_OLD M_OPT_OLD
344 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
345 ///@}
347 ///@}
349 /// \defgroup OptionTypeFlags Option type flags
350 /// \ingroup OptionTypes
352 /// These flags are used to describe special parser capabilities or behavior.
354 ///@{
356 /// Suboption parser flag.
357 /** When this flag is set, m_option::p should point to another m_option
358 * array. Only the parse function will be called. If dst is set, it should
359 * create/update an array of char* containg opt/val pairs. The options in
360 * the child array will then be set automatically by the \ref Config.
361 * Also note that suboptions may be directly accessed by using
362 * -option:subopt blah.
364 #define M_OPT_TYPE_HAS_CHILD (1<<0)
366 /// Wildcard matching flag.
367 /** If set the option type has a use for option names ending with a *
368 * (used for -aa*), this only affects the option name matching.
370 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
372 /// Dynamic data type.
373 /** This flag indicates that the data is dynamically allocated (m_option::p
374 * points to a pointer). It enables a little hack in the \ref Config wich
375 * replaces the initial value of such variables with a dynamic copy in case
376 * the initial value is statically allocated (pretty common with strings).
378 #define M_OPT_TYPE_DYNAMIC (1<<2)
380 /// Indirect option type.
381 /** If this is set the parse function doesn't directly return
382 * the wanted thing. Options use this if for some reasons they have to wait
383 * until the set call to be able to correctly set the target var.
384 * So for those types new values must first be parsed, then set to the target
385 * var. If this flag isn't set then new values can be parsed directly to the
386 * target var. It's used by the callback-based options as the callback call
387 * may append later on.
389 #define M_OPT_TYPE_INDIRECT (1<<3)
391 ///@}
393 ///////////////////////////// Parser flags ////////////////////////////////////////
395 /// \defgroup OptionParserModes Option parser modes
396 /// \ingroup Options
398 /// Some parsers behave differently depending on the mode passed in the src
399 /// parameter of m_option_type::parse. For example the flag type doesn't take
400 /// an argument when parsing from the command line.
401 ///@{
403 /// Set when parsing from a config file.
404 #define M_CONFIG_FILE 0
405 /// Set when parsing command line arguments.
406 #define M_COMMAND_LINE 1
407 /// Set when pre-parsing the command line
408 #define M_COMMAND_LINE_PRE_PARSE 2
410 ///@}
412 /// \defgroup OptionParserReturn Option parser return code
413 /// \ingroup Options
415 /// On success parsers return the number of arguments consumed: 0 or 1.
417 /// To indicate that MPlayer should exit without playing anything,
418 /// parsers return M_OPT_EXIT minus the number of parameters they
419 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
421 /// On error one of the following (negative) error codes is returned:
422 ///@{
424 /// For use by higher level APIs when the option name is invalid.
425 #define M_OPT_UNKNOWN -1
427 /// Returned when a parameter is needed but wasn't provided.
428 #define M_OPT_MISSING_PARAM -2
430 /// Returned when the given parameter couldn't be parsed.
431 #define M_OPT_INVALID -3
433 /// \brief Returned if the value is "out of range". The exact meaning may
434 /// vary from type to type.
435 #define M_OPT_OUT_OF_RANGE -4
437 /// Returned if the parser failed for any other reason than a bad parameter.
438 #define M_OPT_PARSER_ERR -5
440 /// Returned when MPlayer should exit. Used by various help stuff.
441 /** M_OPT_EXIT must be the lowest number on this list.
443 #define M_OPT_EXIT -6
445 /// \defgroup OldOptionParserReturn Backward compatibility
447 /// These are kept for compatibility with older code.
449 ///@{
450 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
451 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
452 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
453 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
454 ///@}
456 ///@}
458 /// Find the option matching the given name in the list.
459 /** \ingroup Options
460 * This function takes the possible wildcards into account (see
461 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
463 * \param list Pointer to an array of \ref m_option.
464 * \param name Name of the option.
465 * \return The matching option or NULL.
467 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
469 /// Helper to parse options, see \ref m_option_type::parse.
470 inline static int
471 m_option_parse(const m_option_t* opt,const char *name, char *param, void* dst, int src) {
472 return opt->type->parse(opt,name,param,dst,src);
475 /// Helper to print options, see \ref m_option_type::print.
476 inline static char*
477 m_option_print(const m_option_t* opt, const void* val_ptr) {
478 if(opt->type->print)
479 return opt->type->print(opt,val_ptr);
480 else
481 return (char*)-1;
484 /// Helper around \ref m_option_type::copy.
485 inline static void
486 m_option_copy(const m_option_t* opt,void* dst, void* src) {
487 if(opt->type->copy)
488 opt->type->copy(opt,dst,src);
489 else if(opt->type->size > 0)
490 memcpy(dst,src,opt->type->size);
493 /// Helper around \ref m_option_type::free.
494 inline static void
495 m_option_free(const m_option_t* opt,void* dst) {
496 if(opt->type->free)
497 opt->type->free(dst);
500 /*@}*/
502 #define OPT_FLAG_ON(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 0, 1, NULL, 1, offsetof(struct MPOpts, varname)}
503 #define OPT_FLAG_OFF(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 1, 0, NULL, 1, offsetof(struct MPOpts, varname)}
504 #define OPT_FLAG_CONSTANTS(optname, varname, flags, offvalue, value) {optname, NULL, &m_option_type_flag, flags, offvalue, value, NULL, 1, offsetof(struct MPOpts, varname)}
505 #define OPT_STRINGLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
506 #define OPT_INT(optname, varname, flags) {optname, NULL, &m_option_type_int, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
507 #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)}
508 #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)}
509 #define OPT_STRING(optname, varname, flags) {optname, NULL, &m_option_type_string, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
510 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) {optname, NULL, &m_option_type_obj_settings_list, flags, 0, 0, objlist, 1, offsetof(struct MPOpts, varname)}
512 #endif /* MPLAYER_M_OPTION_H */