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
;
58 // Callback used by m_option_type_print_func options.
59 typedef int (*m_opt_func_full_t
)(const m_option_t
*, const char *, const char *);
69 // Extra definition needed for \ref m_option_type_obj_settings_list options.
71 // Pointer to an array of pointer to some object type description struct.
73 // Offset of the object type name (char*) in the description struct.
75 // Offset of the object type info string (char*) in the description struct.
77 // Offset of the object type parameter description (\ref m_struct_st)
78 // in the description struct.
82 // The data type used by \ref m_option_type_obj_settings_list.
83 typedef struct m_obj_settings
{
84 // Type of the object.
86 // NULL terminated array of parameter/value pairs.
90 // A parser to set up a list of objects.
91 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
92 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
93 * the available object types.
95 extern const m_option_type_t m_option_type_obj_settings_list
;
97 // Extra definition needed for \ref m_option_type_obj_presets options.
99 // Description of the struct holding the presets.
100 const struct m_struct_st
*in_desc
;
101 // Description of the struct that should be set by the presets.
102 const struct m_struct_st
*out_desc
;
103 // Pointer to an array of structs defining the various presets.
105 // Offset of the preset's name inside the in_struct.
109 // Set several fields in a struct at once.
110 /** For this two struct descriptions are used. One for the struct holding the
111 * preset and one for the struct beeing set. Every field present in both
112 * structs will be copied from the preset struct to the destination one.
113 * The option priv field (\ref m_option::priv) must point to a correctly
114 * filled \ref m_obj_presets_t.
116 extern const m_option_type_t m_option_type_obj_presets
;
118 // Parse an URL into a struct.
119 /** The option priv field (\ref m_option::priv) must point to a
120 * \ref m_struct_st describing which fields of the URL must be used.
122 extern const m_option_type_t m_option_type_custom_url
;
124 // Extra definition needed for \ref m_option_type_obj_params options.
126 // Field descriptions.
127 const struct m_struct_st
*desc
;
128 // Field separator to use.
132 // Parse a set of parameters.
133 /** Parameters are separated by the given separator and each one
134 * successively sets a field from the struct. The option priv field
135 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
137 extern const m_option_type_t m_option_type_obj_params
;
143 // Ready made settings to parse a \ref m_span_t with a start-end syntax.
144 extern const m_obj_params_t m_span_params_def
;
146 struct m_opt_choice_alternatives
{
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_INT64 (&m_option_type_int64)
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_PRINT (&m_option_type_print)
160 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
161 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
162 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
163 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
164 #define CONF_TYPE_POSITION (&m_option_type_position)
165 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
166 #define CONF_TYPE_AFMT (&m_option_type_afmt)
167 #define CONF_TYPE_SPAN (&m_option_type_span)
168 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
169 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
170 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
171 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
172 #define CONF_TYPE_TIME (&m_option_type_time)
173 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
175 ////////////////////////////////////////////////////////////////////////////
177 // Option type description
178 struct m_option_type
{
180 // Size needed for the data.
182 // See \ref OptionTypeFlags.
185 // Parse the data from a string.
186 /** It is the only required function, all others can be NULL.
188 * \param opt The option that is parsed.
189 * \param name The full option name.
190 * \param param The parameter to parse.
191 * \param ambiguous_param: "param" old cmdline style, "param" may or
192 * may not be an argument meant for this option
193 * \param dst Pointer to the memory where the data should be written.
194 * If NULL the parameter validity should still be checked.
195 * talloc_ctx: talloc context if value type requires allocations
196 * \return On error a negative value is returned, on success the number
197 * of arguments consumed. For details see \ref OptionParserReturn.
199 int (*parse
)(const m_option_t
*opt
, struct bstr name
, struct bstr param
,
200 bool ambiguous_param
, void *dst
, void *talloc_ctx
);
202 // Print back a value in string form.
203 /** \param opt The option to print.
204 * \param val Pointer to the memory holding the data to be printed.
205 * \return An allocated string containing the text value or (void*)-1
208 char *(*print
)(const m_option_t
*opt
, const void *val
);
210 // Copy data between two locations. Deep copy if the data has pointers.
211 /** \param opt The option to copy.
212 * \param dst Pointer to the destination memory.
213 * \param src Pointer to the source memory.
214 * talloc_ctx: talloc context to use in deep copy
216 void (*copy
)(const m_option_t
*opt
, void *dst
, const void *src
,
219 // Free the data allocated for a save slot.
220 /** This is only needed for dynamic types like strings.
221 * \param dst Pointer to the data, usually a pointer that should be freed and
224 void (*free
)(void *dst
);
227 // Option description
232 // Reserved for higher level APIs, it shouldn't be used by parsers.
233 /** The suboption parser and func types do use it. They should instead
234 * use the priv field but this was inherited from older versions of the
240 const m_option_type_t
*type
;
242 // See \ref OptionFlags.
245 // \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
249 // \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
253 // Type dependent data (for all kinds of extended settings).
254 /** This used to be a function pointer to hold a 'reverse to defaults' func.
255 * Now it can be used to pass any type of extra args needed by the parser.
263 // Initialize variable to given default before parsing options
268 // The option has a minimum set in \ref m_option::min.
269 #define M_OPT_MIN (1 << 0)
271 // The option has a maximum set in \ref m_option::max.
272 #define M_OPT_MAX (1 << 1)
274 // The option has a minimum and maximum in m_option::min and m_option::max.
275 #define M_OPT_RANGE (M_OPT_MIN | M_OPT_MAX)
277 // The option is forbidden in config files.
278 #define M_OPT_NOCFG (1 << 2)
280 // The option is forbidden on the command line.
281 #define M_OPT_NOCMD (1 << 3)
283 // The option is global in the \ref Config.
284 /** It won't be saved on push and the command line parser will set it when
285 * it's parsed (i.e. it won't be set later)
286 * e.g options : -v, -quiet
288 #define M_OPT_GLOBAL (1 << 4)
290 // The \ref Config won't save this option on push.
291 /** It won't be saved on push but the command line parser will add it with
292 * its entry (i.e. it may be set later)
293 * e.g options : -include
295 #define M_OPT_NOSAVE (1 << 5)
297 // The option should be set during command line pre-parsing
298 #define M_OPT_PRE_PARSE (1 << 6)
300 // These are kept for compatibility with older code.
301 #define CONF_MIN M_OPT_MIN
302 #define CONF_MAX M_OPT_MAX
303 #define CONF_RANGE M_OPT_RANGE
304 #define CONF_NOCFG M_OPT_NOCFG
305 #define CONF_NOCMD M_OPT_NOCMD
306 #define CONF_GLOBAL M_OPT_GLOBAL
307 #define CONF_NOSAVE M_OPT_NOSAVE
308 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
310 // These flags are used to describe special parser capabilities or behavior.
312 // Suboption parser flag.
313 /** When this flag is set, m_option::p should point to another m_option
314 * array. Only the parse function will be called. If dst is set, it should
315 * create/update an array of char* containg opt/val pairs. The options in
316 * the child array will then be set automatically by the \ref Config.
317 * Also note that suboptions may be directly accessed by using
318 * -option:subopt blah.
320 #define M_OPT_TYPE_HAS_CHILD (1 << 0)
322 // Wildcard matching flag.
323 /** If set the option type has a use for option names ending with a *
324 * (used for -aa*), this only affects the option name matching.
326 #define M_OPT_TYPE_ALLOW_WILDCARD (1 << 1)
328 // Dynamic data type.
329 /** This flag indicates that the data is dynamically allocated (m_option::p
330 * points to a pointer). It enables a little hack in the \ref Config wich
331 * replaces the initial value of such variables with a dynamic copy in case
332 * the initial value is statically allocated (pretty common with strings).
334 #define M_OPT_TYPE_DYNAMIC (1 << 2)
336 ///////////////////////////// Parser flags /////////////////////////////////
338 // On success parsers return the number of arguments consumed: 0 or 1.
340 // To indicate that MPlayer should exit without playing anything,
341 // parsers return M_OPT_EXIT minus the number of parameters they
342 // consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
344 // On error one of the following (negative) error codes is returned:
346 // For use by higher level APIs when the option name is invalid.
347 #define M_OPT_UNKNOWN -1
349 // Returned when a parameter is needed but wasn't provided.
350 #define M_OPT_MISSING_PARAM -2
352 // Returned when the given parameter couldn't be parsed.
353 #define M_OPT_INVALID -3
355 // Returned if the value is "out of range". The exact meaning may
356 // vary from type to type.
357 #define M_OPT_OUT_OF_RANGE -4
359 // Returned if the parser failed for any other reason than a bad parameter.
360 #define M_OPT_PARSER_ERR -5
362 // Returned when MPlayer should exit. Used by various help stuff.
363 /** M_OPT_EXIT must be the lowest number on this list.
365 #define M_OPT_EXIT -6
367 char *m_option_strerror(int code
);
369 // Find the option matching the given name in the list.
371 * This function takes the possible wildcards into account (see
372 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
374 * \param list Pointer to an array of \ref m_option.
375 * \param name Name of the option.
376 * \return The matching option or NULL.
378 const m_option_t
*m_option_list_find(const m_option_t
*list
, const char *name
);
380 static inline void *m_option_get_ptr(const struct m_option
*opt
,
383 return opt
->new ? (char *) optstruct
+ opt
->offset
: opt
->p
;
386 // Helper to parse options, see \ref m_option_type::parse.
387 static inline int m_option_parse(const m_option_t
*opt
, struct bstr name
,
388 struct bstr param
, bool ambiguous_param
,
391 return opt
->type
->parse(opt
, name
, param
, ambiguous_param
, dst
, NULL
);
394 // Helper to print options, see \ref m_option_type::print.
395 static inline char *m_option_print(const m_option_t
*opt
, const void *val_ptr
)
397 if (opt
->type
->print
)
398 return opt
->type
->print(opt
, val_ptr
);
403 // Helper around \ref m_option_type::copy.
404 static inline void m_option_copy(const m_option_t
*opt
, void *dst
,
408 opt
->type
->copy(opt
, dst
, src
, NULL
);
411 // Helper around \ref m_option_type::free.
412 static inline void m_option_free(const m_option_t
*opt
, void *dst
)
415 opt
->type
->free(dst
);
420 #define OPTION_LIST_SEPARATOR ','
423 #define OPTION_PATH_SEPARATOR ';'
425 #define OPTION_PATH_SEPARATOR ':'
428 // The code will interpret arguments different from 1 as disabled, thus
429 // CONFIG_FOO etc mean disabled if no such macro is defined.
430 #define OPT_START_CONDITIONAL(enable, featurename) OPT_START_CONDITIONAL_AFTERMACROEVAL(enable, featurename)
431 #define OPT_START_CONDITIONAL_AFTERMACROEVAL(enable, featurename) {"conditional functionality: " #enable, .p = featurename}
433 #define OPTDEF_STR(s) .defval = (void *)&(char * const){s}
434 #define OPTDEF_INT(i) .defval = (void *)&(const int){i}
436 #define OPT_GENERAL(optname, varname, flagv, ...) {.name = optname, .flags = flagv, .new = 1, .offset = offsetof(OPT_BASE_STRUCT, varname), __VA_ARGS__}
438 /* The OPT_FLAG_CONSTANTS->OPT_FLAG_CONSTANTS_ kind of redirection exists to
439 * make the code fully standard-conforming: the C standard requires that
440 * __VA_ARGS__ has at least one argument (though GCC for example would accept
441 * 0). Thus the first OPT_FLAG_CONSTANTS is a wrapper which just adds one
442 * argument to ensure __VA_ARGS__ is not empty when calling the next macro.
444 #define OPT_FLAG_ON(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_flag, .max = 1)
445 #define OPT_FLAG_OFF(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_flag, .min = 1)
446 #define OPT_MAKE_FLAGS(optname, varname, flags) OPT_FLAG_ON(optname, varname, flags), OPT_FLAG_OFF("no" optname, varname, flags)
447 #define OPT_FLAG_CONSTANTS(...) OPT_FLAG_CONSTANTS_(__VA_ARGS__, .type = &m_option_type_flag)
448 #define OPT_FLAG_CONSTANTS_(optname, varname, flags, offvalue, value, ...) OPT_GENERAL(optname, varname, flags, .min = offvalue, .max = value, __VA_ARGS__)
449 #define OPT_STRINGLIST(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_string_list)
450 #define OPT_PATHLIST(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_string_list, .priv = (void *)&(const char){OPTION_PATH_SEPARATOR})
451 #define OPT_INT(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_int)
452 #define OPT_INTRANGE(...) OPT_RANGE_(__VA_ARGS__, .type = &m_option_type_int)
453 #define OPT_RANGE_(optname, varname, flags, minval, maxval, ...) OPT_GENERAL(optname, varname, (flags) | CONF_RANGE, .min = minval, .max = maxval, __VA_ARGS__)
454 #define OPT_INTPAIR(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_intpair)
455 #define OPT_FLOAT(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_float)
456 #define OPT_FLOATRANGE(...) OPT_RANGE_(__VA_ARGS__, .type = &m_option_type_float)
457 #define OPT_STRING(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_string)
458 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) OPT_GENERAL(optname, varname, flags, .type = &m_option_type_obj_settings_list, .priv = objlist)
459 #define OPT_AUDIOFORMAT(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_afmt)
460 #define OPT_HELPER_REMOVEPAREN(...) __VA_ARGS__
461 #define OPT_CHOICE(...) OPT_CHOICE_(__VA_ARGS__, .type = &m_option_type_choice)
462 #define OPT_CHOICE_(optname, varname, flags, choices, ...) OPT_GENERAL(optname, varname, flags, .priv = (void *)&(const struct m_opt_choice_alternatives[]){OPT_HELPER_REMOVEPAREN choices, {NULL}}, __VA_ARGS__)
463 #define OPT_TIME(...) OPT_GENERAL(__VA_ARGS__, .type = &m_option_type_time)
464 #define OPT_ERRORMESSAGE(optname, message) {.name = optname, .p = message, .type = &m_option_type_print}
466 #define OPT_BASE_STRUCT struct MPOpts
468 #endif /* MPLAYER_M_OPTION_H */