core: fix audio-only + framestep weird behavior
[mplayer/glamo.git] / m_option.h
blob05d5751cebe111f43e88a8fd71de8db96e3ffc38
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>
23 #include <stddef.h>
25 /// \defgroup Options
26 /// m_option allows to parse, print and copy data of various types.
27 /// It is the base of the \ref OptionsStruct, \ref Config and
28 /// \ref Properties APIs.
29 ///@{
31 /// \file m_option.h
33 /// \ingroup OptionTypes
34 typedef struct m_option_type m_option_type_t;
35 typedef struct m_option m_option_t;
36 struct m_struct_st;
38 /// \defgroup OptionTypes Options types
39 /// \ingroup Options
40 ///@{
42 ///////////////////////////// Options types declarations ////////////////////////////
44 // Simple types
45 extern const m_option_type_t m_option_type_flag;
46 extern const m_option_type_t m_option_type_int;
47 extern const m_option_type_t m_option_type_int64;
48 extern const m_option_type_t m_option_type_intpair;
49 extern const m_option_type_t m_option_type_float;
50 extern const m_option_type_t m_option_type_double;
51 extern const m_option_type_t m_option_type_string;
52 extern const m_option_type_t m_option_type_string_list;
53 extern const m_option_type_t m_option_type_position;
54 extern const m_option_type_t m_option_type_time;
55 extern const m_option_type_t m_option_type_time_size;
56 extern const m_option_type_t m_option_type_choice;
58 extern const m_option_type_t m_option_type_print;
59 extern const m_option_type_t m_option_type_print_indirect;
60 extern const m_option_type_t m_option_type_print_func;
61 extern const m_option_type_t m_option_type_subconfig;
62 extern const m_option_type_t m_option_type_imgfmt;
63 extern const m_option_type_t m_option_type_afmt;
65 // Func-based types
66 extern const m_option_type_t m_option_type_func_full;
67 extern const m_option_type_t m_option_type_func_param;
68 extern const m_option_type_t m_option_type_func;
70 /// Callback used to reset func options.
71 typedef void (*m_opt_default_func_t)(const m_option_t *, const char*);
73 /// Callback used by m_option_type_func_full options.
74 typedef int (*m_opt_func_full_t)(const m_option_t *, const char *, const char *);
76 /// Callback used by m_option_type_func_param options.
77 typedef int (*m_opt_func_param_t)(const m_option_t *, const char *);
79 /// Callback used by m_option_type_func options.
80 typedef int (*m_opt_func_t)(const m_option_t *);
82 // Backwards compatibility
83 typedef m_opt_default_func_t cfg_default_func_t;
84 typedef m_opt_func_full_t cfg_func_arg_param_t;
85 typedef m_opt_func_param_t cfg_func_param_t;
86 typedef m_opt_func_t cfg_func_t;
88 #define END_AT_NONE 0
89 #define END_AT_TIME 1
90 #define END_AT_SIZE 2
91 typedef struct {
92 double pos;
93 int type;
94 } m_time_size_t;
96 /// Extra definition needed for \ref m_option_type_obj_settings_list options.
97 typedef struct {
98 /// Pointer to an array of pointer to some object type description struct.
99 void** list;
100 /// Offset of the object type name (char*) in the description struct.
101 void* name_off;
102 /// Offset of the object type info string (char*) in the description struct.
103 void* info_off;
104 /// \brief Offset of the object type parameter description (\ref m_struct_st)
105 /// in the description struct.
106 void* desc_off;
107 } m_obj_list_t;
109 /// The data type used by \ref m_option_type_obj_settings_list.
110 typedef struct m_obj_settings {
111 /// Type of the object.
112 char* name;
113 /// NULL terminated array of parameter/value pairs.
114 char** attribs;
115 } m_obj_settings_t;
117 /// A parser to set up a list of objects.
118 /** It creates a NULL terminated array \ref m_obj_settings. The option priv
119 * field (\ref m_option::priv) must point to a \ref m_obj_list_t describing
120 * the available object types.
122 extern const m_option_type_t m_option_type_obj_settings_list;
124 /// Extra definition needed for \ref m_option_type_obj_presets options.
125 typedef struct {
126 /// Description of the struct holding the presets.
127 struct m_struct_st* in_desc;
128 /// Description of the struct that should be set by the presets.
129 struct m_struct_st* out_desc;
130 /// Pointer to an array of structs defining the various presets.
131 void* presets;
132 /// Offset of the preset's name inside the in_struct.
133 void* name_off;
134 } m_obj_presets_t;
136 /// Set several fields in a struct at once.
137 /** For this two struct descriptions are used. One for the struct holding the
138 * preset and one for the struct beeing set. Every field present in both
139 * structs will be copied from the preset struct to the destination one.
140 * The option priv field (\ref m_option::priv) must point to a correctly
141 * filled \ref m_obj_presets_t.
143 extern const m_option_type_t m_option_type_obj_presets;
145 /// Parse an URL into a struct.
146 /** The option priv field (\ref m_option::priv) must point to a
147 * \ref m_struct_st describing which fields of the URL must be used.
149 extern const m_option_type_t m_option_type_custom_url;
151 /// Extra definition needed for \ref m_option_type_obj_params options.
152 typedef struct {
153 /// Field descriptions.
154 const struct m_struct_st* desc;
155 /// Field separator to use.
156 char separator;
157 } m_obj_params_t;
159 /// Parse a set of parameters.
160 /** Parameters are separated by the given separator and each one
161 * successively sets a field from the struct. The option priv field
162 * (\ref m_option::priv) must point to a \ref m_obj_params_t.
164 extern const m_option_type_t m_option_type_obj_params;
166 typedef struct {
167 int start;
168 int end;
169 } m_span_t;
170 /// Ready made settings to parse a \ref m_span_t with a start-end syntax.
171 extern const m_obj_params_t m_span_params_def;
173 struct m_opt_choice_alternatives {
174 char *name;
175 int value;
179 // FIXME: backward compatibility
180 #define CONF_TYPE_FLAG (&m_option_type_flag)
181 #define CONF_TYPE_INT (&m_option_type_int)
182 #define CONF_TYPE_INT64 (&m_option_type_int64)
183 #define CONF_TYPE_FLOAT (&m_option_type_float)
184 #define CONF_TYPE_DOUBLE (&m_option_type_double)
185 #define CONF_TYPE_STRING (&m_option_type_string)
186 #define CONF_TYPE_FUNC (&m_option_type_func)
187 #define CONF_TYPE_FUNC_PARAM (&m_option_type_func_param)
188 #define CONF_TYPE_PRINT (&m_option_type_print)
189 #define CONF_TYPE_PRINT_INDIRECT (&m_option_type_print_indirect)
190 #define CONF_TYPE_PRINT_FUNC (&m_option_type_print_func)
191 #define CONF_TYPE_FUNC_FULL (&m_option_type_func_full)
192 #define CONF_TYPE_SUBCONFIG (&m_option_type_subconfig)
193 #define CONF_TYPE_STRING_LIST (&m_option_type_string_list)
194 #define CONF_TYPE_POSITION (&m_option_type_position)
195 #define CONF_TYPE_IMGFMT (&m_option_type_imgfmt)
196 #define CONF_TYPE_AFMT (&m_option_type_afmt)
197 #define CONF_TYPE_SPAN (&m_option_type_span)
198 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
199 #define CONF_TYPE_OBJ_PRESETS (&m_option_type_obj_presets)
200 #define CONF_TYPE_CUSTOM_URL (&m_option_type_custom_url)
201 #define CONF_TYPE_OBJ_PARAMS (&m_option_type_obj_params)
202 #define CONF_TYPE_TIME (&m_option_type_time)
203 #define CONF_TYPE_TIME_SIZE (&m_option_type_time_size)
205 /////////////////////////////////////////////////////////////////////////////////////////////
207 /// Option type description
208 struct m_option_type {
209 const char* name;
210 /// Syntax description, etc
211 const char* comments;
212 /// Size needed for the data.
213 unsigned int size;
214 /// See \ref OptionTypeFlags.
215 unsigned int flags;
217 /// Parse the data from a string.
218 /** It is the only required function, all others can be NULL.
220 * \param opt The option that is parsed.
221 * \param name The full option name.
222 * \param param The parameter to parse.
223 * \param dst Pointer to the memory where the data should be written.
224 * If NULL the parameter validity should still be checked.
225 * \param src Source of the option, see \ref OptionParserModes.
226 * \return On error a negative value is returned, on success the number of arguments
227 * consumed. For details see \ref OptionParserReturn.
229 int (*parse)(const m_option_t* opt,const char *name, const char *param, void* dst, int src);
231 /// Print back a value in string form.
232 /** \param opt The option to print.
233 * \param val Pointer to the memory holding the data to be printed.
234 * \return An allocated string containing the text value or (void*)-1
235 * on error.
237 char* (*print)(const m_option_t* opt, const void* val);
239 /** \name
240 * These functions are called to save/set/restore the status of the
241 * variables. The difference between the 3 only matters for types like
242 * \ref m_option_type_func where 'setting' needs to do more than just
243 * copying some data.
245 //@{
247 /// Update a save slot (dst) from the current value in the program (src).
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 (*save)(const m_option_t* opt,void* dst, const void* src);
254 /// Set the value in the program (dst) from a save slot.
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 (*set)(const m_option_t* opt,void* dst, const void* src);
261 /// Copy the data between two save slots. If NULL and size is > 0 a memcpy will be used.
262 /** \param opt The option to copy.
263 * \param dst Pointer to the destination memory.
264 * \param src Pointer to the source memory.
266 void (*copy)(const m_option_t* opt,void* dst, const void* src);
267 //@}
269 /// Free the data allocated for a save slot.
270 /** This is only needed for dynamic types like strings.
271 * \param dst Pointer to the data, usually a pointer that should be freed and
272 * set to NULL.
274 void (*free)(void* dst);
277 ///@}
279 /// Option description
280 /** \ingroup Options
282 struct m_option {
283 /// Option name.
284 const char *name;
286 /// Reserved for higher level APIs, it shouldn't be used by parsers.
287 /** The suboption parser and func types do use it. They should instead
288 * use the priv field but this was inherited from older versions of the
289 * config code.
291 void *p;
293 /// Option type.
294 const m_option_type_t* type;
296 /// See \ref OptionFlags.
297 unsigned int flags;
299 /// \brief Mostly useful for numeric types, the \ref M_OPT_MIN flags must
300 /// also be set.
301 double min;
303 /// \brief Mostly useful for numeric types, the \ref M_OPT_MAX flags must
304 /// also be set.
305 double max;
307 /// Type dependent data (for all kinds of extended settings).
308 /** This used to be a function pointer to hold a 'reverse to defaults' func.
309 * Now it can be used to pass any type of extra args needed by the parser.
310 * Passing a 'default func' is still valid for all func based option types.
312 void* priv;
314 int new;
316 int offset;
320 /// \defgroup OptionFlags Option flags
321 ///@{
323 /// The option has a minimum set in \ref m_option::min.
324 #define M_OPT_MIN (1<<0)
326 /// The option has a maximum set in \ref m_option::max.
327 #define M_OPT_MAX (1<<1)
329 /// The option has a minimum and maximum in \ref m_option::min and \ref m_option::max.
330 #define M_OPT_RANGE (M_OPT_MIN|M_OPT_MAX)
332 /// The option is forbidden in config files.
333 #define M_OPT_NOCFG (1<<2)
335 /// The option is forbidden on the command line.
336 #define M_OPT_NOCMD (1<<3)
338 /// The option is global in the \ref Config.
339 /** It won't be saved on push and the command line parser will set it when
340 * it's parsed (i.e. it won't be set later)
341 * e.g options : -v, -quiet
343 #define M_OPT_GLOBAL (1<<4)
345 /// The \ref Config won't save this option on push.
346 /** It won't be saved on push but the command line parser will add it with
347 * its entry (i.e. it may be set later)
348 * e.g options : -include
350 #define M_OPT_NOSAVE (1<<5)
352 /// \brief The \ref Config will emulate the old behavior by pushing the
353 /// option only if it was set by the user.
354 #define M_OPT_OLD (1<<6)
356 /// The option should be set during command line pre-parsing
357 #define M_OPT_PRE_PARSE (1<<7)
359 /// \defgroup OldOptionFlags Backward compatibility
361 /// These are kept for compatibility with older code.
362 /// @{
363 #define CONF_MIN M_OPT_MIN
364 #define CONF_MAX M_OPT_MAX
365 #define CONF_RANGE M_OPT_RANGE
366 #define CONF_NOCFG M_OPT_NOCFG
367 #define CONF_NOCMD M_OPT_NOCMD
368 #define CONF_GLOBAL M_OPT_GLOBAL
369 #define CONF_NOSAVE M_OPT_NOSAVE
370 #define CONF_OLD M_OPT_OLD
371 #define CONF_PRE_PARSE M_OPT_PRE_PARSE
372 ///@}
374 ///@}
376 /// \defgroup OptionTypeFlags Option type flags
377 /// \ingroup OptionTypes
379 /// These flags are used to describe special parser capabilities or behavior.
381 ///@{
383 /// Suboption parser flag.
384 /** When this flag is set, m_option::p should point to another m_option
385 * array. Only the parse function will be called. If dst is set, it should
386 * create/update an array of char* containg opt/val pairs. The options in
387 * the child array will then be set automatically by the \ref Config.
388 * Also note that suboptions may be directly accessed by using
389 * -option:subopt blah.
391 #define M_OPT_TYPE_HAS_CHILD (1<<0)
393 /// Wildcard matching flag.
394 /** If set the option type has a use for option names ending with a *
395 * (used for -aa*), this only affects the option name matching.
397 #define M_OPT_TYPE_ALLOW_WILDCARD (1<<1)
399 /// Dynamic data type.
400 /** This flag indicates that the data is dynamically allocated (m_option::p
401 * points to a pointer). It enables a little hack in the \ref Config wich
402 * replaces the initial value of such variables with a dynamic copy in case
403 * the initial value is statically allocated (pretty common with strings).
405 #define M_OPT_TYPE_DYNAMIC (1<<2)
407 /// Indirect option type.
408 /** If this is set the parse function doesn't directly return
409 * the wanted thing. Options use this if for some reasons they have to wait
410 * until the set call to be able to correctly set the target var.
411 * So for those types new values must first be parsed, then set to the target
412 * var. If this flag isn't set then new values can be parsed directly to the
413 * target var. It's used by the callback-based options as the callback call
414 * may append later on.
416 #define M_OPT_TYPE_INDIRECT (1<<3)
418 ///@}
420 ///////////////////////////// Parser flags ////////////////////////////////////////
422 /// \defgroup OptionParserModes Option parser modes
423 /// \ingroup Options
425 /// Some parsers behave differently depending on the mode passed in the src
426 /// parameter of m_option_type::parse. For example the flag type doesn't take
427 /// an argument when parsing from the command line.
428 ///@{
430 /// Set when parsing from a config file.
431 #define M_CONFIG_FILE 0
432 /// Set when parsing command line arguments.
433 #define M_COMMAND_LINE 1
434 /// Set when pre-parsing the command line
435 #define M_COMMAND_LINE_PRE_PARSE 2
437 ///@}
439 /// \defgroup OptionParserReturn Option parser return code
440 /// \ingroup Options
442 /// On success parsers return the number of arguments consumed: 0 or 1.
444 /// To indicate that MPlayer should exit without playing anything,
445 /// parsers return M_OPT_EXIT minus the number of parameters they
446 /// consumed: \ref M_OPT_EXIT or \ref M_OPT_EXIT-1.
448 /// On error one of the following (negative) error codes is returned:
449 ///@{
451 /// For use by higher level APIs when the option name is invalid.
452 #define M_OPT_UNKNOWN -1
454 /// Returned when a parameter is needed but wasn't provided.
455 #define M_OPT_MISSING_PARAM -2
457 /// Returned when the given parameter couldn't be parsed.
458 #define M_OPT_INVALID -3
460 /// \brief Returned if the value is "out of range". The exact meaning may
461 /// vary from type to type.
462 #define M_OPT_OUT_OF_RANGE -4
464 /// Returned if the parser failed for any other reason than a bad parameter.
465 #define M_OPT_PARSER_ERR -5
467 /// Returned when MPlayer should exit. Used by various help stuff.
468 /** M_OPT_EXIT must be the lowest number on this list.
470 #define M_OPT_EXIT -6
472 /// \defgroup OldOptionParserReturn Backward compatibility
474 /// These are kept for compatibility with older code.
476 ///@{
477 #define ERR_NOT_AN_OPTION M_OPT_UNKNOWN
478 #define ERR_MISSING_PARAM M_OPT_MISSING_PARAM
479 #define ERR_OUT_OF_RANGE M_OPT_OUT_OF_RANGE
480 #define ERR_FUNC_ERR M_OPT_PARSER_ERR
481 ///@}
483 ///@}
485 /// Find the option matching the given name in the list.
486 /** \ingroup Options
487 * This function takes the possible wildcards into account (see
488 * \ref M_OPT_TYPE_ALLOW_WILDCARD).
490 * \param list Pointer to an array of \ref m_option.
491 * \param name Name of the option.
492 * \return The matching option or NULL.
494 const m_option_t* m_option_list_find(const m_option_t* list,const char* name);
496 static inline void *m_option_get_ptr(const struct m_option *opt,
497 void *optstruct)
499 return opt->new ? (char *) optstruct + opt->offset : opt->p;
502 /// Helper to parse options, see \ref m_option_type::parse.
503 inline static int
504 m_option_parse(const m_option_t* opt,const char *name, const char *param, void* dst, int src) {
505 return opt->type->parse(opt,name,param,dst,src);
508 /// Helper to print options, see \ref m_option_type::print.
509 inline static char*
510 m_option_print(const m_option_t* opt, const void* val_ptr) {
511 if(opt->type->print)
512 return opt->type->print(opt,val_ptr);
513 else
514 return (char*)-1;
517 /// Helper around \ref m_option_type::copy.
518 inline static void
519 m_option_copy(const m_option_t* opt,void* dst, const void* src) {
520 if(opt->type->copy)
521 opt->type->copy(opt,dst,src);
522 else if(opt->type->size > 0)
523 memcpy(dst,src,opt->type->size);
526 /// Helper around \ref m_option_type::free.
527 inline static void
528 m_option_free(const m_option_t* opt,void* dst) {
529 if(opt->type->free)
530 opt->type->free(dst);
533 /*@}*/
536 * Parse a string as a timestamp.
538 * @param[in] str the string to parse.
539 * @param[out] time parsed time.
540 * @param[in] endchar return an error of the next character after the
541 * timestamp is neither nul nor endchar.
542 * @return Number of chars in the timestamp.
544 int parse_timestring(const char *str, double *time, char endchar);
546 #define OPT_FLAG_ON(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 0, 1, NULL, 1, offsetof(struct MPOpts, varname)}
547 #define OPT_FLAG_OFF(optname, varname, flags) {optname, NULL, &m_option_type_flag, flags, 1, 0, NULL, 1, offsetof(struct MPOpts, varname)}
548 #define OPT_MAKE_FLAGS(optname, varname, flags) OPT_FLAG_ON(optname, varname, flags), OPT_FLAG_OFF("no" optname, varname, flags)
549 #define OPT_FLAG_CONSTANTS(optname, varname, flags, offvalue, value) {optname, NULL, &m_option_type_flag, flags, offvalue, value, NULL, 1, offsetof(struct MPOpts, varname)}
550 #define OPT_STRINGLIST(optname, varname, flags) {optname, NULL, &m_option_type_string_list, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
551 #define OPT_INT(optname, varname, flags) {optname, NULL, &m_option_type_int, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
552 #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)}
553 #define OPT_INTPAIR(optname, varname, flags) {optname, NULL, &m_option_type_intpair, (flags), 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
554 #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)}
555 #define OPT_STRING(optname, varname, flags) {optname, NULL, &m_option_type_string, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
556 #define OPT_SETTINGSLIST(optname, varname, flags, objlist) {optname, NULL, &m_option_type_obj_settings_list, flags, 0, 0, objlist, 1, offsetof(struct MPOpts, varname)}
557 #define OPT_AUDIOFORMAT(optname, varname, flags) {optname, NULL, &m_option_type_afmt, flags, 0, 0, NULL, 1, offsetof(struct MPOpts, varname)}
558 #define OPT_HELPER_REMOVEPAREN(...) __VA_ARGS__
559 #define OPT_CHOICE(optname, varname, flags, choices) {optname, NULL, &m_option_type_choice, flags, 0, 0, &(const struct m_opt_choice_alternatives[]){OPT_HELPER_REMOVEPAREN choices, {NULL}}, 1, offsetof(struct MPOpts, varname)}
561 #endif /* MPLAYER_M_OPTION_H */