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
29 // m_option allows to parse, print and copy data of various types.
31 typedef struct m_option_type m_option_type_t
;
32 typedef struct m_option m_option_t
;
35 ///////////////////////////// Options types declarations ////////////////////
38 extern const m_option_type_t m_option_type_flag
;
39 extern const m_option_type_t m_option_type_int
;
40 extern const m_option_type_t m_option_type_int64
;
41 extern const m_option_type_t m_option_type_intpair
;
42 extern const m_option_type_t m_option_type_float
;
43 extern const m_option_type_t m_option_type_double
;
44 extern const m_option_type_t m_option_type_string
;
45 extern const m_option_type_t m_option_type_string_list
;
46 extern const m_option_type_t m_option_type_position
;
47 extern const m_option_type_t m_option_type_time
;
48 extern const m_option_type_t m_option_type_time_size
;
49 extern const m_option_type_t m_option_type_choice
;
51 extern const m_option_type_t m_option_type_print
;
52 extern const m_option_type_t m_option_type_print_indirect
;
53 extern const m_option_type_t m_option_type_print_func
;
54 extern const m_option_type_t m_option_type_subconfig
;
55 extern const m_option_type_t m_option_type_imgfmt
;
56 extern const m_option_type_t m_option_type_afmt
;
59 extern const m_option_type_t m_option_type_func_param
;
60 extern const m_option_type_t m_option_type_func
;
62 // Callback used to reset func options.
63 typedef void (*m_opt_default_func_t
)(const m_option_t
*, const char *);
65 // Callback used by m_option_type_func_full options.
66 typedef int (*m_opt_func_full_t
)(const m_option_t
*, const char *, const char *);
68 // Callback used by m_option_type_func_param options.
69 typedef int (*m_opt_func_param_t
)(const m_option_t
*, const char *);
71 // Callback used by m_option_type_func options.
72 typedef int (*m_opt_func_t
)(const m_option_t
*);
74 // Backwards compatibility
75 typedef m_opt_default_func_t cfg_default_func_t
;
76 typedef m_opt_func_full_t cfg_func_arg_param_t
;
77 typedef m_opt_func_param_t cfg_func_param_t
;
78 typedef m_opt_func_t cfg_func_t
;
88 // Extra definition needed for \ref m_option_type_obj_settings_list options.
90 // Pointer to an array of pointer to some object type description struct.
92 // Offset of the object type name (char*) in the description struct.
94 // Offset of the object type info string (char*) in the description struct.
96 // Offset of the object type parameter description (\ref m_struct_st)
97 // in the description struct.
101 // The data type used by \ref m_option_type_obj_settings_list.
102 typedef struct m_obj_settings
{
103 // Type of the object.
105 // NULL terminated array of parameter/value pairs.
109 // A parser to set up a list of objects.
110 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
111 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
112 * the available object types.
114 extern const m_option_type_t m_option_type_obj_settings_list
;
116 // Extra definition needed for \ref m_option_type_obj_presets options.
118 // Description of the struct holding the presets.
119 const struct m_struct_st
*in_desc
;
120 // Description of the struct that should be set by the presets.
121 const struct m_struct_st
*out_desc
;
122 // Pointer to an array of structs defining the various presets.
124 // Offset of the preset's name inside the in_struct.
128 // Set several fields in a struct at once.
129 /** For this two struct descriptions are used. One for the struct holding the
130 * preset and one for the struct beeing set. Every field present in both
131 * structs will be copied from the preset struct to the destination one.
132 * The option priv field (\ref m_option::priv) must point to a correctly
133 * filled \ref m_obj_presets_t.
135 extern const m_option_type_t m_option_type_obj_presets
;
137 // Parse an URL into a struct.
138 /** The option priv field (\ref m_option::priv) must point to a
139 * \ref m_struct_st describing which fields of the URL must be used.
141 extern const m_option_type_t m_option_type_custom_url
;
143 // Extra definition needed for \ref m_option_type_obj_params options.
145 // Field descriptions.
146 const struct m_struct_st
*desc
;
147 // Field separator to use.
151 // Parse a set of parameters.
152 /** Parameters are separated by the given separator and each one
153 * successively sets a field from the struct. The option priv field
154 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
156 extern const m_option_type_t m_option_type_obj_params
;
162 // Ready made settings to parse a \ref m_span_t with a start-end syntax.
163 extern const m_obj_params_t m_span_params_def
;
165 struct m_opt_choice_alternatives
{
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_SUBCONFIG (&m_option_type_subconfig)
184 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
185 #define CONF_TYPE_POSITION (&m_option_type_position)
186 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
187 #define CONF_TYPE_AFMT (&m_option_type_afmt)
188 #define CONF_TYPE_SPAN (&m_option_type_span)
189 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
190 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
191 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
192 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
193 #define CONF_TYPE_TIME (&m_option_type_time)
194 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
196 ////////////////////////////////////////////////////////////////////////////
198 // Option type description
199 struct m_option_type
{
201 // Syntax description, etc
202 const char *comments
;
203 // Size needed for the data.
205 // See \ref OptionTypeFlags.
208 // Parse the data from a string.
209 /** It is the only required function, all others can be NULL.
211 * \param opt The option that is parsed.
212 * \param name The full option name.
213 * \param param The parameter to parse.
214 * \param ambiguous_param: "param" old cmdline style, "param" may or
215 * may not be an argument meant for this option
216 * \param dst Pointer to the memory where the data should be written.
217 * If NULL the parameter validity should still be checked.
218 * \return On error a negative value is returned, on success the number
219 * of arguments consumed. For details see \ref OptionParserReturn.
221 int (*parse
)(const m_option_t
*opt
, struct bstr name
, struct bstr param
,
222 bool ambiguous_param
, void *dst
);
224 // Print back a value in string form.
225 /** \param opt The option to print.
226 * \param val Pointer to the memory holding the data to be printed.
227 * \return An allocated string containing the text value or (void*)-1
230 char *(*print
)(const m_option_t
*opt
, const void *val
);
233 * These functions are called to save/set/restore the status of the
234 * variables. The difference between the 3 only matters for types like
235 * \ref m_option_type_func where 'setting' needs to do more than just
240 // Update a save slot (dst) from the current value in the program (src).
241 /** \param opt The option to copy.
242 * \param dst Pointer to the destination memory.
243 * \param src Pointer to the source memory.
245 void (*save
)(const m_option_t
*opt
, void *dst
, const void *src
);
247 // Set the value in the program (dst) from a save slot.
248 /** \param opt The option to copy.
249 * \param dst Pointer to the destination memory.
250 * \param src Pointer to the source memory.
252 void (*set
)(const m_option_t
*opt
, void *dst
, const void *src
);
254 // Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
255 /** \param opt The option to copy.
256 * \param dst Pointer to the destination memory.
257 * \param src Pointer to the source memory.
259 void (*copy
)(const m_option_t
*opt
, void *dst
, const void *src
);
262 // Free the data allocated for a save slot.
263 /** This is only needed for dynamic types like strings.
264 * \param dst Pointer to the data, usually a pointer that should be freed and
267 void (*free
)(void *dst
);
270 // Option description
275 // Reserved for higher level APIs, it shouldn't be used by parsers.
276 /** The suboption parser and func types do use it. They should instead
277 * use the priv field but this was inherited from older versions of the
283 const m_option_type_t
*type
;
285 // See \ref OptionFlags.
288 // \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
292 // \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
296 // Type dependent data (for all kinds of extended settings).
297 /** This used to be a function pointer to hold a 'reverse to defaults' func.
298 * Now it can be used to pass any type of extra args needed by the parser.
299 * Passing a 'default func' is still valid for all func based option types.
309 // The option has a minimum set in \ref m_option::min.
310 #define M_OPT_MIN (1 << 0)
312 // The option has a maximum set in \ref m_option::max.
313 #define M_OPT_MAX (1 << 1)
315 // The option has a minimum and maximum in m_option::min and m_option::max.
316 #define M_OPT_RANGE (M_OPT_MIN | M_OPT_MAX)
318 // The option is forbidden in config files.
319 #define M_OPT_NOCFG (1 << 2)
321 // The option is forbidden on the command line.
322 #define M_OPT_NOCMD (1 << 3)
324 // The option is global in the \ref Config.
325 /** It won't be saved on push and the command line parser will set it when
326 * it's parsed (i.e. it won't be set later)
327 * e.g options : -v, -quiet
329 #define M_OPT_GLOBAL (1 << 4)
331 // The \ref Config won't save this option on push.
332 /** It won't be saved on push but the command line parser will add it with
333 * its entry (i.e. it may be set later)
334 * e.g options : -include
336 #define M_OPT_NOSAVE (1 << 5)
338 // The option should be set during command line pre-parsing
339 #define M_OPT_PRE_PARSE (1 << 6)
341 // These are kept for compatibility with older code.
342 #define CONF_MIN M_OPT_MIN
343 #define CONF_MAX M_OPT_MAX
344 #define CONF_RANGE M_OPT_RANGE
345 #define CONF_NOCFG M_OPT_NOCFG
346 #define CONF_NOCMD M_OPT_NOCMD
347 #define CONF_GLOBAL M_OPT_GLOBAL
348 #define CONF_NOSAVE M_OPT_NOSAVE
349 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
351 // These flags are used to describe special parser capabilities or behavior.
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 ///////////////////////////// Parser flags /////////////////////////////////
390 // On success parsers return the number of arguments consumed: 0 or 1.
392 // To indicate that MPlayer should exit without playing anything,
393 // parsers return M_OPT_EXIT minus the number of parameters they
394 // consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
396 // On error one of the following (negative) error codes is returned:
398 // For use by higher level APIs when the option name is invalid.
399 #define M_OPT_UNKNOWN -1
401 // Returned when a parameter is needed but wasn't provided.
402 #define M_OPT_MISSING_PARAM -2
404 // Returned when the given parameter couldn't be parsed.
405 #define M_OPT_INVALID -3
407 // Returned if the value is "out of range". The exact meaning may
408 // vary from type to type.
409 #define M_OPT_OUT_OF_RANGE -4
411 // Returned if the parser failed for any other reason than a bad parameter.
412 #define M_OPT_PARSER_ERR -5
414 // Returned when MPlayer should exit. Used by various help stuff.
415 /** M_OPT_EXIT must be the lowest number on this list.
417 #define M_OPT_EXIT -6
419 // These are kept for compatibility with older code.
421 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
422 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
423 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
424 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
426 char *m_option_strerror(int code
);
428 // Find the option matching the given name in the list.
430 * This function takes the possible wildcards into account (see
431 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
433 * \param list Pointer to an array of \ref m_option.
434 * \param name Name of the option.
435 * \return The matching option or NULL.
437 const m_option_t
*m_option_list_find(const m_option_t
*list
, const char *name
);
439 static inline void *m_option_get_ptr(const struct m_option
*opt
,
442 return opt
->new ? (char *) optstruct
+ opt
->offset
: opt
->p
;
445 // Helper to parse options, see \ref m_option_type::parse.
446 static inline int m_option_parse(const m_option_t
*opt
, struct bstr name
,
447 struct bstr param
, bool ambiguous_param
,
450 return opt
->type
->parse(opt
, name
, param
, ambiguous_param
, dst
);
453 // Helper to print options, see \ref m_option_type::print.
454 static inline char *m_option_print(const m_option_t
*opt
, const void *val_ptr
)
456 if (opt
->type
->print
)
457 return opt
->type
->print(opt
, val_ptr
);
462 // Helper around \ref m_option_type::copy.
463 static inline void m_option_copy(const m_option_t
*opt
, void *dst
,
467 opt
->type
->copy(opt
, dst
, src
);
468 else if (opt
->type
->size
> 0)
469 memcpy(dst
, src
, opt
->type
->size
);
472 // Helper around \ref m_option_type::free.
473 static inline void m_option_free(const m_option_t
*opt
, void *dst
)
476 opt
->type
->free(dst
);
481 #define OPTION_LIST_SEPARATOR ','
484 #define OPTION_PATH_SEPARATOR ';'
486 #define OPTION_PATH_SEPARATOR ':'
489 // The code will interpret arguments different from 1 as disabled, thus
490 // CONFIG_FOO etc mean disabled if no such macro is defined.
491 #define OPT_START_CONDITIONAL(enable, featurename) OPT_START_CONDITIONAL_AFTERMACROEVAL(enable, featurename)
492 #define OPT_START_CONDITIONAL_AFTERMACROEVAL(enable, featurename) {"conditional functionality: " #enable, .p = featurename}
494 #define OPT_FLAG_ON(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 0, 1, NULL, 1, offsetof(struct MPOpts, varname)}
495 #define OPT_FLAG_OFF(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 1, 0, NULL, 1, offsetof(struct MPOpts, varname)}
496 #define OPT_MAKE_FLAGS(optname, varname, flags) OPT_FLAG_ON(optname, varname, flags), OPT_FLAG_OFF("no" optname, varname, flags)
497 #define OPT_FLAG_CONSTANTS(optname, varname, flags, offvalue, value) {optname, NULL, &m_option_type_flag, flags, offvalue, value, NULL, 1, offsetof(struct MPOpts, varname)}
498 #define OPT_STRINGLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
499 #define OPT_PATHLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, (void *)&(const char){OPTION_PATH_SEPARATOR}, 1, offsetof(struct MPOpts, varname)}
500 #define OPT_INT(optname, varname, flags) {optname, NULL, &m_option_type_int, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
501 #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)}
502 #define OPT_INTPAIR(optname, varname, flags) {optname, NULL, &m_option_type_intpair, (flags), 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
503 #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)}
504 #define OPT_STRING(optname, varname, flags) {optname, NULL, &m_option_type_string, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
505 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) {optname, NULL, &m_option_type_obj_settings_list, flags, 0, 0, objlist, 1, offsetof(struct MPOpts, varname)}
506 #define OPT_AUDIOFORMAT(optname, varname, flags) {optname, NULL, &m_option_type_afmt, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
507 #define OPT_HELPER_REMOVEPAREN(...) __VA_ARGS__
508 #define OPT_CHOICE(optname, varname, flags, choices) {optname, NULL, &m_option_type_choice, flags, 0, 0, (void *)&(const struct m_opt_choice_alternatives[]){OPT_HELPER_REMOVEPAREN choices, {NULL}}, 1, offsetof(struct MPOpts, varname)}
509 #define OPT_TIME(optname, varname, flags) {optname, NULL, &m_option_type_time, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
511 #endif /* MPLAYER_M_OPTION_H */