sync with en/mplayer.1 rev. 30822
[mplayer.git] / m_option.h
bloba70d8638d2b537b8d6ec01096bb79bf0929c1477
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>
24 /// \defgroup Options
25 /// m_option allows to parse, print and copy data of various types.
26 /// It is the base of the \ref OptionsStruct, \ref Config and
27 /// \ref Properties APIs.
28 ///@{
30 /// \file m_option.h
32 /// \ingroup OptionTypes
33 typedef struct m_option_type m_option_type_t;
34 typedef struct m_option m_option_t;
35 struct m_struct_st;
37 /// \defgroup OptionTypes Options types
38 /// \ingroup Options
39 ///@{
41 ///////////////////////////// Options types declarations ////////////////////////////
43 // Simple types
44 extern const m_option_type_t m_option_type_flag;
45 extern const m_option_type_t m_option_type_int;
46 extern const m_option_type_t m_option_type_int64;
47 extern const m_option_type_t m_option_type_float;
48 extern const m_option_type_t m_option_type_double;
49 extern const m_option_type_t m_option_type_string;
50 extern const m_option_type_t m_option_type_string_list;
51 extern const m_option_type_t m_option_type_position;
52 extern const m_option_type_t m_option_type_time;
53 extern const m_option_type_t m_option_type_time_size;
55 extern const m_option_type_t m_option_type_print;
56 extern const m_option_type_t m_option_type_print_indirect;
57 extern const m_option_type_t m_option_type_print_func;
58 extern const m_option_type_t m_option_type_subconfig;
59 extern const m_option_type_t m_option_type_imgfmt;
60 extern const m_option_type_t m_option_type_afmt;
62 // Func-based types
63 extern const m_option_type_t m_option_type_func_full;
64 extern const m_option_type_t m_option_type_func_param;
65 extern const m_option_type_t m_option_type_func;
67 /// Callback used to reset func options.
68 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
70 /// Callback used by m_option_type_func_full options.
71 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, const char *);
73 /// Callback used by m_option_type_func_param options.
74 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
76 /// Callback used by m_option_type_func options.
77 typedef int (*m_opt_func_t)(const m_option_t *);
79 // Backwards compatibility
80 typedef m_opt_default_func_t cfg_default_func_t;
81 typedef m_opt_func_full_t cfg_func_arg_param_t;
82 typedef m_opt_func_param_t cfg_func_param_t;
83 typedef m_opt_func_t cfg_func_t;
85 #define END_AT_NONE 0
86 #define END_AT_TIME 1
87 #define END_AT_SIZE 2
88 typedef struct {
89 double pos;
90 int type;
91 } m_time_size_t;
93 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
94 typedef struct {
95 /// Pointer to an array of pointer to some object type description struct.
96 void** list;
97 /// Offset of the object type name (char*) in the description struct.
98 void* name_off;
99 /// Offset of the object type info string (char*) in the description struct.
100 void* info_off;
101 /// \brief Offset of the object type parameter description (\ref m_struct_st)
102 /// in the description struct.
103 void* desc_off;
104 } m_obj_list_t;
106 /// The data type used by \ref m_option_type_obj_settings_list.
107 typedef struct m_obj_settings {
108 /// Type of the object.
109 char* name;
110 /// NULL terminated array of parameter/value pairs.
111 char** attribs;
112 } m_obj_settings_t;
114 /// A parser to set up a list of objects.
115 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
116 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
117 * the available object types.
119 extern const m_option_type_t m_option_type_obj_settings_list;
121 /// Extra definition needed for \ref m_option_type_obj_presets options.
122 typedef struct {
123 /// Description of the struct holding the presets.
124 struct m_struct_st* in_desc;
125 /// Description of the struct that should be set by the presets.
126 struct m_struct_st* out_desc;
127 /// Pointer to an array of structs defining the various presets.
128 void* presets;
129 /// Offset of the preset's name inside the in_struct.
130 void* name_off;
131 } m_obj_presets_t;
133 /// Set several fields in a struct at once.
134 /** For this two struct descriptions are used. One for the struct holding the
135 * preset and one for the struct beeing set. Every field present in both
136 * structs will be copied from the preset struct to the destination one.
137 * The option priv field (\ref m_option::priv) must point to a correctly
138 * filled \ref m_obj_presets_t.
140 extern const m_option_type_t m_option_type_obj_presets;
142 /// Parse an URL into a struct.
143 /** The option priv field (\ref m_option::priv) must point to a
144 * \ref m_struct_st describing which fields of the URL must be used.
146 extern const m_option_type_t m_option_type_custom_url;
148 /// Extra definition needed for \ref m_option_type_obj_params options.
149 typedef struct {
150 /// Field descriptions.
151 const struct m_struct_st* desc;
152 /// Field separator to use.
153 char separator;
154 } m_obj_params_t;
156 /// Parse a set of parameters.
157 /** Parameters are separated by the given separator and each one
158 * successively sets a field from the struct. The option priv field
159 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
161 extern const m_option_type_t m_option_type_obj_params;
163 typedef struct {
164 int start;
165 int end;
166 } m_span_t;
167 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
168 extern const m_obj_params_t m_span_params_def;
171 // FIXME: backward compatibility
172 #define CONF_TYPE_FLAG (&m_option_type_flag)
173 #define CONF_TYPE_INT (&m_option_type_int)
174 #define CONF_TYPE_INT64 (&m_option_type_int64)
175 #define CONF_TYPE_FLOAT (&m_option_type_float)
176 #define CONF_TYPE_DOUBLE (&m_option_type_double)
177 #define CONF_TYPE_STRING (&m_option_type_string)
178 #define CONF_TYPE_FUNC (&m_option_type_func)
179 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
180 #define CONF_TYPE_PRINT (&m_option_type_print)
181 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
182 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
183 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
184 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
185 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
186 #define CONF_TYPE_POSITION (&m_option_type_position)
187 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
188 #define CONF_TYPE_AFMT (&m_option_type_afmt)
189 #define CONF_TYPE_SPAN (&m_option_type_span)
190 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
191 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
192 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
193 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
194 #define CONF_TYPE_TIME (&m_option_type_time)
195 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
197 /////////////////////////////////////////////////////////////////////////////////////////////
199 /// Option type description
200 struct m_option_type {
201 const char* name;
202 /// Syntax description, etc
203 const char* comments;
204 /// Size needed for the data.
205 unsigned int size;
206 /// See \ref OptionTypeFlags.
207 unsigned int flags;
209 /// Parse the data from a string.
210 /** It is the only required function, all others can be NULL.
212 * \param opt The option that is parsed.
213 * \param name The full option name.
214 * \param param The parameter to parse.
215 * \param dst Pointer to the memory where the data should be written.
216 * If NULL the parameter validity should still be checked.
217 * \param src Source of the option, see \ref OptionParserModes.
218 * \return On error a negative value is returned, on success the number of arguments
219 * consumed. For details see \ref OptionParserReturn.
221 int (*parse)(const m_option_t* opt,const char *name, const char *param, void* dst, int src);
223 /// Print back a value in string form.
224 /** \param opt The option to print.
225 * \param val Pointer to the memory holding the data to be printed.
226 * \return An allocated string containing the text value or (void*)-1
227 * on error.
229 char* (*print)(const m_option_t* opt, const void* val);
231 /** \name
232 * These functions are called to save/set/restore the status of the
233 * variables. The difference between the 3 only matters for types like
234 * \ref m_option_type_func where 'setting' needs to do more than just
235 * copying some data.
237 //@{
239 /// Update a save slot (dst) from the current value in the program (src).
240 /** \param opt The option to copy.
241 * \param dst Pointer to the destination memory.
242 * \param src Pointer to the source memory.
244 void (*save)(const m_option_t* opt,void* dst, const void* src);
246 /// Set the value in the program (dst) from a save slot.
247 /** \param opt The option to copy.
248 * \param dst Pointer to the destination memory.
249 * \param src Pointer to the source memory.
251 void (*set)(const m_option_t* opt,void* dst, const void* src);
253 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
254 /** \param opt The option to copy.
255 * \param dst Pointer to the destination memory.
256 * \param src Pointer to the source memory.
258 void (*copy)(const m_option_t* opt,void* dst, const void* src);
259 //@}
261 /// Free the data allocated for a save slot.
262 /** This is only needed for dynamic types like strings.
263 * \param dst Pointer to the data, usually a pointer that should be freed and
264 * set to NULL.
266 void (*free)(void* dst);
269 ///@}
271 /// Option description
272 /** \ingroup Options
274 struct m_option {
275 /// Option name.
276 const char *name;
278 /// Reserved for higher level APIs, it shouldn't be used by parsers.
279 /** The suboption parser and func types do use it. They should instead
280 * use the priv field but this was inherited from older versions of the
281 * config code.
283 void *p;
285 /// Option type.
286 const m_option_type_t* type;
288 /// See \ref OptionFlags.
289 unsigned int flags;
291 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
292 /// also be set.
293 double min;
295 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
296 /// also be set.
297 double max;
299 /// Type dependent data (for all kinds of extended settings).
300 /** This used to be a function pointer to hold a 'reverse to defaults' func.
301 * Now it can be used to pass any type of extra args needed by the parser.
302 * Passing a 'default func' is still valid for all func based option types.
304 void* priv;
308 /// \defgroup OptionFlags Option flags
309 ///@{
311 /// The option has a minimum set in \ref m_option::min.
312 #define M_OPT_MIN (1<<0)
314 /// The option has a maximum set in \ref m_option::max.
315 #define M_OPT_MAX (1<<1)
317 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
318 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
320 /// The option is forbidden in config files.
321 #define M_OPT_NOCFG (1<<2)
323 /// The option is forbidden on the command line.
324 #define M_OPT_NOCMD (1<<3)
326 /// The option is global in the \ref Config.
327 /** It won't be saved on push and the command line parser will set it when
328 * it's parsed (i.e. it won't be set later)
329 * e.g options : -v, -quiet
331 #define M_OPT_GLOBAL (1<<4)
333 /// The \ref Config won't save this option on push.
334 /** It won't be saved on push but the command line parser will add it with
335 * its entry (i.e. it may be set later)
336 * e.g options : -include
338 #define M_OPT_NOSAVE (1<<5)
340 /// \brief The \ref Config will emulate the old behavior by pushing the
341 /// option only if it was set by the user.
342 #define M_OPT_OLD (1<<6)
344 /// The option should be set during command line pre-parsing
345 #define M_OPT_PRE_PARSE (1<<7)
347 /// \defgroup OldOptionFlags Backward compatibility
349 /// These are kept for compatibility with older code.
350 /// @{
351 #define CONF_MIN M_OPT_MIN
352 #define CONF_MAX M_OPT_MAX
353 #define CONF_RANGE M_OPT_RANGE
354 #define CONF_NOCFG M_OPT_NOCFG
355 #define CONF_NOCMD M_OPT_NOCMD
356 #define CONF_GLOBAL M_OPT_GLOBAL
357 #define CONF_NOSAVE M_OPT_NOSAVE
358 #define CONF_OLD M_OPT_OLD
359 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
360 ///@}
362 ///@}
364 /// \defgroup OptionTypeFlags Option type flags
365 /// \ingroup OptionTypes
367 /// These flags are used to describe special parser capabilities or behavior.
369 ///@{
371 /// Suboption parser flag.
372 /** When this flag is set, m_option::p should point to another m_option
373 * array. Only the parse function will be called. If dst is set, it should
374 * create/update an array of char* containg opt/val pairs. The options in
375 * the child array will then be set automatically by the \ref Config.
376 * Also note that suboptions may be directly accessed by using
377 * -option:subopt blah.
379 #define M_OPT_TYPE_HAS_CHILD (1<<0)
381 /// Wildcard matching flag.
382 /** If set the option type has a use for option names ending with a *
383 * (used for -aa*), this only affects the option name matching.
385 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
387 /// Dynamic data type.
388 /** This flag indicates that the data is dynamically allocated (m_option::p
389 * points to a pointer). It enables a little hack in the \ref Config wich
390 * replaces the initial value of such variables with a dynamic copy in case
391 * the initial value is statically allocated (pretty common with strings).
393 #define M_OPT_TYPE_DYNAMIC (1<<2)
395 /// Indirect option type.
396 /** If this is set the parse function doesn't directly return
397 * the wanted thing. Options use this if for some reasons they have to wait
398 * until the set call to be able to correctly set the target var.
399 * So for those types new values must first be parsed, then set to the target
400 * var. If this flag isn't set then new values can be parsed directly to the
401 * target var. It's used by the callback-based options as the callback call
402 * may append later on.
404 #define M_OPT_TYPE_INDIRECT (1<<3)
406 ///@}
408 ///////////////////////////// Parser flags ////////////////////////////////////////
410 /// \defgroup OptionParserModes Option parser modes
411 /// \ingroup Options
413 /// Some parsers behave differently depending on the mode passed in the src
414 /// parameter of m_option_type::parse. For example the flag type doesn't take
415 /// an argument when parsing from the command line.
416 ///@{
418 /// Set when parsing from a config file.
419 #define M_CONFIG_FILE 0
420 /// Set when parsing command line arguments.
421 #define M_COMMAND_LINE 1
422 /// Set when pre-parsing the command line
423 #define M_COMMAND_LINE_PRE_PARSE 2
425 ///@}
427 /// \defgroup OptionParserReturn Option parser return code
428 /// \ingroup Options
430 /// On success parsers return the number of arguments consumed: 0 or 1.
432 /// To indicate that MPlayer should exit without playing anything,
433 /// parsers return M_OPT_EXIT minus the number of parameters they
434 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
436 /// On error one of the following (negative) error codes is returned:
437 ///@{
439 /// For use by higher level APIs when the option name is invalid.
440 #define M_OPT_UNKNOWN -1
442 /// Returned when a parameter is needed but wasn't provided.
443 #define M_OPT_MISSING_PARAM -2
445 /// Returned when the given parameter couldn't be parsed.
446 #define M_OPT_INVALID -3
448 /// \brief Returned if the value is "out of range". The exact meaning may
449 /// vary from type to type.
450 #define M_OPT_OUT_OF_RANGE -4
452 /// Returned if the parser failed for any other reason than a bad parameter.
453 #define M_OPT_PARSER_ERR -5
455 /// Returned when MPlayer should exit. Used by various help stuff.
456 /** M_OPT_EXIT must be the lowest number on this list.
458 #define M_OPT_EXIT -6
460 /// \defgroup OldOptionParserReturn Backward compatibility
462 /// These are kept for compatibility with older code.
464 ///@{
465 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
466 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
467 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
468 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
469 ///@}
471 ///@}
473 /// Find the option matching the given name in the list.
474 /** \ingroup Options
475 * This function takes the possible wildcards into account (see
476 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
478 * \param list Pointer to an array of \ref m_option.
479 * \param name Name of the option.
480 * \return The matching option or NULL.
482 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
484 /// Helper to parse options, see \ref m_option_type::parse.
485 inline static int
486 m_option_parse(const m_option_t* opt,const char *name, const char *param, void* dst, int src) {
487 return opt->type->parse(opt,name,param,dst,src);
490 /// Helper to print options, see \ref m_option_type::print.
491 inline static char*
492 m_option_print(const m_option_t* opt, const void* val_ptr) {
493 if(opt->type->print)
494 return opt->type->print(opt,val_ptr);
495 else
496 return (char*)-1;
499 /// Helper around \ref m_option_type::save.
500 inline static void
501 m_option_save(const m_option_t* opt,void* dst, const void* src) {
502 if(opt->type->save)
503 opt->type->save(opt,dst,src);
506 /// Helper around \ref m_option_type::set.
507 inline static void
508 m_option_set(const m_option_t* opt,void* dst, const void* src) {
509 if(opt->type->set)
510 opt->type->set(opt,dst,src);
513 /// Helper around \ref m_option_type::copy.
514 inline static void
515 m_option_copy(const m_option_t* opt,void* dst, const void* src) {
516 if(opt->type->copy)
517 opt->type->copy(opt,dst,src);
518 else if(opt->type->size > 0)
519 memcpy(dst,src,opt->type->size);
522 /// Helper around \ref m_option_type::free.
523 inline static void
524 m_option_free(const m_option_t* opt,void* dst) {
525 if(opt->type->free)
526 opt->type->free(dst);
529 /*@}*/
531 #endif /* MPLAYER_M_OPTION_H */